hostname to ip

Linux specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: hostname to ip

Post by Shardik »

The following example code displays all IP addresses belonging to a given host name. I have tested the cross-platform code successfully on these operating systems:
- Kubuntu 12.04 x64 with PB 5.11 x64 in ASCII and Unicode mode
- MacOS X 10.6.8 (Snow Leopard) with PB 5.11 x86 and x64 in ASCII and Unicode mode
- Windows 7 SP1 x64 with PB 5.11 x86 and x64 in ASCII and Unicode mode

Code: Select all

CompilerIf Defined(HOSTENT, #PB_Structure) = #False
  Structure hostent
    *h_name
    *h_aliases
    h_addrtype.L
    h_length.L
    *h_addr_list
  EndStructure
CompilerEndIf

InitNetwork()

Procedure GetIPAddressesFromHostname(Hostname.S)
  Protected *HostInfos.hostent
  Protected *HostnameBuffer

  *HostNameBuffer = AllocateMemory(StringByteLength(Hostname, #PB_Ascii))
  PokeS(*HostnameBuffer, Hostname, MemorySize(*HostnameBuffer), #PB_Ascii)
  *HostInfos = gethostbyname_(*HostnameBuffer)
  FreeMemory(*HostnameBuffer)
 
  If *HostInfos = 0
    Debug "Error: unable to find host name!"
  Else
    Debug "Hostname: " + PeekS(*HostInfos\h_name, -1, #PB_Ascii)

    If *HostInfos\h_length = 4
      IPType = #PB_Network_IPv4
    Else
      IPType = #PB_Network_IPv6
    EndIf
   
    Debug "IP addresses:"
    *IP = *HostInfos\h_addr_list
   
    While PeekI(*IP) <> 0
      Debug IPString(PeekI(PeekI(*IP)), IPType)
      *IP + SizeOf(Integer)
    Wend
  EndIf

  Debug ""
EndProcedure

GetIPAddressesFromHostname("www.google.com")
GetIPAddressesFromHostname("www.purebasic.com")
Update: I have modified the previous code to also run on Windows.
Last edited by Shardik on Sat May 11, 2013 3:38 pm, edited 1 time in total.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: hostname to ip

Post by Bisonte »

the function

Code: Select all

gethostbyname_(*HostnameBuffer)
is crossplatform ???
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: hostname to ip

Post by Shardik »

Bisonte wrote:the function

Code: Select all

gethostbyname_(*HostnameBuffer)
is crossplatform ???
No, it's a Unix API function. But the core of MacOS X is a BSD Unix, therefore most Unix API functions also work for MacOS X... :wink:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: hostname to ip

Post by ts-soft »

Shardik wrote:No, it's a Unix API function. But the core of MacOS X is a BSD Unix, therefore most Unix API functions also work for MacOS X... :wink:
And it is a windows-API :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: hostname to ip

Post by Shardik »

ts-soft wrote:And it is a windows-API :wink:
Too slow... :twisted:
Thomas is right. Before being able to reboot from MacOS into Windows 7 and being able to test, he posted already his remark. When looking up the structure hostent I wondered already why the structure was also described in a MSDN link... :lol:

I modified my above code example to also run in Windows without any modification.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: hostname to ip

Post by Bisonte »

I'm impressed ;) The first API call that does on all OS the same ...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: hostname to ip

Post by blueznl »

I could use some help on this one, or perhaps slightly related. I'm updating some old code, and in it I found this:

Code: Select all

  buffer_p = AllocateMemory(Len(name)+1)
  PokeS(buffer_p,name,-1,#PB_Ascii)
  bip_p = gethostbyname_(buffer_p)
  FreeMemory(buffer_p)
  If bip_p <> 0
    ProcedureReturn PeekL(PeekL(PeekL(bip_p+12)))
  Else
    ProcedureReturn 0
  EndIf
I didn't write it myself :-) and I haven't got a clue where I stole it :-)

Anyway, the above works fine on Win32 but not on Win64. I suspect it's the PeekL stuff...

In other words, how to retrieve the BIP (not the IP) from a name on Win64?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: hostname to ip

Post by Shardik »

blueznl wrote:Anyway, the above works fine on Win32 but not on Win64. I suspect it's the PeekL stuff...

In other words, how to retrieve the BIP (not the IP) from a name on Win64?
Your BIP (the result your procedure returns) is the first IP address of the requested host name. In order to work on a 64 bit OS you have to change

Code: Select all

    ProcedureReturn PeekL(PeekL(PeekL(bip_p+12)))
to

Code: Select all

    ProcedureReturn PeekI(PeekI(PeekI(bip_p+24)))
However I would strongly advice to use the HOSTENT structure so you don't have to care about 32 or 64 bit systems:

Code: Select all

InitNetwork()

Procedure Get1stIPAddressOfHostName(name.S)
  buffer_p = AllocateMemory(Len(name)+1)
  PokeS(buffer_p,name,-1,#PB_Ascii)
  *bip_p.HOSTENT = gethostbyname_(buffer_p)
  FreeMemory(buffer_p)
  If *bip_p <> 0
    ProcedureReturn PeekI(PeekI(*bip_p\h_addr_list))
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

MessageRequester("Info", "1st IP address of www.purebasic.com: " +
  IPString(Get1stIPAddressOfHostName("www.purebasic.com"), #PB_Network_IPv4))
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: hostname to ip

Post by bbanelli »

Bisonte wrote:I'm impressed ;) The first API call that does on all OS the same ...
Winsock API follows POSIX socket implementation to very high extent, up to some hassle regarding initialization and cleanup.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Post Reply