xref: /netbsd-src/usr.bin/getaddrinfo/getaddrinfo.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: getaddrinfo.c,v 1.1 2013/09/30 06:19:22 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: getaddrinfo.c,v 1.1 2013/09/30 06:19:22 riastradh Exp $");
34 
35 #include <assert.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <netdb.h>
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <util.h>
46 
47 #include "tables.h"
48 
49 static void	usage(void) __dead;
50 static void	printaddrinfo(struct addrinfo *);
51 static bool	parse_af(const char *, int *);
52 static bool	parse_protocol(const char *, int *);
53 static bool	parse_socktype(const char *, int *);
54 static bool	parse_numeric_tabular(const char *, int *, const char *const *,
55 		    size_t);
56 
57 int
58 main(int argc, char **argv)
59 {
60 	static const struct addrinfo zero_addrinfo;
61 	struct addrinfo hints = zero_addrinfo;
62 	struct addrinfo *addrinfo;
63 	const char *hostname = NULL, *service = NULL;
64 	int ch;
65 	int error;
66 
67 	setprogname(argv[0]);
68 
69 	hints.ai_family = AF_UNSPEC;
70 	hints.ai_socktype = 0;
71 	hints.ai_protocol = 0;
72 	hints.ai_flags = 0;
73 
74 	while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
75 		switch (ch) {
76 		case 'c':
77 			hints.ai_flags |= AI_CANONNAME;
78 			break;
79 
80 		case 'f':
81 			if (!parse_af(optarg, &hints.ai_family)) {
82 				warnx("invalid address family: %s", optarg);
83 				usage();
84 			}
85 			break;
86 
87 		case 'n':
88 			hints.ai_flags |= AI_NUMERICHOST;
89 			break;
90 
91 		case 'N':
92 			hints.ai_flags |= AI_NUMERICSERV;
93 			break;
94 
95 		case 's':
96 			service = optarg;
97 			break;
98 
99 		case 'p':
100 			if (!parse_protocol(optarg, &hints.ai_protocol)) {
101 				warnx("invalid protocol: %s", optarg);
102 				usage();
103 			}
104 
105 		case 'P':
106 			hints.ai_flags |= AI_PASSIVE;
107 			break;
108 
109 		case 't':
110 			if (!parse_socktype(optarg, &hints.ai_socktype)) {
111 				warnx("invalid socket type: %s", optarg);
112 				usage();
113 			}
114 			break;
115 
116 		case '?':
117 		default:
118 			usage();
119 		}
120 	}
121 
122 	argc -= optind;
123 	argv += optind;
124 
125 	if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
126 		usage();
127 	if (argc == 1)
128 		hostname = argv[0];
129 
130 	error = getaddrinfo(hostname, service, &hints, &addrinfo);
131 	if (error)
132 		errx(1, "%s", gai_strerror(error));
133 
134 	if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
135 		if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
136 			err(1, "printf");
137 	}
138 
139 	printaddrinfo(addrinfo);
140 
141 	freeaddrinfo(addrinfo);
142 
143 	return 0;
144 }
145 
146 static void __dead
147 usage(void)
148 {
149 
150 	(void)fprintf(stderr, "Usage: %s", getprogname());
151 	(void)fprintf(stderr,
152 	    " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
153 	(void)fprintf(stderr, "   [-cnNP] [<hostname>]\n");
154 	exit(1);
155 }
156 
157 static bool
158 parse_af(const char *string, int *afp)
159 {
160 
161 	return parse_numeric_tabular(string, afp, address_families,
162 	    __arraycount(address_families));
163 }
164 
165 static bool
166 parse_protocol(const char *string, int *protop)
167 {
168 	struct protoent *protoent;
169 	char *end;
170 	long value;
171 
172 	errno = 0;
173 	value = strtol(string, &end, 0);
174 	if ((string[0] == '\0') || (*end != '\0'))
175 		goto numeric_failed;
176 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
177 		goto numeric_failed;
178 	if ((value > INT_MAX) || (value < INT_MIN))
179 		goto numeric_failed;
180 
181 	*protop = value;
182 	return true;
183 
184 numeric_failed:
185 	protoent = getprotobyname(string);
186 	if (protoent == NULL)
187 		goto protoent_failed;
188 
189 	*protop = protoent->p_proto;
190 	return true;
191 
192 protoent_failed:
193 	return false;
194 }
195 
196 static bool
197 parse_socktype(const char *string, int *typep)
198 {
199 
200 	return parse_numeric_tabular(string, typep, socket_types,
201 	    __arraycount(socket_types));
202 }
203 
204 static bool
205 parse_numeric_tabular(const char *string, int *valuep,
206     const char *const *table, size_t n)
207 {
208 	char *end;
209 	long value;
210 	size_t i;
211 
212 	assert((uintmax_t)n <= (uintmax_t)INT_MAX);
213 
214 	errno = 0;
215 	value = strtol(string, &end, 0);
216 	if ((string[0] == '\0') || (*end != '\0'))
217 		goto numeric_failed;
218 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
219 		goto numeric_failed;
220 	if ((value > INT_MAX) || (value < INT_MIN))
221 		goto numeric_failed;
222 
223 	*valuep = value;
224 	return true;
225 
226 numeric_failed:
227 	for (i = 0; i < n; i++)
228 		if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
229 			break;
230 	if (i == n)
231 		goto table_failed;
232 	*valuep = i;
233 	return true;
234 
235 table_failed:
236 	return false;
237 }
238 
239 static void
240 printaddrinfo(struct addrinfo *addrinfo)
241 {
242 	struct addrinfo *ai;
243 	char buf[1024];
244 	int n;
245 	struct protoent *protoent;
246 
247 	for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
248 		/* Print the socket type.  */
249 		if ((ai->ai_socktype >= 0) &&
250 		    ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
251 		    (socket_types[ai->ai_socktype] != NULL))
252 			n = printf("%s", socket_types[ai->ai_socktype]);
253 		else
254 			n = printf("%d", ai->ai_socktype);
255 		if (n < 0)
256 			err(1, "printf");
257 
258 		/* Print the address family.  */
259 		if ((ai->ai_family >= 0) &&
260 		    ((size_t)ai->ai_family < __arraycount(address_families)) &&
261 		    (address_families[ai->ai_family] != NULL))
262 			n = printf(" %s", address_families[ai->ai_family]);
263 		else
264 			n = printf(" %d", ai->ai_family);
265 		if (n < 0)
266 			err(1, "printf");
267 
268 		/* Print the protocol number.  */
269 		protoent = getprotobynumber(ai->ai_protocol);
270 		if (protoent == NULL)
271 			n = printf(" %d", ai->ai_protocol);
272 		else
273 			n = printf(" %s", protoent->p_name);
274 		if (n < 0)
275 			err(1, "printf");
276 
277 		/* Format the sockaddr.  */
278 		switch (ai->ai_family) {
279 		case AF_INET:
280 		case AF_INET6:
281 			n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
282 			    ai->ai_addr);
283 			break;
284 
285 		default:
286 			n = sockaddr_snprintf(buf, sizeof(buf),
287 			    "%a %p %I %F %R %S", ai->ai_addr);
288 		}
289 
290 		/*
291 		 * Check for sockaddr_snprintf failure.
292 		 *
293 		 * XXX sockaddr_snprintf's error reporting is botched
294 		 * -- man page says it sets errno, but if getnameinfo
295 		 * fails, errno is not where it reports the error...
296 		 */
297 		if (n < 0) {
298 			warnx("sockaddr_snprintf failed");
299 			continue;
300 		}
301 		if (sizeof(buf) <= (size_t)n)
302 			warnx("truncated sockaddr_snprintf output");
303 
304 		/* Print the formatted sockaddr.  */
305 		if (printf("%s\n", buf) < 0)
306 			err(1, "printf");
307 	}
308 }
309