res_comp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1985 Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms are permitted provided
00006  * that: (1) source distributions retain this entire copyright notice and
00007  * comment, and (2) distributions including binaries display the following
00008  * acknowledgement:  ``This product includes software developed by the
00009  * University of California, Berkeley and its contributors'' in the
00010  * documentation or other materials provided with the distribution and in
00011  * all advertising materials mentioning features or use of this software.
00012  * Neither the name of the University nor the names of its contributors may
00013  * be used to endorse or promote products derived from this software without
00014  * specific prior written permission.
00015  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00016  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00017  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00018  */
00019 
00020 #if defined(LIBC_SCCS) && !defined(lint)
00021 static char sccsid[] = "@(#)res_comp.c  6.18 (Berkeley) 6/27/90";
00022 #endif /* LIBC_SCCS and not lint */
00023 
00024 #if _MINIX
00025 #include <sys/types.h>
00026 #include <stdlib.h>
00027 
00028 #include <net/gen/in.h>
00029 #include <net/gen/nameser.h>
00030 #include <net/gen/resolv.h>
00031 
00032 static int dn_find _ARGS(( const u_char *exp_dn, const u_char *msg,
00033         u_char **dnptrs, u_char **lastdnptr ));
00034 int dn_skipname _ARGS(( const u_char *comp_dn, const u_char *eom ));
00035 
00036 #define getshort _getshort
00037 #define getlong _getlong
00038 #define putshort __putshort
00039 #define putlong __putlong
00040 #else
00041 #include <sys/types.h>
00042 #include <stdio.h>
00043 #include <arpa/nameser.h>
00044 
00045 static dn_find();
00046 #endif
00047 
00048 #ifdef __STDC__
00049 #define CONST   const
00050 #else
00051 #define CONST
00052 #endif
00053 
00054 /*
00055  * Expand compressed domain name 'comp_dn' to full domain name.
00056  * 'msg' is a pointer to the begining of the message,
00057  * 'eomorig' points to the first location after the message,
00058  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
00059  * Return size of compressed name or -1 if there was an error.
00060  */
00061 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
00062         CONST u_char *msg, *eomorig, *comp_dn;
00063         u_char *exp_dn;
00064         int length;
00065 {
00066         register CONST u_char *cp;
00067         register u_char *dn;
00068         register int n, c;
00069         CONST u_char *eom;
00070         int len = -1, checked = 0;
00071 
00072         dn = exp_dn;
00073         cp = comp_dn;
00074         eom = exp_dn + length;
00075         /*
00076          * fetch next label in domain name
00077          */
00078         while (n = *cp++) {
00079                 /*
00080                  * Check for indirection
00081                  */
00082                 switch (n & INDIR_MASK) {
00083                 case 0:
00084                         if (dn != exp_dn) {
00085                                 if (dn >= eom)
00086                                         return (-1);
00087                                 *dn++ = '.';
00088                         }
00089                         if (dn+n >= eom)
00090                                 return (-1);
00091                         checked += n + 1;
00092                         while (--n >= 0) {
00093                                 if ((c = *cp++) == '.') {
00094                                         if (dn + n + 2 >= eom)
00095                                                 return (-1);
00096                                         *dn++ = '\\';
00097                                 }
00098                                 *dn++ = c;
00099                                 if (cp >= eomorig)      /* out of range */
00100                                         return(-1);
00101                         }
00102                         break;
00103 
00104                 case INDIR_MASK:
00105                         if (len < 0)
00106                                 len = cp - comp_dn + 1;
00107                         cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
00108                         if (cp < msg || cp >= eomorig)  /* out of range */
00109                                 return(-1);
00110                         checked += 2;
00111                         /*
00112                          * Check for loops in the compressed name;
00113                          * if we've looked at the whole message,
00114                          * there must be a loop.
00115                          */
00116                         if (checked >= eomorig - msg)
00117                                 return (-1);
00118                         break;
00119 
00120                 default:
00121                         return (-1);                    /* flag error */
00122                 }
00123         }
00124         *dn = '\0';
00125         if (len < 0)
00126                 len = cp - comp_dn;
00127         return (len);
00128 }
00129 
00130 /*
00131  * Compress domain name 'exp_dn' into 'comp_dn'.
00132  * Return the size of the compressed name or -1.
00133  * 'length' is the size of the array pointed to by 'comp_dn'.
00134  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
00135  * is a pointer to the beginning of the message. The list ends with NULL.
00136  * 'lastdnptr' is a pointer to the end of the arrary pointed to
00137  * by 'dnptrs'. Side effect is to update the list of pointers for
00138  * labels inserted into the message as we compress the name.
00139  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
00140  * is NULL, we don't update the list.
00141  */
00142 int
00143 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
00144         CONST u_char *exp_dn;
00145         u_char *comp_dn;
00146         int length;
00147         u_char **dnptrs, **lastdnptr;
00148 {
00149         register u_char *cp;
00150         register CONST u_char *dn;
00151         register int c, l;
00152         u_char **cpp, **lpp, *sp, *eob;
00153         u_char *msg;
00154 
00155         dn = exp_dn;
00156         cp = comp_dn;
00157         eob = cp + length;
00158         if (dnptrs != NULL) {
00159                 if ((msg = *dnptrs++) != NULL) {
00160                         for (cpp = dnptrs; *cpp != NULL; cpp++)
00161                                 ;
00162                         lpp = cpp;      /* end of list to search */
00163                 }
00164         } else
00165                 msg = NULL;
00166         for (c = *dn++; c != '\0'; ) {
00167                 /* look to see if we can use pointers */
00168                 if (msg != NULL) {
00169                         if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
00170                                 if (cp+1 >= eob)
00171                                         return (-1);
00172                                 *cp++ = (l >> 8) | INDIR_MASK;
00173                                 *cp++ = l % 256;
00174                                 return (cp - comp_dn);
00175                         }
00176                         /* not found, save it */
00177                         if (lastdnptr != NULL && cpp < lastdnptr-1) {
00178                                 *cpp++ = cp;
00179                                 *cpp = NULL;
00180                         }
00181                 }
00182                 sp = cp++;      /* save ptr to length byte */
00183                 do {
00184                         if (c == '.') {
00185                                 c = *dn++;
00186                                 break;
00187                         }
00188                         if (c == '\\') {
00189                                 if ((c = *dn++) == '\0')
00190                                         break;
00191                         }
00192                         if (cp >= eob) {
00193                                 if (msg != NULL)
00194                                         *lpp = NULL;
00195                                 return (-1);
00196                         }
00197                         *cp++ = c;
00198                 } while ((c = *dn++) != '\0');
00199                 /* catch trailing '.'s but not '..' */
00200                 if ((l = cp - sp - 1) == 0 && c == '\0') {
00201                         cp--;
00202                         break;
00203                 }
00204                 if (l <= 0 || l > MAXLABEL) {
00205                         if (msg != NULL)
00206                                 *lpp = NULL;
00207                         return (-1);
00208                 }
00209                 *sp = l;
00210         }
00211         if (cp >= eob) {
00212                 if (msg != NULL)
00213                         *lpp = NULL;
00214                 return (-1);
00215         }
00216         *cp++ = '\0';
00217         return (cp - comp_dn);
00218 }
00219 
00220 /*
00221  * Skip over a compressed domain name. Return the size or -1.
00222  */
00223 dn_skipname(comp_dn, eom)
00224         CONST u_char *comp_dn, *eom;
00225 {
00226         register CONST u_char *cp;
00227         register int n;
00228 
00229         cp = comp_dn;
00230         while (cp < eom && (n = *cp++)) {
00231                 /*
00232                  * check for indirection
00233                  */
00234                 switch (n & INDIR_MASK) {
00235                 case 0:         /* normal case, n == len */
00236                         cp += n;
00237                         continue;
00238                 default:        /* illegal type */
00239                         return (-1);
00240                 case INDIR_MASK:        /* indirection */
00241                         cp++;
00242                 }
00243                 break;
00244         }
00245         return (cp - comp_dn);
00246 }
00247 
00248 /*
00249  * Search for expanded name from a list of previously compressed names.
00250  * Return the offset from msg if found or -1.
00251  * dnptrs is the pointer to the first name on the list,
00252  * not the pointer to the start of the message.
00253  */
00254 static int
00255 dn_find(exp_dn, msg, dnptrs, lastdnptr)
00256         CONST u_char *exp_dn, *msg;
00257         u_char **dnptrs, **lastdnptr;
00258 {
00259         CONST register u_char *dn, *cp;
00260         register u_char **cpp;
00261         register int n;
00262         CONST u_char *sp;
00263 
00264         for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
00265                 dn = exp_dn;
00266                 sp = cp = *cpp;
00267                 while (n = *cp++) {
00268                         /*
00269                          * check for indirection
00270                          */
00271                         switch (n & INDIR_MASK) {
00272                         case 0:         /* normal case, n == len */
00273                                 while (--n >= 0) {
00274                                         if (*dn == '.')
00275                                                 goto next;
00276                                         if (*dn == '\\')
00277                                                 dn++;
00278                                         if (*dn++ != *cp++)
00279                                                 goto next;
00280                                 }
00281                                 if ((n = *dn++) == '\0' && *cp == '\0')
00282                                         return (sp - msg);
00283                                 if (n == '.')
00284                                         continue;
00285                                 goto next;
00286 
00287                         default:        /* illegal type */
00288                                 return (-1);
00289 
00290                         case INDIR_MASK:        /* indirection */
00291                                 cp = msg + (((n & 0x3f) << 8) | *cp);
00292                         }
00293                 }
00294                 if (*dn == '\0')
00295                         return (sp - msg);
00296         next:   ;
00297         }
00298         return (-1);
00299 }
00300 
00301 /*
00302  * Routines to insert/extract short/long's. Must account for byte
00303  * order and non-alignment problems. This code at least has the
00304  * advantage of being portable.
00305  *
00306  * used by sendmail.
00307  */
00308 
00309 u16_t
00310 getshort(msgp)
00311         CONST u8_t *msgp;
00312 {
00313         return ((msgp[0] << 8) | (msgp[1] << 0));
00314 }
00315 
00316 u32_t
00317 getlong(msgp)
00318         CONST u8_t *msgp;
00319 {
00320         return (  ((u32_t) msgp[0] << 24)
00321                 | ((u32_t) msgp[1] << 16)
00322                 | ((u32_t) msgp[2] <<  8)
00323                 | ((u32_t) msgp[3] <<  0));
00324 }
00325 
00326 
00327 void
00328 putshort(s, msgp)
00329         register U16_t s;
00330         register u8_t *msgp;
00331 {
00332 
00333         msgp[1] = s;
00334         msgp[0] = s >> 8;
00335 }
00336 
00337 void
00338 putlong(l, msgp)
00339         register u32_t l;
00340         register u8_t *msgp;
00341 {
00342 
00343         msgp[3] = l;
00344         msgp[2] = (l >>= 8);
00345         msgp[1] = (l >>= 8);
00346         msgp[0] = l >> 8;
00347 }

Generated on Fri Apr 14 22:57:26 2006 for minix by  doxygen 1.4.6