gethnamaddr.c revision 1.87 1 1.87 christos /* $NetBSD: gethnamaddr.c,v 1.87 2014/01/17 02:03:44 christos Exp $ */
2 1.2 mrg
3 1.1 mrg /*
4 1.1 mrg * ++Copyright++ 1985, 1988, 1993
5 1.1 mrg * -
6 1.1 mrg * Copyright (c) 1985, 1988, 1993
7 1.1 mrg * The Regents of the University of California. All rights reserved.
8 1.1 mrg *
9 1.1 mrg * Redistribution and use in source and binary forms, with or without
10 1.1 mrg * modification, are permitted provided that the following conditions
11 1.1 mrg * are met:
12 1.1 mrg * 1. Redistributions of source code must retain the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer.
14 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 mrg * notice, this list of conditions and the following disclaimer in the
16 1.1 mrg * documentation and/or other materials provided with the distribution.
17 1.55 agc * 3. Neither the name of the University nor the names of its contributors
18 1.1 mrg * may be used to endorse or promote products derived from this software
19 1.1 mrg * without specific prior written permission.
20 1.1 mrg *
21 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 mrg * SUCH DAMAGE.
32 1.1 mrg * -
33 1.1 mrg * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 1.1 mrg *
35 1.1 mrg * Permission to use, copy, modify, and distribute this software for any
36 1.1 mrg * purpose with or without fee is hereby granted, provided that the above
37 1.1 mrg * copyright notice and this permission notice appear in all copies, and that
38 1.1 mrg * the name of Digital Equipment Corporation not be used in advertising or
39 1.1 mrg * publicity pertaining to distribution of the document or software without
40 1.1 mrg * specific, written prior permission.
41 1.1 mrg *
42 1.1 mrg * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 1.1 mrg * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 1.1 mrg * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 1.1 mrg * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 1.1 mrg * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 1.1 mrg * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 1.1 mrg * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 1.1 mrg * SOFTWARE.
50 1.1 mrg * -
51 1.1 mrg * --Copyright--
52 1.1 mrg */
53 1.1 mrg
54 1.2 mrg #include <sys/cdefs.h>
55 1.1 mrg #if defined(LIBC_SCCS) && !defined(lint)
56 1.2 mrg #if 0
57 1.1 mrg static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 1.4 perry static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
59 1.2 mrg #else
60 1.87 christos __RCSID("$NetBSD: gethnamaddr.c,v 1.87 2014/01/17 02:03:44 christos Exp $");
61 1.2 mrg #endif
62 1.1 mrg #endif /* LIBC_SCCS and not lint */
63 1.1 mrg
64 1.6 kleink #if defined(_LIBC)
65 1.3 thorpej #include "namespace.h"
66 1.6 kleink #endif
67 1.1 mrg #include <sys/param.h>
68 1.1 mrg #include <sys/socket.h>
69 1.1 mrg #include <netinet/in.h>
70 1.1 mrg #include <arpa/inet.h>
71 1.1 mrg #include <arpa/nameser.h>
72 1.1 mrg
73 1.25 lukem #include <assert.h>
74 1.25 lukem #include <ctype.h>
75 1.25 lukem #include <errno.h>
76 1.1 mrg #include <netdb.h>
77 1.1 mrg #include <resolv.h>
78 1.44 wiz #include <stdarg.h>
79 1.25 lukem #include <stdio.h>
80 1.1 mrg #include <syslog.h>
81 1.1 mrg
82 1.1 mrg #ifndef LOG_AUTH
83 1.1 mrg # define LOG_AUTH 0
84 1.1 mrg #endif
85 1.1 mrg
86 1.1 mrg #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
87 1.1 mrg
88 1.9 lukem #include <nsswitch.h>
89 1.2 mrg #include <stdlib.h>
90 1.2 mrg #include <string.h>
91 1.1 mrg
92 1.2 mrg #ifdef YP
93 1.2 mrg #include <rpc/rpc.h>
94 1.2 mrg #include <rpcsvc/yp_prot.h>
95 1.2 mrg #include <rpcsvc/ypclnt.h>
96 1.5 kleink #endif
97 1.5 kleink
98 1.80 christos #include "hostent.h"
99 1.80 christos
100 1.6 kleink #if defined(_LIBC) && defined(__weak_alias)
101 1.29 mycroft __weak_alias(gethostbyaddr,_gethostbyaddr)
102 1.29 mycroft __weak_alias(gethostbyname,_gethostbyname)
103 1.63 ginsbach __weak_alias(gethostent,_gethostent)
104 1.1 mrg #endif
105 1.1 mrg
106 1.79 christos #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
107 1.79 christos (ok)(nm) != 0)
108 1.79 christos #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
109 1.79 christos #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
110 1.79 christos
111 1.87 christos #define addalias(d, s, arr, siz) do { \
112 1.87 christos if (d >= &arr[siz - 1]) { \
113 1.87 christos char **xptr = realloc(arr, siz + 10); \
114 1.87 christos if (xptr == NULL) \
115 1.87 christos goto nospc; \
116 1.87 christos d = xptr + (d - arr); \
117 1.87 christos arr = xptr; \
118 1.87 christos siz += 10; \
119 1.87 christos } \
120 1.87 christos *d++ = s; \
121 1.86 christos } while (/*CONSTCOND*/0)
122 1.86 christos
123 1.87 christos #define setup(arr, siz) do { \
124 1.87 christos arr = malloc(siz = 10); \
125 1.87 christos if (arr == NULL) \
126 1.87 christos goto nospc; \
127 1.86 christos } while (/*CONSTCOND*/0)
128 1.86 christos
129 1.79 christos
130 1.1 mrg static const char AskedForGot[] =
131 1.80 christos "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
132 1.1 mrg
133 1.1 mrg
134 1.2 mrg #ifdef YP
135 1.2 mrg static char *__ypdomain;
136 1.2 mrg #endif
137 1.2 mrg
138 1.52 itojun #define MAXPACKET (64*1024)
139 1.1 mrg
140 1.1 mrg typedef union {
141 1.80 christos HEADER hdr;
142 1.80 christos u_char buf[MAXPACKET];
143 1.1 mrg } querybuf;
144 1.1 mrg
145 1.1 mrg typedef union {
146 1.80 christos int32_t al;
147 1.80 christos char ac;
148 1.1 mrg } align;
149 1.1 mrg
150 1.1 mrg #ifdef DEBUG
151 1.76 christos static void debugprintf(const char *, res_state, ...)
152 1.59 christos __attribute__((__format__(__printf__, 1, 3)));
153 1.2 mrg #endif
154 1.59 christos static struct hostent *getanswer(const querybuf *, int, const char *, int,
155 1.80 christos res_state, struct hostent *, char *, size_t, int *);
156 1.59 christos static void map_v4v6_address(const char *, char *);
157 1.59 christos static void map_v4v6_hostent(struct hostent *, char **, char *);
158 1.66 tsarna static void addrsort(char **, int, res_state);
159 1.2 mrg
160 1.59 christos void dns_service(void);
161 1.39 christos #undef dn_skipname
162 1.59 christos int dn_skipname(const u_char *, const u_char *);
163 1.80 christos
164 1.2 mrg #ifdef YP
165 1.80 christos static struct hostent *_yp_hostent(char *, int, struct getnamaddr *);
166 1.2 mrg #endif
167 1.2 mrg
168 1.80 christos static struct hostent *gethostbyname_internal(const char *, int, res_state,
169 1.80 christos struct hostent *, char *, size_t, int *);
170 1.59 christos
171 1.12 lukem static const ns_src default_dns_files[] = {
172 1.27 lukem { NSSRC_FILES, NS_SUCCESS },
173 1.11 lukem { NSSRC_DNS, NS_SUCCESS },
174 1.71 christos { 0, 0 }
175 1.11 lukem };
176 1.11 lukem
177 1.2 mrg
178 1.2 mrg #ifdef DEBUG
179 1.1 mrg static void
180 1.76 christos debugprintf(const char *msg, res_state res, ...)
181 1.1 mrg {
182 1.25 lukem _DIAGASSERT(msg != NULL);
183 1.25 lukem
184 1.59 christos if (res->options & RES_DEBUG) {
185 1.1 mrg int save = errno;
186 1.38 sommerfe va_list ap;
187 1.1 mrg
188 1.68 christos va_start (ap, res);
189 1.38 sommerfe vprintf(msg, ap);
190 1.38 sommerfe va_end (ap);
191 1.38 sommerfe
192 1.1 mrg errno = save;
193 1.1 mrg }
194 1.1 mrg }
195 1.1 mrg #else
196 1.76 christos # define debugprintf(msg, res, num) /*nada*/
197 1.1 mrg #endif
198 1.1 mrg
199 1.32 itojun #define BOUNDED_INCR(x) \
200 1.32 itojun do { \
201 1.58 enami cp += (x); \
202 1.58 enami if (cp > eom) { \
203 1.32 itojun h_errno = NO_RECOVERY; \
204 1.59 christos return NULL; \
205 1.32 itojun } \
206 1.34 christos } while (/*CONSTCOND*/0)
207 1.32 itojun
208 1.32 itojun #define BOUNDS_CHECK(ptr, count) \
209 1.32 itojun do { \
210 1.58 enami if ((ptr) + (count) > eom) { \
211 1.32 itojun h_errno = NO_RECOVERY; \
212 1.59 christos return NULL; \
213 1.32 itojun } \
214 1.34 christos } while (/*CONSTCOND*/0)
215 1.32 itojun
216 1.1 mrg static struct hostent *
217 1.59 christos getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
218 1.80 christos res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
219 1.1 mrg {
220 1.25 lukem const HEADER *hp;
221 1.25 lukem const u_char *cp;
222 1.25 lukem int n;
223 1.80 christos size_t qlen;
224 1.32 itojun const u_char *eom, *erdata;
225 1.46 itojun char *bp, **ap, **hap, *ep;
226 1.46 itojun int type, class, ancount, qdcount;
227 1.1 mrg int haveanswer, had_error;
228 1.1 mrg int toobig = 0;
229 1.1 mrg char tbuf[MAXDNAME];
230 1.86 christos char **aliases;
231 1.86 christos size_t maxaliases;
232 1.83 christos char *addr_ptrs[MAXADDRS];
233 1.1 mrg const char *tname;
234 1.59 christos int (*name_ok)(const char *);
235 1.1 mrg
236 1.25 lukem _DIAGASSERT(answer != NULL);
237 1.25 lukem _DIAGASSERT(qname != NULL);
238 1.25 lukem
239 1.1 mrg tname = qname;
240 1.80 christos hent->h_name = NULL;
241 1.1 mrg eom = answer->buf + anslen;
242 1.1 mrg switch (qtype) {
243 1.1 mrg case T_A:
244 1.1 mrg case T_AAAA:
245 1.1 mrg name_ok = res_hnok;
246 1.1 mrg break;
247 1.1 mrg case T_PTR:
248 1.1 mrg name_ok = res_dnok;
249 1.1 mrg break;
250 1.1 mrg default:
251 1.59 christos return NULL; /* XXX should be abort(); */
252 1.1 mrg }
253 1.86 christos
254 1.86 christos setup(aliases, maxaliases);
255 1.1 mrg /*
256 1.1 mrg * find first satisfactory answer
257 1.1 mrg */
258 1.1 mrg hp = &answer->hdr;
259 1.1 mrg ancount = ntohs(hp->ancount);
260 1.1 mrg qdcount = ntohs(hp->qdcount);
261 1.80 christos bp = buf;
262 1.80 christos ep = buf + buflen;
263 1.32 itojun cp = answer->buf;
264 1.32 itojun BOUNDED_INCR(HFIXEDSZ);
265 1.80 christos if (qdcount != 1)
266 1.80 christos goto no_recovery;
267 1.80 christos
268 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
269 1.80 christos if ((n < 0) || !maybe_ok(res, bp, name_ok))
270 1.80 christos goto no_recovery;
271 1.80 christos
272 1.32 itojun BOUNDED_INCR(n + QFIXEDSZ);
273 1.1 mrg if (qtype == T_A || qtype == T_AAAA) {
274 1.1 mrg /* res_send() has already verified that the query name is the
275 1.1 mrg * same as the one we sent; this just gets the expanded name
276 1.1 mrg * (i.e., with the succeeding search-domain tacked on).
277 1.1 mrg */
278 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
279 1.80 christos if (n >= MAXHOSTNAMELEN)
280 1.80 christos goto no_recovery;
281 1.80 christos hent->h_name = bp;
282 1.1 mrg bp += n;
283 1.1 mrg /* The qname can be abbreviated, but h_name is now absolute. */
284 1.80 christos qname = hent->h_name;
285 1.1 mrg }
286 1.80 christos hent->h_aliases = ap = aliases;
287 1.80 christos hent->h_addr_list = hap = addr_ptrs;
288 1.1 mrg *ap = NULL;
289 1.1 mrg *hap = NULL;
290 1.1 mrg haveanswer = 0;
291 1.1 mrg had_error = 0;
292 1.1 mrg while (ancount-- > 0 && cp < eom && !had_error) {
293 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
294 1.79 christos if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
295 1.1 mrg had_error++;
296 1.1 mrg continue;
297 1.1 mrg }
298 1.1 mrg cp += n; /* name */
299 1.32 itojun BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
300 1.1 mrg type = _getshort(cp);
301 1.1 mrg cp += INT16SZ; /* type */
302 1.1 mrg class = _getshort(cp);
303 1.1 mrg cp += INT16SZ + INT32SZ; /* class, TTL */
304 1.1 mrg n = _getshort(cp);
305 1.1 mrg cp += INT16SZ; /* len */
306 1.32 itojun BOUNDS_CHECK(cp, n);
307 1.32 itojun erdata = cp + n;
308 1.1 mrg if (class != C_IN) {
309 1.1 mrg /* XXX - debug? syslog? */
310 1.1 mrg cp += n;
311 1.1 mrg continue; /* XXX - had_error++ ? */
312 1.1 mrg }
313 1.1 mrg if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
314 1.80 christos n = dn_expand(answer->buf, eom, cp, tbuf,
315 1.80 christos (int)sizeof tbuf);
316 1.79 christos if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
317 1.1 mrg had_error++;
318 1.1 mrg continue;
319 1.1 mrg }
320 1.1 mrg cp += n;
321 1.80 christos if (cp != erdata)
322 1.80 christos goto no_recovery;
323 1.1 mrg /* Store alias. */
324 1.87 christos addalias(ap, bp, aliases, maxaliases);
325 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
326 1.1 mrg if (n >= MAXHOSTNAMELEN) {
327 1.1 mrg had_error++;
328 1.1 mrg continue;
329 1.1 mrg }
330 1.1 mrg bp += n;
331 1.1 mrg /* Get canonical name. */
332 1.78 christos n = (int)strlen(tbuf) + 1; /* for the \0 */
333 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN) {
334 1.1 mrg had_error++;
335 1.1 mrg continue;
336 1.1 mrg }
337 1.54 itojun strlcpy(bp, tbuf, (size_t)(ep - bp));
338 1.80 christos hent->h_name = bp;
339 1.1 mrg bp += n;
340 1.1 mrg continue;
341 1.1 mrg }
342 1.1 mrg if (qtype == T_PTR && type == T_CNAME) {
343 1.80 christos n = dn_expand(answer->buf, eom, cp, tbuf,
344 1.80 christos (int)sizeof tbuf);
345 1.79 christos if (n < 0 || !maybe_dnok(res, tbuf)) {
346 1.1 mrg had_error++;
347 1.1 mrg continue;
348 1.1 mrg }
349 1.1 mrg cp += n;
350 1.80 christos if (cp != erdata)
351 1.80 christos goto no_recovery;
352 1.1 mrg /* Get canonical name. */
353 1.78 christos n = (int)strlen(tbuf) + 1; /* for the \0 */
354 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN) {
355 1.1 mrg had_error++;
356 1.1 mrg continue;
357 1.1 mrg }
358 1.54 itojun strlcpy(bp, tbuf, (size_t)(ep - bp));
359 1.1 mrg tname = bp;
360 1.1 mrg bp += n;
361 1.1 mrg continue;
362 1.1 mrg }
363 1.1 mrg if (type != qtype) {
364 1.28 assar if (type != T_KEY && type != T_SIG)
365 1.28 assar syslog(LOG_NOTICE|LOG_AUTH,
366 1.1 mrg "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
367 1.28 assar qname, p_class(C_IN), p_type(qtype),
368 1.28 assar p_type(type));
369 1.1 mrg cp += n;
370 1.1 mrg continue; /* XXX - had_error++ ? */
371 1.1 mrg }
372 1.1 mrg switch (type) {
373 1.1 mrg case T_PTR:
374 1.1 mrg if (strcasecmp(tname, bp) != 0) {
375 1.1 mrg syslog(LOG_NOTICE|LOG_AUTH,
376 1.1 mrg AskedForGot, qname, bp);
377 1.1 mrg cp += n;
378 1.1 mrg continue; /* XXX - had_error++ ? */
379 1.1 mrg }
380 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
381 1.79 christos if ((n < 0) || !maybe_hnok(res, bp)) {
382 1.1 mrg had_error++;
383 1.1 mrg break;
384 1.1 mrg }
385 1.1 mrg #if MULTI_PTRS_ARE_ALIASES
386 1.1 mrg cp += n;
387 1.80 christos if (cp != erdata)
388 1.80 christos goto no_recovery;
389 1.1 mrg if (!haveanswer)
390 1.80 christos hent->h_name = bp;
391 1.87 christos else
392 1.87 christos addalias(ap, bp, aliases, maxaliases);
393 1.1 mrg if (n != -1) {
394 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
395 1.1 mrg if (n >= MAXHOSTNAMELEN) {
396 1.1 mrg had_error++;
397 1.1 mrg break;
398 1.1 mrg }
399 1.1 mrg bp += n;
400 1.1 mrg }
401 1.1 mrg break;
402 1.1 mrg #else
403 1.80 christos hent->h_name = bp;
404 1.59 christos if (res->options & RES_USE_INET6) {
405 1.1 mrg n = strlen(bp) + 1; /* for the \0 */
406 1.1 mrg if (n >= MAXHOSTNAMELEN) {
407 1.1 mrg had_error++;
408 1.1 mrg break;
409 1.1 mrg }
410 1.1 mrg bp += n;
411 1.80 christos map_v4v6_hostent(hent, &bp, ep);
412 1.1 mrg }
413 1.80 christos goto success;
414 1.1 mrg #endif
415 1.1 mrg case T_A:
416 1.1 mrg case T_AAAA:
417 1.80 christos if (strcasecmp(hent->h_name, bp) != 0) {
418 1.1 mrg syslog(LOG_NOTICE|LOG_AUTH,
419 1.80 christos AskedForGot, hent->h_name, bp);
420 1.1 mrg cp += n;
421 1.1 mrg continue; /* XXX - had_error++ ? */
422 1.1 mrg }
423 1.80 christos if (n != hent->h_length) {
424 1.1 mrg cp += n;
425 1.1 mrg continue;
426 1.51 itojun }
427 1.51 itojun if (type == T_AAAA) {
428 1.51 itojun struct in6_addr in6;
429 1.80 christos memcpy(&in6, cp, NS_IN6ADDRSZ);
430 1.51 itojun if (IN6_IS_ADDR_V4MAPPED(&in6)) {
431 1.51 itojun cp += n;
432 1.51 itojun continue;
433 1.51 itojun }
434 1.1 mrg }
435 1.1 mrg if (!haveanswer) {
436 1.25 lukem int nn;
437 1.1 mrg
438 1.80 christos hent->h_name = bp;
439 1.78 christos nn = (int)strlen(bp) + 1; /* for the \0 */
440 1.1 mrg bp += nn;
441 1.1 mrg }
442 1.1 mrg
443 1.14 lukem bp += sizeof(align) -
444 1.14 lukem (size_t)((u_long)bp % sizeof(align));
445 1.1 mrg
446 1.80 christos if (bp + n >= ep) {
447 1.76 christos debugprintf("size (%d) too big\n", res, n);
448 1.1 mrg had_error++;
449 1.1 mrg continue;
450 1.1 mrg }
451 1.80 christos if (hap >= &addr_ptrs[MAXADDRS - 1]) {
452 1.71 christos if (!toobig++) {
453 1.76 christos debugprintf("Too many addresses (%d)\n",
454 1.59 christos res, MAXADDRS);
455 1.71 christos }
456 1.1 mrg cp += n;
457 1.1 mrg continue;
458 1.1 mrg }
459 1.7 kleink (void)memcpy(*hap++ = bp, cp, (size_t)n);
460 1.1 mrg bp += n;
461 1.1 mrg cp += n;
462 1.80 christos if (cp != erdata)
463 1.80 christos goto no_recovery;
464 1.1 mrg break;
465 1.1 mrg default:
466 1.1 mrg abort();
467 1.1 mrg }
468 1.1 mrg if (!had_error)
469 1.1 mrg haveanswer++;
470 1.1 mrg }
471 1.1 mrg if (haveanswer) {
472 1.1 mrg *ap = NULL;
473 1.1 mrg *hap = NULL;
474 1.1 mrg /*
475 1.1 mrg * Note: we sort even if host can take only one address
476 1.1 mrg * in its return structures - should give it the "best"
477 1.1 mrg * address in that case, not some random one
478 1.1 mrg */
479 1.59 christos if (res->nsort && haveanswer > 1 && qtype == T_A)
480 1.80 christos addrsort(addr_ptrs, haveanswer, res);
481 1.80 christos if (!hent->h_name) {
482 1.78 christos n = (int)strlen(qname) + 1; /* for the \0 */
483 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN)
484 1.1 mrg goto no_recovery;
485 1.54 itojun strlcpy(bp, qname, (size_t)(ep - bp));
486 1.80 christos hent->h_name = bp;
487 1.1 mrg bp += n;
488 1.1 mrg }
489 1.59 christos if (res->options & RES_USE_INET6)
490 1.80 christos map_v4v6_hostent(hent, &bp, ep);
491 1.80 christos goto success;
492 1.1 mrg }
493 1.80 christos no_recovery:
494 1.86 christos free(aliases);
495 1.80 christos *he = NO_RECOVERY;
496 1.80 christos return NULL;
497 1.80 christos success:
498 1.80 christos bp = (char *)ALIGN(bp);
499 1.80 christos n = (int)(ap - aliases);
500 1.80 christos qlen = (n + 1) * sizeof(*hent->h_aliases);
501 1.80 christos if ((size_t)(ep - bp) < qlen)
502 1.80 christos goto nospc;
503 1.80 christos hent->h_aliases = (void *)bp;
504 1.80 christos memcpy(bp, aliases, qlen);
505 1.86 christos free(aliases);
506 1.80 christos
507 1.80 christos bp += qlen;
508 1.80 christos n = (int)(hap - addr_ptrs);
509 1.80 christos qlen = (n + 1) * sizeof(*hent->h_addr_list);
510 1.80 christos if ((size_t)(ep - bp) < qlen)
511 1.80 christos goto nospc;
512 1.80 christos hent->h_addr_list = (void *)bp;
513 1.80 christos memcpy(bp, addr_ptrs, qlen);
514 1.80 christos *he = NETDB_SUCCESS;
515 1.80 christos return hent;
516 1.80 christos nospc:
517 1.86 christos free(aliases);
518 1.80 christos errno = ENOSPC;
519 1.80 christos *he = NETDB_INTERNAL;
520 1.59 christos return NULL;
521 1.1 mrg }
522 1.1 mrg
523 1.1 mrg struct hostent *
524 1.80 christos gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
525 1.80 christos int *he)
526 1.1 mrg {
527 1.59 christos res_state res = __res_get_state();
528 1.1 mrg
529 1.80 christos if (res == NULL) {
530 1.80 christos *he = NETDB_INTERNAL;
531 1.60 christos return NULL;
532 1.80 christos }
533 1.60 christos
534 1.25 lukem _DIAGASSERT(name != NULL);
535 1.25 lukem
536 1.59 christos if (res->options & RES_USE_INET6) {
537 1.85 christos struct hostent *nhp = gethostbyname_internal(name, AF_INET6,
538 1.85 christos res, hp, buf, buflen, he);
539 1.85 christos if (nhp) {
540 1.59 christos __res_put_state(res);
541 1.85 christos return nhp;
542 1.59 christos }
543 1.1 mrg }
544 1.80 christos hp = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, he);
545 1.59 christos __res_put_state(res);
546 1.59 christos return hp;
547 1.1 mrg }
548 1.1 mrg
549 1.1 mrg struct hostent *
550 1.80 christos gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
551 1.80 christos size_t buflen, int *he)
552 1.59 christos {
553 1.59 christos res_state res = __res_get_state();
554 1.60 christos
555 1.80 christos if (res == NULL) {
556 1.80 christos *he = NETDB_INTERNAL;
557 1.60 christos return NULL;
558 1.80 christos }
559 1.80 christos hp = gethostbyname_internal(name, af, res, hp, buf, buflen, he);
560 1.59 christos __res_put_state(res);
561 1.59 christos return hp;
562 1.59 christos }
563 1.59 christos
564 1.59 christos static struct hostent *
565 1.80 christos gethostbyname_internal(const char *name, int af, res_state res,
566 1.80 christos struct hostent *hp, char *buf, size_t buflen, int *he)
567 1.1 mrg {
568 1.2 mrg const char *cp;
569 1.80 christos struct getnamaddr info;
570 1.85 christos char hbuf[MAXHOSTNAMELEN];
571 1.80 christos size_t size;
572 1.12 lukem static const ns_dtab dtab[] = {
573 1.80 christos NS_FILES_CB(_hf_gethtbyname, NULL)
574 1.9 lukem { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
575 1.11 lukem NS_NIS_CB(_yp_gethtbyname, NULL)
576 1.71 christos NS_NULL_CB
577 1.9 lukem };
578 1.1 mrg
579 1.25 lukem _DIAGASSERT(name != NULL);
580 1.25 lukem
581 1.1 mrg switch (af) {
582 1.1 mrg case AF_INET:
583 1.80 christos size = NS_INADDRSZ;
584 1.1 mrg break;
585 1.1 mrg case AF_INET6:
586 1.80 christos size = NS_IN6ADDRSZ;
587 1.1 mrg break;
588 1.1 mrg default:
589 1.80 christos *he = NETDB_INTERNAL;
590 1.1 mrg errno = EAFNOSUPPORT;
591 1.59 christos return NULL;
592 1.1 mrg }
593 1.80 christos if (buflen < size)
594 1.80 christos goto nospc;
595 1.1 mrg
596 1.80 christos hp->h_addrtype = af;
597 1.80 christos hp->h_length = (int)size;
598 1.1 mrg
599 1.1 mrg /*
600 1.75 wiz * if there aren't any dots, it could be a user-level alias.
601 1.59 christos * this is also done in res_nquery() since we are not the only
602 1.1 mrg * function that looks up host names.
603 1.1 mrg */
604 1.85 christos if (!strchr(name, '.') && (cp = res_hostalias(res, name,
605 1.85 christos hbuf, sizeof(hbuf))))
606 1.1 mrg name = cp;
607 1.1 mrg
608 1.1 mrg /*
609 1.1 mrg * disallow names consisting only of digits/dots, unless
610 1.1 mrg * they end in a dot.
611 1.1 mrg */
612 1.35 itohy if (isdigit((u_char) name[0]))
613 1.1 mrg for (cp = name;; ++cp) {
614 1.1 mrg if (!*cp) {
615 1.1 mrg if (*--cp == '.')
616 1.1 mrg break;
617 1.1 mrg /*
618 1.1 mrg * All-numeric, no dot at the end.
619 1.1 mrg * Fake up a hostent as if we'd actually
620 1.1 mrg * done a lookup.
621 1.1 mrg */
622 1.80 christos goto fake;
623 1.1 mrg }
624 1.35 itohy if (!isdigit((u_char) *cp) && *cp != '.')
625 1.1 mrg break;
626 1.1 mrg }
627 1.35 itohy if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
628 1.1 mrg name[0] == ':')
629 1.1 mrg for (cp = name;; ++cp) {
630 1.1 mrg if (!*cp) {
631 1.1 mrg if (*--cp == '.')
632 1.1 mrg break;
633 1.1 mrg /*
634 1.1 mrg * All-IPv6-legal, no dot at the end.
635 1.1 mrg * Fake up a hostent as if we'd actually
636 1.1 mrg * done a lookup.
637 1.1 mrg */
638 1.80 christos goto fake;
639 1.1 mrg }
640 1.35 itohy if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
641 1.1 mrg break;
642 1.1 mrg }
643 1.1 mrg
644 1.80 christos *he = NETDB_INTERNAL;
645 1.80 christos info.hp = hp;
646 1.80 christos info.buf = buf;
647 1.80 christos info.buflen = buflen;
648 1.80 christos info.he = he;
649 1.80 christos if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
650 1.45 itojun default_dns_files, name, strlen(name), af) != NS_SUCCESS)
651 1.59 christos return NULL;
652 1.80 christos *he = NETDB_SUCCESS;
653 1.80 christos return hp;
654 1.80 christos nospc:
655 1.80 christos *he = NETDB_INTERNAL;
656 1.80 christos errno = ENOSPC;
657 1.80 christos return NULL;
658 1.80 christos fake:
659 1.84 christos HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
660 1.84 christos HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
661 1.80 christos
662 1.80 christos hp->h_aliases[0] = NULL;
663 1.80 christos if (size > buflen)
664 1.80 christos goto nospc;
665 1.80 christos
666 1.80 christos if (inet_pton(af, name, buf) <= 0) {
667 1.80 christos *he = HOST_NOT_FOUND;
668 1.80 christos return NULL;
669 1.80 christos }
670 1.80 christos hp->h_addr_list[0] = buf;
671 1.80 christos hp->h_addr_list[1] = NULL;
672 1.80 christos buf += size;
673 1.80 christos buflen -= size;
674 1.84 christos HENT_SCOPY(hp->h_name, name, buf, buflen);
675 1.80 christos if (res->options & RES_USE_INET6)
676 1.80 christos map_v4v6_hostent(hp, &buf, buf + buflen);
677 1.80 christos *he = NETDB_SUCCESS;
678 1.59 christos return hp;
679 1.1 mrg }
680 1.1 mrg
681 1.1 mrg struct hostent *
682 1.80 christos gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
683 1.80 christos char *buf, size_t buflen, int *he)
684 1.1 mrg {
685 1.1 mrg const u_char *uaddr = (const u_char *)addr;
686 1.53 thorpej socklen_t size;
687 1.80 christos struct getnamaddr info;
688 1.12 lukem static const ns_dtab dtab[] = {
689 1.80 christos NS_FILES_CB(_hf_gethtbyaddr, NULL)
690 1.9 lukem { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
691 1.11 lukem NS_NIS_CB(_yp_gethtbyaddr, NULL)
692 1.71 christos NS_NULL_CB
693 1.9 lukem };
694 1.1 mrg
695 1.25 lukem _DIAGASSERT(addr != NULL);
696 1.25 lukem
697 1.80 christos if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
698 1.80 christos (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
699 1.80 christos IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
700 1.80 christos *he = HOST_NOT_FOUND;
701 1.59 christos return NULL;
702 1.41 itojun }
703 1.80 christos if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
704 1.80 christos (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
705 1.80 christos IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
706 1.1 mrg /* Unmap. */
707 1.80 christos uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
708 1.80 christos addr = uaddr;
709 1.1 mrg af = AF_INET;
710 1.80 christos len = NS_INADDRSZ;
711 1.1 mrg }
712 1.1 mrg switch (af) {
713 1.1 mrg case AF_INET:
714 1.80 christos size = NS_INADDRSZ;
715 1.1 mrg break;
716 1.1 mrg case AF_INET6:
717 1.80 christos size = NS_IN6ADDRSZ;
718 1.1 mrg break;
719 1.1 mrg default:
720 1.1 mrg errno = EAFNOSUPPORT;
721 1.80 christos *he = NETDB_INTERNAL;
722 1.59 christos return NULL;
723 1.1 mrg }
724 1.1 mrg if (size != len) {
725 1.1 mrg errno = EINVAL;
726 1.80 christos *he = NETDB_INTERNAL;
727 1.59 christos return NULL;
728 1.1 mrg }
729 1.80 christos info.hp = hp;
730 1.80 christos info.buf = buf;
731 1.80 christos info.buflen = buflen;
732 1.80 christos info.he = he;
733 1.80 christos *he = NETDB_INTERNAL;
734 1.80 christos if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
735 1.11 lukem default_dns_files, uaddr, len, af) != NS_SUCCESS)
736 1.59 christos return NULL;
737 1.80 christos *he = NETDB_SUCCESS;
738 1.59 christos return hp;
739 1.1 mrg }
740 1.1 mrg
741 1.1 mrg struct hostent *
742 1.80 christos gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
743 1.1 mrg {
744 1.80 christos char *p, *name;
745 1.25 lukem char *cp, **q;
746 1.1 mrg int af, len;
747 1.80 christos size_t llen, anum;
748 1.86 christos char **aliases;
749 1.86 christos size_t maxaliases;
750 1.80 christos struct in6_addr host_addr;
751 1.1 mrg
752 1.80 christos if (hf == NULL) {
753 1.80 christos *he = NETDB_INTERNAL;
754 1.80 christos errno = EINVAL;
755 1.59 christos return NULL;
756 1.1 mrg }
757 1.86 christos setup(aliases, maxaliases);
758 1.1 mrg again:
759 1.80 christos if ((p = fgetln(hf, &llen)) == NULL) {
760 1.86 christos free(aliases);
761 1.80 christos *he = HOST_NOT_FOUND;
762 1.59 christos return NULL;
763 1.1 mrg }
764 1.80 christos if (llen < 1)
765 1.80 christos goto again;
766 1.1 mrg if (*p == '#')
767 1.1 mrg goto again;
768 1.80 christos p[llen] = '\0';
769 1.1 mrg if (!(cp = strpbrk(p, "#\n")))
770 1.1 mrg goto again;
771 1.1 mrg *cp = '\0';
772 1.1 mrg if (!(cp = strpbrk(p, " \t")))
773 1.1 mrg goto again;
774 1.1 mrg *cp++ = '\0';
775 1.80 christos if (inet_pton(AF_INET6, p, &host_addr) > 0) {
776 1.1 mrg af = AF_INET6;
777 1.80 christos len = NS_IN6ADDRSZ;
778 1.80 christos } else if (inet_pton(AF_INET, p, &host_addr) > 0) {
779 1.59 christos res_state res = __res_get_state();
780 1.60 christos if (res == NULL)
781 1.86 christos goto nospc;
782 1.59 christos if (res->options & RES_USE_INET6) {
783 1.80 christos map_v4v6_address(buf, buf);
784 1.1 mrg af = AF_INET6;
785 1.80 christos len = NS_IN6ADDRSZ;
786 1.1 mrg } else {
787 1.1 mrg af = AF_INET;
788 1.80 christos len = NS_INADDRSZ;
789 1.1 mrg }
790 1.59 christos __res_put_state(res);
791 1.1 mrg } else {
792 1.1 mrg goto again;
793 1.1 mrg }
794 1.21 itojun /* if this is not something we're looking for, skip it. */
795 1.80 christos if (hent->h_addrtype != 0 && hent->h_addrtype != af)
796 1.21 itojun goto again;
797 1.80 christos if (hent->h_length != 0 && hent->h_length != len)
798 1.21 itojun goto again;
799 1.80 christos
800 1.1 mrg while (*cp == ' ' || *cp == '\t')
801 1.1 mrg cp++;
802 1.80 christos if ((cp = strpbrk(name = cp, " \t")) != NULL)
803 1.1 mrg *cp++ = '\0';
804 1.80 christos q = aliases;
805 1.1 mrg while (cp && *cp) {
806 1.1 mrg if (*cp == ' ' || *cp == '\t') {
807 1.1 mrg cp++;
808 1.1 mrg continue;
809 1.1 mrg }
810 1.87 christos addalias(q, cp, aliases, maxaliases);
811 1.80 christos *q++ = cp;
812 1.2 mrg if ((cp = strpbrk(cp, " \t")) != NULL)
813 1.1 mrg *cp++ = '\0';
814 1.1 mrg }
815 1.80 christos hent->h_length = len;
816 1.80 christos hent->h_addrtype = af;
817 1.84 christos HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
818 1.80 christos anum = (size_t)(q - aliases);
819 1.84 christos HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
820 1.84 christos HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
821 1.84 christos buflen);
822 1.80 christos hent->h_addr_list[1] = NULL;
823 1.80 christos
824 1.84 christos HENT_SCOPY(hent->h_name, name, buf, buflen);
825 1.80 christos for (size_t i = 0; i < anum; i++)
826 1.84 christos HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
827 1.80 christos hent->h_aliases[anum] = NULL;
828 1.80 christos
829 1.80 christos *he = NETDB_SUCCESS;
830 1.86 christos free(aliases);
831 1.80 christos return hent;
832 1.80 christos nospc:
833 1.86 christos free(aliases);
834 1.80 christos errno = ENOSPC;
835 1.80 christos *he = NETDB_INTERNAL;
836 1.80 christos return NULL;
837 1.1 mrg }
838 1.1 mrg
839 1.1 mrg static void
840 1.59 christos map_v4v6_address(const char *src, char *dst)
841 1.1 mrg {
842 1.1 mrg u_char *p = (u_char *)dst;
843 1.80 christos char tmp[NS_INADDRSZ];
844 1.1 mrg int i;
845 1.1 mrg
846 1.25 lukem _DIAGASSERT(src != NULL);
847 1.25 lukem _DIAGASSERT(dst != NULL);
848 1.25 lukem
849 1.1 mrg /* Stash a temporary copy so our caller can update in place. */
850 1.80 christos (void)memcpy(tmp, src, NS_INADDRSZ);
851 1.1 mrg /* Mark this ipv6 addr as a mapped ipv4. */
852 1.1 mrg for (i = 0; i < 10; i++)
853 1.1 mrg *p++ = 0x00;
854 1.1 mrg *p++ = 0xff;
855 1.1 mrg *p++ = 0xff;
856 1.1 mrg /* Retrieve the saved copy and we're done. */
857 1.80 christos (void)memcpy(p, tmp, NS_INADDRSZ);
858 1.1 mrg }
859 1.1 mrg
860 1.1 mrg static void
861 1.59 christos map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
862 1.1 mrg {
863 1.1 mrg char **ap;
864 1.1 mrg
865 1.25 lukem _DIAGASSERT(hp != NULL);
866 1.25 lukem _DIAGASSERT(bpp != NULL);
867 1.47 itojun _DIAGASSERT(ep != NULL);
868 1.25 lukem
869 1.80 christos if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
870 1.1 mrg return;
871 1.1 mrg hp->h_addrtype = AF_INET6;
872 1.80 christos hp->h_length = NS_IN6ADDRSZ;
873 1.1 mrg for (ap = hp->h_addr_list; *ap; ap++) {
874 1.78 christos int i = (int)(sizeof(align) -
875 1.78 christos (size_t)((u_long)*bpp % sizeof(align)));
876 1.1 mrg
877 1.80 christos if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
878 1.1 mrg /* Out of memory. Truncate address list here. XXX */
879 1.1 mrg *ap = NULL;
880 1.1 mrg return;
881 1.1 mrg }
882 1.1 mrg *bpp += i;
883 1.1 mrg map_v4v6_address(*ap, *bpp);
884 1.1 mrg *ap = *bpp;
885 1.80 christos *bpp += NS_IN6ADDRSZ;
886 1.1 mrg }
887 1.1 mrg }
888 1.1 mrg
889 1.1 mrg static void
890 1.59 christos addrsort(char **ap, int num, res_state res)
891 1.1 mrg {
892 1.1 mrg int i, j;
893 1.1 mrg char **p;
894 1.1 mrg short aval[MAXADDRS];
895 1.1 mrg int needsort = 0;
896 1.1 mrg
897 1.25 lukem _DIAGASSERT(ap != NULL);
898 1.25 lukem
899 1.1 mrg p = ap;
900 1.1 mrg for (i = 0; i < num; i++, p++) {
901 1.59 christos for (j = 0 ; (unsigned)j < res->nsort; j++)
902 1.59 christos if (res->sort_list[j].addr.s_addr ==
903 1.14 lukem (((struct in_addr *)(void *)(*p))->s_addr &
904 1.59 christos res->sort_list[j].mask))
905 1.1 mrg break;
906 1.1 mrg aval[i] = j;
907 1.1 mrg if (needsort == 0 && i > 0 && j < aval[i-1])
908 1.1 mrg needsort = i;
909 1.1 mrg }
910 1.1 mrg if (!needsort)
911 1.1 mrg return;
912 1.1 mrg
913 1.1 mrg while (needsort < num) {
914 1.1 mrg for (j = needsort - 1; j >= 0; j--) {
915 1.1 mrg if (aval[j] > aval[j+1]) {
916 1.1 mrg char *hp;
917 1.1 mrg
918 1.1 mrg i = aval[j];
919 1.1 mrg aval[j] = aval[j+1];
920 1.1 mrg aval[j+1] = i;
921 1.1 mrg
922 1.1 mrg hp = ap[j];
923 1.1 mrg ap[j] = ap[j+1];
924 1.1 mrg ap[j+1] = hp;
925 1.1 mrg } else
926 1.1 mrg break;
927 1.1 mrg }
928 1.1 mrg needsort++;
929 1.1 mrg }
930 1.1 mrg }
931 1.1 mrg
932 1.1 mrg
933 1.13 christos /*ARGSUSED*/
934 1.9 lukem int
935 1.61 ginsbach _dns_gethtbyname(void *rv, void *cb_data, va_list ap)
936 1.9 lukem {
937 1.52 itojun querybuf *buf;
938 1.9 lukem int n, type;
939 1.9 lukem struct hostent *hp;
940 1.9 lukem const char *name;
941 1.59 christos res_state res;
942 1.80 christos struct getnamaddr *info = rv;
943 1.9 lukem
944 1.25 lukem _DIAGASSERT(rv != NULL);
945 1.25 lukem
946 1.9 lukem name = va_arg(ap, char *);
947 1.80 christos /* NOSTRICT skip string len */(void)va_arg(ap, int);
948 1.80 christos info->hp->h_addrtype = va_arg(ap, int);
949 1.9 lukem
950 1.80 christos switch (info->hp->h_addrtype) {
951 1.9 lukem case AF_INET:
952 1.80 christos info->hp->h_length = NS_INADDRSZ;
953 1.9 lukem type = T_A;
954 1.9 lukem break;
955 1.9 lukem case AF_INET6:
956 1.80 christos info->hp->h_length = NS_IN6ADDRSZ;
957 1.9 lukem type = T_AAAA;
958 1.9 lukem break;
959 1.9 lukem default:
960 1.9 lukem return NS_UNAVAIL;
961 1.9 lukem }
962 1.52 itojun buf = malloc(sizeof(*buf));
963 1.52 itojun if (buf == NULL) {
964 1.80 christos *info->he = NETDB_INTERNAL;
965 1.52 itojun return NS_NOTFOUND;
966 1.52 itojun }
967 1.59 christos res = __res_get_state();
968 1.69 christos if (res == NULL) {
969 1.69 christos free(buf);
970 1.60 christos return NS_NOTFOUND;
971 1.69 christos }
972 1.78 christos n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
973 1.52 itojun if (n < 0) {
974 1.52 itojun free(buf);
975 1.76 christos debugprintf("res_nsearch failed (%d)\n", res, n);
976 1.59 christos __res_put_state(res);
977 1.9 lukem return NS_NOTFOUND;
978 1.9 lukem }
979 1.80 christos hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
980 1.80 christos info->buflen, info->he);
981 1.52 itojun free(buf);
982 1.59 christos __res_put_state(res);
983 1.9 lukem if (hp == NULL)
984 1.9 lukem switch (h_errno) {
985 1.9 lukem case HOST_NOT_FOUND:
986 1.9 lukem return NS_NOTFOUND;
987 1.9 lukem case TRY_AGAIN:
988 1.9 lukem return NS_TRYAGAIN;
989 1.9 lukem default:
990 1.9 lukem return NS_UNAVAIL;
991 1.9 lukem }
992 1.9 lukem return NS_SUCCESS;
993 1.9 lukem }
994 1.9 lukem
995 1.13 christos /*ARGSUSED*/
996 1.9 lukem int
997 1.59 christos _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
998 1.9 lukem {
999 1.49 itojun char qbuf[MAXDNAME + 1], *qp, *ep;
1000 1.9 lukem int n;
1001 1.52 itojun querybuf *buf;
1002 1.9 lukem struct hostent *hp;
1003 1.9 lukem const unsigned char *uaddr;
1004 1.80 christos int advance;
1005 1.59 christos res_state res;
1006 1.80 christos char *bf;
1007 1.80 christos size_t blen;
1008 1.80 christos struct getnamaddr *info = rv;
1009 1.9 lukem
1010 1.25 lukem _DIAGASSERT(rv != NULL);
1011 1.25 lukem
1012 1.9 lukem uaddr = va_arg(ap, unsigned char *);
1013 1.80 christos info->hp->h_length = va_arg(ap, int);
1014 1.80 christos info->hp->h_addrtype = va_arg(ap, int);
1015 1.9 lukem
1016 1.80 christos switch (info->hp->h_addrtype) {
1017 1.9 lukem case AF_INET:
1018 1.43 itojun (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1019 1.43 itojun (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1020 1.43 itojun (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1021 1.9 lukem break;
1022 1.9 lukem
1023 1.9 lukem case AF_INET6:
1024 1.9 lukem qp = qbuf;
1025 1.49 itojun ep = qbuf + sizeof(qbuf) - 1;
1026 1.80 christos for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
1027 1.49 itojun advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1028 1.49 itojun uaddr[n] & 0xf,
1029 1.43 itojun ((unsigned int)uaddr[n] >> 4) & 0xf);
1030 1.49 itojun if (advance > 0 && qp + advance < ep)
1031 1.43 itojun qp += advance;
1032 1.50 itojun else {
1033 1.80 christos *info->he = NETDB_INTERNAL;
1034 1.43 itojun return NS_NOTFOUND;
1035 1.50 itojun }
1036 1.9 lukem }
1037 1.50 itojun if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1038 1.80 christos *info->he = NETDB_INTERNAL;
1039 1.49 itojun return NS_NOTFOUND;
1040 1.50 itojun }
1041 1.9 lukem break;
1042 1.9 lukem default:
1043 1.80 christos return NS_UNAVAIL;
1044 1.9 lukem }
1045 1.9 lukem
1046 1.52 itojun buf = malloc(sizeof(*buf));
1047 1.52 itojun if (buf == NULL) {
1048 1.80 christos *info->he = NETDB_INTERNAL;
1049 1.52 itojun return NS_NOTFOUND;
1050 1.52 itojun }
1051 1.59 christos res = __res_get_state();
1052 1.70 christos if (res == NULL) {
1053 1.70 christos free(buf);
1054 1.60 christos return NS_NOTFOUND;
1055 1.70 christos }
1056 1.78 christos n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
1057 1.9 lukem if (n < 0) {
1058 1.52 itojun free(buf);
1059 1.76 christos debugprintf("res_nquery failed (%d)\n", res, n);
1060 1.59 christos __res_put_state(res);
1061 1.9 lukem return NS_NOTFOUND;
1062 1.9 lukem }
1063 1.80 christos hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
1064 1.80 christos info->buflen, info->he);
1065 1.52 itojun free(buf);
1066 1.59 christos if (hp == NULL) {
1067 1.59 christos __res_put_state(res);
1068 1.80 christos switch (*info->he) {
1069 1.9 lukem case HOST_NOT_FOUND:
1070 1.9 lukem return NS_NOTFOUND;
1071 1.9 lukem case TRY_AGAIN:
1072 1.9 lukem return NS_TRYAGAIN;
1073 1.9 lukem default:
1074 1.9 lukem return NS_UNAVAIL;
1075 1.9 lukem }
1076 1.59 christos }
1077 1.80 christos
1078 1.80 christos bf = (void *)(hp->h_addr_list + 2);
1079 1.80 christos blen = (size_t)(bf - info->buf);
1080 1.80 christos if (blen + info->hp->h_length > info->buflen)
1081 1.80 christos goto nospc;
1082 1.80 christos hp->h_addr_list[0] = bf;
1083 1.80 christos hp->h_addr_list[1] = NULL;
1084 1.80 christos (void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
1085 1.80 christos if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1086 1.80 christos if (blen + NS_IN6ADDRSZ > info->buflen)
1087 1.80 christos goto nospc;
1088 1.80 christos map_v4v6_address(bf, bf);
1089 1.9 lukem hp->h_addrtype = AF_INET6;
1090 1.80 christos hp->h_length = NS_IN6ADDRSZ;
1091 1.9 lukem }
1092 1.9 lukem
1093 1.59 christos __res_put_state(res);
1094 1.80 christos *info->he = NETDB_SUCCESS;
1095 1.9 lukem return NS_SUCCESS;
1096 1.80 christos nospc:
1097 1.80 christos *info->he = NETDB_INTERNAL;
1098 1.80 christos return NS_UNAVAIL;
1099 1.9 lukem }
1100 1.9 lukem
1101 1.2 mrg #ifdef YP
1102 1.13 christos /*ARGSUSED*/
1103 1.80 christos static struct hostent *
1104 1.80 christos _yp_hostent(char *line, int af, struct getnamaddr *info)
1105 1.2 mrg {
1106 1.80 christos struct in6_addr host_addrs[MAXADDRS];
1107 1.86 christos char **aliases;
1108 1.86 christos size_t maxaliases;
1109 1.2 mrg char *p = line;
1110 1.80 christos char *cp, **q, *ptr;
1111 1.80 christos size_t len, anum, i;
1112 1.37 itojun int addrok;
1113 1.2 mrg int more;
1114 1.53 thorpej size_t naddrs;
1115 1.80 christos struct hostent *hp = info->hp;
1116 1.2 mrg
1117 1.25 lukem _DIAGASSERT(line != NULL);
1118 1.25 lukem
1119 1.80 christos hp->h_name = NULL;
1120 1.80 christos hp->h_addrtype = af;
1121 1.37 itojun switch (af) {
1122 1.37 itojun case AF_INET:
1123 1.80 christos hp->h_length = NS_INADDRSZ;
1124 1.37 itojun break;
1125 1.37 itojun case AF_INET6:
1126 1.80 christos hp->h_length = NS_IN6ADDRSZ;
1127 1.37 itojun break;
1128 1.37 itojun default:
1129 1.59 christos return NULL;
1130 1.37 itojun }
1131 1.86 christos setup(aliases, maxaliases);
1132 1.37 itojun naddrs = 0;
1133 1.80 christos q = aliases;
1134 1.2 mrg
1135 1.2 mrg nextline:
1136 1.36 itojun /* check for host_addrs overflow */
1137 1.80 christos if (naddrs >= __arraycount(host_addrs))
1138 1.36 itojun goto done;
1139 1.36 itojun
1140 1.2 mrg more = 0;
1141 1.2 mrg cp = strpbrk(p, " \t");
1142 1.36 itojun if (cp == NULL)
1143 1.36 itojun goto done;
1144 1.2 mrg *cp++ = '\0';
1145 1.2 mrg
1146 1.37 itojun /* p has should have an address */
1147 1.80 christos addrok = inet_pton(af, p, &host_addrs[naddrs]);
1148 1.37 itojun if (addrok != 1) {
1149 1.37 itojun /* skip to the next line */
1150 1.37 itojun while (cp && *cp) {
1151 1.37 itojun if (*cp == '\n') {
1152 1.37 itojun cp++;
1153 1.37 itojun goto nextline;
1154 1.37 itojun }
1155 1.37 itojun cp++;
1156 1.37 itojun }
1157 1.37 itojun goto done;
1158 1.37 itojun }
1159 1.85 christos naddrs++;
1160 1.37 itojun
1161 1.2 mrg while (*cp == ' ' || *cp == '\t')
1162 1.2 mrg cp++;
1163 1.2 mrg p = cp;
1164 1.2 mrg cp = strpbrk(p, " \t\n");
1165 1.2 mrg if (cp != NULL) {
1166 1.2 mrg if (*cp == '\n')
1167 1.2 mrg more = 1;
1168 1.2 mrg *cp++ = '\0';
1169 1.2 mrg }
1170 1.80 christos if (!hp->h_name)
1171 1.80 christos hp->h_name = p;
1172 1.80 christos else if (strcmp(hp->h_name, p) == 0)
1173 1.2 mrg ;
1174 1.87 christos else
1175 1.87 christos addalias(q, p, aliases, maxaliases);
1176 1.2 mrg p = cp;
1177 1.2 mrg if (more)
1178 1.2 mrg goto nextline;
1179 1.2 mrg
1180 1.2 mrg while (cp && *cp) {
1181 1.2 mrg if (*cp == ' ' || *cp == '\t') {
1182 1.2 mrg cp++;
1183 1.2 mrg continue;
1184 1.2 mrg }
1185 1.2 mrg if (*cp == '\n') {
1186 1.2 mrg cp++;
1187 1.2 mrg goto nextline;
1188 1.2 mrg }
1189 1.87 christos addalias(q, cp, aliases, maxaliases);
1190 1.86 christos *q++ = cp;
1191 1.2 mrg cp = strpbrk(cp, " \t");
1192 1.2 mrg if (cp != NULL)
1193 1.2 mrg *cp++ = '\0';
1194 1.2 mrg }
1195 1.37 itojun
1196 1.2 mrg done:
1197 1.86 christos if (hp->h_name == NULL) {
1198 1.86 christos free(aliases);
1199 1.59 christos return NULL;
1200 1.86 christos }
1201 1.80 christos
1202 1.80 christos ptr = info->buf;
1203 1.80 christos len = info->buflen;
1204 1.80 christos
1205 1.80 christos anum = (size_t)(q - aliases);
1206 1.84 christos HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
1207 1.84 christos HENT_ARRAY(hp->h_aliases, anum, ptr, len);
1208 1.80 christos
1209 1.80 christos for (i = 0; i < naddrs; i++)
1210 1.84 christos HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
1211 1.80 christos ptr, len);
1212 1.80 christos hp->h_addr_list[naddrs] = NULL;
1213 1.80 christos
1214 1.84 christos HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
1215 1.80 christos
1216 1.80 christos for (i = 0; i < anum; i++)
1217 1.84 christos HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
1218 1.80 christos hp->h_aliases[anum] = NULL;
1219 1.86 christos free(aliases);
1220 1.80 christos
1221 1.80 christos return hp;
1222 1.80 christos nospc:
1223 1.86 christos free(aliases);
1224 1.80 christos *info->he = NETDB_INTERNAL;
1225 1.80 christos errno = ENOSPC;
1226 1.80 christos return NULL;
1227 1.2 mrg }
1228 1.2 mrg
1229 1.13 christos /*ARGSUSED*/
1230 1.9 lukem int
1231 1.59 christos _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1232 1.2 mrg {
1233 1.59 christos struct hostent *hp = NULL;
1234 1.80 christos char *ypcurrent;
1235 1.80 christos int ypcurrentlen, r;
1236 1.37 itojun char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1237 1.9 lukem const unsigned char *uaddr;
1238 1.17 christos int af;
1239 1.37 itojun const char *map;
1240 1.80 christos struct getnamaddr *info = rv;
1241 1.9 lukem
1242 1.25 lukem _DIAGASSERT(rv != NULL);
1243 1.25 lukem
1244 1.9 lukem uaddr = va_arg(ap, unsigned char *);
1245 1.17 christos /* NOSTRICT skip len */(void)va_arg(ap, int);
1246 1.9 lukem af = va_arg(ap, int);
1247 1.2 mrg
1248 1.2 mrg if (!__ypdomain) {
1249 1.2 mrg if (_yp_check(&__ypdomain) == 0)
1250 1.9 lukem return NS_UNAVAIL;
1251 1.2 mrg }
1252 1.9 lukem /*
1253 1.37 itojun * XXX unfortunately, we cannot support IPv6 extended scoped address
1254 1.37 itojun * notation here. gethostbyaddr() is not scope-aware. too bad.
1255 1.9 lukem */
1256 1.78 christos if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
1257 1.37 itojun return NS_UNAVAIL;
1258 1.37 itojun switch (af) {
1259 1.37 itojun case AF_INET:
1260 1.37 itojun map = "hosts.byaddr";
1261 1.37 itojun break;
1262 1.37 itojun default:
1263 1.37 itojun map = "ipnodes.byaddr";
1264 1.37 itojun break;
1265 1.37 itojun }
1266 1.80 christos ypcurrent = NULL;
1267 1.37 itojun r = yp_match(__ypdomain, map, name,
1268 1.80 christos (int)strlen(name), &ypcurrent, &ypcurrentlen);
1269 1.59 christos if (r == 0)
1270 1.80 christos hp = _yp_hostent(ypcurrent, af, info);
1271 1.80 christos else
1272 1.80 christos hp = NULL;
1273 1.80 christos free(ypcurrent);
1274 1.59 christos if (hp == NULL) {
1275 1.80 christos *info->he = HOST_NOT_FOUND;
1276 1.9 lukem return NS_NOTFOUND;
1277 1.9 lukem }
1278 1.9 lukem return NS_SUCCESS;
1279 1.2 mrg }
1280 1.2 mrg
1281 1.13 christos /*ARGSUSED*/
1282 1.9 lukem int
1283 1.59 christos _yp_gethtbyname(void *rv, void *cb_data, va_list ap)
1284 1.2 mrg {
1285 1.80 christos struct hostent *hp;
1286 1.80 christos char *ypcurrent;
1287 1.80 christos int ypcurrentlen, r;
1288 1.9 lukem const char *name;
1289 1.17 christos int af;
1290 1.37 itojun const char *map;
1291 1.80 christos struct getnamaddr *info = rv;
1292 1.25 lukem
1293 1.25 lukem _DIAGASSERT(rv != NULL);
1294 1.9 lukem
1295 1.9 lukem name = va_arg(ap, char *);
1296 1.80 christos /* NOSTRICT skip string len */(void)va_arg(ap, int);
1297 1.9 lukem af = va_arg(ap, int);
1298 1.2 mrg
1299 1.2 mrg if (!__ypdomain) {
1300 1.2 mrg if (_yp_check(&__ypdomain) == 0)
1301 1.9 lukem return NS_UNAVAIL;
1302 1.2 mrg }
1303 1.37 itojun switch (af) {
1304 1.37 itojun case AF_INET:
1305 1.37 itojun map = "hosts.byname";
1306 1.37 itojun break;
1307 1.37 itojun default:
1308 1.37 itojun map = "ipnodes.byname";
1309 1.37 itojun break;
1310 1.37 itojun }
1311 1.80 christos ypcurrent = NULL;
1312 1.37 itojun r = yp_match(__ypdomain, map, name,
1313 1.80 christos (int)strlen(name), &ypcurrent, &ypcurrentlen);
1314 1.59 christos if (r == 0)
1315 1.80 christos hp = _yp_hostent(ypcurrent, af, info);
1316 1.80 christos else
1317 1.80 christos hp = NULL;
1318 1.80 christos free(ypcurrent);
1319 1.59 christos if (hp == NULL) {
1320 1.80 christos *info->he = HOST_NOT_FOUND;
1321 1.9 lukem return NS_NOTFOUND;
1322 1.9 lukem }
1323 1.9 lukem return NS_SUCCESS;
1324 1.2 mrg }
1325 1.2 mrg #endif
1326 1.80 christos
1327 1.80 christos /*
1328 1.80 christos * Non-reentrant versions.
1329 1.80 christos */
1330 1.80 christos FILE *_h_file;
1331 1.80 christos static struct hostent h_ent;
1332 1.80 christos static char h_buf[16384];
1333 1.80 christos
1334 1.80 christos struct hostent *
1335 1.82 christos gethostbyaddr(const void *addr, socklen_t len, int af) {
1336 1.80 christos return gethostbyaddr_r(addr, len, af, &h_ent, h_buf, sizeof(h_buf),
1337 1.80 christos &h_errno);
1338 1.80 christos }
1339 1.80 christos
1340 1.80 christos struct hostent *
1341 1.80 christos gethostbyname(const char *name) {
1342 1.80 christos return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1343 1.80 christos }
1344 1.80 christos
1345 1.80 christos struct hostent *
1346 1.80 christos gethostbyname2(const char *name, int af) {
1347 1.80 christos return gethostbyname2_r(name, af, &h_ent, h_buf, sizeof(h_buf),
1348 1.80 christos &h_errno);
1349 1.80 christos }
1350 1.80 christos
1351 1.80 christos struct hostent *
1352 1.80 christos gethostent(void)
1353 1.80 christos {
1354 1.80 christos if (_h_file == NULL) {
1355 1.80 christos sethostent_r(&_h_file);
1356 1.80 christos if (_h_file == NULL) {
1357 1.80 christos h_errno = NETDB_INTERNAL;
1358 1.80 christos return NULL;
1359 1.80 christos }
1360 1.80 christos }
1361 1.80 christos memset(&h_ent, 0, sizeof(h_ent));
1362 1.80 christos return gethostent_r(_h_file, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1363 1.80 christos }
1364 1.84 christos
1365