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);
}
|