Thu May 9 01:24:00 1996 Ulrich Drepper <drepper@cygnus.com>
[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
162         tname = qname;
163         host.h_name = NULL;
164         eom = answer->buf + anslen;
165         /*
166          * find first satisfactory answer
167          */
168         hp = &answer->hdr;
169         ancount = ntohs(hp->ancount);
170         qdcount = ntohs(hp->qdcount);
171         bp = hostbuf;
172         buflen = sizeof hostbuf;
173         cp = answer->buf + HFIXEDSZ;
174         if (qdcount != 1) {
175                 h_errno = NO_RECOVERY;
176                 return (NULL);
177         }
178         if ((n = dn_expand(answer->buf, eom, cp, bp, buflen)) < 0) {
179                 h_errno = NO_RECOVERY;
180                 return (NULL);
181         }
182         cp += n + QFIXEDSZ;
183         if (qtype == T_A) {
184                 /* res_send() has already verified that the query name is the
185                  * same as the one we sent; this just gets the expanded name
186                  * (i.e., with the succeeding search-domain tacked on).
187                  */
188                 n = strlen(bp) + 1;             /* for the \0 */
189                 host.h_name = bp;
190                 bp += n;
191                 buflen -= n;
192                 /* The qname can be abbreviated, but h_name is now absolute. */
193                 qname = host.h_name;
194         }
195         ap = host_aliases;
196         *ap = NULL;
197         host.h_aliases = host_aliases;
198         hap = h_addr_ptrs;
199         *hap = NULL;
200 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
201         host.h_addr_list = h_addr_ptrs;
202 #endif
203         haveanswer = 0;
204         had_error = 0;
205         while (ancount-- > 0 && cp < eom && !had_error) {
206                 n = dn_expand(answer->buf, eom, cp, bp, buflen);
207                 if (n < 0) {
208                         had_error++;
209                         continue;
210                 }
211                 cp += n;                        /* name */
212                 type = _getshort(cp);
213                 cp += INT16SZ;                  /* type */
214                 class = _getshort(cp);
215                 cp += INT16SZ + INT32SZ;        /* class, TTL */
216                 n = _getshort(cp);
217                 cp += INT16SZ;                  /* len */
218                 if (class != qclass) {
219                         /* XXX - debug? syslog? */
220                         cp += n;
221                         continue;               /* XXX - had_error++ ? */
222                 }
223                 if (qtype == T_A && type == T_CNAME) {
224                         if (ap >= &host_aliases[MAXALIASES-1])
225                                 continue;
226                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
227                         if (n < 0) {
228                                 had_error++;
229                                 continue;
230                         }
231                         cp += n;
232                         if (host.h_name && strcasecmp(host.h_name, bp) != 0) {
233                                 syslog(LOG_NOTICE|LOG_AUTH,
234                 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
235                                        host.h_name, bp);
236                                 continue;       /* XXX - had_error++ ? */
237                         }
238                         /* Store alias. */
239                         *ap++ = bp;
240                         n = strlen(bp) + 1;     /* for the \0 */
241                         bp += n;
242                         buflen -= n;
243                         /* Get canonical name. */
244                         n = strlen(tbuf) + 1;   /* for the \0 */
245                         if (n > buflen) {
246                                 had_error++;
247                                 continue;
248                         }
249                         strcpy(bp, tbuf);
250                         host.h_name = bp;
251                         bp += n;
252                         buflen -= n;
253                         continue;
254                 }
255                 if (qtype == T_PTR && type == T_CNAME) {
256                         n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
257                         if (n < 0) {
258                                 had_error++;
259                                 continue;
260                         }
261                         cp += n;
262                         /* Get canonical name. */
263                         n = strlen(tbuf) + 1;   /* for the \0 */
264                         if (n > buflen) {
265                                 had_error++;
266                                 continue;
267                         }
268                         strcpy(bp, tbuf);
269                         tname = bp;
270                         bp += n;
271                         buflen -= n;
272                         continue;
273                 }
274                 if (type != qtype) {
275                         syslog(LOG_NOTICE|LOG_AUTH,
276                "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
277                                qname, p_class(qclass), p_type(qtype),
278                                p_type(type));
279                         cp += n;
280                         continue;               /* XXX - had_error++ ? */
281                 }
282                 switch (type) {
283                 case T_PTR:
284                         if (strcasecmp(tname, bp) != 0) {
285                                 syslog(LOG_NOTICE|LOG_AUTH,
286                                        AskedForGot, qname, bp);
287                                 cp += n;
288                                 continue;       /* XXX - had_error++ ? */
289                         }
290                         n = dn_expand(answer->buf, eom, cp, bp, buflen);
291                         if (n < 0) {
292                                 had_error++;
293                                 break;
294                         }
295 #if MULTI_PTRS_ARE_ALIASES
296                         cp += n;
297                         if (!haveanswer)
298                                 host.h_name = bp;
299                         else if (ap < &host_aliases[MAXALIASES-1])
300                                 *ap++ = bp;
301                         else
302                                 n = -1;
303                         if (n != -1) {
304                                 n = strlen(bp) + 1;     /* for the \0 */
305                                 bp += n;
306                                 buflen -= n;
307                         }
308                         break;
309 #else
310                         host.h_name = bp;
311                         h_errno = NETDB_SUCCESS;
312                         return (&host);
313 #endif
314                 case T_A:
315                         if (strcasecmp(host.h_name, bp) != 0) {
316                                 syslog(LOG_NOTICE|LOG_AUTH,
317                                        AskedForGot, host.h_name, bp);
318                                 cp += n;
319                                 continue;       /* XXX - had_error++ ? */
320                         }
321                         if (haveanswer) {
322                                 if (n != host.h_length) {
323                                         cp += n;
324                                         continue;
325                                 }
326                         } else {
327                                 register int nn;
328
329                                 host.h_length = n;
330                                 host.h_addrtype = (class == C_IN)
331                                                   ? AF_INET
332                                                   : AF_UNSPEC;
333                                 host.h_name = bp;
334                                 nn = strlen(bp) + 1;    /* for the \0 */
335                                 bp += nn;
336                                 buflen -= nn;
337                         }
338
339                         bp += sizeof(align) - ((u_long)bp % sizeof(align));
340
341                         if (bp + n >= &hostbuf[sizeof hostbuf]) {
342                                 dprintf("size (%d) too big\n", n);
343                                 had_error++;
344                                 continue;
345                         }
346                         if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
347                                 if (!toobig++)
348                                         dprintf("Too many addresses (%d)\n",
349                                                 MAXADDRS);
350                                 cp += n;
351                                 continue;
352                         }
353                         bcopy(cp, *hap++ = bp, n);
354                         bp += n;
355                         cp += n;
356                         break;
357                 default:
358                         dprintf("Impossible condition (type=%d)\n", type);
359                         h_errno = NO_RECOVERY;
360                         return (NULL);
361                 } /*switch*/
362                 if (!had_error)
363                         haveanswer++;
364         } /*while*/
365         if (haveanswer) {
366                 *ap = NULL;
367                 *hap = NULL;
368 # if defined(RESOLVSORT)
369                 /*
370                  * Note: we sort even if host can take only one address
371                  * in its return structures - should give it the "best"
372                  * address in that case, not some random one
373                  */
374                 if (_res.nsort && haveanswer > 1 &&
375                     qclass == C_IN && qtype == T_A)
376                         addrsort(h_addr_ptrs, haveanswer);
377 # endif /*RESOLVSORT*/
378 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
379                 /* nothing */
380 #else
381                 host.h_addr = h_addr_ptrs[0];
382 #endif /*BSD*/
383                 if (!host.h_name) {
384                         n = strlen(qname) + 1;  /* for the \0 */
385                         strcpy(bp, qname);
386                         host.h_name = bp;
387                 }
388                 h_errno = NETDB_SUCCESS;
389                 return (&host);
390         } else {
391                 h_errno = TRY_AGAIN;
392                 return (NULL);
393         }
394 }
395
396 struct hostent *
397 gethostbyname(name)
398         const char *name;
399 {
400   /* Moved #if line to here because declararing HP would lead to a
401      warining.  --drepper@gnu  */
402 #if defined(AF_INET6) && defined(RES_TRY_INET6)
403         struct hostent *hp;
404
405 /* #if defined(AF_INET6) && defined(RES_TRY_INET6) */
406         if (_res.options & RES_TRY_INET6) {
407                 hp = gethostbyname2(name, AF_INET6);
408                 if (hp)
409                         return (hp);
410         }
411 #endif
412         return (gethostbyname2(name, AF_INET));
413 }
414
415 struct hostent *
416 gethostbyname2(name, af)
417         const char *name;
418         int af;
419 {
420         switch (af) {
421         case AF_INET:
422                 return (gethostbyname_ipv4(name));
423         }
424         errno = EAFNOSUPPORT;
425         h_errno = NETDB_INTERNAL;
426         return (NULL);
427 }
428
429 static struct hostent *
430 gethostbyname_ipv4(name)
431         const char *name;
432 {
433         querybuf buf;
434         register const char *cp;
435         int n;
436         extern struct hostent *_gethtbyname();
437
438         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
439                 h_errno = NETDB_INTERNAL;
440                 return (NULL);
441         }
442
443         /*
444          * if there aren't any dots, it could be a user-level alias.
445          * this is also done in res_query() since we are not the only
446          * function that looks up host names.
447          */
448         if (!strchr(name, '.') && (cp = __hostalias(name)))
449                 name = cp;
450
451         /*
452          * disallow names consisting only of digits/dots, unless
453          * they end in a dot.
454          */
455         if (isdigit(name[0]))
456                 for (cp = name;; ++cp) {
457                         if (!*cp) {
458                                 if (*--cp == '.')
459                                         break;
460                                 /*
461                                  * All-numeric, no dot at the end.
462                                  * Fake up a hostent as if we'd actually
463                                  * done a lookup.
464                                  */
465                                 if (!inet_aton(name, &host_addr)) {
466                                         h_errno = HOST_NOT_FOUND;
467                                         return (NULL);
468                                 }
469                                 strncpy(hostbuf, name, MAXDNAME);
470                                 hostbuf[MAXDNAME] = '\0';
471                                 host.h_name = hostbuf;
472                                 host.h_aliases = host_aliases;
473                                 host_aliases[0] = NULL;
474                                 host.h_addrtype = AF_INET;
475                                 host.h_length = INT32SZ;
476                                 h_addr_ptrs[0] = (char *)&host_addr;
477                                 h_addr_ptrs[1] = NULL;
478 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
479                                 host.h_addr_list = h_addr_ptrs;
480 #else
481                                 host.h_addr = h_addr_ptrs[0];
482 #endif
483                                 h_errno = NETDB_SUCCESS;
484                                 return (&host);
485                         }
486                         if (!isdigit(*cp) && *cp != '.') 
487                                 break;
488                 }
489
490         if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
491                 dprintf("res_search failed (%d)\n", n);
492                 if (errno == ECONNREFUSED)
493                         return (_gethtbyname(name));
494                 return (NULL);
495         }
496         return (getanswer(&buf, n, name, C_IN, T_A));
497 }
498
499 struct hostent *
500 gethostbyaddr(addr, len, type)
501         const char *addr;
502         int len, type;
503 {
504         int n;
505         querybuf buf;
506         register struct hostent *hp;
507         char qbuf[MAXDNAME+1];
508 #ifdef SUNSECURITY
509         register struct hostent *rhp;
510         char **haddr;
511         u_long old_options;
512         char hname2[MAXDNAME+1];
513 #endif /*SUNSECURITY*/
514         extern struct hostent *_gethtbyaddr();
515         
516         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
517                 h_errno = NETDB_INTERNAL;
518                 return (NULL);
519         }
520         if (type != AF_INET) {
521                 errno = EAFNOSUPPORT;
522                 h_errno = NETDB_INTERNAL;
523                 return (NULL);
524         }
525         (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
526                 ((unsigned)addr[3] & 0xff),
527                 ((unsigned)addr[2] & 0xff),
528                 ((unsigned)addr[1] & 0xff),
529                 ((unsigned)addr[0] & 0xff));
530         n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
531         if (n < 0) {
532                 dprintf("res_query failed (%d)\n", n);
533                 if (errno == ECONNREFUSED)
534                         return (_gethtbyaddr(addr, len, type));
535                 return (NULL);
536         }
537         if (!(hp = getanswer(&buf, n, qbuf, C_IN, T_PTR)))
538                 return (NULL);  /* h_errno was set by getanswer() */
539 #ifdef SUNSECURITY
540         /*
541          * turn off search as the name should be absolute,
542          * 'localhost' should be matched by defnames
543          */
544         strncpy(hname2, hp->h_name, MAXDNAME);
545         hname2[MAXDNAME] = '\0';
546         old_options = _res.options;
547         _res.options &= ~RES_DNSRCH;
548         _res.options |= RES_DEFNAMES;
549         if (!(rhp = gethostbyname(hname2))) {
550                 syslog(LOG_NOTICE|LOG_AUTH,
551                        "gethostbyaddr: No A record for %s (verifying [%s])",
552                        hname2, inet_ntoa(*((struct in_addr *)addr)));
553                 _res.options = old_options;
554                 h_errno = HOST_NOT_FOUND;
555                 return (NULL);
556         }
557         _res.options = old_options;
558         for (haddr = rhp->h_addr_list; *haddr; haddr++)
559                 if (!memcmp(*haddr, addr, INADDRSZ))
560                         break;
561         if (!*haddr) {
562                 syslog(LOG_NOTICE|LOG_AUTH,
563                        "gethostbyaddr: A record of %s != PTR record [%s]",
564                        hname2, inet_ntoa(*((struct in_addr *)addr)));
565                 h_errno = HOST_NOT_FOUND;
566                 return (NULL);
567         }
568 #endif /*SUNSECURITY*/
569         hp->h_addrtype = type;
570         hp->h_length = len;
571         h_addr_ptrs[0] = (char *)&host_addr;
572         h_addr_ptrs[1] = NULL;
573         host_addr = *(struct in_addr *)addr;
574         h_errno = NETDB_SUCCESS;
575         return (hp);
576 }
577
578 void
579 _sethtent(f)
580         int f;
581 {
582         if (!hostf)
583                 hostf = fopen(_PATH_HOSTS, "r" );
584         else
585                 rewind(hostf);
586         stayopen = f;
587 }
588
589 void
590 _endhtent()
591 {
592         if (hostf && !stayopen) {
593                 (void) fclose(hostf);
594                 hostf = NULL;
595         }
596 }
597
598 struct hostent *
599 _gethtent()
600 {
601         char *p;
602         register char *cp, **q;
603
604         if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
605                 h_errno = NETDB_INTERNAL;
606                 return (NULL);
607         }
608 again:
609         if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
610                 h_errno = HOST_NOT_FOUND;
611                 return (NULL);
612         }
613         if (*p == '#')
614                 goto again;
615         if (!(cp = strpbrk(p, "#\n")))
616                 goto again;
617         *cp = '\0';
618         if (!(cp = strpbrk(p, " \t")))
619                 goto again;
620         *cp++ = '\0';
621         /* THIS STUFF IS INTERNET SPECIFIC */
622         if (!inet_aton(p, &host_addr))
623                 goto again;
624         h_addr_ptrs[0] = (char *)&host_addr;
625         h_addr_ptrs[1] = NULL;
626 #if BSD >= 43 || defined(h_addr)        /* new-style hostent structure */
627         host.h_addr_list = h_addr_ptrs;
628 #else
629         host.h_addr = h_addr_ptrs[0];
630 #endif
631         host.h_length = INT32SZ;
632         host.h_addrtype = AF_INET;
633         while (*cp == ' ' || *cp == '\t')
634                 cp++;
635         host.h_name = cp;
636         q = host.h_aliases = host_aliases;
637         if (cp = strpbrk(cp, " \t"))
638                 *cp++ = '\0';
639         while (cp && *cp) {
640                 if (*cp == ' ' || *cp == '\t') {
641                         cp++;
642                         continue;
643                 }
644                 if (q < &host_aliases[MAXALIASES - 1])
645                         *q++ = cp;
646                 if (cp = strpbrk(cp, " \t"))
647                         *cp++ = '\0';
648         }
649         *q = NULL;
650         h_errno = NETDB_SUCCESS;
651         return (&host);
652 }
653
654 struct hostent *
655 _gethtbyname(name)
656         char *name;
657 {
658         register struct hostent *p;
659         register char **cp;
660         
661         _sethtent(0);
662         while (p = _gethtent()) {
663                 if (strcasecmp(p->h_name, name) == 0)
664                         break;
665                 for (cp = p->h_aliases; *cp != 0; cp++)
666                         if (strcasecmp(*cp, name) == 0)
667                                 goto found;
668         }
669 found:
670         _endhtent();
671         return (p);
672 }
673
674 struct hostent *
675 _gethtbyaddr(addr, len, type)
676         const char *addr;
677         int len, type;
678 {
679         register struct hostent *p;
680
681         _sethtent(0);
682         while (p = _gethtent())
683                 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
684                         break;
685         _endhtent();
686         return (p);
687 }
688
689 #ifdef RESOLVSORT
690 static void
691 addrsort(ap, num)
692         char **ap;
693         int num;
694 {
695         int i, j;
696         char **p;
697         short aval[MAXADDRS];
698         int needsort = 0;
699
700         p = ap;
701         for (i = 0; i < num; i++, p++) {
702             for (j = 0 ; (unsigned)j < _res.nsort; j++)
703                 if (_res.sort_list[j].addr.s_addr == 
704                     (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
705                         break;
706             aval[i] = j;
707             if (needsort == 0 && i > 0 && j < aval[i-1])
708                 needsort = i;
709         }
710         if (!needsort)
711             return;
712
713         while (needsort < num) {
714             for (j = needsort - 1; j >= 0; j--) {
715                 if (aval[j] > aval[j+1]) {
716                     char *hp;
717
718                     i = aval[j];
719                     aval[j] = aval[j+1];
720                     aval[j+1] = i;
721
722                     hp = ap[j];
723                     ap[j] = ap[j+1];
724                     ap[j+1] = hp;
725
726                 } else
727                     break;
728             }
729             needsort++;
730         }
731 }
732 #endif
733
734 #if defined(BSD43_BSD43_NFS) || defined(sun)
735 /* some libc's out there are bound internally to these names (UMIPS) */
736 void
737 ht_sethostent(stayopen)
738         int stayopen;
739 {
740         _sethtent(stayopen);
741 }
742
743 void
744 ht_endhostent()
745 {
746         _endhtent();
747 }
748
749 struct hostent *
750 ht_gethostbyname(name)
751         char *name;
752 {
753         return (_gethtbyname(name));
754 }
755
756 struct hostent *
757 ht_gethostbyaddr(addr, len, type)
758         const char *addr;
759         int len, type;
760 {
761         return (_gethtbyaddr(addr, len, type));
762 }
763
764 struct hostent *
765 gethostent()
766 {
767         return (_gethtent());
768 }
769
770 void
771 dns_service()
772 {
773         return;
774 }
775
776 #undef dn_skipname
777 dn_skipname(comp_dn, eom)
778         const u_char *comp_dn, *eom;
779 {
780         return (__dn_skipname(comp_dn, eom));
781 }
782 #endif /*old-style libc with yp junk in it*/