other-licenses/android/res_debug.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* $NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $ */
michael@0 2
michael@0 3 /*
michael@0 4 * Copyright (c) 1985
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 * Portions Copyright (c) 1995 by International Business Machines, Inc.
michael@0 58 *
michael@0 59 * International Business Machines, Inc. (hereinafter called IBM) grants
michael@0 60 * permission under its copyrights to use, copy, modify, and distribute this
michael@0 61 * Software with or without fee, provided that the above copyright notice and
michael@0 62 * all paragraphs of this notice appear in all copies, and that the name of IBM
michael@0 63 * not be used in connection with the marketing of any product incorporating
michael@0 64 * the Software or modifications thereof, without specific, written prior
michael@0 65 * permission.
michael@0 66 *
michael@0 67 * To the extent it has a right to do so, IBM grants an immunity from suit
michael@0 68 * under its patents, if any, for the use, sale or manufacture of products to
michael@0 69 * the extent that such products are used for performing Domain Name System
michael@0 70 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
michael@0 71 * granted for any product per se or for any other function of any product.
michael@0 72 *
michael@0 73 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
michael@0 74 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
michael@0 75 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
michael@0 76 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
michael@0 77 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
michael@0 78 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
michael@0 79 */
michael@0 80
michael@0 81 /*
michael@0 82 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
michael@0 83 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
michael@0 84 *
michael@0 85 * Permission to use, copy, modify, and distribute this software for any
michael@0 86 * purpose with or without fee is hereby granted, provided that the above
michael@0 87 * copyright notice and this permission notice appear in all copies.
michael@0 88 *
michael@0 89 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
michael@0 90 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
michael@0 91 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
michael@0 92 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
michael@0 93 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
michael@0 94 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
michael@0 95 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
michael@0 96 */
michael@0 97
michael@0 98 /*
michael@0 99 * This version of this file is derived from Android 2.3 "Gingerbread",
michael@0 100 * which contains uncredited changes by Android/Google developers. It has
michael@0 101 * been modified in 2011 for use in the Android build of Mozilla Firefox by
michael@0 102 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
michael@0 103 * and Steve Workman <sjhworkman@gmail.com>).
michael@0 104 * These changes are offered under the same license as the original NetBSD
michael@0 105 * file, whose copyright and license are unchanged above.
michael@0 106 */
michael@0 107
michael@0 108 #define ANDROID_CHANGES 1
michael@0 109 #define MOZILLA_NECKO_EXCLUDE_CODE 1
michael@0 110
michael@0 111 #include <sys/cdefs.h>
michael@0 112 #if defined(LIBC_SCCS) && !defined(lint)
michael@0 113 #ifdef notdef
michael@0 114 static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
michael@0 115 static const char rcsid[] = "Id: res_debug.c,v 1.3.2.5.4.5 2004/07/28 20:16:46 marka Exp";
michael@0 116 #else
michael@0 117 __RCSID("$NetBSD: res_debug.c,v 1.7 2004/11/07 02:25:01 christos Exp $");
michael@0 118 #endif
michael@0 119 #endif /* LIBC_SCCS and not lint */
michael@0 120
michael@0 121
michael@0 122
michael@0 123 #include <sys/types.h>
michael@0 124 #include <sys/param.h>
michael@0 125 #include <sys/socket.h>
michael@0 126
michael@0 127 #include <netinet/in.h>
michael@0 128 #include <arpa/inet.h>
michael@0 129 #include "arpa_nameser.h"
michael@0 130
michael@0 131 #include <ctype.h>
michael@0 132 #include <errno.h>
michael@0 133 #include <math.h>
michael@0 134 #include <netdb.h>
michael@0 135 #include "resolv_private.h"
michael@0 136 #include <stdio.h>
michael@0 137 #include <stdlib.h>
michael@0 138 #include <string.h>
michael@0 139 #include <time.h>
michael@0 140
michael@0 141
michael@0 142
michael@0 143 #ifdef SPRINTF_CHAR
michael@0 144 # define SPRINTF(x) strlen(sprintf/**/x)
michael@0 145 #else
michael@0 146 # define SPRINTF(x) sprintf x
michael@0 147 #endif
michael@0 148
michael@0 149 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 150 static const char *precsize_ntoa(u_int32_t);
michael@0 151 #endif
michael@0 152
michael@0 153 extern const char * const _res_opcodes[];
michael@0 154 extern const char * const _res_sectioncodes[];
michael@0 155
michael@0 156 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 157 #ifndef _LIBC
michael@0 158 /*
michael@0 159 * Print the current options.
michael@0 160 */
michael@0 161 void
michael@0 162 fp_resstat(const res_state statp, FILE *file) {
michael@0 163 u_long mask;
michael@0 164
michael@0 165 fprintf(file, ";; res options:");
michael@0 166 for (mask = 1; mask != 0U; mask <<= 1)
michael@0 167 if (statp->options & mask)
michael@0 168 fprintf(file, " %s", p_option(mask));
michael@0 169 putc('\n', file);
michael@0 170 }
michael@0 171 #endif
michael@0 172 #endif
michael@0 173
michael@0 174 static void
michael@0 175 do_section(const res_state statp,
michael@0 176 ns_msg *handle, ns_sect section,
michael@0 177 int pflag, FILE *file)
michael@0 178 {
michael@0 179 int n, sflag, rrnum;
michael@0 180 int buflen = 2048;
michael@0 181 char *buf;
michael@0 182 ns_opcode opcode;
michael@0 183 ns_rr rr;
michael@0 184
michael@0 185 /*
michael@0 186 * Print answer records.
michael@0 187 */
michael@0 188 sflag = (statp->pfcode & pflag);
michael@0 189 if (statp->pfcode && !sflag)
michael@0 190 return;
michael@0 191
michael@0 192 buf = malloc((size_t)buflen);
michael@0 193 if (buf == NULL) {
michael@0 194 fprintf(file, ";; memory allocation failure\n");
michael@0 195 return;
michael@0 196 }
michael@0 197
michael@0 198 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
michael@0 199 rrnum = 0;
michael@0 200 for (;;) {
michael@0 201 if (ns_parserr(handle, section, rrnum, &rr)) {
michael@0 202 if (errno != ENODEV)
michael@0 203 fprintf(file, ";; ns_parserr: %s\n",
michael@0 204 strerror(errno));
michael@0 205 else if (rrnum > 0 && sflag != 0 &&
michael@0 206 (statp->pfcode & RES_PRF_HEAD1))
michael@0 207 putc('\n', file);
michael@0 208 goto cleanup;
michael@0 209 }
michael@0 210 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
michael@0 211 fprintf(file, ";; %s SECTION:\n",
michael@0 212 p_section(section, opcode));
michael@0 213 if (section == ns_s_qd)
michael@0 214 fprintf(file, ";;\t%s, type = %s, class = %s\n",
michael@0 215 ns_rr_name(rr),
michael@0 216 p_type(ns_rr_type(rr)),
michael@0 217 p_class(ns_rr_class(rr)));
michael@0 218 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
michael@0 219 u_int32_t ttl = ns_rr_ttl(rr);
michael@0 220 fprintf(file,
michael@0 221 "; EDNS: version: %u, udp=%u, flags=%04x\n",
michael@0 222 (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff);
michael@0 223 } else {
michael@0 224 n = ns_sprintrr(handle, &rr, NULL, NULL,
michael@0 225 buf, (u_int)buflen);
michael@0 226 if (n < 0) {
michael@0 227 if (errno == ENOSPC) {
michael@0 228 free(buf);
michael@0 229 buf = NULL;
michael@0 230 if (buflen < 131072)
michael@0 231 buf = malloc((size_t)(buflen += 1024));
michael@0 232 if (buf == NULL) {
michael@0 233 fprintf(file,
michael@0 234 ";; memory allocation failure\n");
michael@0 235 return;
michael@0 236 }
michael@0 237 continue;
michael@0 238 }
michael@0 239 fprintf(file, ";; ns_sprintrr: %s\n",
michael@0 240 strerror(errno));
michael@0 241 goto cleanup;
michael@0 242 }
michael@0 243 fputs(buf, file);
michael@0 244 fputc('\n', file);
michael@0 245 }
michael@0 246 rrnum++;
michael@0 247 }
michael@0 248 cleanup:
michael@0 249 if (buf != NULL)
michael@0 250 free(buf);
michael@0 251 }
michael@0 252
michael@0 253 /*
michael@0 254 * Print the contents of a query.
michael@0 255 * This is intended to be primarily a debugging routine.
michael@0 256 */
michael@0 257 void
michael@0 258 res_pquery(const res_state statp, const u_char *msg, int len, FILE *file) {
michael@0 259 ns_msg handle;
michael@0 260 int qdcount, ancount, nscount, arcount;
michael@0 261 u_int opcode, rcode, id;
michael@0 262
michael@0 263 if (ns_initparse(msg, len, &handle) < 0) {
michael@0 264 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
michael@0 265 return;
michael@0 266 }
michael@0 267 opcode = ns_msg_getflag(handle, ns_f_opcode);
michael@0 268 rcode = ns_msg_getflag(handle, ns_f_rcode);
michael@0 269 id = ns_msg_id(handle);
michael@0 270 qdcount = ns_msg_count(handle, ns_s_qd);
michael@0 271 ancount = ns_msg_count(handle, ns_s_an);
michael@0 272 nscount = ns_msg_count(handle, ns_s_ns);
michael@0 273 arcount = ns_msg_count(handle, ns_s_ar);
michael@0 274
michael@0 275 /*
michael@0 276 * Print header fields.
michael@0 277 */
michael@0 278 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
michael@0 279 fprintf(file,
michael@0 280 ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n",
michael@0 281 _res_opcodes[opcode], p_rcode((int)rcode), id);
michael@0 282 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX))
michael@0 283 putc(';', file);
michael@0 284 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
michael@0 285 fprintf(file, "; flags:");
michael@0 286 if (ns_msg_getflag(handle, ns_f_qr))
michael@0 287 fprintf(file, " qr");
michael@0 288 if (ns_msg_getflag(handle, ns_f_aa))
michael@0 289 fprintf(file, " aa");
michael@0 290 if (ns_msg_getflag(handle, ns_f_tc))
michael@0 291 fprintf(file, " tc");
michael@0 292 if (ns_msg_getflag(handle, ns_f_rd))
michael@0 293 fprintf(file, " rd");
michael@0 294 if (ns_msg_getflag(handle, ns_f_ra))
michael@0 295 fprintf(file, " ra");
michael@0 296 if (ns_msg_getflag(handle, ns_f_z))
michael@0 297 fprintf(file, " ??");
michael@0 298 if (ns_msg_getflag(handle, ns_f_ad))
michael@0 299 fprintf(file, " ad");
michael@0 300 if (ns_msg_getflag(handle, ns_f_cd))
michael@0 301 fprintf(file, " cd");
michael@0 302 }
michael@0 303 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
michael@0 304 fprintf(file, "; %s: %d",
michael@0 305 p_section(ns_s_qd, (int)opcode), qdcount);
michael@0 306 fprintf(file, ", %s: %d",
michael@0 307 p_section(ns_s_an, (int)opcode), ancount);
michael@0 308 fprintf(file, ", %s: %d",
michael@0 309 p_section(ns_s_ns, (int)opcode), nscount);
michael@0 310 fprintf(file, ", %s: %d",
michael@0 311 p_section(ns_s_ar, (int)opcode), arcount);
michael@0 312 }
michael@0 313 if ((!statp->pfcode) || (statp->pfcode &
michael@0 314 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
michael@0 315 putc('\n',file);
michael@0 316 }
michael@0 317 /*
michael@0 318 * Print the various sections.
michael@0 319 */
michael@0 320 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
michael@0 321 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
michael@0 322 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
michael@0 323 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
michael@0 324 if (qdcount == 0 && ancount == 0 &&
michael@0 325 nscount == 0 && arcount == 0)
michael@0 326 putc('\n', file);
michael@0 327 }
michael@0 328
michael@0 329 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 330 const u_char *
michael@0 331 p_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) {
michael@0 332 char name[MAXDNAME];
michael@0 333 int n;
michael@0 334
michael@0 335 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
michael@0 336 return (NULL);
michael@0 337 if (name[0] == '\0')
michael@0 338 putc('.', file);
michael@0 339 else
michael@0 340 fputs(name, file);
michael@0 341 return (cp + n);
michael@0 342 }
michael@0 343
michael@0 344 const u_char *
michael@0 345 p_cdname(const u_char *cp, const u_char *msg, FILE *file) {
michael@0 346 return (p_cdnname(cp, msg, PACKETSZ, file));
michael@0 347 }
michael@0 348
michael@0 349 /* Return a fully-qualified domain name from a compressed name (with
michael@0 350 length supplied). */
michael@0 351
michael@0 352 const u_char *
michael@0 353 p_fqnname(cp, msg, msglen, name, namelen)
michael@0 354 const u_char *cp, *msg;
michael@0 355 int msglen;
michael@0 356 char *name;
michael@0 357 int namelen;
michael@0 358 {
michael@0 359 int n, newlen;
michael@0 360
michael@0 361 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
michael@0 362 return (NULL);
michael@0 363 newlen = strlen(name);
michael@0 364 if (newlen == 0 || name[newlen - 1] != '.') {
michael@0 365 if (newlen + 1 >= namelen) /* Lack space for final dot */
michael@0 366 return (NULL);
michael@0 367 else
michael@0 368 strcpy(name + newlen, ".");
michael@0 369 }
michael@0 370 return (cp + n);
michael@0 371 }
michael@0 372
michael@0 373 /* XXX: the rest of these functions need to become length-limited, too. */
michael@0 374
michael@0 375 const u_char *
michael@0 376 p_fqname(const u_char *cp, const u_char *msg, FILE *file) {
michael@0 377 char name[MAXDNAME];
michael@0 378 const u_char *n;
michael@0 379
michael@0 380 n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
michael@0 381 if (n == NULL)
michael@0 382 return (NULL);
michael@0 383 fputs(name, file);
michael@0 384 return (n);
michael@0 385 }
michael@0 386 #endif
michael@0 387
michael@0 388 /*
michael@0 389 * Names of RR classes and qclasses. Classes and qclasses are the same, except
michael@0 390 * that C_ANY is a qclass but not a class. (You can ask for records of class
michael@0 391 * C_ANY, but you can't have any records of that class in the database.)
michael@0 392 */
michael@0 393 const struct res_sym __p_class_syms[] = {
michael@0 394 {C_IN, "IN", (char *)0},
michael@0 395 {C_CHAOS, "CH", (char *)0},
michael@0 396 {C_CHAOS, "CHAOS", (char *)0},
michael@0 397 {C_HS, "HS", (char *)0},
michael@0 398 {C_HS, "HESIOD", (char *)0},
michael@0 399 {C_ANY, "ANY", (char *)0},
michael@0 400 {C_NONE, "NONE", (char *)0},
michael@0 401 {C_IN, (char *)0, (char *)0}
michael@0 402 };
michael@0 403
michael@0 404 /*
michael@0 405 * Names of message sections.
michael@0 406 */
michael@0 407 const struct res_sym __p_default_section_syms[] = {
michael@0 408 {ns_s_qd, "QUERY", (char *)0},
michael@0 409 {ns_s_an, "ANSWER", (char *)0},
michael@0 410 {ns_s_ns, "AUTHORITY", (char *)0},
michael@0 411 {ns_s_ar, "ADDITIONAL", (char *)0},
michael@0 412 {0, (char *)0, (char *)0}
michael@0 413 };
michael@0 414
michael@0 415 const struct res_sym __p_update_section_syms[] = {
michael@0 416 {S_ZONE, "ZONE", (char *)0},
michael@0 417 {S_PREREQ, "PREREQUISITE", (char *)0},
michael@0 418 {S_UPDATE, "UPDATE", (char *)0},
michael@0 419 {S_ADDT, "ADDITIONAL", (char *)0},
michael@0 420 {0, (char *)0, (char *)0}
michael@0 421 };
michael@0 422
michael@0 423 const struct res_sym __p_key_syms[] = {
michael@0 424 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
michael@0 425 {NS_ALG_DH, "DH", "Diffie Hellman"},
michael@0 426 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
michael@0 427 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
michael@0 428 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
michael@0 429 {0, NULL, NULL}
michael@0 430 };
michael@0 431
michael@0 432 const struct res_sym __p_cert_syms[] = {
michael@0 433 {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
michael@0 434 {cert_t_spki, "SPKI", "SPKI certificate"},
michael@0 435 {cert_t_pgp, "PGP", "PGP certificate"},
michael@0 436 {cert_t_url, "URL", "URL Private"},
michael@0 437 {cert_t_oid, "OID", "OID Private"},
michael@0 438 {0, NULL, NULL}
michael@0 439 };
michael@0 440
michael@0 441 /*
michael@0 442 * Names of RR types and qtypes. Types and qtypes are the same, except
michael@0 443 * that T_ANY is a qtype but not a type. (You can ask for records of type
michael@0 444 * T_ANY, but you can't have any records of that type in the database.)
michael@0 445 */
michael@0 446 const struct res_sym __p_type_syms[] = {
michael@0 447 {ns_t_a, "A", "address"},
michael@0 448 {ns_t_ns, "NS", "name server"},
michael@0 449 {ns_t_md, "MD", "mail destination (deprecated)"},
michael@0 450 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
michael@0 451 {ns_t_cname, "CNAME", "canonical name"},
michael@0 452 {ns_t_soa, "SOA", "start of authority"},
michael@0 453 {ns_t_mb, "MB", "mailbox"},
michael@0 454 {ns_t_mg, "MG", "mail group member"},
michael@0 455 {ns_t_mr, "MR", "mail rename"},
michael@0 456 {ns_t_null, "NULL", "null"},
michael@0 457 {ns_t_wks, "WKS", "well-known service (deprecated)"},
michael@0 458 {ns_t_ptr, "PTR", "domain name pointer"},
michael@0 459 {ns_t_hinfo, "HINFO", "host information"},
michael@0 460 {ns_t_minfo, "MINFO", "mailbox information"},
michael@0 461 {ns_t_mx, "MX", "mail exchanger"},
michael@0 462 {ns_t_txt, "TXT", "text"},
michael@0 463 {ns_t_rp, "RP", "responsible person"},
michael@0 464 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
michael@0 465 {ns_t_x25, "X25", "X25 address"},
michael@0 466 {ns_t_isdn, "ISDN", "ISDN address"},
michael@0 467 {ns_t_rt, "RT", "router"},
michael@0 468 {ns_t_nsap, "NSAP", "nsap address"},
michael@0 469 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
michael@0 470 {ns_t_sig, "SIG", "signature"},
michael@0 471 {ns_t_key, "KEY", "key"},
michael@0 472 {ns_t_px, "PX", "mapping information"},
michael@0 473 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
michael@0 474 {ns_t_aaaa, "AAAA", "IPv6 address"},
michael@0 475 {ns_t_loc, "LOC", "location"},
michael@0 476 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
michael@0 477 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
michael@0 478 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
michael@0 479 {ns_t_srv, "SRV", "server selection"},
michael@0 480 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
michael@0 481 {ns_t_tkey, "TKEY", "tkey"},
michael@0 482 {ns_t_tsig, "TSIG", "transaction signature"},
michael@0 483 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
michael@0 484 {ns_t_axfr, "AXFR", "zone transfer"},
michael@0 485 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
michael@0 486 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
michael@0 487 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
michael@0 488 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
michael@0 489 {ns_t_kx, "KX", "Key Exchange"},
michael@0 490 {ns_t_cert, "CERT", "Certificate"},
michael@0 491 {ns_t_a6, "A6", "IPv6 Address"},
michael@0 492 {ns_t_dname, "DNAME", "dname"},
michael@0 493 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
michael@0 494 {ns_t_opt, "OPT", "EDNS Options"},
michael@0 495 {ns_t_any, "ANY", "\"any\""},
michael@0 496 {0, NULL, NULL}
michael@0 497 };
michael@0 498
michael@0 499 /*
michael@0 500 * Names of DNS rcodes.
michael@0 501 */
michael@0 502 const struct res_sym __p_rcode_syms[] = {
michael@0 503 {ns_r_noerror, "NOERROR", "no error"},
michael@0 504 {ns_r_formerr, "FORMERR", "format error"},
michael@0 505 {ns_r_servfail, "SERVFAIL", "server failed"},
michael@0 506 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
michael@0 507 {ns_r_notimpl, "NOTIMP", "not implemented"},
michael@0 508 {ns_r_refused, "REFUSED", "refused"},
michael@0 509 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
michael@0 510 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
michael@0 511 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
michael@0 512 {ns_r_notauth, "NOTAUTH", "not authoritative"},
michael@0 513 {ns_r_notzone, "NOTZONE", "Not in zone"},
michael@0 514 {ns_r_max, "", ""},
michael@0 515 {ns_r_badsig, "BADSIG", "bad signature"},
michael@0 516 {ns_r_badkey, "BADKEY", "bad key"},
michael@0 517 {ns_r_badtime, "BADTIME", "bad time"},
michael@0 518 {0, NULL, NULL}
michael@0 519 };
michael@0 520
michael@0 521 int
michael@0 522 sym_ston(const struct res_sym *syms, const char *name, int *success) {
michael@0 523 for (; syms->name != 0; syms++) {
michael@0 524 if (strcasecmp (name, syms->name) == 0) {
michael@0 525 if (success)
michael@0 526 *success = 1;
michael@0 527 return (syms->number);
michael@0 528 }
michael@0 529 }
michael@0 530 if (success)
michael@0 531 *success = 0;
michael@0 532 return (syms->number); /* The default value. */
michael@0 533 }
michael@0 534
michael@0 535 const char *
michael@0 536 sym_ntos(const struct res_sym *syms, int number, int *success) {
michael@0 537 static char unname[20];
michael@0 538
michael@0 539 for (; syms->name != 0; syms++) {
michael@0 540 if (number == syms->number) {
michael@0 541 if (success)
michael@0 542 *success = 1;
michael@0 543 return (syms->name);
michael@0 544 }
michael@0 545 }
michael@0 546
michael@0 547 sprintf(unname, "%d", number); /* XXX nonreentrant */
michael@0 548 if (success)
michael@0 549 *success = 0;
michael@0 550 return (unname);
michael@0 551 }
michael@0 552
michael@0 553 const char *
michael@0 554 sym_ntop(const struct res_sym *syms, int number, int *success) {
michael@0 555 static char unname[20];
michael@0 556
michael@0 557 for (; syms->name != 0; syms++) {
michael@0 558 if (number == syms->number) {
michael@0 559 if (success)
michael@0 560 *success = 1;
michael@0 561 return (syms->humanname);
michael@0 562 }
michael@0 563 }
michael@0 564 sprintf(unname, "%d", number); /* XXX nonreentrant */
michael@0 565 if (success)
michael@0 566 *success = 0;
michael@0 567 return (unname);
michael@0 568 }
michael@0 569
michael@0 570 /*
michael@0 571 * Return a string for the type.
michael@0 572 */
michael@0 573 const char *
michael@0 574 p_type(int type) {
michael@0 575 int success;
michael@0 576 const char *result;
michael@0 577 static char typebuf[20];
michael@0 578
michael@0 579 result = sym_ntos(__p_type_syms, type, &success);
michael@0 580 if (success)
michael@0 581 return (result);
michael@0 582 if (type < 0 || type > 0xffff)
michael@0 583 return ("BADTYPE");
michael@0 584 sprintf(typebuf, "TYPE%d", type);
michael@0 585 return (typebuf);
michael@0 586 }
michael@0 587
michael@0 588 /*
michael@0 589 * Return a string for the type.
michael@0 590 */
michael@0 591 const char *
michael@0 592 p_section(int section, int opcode) {
michael@0 593 const struct res_sym *symbols;
michael@0 594
michael@0 595 switch (opcode) {
michael@0 596 case ns_o_update:
michael@0 597 symbols = __p_update_section_syms;
michael@0 598 break;
michael@0 599 default:
michael@0 600 symbols = __p_default_section_syms;
michael@0 601 break;
michael@0 602 }
michael@0 603 return (sym_ntos(symbols, section, (int *)0));
michael@0 604 }
michael@0 605
michael@0 606 /*
michael@0 607 * Return a mnemonic for class.
michael@0 608 */
michael@0 609 const char *
michael@0 610 p_class(int class) {
michael@0 611 int success;
michael@0 612 const char *result;
michael@0 613 static char classbuf[20];
michael@0 614
michael@0 615 result = sym_ntos(__p_class_syms, class, &success);
michael@0 616 if (success)
michael@0 617 return (result);
michael@0 618 if (class < 0 || class > 0xffff)
michael@0 619 return ("BADCLASS");
michael@0 620 sprintf(classbuf, "CLASS%d", class);
michael@0 621 return (classbuf);
michael@0 622 }
michael@0 623
michael@0 624 /*
michael@0 625 * Return a mnemonic for an option
michael@0 626 */
michael@0 627 const char *
michael@0 628 p_option(u_long option) {
michael@0 629 static char nbuf[40];
michael@0 630
michael@0 631 switch (option) {
michael@0 632 case RES_INIT: return "init";
michael@0 633 case RES_DEBUG: return "debug";
michael@0 634 case RES_AAONLY: return "aaonly(unimpl)";
michael@0 635 case RES_USEVC: return "usevc";
michael@0 636 case RES_PRIMARY: return "primry(unimpl)";
michael@0 637 case RES_IGNTC: return "igntc";
michael@0 638 case RES_RECURSE: return "recurs";
michael@0 639 case RES_DEFNAMES: return "defnam";
michael@0 640 case RES_STAYOPEN: return "styopn";
michael@0 641 case RES_DNSRCH: return "dnsrch";
michael@0 642 case RES_INSECURE1: return "insecure1";
michael@0 643 case RES_INSECURE2: return "insecure2";
michael@0 644 case RES_NOALIASES: return "noaliases";
michael@0 645 case RES_USE_INET6: return "inet6";
michael@0 646 #ifdef RES_USE_EDNS0 /* KAME extension */
michael@0 647 case RES_USE_EDNS0: return "edns0";
michael@0 648 #endif
michael@0 649 #ifdef RES_USE_DNAME
michael@0 650 case RES_USE_DNAME: return "dname";
michael@0 651 #endif
michael@0 652 #ifdef RES_USE_DNSSEC
michael@0 653 case RES_USE_DNSSEC: return "dnssec";
michael@0 654 #endif
michael@0 655 #ifdef RES_NOTLDQUERY
michael@0 656 case RES_NOTLDQUERY: return "no-tld-query";
michael@0 657 #endif
michael@0 658 #ifdef RES_NO_NIBBLE2
michael@0 659 case RES_NO_NIBBLE2: return "no-nibble2";
michael@0 660 #endif
michael@0 661 /* XXX nonreentrant */
michael@0 662 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
michael@0 663 return (nbuf);
michael@0 664 }
michael@0 665 }
michael@0 666
michael@0 667 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 668 /*
michael@0 669 * Return a mnemonic for a time to live.
michael@0 670 */
michael@0 671 const char *
michael@0 672 p_time(u_int32_t value) {
michael@0 673 static char nbuf[40]; /* XXX nonreentrant */
michael@0 674
michael@0 675 if (ns_format_ttl((u_long)value, nbuf, sizeof nbuf) < 0)
michael@0 676 sprintf(nbuf, "%u", value);
michael@0 677 return (nbuf);
michael@0 678 }
michael@0 679 #endif
michael@0 680
michael@0 681 /*
michael@0 682 * Return a string for the rcode.
michael@0 683 */
michael@0 684 const char *
michael@0 685 p_rcode(int rcode) {
michael@0 686 return (sym_ntos(__p_rcode_syms, rcode, (int *)0));
michael@0 687 }
michael@0 688
michael@0 689 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 690 /*
michael@0 691 * Return a string for a res_sockaddr_union.
michael@0 692 */
michael@0 693 const char *
michael@0 694 p_sockun(union res_sockaddr_union u, char *buf, size_t size) {
michael@0 695 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
michael@0 696
michael@0 697 switch (u.sin.sin_family) {
michael@0 698 case AF_INET:
michael@0 699 inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret);
michael@0 700 break;
michael@0 701 #ifdef HAS_INET6_STRUCTS
michael@0 702 case AF_INET6:
michael@0 703 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
michael@0 704 break;
michael@0 705 #endif
michael@0 706 default:
michael@0 707 sprintf(ret, "[af%d]", u.sin.sin_family);
michael@0 708 break;
michael@0 709 }
michael@0 710 if (size > 0U) {
michael@0 711 strncpy(buf, ret, size - 1);
michael@0 712 buf[size - 1] = '0';
michael@0 713 }
michael@0 714 return (buf);
michael@0 715 }
michael@0 716 #endif
michael@0 717
michael@0 718 /*
michael@0 719 * routines to convert between on-the-wire RR format and zone file format.
michael@0 720 * Does not contain conversion to/from decimal degrees; divide or multiply
michael@0 721 * by 60*60*1000 for that.
michael@0 722 */
michael@0 723
michael@0 724 static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
michael@0 725 1000000,10000000,100000000,1000000000};
michael@0 726
michael@0 727 /* takes an XeY precision/size value, returns a string representation. */
michael@0 728 static const char *
michael@0 729 precsize_ntoa(prec)
michael@0 730 u_int32_t prec;
michael@0 731 {
michael@0 732 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
michael@0 733 unsigned long val;
michael@0 734 int mantissa, exponent;
michael@0 735
michael@0 736 mantissa = (int)((prec >> 4) & 0x0f) % 10;
michael@0 737 exponent = (int)((prec >> 0) & 0x0f) % 10;
michael@0 738
michael@0 739 val = mantissa * poweroften[exponent];
michael@0 740
michael@0 741 (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100);
michael@0 742 return (retbuf);
michael@0 743 }
michael@0 744
michael@0 745 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 746 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
michael@0 747 static u_int8_t
michael@0 748 precsize_aton(const char **strptr) {
michael@0 749 unsigned int mval = 0, cmval = 0;
michael@0 750 u_int8_t retval = 0;
michael@0 751 const char *cp;
michael@0 752 int exponent;
michael@0 753 int mantissa;
michael@0 754
michael@0 755 cp = *strptr;
michael@0 756
michael@0 757 while (isdigit((unsigned char)*cp))
michael@0 758 mval = mval * 10 + (*cp++ - '0');
michael@0 759
michael@0 760 if (*cp == '.') { /* centimeters */
michael@0 761 cp++;
michael@0 762 if (isdigit((unsigned char)*cp)) {
michael@0 763 cmval = (*cp++ - '0') * 10;
michael@0 764 if (isdigit((unsigned char)*cp)) {
michael@0 765 cmval += (*cp++ - '0');
michael@0 766 }
michael@0 767 }
michael@0 768 }
michael@0 769 cmval = (mval * 100) + cmval;
michael@0 770
michael@0 771 for (exponent = 0; exponent < 9; exponent++)
michael@0 772 if (cmval < poweroften[exponent+1])
michael@0 773 break;
michael@0 774
michael@0 775 mantissa = cmval / poweroften[exponent];
michael@0 776 if (mantissa > 9)
michael@0 777 mantissa = 9;
michael@0 778
michael@0 779 retval = (mantissa << 4) | exponent;
michael@0 780
michael@0 781 *strptr = cp;
michael@0 782
michael@0 783 return (retval);
michael@0 784 }
michael@0 785
michael@0 786 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
michael@0 787 static u_int32_t
michael@0 788 latlon2ul(const char **latlonstrptr, int *which) {
michael@0 789 const char *cp;
michael@0 790 u_int32_t retval;
michael@0 791 int deg = 0, min = 0, secs = 0, secsfrac = 0;
michael@0 792
michael@0 793 cp = *latlonstrptr;
michael@0 794
michael@0 795 while (isdigit((unsigned char)*cp))
michael@0 796 deg = deg * 10 + (*cp++ - '0');
michael@0 797
michael@0 798 while (isspace((unsigned char)*cp))
michael@0 799 cp++;
michael@0 800
michael@0 801 if (!(isdigit((unsigned char)*cp)))
michael@0 802 goto fndhemi;
michael@0 803
michael@0 804 while (isdigit((unsigned char)*cp))
michael@0 805 min = min * 10 + (*cp++ - '0');
michael@0 806
michael@0 807 while (isspace((unsigned char)*cp))
michael@0 808 cp++;
michael@0 809
michael@0 810 if (!(isdigit((unsigned char)*cp)))
michael@0 811 goto fndhemi;
michael@0 812
michael@0 813 while (isdigit((unsigned char)*cp))
michael@0 814 secs = secs * 10 + (*cp++ - '0');
michael@0 815
michael@0 816 if (*cp == '.') { /* decimal seconds */
michael@0 817 cp++;
michael@0 818 if (isdigit((unsigned char)*cp)) {
michael@0 819 secsfrac = (*cp++ - '0') * 100;
michael@0 820 if (isdigit((unsigned char)*cp)) {
michael@0 821 secsfrac += (*cp++ - '0') * 10;
michael@0 822 if (isdigit((unsigned char)*cp)) {
michael@0 823 secsfrac += (*cp++ - '0');
michael@0 824 }
michael@0 825 }
michael@0 826 }
michael@0 827 }
michael@0 828
michael@0 829 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
michael@0 830 cp++;
michael@0 831
michael@0 832 while (isspace((unsigned char)*cp))
michael@0 833 cp++;
michael@0 834
michael@0 835 fndhemi:
michael@0 836 switch (*cp) {
michael@0 837 case 'N': case 'n':
michael@0 838 case 'E': case 'e':
michael@0 839 retval = ((unsigned)1<<31)
michael@0 840 + (((((deg * 60) + min) * 60) + secs) * 1000)
michael@0 841 + secsfrac;
michael@0 842 break;
michael@0 843 case 'S': case 's':
michael@0 844 case 'W': case 'w':
michael@0 845 retval = ((unsigned)1<<31)
michael@0 846 - (((((deg * 60) + min) * 60) + secs) * 1000)
michael@0 847 - secsfrac;
michael@0 848 break;
michael@0 849 default:
michael@0 850 retval = 0; /* invalid value -- indicates error */
michael@0 851 break;
michael@0 852 }
michael@0 853
michael@0 854 switch (*cp) {
michael@0 855 case 'N': case 'n':
michael@0 856 case 'S': case 's':
michael@0 857 *which = 1; /* latitude */
michael@0 858 break;
michael@0 859 case 'E': case 'e':
michael@0 860 case 'W': case 'w':
michael@0 861 *which = 2; /* longitude */
michael@0 862 break;
michael@0 863 default:
michael@0 864 *which = 0; /* error */
michael@0 865 break;
michael@0 866 }
michael@0 867
michael@0 868 cp++; /* skip the hemisphere */
michael@0 869
michael@0 870 while (!isspace((unsigned char)*cp)) /* if any trailing garbage */
michael@0 871 cp++;
michael@0 872
michael@0 873 while (isspace((unsigned char)*cp)) /* move to next field */
michael@0 874 cp++;
michael@0 875
michael@0 876 *latlonstrptr = cp;
michael@0 877
michael@0 878 return (retval);
michael@0 879 }
michael@0 880
michael@0 881 /* converts a zone file representation in a string to an RDATA on-the-wire
michael@0 882 * representation. */
michael@0 883 int
michael@0 884 loc_aton(ascii, binary)
michael@0 885 const char *ascii;
michael@0 886 u_char *binary;
michael@0 887 {
michael@0 888 const char *cp, *maxcp;
michael@0 889 u_char *bcp;
michael@0 890
michael@0 891 u_int32_t latit = 0, longit = 0, alt = 0;
michael@0 892 u_int32_t lltemp1 = 0, lltemp2 = 0;
michael@0 893 int altmeters = 0, altfrac = 0, altsign = 1;
michael@0 894 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
michael@0 895 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
michael@0 896 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
michael@0 897 int which1 = 0, which2 = 0;
michael@0 898
michael@0 899 cp = ascii;
michael@0 900 maxcp = cp + strlen(ascii);
michael@0 901
michael@0 902 lltemp1 = latlon2ul(&cp, &which1);
michael@0 903
michael@0 904 lltemp2 = latlon2ul(&cp, &which2);
michael@0 905
michael@0 906 switch (which1 + which2) {
michael@0 907 case 3: /* 1 + 2, the only valid combination */
michael@0 908 if ((which1 == 1) && (which2 == 2)) { /* normal case */
michael@0 909 latit = lltemp1;
michael@0 910 longit = lltemp2;
michael@0 911 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
michael@0 912 longit = lltemp1;
michael@0 913 latit = lltemp2;
michael@0 914 } else { /* some kind of brokenness */
michael@0 915 return (0);
michael@0 916 }
michael@0 917 break;
michael@0 918 default: /* we didn't get one of each */
michael@0 919 return (0);
michael@0 920 }
michael@0 921
michael@0 922 /* altitude */
michael@0 923 if (*cp == '-') {
michael@0 924 altsign = -1;
michael@0 925 cp++;
michael@0 926 }
michael@0 927
michael@0 928 if (*cp == '+')
michael@0 929 cp++;
michael@0 930
michael@0 931 while (isdigit((unsigned char)*cp))
michael@0 932 altmeters = altmeters * 10 + (*cp++ - '0');
michael@0 933
michael@0 934 if (*cp == '.') { /* decimal meters */
michael@0 935 cp++;
michael@0 936 if (isdigit((unsigned char)*cp)) {
michael@0 937 altfrac = (*cp++ - '0') * 10;
michael@0 938 if (isdigit((unsigned char)*cp)) {
michael@0 939 altfrac += (*cp++ - '0');
michael@0 940 }
michael@0 941 }
michael@0 942 }
michael@0 943
michael@0 944 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
michael@0 945
michael@0 946 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
michael@0 947 cp++;
michael@0 948
michael@0 949 while (isspace((unsigned char)*cp) && (cp < maxcp))
michael@0 950 cp++;
michael@0 951
michael@0 952 if (cp >= maxcp)
michael@0 953 goto defaults;
michael@0 954
michael@0 955 siz = precsize_aton(&cp);
michael@0 956
michael@0 957 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
michael@0 958 cp++;
michael@0 959
michael@0 960 while (isspace((unsigned char)*cp) && (cp < maxcp))
michael@0 961 cp++;
michael@0 962
michael@0 963 if (cp >= maxcp)
michael@0 964 goto defaults;
michael@0 965
michael@0 966 hp = precsize_aton(&cp);
michael@0 967
michael@0 968 while (!isspace((unsigned char)*cp) && (cp < maxcp)) /* if trailing garbage or m */
michael@0 969 cp++;
michael@0 970
michael@0 971 while (isspace((unsigned char)*cp) && (cp < maxcp))
michael@0 972 cp++;
michael@0 973
michael@0 974 if (cp >= maxcp)
michael@0 975 goto defaults;
michael@0 976
michael@0 977 vp = precsize_aton(&cp);
michael@0 978
michael@0 979 defaults:
michael@0 980
michael@0 981 bcp = binary;
michael@0 982 *bcp++ = (u_int8_t) 0; /* version byte */
michael@0 983 *bcp++ = siz;
michael@0 984 *bcp++ = hp;
michael@0 985 *bcp++ = vp;
michael@0 986 PUTLONG(latit,bcp);
michael@0 987 PUTLONG(longit,bcp);
michael@0 988 PUTLONG(alt,bcp);
michael@0 989
michael@0 990 return (16); /* size of RR in octets */
michael@0 991 }
michael@0 992 #endif
michael@0 993
michael@0 994 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
michael@0 995 const char *
michael@0 996 loc_ntoa(binary, ascii)
michael@0 997 const u_char *binary;
michael@0 998 char *ascii;
michael@0 999 {
michael@0 1000 static const char *error = "?";
michael@0 1001 static char tmpbuf[sizeof
michael@0 1002 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
michael@0 1003 const u_char *cp = binary;
michael@0 1004
michael@0 1005 int latdeg, latmin, latsec, latsecfrac;
michael@0 1006 int longdeg, longmin, longsec, longsecfrac;
michael@0 1007 char northsouth, eastwest;
michael@0 1008 const char *altsign;
michael@0 1009 int altmeters, altfrac;
michael@0 1010
michael@0 1011 const u_int32_t referencealt = 100000 * 100;
michael@0 1012
michael@0 1013 int32_t latval, longval, altval;
michael@0 1014 u_int32_t templ;
michael@0 1015 u_int8_t sizeval, hpval, vpval, versionval;
michael@0 1016
michael@0 1017 char *sizestr, *hpstr, *vpstr;
michael@0 1018
michael@0 1019 versionval = *cp++;
michael@0 1020
michael@0 1021 if (ascii == NULL)
michael@0 1022 ascii = tmpbuf;
michael@0 1023
michael@0 1024 if (versionval) {
michael@0 1025 (void) sprintf(ascii, "; error: unknown LOC RR version");
michael@0 1026 return (ascii);
michael@0 1027 }
michael@0 1028
michael@0 1029 sizeval = *cp++;
michael@0 1030
michael@0 1031 hpval = *cp++;
michael@0 1032 vpval = *cp++;
michael@0 1033
michael@0 1034 GETLONG(templ, cp);
michael@0 1035 latval = (templ - ((unsigned)1<<31));
michael@0 1036
michael@0 1037 GETLONG(templ, cp);
michael@0 1038 longval = (templ - ((unsigned)1<<31));
michael@0 1039
michael@0 1040 GETLONG(templ, cp);
michael@0 1041 if (templ < referencealt) { /* below WGS 84 spheroid */
michael@0 1042 altval = referencealt - templ;
michael@0 1043 altsign = "-";
michael@0 1044 } else {
michael@0 1045 altval = templ - referencealt;
michael@0 1046 altsign = "";
michael@0 1047 }
michael@0 1048
michael@0 1049 if (latval < 0) {
michael@0 1050 northsouth = 'S';
michael@0 1051 latval = -latval;
michael@0 1052 } else
michael@0 1053 northsouth = 'N';
michael@0 1054
michael@0 1055 latsecfrac = latval % 1000;
michael@0 1056 latval = latval / 1000;
michael@0 1057 latsec = latval % 60;
michael@0 1058 latval = latval / 60;
michael@0 1059 latmin = latval % 60;
michael@0 1060 latval = latval / 60;
michael@0 1061 latdeg = latval;
michael@0 1062
michael@0 1063 if (longval < 0) {
michael@0 1064 eastwest = 'W';
michael@0 1065 longval = -longval;
michael@0 1066 } else
michael@0 1067 eastwest = 'E';
michael@0 1068
michael@0 1069 longsecfrac = longval % 1000;
michael@0 1070 longval = longval / 1000;
michael@0 1071 longsec = longval % 60;
michael@0 1072 longval = longval / 60;
michael@0 1073 longmin = longval % 60;
michael@0 1074 longval = longval / 60;
michael@0 1075 longdeg = longval;
michael@0 1076
michael@0 1077 altfrac = altval % 100;
michael@0 1078 altmeters = (altval / 100);
michael@0 1079
michael@0 1080 sizestr = strdup(precsize_ntoa((u_int32_t)sizeval));
michael@0 1081 hpstr = strdup(precsize_ntoa((u_int32_t)hpval));
michael@0 1082 vpstr = strdup(precsize_ntoa((u_int32_t)vpval));
michael@0 1083
michael@0 1084 sprintf(ascii,
michael@0 1085 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
michael@0 1086 latdeg, latmin, latsec, latsecfrac, northsouth,
michael@0 1087 longdeg, longmin, longsec, longsecfrac, eastwest,
michael@0 1088 altsign, altmeters, altfrac,
michael@0 1089 (sizestr != NULL) ? sizestr : error,
michael@0 1090 (hpstr != NULL) ? hpstr : error,
michael@0 1091 (vpstr != NULL) ? vpstr : error);
michael@0 1092
michael@0 1093 if (sizestr != NULL)
michael@0 1094 free(sizestr);
michael@0 1095 if (hpstr != NULL)
michael@0 1096 free(hpstr);
michael@0 1097 if (vpstr != NULL)
michael@0 1098 free(vpstr);
michael@0 1099
michael@0 1100 return (ascii);
michael@0 1101 }
michael@0 1102
michael@0 1103
michael@0 1104 /* Return the number of DNS hierarchy levels in the name. */
michael@0 1105 int
michael@0 1106 dn_count_labels(const char *name) {
michael@0 1107 int i, len, count;
michael@0 1108
michael@0 1109 len = strlen(name);
michael@0 1110 for (i = 0, count = 0; i < len; i++) {
michael@0 1111 /* XXX need to check for \. or use named's nlabels(). */
michael@0 1112 if (name[i] == '.')
michael@0 1113 count++;
michael@0 1114 }
michael@0 1115
michael@0 1116 /* don't count initial wildcard */
michael@0 1117 if (name[0] == '*')
michael@0 1118 if (count)
michael@0 1119 count--;
michael@0 1120
michael@0 1121 /* don't count the null label for root. */
michael@0 1122 /* if terminating '.' not found, must adjust */
michael@0 1123 /* count to include last label */
michael@0 1124 if (len > 0 && name[len-1] != '.')
michael@0 1125 count++;
michael@0 1126 return (count);
michael@0 1127 }
michael@0 1128
michael@0 1129
michael@0 1130 /*
michael@0 1131 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
michael@0 1132 * SIG records are required to be printed like this, by the Secure DNS RFC.
michael@0 1133 */
michael@0 1134 char *
michael@0 1135 p_secstodate (u_long secs) {
michael@0 1136 /* XXX nonreentrant */
michael@0 1137 static char output[15]; /* YYYYMMDDHHMMSS and null */
michael@0 1138 time_t myclock = secs;
michael@0 1139 struct tm *mytime;
michael@0 1140 #ifdef HAVE_TIME_R
michael@0 1141 struct tm res;
michael@0 1142
michael@0 1143 mytime = gmtime_r(&myclock, &res);
michael@0 1144 #else
michael@0 1145 mytime = gmtime(&myclock);
michael@0 1146 #endif
michael@0 1147 mytime->tm_year += 1900;
michael@0 1148 mytime->tm_mon += 1;
michael@0 1149 sprintf(output, "%04d%02d%02d%02d%02d%02d",
michael@0 1150 mytime->tm_year, mytime->tm_mon, mytime->tm_mday,
michael@0 1151 mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
michael@0 1152 return (output);
michael@0 1153 }
michael@0 1154
michael@0 1155 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 1156 u_int16_t
michael@0 1157 res_nametoclass(const char *buf, int *successp) {
michael@0 1158 unsigned long result;
michael@0 1159 char *endptr;
michael@0 1160 int success;
michael@0 1161
michael@0 1162 result = sym_ston(__p_class_syms, buf, &success);
michael@0 1163 if (success)
michael@0 1164 goto done;
michael@0 1165
michael@0 1166 if (strncasecmp(buf, "CLASS", 5) != 0 ||
michael@0 1167 !isdigit((unsigned char)buf[5]))
michael@0 1168 goto done;
michael@0 1169 errno = 0;
michael@0 1170 result = strtoul(buf + 5, &endptr, 10);
michael@0 1171 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
michael@0 1172 success = 1;
michael@0 1173 done:
michael@0 1174 if (successp)
michael@0 1175 *successp = success;
michael@0 1176 return (u_int16_t)(result);
michael@0 1177 }
michael@0 1178
michael@0 1179 u_int16_t
michael@0 1180 res_nametotype(const char *buf, int *successp) {
michael@0 1181 unsigned long result;
michael@0 1182 char *endptr;
michael@0 1183 int success;
michael@0 1184
michael@0 1185 result = sym_ston(__p_type_syms, buf, &success);
michael@0 1186 if (success)
michael@0 1187 goto done;
michael@0 1188
michael@0 1189 if (strncasecmp(buf, "type", 4) != 0 ||
michael@0 1190 !isdigit((unsigned char)buf[4]))
michael@0 1191 goto done;
michael@0 1192 errno = 0;
michael@0 1193 result = strtoul(buf + 4, &endptr, 10);
michael@0 1194 if (errno == 0 && *endptr == '\0' && result <= 0xffffU)
michael@0 1195 success = 1;
michael@0 1196 done:
michael@0 1197 if (successp)
michael@0 1198 *successp = success;
michael@0 1199 return (u_int16_t)(result);
michael@0 1200 }
michael@0 1201 #endif

mercurial