118140Sralph #ifndef lint 2*18546Sralph static char sccsid[] = "@(#)res_comp.c 4.4 (Berkeley) 04/01/85"; 318140Sralph #endif 418140Sralph 5*18546Sralph /* 6*18546Sralph * Copyright (c) 1985 Regents of the University of California 7*18546Sralph * All Rights Reserved 8*18546Sralph */ 9*18546Sralph 1018140Sralph #include <sys/types.h> 1118140Sralph #include <stdio.h> 1218140Sralph #include <ctype.h> 1318140Sralph #include <nameser.h> 1418140Sralph 1518140Sralph /* 1618342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 1718342Sralph * Expanded names are converted to upper case. 1818342Sralph * 'msg' is a pointer to the begining of the message, 1918342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 2018140Sralph * Return size of compressed name or -1 if there was an error. 2118140Sralph */ 2218140Sralph dn_expand(msg, comp_dn, exp_dn, length) 2318140Sralph char *msg, *comp_dn, *exp_dn; 2418140Sralph int length; 2518140Sralph { 2618140Sralph register char *cp, *dn; 2718140Sralph register int n, c; 2818140Sralph char *eom; 2918342Sralph int len = -1; 3018140Sralph 3118140Sralph dn = exp_dn; 3218140Sralph cp = comp_dn; 3318140Sralph eom = exp_dn + length - 1; 3418140Sralph /* 3518140Sralph * fetch next label in domain name 3618140Sralph */ 3718140Sralph while (n = *cp++) { 3818140Sralph /* 3918140Sralph * Check for indirection 4018140Sralph */ 4118140Sralph switch (n & INDIR_MASK) { 4218140Sralph case 0: 4318342Sralph if (dn != exp_dn) { 4418342Sralph if (dn >= eom) 4518342Sralph return (-1); 4618140Sralph *dn++ = '.'; 4718342Sralph } 4818140Sralph if (dn+n >= eom) 4918140Sralph return (-1); 5018140Sralph while (--n >= 0) 5118140Sralph if (islower(c = *cp++)) 5218140Sralph *dn++ = toupper(c); 5318342Sralph else { 5418342Sralph if (c == '.') { 5518342Sralph if (dn+n+1 >= eom) 5618342Sralph return (-1); 5718342Sralph *dn++ = '\\'; 5818342Sralph } 5918140Sralph *dn++ = c; 6018342Sralph } 6118140Sralph break; 6218140Sralph 6318140Sralph case INDIR_MASK: 6418342Sralph if (len < 0) 6518140Sralph len = cp - comp_dn + 1; 6618140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 6718140Sralph break; 6818140Sralph 6918140Sralph default: 7018140Sralph return (-1); /* flag error */ 7118140Sralph } 7218140Sralph } 7318140Sralph *dn = '\0'; 7418342Sralph if (len < 0) 7518140Sralph len = cp - comp_dn; 7618140Sralph return (len); 7718140Sralph } 7818140Sralph 7918140Sralph /* 8018342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 8118342Sralph * Return the size of the compressed name or -1. 8218342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 8318342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 8418140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 8518342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 8618342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 8718342Sralph * labels inserted into the message as we compress the name. 8818342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 8918342Sralph * is NULL, we don't update the list. 9018140Sralph */ 9118140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 9218140Sralph char *exp_dn, *comp_dn; 9318140Sralph int length; 9418140Sralph char **dnptrs, **lastdnptr; 9518140Sralph { 9618140Sralph register char *cp, *dn; 9718140Sralph register int c, l; 9818140Sralph char **cpp, **lpp, *sp, *eob; 9918140Sralph char *msg; 10018140Sralph 10118140Sralph dn = exp_dn; 10218140Sralph cp = comp_dn; 10318140Sralph eob = comp_dn + length; 10418140Sralph if (dnptrs != NULL) { 10518140Sralph if ((msg = *dnptrs++) != NULL) { 10618140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 10718140Sralph ; 10818140Sralph lpp = cpp; /* end of list to search */ 10918140Sralph } 11018140Sralph } else 11118140Sralph msg = NULL; 11218140Sralph for (c = *dn++; c != '\0'; ) { 11318140Sralph /* look to see if we can use pointers */ 11418140Sralph if (msg != NULL) { 11518140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 11618140Sralph if (cp+1 >= eob) 11718140Sralph return (-1); 11818140Sralph *cp++ = (l >> 8) | INDIR_MASK; 11918140Sralph *cp++ = l; 12018140Sralph return (cp - comp_dn); 12118140Sralph } 12218140Sralph /* not found, save it */ 12318140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 12418140Sralph *cpp++ = cp; 12518140Sralph *cpp = NULL; 12618140Sralph } 12718140Sralph } 12818140Sralph sp = cp++; /* save ptr to length byte */ 12918140Sralph do { 13018140Sralph if (c == '.') { 13118140Sralph c = *dn++; 13218140Sralph break; 13318140Sralph } 13418342Sralph if (c == '\\') { 13518342Sralph if ((c = *dn++) == '\0') 13618342Sralph break; 13718342Sralph } 13818140Sralph if (cp >= eob) 13918140Sralph return (-1); 14018140Sralph *cp++ = c; 14118140Sralph } while ((c = *dn++) != '\0'); 14218342Sralph /* catch trailing '.'s but not '..' */ 14318342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 14418342Sralph cp--; 14518342Sralph break; 14618342Sralph } 14718342Sralph if (l <= 0 || l > MAXLABEL) 14818140Sralph return (-1); 14918140Sralph *sp = l; 15018140Sralph } 15118140Sralph if (cp >= eob) 15218140Sralph return (-1); 15318140Sralph *cp++ = '\0'; 15418140Sralph return (cp - comp_dn); 15518140Sralph } 15618140Sralph 15718140Sralph /* 15818342Sralph * Skip over a compressed domain name. Return the size or -1. 15918140Sralph */ 16018342Sralph dn_skip(comp_dn) 16118342Sralph char *comp_dn; 16218140Sralph { 16318140Sralph register char *cp; 16418140Sralph register int n; 16518140Sralph 16618342Sralph cp = comp_dn; 16718140Sralph while (n = *cp++) { 16818140Sralph /* 16918140Sralph * check for indirection 17018140Sralph */ 17118140Sralph switch (n & INDIR_MASK) { 17218140Sralph case 0: /* normal case, n == len */ 17318140Sralph cp += n; 17418140Sralph continue; 17518140Sralph default: /* illegal type */ 17618140Sralph return (-1); 17718140Sralph case INDIR_MASK: /* indirection */ 17818140Sralph cp++; 17918140Sralph } 18018140Sralph break; 18118140Sralph } 18218342Sralph return (cp - comp_dn); 18318140Sralph } 18418140Sralph 18518140Sralph /* 18618140Sralph * Search for expanded name from a list of previously compressed names. 18718140Sralph * Return the offset from msg if found or -1. 18818140Sralph */ 18918140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 19018140Sralph char *exp_dn, *msg; 19118140Sralph char **dnptrs, **lastdnptr; 19218140Sralph { 19318140Sralph register char *dn, *cp, **cpp; 19418140Sralph register int n; 19518140Sralph char *sp; 19618140Sralph 19718140Sralph for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 19818140Sralph dn = exp_dn; 19918140Sralph sp = cp = *cpp; 20018140Sralph while (n = *cp++) { 20118140Sralph /* 20218140Sralph * check for indirection 20318140Sralph */ 20418140Sralph switch (n & INDIR_MASK) { 20518140Sralph case 0: /* normal case, n == len */ 20618342Sralph while (--n >= 0) { 20718342Sralph if (*dn == '\\') 20818342Sralph dn++; 20918140Sralph if (*dn++ != *cp++) 21018140Sralph goto next; 21118342Sralph } 21218140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 21318140Sralph return (sp - msg); 21418140Sralph if (n == '.') 21518140Sralph continue; 21618140Sralph goto next; 21718140Sralph 21818140Sralph default: /* illegal type */ 21918140Sralph return (-1); 22018140Sralph 22118140Sralph case INDIR_MASK: /* indirection */ 22218140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 22318140Sralph } 22418140Sralph } 22518140Sralph if (*dn == '\0') 22618140Sralph return (sp - msg); 22718140Sralph next: ; 22818140Sralph } 22918140Sralph return (-1); 23018140Sralph } 23118528Sralph 23218528Sralph /* 23318528Sralph * Routines to insert/extract short/long's. Must account for byte 23418528Sralph * order and non-alignment problems. This code at least has the 23518528Sralph * advantage of being portable. 23618528Sralph */ 23718528Sralph 23818528Sralph u_short 23918528Sralph getshort(msgp) 24018528Sralph char *msgp; 24118528Sralph { 24218528Sralph register u_char *p = (u_char *) msgp; 24318528Sralph 24418528Sralph return ((*p++ << 8) | *p); 24518528Sralph } 24618528Sralph 24718528Sralph u_long 24818528Sralph getlong(msgp) 24918528Sralph char *msgp; 25018528Sralph { 25118528Sralph register u_char *p = (u_char *) msgp; 25218528Sralph 25318528Sralph return ((((((*p++ << 8) | *p++) << 8) | *p++) << 8) | *p); 25418528Sralph } 25518528Sralph 25618528Sralph u_short 25718528Sralph putshort(s, msgp) 25818528Sralph register u_short s; 25918528Sralph register char *msgp; 26018528Sralph { 26118528Sralph 26218528Sralph msgp[1] = s; 26318528Sralph msgp[0] = s >> 8; 26418528Sralph } 26518528Sralph 26618528Sralph u_long 26718528Sralph putlong(l, msgp) 26818528Sralph register u_long l; 26918528Sralph register char *msgp; 27018528Sralph { 27118528Sralph 27218528Sralph msgp[3] = l; 27318528Sralph msgp[2] = (l >>= 8); 27418528Sralph msgp[1] = (l >>= 8); 27518528Sralph msgp[0] = l >> 8; 27618528Sralph } 277