hostname to ip
hostname to ip
Hi, I need to convert hostname to ip in Linux, how can I do this? Snx in advance.
Re: hostname to ip
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
Update: I have modified the previous code to also run on Windows.
- 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")
Last edited by Shardik on Sat May 11, 2013 3:38 pm, edited 1 time in total.
Re: hostname to ip
the function
is crossplatform ???
Code: Select all
gethostbyname_(*HostnameBuffer)
Re: hostname to ip
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...Bisonte wrote:the functionis crossplatform ???Code: Select all
gethostbyname_(*HostnameBuffer)

Re: hostname to ip
And it is a windows-APIShardik 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...

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.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: hostname to ip
Too slow...ts-soft wrote:And it is a windows-API

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

I modified my above code example to also run in Windows without any modification.
Re: hostname to ip
I'm impressed
The first API call that does on all OS the same ...

Re: hostname to ip
I could use some help on this one, or perhaps slightly related. I'm updating some old code, and in it I found this:
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?
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


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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: hostname to ip
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 changeblueznl 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?
Code: Select all
ProcedureReturn PeekL(PeekL(PeekL(bip_p+12)))
Code: Select all
ProcedureReturn PeekI(PeekI(PeekI(bip_p+24)))
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
Winsock API follows POSIX socket implementation to very high extent, up to some hassle regarding initialization and cleanup.Bisonte wrote:I'm impressedThe first API call that does on all OS the same ...