gethostbyname returns only one IP address

I am running gethostbyname on a machine that has two (network card interfaces) ip addresses.

ifconfig -a options displays two ip addresses.

But when I do a gethostbyname(). It returns only one. I am running it on
Tru64 Unix. /etc/hosts file has two IP addresses listed for this hostname,

The following is the code:

getipaddresses( char *hostname, char **p_ipaddress, int length )
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 )
   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).
All memories are properly allocated.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

MercantilumConnect With a Mentor Commented:
If you resolve names through DNS you have to update your DNS maps.

This is not as straight forward as hosts...
So edit the map of your domain, and add a second IP address

name IN A firstIP
name IN A secondIP

Maps are usually in /var/named or /var/lib/named
Look at your /etc/named.conf to get configuration and zones description

Not sure if I am talking nonsense here, but surely /etc/hosts can only show the hostname against one IP address. Normally the machine has a second hostname which it uses on the second interface. If /etc/hosts has the same name for to IP addresses, only one will be accepted.
CalistaAuthor Commented:
Well I am not sure. I believe /etc/hosts can have multiple entries for the same host name. A machine has multiple interfaces and different interface names for sure. But it is not mandatory to have a one-to-one relation among host names and ip addresses for interfaces have ip addresses and not the machine.

For e.g. in Linux we set "multi on" option in host.conf file for the gethostbyname() to return multiple IP addresses.

I don't know how to do it in Tru64 Unix.

I am not sure, don't let me lead you into a blind alley. Let me know what you think glassd.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

i could be shooting blanks here, but i think you have to call gethostbyname successively until you receive a NULL, each time you call it, you should get a unique ip... ?

if that doesn't work, you could always use gethostent/sethostent to retrive sets of entries and seek for ip's by name yourself?
I double checked the Stevens book "UNIX Network Programming" and a host can have multiple IP addresses.  Also, your use of the hostent structure is correct.  The h_addr_list member is a pointer to a NULL terminated array of character pointers.  Your code looks fine to me.  I don't have access to your flavor of UNIX so I cannot try it.

I was wondering if your DNS is configured correctly.  Have you tried looking up the IP addresses from another machine?  While a name can be in the /etc/hosts file, it is possible that name resolution on the system is not configured to look in the hosts file.  I would check that out just to help make sure the problem is actually with gethostbyname and not with the system.  It is system dependent as to the file that configures name lookup and I do not know what Tru64 uses.  For example, Solaris 2.x and at least some flavors of linux use nsswitch.conf .  
CalistaAuthor Commented:
Yes I can ping both of the addresses from another machine. On linux we turn on the "multi on" option in /etc/host.conf, but on Tru64 I don't know.
Make sure that /etc/hosts is where hostnames are being looked up (and/or check your
dns config - but that's harder :-). For Tru64, I believe this is configured in
/etc/svc.conf. The first method for hosts should be local if you want to consult /etc/hosts
prior to dns.
CalistaAuthor Commented:
It is checking in the /etc/hosts only. If I change the order of IP addresses it returns the first ip only.

I think some other config parameter needs to be changed. And I have no idea how to set up\check dns.
1. regarding /etc/hosts declaration, ensure you have  ip1  name  and  ip2  name  lines, e.g.    myhost    myhost

(same for DNS, you have in the map of the domain
name    IN A
name    IN A )

2. regarding your program, maybe you could try something simpler:

// returns at most 4 IPs for a given hostname
// usage: myprog  name

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>

int getipaddresses (char *hostname, unsigned char **ip, int maxlength)
  struct hostent *hp;
  int i;

  if ( ! (hp = gethostbyname(hostname))) return -1;
  if (hp->h_length > maxlength) return (-1);

  for( i = 0; i < 4 && hp->h_addr_list[i] ; i++ )
     memcpy (ip[i], hp->h_addr_list[i], hp->h_length);

  return (i);

// example of main.c

int main (int argc, char **argv)
  unsigned char *ip[4],ip1[4][30];
  int i;

  if (argc < 2) return (1);
  for (i=0 ; i<4 ; i++) ip[i] = ip1[i];
  i = getipaddresses(*++argv, ip, 30);
  if (i>0) while (i--)
     printf ("%u.%u.%u.%u\n",
        (unsigned)ip[i][0], (unsigned)ip[i][1], (unsigned)ip[i][2], (unsigned)ip[i][3]);
  return (0);
sunnycoderConnect With a Mentor Commented:

Did you try the suggestion I posted http:Q_20919674.html#10604478 ?
CalistaAuthor Commented:

Yes the /etc/hosts files has both of the ip addrs as you listed and the same is true with the other.

I think the code you have given is functionally similar to the one I have given in the beginning. It is as simple as the one I have posted.

I tried the code now. Yes it works and if I find all the interface names (there must be a system call to do it, I will find that out), then I can get all the ip addresses. But (there is always a but :) ), I still think there must be a configure option for Tru64 machines that would make the gethostbyname() call return all of the ip addresses. It is an existing application and  I want to fix the configuration so that it returns all of the ip addrs and not change the code. Well, if there is no other way out :( then I will probably change the code as you suggested.
Hi Calista,
the code is similar, but it works here, can you try at home?
usage: prog  name
CalistaAuthor Commented:
I tried the code, as expected it printed the first IP address given in /etc/hosts file.

My /etc/hosts file is like this:

xxx.xx.xx.xx myhostname aliasname1
yyy.yy.yy.yy myhostname aliasname2

It prints xxx.xx.xx.xx and when the change the order it prints yyy.yy.yy.yy i.e it always prints the first one.

There is something else that must be configured I think. I had a similar problem on linux then I added the line "multi on" in /etc/host.confm then the application started getting both of the ip address (i had to restart my application though). But whats to be done on Tru64?

Thanks Mercantilum.
CalistaAuthor Commented:

what is the DNS option you have mentioned?

name IN A ipaddr1
name IN A ipaddr2

Which file is that? I thought it to be something else.
Can you provide more info on that one?

CalistaAuthor Commented:
Well, I guess there isn't a straight forward solution to this problem. Mercantilum we can't use DNS on our system (there is some reason).

So how do I close this question? Should I have to award points and then close it? I am willing to do that but as the question is not answered is it okay to give points and close it?

thanks for everyone who tried.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.