gaitest.c revision 1.6
1/*	$OpenBSD: gaitest.c,v 1.6 2009/06/09 18:15:08 phessler Exp $	*/
2/*	$NetBSD: gaitest.c,v 1.3 2002/07/05 15:47:43 itojun Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Please note: the order of the responses (and the regress test)
35 * is dependent on the "family" keywords in resolv.conf.
36 *
37 * This expects the default behaviour of "family inet4 inet6"
38 */
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <netdb.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50struct addrinfo ai;
51
52char host[NI_MAXHOST];
53char serv[NI_MAXSERV];
54int vflag = 0;
55
56static void usage(void);
57static void print1(const char *, const struct addrinfo *, char *, char *);
58int main(int, char *[]);
59
60static void
61usage()
62{
63	fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
64}
65
66static void
67print1(title, res, h, s)
68	const char *title;
69	const struct addrinfo *res;
70	char *h;
71	char *s;
72{
73	char *start, *end;
74	int error;
75	const int niflag = NI_NUMERICHOST | NI_NUMERICSERV;
76
77	if (res->ai_addr) {
78		error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
79				    host, sizeof(host), serv, sizeof(serv),
80				    niflag);
81		h = host;
82		s = serv;
83	} else
84		error = 0;
85
86	if (vflag) {
87		start = "\t";
88		end = "\n";
89	} else {
90		start = " ";
91		end = "";
92	}
93	printf("%s%s", title, end);
94	printf("%sflags 0x%x%s", start, res->ai_flags, end);
95	printf("%sfamily %d%s", start, res->ai_family, end);
96	printf("%ssocktype %d%s", start, res->ai_socktype, end);
97	printf("%sprotocol %d%s", start, res->ai_protocol, end);
98	printf("%saddrlen %d%s", start, res->ai_addrlen, end);
99	if (error)
100		printf("%serror %d%s", start, error, end);
101	else {
102		printf("%shost %s%s", start, h, end);
103		printf("%sserv %s%s", start, s, end);
104	}
105#if 0
106	if (res->ai_canonname)
107		printf("%scname \"%s\"%s", start, res->ai_canonname, end);
108#endif
109	if (!vflag)
110		printf("\n");
111
112}
113
114int
115main(argc, argv)
116	int argc;
117	char *argv[];
118{
119	struct addrinfo *res;
120	int error, i;
121	char *p, *q;
122	extern int optind;
123	extern char *optarg;
124	int c;
125	char nbuf[10];
126
127	memset(&ai, 0, sizeof(ai));
128	ai.ai_family = PF_UNSPEC;
129	ai.ai_flags |= AI_CANONNAME;
130	while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
131		switch (c) {
132		case 'D':
133			ai.ai_socktype = SOCK_DGRAM;
134			break;
135		case 'f':
136			ai.ai_family = atoi(optarg);
137			break;
138		case 'p':
139			ai.ai_protocol = atoi(optarg);
140			break;
141		case 'P':
142			ai.ai_flags |= AI_PASSIVE;
143			break;
144		case 'R':
145			ai.ai_socktype = SOCK_RAW;
146			break;
147		case 's':
148			ai.ai_socktype = atoi(optarg);
149			break;
150		case 'S':
151			ai.ai_socktype = SOCK_STREAM;
152			break;
153		case 'v':
154			vflag++;
155			break;
156		case '4':
157			ai.ai_family = PF_INET;
158			break;
159		case '6':
160			ai.ai_family = PF_INET6;
161			break;
162		default:
163			usage();
164			exit(1);
165		}
166	}
167	argc -= optind;
168	argv += optind;
169
170	if (argc != 2){
171		usage();
172		exit(1);
173	}
174
175	p = *argv[0] ? argv[0] : NULL;
176	q = *argv[1] ? argv[1] : NULL;
177
178	print1("arg:", &ai, p ? p : "(empty)", q ? q : "(empty)");
179
180	error = getaddrinfo(p, q, &ai, &res);
181	if (error) {
182		printf("%s\n", gai_strerror(error));
183		exit(1);
184	}
185
186	i = 1;
187	do {
188		snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
189		print1(nbuf, res, NULL, NULL);
190
191		i++;
192	} while ((res = res->ai_next) != NULL);
193
194	exit(0);
195}
196