Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* $NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $ */ |
michael@0 | 2 | |
michael@0 | 3 | /* |
michael@0 | 4 | * Copyright (c) 1985, 1993 |
michael@0 | 5 | * The Regents of the University of California. All rights reserved. |
michael@0 | 6 | * |
michael@0 | 7 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | * modification, are permitted provided that the following conditions |
michael@0 | 9 | * are met: |
michael@0 | 10 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * 3. All advertising materials mentioning features or use of this software |
michael@0 | 16 | * must display the following acknowledgement: |
michael@0 | 17 | * This product includes software developed by the University of |
michael@0 | 18 | * California, Berkeley and its contributors. |
michael@0 | 19 | * 4. Neither the name of the University nor the names of its contributors |
michael@0 | 20 | * may be used to endorse or promote products derived from this software |
michael@0 | 21 | * without specific prior written permission. |
michael@0 | 22 | * |
michael@0 | 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
michael@0 | 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
michael@0 | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
michael@0 | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
michael@0 | 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
michael@0 | 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
michael@0 | 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@0 | 33 | * SUCH DAMAGE. |
michael@0 | 34 | */ |
michael@0 | 35 | |
michael@0 | 36 | /* |
michael@0 | 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
michael@0 | 38 | * |
michael@0 | 39 | * Permission to use, copy, modify, and distribute this software for any |
michael@0 | 40 | * purpose with or without fee is hereby granted, provided that the above |
michael@0 | 41 | * copyright notice and this permission notice appear in all copies, and that |
michael@0 | 42 | * the name of Digital Equipment Corporation not be used in advertising or |
michael@0 | 43 | * publicity pertaining to distribution of the document or software without |
michael@0 | 44 | * specific, written prior permission. |
michael@0 | 45 | * |
michael@0 | 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
michael@0 | 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
michael@0 | 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
michael@0 | 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
michael@0 | 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
michael@0 | 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
michael@0 | 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
michael@0 | 53 | * SOFTWARE. |
michael@0 | 54 | */ |
michael@0 | 55 | |
michael@0 | 56 | /* |
michael@0 | 57 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
michael@0 | 58 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
michael@0 | 59 | * |
michael@0 | 60 | * Permission to use, copy, modify, and distribute this software for any |
michael@0 | 61 | * purpose with or without fee is hereby granted, provided that the above |
michael@0 | 62 | * copyright notice and this permission notice appear in all copies. |
michael@0 | 63 | * |
michael@0 | 64 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
michael@0 | 65 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
michael@0 | 66 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
michael@0 | 67 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
michael@0 | 68 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
michael@0 | 69 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
michael@0 | 70 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
michael@0 | 71 | */ |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * This version of this file is derived from Android 2.3 "Gingerbread", |
michael@0 | 75 | * which contains uncredited changes by Android/Google developers. It has |
michael@0 | 76 | * been modified in 2011 for use in the Android build of Mozilla Firefox by |
michael@0 | 77 | * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>, |
michael@0 | 78 | * and Steve Workman <sjhworkman@gmail.com>). |
michael@0 | 79 | * These changes are offered under the same license as the original NetBSD |
michael@0 | 80 | * file, whose copyright and license are unchanged above. |
michael@0 | 81 | */ |
michael@0 | 82 | |
michael@0 | 83 | #define ANDROID_CHANGES 1 |
michael@0 | 84 | #define MOZILLA_NECKO_EXCLUDE_CODE 1 |
michael@0 | 85 | |
michael@0 | 86 | #include <sys/cdefs.h> |
michael@0 | 87 | #if defined(LIBC_SCCS) && !defined(lint) |
michael@0 | 88 | #ifdef notdef |
michael@0 | 89 | static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; |
michael@0 | 90 | 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 | 91 | #else |
michael@0 | 92 | __RCSID("$NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $"); |
michael@0 | 93 | #endif |
michael@0 | 94 | #endif /* LIBC_SCCS and not lint */ |
michael@0 | 95 | |
michael@0 | 96 | #include <sys/types.h> |
michael@0 | 97 | #include <sys/param.h> |
michael@0 | 98 | #include <netinet/in.h> |
michael@0 | 99 | #include "arpa_nameser.h" |
michael@0 | 100 | #include <ctype.h> |
michael@0 | 101 | #ifdef ANDROID_CHANGES |
michael@0 | 102 | #include "resolv_private.h" |
michael@0 | 103 | #else |
michael@0 | 104 | #include <resolv.h> |
michael@0 | 105 | #endif |
michael@0 | 106 | #include <stdio.h> |
michael@0 | 107 | #include <string.h> |
michael@0 | 108 | #include <unistd.h> |
michael@0 | 109 | |
michael@0 | 110 | /* |
michael@0 | 111 | * Expand compressed domain name 'src' to full domain name. |
michael@0 | 112 | * 'msg' is a pointer to the begining of the message, |
michael@0 | 113 | * 'eom' points to the first location after the message, |
michael@0 | 114 | * 'dst' is a pointer to a buffer of size 'dstsiz' for the result. |
michael@0 | 115 | * Return size of compressed name or -1 if there was an error. |
michael@0 | 116 | */ |
michael@0 | 117 | int |
michael@0 | 118 | dn_expand(const u_char *msg, const u_char *eom, const u_char *src, |
michael@0 | 119 | char *dst, int dstsiz) |
michael@0 | 120 | { |
michael@0 | 121 | int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); |
michael@0 | 122 | |
michael@0 | 123 | if (n > 0 && dst[0] == '.') |
michael@0 | 124 | dst[0] = '\0'; |
michael@0 | 125 | return (n); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | /* |
michael@0 | 129 | * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. |
michael@0 | 130 | * Return the size of the compressed name or -1. |
michael@0 | 131 | * 'length' is the size of the array pointed to by 'comp_dn'. |
michael@0 | 132 | */ |
michael@0 | 133 | int |
michael@0 | 134 | dn_comp(const char *src, u_char *dst, int dstsiz, |
michael@0 | 135 | u_char **dnptrs, u_char **lastdnptr) |
michael@0 | 136 | { |
michael@0 | 137 | return (ns_name_compress(src, dst, (size_t)dstsiz, |
michael@0 | 138 | (const u_char **)dnptrs, |
michael@0 | 139 | (const u_char **)lastdnptr)); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /* |
michael@0 | 143 | * Skip over a compressed domain name. Return the size or -1. |
michael@0 | 144 | */ |
michael@0 | 145 | int |
michael@0 | 146 | dn_skipname(const u_char *ptr, const u_char *eom) { |
michael@0 | 147 | const u_char *saveptr = ptr; |
michael@0 | 148 | |
michael@0 | 149 | if (ns_name_skip(&ptr, eom) == -1) |
michael@0 | 150 | return (-1); |
michael@0 | 151 | return (ptr - saveptr); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | * Verify that a domain name uses an acceptable character set. |
michael@0 | 156 | */ |
michael@0 | 157 | |
michael@0 | 158 | /* |
michael@0 | 159 | * Note the conspicuous absence of ctype macros in these definitions. On |
michael@0 | 160 | * non-ASCII hosts, we can't depend on string literals or ctype macros to |
michael@0 | 161 | * tell us anything about network-format data. The rest of the BIND system |
michael@0 | 162 | * is not careful about this, but for some reason, we're doing it right here. |
michael@0 | 163 | */ |
michael@0 | 164 | |
michael@0 | 165 | /* BIONIC: We also accept underscores in the middle of labels. |
michael@0 | 166 | * This extension is needed to make resolution on some VPN networks |
michael@0 | 167 | * work properly. |
michael@0 | 168 | */ |
michael@0 | 169 | |
michael@0 | 170 | #define PERIOD 0x2e |
michael@0 | 171 | #define hyphenchar(c) ((c) == 0x2d) |
michael@0 | 172 | #define bslashchar(c) ((c) == 0x5c) |
michael@0 | 173 | #define periodchar(c) ((c) == PERIOD) |
michael@0 | 174 | #define asterchar(c) ((c) == 0x2a) |
michael@0 | 175 | #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ |
michael@0 | 176 | || ((c) >= 0x61 && (c) <= 0x7a)) |
michael@0 | 177 | #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) |
michael@0 | 178 | #define underscorechar(c) ((c) == 0x5f) |
michael@0 | 179 | |
michael@0 | 180 | #define borderchar(c) (alphachar(c) || digitchar(c)) |
michael@0 | 181 | #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c)) |
michael@0 | 182 | #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) |
michael@0 | 183 | |
michael@0 | 184 | int |
michael@0 | 185 | res_hnok(const char *dn) { |
michael@0 | 186 | int pch = PERIOD, ch = *dn++; |
michael@0 | 187 | |
michael@0 | 188 | while (ch != '\0') { |
michael@0 | 189 | int nch = *dn++; |
michael@0 | 190 | |
michael@0 | 191 | if (periodchar(ch)) { |
michael@0 | 192 | ; |
michael@0 | 193 | } else if (periodchar(pch)) { |
michael@0 | 194 | if (!borderchar(ch)) |
michael@0 | 195 | return (0); |
michael@0 | 196 | } else if (periodchar(nch) || nch == '\0') { |
michael@0 | 197 | if (!borderchar(ch)) |
michael@0 | 198 | return (0); |
michael@0 | 199 | } else { |
michael@0 | 200 | if (!middlechar(ch)) |
michael@0 | 201 | return (0); |
michael@0 | 202 | } |
michael@0 | 203 | pch = ch, ch = nch; |
michael@0 | 204 | } |
michael@0 | 205 | return (1); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | #ifndef MOZILLA_NECKO_EXCLUDE_CODE |
michael@0 | 209 | /* |
michael@0 | 210 | * hostname-like (A, MX, WKS) owners can have "*" as their first label |
michael@0 | 211 | * but must otherwise be as a host name. |
michael@0 | 212 | */ |
michael@0 | 213 | int |
michael@0 | 214 | res_ownok(const char *dn) { |
michael@0 | 215 | if (asterchar(dn[0])) { |
michael@0 | 216 | if (periodchar(dn[1])) |
michael@0 | 217 | return (res_hnok(dn+2)); |
michael@0 | 218 | if (dn[1] == '\0') |
michael@0 | 219 | return (1); |
michael@0 | 220 | } |
michael@0 | 221 | return (res_hnok(dn)); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | /* |
michael@0 | 225 | * SOA RNAMEs and RP RNAMEs can have any printable character in their first |
michael@0 | 226 | * label, but the rest of the name has to look like a host name. |
michael@0 | 227 | */ |
michael@0 | 228 | int |
michael@0 | 229 | res_mailok(const char *dn) { |
michael@0 | 230 | int ch, escaped = 0; |
michael@0 | 231 | |
michael@0 | 232 | /* "." is a valid missing representation */ |
michael@0 | 233 | if (*dn == '\0') |
michael@0 | 234 | return (1); |
michael@0 | 235 | |
michael@0 | 236 | /* otherwise <label>.<hostname> */ |
michael@0 | 237 | while ((ch = *dn++) != '\0') { |
michael@0 | 238 | if (!domainchar(ch)) |
michael@0 | 239 | return (0); |
michael@0 | 240 | if (!escaped && periodchar(ch)) |
michael@0 | 241 | break; |
michael@0 | 242 | if (escaped) |
michael@0 | 243 | escaped = 0; |
michael@0 | 244 | else if (bslashchar(ch)) |
michael@0 | 245 | escaped = 1; |
michael@0 | 246 | } |
michael@0 | 247 | if (periodchar(ch)) |
michael@0 | 248 | return (res_hnok(dn)); |
michael@0 | 249 | return (0); |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | /* |
michael@0 | 253 | * This function is quite liberal, since RFC 1034's character sets are only |
michael@0 | 254 | * recommendations. |
michael@0 | 255 | */ |
michael@0 | 256 | int |
michael@0 | 257 | res_dnok(const char *dn) { |
michael@0 | 258 | int ch; |
michael@0 | 259 | |
michael@0 | 260 | while ((ch = *dn++) != '\0') |
michael@0 | 261 | if (!domainchar(ch)) |
michael@0 | 262 | return (0); |
michael@0 | 263 | return (1); |
michael@0 | 264 | } |
michael@0 | 265 | #endif |
michael@0 | 266 | |
michael@0 | 267 | #ifdef BIND_4_COMPAT |
michael@0 | 268 | /* |
michael@0 | 269 | * This module must export the following externally-visible symbols: |
michael@0 | 270 | * ___putlong |
michael@0 | 271 | * ___putshort |
michael@0 | 272 | * __getlong |
michael@0 | 273 | * __getshort |
michael@0 | 274 | * Note that one _ comes from C and the others come from us. |
michael@0 | 275 | */ |
michael@0 | 276 | void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } |
michael@0 | 277 | void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } |
michael@0 | 278 | #ifndef __ultrix__ |
michael@0 | 279 | u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } |
michael@0 | 280 | u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } |
michael@0 | 281 | #endif /*__ultrix__*/ |
michael@0 | 282 | #endif /*BIND_4_COMPAT*/ |