It is currently Wed Jun 19, 2013 6:32 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: hostname to ip
PostPosted: Thu Feb 07, 2013 1:06 pm 
Offline
New User
New User

Joined: Thu Feb 07, 2013 1:04 pm
Posts: 1
Hi, I need to convert hostname to ip in Linux, how can I do this? Snx in advance.


Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 11:03 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 821
Location: Germany
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:
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.

Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 2:11 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Oct 09, 2007 2:15 am
Posts: 249
the function
Code:
gethostbyname_(*HostnameBuffer)

is crossplatform ???

_________________
English is not my native language...
ImageImageImage


Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 2:43 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 821
Location: Germany
Bisonte wrote:
the function
Code:
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:


Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 2:49 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4740
Location: Berlin - Germany
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.11 | Windows 7 SP1 (x64) | Linux Mint 15 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 3:00 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 821
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: hostname to ip
PostPosted: Sat May 11, 2013 10:02 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Oct 09, 2007 2:15 am
Posts: 249
I'm impressed ;) The first API call that does on all OS the same ...

_________________
English is not my native language...
ImageImageImage


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye