118546Sralph /* 221384Sdist * Copyright (c) 1985 Regents of the University of California. 321384Sdist * All rights reserved. The Berkeley software License Agreement 421384Sdist * specifies the terms and conditions for redistribution. 518546Sralph */ 618546Sralph 721384Sdist #ifndef lint 8*26240Skjd static char sccsid[] = "@(#)res_comp.c 6.5 (Berkeley) 02/18/86"; 921384Sdist #endif not lint 1021384Sdist 1118140Sralph #include <sys/types.h> 1218140Sralph #include <stdio.h> 1318140Sralph #include <ctype.h> 1424079Skjd #include <arpa/nameser.h> 1518140Sralph 1623870Skjd 1718140Sralph /* 1818342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 1924735Sbloom * Expanded names are converted to lower case. 2018342Sralph * 'msg' is a pointer to the begining of the message, 2126112Skarels * 'eomorig' points to the first location after the message, 2218342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 2318140Sralph * Return size of compressed name or -1 if there was an error. 2418140Sralph */ 2526112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length) 2626112Skarels char *msg, *eomorig, *comp_dn, *exp_dn; 2726112Skarels int length; 2818140Sralph { 2918140Sralph register char *cp, *dn; 3018140Sralph register int n, c; 3126112Skarels char *eom; 3218342Sralph int len = -1; 3318140Sralph 3418140Sralph dn = exp_dn; 3518140Sralph cp = comp_dn; 3618140Sralph eom = exp_dn + length - 1; 3718140Sralph /* 3818140Sralph * fetch next label in domain name 3918140Sralph */ 4018140Sralph while (n = *cp++) { 4118140Sralph /* 4218140Sralph * Check for indirection 4318140Sralph */ 4418140Sralph switch (n & INDIR_MASK) { 4518140Sralph case 0: 4618342Sralph if (dn != exp_dn) { 4718342Sralph if (dn >= eom) 4818342Sralph return (-1); 4918140Sralph *dn++ = '.'; 5018342Sralph } 5118140Sralph if (dn+n >= eom) 5218140Sralph return (-1); 5326064Skjd while (--n >= 0) { 5424735Sbloom if (isupper(c = *cp++)) 5524735Sbloom *dn++ = tolower(c); 5618342Sralph else { 5718342Sralph if (c == '.') { 5818342Sralph if (dn+n+1 >= eom) 5918342Sralph return (-1); 6018342Sralph *dn++ = '\\'; 6118342Sralph } 6218140Sralph *dn++ = c; 6318342Sralph } 6426112Skarels if (cp >= eomorig) /* out of range */ 6526064Skjd return(-1); 6626064Skjd } 6718140Sralph break; 6818140Sralph 6918140Sralph case INDIR_MASK: 7018342Sralph if (len < 0) 7118140Sralph len = cp - comp_dn + 1; 7218140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 7326112Skarels if (cp < msg || cp >= eomorig) /* out of range */ 7426064Skjd return(-1); 7518140Sralph break; 7618140Sralph 7718140Sralph default: 7818140Sralph return (-1); /* flag error */ 7918140Sralph } 8018140Sralph } 8118140Sralph *dn = '\0'; 8218342Sralph if (len < 0) 8318140Sralph len = cp - comp_dn; 8418140Sralph return (len); 8518140Sralph } 8618140Sralph 8718140Sralph /* 8818342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 8918342Sralph * Return the size of the compressed name or -1. 9018342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 9118342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 9218140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 9318342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 9418342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 9518342Sralph * labels inserted into the message as we compress the name. 9618342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 9718342Sralph * is NULL, we don't update the list. 9818140Sralph */ 9918140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 10018140Sralph char *exp_dn, *comp_dn; 10118140Sralph int length; 10218140Sralph char **dnptrs, **lastdnptr; 10318140Sralph { 10418140Sralph register char *cp, *dn; 10518140Sralph register int c, l; 10618140Sralph char **cpp, **lpp, *sp, *eob; 10718140Sralph char *msg; 10818140Sralph 10918140Sralph dn = exp_dn; 11018140Sralph cp = comp_dn; 11125360Sbloom eob = cp + length; 11218140Sralph if (dnptrs != NULL) { 11318140Sralph if ((msg = *dnptrs++) != NULL) { 11418140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 11518140Sralph ; 11618140Sralph lpp = cpp; /* end of list to search */ 11718140Sralph } 11818140Sralph } else 11918140Sralph msg = NULL; 12018140Sralph for (c = *dn++; c != '\0'; ) { 12118140Sralph /* look to see if we can use pointers */ 12218140Sralph if (msg != NULL) { 12318140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 12418140Sralph if (cp+1 >= eob) 12518140Sralph return (-1); 12618140Sralph *cp++ = (l >> 8) | INDIR_MASK; 127*26240Skjd *cp++ = l % 256; 12818140Sralph return (cp - comp_dn); 12918140Sralph } 13018140Sralph /* not found, save it */ 13118140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 13218140Sralph *cpp++ = cp; 13318140Sralph *cpp = NULL; 13418140Sralph } 13518140Sralph } 13618140Sralph sp = cp++; /* save ptr to length byte */ 13718140Sralph do { 13818140Sralph if (c == '.') { 13918140Sralph c = *dn++; 14018140Sralph break; 14118140Sralph } 14218342Sralph if (c == '\\') { 14318342Sralph if ((c = *dn++) == '\0') 14418342Sralph break; 14518342Sralph } 14618140Sralph if (cp >= eob) 14718140Sralph return (-1); 14818140Sralph *cp++ = c; 14918140Sralph } while ((c = *dn++) != '\0'); 15018342Sralph /* catch trailing '.'s but not '..' */ 15118342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 15218342Sralph cp--; 15318342Sralph break; 15418342Sralph } 15518342Sralph if (l <= 0 || l > MAXLABEL) 15618140Sralph return (-1); 15718140Sralph *sp = l; 15818140Sralph } 15918140Sralph if (cp >= eob) 16018140Sralph return (-1); 16118140Sralph *cp++ = '\0'; 16218140Sralph return (cp - comp_dn); 16318140Sralph } 16418140Sralph 16518140Sralph /* 16618342Sralph * Skip over a compressed domain name. Return the size or -1. 16718140Sralph */ 16818342Sralph dn_skip(comp_dn) 16918342Sralph char *comp_dn; 17018140Sralph { 17118140Sralph register char *cp; 17218140Sralph register int n; 17318140Sralph 17418342Sralph cp = comp_dn; 17518140Sralph while (n = *cp++) { 17618140Sralph /* 17718140Sralph * check for indirection 17818140Sralph */ 17918140Sralph switch (n & INDIR_MASK) { 18018140Sralph case 0: /* normal case, n == len */ 18118140Sralph cp += n; 18218140Sralph continue; 18318140Sralph default: /* illegal type */ 18418140Sralph return (-1); 18518140Sralph case INDIR_MASK: /* indirection */ 18618140Sralph cp++; 18718140Sralph } 18818140Sralph break; 18918140Sralph } 19018342Sralph return (cp - comp_dn); 19118140Sralph } 19218140Sralph 19318140Sralph /* 19418140Sralph * Search for expanded name from a list of previously compressed names. 19518140Sralph * Return the offset from msg if found or -1. 19618140Sralph */ 19718140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 19818140Sralph char *exp_dn, *msg; 19918140Sralph char **dnptrs, **lastdnptr; 20018140Sralph { 20118140Sralph register char *dn, *cp, **cpp; 20218140Sralph register int n; 20318140Sralph char *sp; 20418140Sralph 205*26240Skjd for (cpp = dnptrs + 1; cpp < lastdnptr; cpp++) { 20618140Sralph dn = exp_dn; 20718140Sralph sp = cp = *cpp; 20818140Sralph while (n = *cp++) { 20918140Sralph /* 21018140Sralph * check for indirection 21118140Sralph */ 21218140Sralph switch (n & INDIR_MASK) { 21318140Sralph case 0: /* normal case, n == len */ 21418342Sralph while (--n >= 0) { 21518342Sralph if (*dn == '\\') 21618342Sralph dn++; 21718140Sralph if (*dn++ != *cp++) 21818140Sralph goto next; 21918342Sralph } 22018140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 22118140Sralph return (sp - msg); 22218140Sralph if (n == '.') 22318140Sralph continue; 22418140Sralph goto next; 22518140Sralph 22618140Sralph default: /* illegal type */ 22718140Sralph return (-1); 22818140Sralph 22918140Sralph case INDIR_MASK: /* indirection */ 23018140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 23118140Sralph } 23218140Sralph } 23318140Sralph if (*dn == '\0') 23418140Sralph return (sp - msg); 23518140Sralph next: ; 23618140Sralph } 23718140Sralph return (-1); 23818140Sralph } 23918528Sralph 24018528Sralph /* 24118528Sralph * Routines to insert/extract short/long's. Must account for byte 24218528Sralph * order and non-alignment problems. This code at least has the 24318528Sralph * advantage of being portable. 24418528Sralph */ 24518528Sralph 24618528Sralph u_short 24718528Sralph getshort(msgp) 24818528Sralph char *msgp; 24918528Sralph { 25018528Sralph register u_char *p = (u_char *) msgp; 25125360Sbloom #ifdef vax 25225360Sbloom /* 25325360Sbloom * vax compiler doesn't put shorts in registers 25425360Sbloom */ 25525360Sbloom register u_long u; 25625360Sbloom #else 25724735Sbloom register u_short u; 25825360Sbloom #endif 25918528Sralph 26024735Sbloom u = *p++ << 8; 26125360Sbloom return ((u_short)(u | *p)); 26218528Sralph } 26318528Sralph 26418528Sralph u_long 26518528Sralph getlong(msgp) 26618528Sralph char *msgp; 26718528Sralph { 26818528Sralph register u_char *p = (u_char *) msgp; 26923870Skjd register u_long u; 27018528Sralph 27123870Skjd u = *p++; u <<= 8; 27223870Skjd u |= *p++; u <<= 8; 27323870Skjd u |= *p++; u <<= 8; 27423870Skjd return (u | *p); 27518528Sralph } 27618528Sralph 27723870Skjd 27818528Sralph putshort(s, msgp) 27918528Sralph register u_short s; 28018528Sralph register char *msgp; 28118528Sralph { 28218528Sralph 28318528Sralph msgp[1] = s; 28418528Sralph msgp[0] = s >> 8; 28518528Sralph } 28618528Sralph 28718528Sralph putlong(l, msgp) 28818528Sralph register u_long l; 28918528Sralph register char *msgp; 29018528Sralph { 29118528Sralph 29218528Sralph msgp[3] = l; 29318528Sralph msgp[2] = (l >>= 8); 29418528Sralph msgp[1] = (l >>= 8); 29518528Sralph msgp[0] = l >> 8; 29618528Sralph } 297