Résoudre un nom DNS en adresse IP en utilisant WS2_32 (dll winsock)
# 0000000043
ajouté le 04/11/2004 01:53:55 et modifié le 30/03/2005
consulté 12606 fois
Niveau
expert
retourne l'adresse IP d'un nom dns comme www.atoutfox.org en utilisant la DLL winsock
Cordialement
Francis FAURE
Code source :
* ------------- * Résoudre un nom DNS en adresse IP en utilisant WS2_32 * par Francis FAURE * basé sur ressources : Microsoft.com, Fox.wikis.com * -------------
Function GetIP(p_host) local v_Struct, v_Buffer, v_IP, v_r, v_i
p_host = Alltrim(p_host)
v_r = ""
#DEFINE HOSTENT_SIZE 16 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/hostent_2.asp * typedef struct hostent { * char FAR* h_name; * char FAR FAR** h_aliases; * short h_addrtype; * short h_length; * char FAR FAR** h_addr_list; * } hostent; *
* ------------- * initialisation WS2_32.DLL * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsastartup_2.asp * * ------------- DECLAREINTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING lpWSAData if WSAStartup(0x202, Repli(Chr(0),512)) <> 0
v_r = "0.0.0.0, Impossible d'initialiser WinSock (WS2_32.DLL) sur cette machine." waitwindows v_r else * ------------- * appel gethostname * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/gethostbyname_2.asp * DECLAREINTEGER gethostbyname IN ws2_32 STRING host
v_Struct = gethostbyname(p_host) * ------------- * analyse if v_Struct = 0
v_r = "0.0.0.0, host : "+p_host+" résolution IP impossible. Vérifier votre paramètre nom ou vos DNS." waitwindows v_r else
v_Buffer = Repli(Chr(0), HOSTENT_SIZE)
v_IP = Chr(0) + Chr(0) + Chr(0) +Chr(0) * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/kmarch/hh/kmarch/k109_5731ba5b-a7a5-4883-87cf-543768a29a93.xml.asp * DECLARE RtlMoveMemory IN kernel32 STRING @Destination, INTEGER Source, INTEGER Longueur
=RtlMoveMemory(@v_Buffer, v_Struct, HOSTENT_SIZE) * on s'intéresse donc au octets 13 14 15 et 16
=RtlMoveMemory(@v_ip, b2dword(SubStr(v_Buffer,13,4)), 4)
=RtlMoveMemory(@v_ip, b2dword(v_ip), 4) * ------------- * retour sous forme de chaine * ------------- for v_i=1 to 4
v_r = v_r + Alltrim(str(Asc(Substr(v_ip,v_i,1)),3,0))+ Iif(v_i<4,".","") next cleardlls RtlMoveMemory endif * ------------- * termine l'utilisation de WS2_32 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsacleanup_2.asp * DECLAREINTEGER WSACleanup IN ws2_32
=WSACleanup() cleardlls WSACleanup cleardlls gethostbyname endif cleardlls WSAStartup return v_r * ------------- FUNCTION b2dword(p_buffer) RETURNAsc(substr(p_buffer, 1,1)) + Bitlshift(Asc(substr(p_buffer, 2,1)),8) + Bitlshift(Asc(substr(p_buffer, 3,1)),16) + Bitlshift(Asc(substr(p_buffer, 4,1)),24) * -------------
Commentaires
le 05/11/2004, Olivier Hamou a écrit : C'est impeccable francis ,
C'est ce que je recherchais mais peut on obtenir l'adresse wan internet de notre reseau (l'adresse ip internet).
Olivier
le 05/11/2004, Francis Faure a écrit : Olivier, Pour ton adresse externe c'est plus simple : récupére le contenu du fichier http://www.atoutfox.org/ip.asp
C'est impeccable francis ,
C'est ce que je recherchais mais peut on obtenir l'adresse wan internet
de notre reseau (l'adresse ip internet).
Olivier