Question

gethostbyname returns only one IP address

Asked by: Calista

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.

}

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-04-23 at 11:55:40ID20965482
Tags

gethostbyname

Topic

Unix Systems Programming

Participating Experts
6
Points
100
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. gethostbyname weirdness
    I have been stumped for over a week with this one, have scoured the net for hours, and finally it has brought me here. I signed up with experts exchange for this question, so I hope somebody can help me. I am writing a sockets program for windows using VC++ and winsock wher...
  2. How to get multiple local IP addresses without gethostbyna…
    Hi, I am looking for a way to resolve all IP addresses on a local Unix/Linux system without using gethostbyname, since it has proven unable. For me, gethostbyname returns only one IP while ifconfig -a displays two. I have read that gethostbyname can be fixed through config f...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: glassdPosted on 2004-04-23 at 12:01:55ID: 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.

 

by: CalistaPosted on 2004-04-23 at 13:20:46ID: 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.

 

by: fim32Posted on 2004-04-23 at 14:54:45ID: 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?

 

by: ravenscr98Posted on 2004-04-23 at 21:56:17ID: 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 .  

 

by: sunnycoderPosted on 2004-04-25 at 23:06:57ID: 10915736

 

by: CalistaPosted on 2004-04-26 at 08:57:07ID: 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.

 

by: rwhitPosted on 2004-04-26 at 09:58:15ID: 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.

 

by: CalistaPosted on 2004-04-26 at 11:04:38ID: 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.

 

by: MercantilumPosted on 2004-04-26 at 21:50:27ID: 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);
}

 

by: sunnycoderPosted on 2004-04-26 at 22:08:01ID: 10925306

Calista,

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

 

by: CalistaPosted on 2004-04-27 at 09:21:05ID: 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.

 

by: MercantilumPosted on 2004-04-27 at 09:25:38ID: 10930304

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

 

by: CalistaPosted on 2004-04-27 at 11:16:30ID: 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.

 

by: CalistaPosted on 2004-04-27 at 11:23:04ID: 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.

 

by: MercantilumPosted on 2004-04-27 at 19:01:46ID: 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

 

by: CalistaPosted on 2004-05-13 at 13:25:06ID: 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.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...