h_hostent.c revision 1.2.8.2 1 1.2.8.2 tls /* $NetBSD: h_hostent.c,v 1.2.8.2 2014/08/20 00:04:50 tls Exp $ */
2 1.2.8.2 tls
3 1.2.8.2 tls /*-
4 1.2.8.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.8.2 tls * All rights reserved.
6 1.2.8.2 tls *
7 1.2.8.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.8.2 tls * by Christos Zoulas.
9 1.2.8.2 tls *
10 1.2.8.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.8.2 tls * modification, are permitted provided that the following conditions
12 1.2.8.2 tls * are met:
13 1.2.8.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.8.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.8.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.8.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.8.2 tls *
19 1.2.8.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.8.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.8.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.8.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.8.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.8.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.8.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.8.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.8.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.8.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.8.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.8.2 tls */
31 1.2.8.2 tls #include <sys/cdefs.h>
32 1.2.8.2 tls __RCSID("$NetBSD: h_hostent.c,v 1.2.8.2 2014/08/20 00:04:50 tls Exp $");
33 1.2.8.2 tls
34 1.2.8.2 tls #include <stdio.h>
35 1.2.8.2 tls #include <string.h>
36 1.2.8.2 tls #include <nsswitch.h>
37 1.2.8.2 tls #include <netdb.h>
38 1.2.8.2 tls #include <stdint.h>
39 1.2.8.2 tls #include <stdlib.h>
40 1.2.8.2 tls #include <unistd.h>
41 1.2.8.2 tls #include <err.h>
42 1.2.8.2 tls
43 1.2.8.2 tls #include <netinet/in.h>
44 1.2.8.2 tls #include <sys/types.h>
45 1.2.8.2 tls #include <arpa/nameser.h>
46 1.2.8.2 tls #include <arpa/inet.h>
47 1.2.8.2 tls
48 1.2.8.2 tls #include "hostent.h"
49 1.2.8.2 tls
50 1.2.8.2 tls extern const char *__res_conf_name;
51 1.2.8.2 tls
52 1.2.8.2 tls static void
53 1.2.8.2 tls phostent(const struct hostent *h)
54 1.2.8.2 tls {
55 1.2.8.2 tls size_t i;
56 1.2.8.2 tls char buf[1024];
57 1.2.8.2 tls const int af = h->h_length == NS_INADDRSZ ? AF_INET : AF_INET6;
58 1.2.8.2 tls
59 1.2.8.2 tls printf("name=%s, length=%d, addrtype=%d, aliases=[",
60 1.2.8.2 tls h->h_name, h->h_length, h->h_addrtype);
61 1.2.8.2 tls
62 1.2.8.2 tls for (i = 0; h->h_aliases[i]; i++)
63 1.2.8.2 tls printf("%s%s", i == 0 ? "" : " ", h->h_aliases[i]);
64 1.2.8.2 tls
65 1.2.8.2 tls printf("] addr_list=[");
66 1.2.8.2 tls
67 1.2.8.2 tls for (i = 0; h->h_addr_list[i]; i++)
68 1.2.8.2 tls printf("%s%s", i == 0 ? "" : " ", inet_ntop(af,
69 1.2.8.2 tls h->h_addr_list[i], buf, (socklen_t)sizeof(buf)));
70 1.2.8.2 tls
71 1.2.8.2 tls printf("]\n");
72 1.2.8.2 tls }
73 1.2.8.2 tls
74 1.2.8.2 tls static void
75 1.2.8.2 tls usage(void)
76 1.2.8.2 tls {
77 1.2.8.2 tls (void)fprintf(stderr, "Usage: %s [-f <hostsfile>] "
78 1.2.8.2 tls "[-t <any|dns|nis|files>] "
79 1.2.8.2 tls "[-46a] <name|address>\n", getprogname());
80 1.2.8.2 tls exit(EXIT_FAILURE);
81 1.2.8.2 tls }
82 1.2.8.2 tls
83 1.2.8.2 tls static void
84 1.2.8.2 tls getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...)
85 1.2.8.2 tls {
86 1.2.8.2 tls va_list ap;
87 1.2.8.2 tls int e;
88 1.2.8.2 tls
89 1.2.8.2 tls va_start(ap, info);
90 1.2.8.2 tls e = (*f)(info, NULL, ap);
91 1.2.8.2 tls va_end(ap);
92 1.2.8.2 tls switch (e) {
93 1.2.8.2 tls case NS_SUCCESS:
94 1.2.8.2 tls phostent(info->hp);
95 1.2.8.2 tls break;
96 1.2.8.2 tls default:
97 1.2.8.2 tls printf("error %d\n", e);
98 1.2.8.2 tls break;
99 1.2.8.2 tls }
100 1.2.8.2 tls }
101 1.2.8.2 tls
102 1.2.8.2 tls static void
103 1.2.8.2 tls geta(struct hostent *hp) {
104 1.2.8.2 tls if (hp == NULL)
105 1.2.8.2 tls printf("error %d\n", h_errno);
106 1.2.8.2 tls else
107 1.2.8.2 tls phostent(hp);
108 1.2.8.2 tls }
109 1.2.8.2 tls
110 1.2.8.2 tls int
111 1.2.8.2 tls main(int argc, char *argv[])
112 1.2.8.2 tls {
113 1.2.8.2 tls int (*f)(void *, void *, va_list) = NULL;
114 1.2.8.2 tls const char *type = "any";
115 1.2.8.2 tls int c, af, e, byaddr, len;
116 1.2.8.2 tls struct hostent hent;
117 1.2.8.2 tls struct getnamaddr info;
118 1.2.8.2 tls char buf[4096];
119 1.2.8.2 tls
120 1.2.8.2 tls af = AF_INET;
121 1.2.8.2 tls byaddr = 0;
122 1.2.8.2 tls len = 0;
123 1.2.8.2 tls info.hp = &hent;
124 1.2.8.2 tls info.buf = buf;
125 1.2.8.2 tls info.buflen = sizeof(buf);
126 1.2.8.2 tls info.he = &e;
127 1.2.8.2 tls
128 1.2.8.2 tls while ((c = getopt(argc, argv, "46af:r:t:")) != -1) {
129 1.2.8.2 tls switch (c) {
130 1.2.8.2 tls case '4':
131 1.2.8.2 tls af = AF_INET;
132 1.2.8.2 tls break;
133 1.2.8.2 tls case '6':
134 1.2.8.2 tls af = AF_INET6;
135 1.2.8.2 tls break;
136 1.2.8.2 tls case 'a':
137 1.2.8.2 tls byaddr++;
138 1.2.8.2 tls break;
139 1.2.8.2 tls case 'f':
140 1.2.8.2 tls _hf_sethostsfile(optarg);
141 1.2.8.2 tls break;
142 1.2.8.2 tls case 'r':
143 1.2.8.2 tls __res_conf_name = optarg;
144 1.2.8.2 tls break;
145 1.2.8.2 tls case 't':
146 1.2.8.2 tls type = optarg;
147 1.2.8.2 tls break;
148 1.2.8.2 tls default:
149 1.2.8.2 tls usage();
150 1.2.8.2 tls }
151 1.2.8.2 tls }
152 1.2.8.2 tls
153 1.2.8.2 tls argc -= optind;
154 1.2.8.2 tls argv += optind;
155 1.2.8.2 tls
156 1.2.8.2 tls if (argc != 1)
157 1.2.8.2 tls usage();
158 1.2.8.2 tls
159 1.2.8.2 tls switch (*type) {
160 1.2.8.2 tls case 'a':
161 1.2.8.2 tls break;
162 1.2.8.2 tls case 'd':
163 1.2.8.2 tls f = byaddr ? _dns_gethtbyaddr : _dns_gethtbyname;
164 1.2.8.2 tls break;
165 1.2.8.2 tls #ifdef YP
166 1.2.8.2 tls case 'n':
167 1.2.8.2 tls f = byaddr ? _yp_gethtbyaddr : _yp_gethtbyname;
168 1.2.8.2 tls break;
169 1.2.8.2 tls #endif
170 1.2.8.2 tls case 'f':
171 1.2.8.2 tls f = byaddr ? _hf_gethtbyaddr : _hf_gethtbyname;
172 1.2.8.2 tls break;
173 1.2.8.2 tls default:
174 1.2.8.2 tls errx(EXIT_FAILURE, "Unknown db type `%s'", type);
175 1.2.8.2 tls }
176 1.2.8.2 tls
177 1.2.8.2 tls if (byaddr) {
178 1.2.8.2 tls struct in6_addr addr;
179 1.2.8.2 tls af = strchr(*argv, ':') ? AF_INET6 : AF_INET;
180 1.2.8.2 tls len = af == AF_INET ? NS_INADDRSZ : NS_IN6ADDRSZ;
181 1.2.8.2 tls if (inet_pton(af, *argv, &addr) == -1)
182 1.2.8.2 tls err(EXIT_FAILURE, "Can't parse `%s'", *argv);
183 1.2.8.2 tls if (*type == 'a')
184 1.2.8.2 tls geta(gethostbyaddr((const char *)&addr, len, af));
185 1.2.8.2 tls else
186 1.2.8.2 tls getby(f, &info, &addr, len, af);
187 1.2.8.2 tls } else {
188 1.2.8.2 tls if (*type == 'a')
189 1.2.8.2 tls geta(gethostbyname2(*argv, af));
190 1.2.8.2 tls else
191 1.2.8.2 tls getby(f, &info, *argv, len, af);
192 1.2.8.2 tls }
193 1.2.8.2 tls
194 1.2.8.2 tls return 0;
195 1.2.8.2 tls }
196