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*24079Skjd static char sccsid[] = "@(#)res_comp.c 5.3 (Berkeley) 07/29/85"; 921384Sdist #endif not lint 1021384Sdist 1118140Sralph #include <sys/types.h> 1218140Sralph #include <stdio.h> 1318140Sralph #include <ctype.h> 14*24079Skjd #include <arpa/nameser.h> 1518140Sralph 1623870Skjd 1718140Sralph /* 1818342Sralph * Expand compressed domain name 'comp_dn' to full domain name. 1918342Sralph * Expanded names are converted to upper case. 2018342Sralph * 'msg' is a pointer to the begining of the message, 2118342Sralph * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 2218140Sralph * Return size of compressed name or -1 if there was an error. 2318140Sralph */ 2418140Sralph dn_expand(msg, comp_dn, exp_dn, length) 2518140Sralph char *msg, *comp_dn, *exp_dn; 2618140Sralph int length; 2718140Sralph { 2818140Sralph register char *cp, *dn; 2918140Sralph register int n, c; 3018140Sralph char *eom; 3118342Sralph int len = -1; 3218140Sralph 3318140Sralph dn = exp_dn; 3418140Sralph cp = comp_dn; 3518140Sralph eom = exp_dn + length - 1; 3618140Sralph /* 3718140Sralph * fetch next label in domain name 3818140Sralph */ 3918140Sralph while (n = *cp++) { 4018140Sralph /* 4118140Sralph * Check for indirection 4218140Sralph */ 4318140Sralph switch (n & INDIR_MASK) { 4418140Sralph case 0: 4518342Sralph if (dn != exp_dn) { 4618342Sralph if (dn >= eom) 4718342Sralph return (-1); 4818140Sralph *dn++ = '.'; 4918342Sralph } 5018140Sralph if (dn+n >= eom) 5118140Sralph return (-1); 5218140Sralph while (--n >= 0) 5318140Sralph if (islower(c = *cp++)) 5418140Sralph *dn++ = toupper(c); 5518342Sralph else { 5618342Sralph if (c == '.') { 5718342Sralph if (dn+n+1 >= eom) 5818342Sralph return (-1); 5918342Sralph *dn++ = '\\'; 6018342Sralph } 6118140Sralph *dn++ = c; 6218342Sralph } 6318140Sralph break; 6418140Sralph 6518140Sralph case INDIR_MASK: 6618342Sralph if (len < 0) 6718140Sralph len = cp - comp_dn + 1; 6818140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 6918140Sralph break; 7018140Sralph 7118140Sralph default: 7218140Sralph return (-1); /* flag error */ 7318140Sralph } 7418140Sralph } 7518140Sralph *dn = '\0'; 7618342Sralph if (len < 0) 7718140Sralph len = cp - comp_dn; 7818140Sralph return (len); 7918140Sralph } 8018140Sralph 8118140Sralph /* 8218342Sralph * Compress domain name 'exp_dn' into 'comp_dn'. 8318342Sralph * Return the size of the compressed name or -1. 8418342Sralph * 'length' is the size of the array pointed to by 'comp_dn'. 8518342Sralph * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] 8618140Sralph * is a pointer to the beginning of the message. The list ends with NULL. 8718342Sralph * 'lastdnptr' is a pointer to the end of the arrary pointed to 8818342Sralph * by 'dnptrs'. Side effect is to update the list of pointers for 8918342Sralph * labels inserted into the message as we compress the name. 9018342Sralph * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 9118342Sralph * is NULL, we don't update the list. 9218140Sralph */ 9318140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 9418140Sralph char *exp_dn, *comp_dn; 9518140Sralph int length; 9618140Sralph char **dnptrs, **lastdnptr; 9718140Sralph { 9818140Sralph register char *cp, *dn; 9918140Sralph register int c, l; 10018140Sralph char **cpp, **lpp, *sp, *eob; 10118140Sralph char *msg; 10218140Sralph 10318140Sralph dn = exp_dn; 10418140Sralph cp = comp_dn; 10518140Sralph eob = comp_dn + length; 10618140Sralph if (dnptrs != NULL) { 10718140Sralph if ((msg = *dnptrs++) != NULL) { 10818140Sralph for (cpp = dnptrs; *cpp != NULL; cpp++) 10918140Sralph ; 11018140Sralph lpp = cpp; /* end of list to search */ 11118140Sralph } 11218140Sralph } else 11318140Sralph msg = NULL; 11418140Sralph for (c = *dn++; c != '\0'; ) { 11518140Sralph /* look to see if we can use pointers */ 11618140Sralph if (msg != NULL) { 11718140Sralph if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { 11818140Sralph if (cp+1 >= eob) 11918140Sralph return (-1); 12018140Sralph *cp++ = (l >> 8) | INDIR_MASK; 12118140Sralph *cp++ = l; 12218140Sralph return (cp - comp_dn); 12318140Sralph } 12418140Sralph /* not found, save it */ 12518140Sralph if (lastdnptr != NULL && cpp < lastdnptr-1) { 12618140Sralph *cpp++ = cp; 12718140Sralph *cpp = NULL; 12818140Sralph } 12918140Sralph } 13018140Sralph sp = cp++; /* save ptr to length byte */ 13118140Sralph do { 13218140Sralph if (c == '.') { 13318140Sralph c = *dn++; 13418140Sralph break; 13518140Sralph } 13618342Sralph if (c == '\\') { 13718342Sralph if ((c = *dn++) == '\0') 13818342Sralph break; 13918342Sralph } 14018140Sralph if (cp >= eob) 14118140Sralph return (-1); 14218140Sralph *cp++ = c; 14318140Sralph } while ((c = *dn++) != '\0'); 14418342Sralph /* catch trailing '.'s but not '..' */ 14518342Sralph if ((l = cp - sp - 1) == 0 && c == '\0') { 14618342Sralph cp--; 14718342Sralph break; 14818342Sralph } 14918342Sralph if (l <= 0 || l > MAXLABEL) 15018140Sralph return (-1); 15118140Sralph *sp = l; 15218140Sralph } 15318140Sralph if (cp >= eob) 15418140Sralph return (-1); 15518140Sralph *cp++ = '\0'; 15618140Sralph return (cp - comp_dn); 15718140Sralph } 15818140Sralph 15918140Sralph /* 16018342Sralph * Skip over a compressed domain name. Return the size or -1. 16118140Sralph */ 16218342Sralph dn_skip(comp_dn) 16318342Sralph char *comp_dn; 16418140Sralph { 16518140Sralph register char *cp; 16618140Sralph register int n; 16718140Sralph 16818342Sralph cp = comp_dn; 16918140Sralph while (n = *cp++) { 17018140Sralph /* 17118140Sralph * check for indirection 17218140Sralph */ 17318140Sralph switch (n & INDIR_MASK) { 17418140Sralph case 0: /* normal case, n == len */ 17518140Sralph cp += n; 17618140Sralph continue; 17718140Sralph default: /* illegal type */ 17818140Sralph return (-1); 17918140Sralph case INDIR_MASK: /* indirection */ 18018140Sralph cp++; 18118140Sralph } 18218140Sralph break; 18318140Sralph } 18418342Sralph return (cp - comp_dn); 18518140Sralph } 18618140Sralph 18718140Sralph /* 18818140Sralph * Search for expanded name from a list of previously compressed names. 18918140Sralph * Return the offset from msg if found or -1. 19018140Sralph */ 19118140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr) 19218140Sralph char *exp_dn, *msg; 19318140Sralph char **dnptrs, **lastdnptr; 19418140Sralph { 19518140Sralph register char *dn, *cp, **cpp; 19618140Sralph register int n; 19718140Sralph char *sp; 19818140Sralph 19918140Sralph for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 20018140Sralph dn = exp_dn; 20118140Sralph sp = cp = *cpp; 20218140Sralph while (n = *cp++) { 20318140Sralph /* 20418140Sralph * check for indirection 20518140Sralph */ 20618140Sralph switch (n & INDIR_MASK) { 20718140Sralph case 0: /* normal case, n == len */ 20818342Sralph while (--n >= 0) { 20918342Sralph if (*dn == '\\') 21018342Sralph dn++; 21118140Sralph if (*dn++ != *cp++) 21218140Sralph goto next; 21318342Sralph } 21418140Sralph if ((n = *dn++) == '\0' && *cp == '\0') 21518140Sralph return (sp - msg); 21618140Sralph if (n == '.') 21718140Sralph continue; 21818140Sralph goto next; 21918140Sralph 22018140Sralph default: /* illegal type */ 22118140Sralph return (-1); 22218140Sralph 22318140Sralph case INDIR_MASK: /* indirection */ 22418140Sralph cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 22518140Sralph } 22618140Sralph } 22718140Sralph if (*dn == '\0') 22818140Sralph return (sp - msg); 22918140Sralph next: ; 23018140Sralph } 23118140Sralph return (-1); 23218140Sralph } 23318528Sralph 23418528Sralph /* 23518528Sralph * Routines to insert/extract short/long's. Must account for byte 23618528Sralph * order and non-alignment problems. This code at least has the 23718528Sralph * advantage of being portable. 23818528Sralph */ 23918528Sralph 24018528Sralph u_short 24118528Sralph getshort(msgp) 24218528Sralph char *msgp; 24318528Sralph { 24418528Sralph register u_char *p = (u_char *) msgp; 24518528Sralph 24618528Sralph return ((*p++ << 8) | *p); 24718528Sralph } 24818528Sralph 24918528Sralph u_long 25018528Sralph getlong(msgp) 25118528Sralph char *msgp; 25218528Sralph { 25318528Sralph register u_char *p = (u_char *) msgp; 25423870Skjd register u_long u; 25518528Sralph 25623870Skjd u = *p++; u <<= 8; 25723870Skjd u |= *p++; u <<= 8; 25823870Skjd u |= *p++; u <<= 8; 25923870Skjd return (u | *p); 26018528Sralph } 26118528Sralph 26223870Skjd 26318528Sralph putshort(s, msgp) 26418528Sralph register u_short s; 26518528Sralph register char *msgp; 26618528Sralph { 26718528Sralph 26818528Sralph msgp[1] = s; 26918528Sralph msgp[0] = s >> 8; 27018528Sralph } 27118528Sralph 27218528Sralph putlong(l, msgp) 27318528Sralph register u_long l; 27418528Sralph register char *msgp; 27518528Sralph { 27618528Sralph 27718528Sralph msgp[3] = l; 27818528Sralph msgp[2] = (l >>= 8); 27918528Sralph msgp[1] = (l >>= 8); 28018528Sralph msgp[0] = l >> 8; 28118528Sralph } 282