Home | History | Annotate | Line # | Download | only in getaddrinfo
getaddrinfo.c revision 1.2
      1  1.2  ginsbach /*	$NetBSD: getaddrinfo.c,v 1.2 2014/03/14 13:17:18 ginsbach Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.2  ginsbach __RCSID("$NetBSD: getaddrinfo.c,v 1.2 2014/03/14 13:17:18 ginsbach Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <assert.h>
     36  1.1  riastrad #include <err.h>
     37  1.1  riastrad #include <errno.h>
     38  1.1  riastrad #include <limits.h>
     39  1.1  riastrad #include <netdb.h>
     40  1.1  riastrad #include <stdbool.h>
     41  1.1  riastrad #include <stdint.h>
     42  1.1  riastrad #include <stdlib.h>
     43  1.1  riastrad #include <string.h>
     44  1.1  riastrad #include <unistd.h>
     45  1.1  riastrad #include <util.h>
     46  1.1  riastrad 
     47  1.1  riastrad #include "tables.h"
     48  1.1  riastrad 
     49  1.1  riastrad static void	usage(void) __dead;
     50  1.1  riastrad static void	printaddrinfo(struct addrinfo *);
     51  1.1  riastrad static bool	parse_af(const char *, int *);
     52  1.1  riastrad static bool	parse_protocol(const char *, int *);
     53  1.1  riastrad static bool	parse_socktype(const char *, int *);
     54  1.1  riastrad static bool	parse_numeric_tabular(const char *, int *, const char *const *,
     55  1.1  riastrad 		    size_t);
     56  1.1  riastrad 
     57  1.1  riastrad int
     58  1.1  riastrad main(int argc, char **argv)
     59  1.1  riastrad {
     60  1.1  riastrad 	static const struct addrinfo zero_addrinfo;
     61  1.1  riastrad 	struct addrinfo hints = zero_addrinfo;
     62  1.1  riastrad 	struct addrinfo *addrinfo;
     63  1.1  riastrad 	const char *hostname = NULL, *service = NULL;
     64  1.1  riastrad 	int ch;
     65  1.1  riastrad 	int error;
     66  1.1  riastrad 
     67  1.1  riastrad 	setprogname(argv[0]);
     68  1.1  riastrad 
     69  1.1  riastrad 	hints.ai_family = AF_UNSPEC;
     70  1.1  riastrad 	hints.ai_socktype = 0;
     71  1.1  riastrad 	hints.ai_protocol = 0;
     72  1.1  riastrad 	hints.ai_flags = 0;
     73  1.1  riastrad 
     74  1.1  riastrad 	while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
     75  1.1  riastrad 		switch (ch) {
     76  1.1  riastrad 		case 'c':
     77  1.1  riastrad 			hints.ai_flags |= AI_CANONNAME;
     78  1.1  riastrad 			break;
     79  1.1  riastrad 
     80  1.1  riastrad 		case 'f':
     81  1.1  riastrad 			if (!parse_af(optarg, &hints.ai_family)) {
     82  1.1  riastrad 				warnx("invalid address family: %s", optarg);
     83  1.1  riastrad 				usage();
     84  1.1  riastrad 			}
     85  1.1  riastrad 			break;
     86  1.1  riastrad 
     87  1.1  riastrad 		case 'n':
     88  1.1  riastrad 			hints.ai_flags |= AI_NUMERICHOST;
     89  1.1  riastrad 			break;
     90  1.1  riastrad 
     91  1.1  riastrad 		case 'N':
     92  1.1  riastrad 			hints.ai_flags |= AI_NUMERICSERV;
     93  1.1  riastrad 			break;
     94  1.1  riastrad 
     95  1.1  riastrad 		case 's':
     96  1.1  riastrad 			service = optarg;
     97  1.1  riastrad 			break;
     98  1.1  riastrad 
     99  1.1  riastrad 		case 'p':
    100  1.1  riastrad 			if (!parse_protocol(optarg, &hints.ai_protocol)) {
    101  1.1  riastrad 				warnx("invalid protocol: %s", optarg);
    102  1.1  riastrad 				usage();
    103  1.1  riastrad 			}
    104  1.2  ginsbach 			break;
    105  1.1  riastrad 
    106  1.1  riastrad 		case 'P':
    107  1.1  riastrad 			hints.ai_flags |= AI_PASSIVE;
    108  1.1  riastrad 			break;
    109  1.1  riastrad 
    110  1.1  riastrad 		case 't':
    111  1.1  riastrad 			if (!parse_socktype(optarg, &hints.ai_socktype)) {
    112  1.1  riastrad 				warnx("invalid socket type: %s", optarg);
    113  1.1  riastrad 				usage();
    114  1.1  riastrad 			}
    115  1.1  riastrad 			break;
    116  1.1  riastrad 
    117  1.1  riastrad 		case '?':
    118  1.1  riastrad 		default:
    119  1.1  riastrad 			usage();
    120  1.1  riastrad 		}
    121  1.1  riastrad 	}
    122  1.1  riastrad 
    123  1.1  riastrad 	argc -= optind;
    124  1.1  riastrad 	argv += optind;
    125  1.1  riastrad 
    126  1.1  riastrad 	if (!((argc == 1) || ((argc == 0) && (hints.ai_flags & AI_PASSIVE))))
    127  1.1  riastrad 		usage();
    128  1.1  riastrad 	if (argc == 1)
    129  1.1  riastrad 		hostname = argv[0];
    130  1.1  riastrad 
    131  1.1  riastrad 	error = getaddrinfo(hostname, service, &hints, &addrinfo);
    132  1.1  riastrad 	if (error)
    133  1.1  riastrad 		errx(1, "%s", gai_strerror(error));
    134  1.1  riastrad 
    135  1.1  riastrad 	if ((hints.ai_flags & AI_CANONNAME) && (addrinfo != NULL)) {
    136  1.1  riastrad 		if (printf("canonname %s\n", addrinfo->ai_canonname) < 0)
    137  1.1  riastrad 			err(1, "printf");
    138  1.1  riastrad 	}
    139  1.1  riastrad 
    140  1.1  riastrad 	printaddrinfo(addrinfo);
    141  1.1  riastrad 
    142  1.1  riastrad 	freeaddrinfo(addrinfo);
    143  1.1  riastrad 
    144  1.1  riastrad 	return 0;
    145  1.1  riastrad }
    146  1.1  riastrad 
    147  1.1  riastrad static void __dead
    148  1.1  riastrad usage(void)
    149  1.1  riastrad {
    150  1.1  riastrad 
    151  1.1  riastrad 	(void)fprintf(stderr, "Usage: %s", getprogname());
    152  1.1  riastrad 	(void)fprintf(stderr,
    153  1.1  riastrad 	    " [-f <family>] [-p <protocol>] [-t <socktype>] [-s <service>]\n");
    154  1.1  riastrad 	(void)fprintf(stderr, "   [-cnNP] [<hostname>]\n");
    155  1.1  riastrad 	exit(1);
    156  1.1  riastrad }
    157  1.1  riastrad 
    158  1.1  riastrad static bool
    159  1.1  riastrad parse_af(const char *string, int *afp)
    160  1.1  riastrad {
    161  1.1  riastrad 
    162  1.1  riastrad 	return parse_numeric_tabular(string, afp, address_families,
    163  1.1  riastrad 	    __arraycount(address_families));
    164  1.1  riastrad }
    165  1.1  riastrad 
    166  1.1  riastrad static bool
    167  1.1  riastrad parse_protocol(const char *string, int *protop)
    168  1.1  riastrad {
    169  1.1  riastrad 	struct protoent *protoent;
    170  1.1  riastrad 	char *end;
    171  1.1  riastrad 	long value;
    172  1.1  riastrad 
    173  1.1  riastrad 	errno = 0;
    174  1.1  riastrad 	value = strtol(string, &end, 0);
    175  1.1  riastrad 	if ((string[0] == '\0') || (*end != '\0'))
    176  1.1  riastrad 		goto numeric_failed;
    177  1.1  riastrad 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
    178  1.1  riastrad 		goto numeric_failed;
    179  1.1  riastrad 	if ((value > INT_MAX) || (value < INT_MIN))
    180  1.1  riastrad 		goto numeric_failed;
    181  1.1  riastrad 
    182  1.1  riastrad 	*protop = value;
    183  1.1  riastrad 	return true;
    184  1.1  riastrad 
    185  1.1  riastrad numeric_failed:
    186  1.1  riastrad 	protoent = getprotobyname(string);
    187  1.1  riastrad 	if (protoent == NULL)
    188  1.1  riastrad 		goto protoent_failed;
    189  1.1  riastrad 
    190  1.1  riastrad 	*protop = protoent->p_proto;
    191  1.1  riastrad 	return true;
    192  1.1  riastrad 
    193  1.1  riastrad protoent_failed:
    194  1.1  riastrad 	return false;
    195  1.1  riastrad }
    196  1.1  riastrad 
    197  1.1  riastrad static bool
    198  1.1  riastrad parse_socktype(const char *string, int *typep)
    199  1.1  riastrad {
    200  1.1  riastrad 
    201  1.1  riastrad 	return parse_numeric_tabular(string, typep, socket_types,
    202  1.1  riastrad 	    __arraycount(socket_types));
    203  1.1  riastrad }
    204  1.1  riastrad 
    205  1.1  riastrad static bool
    206  1.1  riastrad parse_numeric_tabular(const char *string, int *valuep,
    207  1.1  riastrad     const char *const *table, size_t n)
    208  1.1  riastrad {
    209  1.1  riastrad 	char *end;
    210  1.1  riastrad 	long value;
    211  1.1  riastrad 	size_t i;
    212  1.1  riastrad 
    213  1.1  riastrad 	assert((uintmax_t)n <= (uintmax_t)INT_MAX);
    214  1.1  riastrad 
    215  1.1  riastrad 	errno = 0;
    216  1.1  riastrad 	value = strtol(string, &end, 0);
    217  1.1  riastrad 	if ((string[0] == '\0') || (*end != '\0'))
    218  1.1  riastrad 		goto numeric_failed;
    219  1.1  riastrad 	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
    220  1.1  riastrad 		goto numeric_failed;
    221  1.1  riastrad 	if ((value > INT_MAX) || (value < INT_MIN))
    222  1.1  riastrad 		goto numeric_failed;
    223  1.1  riastrad 
    224  1.1  riastrad 	*valuep = value;
    225  1.1  riastrad 	return true;
    226  1.1  riastrad 
    227  1.1  riastrad numeric_failed:
    228  1.1  riastrad 	for (i = 0; i < n; i++)
    229  1.1  riastrad 		if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
    230  1.1  riastrad 			break;
    231  1.1  riastrad 	if (i == n)
    232  1.1  riastrad 		goto table_failed;
    233  1.1  riastrad 	*valuep = i;
    234  1.1  riastrad 	return true;
    235  1.1  riastrad 
    236  1.1  riastrad table_failed:
    237  1.1  riastrad 	return false;
    238  1.1  riastrad }
    239  1.1  riastrad 
    240  1.1  riastrad static void
    241  1.1  riastrad printaddrinfo(struct addrinfo *addrinfo)
    242  1.1  riastrad {
    243  1.1  riastrad 	struct addrinfo *ai;
    244  1.1  riastrad 	char buf[1024];
    245  1.1  riastrad 	int n;
    246  1.1  riastrad 	struct protoent *protoent;
    247  1.1  riastrad 
    248  1.1  riastrad 	for (ai = addrinfo; ai != NULL; ai = ai->ai_next) {
    249  1.1  riastrad 		/* Print the socket type.  */
    250  1.1  riastrad 		if ((ai->ai_socktype >= 0) &&
    251  1.1  riastrad 		    ((size_t)ai->ai_socktype < __arraycount(socket_types)) &&
    252  1.1  riastrad 		    (socket_types[ai->ai_socktype] != NULL))
    253  1.1  riastrad 			n = printf("%s", socket_types[ai->ai_socktype]);
    254  1.1  riastrad 		else
    255  1.1  riastrad 			n = printf("%d", ai->ai_socktype);
    256  1.1  riastrad 		if (n < 0)
    257  1.1  riastrad 			err(1, "printf");
    258  1.1  riastrad 
    259  1.1  riastrad 		/* Print the address family.  */
    260  1.1  riastrad 		if ((ai->ai_family >= 0) &&
    261  1.1  riastrad 		    ((size_t)ai->ai_family < __arraycount(address_families)) &&
    262  1.1  riastrad 		    (address_families[ai->ai_family] != NULL))
    263  1.1  riastrad 			n = printf(" %s", address_families[ai->ai_family]);
    264  1.1  riastrad 		else
    265  1.1  riastrad 			n = printf(" %d", ai->ai_family);
    266  1.1  riastrad 		if (n < 0)
    267  1.1  riastrad 			err(1, "printf");
    268  1.1  riastrad 
    269  1.1  riastrad 		/* Print the protocol number.  */
    270  1.1  riastrad 		protoent = getprotobynumber(ai->ai_protocol);
    271  1.1  riastrad 		if (protoent == NULL)
    272  1.1  riastrad 			n = printf(" %d", ai->ai_protocol);
    273  1.1  riastrad 		else
    274  1.1  riastrad 			n = printf(" %s", protoent->p_name);
    275  1.1  riastrad 		if (n < 0)
    276  1.1  riastrad 			err(1, "printf");
    277  1.1  riastrad 
    278  1.1  riastrad 		/* Format the sockaddr.  */
    279  1.1  riastrad 		switch (ai->ai_family) {
    280  1.1  riastrad 		case AF_INET:
    281  1.1  riastrad 		case AF_INET6:
    282  1.1  riastrad 			n = sockaddr_snprintf(buf, sizeof(buf), " %a %p",
    283  1.1  riastrad 			    ai->ai_addr);
    284  1.1  riastrad 			break;
    285  1.1  riastrad 
    286  1.1  riastrad 		default:
    287  1.1  riastrad 			n = sockaddr_snprintf(buf, sizeof(buf),
    288  1.1  riastrad 			    "%a %p %I %F %R %S", ai->ai_addr);
    289  1.1  riastrad 		}
    290  1.1  riastrad 
    291  1.1  riastrad 		/*
    292  1.1  riastrad 		 * Check for sockaddr_snprintf failure.
    293  1.1  riastrad 		 *
    294  1.1  riastrad 		 * XXX sockaddr_snprintf's error reporting is botched
    295  1.1  riastrad 		 * -- man page says it sets errno, but if getnameinfo
    296  1.1  riastrad 		 * fails, errno is not where it reports the error...
    297  1.1  riastrad 		 */
    298  1.1  riastrad 		if (n < 0) {
    299  1.1  riastrad 			warnx("sockaddr_snprintf failed");
    300  1.1  riastrad 			continue;
    301  1.1  riastrad 		}
    302  1.1  riastrad 		if (sizeof(buf) <= (size_t)n)
    303  1.1  riastrad 			warnx("truncated sockaddr_snprintf output");
    304  1.1  riastrad 
    305  1.1  riastrad 		/* Print the formatted sockaddr.  */
    306  1.1  riastrad 		if (printf("%s\n", buf) < 0)
    307  1.1  riastrad 			err(1, "printf");
    308  1.1  riastrad 	}
    309  1.1  riastrad }
    310