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