When would multiple calls to gethostbyname be unsafe?

344 views Asked by At

From gethostbyname(3) - Linux manual

The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls.  Copying the
struct hostent does not suffice, since it contains pointers; a deep
copy is required.

I've written programs that make multiple calls to gethostbyname and haven't had anything break because of overwriting of static data.

Could I ask for an example when multiple calls to gethostbyname would overwrite this static data?

3

There are 3 answers

0
Pablo On BEST ANSWER

It will be a problem when you do something like this:

struct hostent *google = gethostbyname("www.google.com");
struct hostent *youtube = gethostbyname("www.youtube.com");

printf("Official name of host for www.google.com: %s\n", google->h_name);
printf("Official name of host for www.youtube.com: %s\n", youtube->h_name);

printf("same? %s\n", google == youtube ? "yes" : "no");

The output will be

Official name of host for www.google.com: youtube-ui.l.google.com
Official name of host for www.youtube.com: youtube-ui.l.google.com
same? yes

which is wrong, as the official host name of www.google.com is www.google.com and not youtube-ui.l.google.com. The problem is that google and youtube point at the same location (as you can see from the same? yes output), so the information about www.google.com is lost when you execute gethostbyname again.

If you however do

struct hostent *google = gethostbyname("www.google.com");
printf("Official name of host for www.google.com: %s\n", google->h_name);

struct hostent *youtube = gethostbyname("www.youtube.com");
printf("Official name of host for www.youtube.com: %s\n", youtube->h_name);

then the output will be

Official name of host for www.google.com: www.google.com
Official name of host for www.youtube.com: youtube-ui.l.google.com

So as long as you process the hostent pointer of the first gethostbyname call before you do the second call, you will be fine.

1
pm100 On
struct hostent *host1 = gethostbyname("host1");
struct hostent *host2 = gethostbyname("host2");

if(host1......)

the second call has overwritten (possibly) the result of the first one

0
dbush On

Here's an example:

struct hostent *he1 = gethostbyname("host1");
struct in_addr *addr1 = (struct in_addr *)(he1->h_addr);

printf("addr1=%s\n", inet_ntoa(*addr1));    // prints IP1

struct hostent *he2 = gethostbyname("host2");
struct in_addr *addr2 = (struct in_addr *)(he2->h_addr);

printf("addr2=%s\n", inet_ntoa(*addr2));    // prints IP2

printf("addr1=%s\n", inet_ntoa(*addr1));    // prints IP1 (!)