1/*	$OpenBSD: getaddrinfo.c,v 1.3 2018/12/15 15:16:12 eric Exp $	*/
2/*
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include <sys/types.h>
18#include <sys/socket.h>
19
20#include <netinet/in.h>
21
22#include <err.h>
23#include <errno.h>
24#include <getopt.h>
25#include <netdb.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "common.h"
31
32static void
33usage(void)
34{
35	extern const char * __progname;
36
37	fprintf(stderr, "usage: %s [-CHSPe] [-f family] [-p proto] "
38		"[-s servname]\n	[-t socktype] <host...>\n", __progname);
39	exit(1);
40}
41
42int
43main(int argc, char *argv[])
44{
45	struct addrinfo		*ai, *res, hints;
46	char			*servname = NULL, *host;
47	int			 i, ch;
48
49	memset(&hints, 0, sizeof hints);
50
51	while((ch = getopt(argc, argv, "CFHPR:Sef:p:s:t:")) !=  -1) {
52		switch(ch) {
53		case 'C':
54			hints.ai_flags |= AI_CANONNAME;
55			break;
56		case 'F':
57			hints.ai_flags |= AI_FQDN;
58			break;
59		case 'H':
60			hints.ai_flags |= AI_NUMERICHOST;
61			break;
62		case 'P':
63			hints.ai_flags |= AI_PASSIVE;
64			break;
65		case 'R':
66			parseresopt(optarg);
67			break;
68		case 'S':
69			hints.ai_flags |= AI_NUMERICSERV;
70			break;
71		case 'e':
72			long_err += 1;
73			break;
74		case 'f':
75			if (!strcmp(optarg, "inet"))
76				hints.ai_family = AF_INET;
77			else if (!strcmp(optarg, "inet6"))
78				hints.ai_family = AF_INET6;
79			else
80				usage();
81			break;
82		case 'p':
83			if (!strcmp(optarg, "udp"))
84				hints.ai_protocol = IPPROTO_UDP;
85			else if (!strcmp(optarg, "tcp"))
86				hints.ai_protocol = IPPROTO_TCP;
87			else if (!strcmp(optarg, "icmp"))
88				hints.ai_protocol = IPPROTO_ICMP;
89			else if (!strcmp(optarg, "icmpv6"))
90				hints.ai_protocol = IPPROTO_ICMPV6;
91			else
92				usage();
93			break;
94		case 's':
95			servname = optarg;
96			break;
97		case 't':
98			if (!strcmp(optarg, "stream"))
99				hints.ai_socktype = SOCK_STREAM;
100			else if (!strcmp(optarg, "dgram"))
101				hints.ai_socktype = SOCK_DGRAM;
102			else if (!strcmp(optarg, "raw"))
103				hints.ai_socktype = SOCK_RAW;
104			else
105				usage();
106			break;
107		default:
108			usage();
109			/* NOTREACHED */
110		}
111	}
112	argc -= optind;
113	argv += optind;
114
115	for(i = 0; i < argc; i++) {
116
117		if (i)
118			printf("\n");
119		printf("===> \"%s\"\n", argv[i]);
120		host = gethostarg(argv[i]);
121
122		errno = 0;
123		h_errno = 0;
124		gai_errno = 0;
125		rrset_errno = 0;
126
127		gai_errno = getaddrinfo(host, servname, &hints, &ai);
128
129		print_errors();
130		if (gai_errno == 0) {
131			for (res = ai; res; res = res->ai_next)
132				print_addrinfo(res);
133			freeaddrinfo(ai);
134		}
135	}
136
137	return (0);
138}
139