gethnamaddr.c revision 1.80 1 1.80 christos /* $NetBSD: gethnamaddr.c,v 1.80 2013/08/16 15:27:12 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.80 christos __RCSID("$NetBSD: gethnamaddr.c,v 1.80 2013/08/16 15:27:12 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.79 christos
112 1.1 mrg #define MAXALIASES 35
113 1.1 mrg #define MAXADDRS 35
114 1.1 mrg
115 1.1 mrg static const char AskedForGot[] =
116 1.80 christos "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
117 1.1 mrg
118 1.1 mrg
119 1.2 mrg #ifdef YP
120 1.2 mrg static char *__ypdomain;
121 1.2 mrg #endif
122 1.2 mrg
123 1.52 itojun #define MAXPACKET (64*1024)
124 1.1 mrg
125 1.1 mrg typedef union {
126 1.80 christos HEADER hdr;
127 1.80 christos u_char buf[MAXPACKET];
128 1.1 mrg } querybuf;
129 1.1 mrg
130 1.1 mrg typedef union {
131 1.80 christos int32_t al;
132 1.80 christos char ac;
133 1.1 mrg } align;
134 1.1 mrg
135 1.1 mrg #ifdef DEBUG
136 1.76 christos static void debugprintf(const char *, res_state, ...)
137 1.59 christos __attribute__((__format__(__printf__, 1, 3)));
138 1.2 mrg #endif
139 1.59 christos static struct hostent *getanswer(const querybuf *, int, const char *, int,
140 1.80 christos res_state, struct hostent *, char *, size_t, int *);
141 1.59 christos static void map_v4v6_address(const char *, char *);
142 1.59 christos static void map_v4v6_hostent(struct hostent *, char **, char *);
143 1.66 tsarna static void addrsort(char **, int, res_state);
144 1.2 mrg
145 1.59 christos void dns_service(void);
146 1.39 christos #undef dn_skipname
147 1.59 christos int dn_skipname(const u_char *, const u_char *);
148 1.80 christos
149 1.80 christos static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
150 1.80 christos
151 1.2 mrg #ifdef YP
152 1.80 christos static struct hostent *_yp_hostent(char *, int, struct getnamaddr *);
153 1.2 mrg #endif
154 1.2 mrg
155 1.80 christos static struct hostent *gethostbyname_internal(const char *, int, res_state,
156 1.80 christos struct hostent *, char *, size_t, int *);
157 1.59 christos
158 1.12 lukem static const ns_src default_dns_files[] = {
159 1.27 lukem { NSSRC_FILES, NS_SUCCESS },
160 1.11 lukem { NSSRC_DNS, NS_SUCCESS },
161 1.71 christos { 0, 0 }
162 1.11 lukem };
163 1.11 lukem
164 1.2 mrg
165 1.80 christos #define ARRAY(dst, anum, ptr, len) \
166 1.80 christos do { \
167 1.80 christos size_t _len = (anum + 1) * sizeof(*dst); \
168 1.80 christos if (_len > len) \
169 1.80 christos goto nospc; \
170 1.80 christos dst = (void *)ptr; \
171 1.80 christos ptr += _len; \
172 1.80 christos len -= _len; \
173 1.80 christos } while (/*CONSTCOND*/0)
174 1.80 christos
175 1.80 christos #define COPY(dst, src, slen, ptr, len) \
176 1.80 christos do { \
177 1.80 christos if ((size_t)slen > len) \
178 1.80 christos goto nospc; \
179 1.80 christos memcpy(ptr, src, (size_t)slen); \
180 1.80 christos dst = ptr; \
181 1.80 christos ptr += slen; \
182 1.80 christos len -= slen; \
183 1.80 christos } while (/* CONSTCOND */0)
184 1.80 christos
185 1.80 christos #define SCOPY(dst, src, ptr, len) \
186 1.80 christos do { \
187 1.80 christos size_t _len = strlen(src) + 1; \
188 1.80 christos COPY(dst, src, _len, ptr, len); \
189 1.80 christos } while (/* CONSTCOND */0)
190 1.80 christos
191 1.80 christos
192 1.2 mrg #ifdef DEBUG
193 1.1 mrg static void
194 1.76 christos debugprintf(const char *msg, res_state res, ...)
195 1.1 mrg {
196 1.25 lukem _DIAGASSERT(msg != NULL);
197 1.25 lukem
198 1.59 christos if (res->options & RES_DEBUG) {
199 1.1 mrg int save = errno;
200 1.38 sommerfe va_list ap;
201 1.1 mrg
202 1.68 christos va_start (ap, res);
203 1.38 sommerfe vprintf(msg, ap);
204 1.38 sommerfe va_end (ap);
205 1.38 sommerfe
206 1.1 mrg errno = save;
207 1.1 mrg }
208 1.1 mrg }
209 1.1 mrg #else
210 1.76 christos # define debugprintf(msg, res, num) /*nada*/
211 1.1 mrg #endif
212 1.1 mrg
213 1.32 itojun #define BOUNDED_INCR(x) \
214 1.32 itojun do { \
215 1.58 enami cp += (x); \
216 1.58 enami if (cp > eom) { \
217 1.32 itojun h_errno = NO_RECOVERY; \
218 1.59 christos return NULL; \
219 1.32 itojun } \
220 1.34 christos } while (/*CONSTCOND*/0)
221 1.32 itojun
222 1.32 itojun #define BOUNDS_CHECK(ptr, count) \
223 1.32 itojun do { \
224 1.58 enami if ((ptr) + (count) > eom) { \
225 1.32 itojun h_errno = NO_RECOVERY; \
226 1.59 christos return NULL; \
227 1.32 itojun } \
228 1.34 christos } while (/*CONSTCOND*/0)
229 1.32 itojun
230 1.1 mrg static struct hostent *
231 1.59 christos getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
232 1.80 christos res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
233 1.1 mrg {
234 1.25 lukem const HEADER *hp;
235 1.25 lukem const u_char *cp;
236 1.25 lukem int n;
237 1.80 christos size_t qlen;
238 1.32 itojun const u_char *eom, *erdata;
239 1.46 itojun char *bp, **ap, **hap, *ep;
240 1.46 itojun int type, class, ancount, qdcount;
241 1.1 mrg int haveanswer, had_error;
242 1.1 mrg int toobig = 0;
243 1.1 mrg char tbuf[MAXDNAME];
244 1.80 christos char *aliases[MAXALIASES];
245 1.80 christos char *addr_ptrs[MAXADDRS + 1];
246 1.1 mrg const char *tname;
247 1.59 christos int (*name_ok)(const char *);
248 1.1 mrg
249 1.25 lukem _DIAGASSERT(answer != NULL);
250 1.25 lukem _DIAGASSERT(qname != NULL);
251 1.25 lukem
252 1.1 mrg tname = qname;
253 1.80 christos hent->h_name = NULL;
254 1.1 mrg eom = answer->buf + anslen;
255 1.1 mrg switch (qtype) {
256 1.1 mrg case T_A:
257 1.1 mrg case T_AAAA:
258 1.1 mrg name_ok = res_hnok;
259 1.1 mrg break;
260 1.1 mrg case T_PTR:
261 1.1 mrg name_ok = res_dnok;
262 1.1 mrg break;
263 1.1 mrg default:
264 1.59 christos return NULL; /* XXX should be abort(); */
265 1.1 mrg }
266 1.1 mrg /*
267 1.1 mrg * find first satisfactory answer
268 1.1 mrg */
269 1.1 mrg hp = &answer->hdr;
270 1.1 mrg ancount = ntohs(hp->ancount);
271 1.1 mrg qdcount = ntohs(hp->qdcount);
272 1.80 christos bp = buf;
273 1.80 christos ep = buf + buflen;
274 1.32 itojun cp = answer->buf;
275 1.32 itojun BOUNDED_INCR(HFIXEDSZ);
276 1.80 christos if (qdcount != 1)
277 1.80 christos goto no_recovery;
278 1.80 christos
279 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
280 1.80 christos if ((n < 0) || !maybe_ok(res, bp, name_ok))
281 1.80 christos goto no_recovery;
282 1.80 christos
283 1.32 itojun BOUNDED_INCR(n + QFIXEDSZ);
284 1.1 mrg if (qtype == T_A || qtype == T_AAAA) {
285 1.1 mrg /* res_send() has already verified that the query name is the
286 1.1 mrg * same as the one we sent; this just gets the expanded name
287 1.1 mrg * (i.e., with the succeeding search-domain tacked on).
288 1.1 mrg */
289 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
290 1.80 christos if (n >= MAXHOSTNAMELEN)
291 1.80 christos goto no_recovery;
292 1.80 christos hent->h_name = bp;
293 1.1 mrg bp += n;
294 1.1 mrg /* The qname can be abbreviated, but h_name is now absolute. */
295 1.80 christos qname = hent->h_name;
296 1.1 mrg }
297 1.80 christos hent->h_aliases = ap = aliases;
298 1.80 christos hent->h_addr_list = hap = addr_ptrs;
299 1.1 mrg *ap = NULL;
300 1.1 mrg *hap = NULL;
301 1.1 mrg haveanswer = 0;
302 1.1 mrg had_error = 0;
303 1.1 mrg while (ancount-- > 0 && cp < eom && !had_error) {
304 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
305 1.79 christos if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
306 1.1 mrg had_error++;
307 1.1 mrg continue;
308 1.1 mrg }
309 1.1 mrg cp += n; /* name */
310 1.32 itojun BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
311 1.1 mrg type = _getshort(cp);
312 1.1 mrg cp += INT16SZ; /* type */
313 1.1 mrg class = _getshort(cp);
314 1.1 mrg cp += INT16SZ + INT32SZ; /* class, TTL */
315 1.1 mrg n = _getshort(cp);
316 1.1 mrg cp += INT16SZ; /* len */
317 1.32 itojun BOUNDS_CHECK(cp, n);
318 1.32 itojun erdata = cp + n;
319 1.1 mrg if (class != C_IN) {
320 1.1 mrg /* XXX - debug? syslog? */
321 1.1 mrg cp += n;
322 1.1 mrg continue; /* XXX - had_error++ ? */
323 1.1 mrg }
324 1.1 mrg if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
325 1.80 christos if (ap >= &aliases[MAXALIASES-1])
326 1.1 mrg continue;
327 1.80 christos n = dn_expand(answer->buf, eom, cp, tbuf,
328 1.80 christos (int)sizeof tbuf);
329 1.79 christos if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
330 1.1 mrg had_error++;
331 1.1 mrg continue;
332 1.1 mrg }
333 1.1 mrg cp += n;
334 1.80 christos if (cp != erdata)
335 1.80 christos goto no_recovery;
336 1.1 mrg /* Store alias. */
337 1.1 mrg *ap++ = bp;
338 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
339 1.1 mrg if (n >= MAXHOSTNAMELEN) {
340 1.1 mrg had_error++;
341 1.1 mrg continue;
342 1.1 mrg }
343 1.1 mrg bp += n;
344 1.1 mrg /* Get canonical name. */
345 1.78 christos n = (int)strlen(tbuf) + 1; /* for the \0 */
346 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN) {
347 1.1 mrg had_error++;
348 1.1 mrg continue;
349 1.1 mrg }
350 1.54 itojun strlcpy(bp, tbuf, (size_t)(ep - bp));
351 1.80 christos hent->h_name = bp;
352 1.1 mrg bp += n;
353 1.1 mrg continue;
354 1.1 mrg }
355 1.1 mrg if (qtype == T_PTR && type == T_CNAME) {
356 1.80 christos n = dn_expand(answer->buf, eom, cp, tbuf,
357 1.80 christos (int)sizeof tbuf);
358 1.79 christos if (n < 0 || !maybe_dnok(res, tbuf)) {
359 1.1 mrg had_error++;
360 1.1 mrg continue;
361 1.1 mrg }
362 1.1 mrg cp += n;
363 1.80 christos if (cp != erdata)
364 1.80 christos goto no_recovery;
365 1.1 mrg /* Get canonical name. */
366 1.78 christos n = (int)strlen(tbuf) + 1; /* for the \0 */
367 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN) {
368 1.1 mrg had_error++;
369 1.1 mrg continue;
370 1.1 mrg }
371 1.54 itojun strlcpy(bp, tbuf, (size_t)(ep - bp));
372 1.1 mrg tname = bp;
373 1.1 mrg bp += n;
374 1.1 mrg continue;
375 1.1 mrg }
376 1.1 mrg if (type != qtype) {
377 1.28 assar if (type != T_KEY && type != T_SIG)
378 1.28 assar syslog(LOG_NOTICE|LOG_AUTH,
379 1.1 mrg "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
380 1.28 assar qname, p_class(C_IN), p_type(qtype),
381 1.28 assar p_type(type));
382 1.1 mrg cp += n;
383 1.1 mrg continue; /* XXX - had_error++ ? */
384 1.1 mrg }
385 1.1 mrg switch (type) {
386 1.1 mrg case T_PTR:
387 1.1 mrg if (strcasecmp(tname, bp) != 0) {
388 1.1 mrg syslog(LOG_NOTICE|LOG_AUTH,
389 1.1 mrg AskedForGot, qname, bp);
390 1.1 mrg cp += n;
391 1.1 mrg continue; /* XXX - had_error++ ? */
392 1.1 mrg }
393 1.78 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
394 1.79 christos if ((n < 0) || !maybe_hnok(res, bp)) {
395 1.1 mrg had_error++;
396 1.1 mrg break;
397 1.1 mrg }
398 1.1 mrg #if MULTI_PTRS_ARE_ALIASES
399 1.1 mrg cp += n;
400 1.80 christos if (cp != erdata)
401 1.80 christos goto no_recovery;
402 1.1 mrg if (!haveanswer)
403 1.80 christos hent->h_name = bp;
404 1.80 christos else if (ap < &aliases[MAXALIASES-1])
405 1.1 mrg *ap++ = bp;
406 1.1 mrg else
407 1.1 mrg n = -1;
408 1.1 mrg if (n != -1) {
409 1.78 christos n = (int)strlen(bp) + 1; /* for the \0 */
410 1.1 mrg if (n >= MAXHOSTNAMELEN) {
411 1.1 mrg had_error++;
412 1.1 mrg break;
413 1.1 mrg }
414 1.1 mrg bp += n;
415 1.1 mrg }
416 1.1 mrg break;
417 1.1 mrg #else
418 1.80 christos hent->h_name = bp;
419 1.59 christos if (res->options & RES_USE_INET6) {
420 1.1 mrg n = strlen(bp) + 1; /* for the \0 */
421 1.1 mrg if (n >= MAXHOSTNAMELEN) {
422 1.1 mrg had_error++;
423 1.1 mrg break;
424 1.1 mrg }
425 1.1 mrg bp += n;
426 1.80 christos map_v4v6_hostent(hent, &bp, ep);
427 1.1 mrg }
428 1.80 christos goto success;
429 1.1 mrg #endif
430 1.1 mrg case T_A:
431 1.1 mrg case T_AAAA:
432 1.80 christos if (strcasecmp(hent->h_name, bp) != 0) {
433 1.1 mrg syslog(LOG_NOTICE|LOG_AUTH,
434 1.80 christos AskedForGot, hent->h_name, bp);
435 1.1 mrg cp += n;
436 1.1 mrg continue; /* XXX - had_error++ ? */
437 1.1 mrg }
438 1.80 christos if (n != hent->h_length) {
439 1.1 mrg cp += n;
440 1.1 mrg continue;
441 1.51 itojun }
442 1.51 itojun if (type == T_AAAA) {
443 1.51 itojun struct in6_addr in6;
444 1.80 christos memcpy(&in6, cp, NS_IN6ADDRSZ);
445 1.51 itojun if (IN6_IS_ADDR_V4MAPPED(&in6)) {
446 1.51 itojun cp += n;
447 1.51 itojun continue;
448 1.51 itojun }
449 1.1 mrg }
450 1.1 mrg if (!haveanswer) {
451 1.25 lukem int nn;
452 1.1 mrg
453 1.80 christos hent->h_name = bp;
454 1.78 christos nn = (int)strlen(bp) + 1; /* for the \0 */
455 1.1 mrg bp += nn;
456 1.1 mrg }
457 1.1 mrg
458 1.14 lukem bp += sizeof(align) -
459 1.14 lukem (size_t)((u_long)bp % sizeof(align));
460 1.1 mrg
461 1.80 christos if (bp + n >= ep) {
462 1.76 christos debugprintf("size (%d) too big\n", res, n);
463 1.1 mrg had_error++;
464 1.1 mrg continue;
465 1.1 mrg }
466 1.80 christos if (hap >= &addr_ptrs[MAXADDRS - 1]) {
467 1.71 christos if (!toobig++) {
468 1.76 christos debugprintf("Too many addresses (%d)\n",
469 1.59 christos res, MAXADDRS);
470 1.71 christos }
471 1.1 mrg cp += n;
472 1.1 mrg continue;
473 1.1 mrg }
474 1.7 kleink (void)memcpy(*hap++ = bp, cp, (size_t)n);
475 1.1 mrg bp += n;
476 1.1 mrg cp += n;
477 1.80 christos if (cp != erdata)
478 1.80 christos goto no_recovery;
479 1.1 mrg break;
480 1.1 mrg default:
481 1.1 mrg abort();
482 1.1 mrg }
483 1.1 mrg if (!had_error)
484 1.1 mrg haveanswer++;
485 1.1 mrg }
486 1.1 mrg if (haveanswer) {
487 1.1 mrg *ap = NULL;
488 1.1 mrg *hap = NULL;
489 1.1 mrg /*
490 1.1 mrg * Note: we sort even if host can take only one address
491 1.1 mrg * in its return structures - should give it the "best"
492 1.1 mrg * address in that case, not some random one
493 1.1 mrg */
494 1.59 christos if (res->nsort && haveanswer > 1 && qtype == T_A)
495 1.80 christos addrsort(addr_ptrs, haveanswer, res);
496 1.80 christos if (!hent->h_name) {
497 1.78 christos n = (int)strlen(qname) + 1; /* for the \0 */
498 1.46 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN)
499 1.1 mrg goto no_recovery;
500 1.54 itojun strlcpy(bp, qname, (size_t)(ep - bp));
501 1.80 christos hent->h_name = bp;
502 1.1 mrg bp += n;
503 1.1 mrg }
504 1.59 christos if (res->options & RES_USE_INET6)
505 1.80 christos map_v4v6_hostent(hent, &bp, ep);
506 1.80 christos goto success;
507 1.1 mrg }
508 1.80 christos no_recovery:
509 1.80 christos *he = NO_RECOVERY;
510 1.80 christos return NULL;
511 1.80 christos success:
512 1.80 christos bp = (char *)ALIGN(bp);
513 1.80 christos n = (int)(ap - aliases);
514 1.80 christos qlen = (n + 1) * sizeof(*hent->h_aliases);
515 1.80 christos if ((size_t)(ep - bp) < qlen)
516 1.80 christos goto nospc;
517 1.80 christos hent->h_aliases = (void *)bp;
518 1.80 christos memcpy(bp, aliases, qlen);
519 1.80 christos
520 1.80 christos bp += qlen;
521 1.80 christos n = (int)(hap - addr_ptrs);
522 1.80 christos qlen = (n + 1) * sizeof(*hent->h_addr_list);
523 1.80 christos if ((size_t)(ep - bp) < qlen)
524 1.80 christos goto nospc;
525 1.80 christos hent->h_addr_list = (void *)bp;
526 1.80 christos memcpy(bp, addr_ptrs, qlen);
527 1.80 christos *he = NETDB_SUCCESS;
528 1.80 christos return hent;
529 1.80 christos nospc:
530 1.80 christos errno = ENOSPC;
531 1.80 christos *he = NETDB_INTERNAL;
532 1.59 christos return NULL;
533 1.1 mrg }
534 1.1 mrg
535 1.1 mrg struct hostent *
536 1.80 christos gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
537 1.80 christos int *he)
538 1.1 mrg {
539 1.59 christos res_state res = __res_get_state();
540 1.1 mrg
541 1.80 christos if (res == NULL) {
542 1.80 christos *he = NETDB_INTERNAL;
543 1.60 christos return NULL;
544 1.80 christos }
545 1.60 christos
546 1.25 lukem _DIAGASSERT(name != NULL);
547 1.25 lukem
548 1.59 christos if (res->options & RES_USE_INET6) {
549 1.80 christos hp = gethostbyname_internal(name, AF_INET6, res, hp, buf,
550 1.80 christos buflen, he);
551 1.59 christos if (hp) {
552 1.59 christos __res_put_state(res);
553 1.59 christos return hp;
554 1.59 christos }
555 1.1 mrg }
556 1.80 christos hp = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, he);
557 1.59 christos __res_put_state(res);
558 1.59 christos return hp;
559 1.1 mrg }
560 1.1 mrg
561 1.1 mrg struct hostent *
562 1.80 christos gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
563 1.80 christos size_t buflen, int *he)
564 1.59 christos {
565 1.59 christos res_state res = __res_get_state();
566 1.60 christos
567 1.80 christos if (res == NULL) {
568 1.80 christos *he = NETDB_INTERNAL;
569 1.60 christos return NULL;
570 1.80 christos }
571 1.80 christos hp = gethostbyname_internal(name, af, res, hp, buf, buflen, he);
572 1.59 christos __res_put_state(res);
573 1.59 christos return hp;
574 1.59 christos }
575 1.59 christos
576 1.59 christos static struct hostent *
577 1.80 christos gethostbyname_internal(const char *name, int af, res_state res,
578 1.80 christos struct hostent *hp, char *buf, size_t buflen, int *he)
579 1.1 mrg {
580 1.2 mrg const char *cp;
581 1.80 christos struct getnamaddr info;
582 1.80 christos size_t size;
583 1.12 lukem static const ns_dtab dtab[] = {
584 1.80 christos NS_FILES_CB(_hf_gethtbyname, NULL)
585 1.9 lukem { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
586 1.11 lukem NS_NIS_CB(_yp_gethtbyname, NULL)
587 1.71 christos NS_NULL_CB
588 1.9 lukem };
589 1.1 mrg
590 1.25 lukem _DIAGASSERT(name != NULL);
591 1.25 lukem
592 1.1 mrg switch (af) {
593 1.1 mrg case AF_INET:
594 1.80 christos size = NS_INADDRSZ;
595 1.1 mrg break;
596 1.1 mrg case AF_INET6:
597 1.80 christos size = NS_IN6ADDRSZ;
598 1.1 mrg break;
599 1.1 mrg default:
600 1.80 christos *he = NETDB_INTERNAL;
601 1.1 mrg errno = EAFNOSUPPORT;
602 1.59 christos return NULL;
603 1.1 mrg }
604 1.80 christos if (buflen < size)
605 1.80 christos goto nospc;
606 1.1 mrg
607 1.80 christos hp->h_addrtype = af;
608 1.80 christos hp->h_length = (int)size;
609 1.1 mrg
610 1.1 mrg /*
611 1.75 wiz * if there aren't any dots, it could be a user-level alias.
612 1.59 christos * this is also done in res_nquery() since we are not the only
613 1.1 mrg * function that looks up host names.
614 1.1 mrg */
615 1.1 mrg if (!strchr(name, '.') && (cp = __hostalias(name)))
616 1.1 mrg name = cp;
617 1.1 mrg
618 1.1 mrg /*
619 1.1 mrg * disallow names consisting only of digits/dots, unless
620 1.1 mrg * they end in a dot.
621 1.1 mrg */
622 1.35 itohy if (isdigit((u_char) name[0]))
623 1.1 mrg for (cp = name;; ++cp) {
624 1.1 mrg if (!*cp) {
625 1.1 mrg if (*--cp == '.')
626 1.1 mrg break;
627 1.1 mrg /*
628 1.1 mrg * All-numeric, no dot at the end.
629 1.1 mrg * Fake up a hostent as if we'd actually
630 1.1 mrg * done a lookup.
631 1.1 mrg */
632 1.80 christos goto fake;
633 1.1 mrg }
634 1.35 itohy if (!isdigit((u_char) *cp) && *cp != '.')
635 1.1 mrg break;
636 1.1 mrg }
637 1.35 itohy if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
638 1.1 mrg name[0] == ':')
639 1.1 mrg for (cp = name;; ++cp) {
640 1.1 mrg if (!*cp) {
641 1.1 mrg if (*--cp == '.')
642 1.1 mrg break;
643 1.1 mrg /*
644 1.1 mrg * All-IPv6-legal, no dot at the end.
645 1.1 mrg * Fake up a hostent as if we'd actually
646 1.1 mrg * done a lookup.
647 1.1 mrg */
648 1.80 christos goto fake;
649 1.1 mrg }
650 1.35 itohy if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
651 1.1 mrg break;
652 1.1 mrg }
653 1.1 mrg
654 1.80 christos *he = NETDB_INTERNAL;
655 1.80 christos info.hp = hp;
656 1.80 christos info.buf = buf;
657 1.80 christos info.buflen = buflen;
658 1.80 christos info.he = he;
659 1.80 christos if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
660 1.45 itojun default_dns_files, name, strlen(name), af) != NS_SUCCESS)
661 1.59 christos return NULL;
662 1.80 christos *he = NETDB_SUCCESS;
663 1.80 christos return hp;
664 1.80 christos nospc:
665 1.80 christos *he = NETDB_INTERNAL;
666 1.80 christos errno = ENOSPC;
667 1.80 christos return NULL;
668 1.80 christos fake:
669 1.80 christos ARRAY(hp->h_addr_list, 1, buf, buflen);
670 1.80 christos ARRAY(hp->h_aliases, 0, buf, buflen);
671 1.80 christos
672 1.80 christos hp->h_aliases[0] = NULL;
673 1.80 christos if (size > buflen)
674 1.80 christos goto nospc;
675 1.80 christos
676 1.80 christos if (inet_pton(af, name, buf) <= 0) {
677 1.80 christos *he = HOST_NOT_FOUND;
678 1.80 christos return NULL;
679 1.80 christos }
680 1.80 christos hp->h_addr_list[0] = buf;
681 1.80 christos hp->h_addr_list[1] = NULL;
682 1.80 christos buf += size;
683 1.80 christos buflen -= size;
684 1.80 christos SCOPY(hp->h_name, name, buf, buflen);
685 1.80 christos if (res->options & RES_USE_INET6)
686 1.80 christos map_v4v6_hostent(hp, &buf, buf + buflen);
687 1.80 christos *he = NETDB_SUCCESS;
688 1.59 christos return hp;
689 1.1 mrg }
690 1.1 mrg
691 1.1 mrg struct hostent *
692 1.80 christos gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
693 1.80 christos char *buf, size_t buflen, int *he)
694 1.1 mrg {
695 1.1 mrg const u_char *uaddr = (const u_char *)addr;
696 1.53 thorpej socklen_t size;
697 1.80 christos struct getnamaddr info;
698 1.12 lukem static const ns_dtab dtab[] = {
699 1.80 christos NS_FILES_CB(_hf_gethtbyaddr, NULL)
700 1.9 lukem { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
701 1.11 lukem NS_NIS_CB(_yp_gethtbyaddr, NULL)
702 1.71 christos NS_NULL_CB
703 1.9 lukem };
704 1.1 mrg
705 1.25 lukem _DIAGASSERT(addr != NULL);
706 1.25 lukem
707 1.80 christos if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
708 1.80 christos (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
709 1.80 christos IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
710 1.80 christos *he = HOST_NOT_FOUND;
711 1.59 christos return NULL;
712 1.41 itojun }
713 1.80 christos if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
714 1.80 christos (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
715 1.80 christos IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
716 1.1 mrg /* Unmap. */
717 1.80 christos uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
718 1.80 christos addr = uaddr;
719 1.1 mrg af = AF_INET;
720 1.80 christos len = NS_INADDRSZ;
721 1.1 mrg }
722 1.1 mrg switch (af) {
723 1.1 mrg case AF_INET:
724 1.80 christos size = NS_INADDRSZ;
725 1.1 mrg break;
726 1.1 mrg case AF_INET6:
727 1.80 christos size = NS_IN6ADDRSZ;
728 1.1 mrg break;
729 1.1 mrg default:
730 1.1 mrg errno = EAFNOSUPPORT;
731 1.80 christos *he = NETDB_INTERNAL;
732 1.59 christos return NULL;
733 1.1 mrg }
734 1.1 mrg if (size != len) {
735 1.1 mrg errno = EINVAL;
736 1.80 christos *he = NETDB_INTERNAL;
737 1.59 christos return NULL;
738 1.1 mrg }
739 1.80 christos info.hp = hp;
740 1.80 christos info.buf = buf;
741 1.80 christos info.buflen = buflen;
742 1.80 christos info.he = he;
743 1.80 christos *he = NETDB_INTERNAL;
744 1.80 christos if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
745 1.11 lukem default_dns_files, uaddr, len, af) != NS_SUCCESS)
746 1.59 christos return NULL;
747 1.80 christos *he = NETDB_SUCCESS;
748 1.59 christos return hp;
749 1.1 mrg }
750 1.1 mrg
751 1.80 christos static const char *_h_hosts = _PATH_HOSTS;
752 1.80 christos
753 1.80 christos void
754 1.80 christos _hf_sethostsfile(const char *f) {
755 1.80 christos _h_hosts = f;
756 1.80 christos }
757 1.80 christos
758 1.1 mrg void
759 1.80 christos sethostent_r(FILE **hf)
760 1.1 mrg {
761 1.80 christos if (!*hf)
762 1.80 christos *hf = fopen(_h_hosts, "re");
763 1.1 mrg else
764 1.80 christos rewind(*hf);
765 1.1 mrg }
766 1.1 mrg
767 1.1 mrg void
768 1.80 christos endhostent_r(FILE **hf)
769 1.1 mrg {
770 1.80 christos if (*hf) {
771 1.80 christos (void)fclose(*hf);
772 1.80 christos *hf = NULL;
773 1.1 mrg }
774 1.1 mrg }
775 1.1 mrg
776 1.1 mrg struct hostent *
777 1.80 christos gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
778 1.1 mrg {
779 1.80 christos char *p, *name;
780 1.25 lukem char *cp, **q;
781 1.1 mrg int af, len;
782 1.80 christos size_t llen, anum;
783 1.80 christos char *aliases[MAXALIASES];
784 1.80 christos struct in6_addr host_addr;
785 1.1 mrg
786 1.80 christos if (hf == NULL) {
787 1.80 christos *he = NETDB_INTERNAL;
788 1.80 christos errno = EINVAL;
789 1.59 christos return NULL;
790 1.1 mrg }
791 1.1 mrg again:
792 1.80 christos if ((p = fgetln(hf, &llen)) == NULL) {
793 1.80 christos *he = HOST_NOT_FOUND;
794 1.59 christos return NULL;
795 1.1 mrg }
796 1.80 christos if (llen < 1)
797 1.80 christos goto again;
798 1.1 mrg if (*p == '#')
799 1.1 mrg goto again;
800 1.80 christos p[llen] = '\0';
801 1.1 mrg if (!(cp = strpbrk(p, "#\n")))
802 1.1 mrg goto again;
803 1.1 mrg *cp = '\0';
804 1.1 mrg if (!(cp = strpbrk(p, " \t")))
805 1.1 mrg goto again;
806 1.1 mrg *cp++ = '\0';
807 1.80 christos if (inet_pton(AF_INET6, p, &host_addr) > 0) {
808 1.1 mrg af = AF_INET6;
809 1.80 christos len = NS_IN6ADDRSZ;
810 1.80 christos } else if (inet_pton(AF_INET, p, &host_addr) > 0) {
811 1.59 christos res_state res = __res_get_state();
812 1.60 christos if (res == NULL)
813 1.60 christos return NULL;
814 1.59 christos if (res->options & RES_USE_INET6) {
815 1.80 christos map_v4v6_address(buf, buf);
816 1.1 mrg af = AF_INET6;
817 1.80 christos len = NS_IN6ADDRSZ;
818 1.1 mrg } else {
819 1.1 mrg af = AF_INET;
820 1.80 christos len = NS_INADDRSZ;
821 1.1 mrg }
822 1.59 christos __res_put_state(res);
823 1.1 mrg } else {
824 1.1 mrg goto again;
825 1.1 mrg }
826 1.21 itojun /* if this is not something we're looking for, skip it. */
827 1.80 christos if (hent->h_addrtype != 0 && hent->h_addrtype != af)
828 1.21 itojun goto again;
829 1.80 christos if (hent->h_length != 0 && hent->h_length != len)
830 1.21 itojun goto again;
831 1.80 christos
832 1.1 mrg while (*cp == ' ' || *cp == '\t')
833 1.1 mrg cp++;
834 1.80 christos if ((cp = strpbrk(name = cp, " \t")) != NULL)
835 1.1 mrg *cp++ = '\0';
836 1.80 christos q = aliases;
837 1.1 mrg while (cp && *cp) {
838 1.1 mrg if (*cp == ' ' || *cp == '\t') {
839 1.1 mrg cp++;
840 1.1 mrg continue;
841 1.1 mrg }
842 1.80 christos if (q >= &aliases[__arraycount(aliases)])
843 1.80 christos goto nospc;
844 1.80 christos *q++ = cp;
845 1.2 mrg if ((cp = strpbrk(cp, " \t")) != NULL)
846 1.1 mrg *cp++ = '\0';
847 1.1 mrg }
848 1.80 christos hent->h_length = len;
849 1.80 christos hent->h_addrtype = af;
850 1.80 christos ARRAY(hent->h_addr_list, 1, buf, buflen);
851 1.80 christos anum = (size_t)(q - aliases);
852 1.80 christos ARRAY(hent->h_aliases, anum, buf, buflen);
853 1.80 christos COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
854 1.80 christos hent->h_addr_list[1] = NULL;
855 1.80 christos
856 1.80 christos SCOPY(hent->h_name, name, buf, buflen);
857 1.80 christos for (size_t i = 0; i < anum; i++)
858 1.80 christos SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
859 1.80 christos hent->h_aliases[anum] = NULL;
860 1.80 christos
861 1.80 christos *he = NETDB_SUCCESS;
862 1.80 christos return hent;
863 1.80 christos nospc:
864 1.80 christos errno = ENOSPC;
865 1.80 christos *he = NETDB_INTERNAL;
866 1.80 christos return NULL;
867 1.1 mrg }
868 1.1 mrg
869 1.13 christos /*ARGSUSED*/
870 1.9 lukem int
871 1.80 christos _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
872 1.1 mrg {
873 1.1 mrg struct hostent *hp;
874 1.9 lukem const char *name;
875 1.30 mycroft int af;
876 1.80 christos struct getnamaddr *info = rv;
877 1.1 mrg
878 1.25 lukem _DIAGASSERT(rv != NULL);
879 1.25 lukem
880 1.9 lukem name = va_arg(ap, char *);
881 1.80 christos /* NOSTRICT skip string len */(void)va_arg(ap, int);
882 1.9 lukem af = va_arg(ap, int);
883 1.9 lukem
884 1.20 itojun #if 0
885 1.59 christos {
886 1.59 christos res_state res = __res_get_state();
887 1.60 christos if (res == NULL)
888 1.60 christos return NS_NOTFOUND;
889 1.59 christos if (res->options & RES_USE_INET6)
890 1.80 christos hp = _hf_gethtbyname2(name, AF_INET6, info);
891 1.80 christos else
892 1.80 christos hp = NULL;
893 1.80 christos if (hp == NULL)
894 1.80 christos hp = _hf_gethtbyname2(name, AF_INET, info);
895 1.59 christos __res_put_state(res);
896 1.59 christos }
897 1.20 itojun #else
898 1.80 christos hp = _hf_gethtbyname2(name, af, info);
899 1.20 itojun #endif
900 1.41 itojun if (hp == NULL) {
901 1.80 christos *info->he = HOST_NOT_FOUND;
902 1.9 lukem return NS_NOTFOUND;
903 1.1 mrg }
904 1.9 lukem return NS_SUCCESS;
905 1.1 mrg }
906 1.1 mrg
907 1.80 christos static struct hostent *
908 1.80 christos _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
909 1.1 mrg {
910 1.80 christos struct hostent *hp, hent;
911 1.80 christos char *buf, *ptr;
912 1.80 christos size_t len, anum, num, i;
913 1.80 christos FILE *hf;
914 1.80 christos char *aliases[MAXALIASES];
915 1.80 christos char *addr_ptrs[MAXADDRS + 1];
916 1.25 lukem
917 1.25 lukem _DIAGASSERT(name != NULL);
918 1.18 tron
919 1.80 christos hf = NULL;
920 1.80 christos sethostent_r(&hf);
921 1.80 christos if (hf == NULL) {
922 1.80 christos errno = EINVAL;
923 1.80 christos *info->he = NETDB_INTERNAL;
924 1.80 christos return NULL;
925 1.80 christos }
926 1.80 christos
927 1.80 christos if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
928 1.80 christos *info->he = NETDB_INTERNAL;
929 1.80 christos return NULL;
930 1.80 christos }
931 1.80 christos
932 1.80 christos anum = 0; /* XXX: gcc */
933 1.80 christos hent.h_name = NULL; /* XXX: gcc */
934 1.80 christos hent.h_addrtype = 0; /* XXX: gcc */
935 1.80 christos hent.h_length = 0; /* XXX: gcc */
936 1.80 christos
937 1.80 christos for (num = 0; num < MAXADDRS;) {
938 1.80 christos info->hp->h_addrtype = af;
939 1.80 christos info->hp->h_length = 0;
940 1.80 christos
941 1.80 christos hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
942 1.80 christos info->he);
943 1.80 christos if (hp == NULL)
944 1.80 christos break;
945 1.80 christos
946 1.80 christos if (strcasecmp(hp->h_name, name) != 0) {
947 1.80 christos char **cp;
948 1.80 christos for (cp = hp->h_aliases; *cp != NULL; cp++)
949 1.18 tron if (strcasecmp(*cp, name) == 0)
950 1.18 tron break;
951 1.18 tron if (*cp == NULL) continue;
952 1.18 tron }
953 1.18 tron
954 1.18 tron if (num == 0) {
955 1.80 christos hent.h_addrtype = af = hp->h_addrtype;
956 1.80 christos hent.h_length = hp->h_length;
957 1.18 tron
958 1.80 christos SCOPY(hent.h_name, hp->h_name, ptr, len);
959 1.80 christos for (anum = 0; hp->h_aliases[anum]; anum++) {
960 1.80 christos if (anum >= __arraycount(aliases))
961 1.80 christos goto nospc;
962 1.80 christos SCOPY(aliases[anum], hp->h_aliases[anum],
963 1.80 christos ptr, len);
964 1.80 christos }
965 1.80 christos ptr = (void *)ALIGN(ptr);
966 1.80 christos if ((size_t)(ptr - buf) >= info->buflen)
967 1.80 christos goto nospc;
968 1.80 christos }
969 1.80 christos
970 1.80 christos if (num >= __arraycount(addr_ptrs))
971 1.80 christos goto nospc;
972 1.80 christos COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
973 1.80 christos len);
974 1.18 tron num++;
975 1.1 mrg }
976 1.80 christos endhostent_r(&hf);
977 1.18 tron
978 1.80 christos if (num == 0) {
979 1.80 christos *info->he = HOST_NOT_FOUND;
980 1.18 tron return NULL;
981 1.18 tron }
982 1.18 tron
983 1.80 christos hp = info->hp;
984 1.80 christos ptr = info->buf;
985 1.80 christos len = info->buflen;
986 1.18 tron
987 1.80 christos hp->h_addrtype = hent.h_addrtype;
988 1.80 christos hp->h_length = hent.h_length;
989 1.80 christos
990 1.80 christos ARRAY(hp->h_aliases, anum, ptr, len);
991 1.80 christos ARRAY(hp->h_addr_list, num, ptr, len);
992 1.80 christos
993 1.80 christos for (i = 0; i < num; i++)
994 1.80 christos COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
995 1.80 christos hp->h_addr_list[num] = NULL;
996 1.80 christos
997 1.80 christos SCOPY(hp->h_name, hent.h_name, ptr, len);
998 1.18 tron
999 1.80 christos for (i = 0; i < anum; i++)
1000 1.80 christos SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
1001 1.80 christos hp->h_aliases[anum] = NULL;
1002 1.18 tron
1003 1.80 christos return hp;
1004 1.80 christos nospc:
1005 1.80 christos *info->he = NETDB_INTERNAL;
1006 1.80 christos errno = ENOSPC;
1007 1.80 christos return NULL;
1008 1.1 mrg }
1009 1.1 mrg
1010 1.13 christos /*ARGSUSED*/
1011 1.9 lukem int
1012 1.80 christos _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1013 1.1 mrg {
1014 1.80 christos struct hostent *hp;
1015 1.9 lukem const unsigned char *addr;
1016 1.80 christos struct getnamaddr *info = rv;
1017 1.80 christos FILE *hf;
1018 1.9 lukem
1019 1.25 lukem _DIAGASSERT(rv != NULL);
1020 1.25 lukem
1021 1.9 lukem addr = va_arg(ap, unsigned char *);
1022 1.80 christos info->hp->h_length = va_arg(ap, int);
1023 1.80 christos info->hp->h_addrtype = va_arg(ap, int);
1024 1.9 lukem
1025 1.80 christos hf = NULL;
1026 1.80 christos sethostent_r(&hf);
1027 1.80 christos if (hf == NULL) {
1028 1.80 christos *info->he = NETDB_INTERNAL;
1029 1.80 christos return NS_UNAVAIL;
1030 1.80 christos }
1031 1.80 christos while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
1032 1.80 christos info->he)) != NULL)
1033 1.80 christos if (!memcmp(hp->h_addr_list[0], addr, hp->h_length))
1034 1.80 christos break;
1035 1.80 christos endhostent_r(&hf);
1036 1.1 mrg
1037 1.80 christos if (hp == NULL) {
1038 1.80 christos *info->he = HOST_NOT_FOUND;
1039 1.9 lukem return NS_NOTFOUND;
1040 1.9 lukem }
1041 1.9 lukem return NS_SUCCESS;
1042 1.1 mrg }
1043 1.1 mrg
1044 1.1 mrg static void
1045 1.59 christos map_v4v6_address(const char *src, char *dst)
1046 1.1 mrg {
1047 1.1 mrg u_char *p = (u_char *)dst;
1048 1.80 christos char tmp[NS_INADDRSZ];
1049 1.1 mrg int i;
1050 1.1 mrg
1051 1.25 lukem _DIAGASSERT(src != NULL);
1052 1.25 lukem _DIAGASSERT(dst != NULL);
1053 1.25 lukem
1054 1.1 mrg /* Stash a temporary copy so our caller can update in place. */
1055 1.80 christos (void)memcpy(tmp, src, NS_INADDRSZ);
1056 1.1 mrg /* Mark this ipv6 addr as a mapped ipv4. */
1057 1.1 mrg for (i = 0; i < 10; i++)
1058 1.1 mrg *p++ = 0x00;
1059 1.1 mrg *p++ = 0xff;
1060 1.1 mrg *p++ = 0xff;
1061 1.1 mrg /* Retrieve the saved copy and we're done. */
1062 1.80 christos (void)memcpy(p, tmp, NS_INADDRSZ);
1063 1.1 mrg }
1064 1.1 mrg
1065 1.1 mrg static void
1066 1.59 christos map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
1067 1.1 mrg {
1068 1.1 mrg char **ap;
1069 1.1 mrg
1070 1.25 lukem _DIAGASSERT(hp != NULL);
1071 1.25 lukem _DIAGASSERT(bpp != NULL);
1072 1.47 itojun _DIAGASSERT(ep != NULL);
1073 1.25 lukem
1074 1.80 christos if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
1075 1.1 mrg return;
1076 1.1 mrg hp->h_addrtype = AF_INET6;
1077 1.80 christos hp->h_length = NS_IN6ADDRSZ;
1078 1.1 mrg for (ap = hp->h_addr_list; *ap; ap++) {
1079 1.78 christos int i = (int)(sizeof(align) -
1080 1.78 christos (size_t)((u_long)*bpp % sizeof(align)));
1081 1.1 mrg
1082 1.80 christos if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
1083 1.1 mrg /* Out of memory. Truncate address list here. XXX */
1084 1.1 mrg *ap = NULL;
1085 1.1 mrg return;
1086 1.1 mrg }
1087 1.1 mrg *bpp += i;
1088 1.1 mrg map_v4v6_address(*ap, *bpp);
1089 1.1 mrg *ap = *bpp;
1090 1.80 christos *bpp += NS_IN6ADDRSZ;
1091 1.1 mrg }
1092 1.1 mrg }
1093 1.1 mrg
1094 1.1 mrg static void
1095 1.59 christos addrsort(char **ap, int num, res_state res)
1096 1.1 mrg {
1097 1.1 mrg int i, j;
1098 1.1 mrg char **p;
1099 1.1 mrg short aval[MAXADDRS];
1100 1.1 mrg int needsort = 0;
1101 1.1 mrg
1102 1.25 lukem _DIAGASSERT(ap != NULL);
1103 1.25 lukem
1104 1.1 mrg p = ap;
1105 1.1 mrg for (i = 0; i < num; i++, p++) {
1106 1.59 christos for (j = 0 ; (unsigned)j < res->nsort; j++)
1107 1.59 christos if (res->sort_list[j].addr.s_addr ==
1108 1.14 lukem (((struct in_addr *)(void *)(*p))->s_addr &
1109 1.59 christos res->sort_list[j].mask))
1110 1.1 mrg break;
1111 1.1 mrg aval[i] = j;
1112 1.1 mrg if (needsort == 0 && i > 0 && j < aval[i-1])
1113 1.1 mrg needsort = i;
1114 1.1 mrg }
1115 1.1 mrg if (!needsort)
1116 1.1 mrg return;
1117 1.1 mrg
1118 1.1 mrg while (needsort < num) {
1119 1.1 mrg for (j = needsort - 1; j >= 0; j--) {
1120 1.1 mrg if (aval[j] > aval[j+1]) {
1121 1.1 mrg char *hp;
1122 1.1 mrg
1123 1.1 mrg i = aval[j];
1124 1.1 mrg aval[j] = aval[j+1];
1125 1.1 mrg aval[j+1] = i;
1126 1.1 mrg
1127 1.1 mrg hp = ap[j];
1128 1.1 mrg ap[j] = ap[j+1];
1129 1.1 mrg ap[j+1] = hp;
1130 1.1 mrg } else
1131 1.1 mrg break;
1132 1.1 mrg }
1133 1.1 mrg needsort++;
1134 1.1 mrg }
1135 1.1 mrg }
1136 1.1 mrg
1137 1.1 mrg
1138 1.13 christos /*ARGSUSED*/
1139 1.9 lukem int
1140 1.61 ginsbach _dns_gethtbyname(void *rv, void *cb_data, va_list ap)
1141 1.9 lukem {
1142 1.52 itojun querybuf *buf;
1143 1.9 lukem int n, type;
1144 1.9 lukem struct hostent *hp;
1145 1.9 lukem const char *name;
1146 1.59 christos res_state res;
1147 1.80 christos struct getnamaddr *info = rv;
1148 1.9 lukem
1149 1.25 lukem _DIAGASSERT(rv != NULL);
1150 1.25 lukem
1151 1.9 lukem name = va_arg(ap, char *);
1152 1.80 christos /* NOSTRICT skip string len */(void)va_arg(ap, int);
1153 1.80 christos info->hp->h_addrtype = va_arg(ap, int);
1154 1.9 lukem
1155 1.80 christos switch (info->hp->h_addrtype) {
1156 1.9 lukem case AF_INET:
1157 1.80 christos info->hp->h_length = NS_INADDRSZ;
1158 1.9 lukem type = T_A;
1159 1.9 lukem break;
1160 1.9 lukem case AF_INET6:
1161 1.80 christos info->hp->h_length = NS_IN6ADDRSZ;
1162 1.9 lukem type = T_AAAA;
1163 1.9 lukem break;
1164 1.9 lukem default:
1165 1.9 lukem return NS_UNAVAIL;
1166 1.9 lukem }
1167 1.52 itojun buf = malloc(sizeof(*buf));
1168 1.52 itojun if (buf == NULL) {
1169 1.80 christos *info->he = NETDB_INTERNAL;
1170 1.52 itojun return NS_NOTFOUND;
1171 1.52 itojun }
1172 1.59 christos res = __res_get_state();
1173 1.69 christos if (res == NULL) {
1174 1.69 christos free(buf);
1175 1.60 christos return NS_NOTFOUND;
1176 1.69 christos }
1177 1.78 christos n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
1178 1.52 itojun if (n < 0) {
1179 1.52 itojun free(buf);
1180 1.76 christos debugprintf("res_nsearch failed (%d)\n", res, n);
1181 1.59 christos __res_put_state(res);
1182 1.9 lukem return NS_NOTFOUND;
1183 1.9 lukem }
1184 1.80 christos hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
1185 1.80 christos info->buflen, info->he);
1186 1.52 itojun free(buf);
1187 1.59 christos __res_put_state(res);
1188 1.9 lukem if (hp == NULL)
1189 1.9 lukem switch (h_errno) {
1190 1.9 lukem case HOST_NOT_FOUND:
1191 1.9 lukem return NS_NOTFOUND;
1192 1.9 lukem case TRY_AGAIN:
1193 1.9 lukem return NS_TRYAGAIN;
1194 1.9 lukem default:
1195 1.9 lukem return NS_UNAVAIL;
1196 1.9 lukem }
1197 1.9 lukem return NS_SUCCESS;
1198 1.9 lukem }
1199 1.9 lukem
1200 1.13 christos /*ARGSUSED*/
1201 1.9 lukem int
1202 1.59 christos _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1203 1.9 lukem {
1204 1.49 itojun char qbuf[MAXDNAME + 1], *qp, *ep;
1205 1.9 lukem int n;
1206 1.52 itojun querybuf *buf;
1207 1.9 lukem struct hostent *hp;
1208 1.9 lukem const unsigned char *uaddr;
1209 1.80 christos int advance;
1210 1.59 christos res_state res;
1211 1.80 christos char *bf;
1212 1.80 christos size_t blen;
1213 1.80 christos struct getnamaddr *info = rv;
1214 1.9 lukem
1215 1.25 lukem _DIAGASSERT(rv != NULL);
1216 1.25 lukem
1217 1.9 lukem uaddr = va_arg(ap, unsigned char *);
1218 1.80 christos info->hp->h_length = va_arg(ap, int);
1219 1.80 christos info->hp->h_addrtype = va_arg(ap, int);
1220 1.9 lukem
1221 1.80 christos switch (info->hp->h_addrtype) {
1222 1.9 lukem case AF_INET:
1223 1.43 itojun (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1224 1.43 itojun (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1225 1.43 itojun (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1226 1.9 lukem break;
1227 1.9 lukem
1228 1.9 lukem case AF_INET6:
1229 1.9 lukem qp = qbuf;
1230 1.49 itojun ep = qbuf + sizeof(qbuf) - 1;
1231 1.80 christos for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
1232 1.49 itojun advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1233 1.49 itojun uaddr[n] & 0xf,
1234 1.43 itojun ((unsigned int)uaddr[n] >> 4) & 0xf);
1235 1.49 itojun if (advance > 0 && qp + advance < ep)
1236 1.43 itojun qp += advance;
1237 1.50 itojun else {
1238 1.80 christos *info->he = NETDB_INTERNAL;
1239 1.43 itojun return NS_NOTFOUND;
1240 1.50 itojun }
1241 1.9 lukem }
1242 1.50 itojun if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1243 1.80 christos *info->he = NETDB_INTERNAL;
1244 1.49 itojun return NS_NOTFOUND;
1245 1.50 itojun }
1246 1.9 lukem break;
1247 1.9 lukem default:
1248 1.80 christos return NS_UNAVAIL;
1249 1.9 lukem }
1250 1.9 lukem
1251 1.52 itojun buf = malloc(sizeof(*buf));
1252 1.52 itojun if (buf == NULL) {
1253 1.80 christos *info->he = NETDB_INTERNAL;
1254 1.52 itojun return NS_NOTFOUND;
1255 1.52 itojun }
1256 1.59 christos res = __res_get_state();
1257 1.70 christos if (res == NULL) {
1258 1.70 christos free(buf);
1259 1.60 christos return NS_NOTFOUND;
1260 1.70 christos }
1261 1.78 christos n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
1262 1.9 lukem if (n < 0) {
1263 1.52 itojun free(buf);
1264 1.76 christos debugprintf("res_nquery failed (%d)\n", res, n);
1265 1.59 christos __res_put_state(res);
1266 1.9 lukem return NS_NOTFOUND;
1267 1.9 lukem }
1268 1.80 christos hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
1269 1.80 christos info->buflen, info->he);
1270 1.52 itojun free(buf);
1271 1.59 christos if (hp == NULL) {
1272 1.59 christos __res_put_state(res);
1273 1.80 christos switch (*info->he) {
1274 1.9 lukem case HOST_NOT_FOUND:
1275 1.9 lukem return NS_NOTFOUND;
1276 1.9 lukem case TRY_AGAIN:
1277 1.9 lukem return NS_TRYAGAIN;
1278 1.9 lukem default:
1279 1.9 lukem return NS_UNAVAIL;
1280 1.9 lukem }
1281 1.59 christos }
1282 1.80 christos
1283 1.80 christos bf = (void *)(hp->h_addr_list + 2);
1284 1.80 christos blen = (size_t)(bf - info->buf);
1285 1.80 christos if (blen + info->hp->h_length > info->buflen)
1286 1.80 christos goto nospc;
1287 1.80 christos hp->h_addr_list[0] = bf;
1288 1.80 christos hp->h_addr_list[1] = NULL;
1289 1.80 christos (void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
1290 1.80 christos if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1291 1.80 christos if (blen + NS_IN6ADDRSZ > info->buflen)
1292 1.80 christos goto nospc;
1293 1.80 christos map_v4v6_address(bf, bf);
1294 1.9 lukem hp->h_addrtype = AF_INET6;
1295 1.80 christos hp->h_length = NS_IN6ADDRSZ;
1296 1.9 lukem }
1297 1.9 lukem
1298 1.59 christos __res_put_state(res);
1299 1.80 christos *info->he = NETDB_SUCCESS;
1300 1.9 lukem return NS_SUCCESS;
1301 1.80 christos nospc:
1302 1.80 christos *info->he = NETDB_INTERNAL;
1303 1.80 christos return NS_UNAVAIL;
1304 1.9 lukem }
1305 1.9 lukem
1306 1.2 mrg #ifdef YP
1307 1.13 christos /*ARGSUSED*/
1308 1.80 christos static struct hostent *
1309 1.80 christos _yp_hostent(char *line, int af, struct getnamaddr *info)
1310 1.2 mrg {
1311 1.80 christos struct in6_addr host_addrs[MAXADDRS];
1312 1.80 christos char *aliases[MAXALIASES];
1313 1.2 mrg char *p = line;
1314 1.80 christos char *cp, **q, *ptr;
1315 1.80 christos size_t len, anum, i;
1316 1.37 itojun int addrok;
1317 1.2 mrg int more;
1318 1.53 thorpej size_t naddrs;
1319 1.80 christos struct hostent *hp = info->hp;
1320 1.2 mrg
1321 1.25 lukem _DIAGASSERT(line != NULL);
1322 1.25 lukem
1323 1.80 christos hp->h_name = NULL;
1324 1.80 christos hp->h_addrtype = af;
1325 1.37 itojun switch (af) {
1326 1.37 itojun case AF_INET:
1327 1.80 christos hp->h_length = NS_INADDRSZ;
1328 1.37 itojun break;
1329 1.37 itojun case AF_INET6:
1330 1.80 christos hp->h_length = NS_IN6ADDRSZ;
1331 1.37 itojun break;
1332 1.37 itojun default:
1333 1.59 christos return NULL;
1334 1.37 itojun }
1335 1.37 itojun naddrs = 0;
1336 1.80 christos q = aliases;
1337 1.2 mrg
1338 1.2 mrg nextline:
1339 1.36 itojun /* check for host_addrs overflow */
1340 1.80 christos if (naddrs >= __arraycount(host_addrs))
1341 1.36 itojun goto done;
1342 1.36 itojun
1343 1.2 mrg more = 0;
1344 1.2 mrg cp = strpbrk(p, " \t");
1345 1.36 itojun if (cp == NULL)
1346 1.36 itojun goto done;
1347 1.2 mrg *cp++ = '\0';
1348 1.2 mrg
1349 1.37 itojun /* p has should have an address */
1350 1.80 christos addrok = inet_pton(af, p, &host_addrs[naddrs]);
1351 1.37 itojun if (addrok != 1) {
1352 1.37 itojun /* skip to the next line */
1353 1.37 itojun while (cp && *cp) {
1354 1.37 itojun if (*cp == '\n') {
1355 1.37 itojun cp++;
1356 1.37 itojun goto nextline;
1357 1.37 itojun }
1358 1.37 itojun cp++;
1359 1.37 itojun }
1360 1.37 itojun goto done;
1361 1.37 itojun }
1362 1.37 itojun
1363 1.2 mrg while (*cp == ' ' || *cp == '\t')
1364 1.2 mrg cp++;
1365 1.2 mrg p = cp;
1366 1.2 mrg cp = strpbrk(p, " \t\n");
1367 1.2 mrg if (cp != NULL) {
1368 1.2 mrg if (*cp == '\n')
1369 1.2 mrg more = 1;
1370 1.2 mrg *cp++ = '\0';
1371 1.2 mrg }
1372 1.80 christos if (!hp->h_name)
1373 1.80 christos hp->h_name = p;
1374 1.80 christos else if (strcmp(hp->h_name, p) == 0)
1375 1.2 mrg ;
1376 1.80 christos else if (q < &aliases[MAXALIASES - 1])
1377 1.2 mrg *q++ = p;
1378 1.2 mrg p = cp;
1379 1.2 mrg if (more)
1380 1.2 mrg goto nextline;
1381 1.2 mrg
1382 1.2 mrg while (cp && *cp) {
1383 1.2 mrg if (*cp == ' ' || *cp == '\t') {
1384 1.2 mrg cp++;
1385 1.2 mrg continue;
1386 1.2 mrg }
1387 1.2 mrg if (*cp == '\n') {
1388 1.2 mrg cp++;
1389 1.2 mrg goto nextline;
1390 1.2 mrg }
1391 1.80 christos if (q < &aliases[MAXALIASES - 1])
1392 1.2 mrg *q++ = cp;
1393 1.2 mrg cp = strpbrk(cp, " \t");
1394 1.2 mrg if (cp != NULL)
1395 1.2 mrg *cp++ = '\0';
1396 1.2 mrg }
1397 1.37 itojun
1398 1.2 mrg done:
1399 1.80 christos if (hp->h_name == NULL)
1400 1.59 christos return NULL;
1401 1.80 christos
1402 1.80 christos ptr = info->buf;
1403 1.80 christos len = info->buflen;
1404 1.80 christos
1405 1.80 christos anum = (size_t)(q - aliases);
1406 1.80 christos ARRAY(hp->h_addr_list, naddrs, ptr, len);
1407 1.80 christos ARRAY(hp->h_aliases, anum, ptr, len);
1408 1.80 christos
1409 1.80 christos for (i = 0; i < naddrs; i++)
1410 1.80 christos COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
1411 1.80 christos ptr, len);
1412 1.80 christos hp->h_addr_list[naddrs] = NULL;
1413 1.80 christos
1414 1.80 christos SCOPY(hp->h_name, hp->h_name, ptr, len);
1415 1.80 christos
1416 1.80 christos for (i = 0; i < anum; i++)
1417 1.80 christos SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
1418 1.80 christos hp->h_aliases[anum] = NULL;
1419 1.80 christos
1420 1.80 christos return hp;
1421 1.80 christos nospc:
1422 1.80 christos *info->he = NETDB_INTERNAL;
1423 1.80 christos errno = ENOSPC;
1424 1.80 christos return NULL;
1425 1.2 mrg }
1426 1.2 mrg
1427 1.13 christos /*ARGSUSED*/
1428 1.9 lukem int
1429 1.59 christos _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1430 1.2 mrg {
1431 1.59 christos struct hostent *hp = NULL;
1432 1.80 christos char *ypcurrent;
1433 1.80 christos int ypcurrentlen, r;
1434 1.37 itojun char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1435 1.9 lukem const unsigned char *uaddr;
1436 1.17 christos int af;
1437 1.37 itojun const char *map;
1438 1.80 christos struct getnamaddr *info = rv;
1439 1.9 lukem
1440 1.25 lukem _DIAGASSERT(rv != NULL);
1441 1.25 lukem
1442 1.9 lukem uaddr = va_arg(ap, unsigned char *);
1443 1.17 christos /* NOSTRICT skip len */(void)va_arg(ap, int);
1444 1.9 lukem af = va_arg(ap, int);
1445 1.2 mrg
1446 1.2 mrg if (!__ypdomain) {
1447 1.2 mrg if (_yp_check(&__ypdomain) == 0)
1448 1.9 lukem return NS_UNAVAIL;
1449 1.2 mrg }
1450 1.9 lukem /*
1451 1.37 itojun * XXX unfortunately, we cannot support IPv6 extended scoped address
1452 1.37 itojun * notation here. gethostbyaddr() is not scope-aware. too bad.
1453 1.9 lukem */
1454 1.78 christos if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
1455 1.37 itojun return NS_UNAVAIL;
1456 1.37 itojun switch (af) {
1457 1.37 itojun case AF_INET:
1458 1.37 itojun map = "hosts.byaddr";
1459 1.37 itojun break;
1460 1.37 itojun default:
1461 1.37 itojun map = "ipnodes.byaddr";
1462 1.37 itojun break;
1463 1.37 itojun }
1464 1.80 christos ypcurrent = NULL;
1465 1.37 itojun r = yp_match(__ypdomain, map, name,
1466 1.80 christos (int)strlen(name), &ypcurrent, &ypcurrentlen);
1467 1.59 christos if (r == 0)
1468 1.80 christos hp = _yp_hostent(ypcurrent, af, info);
1469 1.80 christos else
1470 1.80 christos hp = NULL;
1471 1.80 christos free(ypcurrent);
1472 1.59 christos if (hp == NULL) {
1473 1.80 christos *info->he = HOST_NOT_FOUND;
1474 1.9 lukem return NS_NOTFOUND;
1475 1.9 lukem }
1476 1.9 lukem return NS_SUCCESS;
1477 1.2 mrg }
1478 1.2 mrg
1479 1.13 christos /*ARGSUSED*/
1480 1.9 lukem int
1481 1.59 christos _yp_gethtbyname(void *rv, void *cb_data, va_list ap)
1482 1.2 mrg {
1483 1.80 christos struct hostent *hp;
1484 1.80 christos char *ypcurrent;
1485 1.80 christos int ypcurrentlen, r;
1486 1.9 lukem const char *name;
1487 1.17 christos int af;
1488 1.37 itojun const char *map;
1489 1.80 christos struct getnamaddr *info = rv;
1490 1.25 lukem
1491 1.25 lukem _DIAGASSERT(rv != NULL);
1492 1.9 lukem
1493 1.9 lukem name = va_arg(ap, char *);
1494 1.80 christos /* NOSTRICT skip string len */(void)va_arg(ap, int);
1495 1.9 lukem af = va_arg(ap, int);
1496 1.2 mrg
1497 1.2 mrg if (!__ypdomain) {
1498 1.2 mrg if (_yp_check(&__ypdomain) == 0)
1499 1.9 lukem return NS_UNAVAIL;
1500 1.2 mrg }
1501 1.37 itojun switch (af) {
1502 1.37 itojun case AF_INET:
1503 1.37 itojun map = "hosts.byname";
1504 1.37 itojun break;
1505 1.37 itojun default:
1506 1.37 itojun map = "ipnodes.byname";
1507 1.37 itojun break;
1508 1.37 itojun }
1509 1.80 christos ypcurrent = NULL;
1510 1.37 itojun r = yp_match(__ypdomain, map, name,
1511 1.80 christos (int)strlen(name), &ypcurrent, &ypcurrentlen);
1512 1.59 christos if (r == 0)
1513 1.80 christos hp = _yp_hostent(ypcurrent, af, info);
1514 1.80 christos else
1515 1.80 christos hp = NULL;
1516 1.80 christos free(ypcurrent);
1517 1.59 christos if (hp == NULL) {
1518 1.80 christos *info->he = HOST_NOT_FOUND;
1519 1.9 lukem return NS_NOTFOUND;
1520 1.9 lukem }
1521 1.9 lukem return NS_SUCCESS;
1522 1.2 mrg }
1523 1.2 mrg #endif
1524 1.80 christos
1525 1.80 christos /*
1526 1.80 christos * Non-reentrant versions.
1527 1.80 christos */
1528 1.80 christos FILE *_h_file;
1529 1.80 christos static struct hostent h_ent;
1530 1.80 christos static char h_buf[16384];
1531 1.80 christos
1532 1.80 christos struct hostent *
1533 1.80 christos gethostbyaddr(const char *addr, socklen_t len, int af) {
1534 1.80 christos return gethostbyaddr_r(addr, len, af, &h_ent, h_buf, sizeof(h_buf),
1535 1.80 christos &h_errno);
1536 1.80 christos }
1537 1.80 christos
1538 1.80 christos struct hostent *
1539 1.80 christos gethostbyname(const char *name) {
1540 1.80 christos return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1541 1.80 christos }
1542 1.80 christos
1543 1.80 christos struct hostent *
1544 1.80 christos gethostbyname2(const char *name, int af) {
1545 1.80 christos return gethostbyname2_r(name, af, &h_ent, h_buf, sizeof(h_buf),
1546 1.80 christos &h_errno);
1547 1.80 christos }
1548 1.80 christos
1549 1.80 christos struct hostent *
1550 1.80 christos gethostent(void)
1551 1.80 christos {
1552 1.80 christos if (_h_file == NULL) {
1553 1.80 christos sethostent_r(&_h_file);
1554 1.80 christos if (_h_file == NULL) {
1555 1.80 christos h_errno = NETDB_INTERNAL;
1556 1.80 christos return NULL;
1557 1.80 christos }
1558 1.80 christos }
1559 1.80 christos memset(&h_ent, 0, sizeof(h_ent));
1560 1.80 christos return gethostent_r(_h_file, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1561 1.80 christos }
1562