118140Sralph #ifndef lint 2*18342Sralph static char sccsid[] = "@(#)res_comp.c 4.2 (Berkeley) 03/15/85"; 318140Sralph #endif 418140Sralph 518140Sralph #include <sys/types.h> 618140Sralph #include <stdio.h> 718140Sralph #include <ctype.h> 818140Sralph #include <nameser.h> 918140Sralph 1018140Sralph 1118140Sralph /* 12*18342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 13*18342Sralph * Expanded names are converted to upper case. 14*18342Sralph * 'msg' is a pointer to the begining of the message, 15*18342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 1618140Sralph * Return size of compressed name or -1 if there was an error. 1718140Sralph */ 1818140Sralph dn_expand(msg, comp_dn, exp_dn, length) 1918140Sralph char *msg, *comp_dn, *exp_dn; 2018140Sralph int length; 2118140Sralph { 2218140Sralph register char *cp, *dn; 2318140Sralph register int n, c; 2418140Sralph char *eom; 25*18342Sralph int len = -1; 2618140Sralph 2718140Sralph dn = exp_dn; 2818140Sralph cp = comp_dn; 2918140Sralph eom = exp_dn + length - 1; 3018140Sralph /* 3118140Sralph * fetch next label in domain name 3218140Sralph */ 3318140Sralph while (n = *cp++) { 3418140Sralph /* 3518140Sralph * Check for indirection 3618140Sralph */ 3718140Sralph switch (n & INDIR_MASK) { 3818140Sralph case 0: 39*18342Sralph if (dn != exp_dn) { 40*18342Sralph if (dn >= eom) 41*18342Sralph return (-1); 4218140Sralph *dn++ = '.'; 43*18342Sralph } 4418140Sralph if (dn+n >= eom) 4518140Sralph return (-1); 4618140Sralph while (--n >= 0) 4718140Sralph if (islower(c = *cp++)) 4818140Sralph *dn++ = toupper(c); 49*18342Sralph else { 50*18342Sralph if (c == '.') { 51*18342Sralph if (dn+n+1 >= eom) 52*18342Sralph return (-1); 53*18342Sralph *dn++ = '\\'; 54*18342Sralph } 5518140Sralph *dn++ = c; 56*18342Sralph } 5718140Sralph break; 5818140Sralph 5918140Sralph case INDIR_MASK: 60*18342Sralph if (len < 0) 6118140Sralph len = cp - comp_dn + 1; 6218140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 6318140Sralph break; 6418140Sralph 6518140Sralph default: 6618140Sralph return (-1); /* flag error */ 6718140Sralph } 6818140Sralph } 6918140Sralph *dn = '\0'; 70*18342Sralph if (len < 0) 7118140Sralph len = cp - comp_dn; 7218140Sralph return (len); 7318140Sralph } 7418140Sralph 7518140Sralph /* 76*18342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 77*18342Sralph * Return the size of the compressed name or -1. 78*18342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 79*18342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 8018140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 81*18342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 82*18342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 83*18342Sralph * labels inserted into the message as we compress the name. 84*18342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 85*18342Sralph * is NULL, we don't update the list. 8618140Sralph */ 8718140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 8818140Sralph char *exp_dn, *comp_dn; 8918140Sralph int length; 9018140Sralph char **dnptrs, **lastdnptr; 9118140Sralph { 9218140Sralph register char *cp, *dn; 9318140Sralph register int c, l; 9418140Sralph char **cpp, **lpp, *sp, *eob; 9518140Sralph char *msg; 9618140Sralph 9718140Sralph dn = exp_dn; 9818140Sralph cp = comp_dn; 9918140Sralph eob = comp_dn + length; 10018140Sralph if (dnptrs != NULL) { 10118140Sralph if ((msg = *dnptrs++) != NULL) { 10218140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 10318140Sralph ; 10418140Sralph lpp = cpp; /* end of list to search */ 10518140Sralph } 10618140Sralph } else 10718140Sralph msg = NULL; 10818140Sralph for (c = *dn++; c != '\0'; ) { 10918140Sralph /* look to see if we can use pointers */ 11018140Sralph if (msg != NULL) { 11118140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 11218140Sralph if (cp+1 >= eob) 11318140Sralph return (-1); 11418140Sralph *cp++ = (l >> 8) | INDIR_MASK; 11518140Sralph *cp++ = l; 11618140Sralph return (cp - comp_dn); 11718140Sralph } 11818140Sralph /* not found, save it */ 11918140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 12018140Sralph *cpp++ = cp; 12118140Sralph *cpp = NULL; 12218140Sralph } 12318140Sralph } 12418140Sralph sp = cp++; /* save ptr to length byte */ 12518140Sralph do { 12618140Sralph if (c == '.') { 12718140Sralph c = *dn++; 12818140Sralph break; 12918140Sralph } 130*18342Sralph if (c == '\\') { 131*18342Sralph if ((c = *dn++) == '\0') 132*18342Sralph break; 133*18342Sralph } 13418140Sralph if (cp >= eob) 13518140Sralph return (-1); 13618140Sralph *cp++ = c; 13718140Sralph } while ((c = *dn++) != '\0'); 138*18342Sralph /* catch trailing '.'s but not '..' */ 139*18342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 140*18342Sralph cp--; 141*18342Sralph break; 142*18342Sralph } 143*18342Sralph if (l <= 0 || l > MAXLABEL) 14418140Sralph return (-1); 14518140Sralph *sp = l; 14618140Sralph } 14718140Sralph if (cp >= eob) 14818140Sralph return (-1); 14918140Sralph *cp++ = '\0'; 15018140Sralph return (cp - comp_dn); 15118140Sralph } 15218140Sralph 15318140Sralph /* 154*18342Sralph * Skip over a compressed domain name. Return the size or -1. 15518140Sralph */ 156*18342Sralph dn_skip(comp_dn) 157*18342Sralph char *comp_dn; 15818140Sralph { 15918140Sralph register char *cp; 16018140Sralph register int n; 16118140Sralph 162*18342Sralph cp = comp_dn; 16318140Sralph while (n = *cp++) { 16418140Sralph /* 16518140Sralph * check for indirection 16618140Sralph */ 16718140Sralph switch (n & INDIR_MASK) { 16818140Sralph case 0: /* normal case, n == len */ 16918140Sralph cp += n; 17018140Sralph continue; 17118140Sralph default: /* illegal type */ 17218140Sralph return (-1); 17318140Sralph case INDIR_MASK: /* indirection */ 17418140Sralph cp++; 17518140Sralph } 17618140Sralph break; 17718140Sralph } 178*18342Sralph return (cp - comp_dn); 17918140Sralph } 18018140Sralph 18118140Sralph /* 18218140Sralph * Search for expanded name from a list of previously compressed names. 18318140Sralph * Return the offset from msg if found or -1. 18418140Sralph */ 18518140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 18618140Sralph char *exp_dn, *msg; 18718140Sralph char **dnptrs, **lastdnptr; 18818140Sralph { 18918140Sralph register char *dn, *cp, **cpp; 19018140Sralph register int n; 19118140Sralph char *sp; 19218140Sralph 19318140Sralph for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 19418140Sralph dn = exp_dn; 19518140Sralph sp = cp = *cpp; 19618140Sralph while (n = *cp++) { 19718140Sralph /* 19818140Sralph * check for indirection 19918140Sralph */ 20018140Sralph switch (n & INDIR_MASK) { 20118140Sralph case 0: /* normal case, n == len */ 202*18342Sralph while (--n >= 0) { 203*18342Sralph if (*dn == '\\') 204*18342Sralph dn++; 20518140Sralph if (*dn++ != *cp++) 20618140Sralph goto next; 207*18342Sralph } 20818140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 20918140Sralph return (sp - msg); 21018140Sralph if (n == '.') 21118140Sralph continue; 21218140Sralph goto next; 21318140Sralph 21418140Sralph default: /* illegal type */ 21518140Sralph return (-1); 21618140Sralph 21718140Sralph case INDIR_MASK: /* indirection */ 21818140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 21918140Sralph } 22018140Sralph } 22118140Sralph if (*dn == '\0') 22218140Sralph return (sp - msg); 22318140Sralph next: ; 22418140Sralph } 22518140Sralph return (-1); 22618140Sralph } 227