[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


gethostbyname returns only one IP address

Posted on 2004-04-23
Medium Priority
Last Modified: 2013-12-26
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.

Question by:Calista
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +4

Expert Comment

ID: 10902819
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.

Author Comment

ID: 10903549
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.


Expert Comment

ID: 10904167
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?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 10906207
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 .  
LVL 45

Expert Comment

ID: 10915736

Author Comment

ID: 10919690
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.

Expert Comment

ID: 10920359
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.

Author Comment

ID: 10920984
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.
LVL 10

Expert Comment

ID: 10925213
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);
LVL 45

Assisted Solution

sunnycoder earned 200 total points
ID: 10925306

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

Author Comment

ID: 10930257

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.
LVL 10

Expert Comment

ID: 10930304
Hi Calista,
the code is similar, but it works here, can you try at home?
usage: prog  name

Author Comment

ID: 10931466
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.

Author Comment

ID: 10931541

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?

LVL 10

Accepted Solution

Mercantilum earned 200 total points
ID: 10934723
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

See http://www.charvolant.org/~doug/network/html/node10.html#SECTION00064200000000000000

Author Comment

ID: 11063052
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.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question