michael@0: /* $NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 1985, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. All advertising materials mentioning features or use of this software michael@0: * must display the following acknowledgement: michael@0: * This product includes software developed by the University of michael@0: * California, Berkeley and its contributors. michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* michael@0: * Portions Copyright (c) 1993 by Digital Equipment Corporation. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies, and that michael@0: * the name of Digital Equipment Corporation not be used in advertising or michael@0: * publicity pertaining to distribution of the document or software without michael@0: * specific, written prior permission. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL michael@0: * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT michael@0: * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL michael@0: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR michael@0: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS michael@0: * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS michael@0: * SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Portions Copyright (c) 1996-1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: #ifdef notdef michael@0: static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; michael@0: static const char rcsid[] = "Id: res_comp.c,v 1.1.2.1.4.1 2004/03/09 08:33:54 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $"); michael@0: #endif michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: #include michael@0: #ifdef ANDROID_CHANGES michael@0: #include "resolv_private.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: * Expand compressed domain name 'src' to full domain name. michael@0: * 'msg' is a pointer to the begining of the message, michael@0: * 'eom' points to the first location after the message, michael@0: * 'dst' is a pointer to a buffer of size 'dstsiz' for the result. michael@0: * Return size of compressed name or -1 if there was an error. michael@0: */ michael@0: int michael@0: dn_expand(const u_char *msg, const u_char *eom, const u_char *src, michael@0: char *dst, int dstsiz) michael@0: { michael@0: int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); michael@0: michael@0: if (n > 0 && dst[0] == '.') michael@0: dst[0] = '\0'; michael@0: return (n); michael@0: } michael@0: michael@0: /* michael@0: * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. michael@0: * Return the size of the compressed name or -1. michael@0: * 'length' is the size of the array pointed to by 'comp_dn'. michael@0: */ michael@0: int michael@0: dn_comp(const char *src, u_char *dst, int dstsiz, michael@0: u_char **dnptrs, u_char **lastdnptr) michael@0: { michael@0: return (ns_name_compress(src, dst, (size_t)dstsiz, michael@0: (const u_char **)dnptrs, michael@0: (const u_char **)lastdnptr)); michael@0: } michael@0: michael@0: /* michael@0: * Skip over a compressed domain name. Return the size or -1. michael@0: */ michael@0: int michael@0: dn_skipname(const u_char *ptr, const u_char *eom) { michael@0: const u_char *saveptr = ptr; michael@0: michael@0: if (ns_name_skip(&ptr, eom) == -1) michael@0: return (-1); michael@0: return (ptr - saveptr); michael@0: } michael@0: michael@0: /* michael@0: * Verify that a domain name uses an acceptable character set. michael@0: */ michael@0: michael@0: /* michael@0: * Note the conspicuous absence of ctype macros in these definitions. On michael@0: * non-ASCII hosts, we can't depend on string literals or ctype macros to michael@0: * tell us anything about network-format data. The rest of the BIND system michael@0: * is not careful about this, but for some reason, we're doing it right here. michael@0: */ michael@0: michael@0: /* BIONIC: We also accept underscores in the middle of labels. michael@0: * This extension is needed to make resolution on some VPN networks michael@0: * work properly. michael@0: */ michael@0: michael@0: #define PERIOD 0x2e michael@0: #define hyphenchar(c) ((c) == 0x2d) michael@0: #define bslashchar(c) ((c) == 0x5c) michael@0: #define periodchar(c) ((c) == PERIOD) michael@0: #define asterchar(c) ((c) == 0x2a) michael@0: #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ michael@0: || ((c) >= 0x61 && (c) <= 0x7a)) michael@0: #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) michael@0: #define underscorechar(c) ((c) == 0x5f) michael@0: michael@0: #define borderchar(c) (alphachar(c) || digitchar(c)) michael@0: #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c)) michael@0: #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) michael@0: michael@0: int michael@0: res_hnok(const char *dn) { michael@0: int pch = PERIOD, ch = *dn++; michael@0: michael@0: while (ch != '\0') { michael@0: int nch = *dn++; michael@0: michael@0: if (periodchar(ch)) { michael@0: ; michael@0: } else if (periodchar(pch)) { michael@0: if (!borderchar(ch)) michael@0: return (0); michael@0: } else if (periodchar(nch) || nch == '\0') { michael@0: if (!borderchar(ch)) michael@0: return (0); michael@0: } else { michael@0: if (!middlechar(ch)) michael@0: return (0); michael@0: } michael@0: pch = ch, ch = nch; michael@0: } michael@0: return (1); michael@0: } michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: /* michael@0: * hostname-like (A, MX, WKS) owners can have "*" as their first label michael@0: * but must otherwise be as a host name. michael@0: */ michael@0: int michael@0: res_ownok(const char *dn) { michael@0: if (asterchar(dn[0])) { michael@0: if (periodchar(dn[1])) michael@0: return (res_hnok(dn+2)); michael@0: if (dn[1] == '\0') michael@0: return (1); michael@0: } michael@0: return (res_hnok(dn)); michael@0: } michael@0: michael@0: /* michael@0: * SOA RNAMEs and RP RNAMEs can have any printable character in their first michael@0: * label, but the rest of the name has to look like a host name. michael@0: */ michael@0: int michael@0: res_mailok(const char *dn) { michael@0: int ch, escaped = 0; michael@0: michael@0: /* "." is a valid missing representation */ michael@0: if (*dn == '\0') michael@0: return (1); michael@0: michael@0: /* otherwise