1272343Sngie/*	$NetBSD: h_gai.c,v 1.1 2011/01/12 02:58:40 pgoyette Exp $	*/
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie * 3. Neither the name of the project nor the names of its contributors
16272343Sngie *    may be used to endorse or promote products derived from this software
17272343Sngie *    without specific prior written permission.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29272343Sngie * SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <netdb.h>
33272343Sngie#include <stdio.h>
34272343Sngie#include <stdlib.h>
35272343Sngie#include <string.h>
36272343Sngie#include <unistd.h>
37272343Sngie
38272343Sngie#include <sys/types.h>
39272343Sngie#include <sys/socket.h>
40272343Sngie
41272343Sngie#include <netinet/in.h>
42272343Sngie
43272343Sngie#include <arpa/inet.h>
44272343Sngie
45272343Sngiestruct addrinfo ai;
46272343Sngie
47272343Sngiechar host[NI_MAXHOST];
48272343Sngiechar serv[NI_MAXSERV];
49272343Sngieint vflag = 0;
50272343Sngie
51272343Sngiestatic void usage(void);
52272343Sngiestatic void print1(const char *, const struct addrinfo *, char *, char *);
53272343Sngieint main(int, char *[]);
54272343Sngie
55272343Sngiestatic void
56272343Sngieusage()
57272343Sngie{
58272343Sngie	fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
59272343Sngie}
60272343Sngie
61272343Sngiestatic void
62272343Sngieprint1(const char *title, const struct addrinfo *res, char *h, char *s)
63272343Sngie{
64272343Sngie	const char *start, *end;
65272343Sngie	int error;
66272343Sngie	const int niflag = NI_NUMERICHOST;
67272343Sngie
68272343Sngie	if (res->ai_addr) {
69272343Sngie		error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
70272343Sngie				    host, sizeof(host), serv, sizeof(serv),
71272343Sngie				    niflag);
72272343Sngie		h = host;
73272343Sngie		s = serv;
74272343Sngie	} else
75272343Sngie		error = 0;
76272343Sngie
77272343Sngie	if (vflag) {
78272343Sngie		start = "\t";
79272343Sngie		end = "\n";
80272343Sngie	} else {
81272343Sngie		start = " ";
82272343Sngie		end = "";
83272343Sngie	}
84272343Sngie	printf("%s%s", title, end);
85272343Sngie	printf("%sflags 0x%x%s", start, res->ai_flags, end);
86272343Sngie	printf("%sfamily %d%s", start, res->ai_family, end);
87272343Sngie	printf("%ssocktype %d%s", start, res->ai_socktype, end);
88272343Sngie	printf("%sprotocol %d%s", start, res->ai_protocol, end);
89272343Sngie	printf("%saddrlen %d%s", start, res->ai_addrlen, end);
90272343Sngie	if (error)
91272343Sngie		printf("%serror %d%s", start, error, end);
92272343Sngie	else {
93272343Sngie		printf("%shost %s%s", start, h, end);
94272343Sngie		printf("%sserv %s%s", start, s, end);
95272343Sngie	}
96272343Sngie#if 0
97272343Sngie	if (res->ai_canonname)
98272343Sngie		printf("%scname \"%s\"%s", start, res->ai_canonname, end);
99272343Sngie#endif
100272343Sngie	if (!vflag)
101272343Sngie		printf("\n");
102272343Sngie
103272343Sngie}
104272343Sngie
105272343Sngieint
106272343Sngiemain(int argc, char *argv[])
107272343Sngie{
108272343Sngie	struct addrinfo *res;
109272343Sngie	int error, i;
110272343Sngie	char *p, *q;
111272343Sngie	extern int optind;
112272343Sngie	extern char *optarg;
113272343Sngie	int c;
114272343Sngie	char nbuf[10];
115272343Sngie
116272343Sngie	memset(&ai, 0, sizeof(ai));
117272343Sngie	ai.ai_family = PF_UNSPEC;
118272343Sngie	ai.ai_flags |= AI_CANONNAME;
119272343Sngie	while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
120272343Sngie		switch (c) {
121272343Sngie		case 'D':
122272343Sngie			ai.ai_socktype = SOCK_DGRAM;
123272343Sngie			break;
124272343Sngie		case 'f':
125272343Sngie			ai.ai_family = atoi(optarg);
126272343Sngie			break;
127272343Sngie		case 'p':
128272343Sngie			ai.ai_protocol = atoi(optarg);
129272343Sngie			break;
130272343Sngie		case 'P':
131272343Sngie			ai.ai_flags |= AI_PASSIVE;
132272343Sngie			break;
133272343Sngie		case 'R':
134272343Sngie			ai.ai_socktype = SOCK_RAW;
135272343Sngie			break;
136272343Sngie		case 's':
137272343Sngie			ai.ai_socktype = atoi(optarg);
138272343Sngie			break;
139272343Sngie		case 'S':
140272343Sngie			ai.ai_socktype = SOCK_STREAM;
141272343Sngie			break;
142272343Sngie		case 'v':
143272343Sngie			vflag++;
144272343Sngie			break;
145272343Sngie		case '4':
146272343Sngie			ai.ai_family = PF_INET;
147272343Sngie			break;
148272343Sngie		case '6':
149272343Sngie			ai.ai_family = PF_INET6;
150272343Sngie			break;
151272343Sngie		default:
152272343Sngie			usage();
153272343Sngie			exit(1);
154272343Sngie		}
155272343Sngie	}
156272343Sngie	argc -= optind;
157272343Sngie	argv += optind;
158272343Sngie
159272343Sngie	if (argc != 2){
160272343Sngie		usage();
161272343Sngie		exit(1);
162272343Sngie	}
163272343Sngie
164272343Sngie	p = *argv[0] ? argv[0] : NULL;
165272343Sngie	q = *argv[1] ? argv[1] : NULL;
166272343Sngie
167272343Sngie	strncpy(nbuf, "(empty)", sizeof(nbuf));
168272343Sngie	print1("arg:", &ai, p ? p : nbuf , q ? q : nbuf);
169272343Sngie
170272343Sngie	error = getaddrinfo(p, q, &ai, &res);
171272343Sngie	if (error) {
172272343Sngie		printf("%s\n", gai_strerror(error));
173272343Sngie		exit(1);
174272343Sngie	}
175272343Sngie
176272343Sngie	i = 1;
177272343Sngie	do {
178272343Sngie		snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
179272343Sngie		print1(nbuf, res, NULL, NULL);
180272343Sngie
181272343Sngie		i++;
182272343Sngie	} while ((res = res->ai_next) != NULL);
183272343Sngie	printf("\n");
184272343Sngie
185272343Sngie	exit(0);
186272343Sngie}
187