Click to See Complete Forum and Search --> : [SOLVED] hostname to ip address


bryan.6
01-08-2005, 05:11 PM
language: c

i've been searching all day to try to figure out how i can convert a hostname, like "www.justlinux.com" or "www.google.com" to an ip address, like "11.56.203.12" or "226.23.67.100" (names and address not respective, just random numbers). i've tried using gethostbyname, all sorts of inet_?to?'s, but i just can't figure it out. i don't think there's a simple char * hostnameToIPAddress( char * ) like call, but that's basically what i'm trying to do, input a string "www.justlinux.com" and return a string "11.56.203.12". surely this is done all the time, so... can anybody offer me any advice as to where to look? thanks in advance,

bryan

fatTrav
01-08-2005, 07:04 PM
Maybe the source for nslookup or dig would be helpful?

I did some googleing and found examples in java, c++, and c# but not c. I'm sure this is one of those things that is trivial in any language but c.

bryan.6
01-08-2005, 07:53 PM
more looking and i figured it out:
use gethostbyname to convert host into a 'struct hostent'.
struct hostent has h_addr_list[0], which somehow is a 'struct in_addr'.
use inet_ntoa to convert struct in_addr to string.

my line of code:
char host[ 255 ] = "www.someplace.com";
strncpy( host, inet_ntoa( *((struct in_addr*) gethostbyname( host )->h_addr_list[0]) ), 255 );

this may be a little more understandable, but i deleted my examples already, so i can't definitively vouch for the accuracy of this example.
char host[ 255 ] = "www.someplace.com";
struct hostent *my_hostent = gethostbyname( host );
struct in_addr *my_in_addr = (struct in_addr*) my_hostent->h_addr_list[0]);
char* c = inet_ntoa( my_in_addr );
strncpy( host, c, 255 );

soulestream
01-09-2005, 01:51 AM
cant you just use PING?


soule:D

bwkaz
01-09-2005, 03:50 PM
gethostbyname works, but is deprecated according to the POSIX 2001 revision (IEEE 1003.1-2001).

You're supposed to use getaddrinfo instead. This is partly because it has a separate strerror() function (gai_strerror()) so you don't have to use h_errno anymore, and partly because you can specify the address family to get info on (IPv4 or IPv6). But it's mostly because getaddrinfo() is explicitly thread safe (gethostbyname is not, unless you use glibc specific extensions).

Check the manpage for each function for its use, also -- any function in the C library (and many of them outside the C library, such as OpenGL) have manpages.

bryan.6
01-09-2005, 03:57 PM
Originally posted by bwkaz
gethostbyname works, but is deprecated according to the POSIX 2001 revision (IEEE 1003.1-2001).

You're supposed to use getaddrinfo instead. This is partly because it has a separate strerror() function (gai_strerror()) so you don't have to use h_errno anymore, and partly because you can specify the address family to get info on (IPv4 or IPv6). But it's mostly because getaddrinfo() is explicitly thread safe (gethostbyname is not, unless you use glibc specific extensions).

Check the manpage for each function for its use, also -- any function in the C library (and many of them outside the C library, such as OpenGL) have manpages.

i realize most functions have man pages, that's where i get the bulk of my information. thanks for the advice on getaddinfo, i'll look into that when i have some time. (i had to put my project on hold to write some scholarship applications).

thanks again!
bryan