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