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 #include <sys/types.h>
34 #include <sys/socket.h>
51 static void usage(void) __dead;
52 static void printaddrinfo(struct addrinfo *);
53 static bool parse_af(const char *, int *);
54 static bool parse_protocol(const char *, int *);
55 static bool parse_socktype(const char *, int *);
56 static bool parse_numeric_tabular(const char *, int *, const char *const *,
60 main(int argc, char **argv)
62 static const struct addrinfo zero_addrinfo;
63 struct addrinfo hints = zero_addrinfo;
64 struct addrinfo *addrinfo;
65 const char *hostname = NULL, *service = NULL;
71 hints.ai_family = AF_UNSPEC;
72 hints.ai_socktype = 0;
73 hints.ai_protocol = 0;
76 while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
79 hints.ai_flags |= AI_CANONNAME;
83 if (!parse_af(optarg, &hints.ai_family)) {
84 warnx("invalid address family: %s", optarg);
90 hints.ai_flags |= AI_NUMERICHOST;
94 hints.ai_flags |= AI_NUMERICSERV;
102 if (!parse_protocol(optarg, &hints.ai_protocol)) {
103 warnx("invalid protocol: %s", optarg);
109 hints.ai_flags |= AI_PASSIVE;
113 if (!parse_socktype(optarg, &hints.ai_socktype)) {
114 warnx("invalid socket type: %s", optarg);
128 if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
133 if (service != NULL) {
136 if ((p = strchr(service, '/')) != NULL) {
137 if (hints.ai_protocol != 0) {
138 warnx("protocol already specified");
144 if (!parse_protocol(p, &hints.ai_protocol)) {
145 warnx("invalid protocol: %s", p);
151 error = getaddrinfo(hostname, service, &hints, &addrinfo);
153 errx(1, "%s", gai_strerror(error));
155 if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
156 if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
160 printaddrinfo(addrinfo);
162 freeaddrinfo(addrinfo);
171 (void)fprintf(stderr, "Usage: %s", getprogname());
172 (void)fprintf(stderr,
173 " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
174 (void)fprintf(stderr, " [-cnNP] [<hostname>]\n");
179 parse_af(const char *string, int *afp)
182 return parse_numeric_tabular(string, afp, address_families,
183 __arraycount(address_families));
187 parse_protocol(const char *string, int *protop)
189 struct protoent *protoent;
194 value = strtol(string, &end, 0);
195 if ((string[0] == '\0') || (*end != '\0'))
197 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
199 if ((value > INT_MAX) || (value < INT_MIN))
206 protoent = getprotobyname(string);
207 if (protoent == NULL)
208 goto protoent_failed;
210 *protop = protoent->p_proto;
218 parse_socktype(const char *string, int *typep)
221 return parse_numeric_tabular(string, typep, socket_types,
222 __arraycount(socket_types));
226 parse_numeric_tabular(const char *string, int *valuep,
227 const char *const *table, size_t n)
233 assert((uintmax_t)n <= (uintmax_t)INT_MAX);
236 value = strtol(string, &end, 0);
237 if ((string[0] == '\0') || (*end != '\0'))
239 if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
241 if ((value > INT_MAX) || (value < INT_MIN))
248 for (i = 0; i < n; i++)
249 if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
261 printaddrinfo(struct addrinfo *addrinfo)
266 struct protoent *protoent;
268 for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
269 /* Print the socket type. */
270 if ((ai->ai_socktype >= 0) &&
271 ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
272 (socket_types[ai->ai_socktype] != NULL))
273 n = printf("%s", socket_types[ai->ai_socktype]);
275 n = printf("%d", ai->ai_socktype);
279 /* Print the address family. */
280 if ((ai->ai_family >= 0) &&
281 ((size_t)ai->ai_family < __arraycount(address_families)) &&
282 (address_families[ai->ai_family] != NULL))
283 n = printf(" %s", address_families[ai->ai_family]);
285 n = printf(" %d", ai->ai_family);
289 /* Print the protocol number. */
290 protoent = getprotobynumber(ai->ai_protocol);
291 if (protoent == NULL)
292 n = printf(" %d", ai->ai_protocol);
294 n = printf(" %s", protoent->p_name);
298 /* Format the sockaddr. */
299 switch (ai->ai_family) {
302 n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
307 n = sockaddr_snprintf(buf, sizeof(buf),
308 "%a %p %I %F %R %S", ai->ai_addr);
312 * Check for sockaddr_snprintf failure.
314 * XXX sockaddr_snprintf's error reporting is botched
315 * -- man page says it sets errno, but if getnameinfo
316 * fails, errno is not where it reports the error...
319 warnx("sockaddr_snprintf failed");
322 if (sizeof(buf) <= (size_t)n)
323 warnx("truncated sockaddr_snprintf output");
325 /* Print the formatted sockaddr. */
326 if (printf("%s\n", buf) < 0)