Page 1 of 1

hostname to ip

Posted: Thu Feb 07, 2013 1:06 pm
by FanOfGun
Hi, I need to convert hostname to ip in Linux, how can I do this? Snx in advance.

Re: hostname to ip

Posted: Sat May 11, 2013 11:03 am
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.

Re: hostname to ip

Posted: Sat May 11, 2013 2:11 pm
by Bisonte
the function

Code: Select all

gethostbyname_(*HostnameBuffer)
is crossplatform ???

Re: hostname to ip

Posted: Sat May 11, 2013 2:43 pm
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:

Re: hostname to ip

Posted: Sat May 11, 2013 2:49 pm
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:

Re: hostname to ip

Posted: Sat May 11, 2013 3:00 pm
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.

Re: hostname to ip

Posted: Sat May 11, 2013 10:02 pm
by Bisonte
I'm impressed ;) The first API call that does on all OS the same ...

Re: hostname to ip

Posted: Fri Sep 12, 2014 9:19 pm
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?

Re: hostname to ip

Posted: Sat Sep 13, 2014 4:30 pm
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))

Re: hostname to ip

Posted: Sun Sep 14, 2014 2:30 pm
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.