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 726631Sdonn #if defined(LIBC_SCCS) && !defined(lint) 8*33617Skarels static char sccsid[] = "@(#)res_comp.c 6.11 (Berkeley) 02/28/88"; 926631Sdonn #endif LIBC_SCCS and not lint 1021384Sdist 1118140Sralph #include <sys/types.h> 1218140Sralph #include <stdio.h> 1324079Skjd #include <arpa/nameser.h> 1418140Sralph 1523870Skjd 1618140Sralph /* 1718342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 1818342Sralph * 'msg' is a pointer to the begining of the message, 1926112Skarels * 'eomorig' points to the first location after the message, 2018342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 2118140Sralph * Return size of compressed name or -1 if there was an error. 2218140Sralph */ 2326112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length) 2432647Skarels u_char *msg, *eomorig, *comp_dn, *exp_dn; 2526112Skarels int length; 2618140Sralph { 2732647Skarels register u_char *cp, *dn; 2818140Sralph register int n, c; 2932647Skarels u_char *eom; 3031686Skarels int len = -1, checked = 0; 3118140Sralph 3218140Sralph dn = exp_dn; 3318140Sralph cp = comp_dn; 3418140Sralph eom = exp_dn + length - 1; 3518140Sralph /* 3618140Sralph * fetch next label in domain name 3718140Sralph */ 3818140Sralph while (n = *cp++) { 3918140Sralph /* 4018140Sralph * Check for indirection 4118140Sralph */ 4218140Sralph switch (n & INDIR_MASK) { 4318140Sralph case 0: 4418342Sralph if (dn != exp_dn) { 4518342Sralph if (dn >= eom) 4618342Sralph return (-1); 4718140Sralph *dn++ = '.'; 4818342Sralph } 4918140Sralph if (dn+n >= eom) 5018140Sralph return (-1); 5131686Skarels checked += n + 1; 5226064Skjd while (--n >= 0) { 5326812Sbloom if ((c = *cp++) == '.') { 5426812Sbloom if (dn+n+1 >= eom) 5526812Sbloom return (-1); 5626812Sbloom *dn++ = '\\'; 5718342Sralph } 5826812Sbloom *dn++ = c; 5926112Skarels if (cp >= eomorig) /* out of range */ 6026064Skjd return(-1); 6126064Skjd } 6218140Sralph break; 6318140Sralph 6418140Sralph case INDIR_MASK: 6518342Sralph if (len < 0) 6618140Sralph len = cp - comp_dn + 1; 6718140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 6826112Skarels if (cp < msg || cp >= eomorig) /* out of range */ 6926064Skjd return(-1); 7031686Skarels checked += 2; 7131686Skarels /* 7231686Skarels * Check for loops in the compressed name; 7331686Skarels * if we've looked at the whole message, 7431686Skarels * there must be a loop. 7531686Skarels */ 7631686Skarels if (checked >= eomorig - msg) 7731686Skarels return (-1); 7818140Sralph break; 7918140Sralph 8018140Sralph default: 8118140Sralph return (-1); /* flag error */ 8218140Sralph } 8318140Sralph } 8418140Sralph *dn = '\0'; 8518342Sralph if (len < 0) 8618140Sralph len = cp - comp_dn; 8718140Sralph return (len); 8818140Sralph } 8918140Sralph 9018140Sralph /* 9118342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 9218342Sralph * Return the size of the compressed name or -1. 9318342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 9418342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 9518140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 9618342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 9718342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 9818342Sralph * labels inserted into the message as we compress the name. 9918342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 10018342Sralph * is NULL, we don't update the list. 10118140Sralph */ 10218140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 10332647Skarels u_char *exp_dn, *comp_dn; 10418140Sralph int length; 10532647Skarels u_char **dnptrs, **lastdnptr; 10618140Sralph { 10732647Skarels register u_char *cp, *dn; 10818140Sralph register int c, l; 10932647Skarels u_char **cpp, **lpp, *sp, *eob; 11032647Skarels u_char *msg; 11118140Sralph 11218140Sralph dn = exp_dn; 11318140Sralph cp = comp_dn; 11425360Sbloom eob = cp + length; 11518140Sralph if (dnptrs != NULL) { 11618140Sralph if ((msg = *dnptrs++) != NULL) { 11718140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 11818140Sralph ; 11918140Sralph lpp = cpp; /* end of list to search */ 12018140Sralph } 12118140Sralph } else 12218140Sralph msg = NULL; 12318140Sralph for (c = *dn++; c != '\0'; ) { 12418140Sralph /* look to see if we can use pointers */ 12518140Sralph if (msg != NULL) { 12618140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 12718140Sralph if (cp+1 >= eob) 12818140Sralph return (-1); 12918140Sralph *cp++ = (l >> 8) | INDIR_MASK; 13026240Skjd *cp++ = l % 256; 13118140Sralph return (cp - comp_dn); 13218140Sralph } 13318140Sralph /* not found, save it */ 13418140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 13518140Sralph *cpp++ = cp; 13618140Sralph *cpp = NULL; 13718140Sralph } 13818140Sralph } 13918140Sralph sp = cp++; /* save ptr to length byte */ 14018140Sralph do { 14118140Sralph if (c == '.') { 14218140Sralph c = *dn++; 14318140Sralph break; 14418140Sralph } 14518342Sralph if (c == '\\') { 14618342Sralph if ((c = *dn++) == '\0') 14718342Sralph break; 14818342Sralph } 14918140Sralph if (cp >= eob) 15018140Sralph return (-1); 15118140Sralph *cp++ = c; 15218140Sralph } while ((c = *dn++) != '\0'); 15318342Sralph /* catch trailing '.'s but not '..' */ 15418342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 15518342Sralph cp--; 15618342Sralph break; 15718342Sralph } 15818342Sralph if (l <= 0 || l > MAXLABEL) 15918140Sralph return (-1); 16018140Sralph *sp = l; 16118140Sralph } 16218140Sralph if (cp >= eob) 16318140Sralph return (-1); 16418140Sralph *cp++ = '\0'; 16518140Sralph return (cp - comp_dn); 16618140Sralph } 16718140Sralph 16818140Sralph /* 16918342Sralph * Skip over a compressed domain name. Return the size or -1. 17018140Sralph */ 17132647Skarels dn_skipname(comp_dn, eom) 17232647Skarels u_char *comp_dn, *eom; 17318140Sralph { 17432647Skarels register u_char *cp; 17518140Sralph register int n; 17618140Sralph 17718342Sralph cp = comp_dn; 17832647Skarels while (cp < eom && (n = *cp++)) { 17918140Sralph /* 18018140Sralph * check for indirection 18118140Sralph */ 18218140Sralph switch (n & INDIR_MASK) { 18318140Sralph case 0: /* normal case, n == len */ 18418140Sralph cp += n; 18518140Sralph continue; 18618140Sralph default: /* illegal type */ 18718140Sralph return (-1); 18818140Sralph case INDIR_MASK: /* indirection */ 18918140Sralph cp++; 19018140Sralph } 19118140Sralph break; 19218140Sralph } 19318342Sralph return (cp - comp_dn); 19418140Sralph } 19518140Sralph 19618140Sralph /* 19718140Sralph * Search for expanded name from a list of previously compressed names. 19818140Sralph * Return the offset from msg if found or -1. 199*33617Skarels * dnptrs is the pointer to the first name on the list, 200*33617Skarels * not the pointer to the start of the message. 20118140Sralph */ 202*33617Skarels static 20318140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 20432647Skarels u_char *exp_dn, *msg; 20532647Skarels u_char **dnptrs, **lastdnptr; 20618140Sralph { 20732647Skarels register u_char *dn, *cp, **cpp; 20818140Sralph register int n; 20932647Skarels u_char *sp; 21018140Sralph 211*33617Skarels for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 21218140Sralph dn = exp_dn; 21318140Sralph sp = cp = *cpp; 21418140Sralph while (n = *cp++) { 21518140Sralph /* 21618140Sralph * check for indirection 21718140Sralph */ 21818140Sralph switch (n & INDIR_MASK) { 21918140Sralph case 0: /* normal case, n == len */ 22018342Sralph while (--n >= 0) { 22118342Sralph if (*dn == '\\') 22218342Sralph dn++; 22318140Sralph if (*dn++ != *cp++) 22418140Sralph goto next; 22518342Sralph } 22618140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 22718140Sralph return (sp - msg); 22818140Sralph if (n == '.') 22918140Sralph continue; 23018140Sralph goto next; 23118140Sralph 23218140Sralph default: /* illegal type */ 23318140Sralph return (-1); 23418140Sralph 23518140Sralph case INDIR_MASK: /* indirection */ 23632647Skarels cp = msg + (((n & 0x3f) << 8) | *cp); 23718140Sralph } 23818140Sralph } 23918140Sralph if (*dn == '\0') 24018140Sralph return (sp - msg); 24118140Sralph next: ; 24218140Sralph } 24318140Sralph return (-1); 24418140Sralph } 24518528Sralph 24618528Sralph /* 24718528Sralph * Routines to insert/extract short/long's. Must account for byte 24818528Sralph * order and non-alignment problems. This code at least has the 24918528Sralph * advantage of being portable. 25018528Sralph */ 25118528Sralph 25218528Sralph u_short 25330441Skjd _getshort(msgp) 25432647Skarels u_char *msgp; 25518528Sralph { 25618528Sralph register u_char *p = (u_char *) msgp; 25725360Sbloom #ifdef vax 25825360Sbloom /* 25925360Sbloom * vax compiler doesn't put shorts in registers 26025360Sbloom */ 26125360Sbloom register u_long u; 26225360Sbloom #else 26324735Sbloom register u_short u; 26425360Sbloom #endif 26518528Sralph 26624735Sbloom u = *p++ << 8; 26725360Sbloom return ((u_short)(u | *p)); 26818528Sralph } 26918528Sralph 27018528Sralph u_long 27130441Skjd _getlong(msgp) 27232647Skarels u_char *msgp; 27318528Sralph { 27418528Sralph register u_char *p = (u_char *) msgp; 27523870Skjd register u_long u; 27618528Sralph 27723870Skjd u = *p++; u <<= 8; 27823870Skjd u |= *p++; u <<= 8; 27923870Skjd u |= *p++; u <<= 8; 28023870Skjd return (u | *p); 28118528Sralph } 28218528Sralph 28323870Skjd 28418528Sralph putshort(s, msgp) 28518528Sralph register u_short s; 28632647Skarels register u_char *msgp; 28718528Sralph { 28818528Sralph 28918528Sralph msgp[1] = s; 29018528Sralph msgp[0] = s >> 8; 29118528Sralph } 29218528Sralph 29318528Sralph putlong(l, msgp) 29418528Sralph register u_long l; 29532647Skarels register u_char *msgp; 29618528Sralph { 29718528Sralph 29818528Sralph msgp[3] = l; 29918528Sralph msgp[2] = (l >>= 8); 30018528Sralph msgp[1] = (l >>= 8); 30118528Sralph msgp[0] = l >> 8; 30218528Sralph } 303