118546Sralph /* 221384Sdist * Copyright (c) 1985 Regents of the University of California. 333679Sbostic * All rights reserved. 433679Sbostic * 533679Sbostic * Redistribution and use in source and binary forms are permitted 6*34817Sbostic * provided that the above copyright notice and this paragraph are 7*34817Sbostic * duplicated in all such forms and that any documentation, 8*34817Sbostic * advertising materials, and other materials related to such 9*34817Sbostic * distribution and use acknowledge that the software was developed 10*34817Sbostic * by the University of California, Berkeley. The name of the 11*34817Sbostic * University may not be used to endorse or promote products derived 12*34817Sbostic * from this software without specific prior written permission. 13*34817Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34817Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34817Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1618546Sralph */ 1718546Sralph 1826631Sdonn #if defined(LIBC_SCCS) && !defined(lint) 19*34817Sbostic static char sccsid[] = "@(#)res_comp.c 6.14 (Berkeley) 06/27/88"; 2033679Sbostic #endif /* LIBC_SCCS and not lint */ 2121384Sdist 2218140Sralph #include <sys/types.h> 2318140Sralph #include <stdio.h> 2424079Skjd #include <arpa/nameser.h> 2518140Sralph 2618140Sralph /* 2718342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 2818342Sralph * 'msg' is a pointer to the begining of the message, 2926112Skarels * 'eomorig' points to the first location after the message, 3018342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 3118140Sralph * Return size of compressed name or -1 if there was an error. 3218140Sralph */ 3326112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length) 3432647Skarels u_char *msg, *eomorig, *comp_dn, *exp_dn; 3526112Skarels int length; 3618140Sralph { 3732647Skarels register u_char *cp, *dn; 3818140Sralph register int n, c; 3932647Skarels u_char *eom; 4031686Skarels int len = -1, checked = 0; 4118140Sralph 4218140Sralph dn = exp_dn; 4318140Sralph cp = comp_dn; 4418140Sralph eom = exp_dn + length - 1; 4518140Sralph /* 4618140Sralph * fetch next label in domain name 4718140Sralph */ 4818140Sralph while (n = *cp++) { 4918140Sralph /* 5018140Sralph * Check for indirection 5118140Sralph */ 5218140Sralph switch (n & INDIR_MASK) { 5318140Sralph case 0: 5418342Sralph if (dn != exp_dn) { 5518342Sralph if (dn >= eom) 5618342Sralph return (-1); 5718140Sralph *dn++ = '.'; 5818342Sralph } 5918140Sralph if (dn+n >= eom) 6018140Sralph return (-1); 6131686Skarels checked += n + 1; 6226064Skjd while (--n >= 0) { 6326812Sbloom if ((c = *cp++) == '.') { 6426812Sbloom if (dn+n+1 >= eom) 6526812Sbloom return (-1); 6626812Sbloom *dn++ = '\\'; 6718342Sralph } 6826812Sbloom *dn++ = c; 6926112Skarels if (cp >= eomorig) /* out of range */ 7026064Skjd return(-1); 7126064Skjd } 7218140Sralph break; 7318140Sralph 7418140Sralph case INDIR_MASK: 7518342Sralph if (len < 0) 7618140Sralph len = cp - comp_dn + 1; 7718140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 7826112Skarels if (cp < msg || cp >= eomorig) /* out of range */ 7926064Skjd return(-1); 8031686Skarels checked += 2; 8131686Skarels /* 8231686Skarels * Check for loops in the compressed name; 8331686Skarels * if we've looked at the whole message, 8431686Skarels * there must be a loop. 8531686Skarels */ 8631686Skarels if (checked >= eomorig - msg) 8731686Skarels return (-1); 8818140Sralph break; 8918140Sralph 9018140Sralph default: 9118140Sralph return (-1); /* flag error */ 9218140Sralph } 9318140Sralph } 9418140Sralph *dn = '\0'; 9518342Sralph if (len < 0) 9618140Sralph len = cp - comp_dn; 9718140Sralph return (len); 9818140Sralph } 9918140Sralph 10018140Sralph /* 10118342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 10218342Sralph * Return the size of the compressed name or -1. 10318342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 10418342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 10518140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 10618342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 10718342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 10818342Sralph * labels inserted into the message as we compress the name. 10918342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 11018342Sralph * is NULL, we don't update the list. 11118140Sralph */ 11218140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 11332647Skarels u_char *exp_dn, *comp_dn; 11418140Sralph int length; 11532647Skarels u_char **dnptrs, **lastdnptr; 11618140Sralph { 11732647Skarels register u_char *cp, *dn; 11818140Sralph register int c, l; 11932647Skarels u_char **cpp, **lpp, *sp, *eob; 12032647Skarels u_char *msg; 12118140Sralph 12218140Sralph dn = exp_dn; 12318140Sralph cp = comp_dn; 12425360Sbloom eob = cp + length; 12518140Sralph if (dnptrs != NULL) { 12618140Sralph if ((msg = *dnptrs++) != NULL) { 12718140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 12818140Sralph ; 12918140Sralph lpp = cpp; /* end of list to search */ 13018140Sralph } 13118140Sralph } else 13218140Sralph msg = NULL; 13318140Sralph for (c = *dn++; c != '\0'; ) { 13418140Sralph /* look to see if we can use pointers */ 13518140Sralph if (msg != NULL) { 13618140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 13718140Sralph if (cp+1 >= eob) 13818140Sralph return (-1); 13918140Sralph *cp++ = (l >> 8) | INDIR_MASK; 14026240Skjd *cp++ = l % 256; 14118140Sralph return (cp - comp_dn); 14218140Sralph } 14318140Sralph /* not found, save it */ 14418140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 14518140Sralph *cpp++ = cp; 14618140Sralph *cpp = NULL; 14718140Sralph } 14818140Sralph } 14918140Sralph sp = cp++; /* save ptr to length byte */ 15018140Sralph do { 15118140Sralph if (c == '.') { 15218140Sralph c = *dn++; 15318140Sralph break; 15418140Sralph } 15518342Sralph if (c == '\\') { 15618342Sralph if ((c = *dn++) == '\0') 15718342Sralph break; 15818342Sralph } 15918140Sralph if (cp >= eob) 16018140Sralph return (-1); 16118140Sralph *cp++ = c; 16218140Sralph } while ((c = *dn++) != '\0'); 16318342Sralph /* catch trailing '.'s but not '..' */ 16418342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 16518342Sralph cp--; 16618342Sralph break; 16718342Sralph } 16818342Sralph if (l <= 0 || l > MAXLABEL) 16918140Sralph return (-1); 17018140Sralph *sp = l; 17118140Sralph } 17218140Sralph if (cp >= eob) 17318140Sralph return (-1); 17418140Sralph *cp++ = '\0'; 17518140Sralph return (cp - comp_dn); 17618140Sralph } 17718140Sralph 17818140Sralph /* 17918342Sralph * Skip over a compressed domain name. Return the size or -1. 18018140Sralph */ 18132647Skarels dn_skipname(comp_dn, eom) 18232647Skarels u_char *comp_dn, *eom; 18318140Sralph { 18432647Skarels register u_char *cp; 18518140Sralph register int n; 18618140Sralph 18718342Sralph cp = comp_dn; 18832647Skarels while (cp < eom && (n = *cp++)) { 18918140Sralph /* 19018140Sralph * check for indirection 19118140Sralph */ 19218140Sralph switch (n & INDIR_MASK) { 19318140Sralph case 0: /* normal case, n == len */ 19418140Sralph cp += n; 19518140Sralph continue; 19618140Sralph default: /* illegal type */ 19718140Sralph return (-1); 19818140Sralph case INDIR_MASK: /* indirection */ 19918140Sralph cp++; 20018140Sralph } 20118140Sralph break; 20218140Sralph } 20318342Sralph return (cp - comp_dn); 20418140Sralph } 20518140Sralph 20618140Sralph /* 20718140Sralph * Search for expanded name from a list of previously compressed names. 20818140Sralph * Return the offset from msg if found or -1. 20933617Skarels * dnptrs is the pointer to the first name on the list, 21033617Skarels * not the pointer to the start of the message. 21118140Sralph */ 21233617Skarels static 21318140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 21432647Skarels u_char *exp_dn, *msg; 21532647Skarels u_char **dnptrs, **lastdnptr; 21618140Sralph { 21732647Skarels register u_char *dn, *cp, **cpp; 21818140Sralph register int n; 21932647Skarels u_char *sp; 22018140Sralph 22133617Skarels for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 22218140Sralph dn = exp_dn; 22318140Sralph sp = cp = *cpp; 22418140Sralph while (n = *cp++) { 22518140Sralph /* 22618140Sralph * check for indirection 22718140Sralph */ 22818140Sralph switch (n & INDIR_MASK) { 22918140Sralph case 0: /* normal case, n == len */ 23018342Sralph while (--n >= 0) { 23118342Sralph if (*dn == '\\') 23218342Sralph dn++; 23318140Sralph if (*dn++ != *cp++) 23418140Sralph goto next; 23518342Sralph } 23618140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 23718140Sralph return (sp - msg); 23818140Sralph if (n == '.') 23918140Sralph continue; 24018140Sralph goto next; 24118140Sralph 24218140Sralph default: /* illegal type */ 24318140Sralph return (-1); 24418140Sralph 24518140Sralph case INDIR_MASK: /* indirection */ 24632647Skarels cp = msg + (((n & 0x3f) << 8) | *cp); 24718140Sralph } 24818140Sralph } 24918140Sralph if (*dn == '\0') 25018140Sralph return (sp - msg); 25118140Sralph next: ; 25218140Sralph } 25318140Sralph return (-1); 25418140Sralph } 25518528Sralph 25618528Sralph /* 25718528Sralph * Routines to insert/extract short/long's. Must account for byte 25818528Sralph * order and non-alignment problems. This code at least has the 25918528Sralph * advantage of being portable. 26033727Sbostic * 26133727Sbostic * used by sendmail. 26218528Sralph */ 26318528Sralph 26418528Sralph u_short 26530441Skjd _getshort(msgp) 26632647Skarels u_char *msgp; 26718528Sralph { 26818528Sralph register u_char *p = (u_char *) msgp; 26925360Sbloom #ifdef vax 27025360Sbloom /* 27125360Sbloom * vax compiler doesn't put shorts in registers 27225360Sbloom */ 27325360Sbloom register u_long u; 27425360Sbloom #else 27524735Sbloom register u_short u; 27625360Sbloom #endif 27718528Sralph 27824735Sbloom u = *p++ << 8; 27925360Sbloom return ((u_short)(u | *p)); 28018528Sralph } 28118528Sralph 28218528Sralph u_long 28330441Skjd _getlong(msgp) 28432647Skarels u_char *msgp; 28518528Sralph { 28618528Sralph register u_char *p = (u_char *) msgp; 28723870Skjd register u_long u; 28818528Sralph 28923870Skjd u = *p++; u <<= 8; 29023870Skjd u |= *p++; u <<= 8; 29123870Skjd u |= *p++; u <<= 8; 29223870Skjd return (u | *p); 29318528Sralph } 29418528Sralph 29523870Skjd 29618528Sralph putshort(s, msgp) 29718528Sralph register u_short s; 29832647Skarels register u_char *msgp; 29918528Sralph { 30018528Sralph 30118528Sralph msgp[1] = s; 30218528Sralph msgp[0] = s >> 8; 30318528Sralph } 30418528Sralph 30518528Sralph putlong(l, msgp) 30618528Sralph register u_long l; 30732647Skarels register u_char *msgp; 30818528Sralph { 30918528Sralph 31018528Sralph msgp[3] = l; 31118528Sralph msgp[2] = (l >>= 8); 31218528Sralph msgp[1] = (l >>= 8); 31318528Sralph msgp[0] = l >> 8; 31418528Sralph } 315