Solved

gethostbyname returns only one IP address

Posted on 2004-04-23
18
837 Views
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:

void
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 )
 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).
All memories are properly allocated.

}
0
Comment
Question by:Calista
  • 7
  • 3
  • 2
  • +4
18 Comments
 
LVL 7

Expert Comment

by:glassd
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.
0
 

Author Comment

by:Calista
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.

Thanks.
0
 
LVL 7

Expert Comment

by:fim32
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?
0
 
LVL 1

Expert Comment

by:ravenscr98
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 .  
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10915736
0
 

Author Comment

by:Calista
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.
0
 

Expert Comment

by:rwhit
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.
0
 

Author Comment

by:Calista
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:Mercantilum
ID: 10925213
1. regarding /etc/hosts declaration, ensure you have  ip1  name  and  ip2  name  lines, e.g.

1.2.3.4    myhost
5.6.7.8    myhost

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


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);
}
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
ID: 10925306
Calista,

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

Author Comment

by:Calista
ID: 10930257
Mercantilum,

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.

Sunnycoder,
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.
0
 
LVL 10

Expert Comment

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

Author Comment

by:Calista
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.
0
 

Author Comment

by:Calista
ID: 10931541
Mercantilum,

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?

Thanks,
Calista.
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 50 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
0
 

Author Comment

by:Calista
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now