Updated from BIND-4.9.3P2C3
[kopensolaris-gnu/glibc.git] / resolv / gethnamaddr.c
1 /*
2  * ++Copyright++ 1985, 1988, 1993
3  * -
4  * Copyright (c) 1985, 1988, 1993
5  *    The Regents of the University of California.  All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  * -
35  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36  * 
37  * Permission to use, copy, modify, and distribute this software for any
38  * purpose with or without fee is hereby granted, provided that the above
39  * copyright notice and this permission notice appear in all copies, and that
40  * the name of Digital Equipment Corporation not be used in advertising or
41  * publicity pertaining to distribution of the document or software without
42  * specific, written prior permission.
43  * 
44  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
47  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51  * SOFTWARE.
52  * -
53  * --Copyright--
54  */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c     8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
60
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
66
67 #include <stdio.h>
68 #include <netdb.h>
69 #include <resolv.h>
70 #include <ctype.h>
71 #include <errno.h>
72 #include <syslog.h>
73
74 #ifndef LOG_AUTH
75 # define LOG_AUTH 0
76 #endif
77
78 #define MULTI_PTRS_ARE_ALIASES 1        /* XXX - experimental */
79
80 #if defined(BSD) && (BSD >= 199103)
81 # include <string.h>
82 #else
83 # include "../conf/portability.h"
84 #endif
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
87 #endif
88
89 #define MAXALIASES      35
90 #define MAXADDRS        35
91
92 static const char AskedForGot[] =
93                           "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
94
95 static char *h_addr_ptrs[MAXADDRS + 1];
96 static struct hostent *gethostbyname_ipv4 __P((const char *));
97
98 static struct hostent host;
99 static char *host_aliases[MAXALIASES];
100 static char hostbuf[8*1024];
101 static struct in_addr host_addr;
102 static FILE *hostf = NULL;
103 static int stayopen = 0;
104
105 #ifdef RESOLVSORT
106 static void addrsort __P((char **, int));
107 #endif
108
109 #if PACKETSZ > 1024
110 #define MAXPACKET       PACKETSZ
111 #else
112 #define MAXPACKET       1024
113 #endif
114
115 typedef union {
116     HEADER hdr;
117     u_char buf[MAXPACKET];
118 } querybuf;
119
120 typedef union {
121     int32_t al;
122     char ac;
123 } align;
124
125 extern int h_errno;
126
127 #ifdef DEBUG
128 static void
129 dprintf(msg, num)
130         char *msg;
131         int num;
132 {
133         if (_res.options & RES_DEBUG) {
134                 int save = errno;
135
136                 printf(msg, num);
137                 errno = save;
138         }
139 }
140 #else
141 # define dprintf(msg, num) /*nada*/
142 #endif
143
144 static struct hostent *
145 getanswer(answer, anslen, qname, qclass, qtype)
146         const querybuf *answer;
147         int anslen;
148         const char *qname;
149         int qclass, qtype;
150 {
151         register const HEADER *hp;
152         register const u_char *cp;
153         register int n;
154         const u_char *eom;
155         char *bp, **ap, **hap;
156         int type, class, buflen, ancount, qdcount;
157         int haveanswer, had_error;
158         int toobig = 0;
159         char tbuf[MAXDNAME+1];
160         const char *tname;
161         int (*name_ok) __P((const char *));
162
163         tname = qname;
164         host.h_name = NULL;
165         eom = answer->buf + anslen;
166         switch (qtype) {
167         case T_A:
168                 name_ok = res_hnok;
169                 break;
170         case T_PTR:
171                 name_ok = dn_isvalid;
172                 break;
173         default:
174                 abort();
175         }
176         /*
177          * find first satisfactory answer
178          */
179         hp = &answer->hdr;
180         ancount = ntohs(hp->ancount);
181         qdcount = ntohs(hp->qdcount);
182         bp = hostbuf;
183         buflen = sizeof hostbuf;
184         cp = answer->buf + HFIXEDSZ;
185         if (qdcount != 1) {
186                 h_errno = NO_RECOVERY;
187                 return (NULL);
188         }
189         n = dn_expand(answer->buf, eom, cp, bp, buflen);
190         if ((n < 0) || !(*name_ok)(bp)) {
191                 h_errno = NO_RECOVERY;
192                 return (NULL);
193         }
194         cp += n + QFIXEDSZ;
195         if (qtype == T_A) {
196                 /* res_send() has already verified that the query name is the
197                  * same as the one we sent; this just gets the expanded name
198                  * (i.e., with the succeeding search-domain tacked on).
199                  */
200                 n = strlen(bp) + 1;             /* for the \0 */
201                 host.h_name = bp;
202                 bp += n;
203                 buflen -= n;
204                 /* The qname can be abbreviated, but h_name is now absolute. */
205                 qname = host.h_name;
206         }
207         ap = host_aliases;
208         *ap = NULL;
209         host.h_aliases = host_aliases;
210         hap = h_addr_ptrs;
211         *hap = NULL;
212 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
213         host.h_addr_list = h_addr_ptrs;
214 #endif
215         haveanswer = 0;
216         had_error = 0;
217         while (ancount-- > 0 && cp < eom && !had_error) {
218                 n = dn_expand(answer->buf, eom, cp, bp, buflen);
219                 if ((n < 0) || !(*name_ok)(bp)) {
220                         had_error++;
221                         continue;
222                 }
223                 cp += n;                        /* name */
224                 type = _getshort(cp);
225                 cp += INT16SZ;                  /* type */
226                 class = _getshort(cp);
227                 cp += INT16SZ + INT32SZ;        /* class, TTL */
228                 n = _getshort(cp);
229                 cp += INT16SZ;                  /* len */
230                 if (class != qclass) {
231                         /* XXX - debug? syslog? */
232                         cp += n;
233                         continue;               /* XXX - had_error++ ? */
234                 }
235                 if (qtype == T_A && type == T_CNAME) {
236                         if (ap >= &host_aliases[MAXALIASES-1])
237                                 continue;
238                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
239                         if ((n < 0) || !(*name_ok)(tbuf)) {
240                                 had_error++;
241                                 continue;
242                         }
243                         cp += n;
244                         if (host.h_name && strcasecmp(host.h_name, bp) != 0) {
245                                 syslog(LOG_NOTICE|LOG_AUTH,
246                 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
247                                        host.h_name, bp);
248                                 continue;       /* XXX - had_error++ ? */
249                         }
250                         /* Store alias. */
251                         *ap++ = bp;
252                         n = strlen(bp) + 1;     /* for the \0 */
253                         bp += n;
254                         buflen -= n;
255                         /* Get canonical name. */
256                         n = strlen(tbuf) + 1;   /* for the \0 */
257                         if (n > buflen) {
258                                 had_error++;
259                                 continue;
260                         }
261                         strcpy(bp, tbuf);
262                         host.h_name = bp;
263                         bp += n;
264                         buflen -= n;
265                         continue;
266                 }
267                 if (qtype == T_PTR && type == T_CNAME) {
268                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
269                         if ((n < 0) || !res_hnok(tbuf)) {
270                                 had_error++;
271                                 continue;
272                         }
273                         cp += n;
274                         /* Get canonical name. */
275                         n = strlen(tbuf) + 1;   /* for the \0 */
276                         if (n > buflen) {
277                                 had_error++;
278                                 continue;
279                         }
280                         strcpy(bp, tbuf);
281                         tname = bp;
282                         bp += n;
283                         buflen -= n;
284                         continue;
285                 }
286                 if (type != qtype) {
287                         syslog(LOG_NOTICE|LOG_AUTH,
288                "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
289                                qname, p_class(qclass), p_type(qtype),
290                                p_type(type));
291                         cp += n;
292                         continue;               /* XXX - had_error++ ? */
293                 }
294                 switch (type) {
295                 case T_PTR:
296                         if (strcasecmp(tname, bp) != 0) {
297                                 syslog(LOG_NOTICE|LOG_AUTH,
298                                        AskedForGot, qname, bp);
299                                 cp += n;
300                                 continue;       /* XXX - had_error++ ? */
301                         }
302                         n = dn_expand(answer->buf, eom, cp, bp, buflen);
303                         if ((n < 0) || !res_hnok(bp)) {
304                                 had_error++;
305                                 break;
306                         }
307 #if MULTI_PTRS_ARE_ALIASES
308                         cp += n;
309                         if (!haveanswer)
310                                 host.h_name = bp;
311                         else if (ap < &host_aliases[MAXALIASES-1])
312                                 *ap++ = bp;
313                         else
314                                 n = -1;
315                         if (n != -1) {
316                                 n = strlen(bp) + 1;     /* for the \0 */
317                                 bp += n;
318                                 buflen -= n;
319                         }
320                         break;
321 #else
322                         host.h_name = bp;
323                         h_errno = NETDB_SUCCESS;
324                         return (&host);
325 #endif
326                 case T_A:
327                         if (strcasecmp(host.h_name, bp) != 0) {
328                                 syslog(LOG_NOTICE|LOG_AUTH,
329                                        AskedForGot, host.h_name, bp);
330                                 cp += n;
331                                 continue;       /* XXX - had_error++ ? */
332                         }
333                         if (haveanswer) {
334                                 if (n != host.h_length) {
335                                         cp += n;
336                                         continue;
337                                 }
338                         } else {
339                                 register int nn;
340
341                                 host.h_length = n;
342                                 host.h_addrtype = (class == C_IN)
343                                                   ? AF_INET
344                                                   : AF_UNSPEC;
345                                 host.h_name = bp;
346                                 nn = strlen(bp) + 1;    /* for the \0 */
347                                 bp += nn;
348                                 buflen -= nn;
349                         }
350
351                         bp += sizeof(align) - ((u_long)bp % sizeof(align));
352
353                         if (bp + n >= &hostbuf[sizeof hostbuf]) {
354                                 dprintf("size (%d) too big\n", n);
355                                 had_error++;
356                                 continue;
357                         }
358                         if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
359                                 if (!toobig++)
360                                         dprintf("Too many addresses (%d)\n",
361                                                 MAXADDRS);
362                                 cp += n;
363                                 continue;
364                         }
365                         bcopy(cp, *hap++ = bp, n);
366                         bp += n;
367                         cp += n;
368                         break;
369                 default:
370                         dprintf("Impossible condition (type=%d)\n", type);
371                         h_errno = NO_RECOVERY;
372                         return (NULL);
373                 } /*switch*/
374                 if (!had_error)
375                         haveanswer++;
376         } /*while*/
377         if (haveanswer) {
378                 *ap = NULL;
379                 *hap = NULL;
380 # if defined(RESOLVSORT)
381                 /*
382                  * Note: we sort even if host can take only one address
383                  * in its return structures - should give it the "best"
384                  * address in that case, not some random one
385                  */
386                 if (_res.nsort && haveanswer > 1 &&
387                     qclass == C_IN && qtype == T_A)
388                         addrsort(h_addr_ptrs, haveanswer);
389 # endif /*RESOLVSORT*/
390 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
391                 /* nothing */
392 #else
393                 host.h_addr = h_addr_ptrs[0];
394 #endif /*BSD*/
395                 if (!host.h_name) {
396                         n = strlen(qname) + 1;  /* for the \0 */
397                         strcpy(bp, qname);
398                         host.h_name = bp;
399                 }
400                 h_errno = NETDB_SUCCESS;
401                 return (&host);
402         } else {
403                 h_errno = TRY_AGAIN;
404                 return (NULL);
405         }
406 }
407
408 struct hostent *
409 gethostbyname(name)
410         const char *name;
411 {
412   /* Moved #if line to here because declararing HP would lead to a
413      warining.  --drepper@gnu  */
414 #if defined(AF_INET6) && defined(RES_TRY_INET6)
415         struct hostent *hp;
416
417 /* #if defined(AF_INET6) && defined(RES_TRY_INET6) */
418         if (_res.options & RES_TRY_INET6) {
419                 hp = gethostbyname2(name, AF_INET6);
420                 if (hp)
421                         return (hp);
422         }
423 #endif
424         return (gethostbyname2(name, AF_INET));
425 }
426
427 struct hostent *
428 gethostbyname2(name, af)
429         const char *name;
430         int af;
431 {
432         switch (af) {
433         case AF_INET:
434                 return (gethostbyname_ipv4(name));
435         }
436         errno = EAFNOSUPPORT;
437         h_errno = NETDB_INTERNAL;
438         return (NULL);
439 }
440
441 static struct hostent *
442 gethostbyname_ipv4(name)
443         const char *name;
444 {
445         querybuf buf;
446         register const char *cp;
447         int n;
448         extern struct hostent *_gethtbyname();
449
450         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
451                 h_errno = NETDB_INTERNAL;
452                 return (NULL);
453         }
454
455         /*
456          * if there aren't any dots, it could be a user-level alias.
457          * this is also done in res_query() since we are not the only
458          * function that looks up host names.
459          */
460         if (!strchr(name, '.') && (cp = __hostalias(name)))
461                 name = cp;
462
463         /*
464          * disallow names consisting only of digits/dots, unless
465          * they end in a dot.
466          */
467         if (isdigit(name[0]))
468                 for (cp = name;; ++cp) {
469                         if (!*cp) {
470                                 if (*--cp == '.')
471                                         break;
472                                 /*
473                                  * All-numeric, no dot at the end.
474                                  * Fake up a hostent as if we'd actually
475                                  * done a lookup.
476                                  */
477                                 if (!inet_aton(name, &host_addr)) {
478                                         h_errno = HOST_NOT_FOUND;
479                                         return (NULL);
480                                 }
481                                 strncpy(hostbuf, name, MAXDNAME);
482                                 hostbuf[MAXDNAME] = '\0';
483                                 host.h_name = hostbuf;
484                                 host.h_aliases = host_aliases;
485                                 host_aliases[0] = NULL;
486                                 host.h_addrtype = AF_INET;
487                                 host.h_length = INT32SZ;
488                                 h_addr_ptrs[0] = (char *)&host_addr;
489                                 h_addr_ptrs[1] = NULL;
490 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
491                                 host.h_addr_list = h_addr_ptrs;
492 #else
493                                 host.h_addr = h_addr_ptrs[0];
494 #endif
495                                 h_errno = NETDB_SUCCESS;
496                                 return (&host);
497                         }
498                         if (!isdigit(*cp) && *cp != '.') 
499                                 break;
500                 }
501
502         if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
503                 dprintf("res_search failed (%d)\n", n);
504                 if (errno == ECONNREFUSED)
505                         return (_gethtbyname(name));
506                 return (NULL);
507         }
508         return (getanswer(&buf, n, name, C_IN, T_A));
509 }
510
511 struct hostent *
512 gethostbyaddr(addr, len, type)
513         const char *addr;
514         int len, type;
515 {
516         int n;
517         querybuf buf;
518         register struct hostent *hp;
519         char qbuf[MAXDNAME+1];
520 #ifdef SUNSECURITY
521         register struct hostent *rhp;
522         char **haddr;
523         u_long old_options;
524         char hname2[MAXDNAME+1];
525 #endif /*SUNSECURITY*/
526         extern struct hostent *_gethtbyaddr();
527         
528         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
529                 h_errno = NETDB_INTERNAL;
530                 return (NULL);
531         }
532         if (type != AF_INET || len != INADDRSZ) {
533                 errno = EAFNOSUPPORT;
534                 h_errno = NETDB_INTERNAL;
535                 return (NULL);
536         }
537         (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
538                 ((unsigned)addr[3] & 0xff),
539                 ((unsigned)addr[2] & 0xff),
540                 ((unsigned)addr[1] & 0xff),
541                 ((unsigned)addr[0] & 0xff));
542         n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
543         if (n < 0) {
544                 dprintf("res_query failed (%d)\n", n);
545                 if (errno == ECONNREFUSED)
546                         return (_gethtbyaddr(addr, len, type));
547                 return (NULL);
548         }
549         if (!(hp = getanswer(&buf, n, qbuf, C_IN, T_PTR)))
550                 return (NULL);  /* h_errno was set by getanswer() */
551 #ifdef SUNSECURITY
552         /*
553          * turn off search as the name should be absolute,
554          * 'localhost' should be matched by defnames
555          */
556         strncpy(hname2, hp->h_name, MAXDNAME);
557         hname2[MAXDNAME] = '\0';
558         old_options = _res.options;
559         _res.options &= ~RES_DNSRCH;
560         _res.options |= RES_DEFNAMES;
561         if (!(rhp = gethostbyname(hname2))) {
562                 syslog(LOG_NOTICE|LOG_AUTH,
563                        "gethostbyaddr: No A record for %s (verifying [%s])",
564                        hname2, inet_ntoa(*((struct in_addr *)addr)));
565                 _res.options = old_options;
566                 h_errno = HOST_NOT_FOUND;
567                 return (NULL);
568         }
569         _res.options = old_options;
570         for (haddr = rhp->h_addr_list; *haddr; haddr++)
571                 if (!memcmp(*haddr, addr, INADDRSZ))
572                         break;
573         if (!*haddr) {
574                 syslog(LOG_NOTICE|LOG_AUTH,
575                        "gethostbyaddr: A record of %s != PTR record [%s]",
576                        hname2, inet_ntoa(*((struct in_addr *)addr)));
577                 h_errno = HOST_NOT_FOUND;
578                 return (NULL);
579         }
580 #endif /*SUNSECURITY*/
581         hp->h_addrtype = type;
582         hp->h_length = len;
583         h_addr_ptrs[0] = (char *)&host_addr;
584         h_addr_ptrs[1] = NULL;
585         host_addr = *(struct in_addr *)addr;
586         h_errno = NETDB_SUCCESS;
587         return (hp);
588 }
589
590 void
591 _sethtent(f)
592         int f;
593 {
594         if (!hostf)
595                 hostf = fopen(_PATH_HOSTS, "r" );
596         else
597                 rewind(hostf);
598         stayopen = f;
599 }
600
601 void
602 _endhtent()
603 {
604         if (hostf && !stayopen) {
605                 (void) fclose(hostf);
606                 hostf = NULL;
607         }
608 }
609
610 struct hostent *
611 _gethtent()
612 {
613         char *p;
614         register char *cp, **q;
615
616         if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
617                 h_errno = NETDB_INTERNAL;
618                 return (NULL);
619         }
620 again:
621         if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
622                 h_errno = HOST_NOT_FOUND;
623                 return (NULL);
624         }
625         if (*p == '#')
626                 goto again;
627         if (!(cp = strpbrk(p, "#\n")))
628                 goto again;
629         *cp = '\0';
630         if (!(cp = strpbrk(p, " \t")))
631                 goto again;
632         *cp++ = '\0';
633         /* THIS STUFF IS INTERNET SPECIFIC */
634         if (!inet_aton(p, &host_addr))
635                 goto again;
636         h_addr_ptrs[0] = (char *)&host_addr;
637         h_addr_ptrs[1] = NULL;
638 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
639         host.h_addr_list = h_addr_ptrs;
640 #else
641         host.h_addr = h_addr_ptrs[0];
642 #endif
643         host.h_length = INT32SZ;
644         host.h_addrtype = AF_INET;
645         while (*cp == ' ' || *cp == '\t')
646                 cp++;
647         host.h_name = cp;
648         q = host.h_aliases = host_aliases;
649         if (cp = strpbrk(cp, " \t"))
650                 *cp++ = '\0';
651         while (cp && *cp) {
652                 if (*cp == ' ' || *cp == '\t') {
653                         cp++;
654                         continue;
655                 }
656                 if (q < &host_aliases[MAXALIASES - 1])
657                         *q++ = cp;
658                 if (cp = strpbrk(cp, " \t"))
659                         *cp++ = '\0';
660         }
661         *q = NULL;
662         h_errno = NETDB_SUCCESS;
663         return (&host);
664 }
665
666 struct hostent *
667 _gethtbyname(name)
668         char *name;
669 {
670         register struct hostent *p;
671         register char **cp;
672         
673         _sethtent(0);
674         while (p = _gethtent()) {
675                 if (strcasecmp(p->h_name, name) == 0)
676                         break;
677                 for (cp = p->h_aliases; *cp != 0; cp++)
678                         if (strcasecmp(*cp, name) == 0)
679                                 goto found;
680         }
681 found:
682         _endhtent();
683         return (p);
684 }
685
686 struct hostent *
687 _gethtbyaddr(addr, len, type)
688         const char *addr;
689         int len, type;
690 {
691         register struct hostent *p;
692
693         _sethtent(0);
694         while (p = _gethtent())
695                 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
696                         break;
697         _endhtent();
698         return (p);
699 }
700
701 #ifdef RESOLVSORT
702 static void
703 addrsort(ap, num)
704         char **ap;
705         int num;
706 {
707         int i, j;
708         char **p;
709         short aval[MAXADDRS];
710         int needsort = 0;
711
712         p = ap;
713         for (i = 0; i < num; i++, p++) {
714             for (j = 0 ; (unsigned)j < _res.nsort; j++)
715                 if (_res.sort_list[j].addr.s_addr == 
716                     (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
717                         break;
718             aval[i] = j;
719             if (needsort == 0 && i > 0 && j < aval[i-1])
720                 needsort = i;
721         }
722         if (!needsort)
723             return;
724
725         while (needsort < num) {
726             for (j = needsort - 1; j >= 0; j--) {
727                 if (aval[j] > aval[j+1]) {
728                     char *hp;
729
730                     i = aval[j];
731                     aval[j] = aval[j+1];
732                     aval[j+1] = i;
733
734                     hp = ap[j];
735                     ap[j] = ap[j+1];
736                     ap[j+1] = hp;
737
738                 } else
739                     break;
740             }
741             needsort++;
742         }
743 }
744 #endif
745
746 #if defined(BSD43_BSD43_NFS) || defined(sun)
747 /* some libc's out there are bound internally to these names (UMIPS) */
748 void
749 ht_sethostent(stayopen)
750         int stayopen;
751 {
752         _sethtent(stayopen);
753 }
754
755 void
756 ht_endhostent()
757 {
758         _endhtent();
759 }
760
761 struct hostent *
762 ht_gethostbyname(name)
763         char *name;
764 {
765         return (_gethtbyname(name));
766 }
767
768 struct hostent *
769 ht_gethostbyaddr(addr, len, type)
770         const char *addr;
771         int len, type;
772 {
773         return (_gethtbyaddr(addr, len, type));
774 }
775
776 struct hostent *
777 gethostent()
778 {
779         return (_gethtent());
780 }
781
782 void
783 dns_service()
784 {
785         return;
786 }
787
788 #undef dn_skipname
789 dn_skipname(comp_dn, eom)
790         const u_char *comp_dn, *eom;
791 {
792         return (__dn_skipname(comp_dn, eom));
793 }
794 #endif /*old-style libc with yp junk in it*/