xref: /freebsd/contrib/netbsd-tests/lib/libc/net/h_hostent.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*	$NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos 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 Christos Zoulas.
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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos Exp $");
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <nsswitch.h>
37 #include <netdb.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <err.h>
42 
43 #include <netinet/in.h>
44 #include <sys/types.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
47 
48 #include "hostent.h"
49 
50 extern const char *__res_conf_name;
51 
52 static void
53 phostent(const struct hostent *h)
54 {
55 	size_t i;
56 	char buf[1024];
57 	const int af = h->h_length == NS_INADDRSZ ? AF_INET : AF_INET6;
58 
59 	printf("name=%s, length=%d, addrtype=%d, aliases=[",
60 	    h->h_name, h->h_length, h->h_addrtype);
61 
62 	for (i = 0; h->h_aliases[i]; i++)
63 		printf("%s%s", i == 0 ? "" : " ", h->h_aliases[i]);
64 
65 	printf("] addr_list=[");
66 
67 	for (i = 0; h->h_addr_list[i]; i++)
68 		printf("%s%s", i == 0 ? "" : " ", inet_ntop(af,
69 		    h->h_addr_list[i], buf, (socklen_t)sizeof(buf)));
70 
71 	printf("]\n");
72 }
73 
74 static void
75 usage(void)
76 {
77 	(void)fprintf(stderr, "Usage: %s [-f <hostsfile>] "
78 	    "[-t <any|dns|nis|files>] "
79 	    "[-46a] <name|address>\n", getprogname());
80 	exit(EXIT_FAILURE);
81 }
82 
83 static void
84 getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...)
85 {
86 	va_list ap;
87 	int e;
88 
89 	va_start(ap, info);
90 	e = (*f)(info, NULL, ap);
91 	va_end(ap);
92 	switch (e) {
93 	case NS_SUCCESS:
94 		phostent(info->hp);
95 		break;
96 	default:
97 		printf("error %d\n", e);
98 		break;
99 	}
100 }
101 
102 static void
103 geta(struct hostent *hp) {
104 	if (hp == NULL)
105 		printf("error %d\n", h_errno);
106 	else
107 		phostent(hp);
108 }
109 
110 int
111 main(int argc, char *argv[])
112 {
113 	int (*f)(void *, void *, va_list) = NULL;
114 	const char *type = "any";
115 	int c, af, e, byaddr, len;
116 	struct hostent hent;
117 	struct getnamaddr info;
118 	char buf[4096];
119 
120 	af = AF_INET;
121 	byaddr = 0;
122 	len = 0;
123 	info.hp = &hent;
124 	info.buf = buf;
125 	info.buflen = sizeof(buf);
126 	info.he = &e;
127 
128 	while ((c = getopt(argc, argv, "46af:r:t:")) != -1) {
129 		switch (c) {
130 		case '4':
131 			af = AF_INET;
132 			break;
133 		case '6':
134 			af = AF_INET6;
135 			break;
136 		case 'a':
137 			byaddr++;
138 			break;
139 		case 'f':
140 			_hf_sethostsfile(optarg);
141 			break;
142 		case 'r':
143 			__res_conf_name = optarg;
144 			break;
145 		case 't':
146 			type = optarg;
147 			break;
148 		default:
149 			usage();
150 		}
151 	}
152 
153 	argc -= optind;
154 	argv += optind;
155 
156 	if (argc != 1)
157 		usage();
158 
159 	switch (*type) {
160 	case 'a':
161 		break;
162 	case 'd':
163 		f = byaddr ? _dns_gethtbyaddr : _dns_gethtbyname;
164 		break;
165 #ifdef YP
166 	case 'n':
167 		f = byaddr ? _yp_gethtbyaddr : _yp_gethtbyname;
168 		break;
169 #endif
170 	case 'f':
171 		f = byaddr ? _hf_gethtbyaddr : _hf_gethtbyname;
172 		break;
173 	default:
174 		errx(EXIT_FAILURE, "Unknown db type `%s'", type);
175 	}
176 
177 	if (byaddr) {
178 		struct in6_addr addr;
179 		af = strchr(*argv, ':') ? AF_INET6 : AF_INET;
180 		len = af == AF_INET ? NS_INADDRSZ : NS_IN6ADDRSZ;
181 		if (inet_pton(af, *argv, &addr) == -1)
182 			err(EXIT_FAILURE, "Can't parse `%s'", *argv);
183 		if (*type == 'a')
184 			geta(gethostbyaddr((const char *)&addr, len, af));
185 		else
186 			getby(f, &info, &addr, len, af);
187 	} else {
188 		if (*type == 'a')
189 			geta(gethostbyname2(*argv, af));
190 		else
191 			getby(f, &info, *argv, len, af);
192 	}
193 
194 	return 0;
195 }
196