ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Résoudre un nom DNS en adresse IP en utilisant WS2_32 (dll winsock)   



L'auteur

Francis Faure
France France
Membre Actif (personne physique)
# 0000000001
enregistré le 11/10/2004

http://www.wanagain.net
56 ans
Faure Francis
de la société Design Or Decline
Fiche personnelle


Note des membres
pas de note

Contributions > 05 - API et appels systèmes > IP

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é 12778 fois
Niveau expert

Version(s) Foxpro :
VFP 9.0
VFP 8.0
VFP 7.0
VFP 6.0

Description

? GetIp("www.AtoutFox.org")

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
* -------------

* exemples :
clear
? GetIp("www.AtoutFox.org")
? GetIp("www.foxforum.net")
? GetIp("www.foxprofr.com")
? GetIp("news.atoutfox.org")
? GetIp("xfrx.fr")
? GetIp("www.microsoft.com")
? GetIp("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
  *
  * -------------
  DECLARE INTEGER 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."
    wait windows v_r
  else
    * -------------
    * appel gethostname
    * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/gethostbyname_2.asp
    *
    DECLARE INTEGER 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."
      wait windows 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
      clear dlls RtlMoveMemory
    endif
    * -------------
    * termine l'utilisation de WS2_32
    * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsacleanup_2.asp
    *
    DECLARE INTEGER WSACleanup IN ws2_32
    =WSACleanup()
    clear dlls WSACleanup
    clear dlls gethostbyname
  endif
  clear dlls WSAStartup
return v_r
* -------------
FUNCTION b2dword(p_buffer)
RETURN Asc(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

Cordialement
Francis

le 04/02/2005, Francis Faure a écrit :
Olivier,

http://www.atoutfox.org/articles.asp?ACTION=FCONSULTER&ID=0000000134


Publicité

Les pubs en cours :


www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2024.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3