Home | History | Annotate | Line # | Download | only in getaddrinfo
      1 /*	$NetBSD: h_gai.c,v 1.2 2019/02/03 10:48:47 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <netdb.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <unistd.h>
     37 
     38 #include <sys/types.h>
     39 #include <sys/socket.h>
     40 
     41 #include <netinet/in.h>
     42 
     43 #include <arpa/inet.h>
     44 
     45 struct addrinfo ai;
     46 
     47 char host[NI_MAXHOST];
     48 char serv[NI_MAXSERV];
     49 int vflag = 0;
     50 
     51 static void usage(void);
     52 static void print1(const char *, const struct addrinfo *, char *, char *);
     53 int main(int, char *[]);
     54 
     55 static void
     56 usage()
     57 {
     58 	fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
     59 }
     60 
     61 static void
     62 print1(const char *title, const struct addrinfo *res, char *h, char *s)
     63 {
     64 	const char *start, *end;
     65 	int error;
     66 	const int niflag = NI_NUMERICHOST;
     67 
     68 	if (res->ai_addr) {
     69 		error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
     70 				    host, sizeof(host), serv, sizeof(serv),
     71 				    niflag);
     72 		h = host;
     73 		s = serv;
     74 	} else
     75 		error = 0;
     76 
     77 	if (vflag) {
     78 		start = "\t";
     79 		end = "\n";
     80 	} else {
     81 		start = " ";
     82 		end = "";
     83 	}
     84 	printf("%s%s", title, end);
     85 	printf("%sflags 0x%x%s", start, res->ai_flags, end);
     86 	printf("%sfamily %d%s", start, res->ai_family, end);
     87 	printf("%ssocktype %d%s", start, res->ai_socktype, end);
     88 	printf("%sprotocol %d%s", start, res->ai_protocol, end);
     89 	printf("%saddrlen %d%s", start, res->ai_addrlen, end);
     90 	if (error)
     91 		printf("%serror %d%s", start, error, end);
     92 	else {
     93 		printf("%shost %s%s", start, h, end);
     94 		printf("%sserv %s%s", start, s, end);
     95 	}
     96 #if 0
     97 	if (res->ai_canonname)
     98 		printf("%scname \"%s\"%s", start, res->ai_canonname, end);
     99 #endif
    100 	if (!vflag)
    101 		printf("\n");
    102 
    103 }
    104 
    105 int
    106 main(int argc, char *argv[])
    107 {
    108 	struct addrinfo *res;
    109 	int error, i;
    110 	char *p, *q;
    111 	extern int optind;
    112 	extern char *optarg;
    113 	int c;
    114 	char nbuf[14];
    115 
    116 	memset(&ai, 0, sizeof(ai));
    117 	ai.ai_family = PF_UNSPEC;
    118 	ai.ai_flags |= AI_CANONNAME;
    119 	while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
    120 		switch (c) {
    121 		case 'D':
    122 			ai.ai_socktype = SOCK_DGRAM;
    123 			break;
    124 		case 'f':
    125 			ai.ai_family = atoi(optarg);
    126 			break;
    127 		case 'p':
    128 			ai.ai_protocol = atoi(optarg);
    129 			break;
    130 		case 'P':
    131 			ai.ai_flags |= AI_PASSIVE;
    132 			break;
    133 		case 'R':
    134 			ai.ai_socktype = SOCK_RAW;
    135 			break;
    136 		case 's':
    137 			ai.ai_socktype = atoi(optarg);
    138 			break;
    139 		case 'S':
    140 			ai.ai_socktype = SOCK_STREAM;
    141 			break;
    142 		case 'v':
    143 			vflag++;
    144 			break;
    145 		case '4':
    146 			ai.ai_family = PF_INET;
    147 			break;
    148 		case '6':
    149 			ai.ai_family = PF_INET6;
    150 			break;
    151 		default:
    152 			usage();
    153 			exit(1);
    154 		}
    155 	}
    156 	argc -= optind;
    157 	argv += optind;
    158 
    159 	if (argc != 2){
    160 		usage();
    161 		exit(1);
    162 	}
    163 
    164 	p = *argv[0] ? argv[0] : NULL;
    165 	q = *argv[1] ? argv[1] : NULL;
    166 
    167 	strncpy(nbuf, "(empty)", sizeof(nbuf));
    168 	print1("arg:", &ai, p ? p : nbuf , q ? q : nbuf);
    169 
    170 	error = getaddrinfo(p, q, &ai, &res);
    171 	if (error) {
    172 		printf("%s\n", gai_strerror(error));
    173 		exit(1);
    174 	}
    175 
    176 	i = 1;
    177 	do {
    178 		snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
    179 		print1(nbuf, res, NULL, NULL);
    180 
    181 		i++;
    182 	} while ((res = res->ai_next) != NULL);
    183 	printf("\n");
    184 
    185 	exit(0);
    186 }
    187