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/types.h>
33 #include <sys/socket.h>
50 static void usage(void) __dead;
51 static void printaddrinfo(struct addrinfo *);
52 static bool parse_af(const char *, int *);
53 static bool parse_protocol(const char *, int *);
54 static bool parse_socktype(const char *, int *);
55 static bool parse_numeric_tabular(const char *, int *, const char *const *,
59 main(int argc, char **argv)
61 static const struct addrinfo zero_addrinfo;
62 struct addrinfo hints = zero_addrinfo;
63 struct addrinfo *addrinfo;
64 const char *hostname = NULL, *service = NULL;
70 hints.ai_family = AF_UNSPEC;
71 hints.ai_socktype = 0;
72 hints.ai_protocol = 0;
75 while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
78 hints.ai_flags |= AI_CANONNAME;
82 if (!parse_af(optarg, &hints.ai_family)) {
83 warnx("invalid address family: %s", optarg);
89 hints.ai_flags |= AI_NUMERICHOST;
93 hints.ai_flags |= AI_NUMERICSERV;
101 if (!parse_protocol(optarg, &hints.ai_protocol)) {
102 warnx("invalid protocol: %s", optarg);
108 hints.ai_flags |= AI_PASSIVE;
112 if (!parse_socktype(optarg, &hints.ai_socktype)) {
113 warnx("invalid socket type: %s", optarg);
127 if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
132 if (service != NULL) {
135 if ((p = strchr(service, '/')) != NULL) {
136 if (hints.ai_protocol != 0) {
137 warnx("protocol already specified");
143 if (!parse_protocol(p, &hints.ai_protocol)) {
144 warnx("invalid protocol: %s", p);
150 error = getaddrinfo(hostname, service, &hints, &addrinfo);
152 errx(1, "%s", gai_strerror(error));
154 if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
155 if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
159 printaddrinfo(addrinfo);
161 freeaddrinfo(addrinfo);
170 (void)fprintf(stderr, "Usage: %s", getprogname());
171 (void)fprintf(stderr,
172 " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
173 (void)fprintf(stderr, " [-cnNP] [<hostname>]\n");
178 parse_af(const char *string, int *afp)
181 return parse_numeric_tabular(string, afp, address_families,
182 __arraycount(address_families));
186 parse_protocol(const char *string, int *protop)
188 struct protoent *protoent;
193 value = strtol(string, &end, 0);
194 if ((string[0] == '\0') || (*end != '\0'))
196 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
198 if ((value > INT_MAX) || (value < INT_MIN))
205 protoent = getprotobyname(string);
206 if (protoent == NULL)
207 goto protoent_failed;
209 *protop = protoent->p_proto;
217 parse_socktype(const char *string, int *typep)
220 return parse_numeric_tabular(string, typep, socket_types,
221 __arraycount(socket_types));
225 parse_numeric_tabular(const char *string, int *valuep,
226 const char *const *table, size_t n)
232 assert((uintmax_t)n <= (uintmax_t)INT_MAX);
235 value = strtol(string, &end, 0);
236 if ((string[0] == '\0') || (*end != '\0'))
238 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
240 if ((value > INT_MAX) || (value < INT_MIN))
247 for (i = 0; i < n; i++)
248 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
260 printaddrinfo(struct addrinfo *addrinfo)
265 struct protoent *protoent;
267 for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
268 /* Print the socket type. */
269 if ((ai->ai_socktype >= 0) &&
270 ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
271 (socket_types[ai->ai_socktype] != NULL))
272 n = printf("%s", socket_types[ai->ai_socktype]);
274 n = printf("%d", ai->ai_socktype);
278 /* Print the address family. */
279 if ((ai->ai_family >= 0) &&
280 ((size_t)ai->ai_family < __arraycount(address_families)) &&
281 (address_families[ai->ai_family] != NULL))
282 n = printf(" %s", address_families[ai->ai_family]);
284 n = printf(" %d", ai->ai_family);
288 /* Print the protocol number. */
289 protoent = getprotobynumber(ai->ai_protocol);
290 if (protoent == NULL)
291 n = printf(" %d", ai->ai_protocol);
293 n = printf(" %s", protoent->p_name);
297 /* Format the sockaddr. */
298 switch (ai->ai_family) {
301 n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
306 n = sockaddr_snprintf(buf, sizeof(buf),
307 "%a %p %I %F %R %S", ai->ai_addr);
311 * Check for sockaddr_snprintf failure.
313 * XXX sockaddr_snprintf's error reporting is botched
314 * -- man page says it sets errno, but if getnameinfo
315 * fails, errno is not where it reports the error...
318 warnx("sockaddr_snprintf failed");
321 if (sizeof(buf) <= (size_t)n)
322 warnx("truncated sockaddr_snprintf output");
324 /* Print the formatted sockaddr. */
325 if (printf("%s\n", buf) < 0)