1/* 2 * ++Copyright++ 1985, 1993 3 * - 4 * Copyright (c) 1985, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * - 35 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies, and that 40 * the name of Digital Equipment Corporation not be used in advertising or 41 * publicity pertaining to distribution of the document or software without 42 * specific, written prior permission. 43 * 44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51 * SOFTWARE. 52 * - 53 * --Copyright-- 54 */ 55 56#if defined(LIBC_SCCS) && !defined(lint) 57static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 58static char rcsid[] = "$Id: res_comp.c,v 1.4 2003/02/18 17:29:24 majka Exp $"; 59#endif /* LIBC_SCCS and not lint */ 60 61#include <sys/param.h> 62#include <netinet/in.h> 63 64#include <stdio.h> 65#include <ctype.h> 66 67#include <arpa/nameser_compat.h> 68#include <nameser.h> 69 70/* 71 * Expand compressed domain name 'comp_dn' to full domain name. 72 * 'msg' is a pointer to the begining of the message, 73 * 'eomorig' points to the first location after the message, 74 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 75 * Return size of compressed name or -1 if there was an error. 76 */ 77int 78dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn, char *exp_dn, int length) 79{ 80 register const u_char *cp; 81 register char *dn; 82 register int n, c; 83 char *eom; 84 int len = -1, checked = 0; 85 86 dn = exp_dn; 87 cp = comp_dn; 88 eom = exp_dn + length; 89 /* 90 * fetch next label in domain name 91 */ 92 while ((n = *cp++)) { 93 /* 94 * Check for indirection 95 */ 96 switch (n & INDIR_MASK) { 97 case 0: 98 if (dn != exp_dn) { 99 if (dn >= eom) 100 return (-1); 101 *dn++ = '.'; 102 } 103 if (dn+n >= eom) 104 return (-1); 105 checked += n + 1; 106 while (--n >= 0) { 107 if ((c = *cp++) == '.') { 108 if (dn + n + 2 >= eom) 109 return (-1); 110 *dn++ = '\\'; 111 } 112 *dn++ = c; 113 if (cp >= eomorig) /* out of range */ 114 return (-1); 115 } 116 break; 117 118 case INDIR_MASK: 119 if (len < 0) 120 len = cp - comp_dn + 1; 121 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 122 if (cp < msg || cp >= eomorig) /* out of range */ 123 return (-1); 124 checked += 2; 125 /* 126 * Check for loops in the compressed name; 127 * if we've looked at the whole message, 128 * there must be a loop. 129 */ 130 if (checked >= eomorig - msg) 131 return (-1); 132 break; 133 134 default: 135 return (-1); /* flag error */ 136 } 137 } 138 *dn = '\0'; 139 for (dn = exp_dn; (c = *dn) != '\0'; dn++) 140 if (isascii(c) && isspace(c)) 141 return (-1); 142 if (len < 0) 143 len = cp - comp_dn; 144 return (len); 145} 146 147/* 148 * Skip over a compressed domain name. Return the size or -1. 149 */ 150int 151__dn_skipname(const u_char *comp_dn, const u_char *eom) 152{ 153 register const u_char *cp; 154 register int n; 155 156 cp = comp_dn; 157 while (cp < eom && (n = *cp++)) { 158 /* 159 * check for indirection 160 */ 161 switch (n & INDIR_MASK) { 162 case 0: /* normal case, n == len */ 163 cp += n; 164 continue; 165 case INDIR_MASK: /* indirection */ 166 cp++; 167 break; 168 default: /* illegal type */ 169 return (-1); 170 } 171 break; 172 } 173 if (cp > eom) 174 return (-1); 175 return (cp - comp_dn); 176} 177 178/* 179 * Routines to insert/extract short/long's. 180 */ 181 182u_int16_t 183_getshort(const u_char *msgp) 184{ 185 u_int16_t u; 186 GETSHORT(u, msgp); 187 return u; 188} 189 190u_int32_t 191_getlong(const u_char *msgp) 192{ 193 u_int32_t u; 194 GETLONG(u, msgp); 195 return u; 196} 197