gaitest.c revision 1.4
1/*	$OpenBSD: gaitest.c,v 1.4 2004/02/28 12:20:23 itojun 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#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <netdb.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43struct addrinfo ai;
44
45char host[NI_MAXHOST];
46char serv[NI_MAXSERV];
47int vflag = 0;
48
49static void usage(void);
50static void print1(const char *, const struct addrinfo *, char *, char *);
51int main(int, char *[]);
52
53static void
54usage()
55{
56	fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
57}
58
59static void
60print1(title, res, h, s)
61	const char *title;
62	const struct addrinfo *res;
63	char *h;
64	char *s;
65{
66	char *start, *end;
67	int error;
68	const int niflag = NI_NUMERICHOST;
69
70	if (res->ai_addr) {
71		error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
72				    host, sizeof(host), serv, sizeof(serv),
73				    niflag);
74		h = host;
75		s = serv;
76	} else
77		error = 0;
78
79	if (vflag) {
80		start = "\t";
81		end = "\n";
82	} else {
83		start = " ";
84		end = "";
85	}
86	printf("%s%s", title, end);
87	printf("%sflags 0x%x%s", start, res->ai_flags, end);
88	printf("%sfamily %d%s", start, res->ai_family, end);
89	printf("%ssocktype %d%s", start, res->ai_socktype, end);
90	printf("%sprotocol %d%s", start, res->ai_protocol, end);
91	printf("%saddrlen %d%s", start, res->ai_addrlen, end);
92	if (error)
93		printf("%serror %d%s", start, error, end);
94	else {
95		printf("%shost %s%s", start, h, end);
96		printf("%sserv %s%s", start, s, end);
97	}
98#if 0
99	if (res->ai_canonname)
100		printf("%scname \"%s\"%s", start, res->ai_canonname, end);
101#endif
102	if (!vflag)
103		printf("\n");
104
105}
106
107int
108main(argc, argv)
109	int argc;
110	char *argv[];
111{
112	struct addrinfo *res;
113	int error, i;
114	char *p, *q;
115	extern int optind;
116	extern char *optarg;
117	int c;
118	char nbuf[10];
119
120	memset(&ai, 0, sizeof(ai));
121	ai.ai_family = PF_UNSPEC;
122	ai.ai_flags |= AI_CANONNAME;
123	while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
124		switch (c) {
125		case 'D':
126			ai.ai_socktype = SOCK_DGRAM;
127			break;
128		case 'f':
129			ai.ai_family = atoi(optarg);
130			break;
131		case 'p':
132			ai.ai_protocol = atoi(optarg);
133			break;
134		case 'P':
135			ai.ai_flags |= AI_PASSIVE;
136			break;
137		case 'R':
138			ai.ai_socktype = SOCK_RAW;
139			break;
140		case 's':
141			ai.ai_socktype = atoi(optarg);
142			break;
143		case 'S':
144			ai.ai_socktype = SOCK_STREAM;
145			break;
146		case 'v':
147			vflag++;
148			break;
149		case '4':
150			ai.ai_family = PF_INET;
151			break;
152		case '6':
153			ai.ai_family = PF_INET6;
154			break;
155		default:
156			usage();
157			exit(1);
158		}
159	}
160	argc -= optind;
161	argv += optind;
162
163	if (argc != 2){
164		usage();
165		exit(1);
166	}
167
168	p = *argv[0] ? argv[0] : NULL;
169	q = *argv[1] ? argv[1] : NULL;
170
171	print1("arg:", &ai, p ? p : "(empty)", q ? q : "(empty)");
172
173	error = getaddrinfo(p, q, &ai, &res);
174	if (error) {
175		printf("%s\n", gai_strerror(error));
176		exit(1);
177	}
178
179	i = 1;
180	do {
181		snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
182		print1(nbuf, res, NULL, NULL);
183
184		i++;
185	} while ((res = res->ai_next) != NULL);
186
187	exit(0);
188}
189