Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gethostbyname returns only single IP address

605 views
Skip to first unread message

Calista

unread,
Apr 22, 2004, 11:43:54 AM4/22/04
to
Hi,

I use gethostbyname() to get all the IP addresses on my machine. But
it only returns the first one in /etc/hosts file.

Should I do anything different?

I am running "DEC1 V5.1 1885 alpha".

Thanks,
Calista.

Stuart Fuller

unread,
Apr 22, 2004, 9:42:26 PM4/22/04
to
Calista wrote:

> Hi,
>
> I use gethostbyname() to get all the IP addresses on my machine. But
> it only returns the first one in /etc/hosts file.
>
> Should I do anything different?
>

Post exactly what you're doing, what you expect to see, and what you're
seeing.

--

Stu

Calista

unread,
Apr 23, 2004, 9:04:41 AM4/23/04
to
hostname is passed to this function.

if ((hp = gethostbyname(hostname)) <= (struct hostent *)0
return -1;

for( i = 0; i < 4; i++ ) { /* I can have max 4 ip addresses */
if( hp->h_addr_list[i] == NULL )
break;
memzero (&sin, sizeof(sin));
memcpy (hp->h_addr_list[i], &sin.sin_addr, hp->h_length);
sin.sin_family = hp->h_addrtype;
/* Get IP address. */
strncpy( ipaddress[i], inet_ntoa(sin.sin_addr), length );
/* take care of NULL character at end in ipaddress[i] */
}

Now I print all IP addresses. It prints only one IP address although my
machine has multiple interfaces (two ip addresses).

Let me know if you need more info.

Calista.


Stuart Fuller <stuf...@usa.net> wrote in message news:<2as96c...@dadsys2.fuller.local>...

Calista

unread,
Apr 23, 2004, 9:15:36 AM4/23/04
to
Here is some more info if you need.

This is the function prototype

getipaddresses( char *hostname, char **p_ipaddress, int length )

All memories are properly malloced. length is the maximum length
for an IP address. I use this so that I don't go out of bounds.

I think the function is doing what it is supposed to. Nothing wrong with it.
The problem might be in the configuration options for the multi homed machine.

Thanks.
Calista.

Rick Jones

unread,
Apr 23, 2004, 4:43:28 PM4/23/04
to
Calista <calist...@yahoo.com> wrote:
> I use gethostbyname() to get all the IP addresses on my machine. But
> it only returns the first one in /etc/hosts file.

> Should I do anything different?

I thought that when name resolution was accomplished via /etc/hosts
only one address was returned and it was only with DNS or whatnot that
one would get multiple IPs.

At least under HP-UX, the manpage for gethostbyname reads (in part):

Nonserver Operation
If the /etc/hosts file is used for name or address resolution, then
the function:

gethostent() Reads the next line of /etc/hosts, opening
the file if necessary.

sethostent() Opens and rewinds the file. If the
stayopen flag is non-zero, the host data
base is not closed after each call to
gethostent() (either directly or
indirectly through one of the other
gethost calls).

endhostent() Closes the file.

gethostbyname() Sequentially searches from the beginning
of the file until a host name (among
either the official names or the aliases)
matching its name parameter is found, or
until EOF is encountered. Names are
matched without respect to uppercase or
lowercase, as described above in the name
server case.

gethostbyaddr() Sequentially searches from the beginning
of the file until an Internet address
matching its addr parameter is found, or
until EOF is encountered.

Which suggests that gethostbyname() stops after the first match. Now,
whether the Tru64 manpage says the same I don't know.

rick jones
--
Wisdom Teeth are impacted, people are affected by the effects of events.
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to raj in cup.hp.com but NOT BOTH...

Peter da Silva

unread,
Apr 23, 2004, 9:32:44 PM4/23/04
to
In article <5d1678ff.04042...@posting.google.com>,

Calista <calist...@yahoo.com> wrote:
> hostname is passed to this function.

The name "gethostbyname" is deceptive, because in DNS hosts don't
have name, interfaces do.

Typically the interfaces on a multihomed host are given a name like:

hostname.subnet1.dom.ain
hostname.subnet2.dom.ain

By convention, one of these interfaces is often considered the "primary"
interface, and the machine is referred to by that name, but there's no
such thing in DNS.

> Now I print all IP addresses. It prints only one IP address although my
> machine has multiple interfaces (two ip addresses).

DNS doesn't know that the machine has multiple interfaces, though.

You need to talk to the people who designed the DNS namespace to find out
whether there's some name that's had both interfaces assigned to it (for
example, for quick-and-dirty round-robin load-balancing).

--
I've seen things you people can't imagine. Chimneysweeps on fire over the roofs
of London. I've watched kite-strings glitter in the sun at Hyde Park Gate. All
these things will be lost in time, like chalk-paintings in the rain. `-_-'
Time for your nap. | Peter da Silva | Har du kramat din varg, idag? 'U`

Brian Haley

unread,
Apr 26, 2004, 10:05:00 AM4/26/04
to
I'm not sure gethostbyname() will return what you're looking for.

You said the system has two interfaces with two IP addresses, I'm
assuming each has a name associated with it, for example:

10.0.0.1 foo # address on ee0
11.0.0.1 bar # address on ee1

If I do a gethostbyname("foo") I would only expect to get one IP address
back - the one for foo, the address for bar would not be returned.

The preferred way to get all the local IP address information is to use
ioctl(SIOCGIFCONF, ...).

Have you tried this on a similarly configured system running a different
OS, maybe Linux? Or a different Tru64 version? Is the behavior the same?

-Brian

Calista

unread,
Apr 26, 2004, 4:54:47 PM4/26/04
to
Brian Haley <Brian.Haley@nospam_hp.com> wrote in message news:<gG8jc.345$kV....@news.cpqcorp.net>...

> I'm not sure gethostbyname() will return what you're looking for.
>
> You said the system has two interfaces with two IP addresses, I'm
> assuming each has a name associated with it, for example:
>
> 10.0.0.1 foo # address on ee0
> 11.0.0.1 bar # address on ee1
>
> If I do a gethostbyname("foo") I would only expect to get one IP address
> back - the one for foo, the address for bar would not be returned.
>
> The preferred way to get all the local IP address information is to use
> ioctl(SIOCGIFCONF, ...).
>
> Have you tried this on a similarly configured system running a different
> OS, maybe Linux? Or a different Tru64 version? Is the behavior the same?
>
> -Brian

Yes Brian, I have tried the program on Linux, and there it prints both
of the IP addresses. It doesn't print both on Tru64 though. I suspect
the problem is with how the system is configured, but I have no idea
where\what to look for.

Calista.

Brian Haley

unread,
Apr 26, 2004, 6:51:12 PM4/26/04
to

Calista wrote:

> Yes Brian, I have tried the program on Linux, and there it prints both
> of the IP addresses. It doesn't print both on Tru64 though. I suspect
> the problem is with how the system is configured, but I have no idea
> where\what to look for.

You still haven't given enough configuration information - do the two IP
addresses have different hostnames associated with them? I would not
expect that to return more than one address unless there is some special
DNS configuration.

Just so we're on the same page, I've included an example gethostbyname()
utility I use.

# ./gethostbyname `hostname`

Oh, and my Linux box behaves like Tru64 :)

-Brian


#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void printhostent();
void print_h_errno();

main ( argc, argv )
int argc;
char *argv[];
{
struct hostent *hp;

if ( argc != 2 )
printf("Usage: gethostbyname host\n");
else if ( hp = gethostbyname(argv[1]) ) {
printf("hp = 0x%lx\n", hp);
printhostent(hp);
} else
print_h_errno();

}


void
printhostent ( hp )
struct hostent *hp;
{
char **cpp;
struct in_addr addr;

printf("struct hostent:\n");
printf("\t h_name %s\n",hp->h_name);
cpp = hp->h_aliases;
if ( !*cpp )
printf("\t h_aliases\n");
else {
printf("\t h_aliases %s\n",*cpp);
while ( *++cpp )
printf("\t %s\n",*cpp);
}
printf("\t h_addrtype %d\n",hp->h_addrtype); /*** CONVERT TO
MNEMONIC ***/
printf("\t h_length %d\n",hp->h_length);
cpp = hp->h_addr_list;
if ( !*cpp )
printf("\t h_addr_list\n");
else {
addr.s_addr = **(u_int **)cpp;
printf("\t h_addr_list %s\n",inet_ntoa(addr));
while ( *++cpp ) {
addr.s_addr = **(u_int **)cpp;
printf("\t %s\n",inet_ntoa(addr));
}
}
}


void
print_h_errno()
{
char *msg;

switch(h_errno) {
case HOST_NOT_FOUND:
msg = "HOST_NOT_FOUND";
break;
case TRY_AGAIN:
msg = "TRY_AGAIN";
break;
case NO_RECOVERY:
msg = "NO_RECOVERY";
break;
case NO_ADDRESS:
msg = "NO_ADDRESS";
break;
default:
msg = "unknown error code";
}
printf("h_errno: %s\n",msg);
}

Calista

unread,
Apr 27, 2004, 12:54:06 PM4/27/04
to
Brian,

Here is the info.

/etc/hosts file there are entries like this

xxx.xx.xx.xx myhostname myhostalias1
yyy.yy.yy.yy myhostname myhostalias2

/* PS: actual ip addrs\hostname are replaced with xxx yyy. etc */

So, I do a gethostbyname(myhostname). It returns only xxx.xx.xx.xx, in
other words only the first one listed in /etc/hosts file.

I ran your code on Tru64 that also displays only xxx.xx.xx.xx

So there must be some configuration option that I am missing.
Any ideas on how I can check my DNS configuration?

Thanks Brian.
Calista.

Brian Haley <Brian.Haley@nospam_hp.com> wrote in message news:<408D8D8C.3030801@nospam_hp.com>...

Brian Haley

unread,
Apr 27, 2004, 4:36:32 PM4/27/04
to
Ok, now it's making sense.

Linux has a configuration parameter to control this behavior, from
host.conf(5):

(referring to /etc/host.conf file)

multi Valid values are on and off. If set to on, the resolv+
library will return all valid addresses for a host that
appears in the /etc/hosts file, instead of only the
first. This is off by default, as it may cause a
substantial performance loss at sites with large hosts
files.

The actual default on my Linux system was "on", when I turned it off I
got the same behavior at Tru64.

I don't think there is a similar switch on Tru64 as this doesn't seem to
be part of any standard. You might want to try putting them both in DNS
to see if that works. Sorry, I don't know that particular code very well.

-Brian

Peter da Silva

unread,
May 2, 2004, 11:15:03 PM5/2/04
to
> So there must be some configuration option that I am missing.
> Any ideas on how I can check my DNS configuration?

Your DNS configuration doesn't come into it. /etc/hosts has nothing to do
with DNS. It's what you use instead OF DNS if your nameserver isn't set up.

What is in your /etc/svc.conf and /etc/nsswitch.conf?

0 new messages