Networking Forums

Networking Forums > Computer Networking > Linux Networking > Why does getaddrinfo return only one address?

Reply
Thread Tools Display Modes

Why does getaddrinfo return only one address?

 
 
David
Guest
Posts: n/a

 
      12-16-2003, 08:21 PM
Any guesses why the following program works fine on Solaris, but not
on linux?

/*
* Author: David Lehmann 3/27/2003
* Purpose: Demonstrate that getaddrinfo returns one address.
*
* If /etc/hosts contains the following two entries, BOTH should be
* obtained by getaddinfo. Currently, only the first entry is obtained.
* This identical program works fine on Solaris.
*
* 172.25.4.94 fred fred-1
* 10.2.89.3 fred fred-2
*/


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

void print_addr(char *host, int pf);
void print_ip_addr(struct addrinfo *a, int count);

int
main(int argc, char **argv)
{
char *host = argv[1];

if (!host)
{
fprintf(stderr, "Usage: %s host\n", argv[0]);
exit(1);
}

printf("host %s, searching for IPv4 addresses...\n", argv[0]);
print_addr(host, PF_INET);

printf("host %s, searching for IPv6 addresses...\n", argv[0]);
print_addr(host, PF_INET6);

return 0;
}

void
print_addr(char *host, int pf)
{
struct addrinfo *res = 0;
struct addrinfo hints; /* see getaddrinfo */
int error;
int i;

memset(&hints, 0, sizeof(hints));
hints.ai_family = pf;
hints.ai_socktype = SOCK_RAW;
error = getaddrinfo(host, NULL, &hints, &res);

if (error)
{
fprintf(stderr, "getaddrinfo failed: %d - %s\n",
error, gai_strerror(error));
return;
}

print_ip_addr(res, 0);
freeaddrinfo(res);
}

void
print_ip_addr(struct addrinfo *a, int count)
{
char buf[INET6_ADDRSTRLEN + 1];
void *addr;

if (!a)
return;

if (a->ai_family == AF_INET)
addr = &((struct sockaddr_in *)a->ai_addr)->sin_addr;
else
addr = &((struct sockaddr_in6 *)a->ai_addr)->sin6_addr;

inet_ntop(a->ai_family, addr, buf, sizeof(buf));
printf("%d: %s\n", count, buf);

print_ip_addr(a->ai_next, count + 1);
}
 
Reply With Quote
 
 
 
 
P.T. Breuer
Guest
Posts: n/a

 
      12-19-2003, 08:50 AM
David <(E-Mail Removed)> wrote:
> Any guesses why the following program works fine on Solaris, but not
> on linux?


cut it down to 10 lines at most and people may have an opinion.
Add your running tests, debugging info, and people may have some data.

> * If /etc/hosts contains the following two entries, BOTH should be
> * obtained by getaddinfo. Currently, only the first entry is obtained.
> * This identical program works fine on Solaris.
> *
> * 172.25.4.94 fred fred-1
> * 10.2.89.3 fred fred-2


Those entries are incorrect. man hosts. It says:

For each host a single line should be present with the following
information:
IP_address canonical_hostname aliases

You have multiple lines for fred, and fred is not a FQDN either, if you
care, so the canonicalisation is off.

I've never heard of getaddinfo. The standard way of interrogating the
resolver tables is with gethostbyname:


The domain name queries carried out by gethostbyname() and
gethostbyaddr() use a combination of any or all of the name
server named(8), a broken out line from /etc/hosts, and the
Network Information Service (NIS or YP), depending upon the
contents of the order line in /etc/host.conf. (See resolv+(8)).
The default action is to query named(8), followed by /etc/hosts.




Peter
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getaddrinfo return only one IP address Rajesh Gupta Windows Networking 0 07-22-2008 11:59 PM
can't get multi-homed host address using getaddrinfo() Options Arthur Linux Networking 2 07-20-2007 09:02 AM
Is it possible for someone to use a fake return email address? mail1227418@lawrabbit.com Linux Networking 10 03-17-2007 12:34 PM
wrong return address John I-Chung Wang Linux Networking 0 10-27-2004 05:50 AM
Spammer is using my domain as a return address (HELP) Anthony Broadband 43 11-20-2003 07:33 PM



1 2 3 4 5 6 7 8 9 10 11