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