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