Subject: getaddrinfo and numeric IP addresses
To: None <tech-userlevel@netbsd.org>
From: Christoph Kaegi <kgc@zhwin.ch>
List: tech-userlevel
Date: 05/06/2003 07:23:52
I hope, this is not too offtopic:

Why does the following code not do, what the manpage suggests it should?

-------------------------------------- 8< --------------------------------------
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/socket.h>

int
main(void) {
	
	struct addrinfo ai_peer_hints;
	struct addrinfo *ai_peer;
	char *HostIP = "160.85.104.32";
	int gai_error = 0;

	/*--------------------------------------------------------------------*/
	/* Prepare addressinfo structure for peer */
	/*--------------------------------------------------------------------*/
	bzero(&ai_peer_hints, sizeof(struct addrinfo));
	ai_peer_hints.ai_flags = AI_CANONNAME;
	ai_peer_hints.ai_family = PF_INET;
	ai_peer_hints.ai_socktype = SOCK_RAW;
	ai_peer_hints.ai_protocol = IPPROTO_ICMP;
	ai_peer_hints.ai_addrlen = 0;
	ai_peer_hints.ai_canonname = (char *) NULL;
	ai_peer_hints.ai_addr = (struct sockaddr *) NULL;
	ai_peer_hints.ai_next = (struct addrinfo *) NULL;

	gai_error = getaddrinfo(HostIP, (char *) NULL, &ai_peer_hints, &ai_peer);

	if (gai_error != 0) {
		if (gai_error == EAI_SYSTEM) {
			fprintf(stderr, "getaddrinfo error (%s)\n", strerror(errno));
			return(1);
		}
		else {
			fprintf(stderr, "getaddrinfo error (%s)\n", gai_strerror(gai_error));
			return(1);
		}
	}

	fprintf(stdout, "Hostname is %s\n", ai_peer->ai_canonname);
	
	return(0);
}

/*--------------------------------------------------------------------*/
/* End of test.c                                                      */
/*--------------------------------------------------------------------*/
-------------------------------------- 8< --------------------------------------

On my NetBSD 1.6.1 box it says: "Hostname is (null)"
On Solaris it says: "Hostname is 160.85.104.32"

Both do not return the canonical hostname.

Am I missing something?

Chris

-- 
----------------------------------------------------------------------
Christoph Kaegi                                           kgc@zhwin.ch
----------------------------------------------------------------------