xref: /openbsd/regress/lib/libc/asr/bin/getaddrinfo.c (revision a9e85050)
1*a9e85050Seric /*	$OpenBSD: getaddrinfo.c,v 1.3 2018/12/15 15:16:12 eric Exp $	*/
24c1e55dcSeric /*
34c1e55dcSeric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
44c1e55dcSeric  *
54c1e55dcSeric  * Permission to use, copy, modify, and distribute this software for any
64c1e55dcSeric  * purpose with or without fee is hereby granted, provided that the above
74c1e55dcSeric  * copyright notice and this permission notice appear in all copies.
84c1e55dcSeric  *
94c1e55dcSeric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
104c1e55dcSeric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114c1e55dcSeric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
124c1e55dcSeric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134c1e55dcSeric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
144c1e55dcSeric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
154c1e55dcSeric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
164c1e55dcSeric  */
174c1e55dcSeric #include <sys/types.h>
184c1e55dcSeric #include <sys/socket.h>
194c1e55dcSeric 
204c1e55dcSeric #include <netinet/in.h>
214c1e55dcSeric 
224c1e55dcSeric #include <err.h>
234c1e55dcSeric #include <errno.h>
244c1e55dcSeric #include <getopt.h>
254c1e55dcSeric #include <netdb.h>
264c1e55dcSeric #include <stdio.h>
274c1e55dcSeric #include <stdlib.h>
284c1e55dcSeric #include <string.h>
294c1e55dcSeric 
304c1e55dcSeric #include "common.h"
314c1e55dcSeric 
324c1e55dcSeric static void
usage(void)334c1e55dcSeric usage(void)
344c1e55dcSeric {
354c1e55dcSeric 	extern const char * __progname;
364c1e55dcSeric 
374c1e55dcSeric 	fprintf(stderr, "usage: %s [-CHSPe] [-f family] [-p proto] "
384c1e55dcSeric 		"[-s servname]\n	[-t socktype] <host...>\n", __progname);
394c1e55dcSeric 	exit(1);
404c1e55dcSeric }
414c1e55dcSeric 
424c1e55dcSeric int
main(int argc,char * argv[])434c1e55dcSeric main(int argc, char *argv[])
444c1e55dcSeric {
454c1e55dcSeric 	struct addrinfo		*ai, *res, hints;
464c1e55dcSeric 	char			*servname = NULL, *host;
474c1e55dcSeric 	int			 i, ch;
484c1e55dcSeric 
494c1e55dcSeric 	memset(&hints, 0, sizeof hints);
504c1e55dcSeric 
51*a9e85050Seric 	while((ch = getopt(argc, argv, "CFHPR:Sef:p:s:t:")) !=  -1) {
524c1e55dcSeric 		switch(ch) {
534c1e55dcSeric 		case 'C':
544c1e55dcSeric 			hints.ai_flags |= AI_CANONNAME;
554c1e55dcSeric 			break;
564c1e55dcSeric 		case 'F':
574c1e55dcSeric 			hints.ai_flags |= AI_FQDN;
584c1e55dcSeric 			break;
594c1e55dcSeric 		case 'H':
604c1e55dcSeric 			hints.ai_flags |= AI_NUMERICHOST;
614c1e55dcSeric 			break;
624c1e55dcSeric 		case 'P':
634c1e55dcSeric 			hints.ai_flags |= AI_PASSIVE;
644c1e55dcSeric 			break;
65*a9e85050Seric 		case 'R':
66*a9e85050Seric 			parseresopt(optarg);
67*a9e85050Seric 			break;
684c1e55dcSeric 		case 'S':
694c1e55dcSeric 			hints.ai_flags |= AI_NUMERICSERV;
704c1e55dcSeric 			break;
714c1e55dcSeric 		case 'e':
724c1e55dcSeric 			long_err += 1;
734c1e55dcSeric 			break;
744c1e55dcSeric 		case 'f':
754c1e55dcSeric 			if (!strcmp(optarg, "inet"))
764c1e55dcSeric 				hints.ai_family = AF_INET;
774c1e55dcSeric 			else if (!strcmp(optarg, "inet6"))
784c1e55dcSeric 				hints.ai_family = AF_INET6;
794c1e55dcSeric 			else
804c1e55dcSeric 				usage();
814c1e55dcSeric 			break;
824c1e55dcSeric 		case 'p':
834c1e55dcSeric 			if (!strcmp(optarg, "udp"))
844c1e55dcSeric 				hints.ai_protocol = IPPROTO_UDP;
854c1e55dcSeric 			else if (!strcmp(optarg, "tcp"))
864c1e55dcSeric 				hints.ai_protocol = IPPROTO_TCP;
87f2033d2fSeric 			else if (!strcmp(optarg, "icmp"))
88f2033d2fSeric 				hints.ai_protocol = IPPROTO_ICMP;
89f2033d2fSeric 			else if (!strcmp(optarg, "icmpv6"))
90f2033d2fSeric 				hints.ai_protocol = IPPROTO_ICMPV6;
914c1e55dcSeric 			else
924c1e55dcSeric 				usage();
934c1e55dcSeric 			break;
944c1e55dcSeric 		case 's':
954c1e55dcSeric 			servname = optarg;
964c1e55dcSeric 			break;
974c1e55dcSeric 		case 't':
984c1e55dcSeric 			if (!strcmp(optarg, "stream"))
994c1e55dcSeric 				hints.ai_socktype = SOCK_STREAM;
1004c1e55dcSeric 			else if (!strcmp(optarg, "dgram"))
1014c1e55dcSeric 				hints.ai_socktype = SOCK_DGRAM;
1024c1e55dcSeric 			else if (!strcmp(optarg, "raw"))
1034c1e55dcSeric 				hints.ai_socktype = SOCK_RAW;
1044c1e55dcSeric 			else
1054c1e55dcSeric 				usage();
1064c1e55dcSeric 			break;
1074c1e55dcSeric 		default:
1084c1e55dcSeric 			usage();
1094c1e55dcSeric 			/* NOTREACHED */
1104c1e55dcSeric 		}
1114c1e55dcSeric 	}
1124c1e55dcSeric 	argc -= optind;
1134c1e55dcSeric 	argv += optind;
1144c1e55dcSeric 
1154c1e55dcSeric 	for(i = 0; i < argc; i++) {
1164c1e55dcSeric 
1174c1e55dcSeric 		if (i)
1184c1e55dcSeric 			printf("\n");
1194c1e55dcSeric 		printf("===> \"%s\"\n", argv[i]);
1204c1e55dcSeric 		host = gethostarg(argv[i]);
1214c1e55dcSeric 
1224c1e55dcSeric 		errno = 0;
1234c1e55dcSeric 		h_errno = 0;
1244c1e55dcSeric 		gai_errno = 0;
1254c1e55dcSeric 		rrset_errno = 0;
1264c1e55dcSeric 
1274c1e55dcSeric 		gai_errno = getaddrinfo(host, servname, &hints, &ai);
1284c1e55dcSeric 
1294c1e55dcSeric 		print_errors();
1304c1e55dcSeric 		if (gai_errno == 0) {
1314c1e55dcSeric 			for (res = ai; res; res = res->ai_next)
1324c1e55dcSeric 				print_addrinfo(res);
1334c1e55dcSeric 			freeaddrinfo(ai);
1344c1e55dcSeric 		}
1354c1e55dcSeric 	}
1364c1e55dcSeric 
1374c1e55dcSeric 	return (0);
1384c1e55dcSeric }
139