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