xref: /freebsd/usr.bin/getaddrinfo/getaddrinfo.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*	$NetBSD: getaddrinfo.c,v 1.4 2014/04/22 02:23:03 ginsbach 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 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <assert.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <netdb.h>
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <util.h>
48 
49 #include "tables.h"
50 
51 static void	usage(void) __dead;
52 static void	printaddrinfo(struct addrinfo *);
53 static bool	parse_af(const char *, int *);
54 static bool	parse_protocol(const char *, int *);
55 static bool	parse_socktype(const char *, int *);
56 static bool	parse_numeric_tabular(const char *, int *, const char *const *,
57 		    size_t);
58 
59 int
60 main(int argc, char **argv)
61 {
62 	static const struct addrinfo zero_addrinfo;
63 	struct addrinfo hints = zero_addrinfo;
64 	struct addrinfo *addrinfo;
65 	const char *hostname = NULL, *service = NULL;
66 	int ch;
67 	int error;
68 
69 	setprogname(argv[0]);
70 
71 	hints.ai_family = AF_UNSPEC;
72 	hints.ai_socktype = 0;
73 	hints.ai_protocol = 0;
74 	hints.ai_flags = 0;
75 
76 	while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
77 		switch (ch) {
78 		case 'c':
79 			hints.ai_flags |= AI_CANONNAME;
80 			break;
81 
82 		case 'f':
83 			if (!parse_af(optarg, &hints.ai_family)) {
84 				warnx("invalid address family: %s", optarg);
85 				usage();
86 			}
87 			break;
88 
89 		case 'n':
90 			hints.ai_flags |= AI_NUMERICHOST;
91 			break;
92 
93 		case 'N':
94 			hints.ai_flags |= AI_NUMERICSERV;
95 			break;
96 
97 		case 's':
98 			service = optarg;
99 			break;
100 
101 		case 'p':
102 			if (!parse_protocol(optarg, &hints.ai_protocol)) {
103 				warnx("invalid protocol: %s", optarg);
104 				usage();
105 			}
106 			break;
107 
108 		case 'P':
109 			hints.ai_flags |= AI_PASSIVE;
110 			break;
111 
112 		case 't':
113 			if (!parse_socktype(optarg, &hints.ai_socktype)) {
114 				warnx("invalid socket type: %s", optarg);
115 				usage();
116 			}
117 			break;
118 
119 		case '?':
120 		default:
121 			usage();
122 		}
123 	}
124 
125 	argc -= optind;
126 	argv += optind;
127 
128 	if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
129 		usage();
130 	if (argc == 1)
131 		hostname = argv[0];
132 
133 	if (service != NULL) {
134 		char *p;
135 
136 		if ((p = strchr(service, '/')) != NULL) {
137 			if (hints.ai_protocol != 0) {
138 				warnx("protocol already specified");
139 				usage();
140 			}
141 			*p = '\0';
142 			p++;
143 
144 			if (!parse_protocol(p, &hints.ai_protocol)) {
145 				warnx("invalid protocol: %s", p);
146 				usage();
147 			}
148 		}
149 	}
150 
151 	error = getaddrinfo(hostname, service, &hints, &addrinfo);
152 	if (error)
153 		errx(1, "%s", gai_strerror(error));
154 
155 	if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
156 		if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
157 			err(1, "printf");
158 	}
159 
160 	printaddrinfo(addrinfo);
161 
162 	freeaddrinfo(addrinfo);
163 
164 	return 0;
165 }
166 
167 static void __dead
168 usage(void)
169 {
170 
171 	(void)fprintf(stderr, "Usage: %s", getprogname());
172 	(void)fprintf(stderr,
173 	    " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
174 	(void)fprintf(stderr, "   [-cnNP] [<hostname>]\n");
175 	exit(1);
176 }
177 
178 static bool
179 parse_af(const char *string, int *afp)
180 {
181 
182 	return parse_numeric_tabular(string, afp, address_families,
183 	    __arraycount(address_families));
184 }
185 
186 static bool
187 parse_protocol(const char *string, int *protop)
188 {
189 	struct protoent *protoent;
190 	char *end;
191 	long value;
192 
193 	errno = 0;
194 	value = strtol(string, &end, 0);
195 	if ((string[0] == '\0') || (*end != '\0'))
196 		goto numeric_failed;
197 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
198 		goto numeric_failed;
199 	if ((value > INT_MAX) || (value < INT_MIN))
200 		goto numeric_failed;
201 
202 	*protop = value;
203 	return true;
204 
205 numeric_failed:
206 	protoent = getprotobyname(string);
207 	if (protoent == NULL)
208 		goto protoent_failed;
209 
210 	*protop = protoent->p_proto;
211 	return true;
212 
213 protoent_failed:
214 	return false;
215 }
216 
217 static bool
218 parse_socktype(const char *string, int *typep)
219 {
220 
221 	return parse_numeric_tabular(string, typep, socket_types,
222 	    __arraycount(socket_types));
223 }
224 
225 static bool
226 parse_numeric_tabular(const char *string, int *valuep,
227     const char *const *table, size_t n)
228 {
229 	char *end;
230 	long value;
231 	size_t i;
232 
233 	assert((uintmax_t)n <= (uintmax_t)INT_MAX);
234 
235 	errno = 0;
236 	value = strtol(string, &end, 0);
237 	if ((string[0] == '\0') || (*end != '\0'))
238 		goto numeric_failed;
239 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
240 		goto numeric_failed;
241 	if ((value > INT_MAX) || (value < INT_MIN))
242 		goto numeric_failed;
243 
244 	*valuep = value;
245 	return true;
246 
247 numeric_failed:
248 	for (i = 0; i < n; i++)
249 		if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
250 			break;
251 	if (i == n)
252 		goto table_failed;
253 	*valuep = i;
254 	return true;
255 
256 table_failed:
257 	return false;
258 }
259 
260 static void
261 printaddrinfo(struct addrinfo *addrinfo)
262 {
263 	struct addrinfo *ai;
264 	char buf[1024];
265 	int n;
266 	struct protoent *protoent;
267 
268 	for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
269 		/* Print the socket type.  */
270 		if ((ai->ai_socktype >= 0) &&
271 		    ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
272 		    (socket_types[ai->ai_socktype] != NULL))
273 			n = printf("%s", socket_types[ai->ai_socktype]);
274 		else
275 			n = printf("%d", ai->ai_socktype);
276 		if (n < 0)
277 			err(1, "printf");
278 
279 		/* Print the address family.  */
280 		if ((ai->ai_family >= 0) &&
281 		    ((size_t)ai->ai_family < __arraycount(address_families)) &&
282 		    (address_families[ai->ai_family] != NULL))
283 			n = printf(" %s", address_families[ai->ai_family]);
284 		else
285 			n = printf(" %d", ai->ai_family);
286 		if (n < 0)
287 			err(1, "printf");
288 
289 		/* Print the protocol number.  */
290 		protoent = getprotobynumber(ai->ai_protocol);
291 		if (protoent == NULL)
292 			n = printf(" %d", ai->ai_protocol);
293 		else
294 			n = printf(" %s", protoent->p_name);
295 		if (n < 0)
296 			err(1, "printf");
297 
298 		/* Format the sockaddr.  */
299 		switch (ai->ai_family) {
300 		case AF_INET:
301 		case AF_INET6:
302 			n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
303 			    ai->ai_addr);
304 			break;
305 
306 		default:
307 			n = sockaddr_snprintf(buf, sizeof(buf),
308 			    "%a %p %I %F %R %S", ai->ai_addr);
309 		}
310 
311 		/*
312 		 * Check for sockaddr_snprintf failure.
313 		 *
314 		 * XXX sockaddr_snprintf's error reporting is botched
315 		 * -- man page says it sets errno, but if getnameinfo
316 		 * fails, errno is not where it reports the error...
317 		 */
318 		if (n < 0) {
319 			warnx("sockaddr_snprintf failed");
320 			continue;
321 		}
322 		if (sizeof(buf) <= (size_t)n)
323 			warnx("truncated sockaddr_snprintf output");
324 
325 		/* Print the formatted sockaddr.  */
326 		if (printf("%s\n", buf) < 0)
327 			err(1, "printf");
328 	}
329 }
330