getaddrinfo.c revision 1.2 1 /* $NetBSD: getaddrinfo.c,v 1.2 2014/03/14 13:17:18 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.2 2014/03/14 13:17:18 ginsbach 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 break;
105
106 case 'P':
107 hints.ai_flags |= AI_PASSIVE;
108 break;
109
110 case 't':
111 if (!parse_socktype(optarg, &hints.ai_socktype)) {
112 warnx("invalid socket type: %s", optarg);
113 usage();
114 }
115 break;
116
117 case '?':
118 default:
119 usage();
120 }
121 }
122
123 argc -= optind;
124 argv += optind;
125
126 if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
127 usage();
128 if (argc == 1)
129 hostname = argv[0];
130
131 error = getaddrinfo(hostname, service, &hints, &addrinfo);
132 if (error)
133 errx(1, "%s", gai_strerror(error));
134
135 if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
136 if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
137 err(1, "printf");
138 }
139
140 printaddrinfo(addrinfo);
141
142 freeaddrinfo(addrinfo);
143
144 return 0;
145 }
146
147 static void __dead
148 usage(void)
149 {
150
151 (void)fprintf(stderr, "Usage: %s", getprogname());
152 (void)fprintf(stderr,
153 " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
154 (void)fprintf(stderr, " [-cnNP] [<hostname>]\n");
155 exit(1);
156 }
157
158 static bool
159 parse_af(const char *string, int *afp)
160 {
161
162 return parse_numeric_tabular(string, afp, address_families,
163 __arraycount(address_families));
164 }
165
166 static bool
167 parse_protocol(const char *string, int *protop)
168 {
169 struct protoent *protoent;
170 char *end;
171 long value;
172
173 errno = 0;
174 value = strtol(string, &end, 0);
175 if ((string[0] == '\0') || (*end != '\0'))
176 goto numeric_failed;
177 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
178 goto numeric_failed;
179 if ((value > INT_MAX) || (value < INT_MIN))
180 goto numeric_failed;
181
182 *protop = value;
183 return true;
184
185 numeric_failed:
186 protoent = getprotobyname(string);
187 if (protoent == NULL)
188 goto protoent_failed;
189
190 *protop = protoent->p_proto;
191 return true;
192
193 protoent_failed:
194 return false;
195 }
196
197 static bool
198 parse_socktype(const char *string, int *typep)
199 {
200
201 return parse_numeric_tabular(string, typep, socket_types,
202 __arraycount(socket_types));
203 }
204
205 static bool
206 parse_numeric_tabular(const char *string, int *valuep,
207 const char *const *table, size_t n)
208 {
209 char *end;
210 long value;
211 size_t i;
212
213 assert((uintmax_t)n <= (uintmax_t)INT_MAX);
214
215 errno = 0;
216 value = strtol(string, &end, 0);
217 if ((string[0] == '\0') || (*end != '\0'))
218 goto numeric_failed;
219 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
220 goto numeric_failed;
221 if ((value > INT_MAX) || (value < INT_MIN))
222 goto numeric_failed;
223
224 *valuep = value;
225 return true;
226
227 numeric_failed:
228 for (i = 0; i < n; i++)
229 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
230 break;
231 if (i == n)
232 goto table_failed;
233 *valuep = i;
234 return true;
235
236 table_failed:
237 return false;
238 }
239
240 static void
241 printaddrinfo(struct addrinfo *addrinfo)
242 {
243 struct addrinfo *ai;
244 char buf[1024];
245 int n;
246 struct protoent *protoent;
247
248 for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
249 /* Print the socket type. */
250 if ((ai->ai_socktype >= 0) &&
251 ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
252 (socket_types[ai->ai_socktype] != NULL))
253 n = printf("%s", socket_types[ai->ai_socktype]);
254 else
255 n = printf("%d", ai->ai_socktype);
256 if (n < 0)
257 err(1, "printf");
258
259 /* Print the address family. */
260 if ((ai->ai_family >= 0) &&
261 ((size_t)ai->ai_family < __arraycount(address_families)) &&
262 (address_families[ai->ai_family] != NULL))
263 n = printf(" %s", address_families[ai->ai_family]);
264 else
265 n = printf(" %d", ai->ai_family);
266 if (n < 0)
267 err(1, "printf");
268
269 /* Print the protocol number. */
270 protoent = getprotobynumber(ai->ai_protocol);
271 if (protoent == NULL)
272 n = printf(" %d", ai->ai_protocol);
273 else
274 n = printf(" %s", protoent->p_name);
275 if (n < 0)
276 err(1, "printf");
277
278 /* Format the sockaddr. */
279 switch (ai->ai_family) {
280 case AF_INET:
281 case AF_INET6:
282 n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
283 ai->ai_addr);
284 break;
285
286 default:
287 n = sockaddr_snprintf(buf, sizeof(buf),
288 "%a %p %I %F %R %S", ai->ai_addr);
289 }
290
291 /*
292 * Check for sockaddr_snprintf failure.
293 *
294 * XXX sockaddr_snprintf's error reporting is botched
295 * -- man page says it sets errno, but if getnameinfo
296 * fails, errno is not where it reports the error...
297 */
298 if (n < 0) {
299 warnx("sockaddr_snprintf failed");
300 continue;
301 }
302 if (sizeof(buf) <= (size_t)n)
303 warnx("truncated sockaddr_snprintf output");
304
305 /* Print the formatted sockaddr. */
306 if (printf("%s\n", buf) < 0)
307 err(1, "printf");
308 }
309 }
310