oops
[unix-history] / usr / src / lib / libc / net / gethostnamadr.c
index b03dbe9..a945985 100644 (file)
@@ -1,12 +1,18 @@
 /*
 /*
- * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1985, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of California at Berkeley. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)gethostnamadr.c    6.28 (Berkeley) %G%";
-#endif LIBC_SCCS and not lint
+static char sccsid[] = "@(#)gethostnamadr.c    6.32 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #include <sys/socket.h>
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -20,7 +26,7 @@ static char sccsid[] = "@(#)gethostnamadr.c   6.28 (Berkeley) %G%";
 #include <resolv.h>
 
 #define        MAXALIASES      35
 #include <resolv.h>
 
 #define        MAXALIASES      35
-#define MAXADDRS       35
+#define        MAXADDRS        35
 
 static char *h_addr_ptrs[MAXADDRS + 1];
 
 
 static char *h_addr_ptrs[MAXADDRS + 1];
 
@@ -30,21 +36,20 @@ static char hostbuf[BUFSIZ+1];
 static struct in_addr host_addr;
 static char HOSTDB[] = "/etc/hosts";
 static FILE *hostf = NULL;
 static struct in_addr host_addr;
 static char HOSTDB[] = "/etc/hosts";
 static FILE *hostf = NULL;
-static char line[BUFSIZ+1];
 static char hostaddr[MAXADDRS];
 static char *host_addrs[2];
 static int stayopen = 0;
 static char *any();
 
 #if PACKETSZ > 1024
 static char hostaddr[MAXADDRS];
 static char *host_addrs[2];
 static int stayopen = 0;
 static char *any();
 
 #if PACKETSZ > 1024
-#define MAXPACKET      PACKETSZ
+#define        MAXPACKET       PACKETSZ
 #else
 #else
-#define MAXPACKET      1024
+#define        MAXPACKET       1024
 #endif
 
 typedef union {
 #endif
 
 typedef union {
-    HEADER qb1;
-    char qb2[MAXPACKET];
+    HEADER hdr;
+    u_char buf[MAXPACKET];
 } querybuf;
 
 static union {
 } querybuf;
 
 static union {
@@ -57,73 +62,33 @@ int h_errno;
 extern errno;
 
 static struct hostent *
 extern errno;
 
 static struct hostent *
-getanswer(msg, msglen, iquery)
-       char *msg;
-       int msglen, iquery;
+getanswer(answer, anslen, iquery)
+       querybuf *answer;
+       int anslen;
+       int iquery;
 {
        register HEADER *hp;
 {
        register HEADER *hp;
-       register char *cp;
+       register u_char *cp;
        register int n;
        register int n;
-       querybuf answer;
-       char *eom, *bp, **ap;
+       u_char *eom;
+       char *bp, **ap;
        int type, class, buflen, ancount, qdcount;
        int haveanswer, getclass = C_ANY;
        char **hap;
 
        int type, class, buflen, ancount, qdcount;
        int haveanswer, getclass = C_ANY;
        char **hap;
 
-       n = res_send(msg, msglen, (char *)&answer, sizeof(answer));
-       if (n < 0) {
-#ifdef DEBUG
-               int terrno;
-               terrno = errno;
-               if (_res.options & RES_DEBUG)
-                       printf("res_send failed\n");
-               errno = terrno;
-#endif
-               h_errno = TRY_AGAIN;
-               return ((struct hostent *) NULL);
-       }
-       eom = (char *)&answer + n;
+       eom = answer->buf + anslen;
        /*
         * find first satisfactory answer
         */
        /*
         * find first satisfactory answer
         */
-       hp = (HEADER *) &answer;
+       hp = &answer->hdr;
        ancount = ntohs(hp->ancount);
        qdcount = ntohs(hp->qdcount);
        ancount = ntohs(hp->ancount);
        qdcount = ntohs(hp->qdcount);
-       if (hp->rcode != NOERROR || ancount == 0) {
-#ifdef DEBUG
-               if (_res.options & RES_DEBUG)
-                       printf("rcode = %d, ancount=%d\n", hp->rcode, ancount);
-#endif
-               switch (hp->rcode) {
-                       case NXDOMAIN:
-                               /* Check if it's an authoritive answer */
-                               if (hp->aa)
-                                       h_errno = HOST_NOT_FOUND;
-                               else
-                                       h_errno = TRY_AGAIN;
-                               break;
-                       case SERVFAIL:
-                               h_errno = TRY_AGAIN;
-                               break;
-                       case NOERROR:
-                               if (hp->aa)
-                                       h_errno = NO_ADDRESS;
-                               else
-                                       h_errno = TRY_AGAIN;
-                               break;
-                       case FORMERR:
-                       case NOTIMP:
-                       case REFUSED:
-                               h_errno = NO_RECOVERY;
-               }
-               return ((struct hostent *) NULL);
-       }
        bp = hostbuf;
        buflen = sizeof(hostbuf);
        bp = hostbuf;
        buflen = sizeof(hostbuf);
-       cp = (char *)&answer + sizeof(HEADER);
+       cp = answer->buf + sizeof(HEADER);
        if (qdcount) {
                if (iquery) {
        if (qdcount) {
                if (iquery) {
-                       if ((n = dn_expand((char *)&answer, eom,
+                       if ((n = dn_expand((char *)answer->buf, eom,
                             cp, bp, buflen)) < 0) {
                                h_errno = NO_RECOVERY;
                                return ((struct hostent *) NULL);
                             cp, bp, buflen)) < 0) {
                                h_errno = NO_RECOVERY;
                                return ((struct hostent *) NULL);
@@ -147,12 +112,12 @@ getanswer(msg, msglen, iquery)
        ap = host_aliases;
        host.h_aliases = host_aliases;
        hap = h_addr_ptrs;
        ap = host_aliases;
        host.h_aliases = host_aliases;
        hap = h_addr_ptrs;
-#if BSD >= 43
+#if BSD >= 43 || defined(h_addr)       /* new-style hostent structure */
        host.h_addr_list = h_addr_ptrs;
 #endif
        haveanswer = 0;
        while (--ancount >= 0 && cp < eom) {
        host.h_addr_list = h_addr_ptrs;
 #endif
        haveanswer = 0;
        while (--ancount >= 0 && cp < eom) {
-               if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0)
+               if ((n = dn_expand((char *)answer->buf, eom, cp, bp, buflen)) < 0)
                        break;
                cp += n;
                type = _getshort(cp);
                        break;
                cp += n;
                type = _getshort(cp);
@@ -171,8 +136,8 @@ getanswer(msg, msglen, iquery)
                        buflen -= n;
                        continue;
                }
                        buflen -= n;
                        continue;
                }
-               if (type == T_PTR) {
-                       if ((n = dn_expand((char *)&answer, eom,
+               if (iquery && type == T_PTR) {
+                       if ((n = dn_expand((char *)answer->buf, eom,
                            cp, bp, buflen)) < 0) {
                                cp += n;
                                continue;
                            cp, bp, buflen)) < 0) {
                                cp += n;
                                continue;
@@ -181,7 +146,7 @@ getanswer(msg, msglen, iquery)
                        host.h_name = bp;
                        return(&host);
                }
                        host.h_name = bp;
                        return(&host);
                }
-               if (type != T_A)  {
+               if (iquery || type != T_A)  {
 #ifdef DEBUG
                        if (_res.options & RES_DEBUG)
                                printf("unexpected answer type %d, size %d\n",
 #ifdef DEBUG
                        if (_res.options & RES_DEBUG)
                                printf("unexpected answer type %d, size %d\n",
@@ -225,7 +190,7 @@ getanswer(msg, msglen, iquery)
        }
        if (haveanswer) {
                *ap = NULL;
        }
        if (haveanswer) {
                *ap = NULL;
-#if BSD >= 43
+#if BSD >= 43 || defined(h_addr)       /* new-style hostent structure */
                *hap = NULL;
 #else
                host.h_addr = h_addr_ptrs[0];
                *hap = NULL;
 #else
                host.h_addr = h_addr_ptrs[0];
@@ -241,14 +206,12 @@ struct hostent *
 gethostbyname(name)
        char *name;
 {
 gethostbyname(name)
        char *name;
 {
-       register char *cp, **domain;
+       querybuf buf;
+       register char *cp;
        int n;
        struct hostent *hp, *gethostdomain();
        int n;
        struct hostent *hp, *gethostdomain();
-       char *hostalias();
        extern struct hostent *_gethtbyname();
 
        extern struct hostent *_gethtbyname();
 
-       if (!(_res.options & RES_INIT) && res_init() == -1)
-               return ((struct hostent *) NULL);
        /*
         * disallow names consisting only of digits/dots, unless
         * they end in a dot.
        /*
         * disallow names consisting only of digits/dots, unless
         * they end in a dot.
@@ -264,78 +227,18 @@ gethostbyname(name)
                        if (!isdigit(*cp) && *cp != '.') 
                                break;
                }
                        if (!isdigit(*cp) && *cp != '.') 
                                break;
                }
-       errno = 0;
-       for (cp = name, n = 0; *cp; cp++)
-               if (*cp == '.')
-                       n++;
-       if (n == 0 && (cp = hostalias(name)))
-               return (gethostdomain(cp, (char *)NULL));
-       if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES))
-           for (domain = _res.dnsrch; *domain; domain++) {
-               hp = gethostdomain(name, *domain);
-               if (hp)
-                       return (hp);
-               /*
-                * If no server present, use host table.
-                * If host isn't found in this domain,
-                * keep trying higher domains in the search list
-                * (if that's enabled).
-                * On a NO_ADDRESS error, keep trying, otherwise
-                * a wildcard MX entry could keep us from finding
-                * host entries higher in the domain.
-                * If we get some other error (non-authoritative negative
-                * answer or server failure), then stop searching up,
-                * but try the input name below in case it's fully-qualified.
-                */
-               if (errno == ECONNREFUSED)
-                       return (_gethtbyname(name));
-               if ((h_errno != HOST_NOT_FOUND && h_errno != NO_ADDRESS) ||
-                   (_res.options & RES_DNSRCH) == 0)
-                       break;
-               h_errno = 0;
-       }
-       /*
-        * If the search/default failed, try the name as fully-qualified,
-        * but only if it contained at least one dot (even trailing).
-        */
-       if (n)
-               return (gethostdomain(name, (char *)NULL));
-       return ((struct hostent *) NULL);
-}
-
-static struct hostent *
-gethostdomain(name, domain)
-       char *name, *domain;
-{
-       querybuf buf;
-       char nbuf[2*MAXDNAME+2];
-       char *longname = nbuf;
-       int n;
 
 
-       if (domain == NULL) {
-               /*
-                * Check for trailing '.';
-                * copy without '.' if present.
-                */
-               n = strlen(name) - 1;
-               if (name[n] == '.' && n < sizeof(nbuf) - 1) {
-                       bcopy(name, nbuf, n);
-                       nbuf[n] = '\0';
-               } else
-                       longname = name;
-       } else
-               (void)sprintf(nbuf, "%.*s.%.*s",
-                   MAXDNAME, name, MAXDNAME, domain);
-       n = res_mkquery(QUERY, longname, C_IN, T_A, (char *)NULL, 0, NULL,
-               (char *)&buf, sizeof(buf));
-       if (n < 0) {
+       if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("res_mkquery failed\n");
+                       printf("res_search failed\n");
 #endif
 #endif
-               return ((struct hostent *) NULL);
+               if (errno == ECONNREFUSED)
+                       return (_gethtbyname(name));
+               else
+                       return ((struct hostent *) NULL);
        }
        }
-       return (getanswer((char *)&buf, n, 0));
+       return (getanswer(&buf, n, 0));
 }
 
 struct hostent *
 }
 
 struct hostent *
@@ -356,18 +259,17 @@ gethostbyaddr(addr, len, type)
                ((unsigned)addr[2] & 0xff),
                ((unsigned)addr[1] & 0xff),
                ((unsigned)addr[0] & 0xff));
                ((unsigned)addr[2] & 0xff),
                ((unsigned)addr[1] & 0xff),
                ((unsigned)addr[0] & 0xff));
-       n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL,
-               (char *)&buf, sizeof(buf));
+       n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
        if (n < 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
        if (n < 0) {
 #ifdef DEBUG
                if (_res.options & RES_DEBUG)
-                       printf("res_mkquery failed\n");
+                       printf("res_query failed\n");
 #endif
 #endif
+               if (errno == ECONNREFUSED)
+                       return (_gethtbyaddr(addr, len, type));
                return ((struct hostent *) NULL);
        }
                return ((struct hostent *) NULL);
        }
-       hp = getanswer((char *)&buf, n, 1);
-       if (hp == NULL && errno == ECONNREFUSED)
-               hp = _gethtbyaddr(addr, len, type);
+       hp = getanswer(&buf, n, 1);
        if (hp == NULL)
                return ((struct hostent *) NULL);
        hp->h_addrtype = type;
        if (hp == NULL)
                return ((struct hostent *) NULL);
        hp->h_addrtype = type;
@@ -378,40 +280,6 @@ gethostbyaddr(addr, len, type)
        return(hp);
 }
 
        return(hp);
 }
 
-char *
-hostalias(name)
-       register char *name;
-{
-       register char *C1, *C2;
-       FILE *fp;
-       char *file, *getenv(), *strcpy(), *strncpy();
-       char buf[BUFSIZ];
-       static char abuf[MAXDNAME];
-
-       file = getenv("HOSTALIASES");
-       if (file == NULL || (fp = fopen(file, "r")) == NULL)
-               return (NULL);
-       buf[sizeof(buf) - 1] = '\0';
-       while (fgets(buf, sizeof(buf), fp)) {
-               for (C1 = buf; *C1 && !isspace(*C1); ++C1);
-               if (!*C1)
-                       break;
-               *C1 = '\0';
-               if (!strcasecmp(buf, name)) {
-                       while (isspace(*++C1));
-                       if (!*C1)
-                               break;
-                       for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
-                       abuf[sizeof(abuf) - 1] = *C2 = '\0';
-                       (void)strncpy(abuf, C1, sizeof(abuf) - 1);
-                       fclose(fp);
-                       return (abuf);
-               }
-       }
-       fclose(fp);
-       return (NULL);
-}
-
 _sethtent(f)
        int f;
 {
 _sethtent(f)
        int f;
 {
@@ -439,7 +307,7 @@ _gethtent()
        if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
                return (NULL);
 again:
        if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
                return (NULL);
 again:
-       if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
+       if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
                return (NULL);
        if (*p == '#')
                goto again;
                return (NULL);
        if (*p == '#')
                goto again;
@@ -452,7 +320,7 @@ again:
                goto again;
        *cp++ = '\0';
        /* THIS STUFF IS INTERNET SPECIFIC */
                goto again;
        *cp++ = '\0';
        /* THIS STUFF IS INTERNET SPECIFIC */
-#if    BSD >= 43
+#if BSD >= 43 || defined(h_addr)       /* new-style hostent structure */
        host.h_addr_list = host_addrs;
 #endif
        host.h_addr = hostaddr;
        host.h_addr_list = host_addrs;
 #endif
        host.h_addr = hostaddr;