1 /* $NetBSD: getaddrinfo.c,v 1.4 2014/04/22 02:23:03 ginsbach Exp $ */
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/socket.h>
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 *,
62 main(int argc, char **argv)
64 static const struct addrinfo zero_addrinfo;
65 struct addrinfo hints = zero_addrinfo;
66 struct addrinfo *addrinfo;
67 const char *hostname = NULL, *service = NULL;
73 hints.ai_family = AF_UNSPEC;
74 hints.ai_socktype = 0;
75 hints.ai_protocol = 0;
78 while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
81 hints.ai_flags |= AI_CANONNAME;
85 if (!parse_af(optarg, &hints.ai_family)) {
86 warnx("invalid address family: %s", optarg);
92 hints.ai_flags |= AI_NUMERICHOST;
96 hints.ai_flags |= AI_NUMERICSERV;
104 if (!parse_protocol(optarg, &hints.ai_protocol)) {
105 warnx("invalid protocol: %s", optarg);
111 hints.ai_flags |= AI_PASSIVE;
115 if (!parse_socktype(optarg, &hints.ai_socktype)) {
116 warnx("invalid socket type: %s", optarg);
130 if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
135 if (service != NULL) {
138 if ((p = strchr(service, '/')) != NULL) {
139 if (hints.ai_protocol != 0) {
140 warnx("protocol already specified");
146 if (!parse_protocol(p, &hints.ai_protocol)) {
147 warnx("invalid protocol: %s", p);
153 error = getaddrinfo(hostname, service, &hints, &addrinfo);
155 errx(1, "%s", gai_strerror(error));
157 if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
158 if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
162 printaddrinfo(addrinfo);
164 freeaddrinfo(addrinfo);
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");
181 parse_af(const char *string, int *afp)
184 return parse_numeric_tabular(string, afp, address_families,
185 __arraycount(address_families));
189 parse_protocol(const char *string, int *protop)
191 struct protoent *protoent;
196 value = strtol(string, &end, 0);
197 if ((string[0] == '\0') || (*end != '\0'))
199 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
201 if ((value > INT_MAX) || (value < INT_MIN))
208 protoent = getprotobyname(string);
209 if (protoent == NULL)
210 goto protoent_failed;
212 *protop = protoent->p_proto;
220 parse_socktype(const char *string, int *typep)
223 return parse_numeric_tabular(string, typep, socket_types,
224 __arraycount(socket_types));
228 parse_numeric_tabular(const char *string, int *valuep,
229 const char *const *table, size_t n)
235 assert((uintmax_t)n <= (uintmax_t)INT_MAX);
238 value = strtol(string, &end, 0);
239 if ((string[0] == '\0') || (*end != '\0'))
241 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
243 if ((value > INT_MAX) || (value < INT_MIN))
250 for (i = 0; i < n; i++)
251 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
263 printaddrinfo(struct addrinfo *addrinfo)
268 struct protoent *protoent;
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]);
277 n = printf("%d", ai->ai_socktype);
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]);
287 n = printf(" %d", ai->ai_family);
291 /* Print the protocol number. */
292 protoent = getprotobynumber(ai->ai_protocol);
293 if (protoent == NULL)
294 n = printf(" %d", ai->ai_protocol);
296 n = printf(" %s", protoent->p_name);
300 /* Format the sockaddr. */
301 switch (ai->ai_family) {
304 n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
309 n = sockaddr_snprintf(buf, sizeof(buf),
310 "%a %p %I %F %R %S", ai->ai_addr);
314 * Check for sockaddr_snprintf failure.
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...
321 warnx("sockaddr_snprintf failed");
324 if (sizeof(buf) <= (size_t)n)
325 warnx("truncated sockaddr_snprintf output");
327 /* Print the formatted sockaddr. */
328 if (printf("%s\n", buf) < 0)