SCCS id got lost along the way
[unix-history] / usr / src / lib / libc / net / res_comp.c
index ecb6cc3..f1b2826 100644 (file)
@@ -2,22 +2,20 @@
  * Copyright (c) 1985 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1985 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.
+ * %sccs.include.redist.c%
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)res_comp.c 6.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)res_comp.c 6.22 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #endif /* LIBC_SCCS and not lint */
 
-#include <sys/types.h>
-#include <stdio.h>
+#include <sys/param.h>
 #include <arpa/nameser.h>
 #include <arpa/nameser.h>
+#include <netinet/in.h>
+#include <resolv.h>
+#include <stdio.h>
 
 
+static dn_find();
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
@@ -27,7 +25,8 @@ static char sccsid[] = "@(#)res_comp.c        6.12 (Berkeley) %G%";
  * Return size of compressed name or -1 if there was an error.
  */
 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  * Return size of compressed name or -1 if there was an error.
  */
 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
-       u_char *msg, *eomorig, *comp_dn, *exp_dn;
+       const u_char *msg, *eomorig, *comp_dn;
+       u_char *exp_dn;
        int length;
 {
        register u_char *cp, *dn;
        int length;
 {
        register u_char *cp, *dn;
@@ -36,8 +35,8 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
        int len = -1, checked = 0;
 
        dn = exp_dn;
        int len = -1, checked = 0;
 
        dn = exp_dn;
-       cp = comp_dn;
-       eom = exp_dn + length - 1;
+       cp = (u_char *)comp_dn;
+       eom = exp_dn + length;
        /*
         * fetch next label in domain name
         */
        /*
         * fetch next label in domain name
         */
@@ -57,7 +56,7 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
                        checked += n + 1;
                        while (--n >= 0) {
                                if ((c = *cp++) == '.') {
                        checked += n + 1;
                        while (--n >= 0) {
                                if ((c = *cp++) == '.') {
-                                       if (dn+n+1 >= eom)
+                                       if (dn + n + 2 >= eom)
                                                return (-1);
                                        *dn++ = '\\';
                                }
                                                return (-1);
                                        *dn++ = '\\';
                                }
@@ -70,7 +69,7 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
                case INDIR_MASK:
                        if (len < 0)
                                len = cp - comp_dn + 1;
                case INDIR_MASK:
                        if (len < 0)
                                len = cp - comp_dn + 1;
-                       cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
+                       cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff));
                        if (cp < msg || cp >= eomorig)  /* out of range */
                                return(-1);
                        checked += 2;
                        if (cp < msg || cp >= eomorig)  /* out of range */
                                return(-1);
                        checked += 2;
@@ -106,16 +105,16 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  * is NULL, we don't update the list.
  */
 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
  * is NULL, we don't update the list.
  */
 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
-       u_char *exp_dn, *comp_dn;
+       const u_char *exp_dn;
+       u_char *comp_dn, **dnptrs, **lastdnptr;
        int length;
        int length;
-       u_char **dnptrs, **lastdnptr;
 {
        register u_char *cp, *dn;
        register int c, l;
        u_char **cpp, **lpp, *sp, *eob;
        u_char *msg;
 
 {
        register u_char *cp, *dn;
        register int c, l;
        u_char **cpp, **lpp, *sp, *eob;
        u_char *msg;
 
-       dn = exp_dn;
+       dn = (u_char *)exp_dn;
        cp = comp_dn;
        eob = cp + length;
        if (dnptrs != NULL) {
        cp = comp_dn;
        eob = cp + length;
        if (dnptrs != NULL) {
@@ -152,8 +151,11 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
                                if ((c = *dn++) == '\0')
                                        break;
                        }
                                if ((c = *dn++) == '\0')
                                        break;
                        }
-                       if (cp >= eob)
+                       if (cp >= eob) {
+                               if (msg != NULL)
+                                       *lpp = NULL;
                                return (-1);
                                return (-1);
+                       }
                        *cp++ = c;
                } while ((c = *dn++) != '\0');
                /* catch trailing '.'s but not '..' */
                        *cp++ = c;
                } while ((c = *dn++) != '\0');
                /* catch trailing '.'s but not '..' */
@@ -161,12 +163,18 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
                        cp--;
                        break;
                }
                        cp--;
                        break;
                }
-               if (l <= 0 || l > MAXLABEL)
+               if (l <= 0 || l > MAXLABEL) {
+                       if (msg != NULL)
+                               *lpp = NULL;
                        return (-1);
                        return (-1);
+               }
                *sp = l;
        }
                *sp = l;
        }
-       if (cp >= eob)
+       if (cp >= eob) {
+               if (msg != NULL)
+                       *lpp = NULL;
                return (-1);
                return (-1);
+       }
        *cp++ = '\0';
        return (cp - comp_dn);
 }
        *cp++ = '\0';
        return (cp - comp_dn);
 }
@@ -174,13 +182,13 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
 /*
  * Skip over a compressed domain name. Return the size or -1.
  */
 /*
  * Skip over a compressed domain name. Return the size or -1.
  */
-dn_skipname(comp_dn, eom)
-       u_char *comp_dn, *eom;
+__dn_skipname(comp_dn, eom)
+       const u_char *comp_dn, *eom;
 {
        register u_char *cp;
        register int n;
 
 {
        register u_char *cp;
        register int n;
 
-       cp = comp_dn;
+       cp = (u_char *)comp_dn;
        while (cp < eom && (n = *cp++)) {
                /*
                 * check for indirection
        while (cp < eom && (n = *cp++)) {
                /*
                 * check for indirection
@@ -224,6 +232,8 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
                        switch (n & INDIR_MASK) {
                        case 0:         /* normal case, n == len */
                                while (--n >= 0) {
                        switch (n & INDIR_MASK) {
                        case 0:         /* normal case, n == len */
                                while (--n >= 0) {
+                                       if (*dn == '.')
+                                               goto next;
                                        if (*dn == '\\')
                                                dn++;
                                        if (*dn++ != *cp++)
                                        if (*dn == '\\')
                                                dn++;
                                        if (*dn++ != *cp++)
@@ -253,6 +263,8 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
  * Routines to insert/extract short/long's. Must account for byte
  * order and non-alignment problems. This code at least has the
  * advantage of being portable.
  * Routines to insert/extract short/long's. Must account for byte
  * order and non-alignment problems. This code at least has the
  * advantage of being portable.
+ *
+ * used by sendmail.
  */
 
 u_short
  */
 
 u_short
@@ -286,21 +298,24 @@ _getlong(msgp)
        return (u | *p);
 }
 
        return (u | *p);
 }
 
-
-putshort(s, msgp)
+void
+#ifdef __STDC__
+__putshort(register u_short s, register u_char *msgp)
+#else
+__putshort(s, msgp)
        register u_short s;
        register u_char *msgp;
        register u_short s;
        register u_char *msgp;
+#endif
 {
 {
-
        msgp[1] = s;
        msgp[0] = s >> 8;
 }
 
        msgp[1] = s;
        msgp[0] = s >> 8;
 }
 
-putlong(l, msgp)
+void
+__putlong(l, msgp)
        register u_long l;
        register u_char *msgp;
 {
        register u_long l;
        register u_char *msgp;
 {
-
        msgp[3] = l;
        msgp[2] = (l >>= 8);
        msgp[1] = (l >>= 8);
        msgp[3] = l;
        msgp[2] = (l >>= 8);
        msgp[1] = (l >>= 8);