1156952Sume/* 2270838Sume * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC") 3270838Sume * Portions Copyright (C) 1996-2003 Internet Software Consortium. 4270838Sume * 5270838Sume * Permission to use, copy, modify, and/or distribute this software for any 6270838Sume * purpose with or without fee is hereby granted, provided that the above 7270838Sume * copyright notice and this permission notice appear in all copies. 8270838Sume * 9270838Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10270838Sume * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11270838Sume * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12270838Sume * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13270838Sume * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14270838Sume * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15270838Sume * PERFORMANCE OF THIS SOFTWARE. 16270838Sume */ 17270838Sume 18270838Sume/* 19156952Sume * Copyright (c) 1985 20156952Sume * The Regents of the University of California. All rights reserved. 21270838Sume * 22156952Sume * Redistribution and use in source and binary forms, with or without 23156952Sume * modification, are permitted provided that the following conditions 24156952Sume * are met: 25156952Sume * 1. Redistributions of source code must retain the above copyright 26156952Sume * notice, this list of conditions and the following disclaimer. 27156952Sume * 2. Redistributions in binary form must reproduce the above copyright 28156952Sume * notice, this list of conditions and the following disclaimer in the 29156952Sume * documentation and/or other materials provided with the distribution. 30156952Sume * 4. Neither the name of the University nor the names of its contributors 31156952Sume * may be used to endorse or promote products derived from this software 32156952Sume * without specific prior written permission. 33270838Sume * 34156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 35156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 38156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44156952Sume * SUCH DAMAGE. 45156952Sume */ 46156952Sume 47156952Sume/* 48156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 49270838Sume * 50156952Sume * Permission to use, copy, modify, and distribute this software for any 51156952Sume * purpose with or without fee is hereby granted, provided that the above 52156952Sume * copyright notice and this permission notice appear in all copies, and that 53156952Sume * the name of Digital Equipment Corporation not be used in advertising or 54156952Sume * publicity pertaining to distribution of the document or software without 55156952Sume * specific, written prior permission. 56270838Sume * 57156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 58156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 59156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 60156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 61156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 62156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 63156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 64156952Sume * SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume/* 68156952Sume * Portions Copyright (c) 1995 by International Business Machines, Inc. 69156952Sume * 70156952Sume * International Business Machines, Inc. (hereinafter called IBM) grants 71156952Sume * permission under its copyrights to use, copy, modify, and distribute this 72156952Sume * Software with or without fee, provided that the above copyright notice and 73156952Sume * all paragraphs of this notice appear in all copies, and that the name of IBM 74156952Sume * not be used in connection with the marketing of any product incorporating 75156952Sume * the Software or modifications thereof, without specific, written prior 76156952Sume * permission. 77156952Sume * 78156952Sume * To the extent it has a right to do so, IBM grants an immunity from suit 79156952Sume * under its patents, if any, for the use, sale or manufacture of products to 80156952Sume * the extent that such products are used for performing Domain Name System 81156952Sume * dynamic updates in TCP/IP networks by means of the Software. No immunity is 82156952Sume * granted for any product per se or for any other function of any product. 83156952Sume * 84156952Sume * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, 85156952Sume * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 86156952Sume * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, 87156952Sume * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING 88156952Sume * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN 89156952Sume * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. 90156952Sume */ 91156952Sume 92156952Sume#if defined(LIBC_SCCS) && !defined(lint) 93156952Sumestatic const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; 94270838Sumestatic const char rcsid[] = "$Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp $"; 95156952Sume#endif /* LIBC_SCCS and not lint */ 96156956Sume#include <sys/cdefs.h> 97156956Sume__FBSDID("$FreeBSD$"); 98156952Sume 99156952Sume#include "port_before.h" 100156952Sume 101156952Sume#include <sys/types.h> 102156952Sume#include <sys/param.h> 103156952Sume#include <sys/socket.h> 104156952Sume 105156952Sume#include <netinet/in.h> 106156952Sume#include <arpa/inet.h> 107156952Sume#include <arpa/nameser.h> 108156952Sume 109156952Sume#include <ctype.h> 110156952Sume#include <errno.h> 111156952Sume#include <math.h> 112156952Sume#include <netdb.h> 113156952Sume#include <resolv.h> 114156952Sume#include <resolv_mt.h> 115156952Sume#include <stdio.h> 116156952Sume#include <stdlib.h> 117156952Sume#include <string.h> 118156952Sume#include <time.h> 119156952Sume 120156952Sume#include "port_after.h" 121156952Sume 122156952Sume#ifdef SPRINTF_CHAR 123156952Sume# define SPRINTF(x) strlen(sprintf/**/x) 124156952Sume#else 125156952Sume# define SPRINTF(x) sprintf x 126156952Sume#endif 127156952Sume 128156952Sumeextern const char *_res_opcodes[]; 129156952Sumeextern const char *_res_sectioncodes[]; 130156952Sume 131170244Sume/*% 132156952Sume * Print the current options. 133156952Sume */ 134156952Sumevoid 135156952Sumefp_resstat(const res_state statp, FILE *file) { 136156952Sume u_long mask; 137156952Sume 138156952Sume fprintf(file, ";; res options:"); 139156952Sume for (mask = 1; mask != 0U; mask <<= 1) 140156952Sume if (statp->options & mask) 141156952Sume fprintf(file, " %s", p_option(mask)); 142156952Sume putc('\n', file); 143156952Sume} 144156952Sume 145156952Sumestatic void 146156952Sumedo_section(const res_state statp, 147156952Sume ns_msg *handle, ns_sect section, 148156952Sume int pflag, FILE *file) 149156952Sume{ 150156952Sume int n, sflag, rrnum; 151156952Sume static int buflen = 2048; 152156952Sume char *buf; 153156952Sume ns_opcode opcode; 154156952Sume ns_rr rr; 155156952Sume 156156952Sume /* 157156952Sume * Print answer records. 158156952Sume */ 159156952Sume sflag = (statp->pfcode & pflag); 160156952Sume if (statp->pfcode && !sflag) 161156952Sume return; 162156952Sume 163156952Sume buf = malloc(buflen); 164156952Sume if (buf == NULL) { 165156952Sume fprintf(file, ";; memory allocation failure\n"); 166156952Sume return; 167156952Sume } 168156952Sume 169156952Sume opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode); 170156952Sume rrnum = 0; 171156952Sume for (;;) { 172156952Sume if (ns_parserr(handle, section, rrnum, &rr)) { 173156952Sume if (errno != ENODEV) 174156952Sume fprintf(file, ";; ns_parserr: %s\n", 175156952Sume strerror(errno)); 176156952Sume else if (rrnum > 0 && sflag != 0 && 177156952Sume (statp->pfcode & RES_PRF_HEAD1)) 178156952Sume putc('\n', file); 179156952Sume goto cleanup; 180156952Sume } 181156952Sume if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1)) 182156952Sume fprintf(file, ";; %s SECTION:\n", 183156952Sume p_section(section, opcode)); 184156952Sume if (section == ns_s_qd) 185156952Sume fprintf(file, ";;\t%s, type = %s, class = %s\n", 186156952Sume ns_rr_name(rr), 187156952Sume p_type(ns_rr_type(rr)), 188156952Sume p_class(ns_rr_class(rr))); 189156952Sume else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { 190186090Sume u_int16_t optcode, optlen, rdatalen = ns_rr_rdlen(rr); 191156952Sume u_int32_t ttl = ns_rr_ttl(rr); 192186090Sume 193156952Sume fprintf(file, 194156952Sume "; EDNS: version: %u, udp=%u, flags=%04x\n", 195156952Sume (ttl>>16)&0xff, ns_rr_class(rr), ttl&0xffff); 196186090Sume 197186090Sume while (rdatalen >= 4) { 198186090Sume const u_char *cp = ns_rr_rdata(rr); 199186090Sume int i; 200186090Sume 201186090Sume GETSHORT(optcode, cp); 202186090Sume GETSHORT(optlen, cp); 203186090Sume 204186090Sume if (optcode == NS_OPT_NSID) { 205186090Sume fputs("; NSID: ", file); 206186090Sume if (optlen == 0) { 207186090Sume fputs("; NSID\n", file); 208186090Sume } else { 209186090Sume fputs("; NSID: ", file); 210186090Sume for (i = 0; i < optlen; i++) 211186090Sume fprintf(file, "%02x ", 212186090Sume cp[i]); 213186090Sume fputs(" (",file); 214186090Sume for (i = 0; i < optlen; i++) 215186090Sume fprintf(file, "%c", 216186090Sume isprint(cp[i])? 217186090Sume cp[i] : '.'); 218186090Sume fputs(")\n", file); 219186090Sume } 220186090Sume } else { 221186090Sume if (optlen == 0) { 222186090Sume fprintf(file, "; OPT=%u\n", 223186090Sume optcode); 224186090Sume } else { 225186090Sume fprintf(file, "; OPT=%u: ", 226186090Sume optcode); 227186090Sume for (i = 0; i < optlen; i++) 228186090Sume fprintf(file, "%02x ", 229186090Sume cp[i]); 230186090Sume fputs(" (",file); 231186090Sume for (i = 0; i < optlen; i++) 232186090Sume fprintf(file, "%c", 233186090Sume isprint(cp[i]) ? 234186090Sume cp[i] : '.'); 235186090Sume fputs(")\n", file); 236186090Sume } 237186090Sume } 238186090Sume rdatalen -= 4 + optlen; 239186090Sume } 240156952Sume } else { 241156952Sume n = ns_sprintrr(handle, &rr, NULL, NULL, 242156952Sume buf, buflen); 243156952Sume if (n < 0) { 244156952Sume if (errno == ENOSPC) { 245156952Sume free(buf); 246156952Sume buf = NULL; 247156952Sume if (buflen < 131072) 248156952Sume buf = malloc(buflen += 1024); 249156952Sume if (buf == NULL) { 250156952Sume fprintf(file, 251186090Sume ";; memory allocation failure\n"); 252156952Sume return; 253156952Sume } 254156952Sume continue; 255156952Sume } 256156952Sume fprintf(file, ";; ns_sprintrr: %s\n", 257156952Sume strerror(errno)); 258156952Sume goto cleanup; 259156952Sume } 260156952Sume fputs(buf, file); 261156952Sume fputc('\n', file); 262156952Sume } 263156952Sume rrnum++; 264156952Sume } 265156952Sume cleanup: 266156952Sume if (buf != NULL) 267156952Sume free(buf); 268156952Sume} 269156952Sume 270170244Sume/*% 271156952Sume * Print the contents of a query. 272156952Sume * This is intended to be primarily a debugging routine. 273156952Sume */ 274156952Sumevoid 275156952Sumeres_pquery(const res_state statp, const u_char *msg, int len, FILE *file) { 276156952Sume ns_msg handle; 277156952Sume int qdcount, ancount, nscount, arcount; 278156952Sume u_int opcode, rcode, id; 279156952Sume 280156952Sume if (ns_initparse(msg, len, &handle) < 0) { 281156952Sume fprintf(file, ";; ns_initparse: %s\n", strerror(errno)); 282156952Sume return; 283156952Sume } 284156952Sume opcode = ns_msg_getflag(handle, ns_f_opcode); 285156952Sume rcode = ns_msg_getflag(handle, ns_f_rcode); 286156952Sume id = ns_msg_id(handle); 287156952Sume qdcount = ns_msg_count(handle, ns_s_qd); 288156952Sume ancount = ns_msg_count(handle, ns_s_an); 289156952Sume nscount = ns_msg_count(handle, ns_s_ns); 290156952Sume arcount = ns_msg_count(handle, ns_s_ar); 291156952Sume 292156952Sume /* 293156952Sume * Print header fields. 294156952Sume */ 295156952Sume if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode) 296156952Sume fprintf(file, 297156952Sume ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", 298156952Sume _res_opcodes[opcode], p_rcode(rcode), id); 299156952Sume if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) 300156952Sume putc(';', file); 301156952Sume if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) { 302156952Sume fprintf(file, "; flags:"); 303156952Sume if (ns_msg_getflag(handle, ns_f_qr)) 304156952Sume fprintf(file, " qr"); 305156952Sume if (ns_msg_getflag(handle, ns_f_aa)) 306156952Sume fprintf(file, " aa"); 307156952Sume if (ns_msg_getflag(handle, ns_f_tc)) 308156952Sume fprintf(file, " tc"); 309156952Sume if (ns_msg_getflag(handle, ns_f_rd)) 310156952Sume fprintf(file, " rd"); 311156952Sume if (ns_msg_getflag(handle, ns_f_ra)) 312156952Sume fprintf(file, " ra"); 313156952Sume if (ns_msg_getflag(handle, ns_f_z)) 314156952Sume fprintf(file, " ??"); 315156952Sume if (ns_msg_getflag(handle, ns_f_ad)) 316156952Sume fprintf(file, " ad"); 317156952Sume if (ns_msg_getflag(handle, ns_f_cd)) 318156952Sume fprintf(file, " cd"); 319156952Sume } 320156952Sume if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) { 321156952Sume fprintf(file, "; %s: %d", 322156952Sume p_section(ns_s_qd, opcode), qdcount); 323156952Sume fprintf(file, ", %s: %d", 324156952Sume p_section(ns_s_an, opcode), ancount); 325156952Sume fprintf(file, ", %s: %d", 326156952Sume p_section(ns_s_ns, opcode), nscount); 327156952Sume fprintf(file, ", %s: %d", 328156952Sume p_section(ns_s_ar, opcode), arcount); 329156952Sume } 330270838Sume if ((!statp->pfcode) || (statp->pfcode & 331156952Sume (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { 332156952Sume putc('\n',file); 333156952Sume } 334156952Sume /* 335156952Sume * Print the various sections. 336156952Sume */ 337156952Sume do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file); 338156952Sume do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file); 339156952Sume do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file); 340156952Sume do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file); 341156952Sume if (qdcount == 0 && ancount == 0 && 342156952Sume nscount == 0 && arcount == 0) 343156952Sume putc('\n', file); 344156952Sume} 345156952Sume 346156952Sumeconst u_char * 347156952Sumep_cdnname(const u_char *cp, const u_char *msg, int len, FILE *file) { 348156952Sume char name[MAXDNAME]; 349156952Sume int n; 350156952Sume 351156952Sume if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0) 352156952Sume return (NULL); 353156952Sume if (name[0] == '\0') 354156952Sume putc('.', file); 355156952Sume else 356156952Sume fputs(name, file); 357156952Sume return (cp + n); 358156952Sume} 359156952Sume 360156952Sumeconst u_char * 361156952Sumep_cdname(const u_char *cp, const u_char *msg, FILE *file) { 362156952Sume return (p_cdnname(cp, msg, PACKETSZ, file)); 363156952Sume} 364156952Sume 365170244Sume/*% 366170244Sume * Return a fully-qualified domain name from a compressed name (with 367156952Sume length supplied). */ 368156952Sume 369156952Sumeconst u_char * 370156952Sumep_fqnname(cp, msg, msglen, name, namelen) 371156952Sume const u_char *cp, *msg; 372156952Sume int msglen; 373156952Sume char *name; 374156952Sume int namelen; 375156952Sume{ 376156952Sume int n, newlen; 377156952Sume 378156952Sume if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) 379156952Sume return (NULL); 380156952Sume newlen = strlen(name); 381156952Sume if (newlen == 0 || name[newlen - 1] != '.') { 382170244Sume if (newlen + 1 >= namelen) /*%< Lack space for final dot */ 383156952Sume return (NULL); 384156952Sume else 385156952Sume strcpy(name + newlen, "."); 386156952Sume } 387156952Sume return (cp + n); 388156952Sume} 389156952Sume 390156952Sume/* XXX: the rest of these functions need to become length-limited, too. */ 391156952Sume 392156952Sumeconst u_char * 393156952Sumep_fqname(const u_char *cp, const u_char *msg, FILE *file) { 394156952Sume char name[MAXDNAME]; 395156952Sume const u_char *n; 396156952Sume 397156952Sume n = p_fqnname(cp, msg, MAXCDNAME, name, sizeof name); 398156952Sume if (n == NULL) 399156952Sume return (NULL); 400156952Sume fputs(name, file); 401156952Sume return (n); 402156952Sume} 403156952Sume 404170244Sume/*% 405156952Sume * Names of RR classes and qclasses. Classes and qclasses are the same, except 406156952Sume * that C_ANY is a qclass but not a class. (You can ask for records of class 407156952Sume * C_ANY, but you can't have any records of that class in the database.) 408156952Sume */ 409156952Sumeconst struct res_sym __p_class_syms[] = { 410156952Sume {C_IN, "IN", (char *)0}, 411156952Sume {C_CHAOS, "CH", (char *)0}, 412156952Sume {C_CHAOS, "CHAOS", (char *)0}, 413156952Sume {C_HS, "HS", (char *)0}, 414156952Sume {C_HS, "HESIOD", (char *)0}, 415156952Sume {C_ANY, "ANY", (char *)0}, 416156952Sume {C_NONE, "NONE", (char *)0}, 417156952Sume {C_IN, (char *)0, (char *)0} 418156952Sume}; 419156952Sume 420170244Sume/*% 421156952Sume * Names of message sections. 422156952Sume */ 423156956Sumestatic const struct res_sym __p_default_section_syms[] = { 424156952Sume {ns_s_qd, "QUERY", (char *)0}, 425156952Sume {ns_s_an, "ANSWER", (char *)0}, 426156952Sume {ns_s_ns, "AUTHORITY", (char *)0}, 427156952Sume {ns_s_ar, "ADDITIONAL", (char *)0}, 428186090Sume {0, (char *)0, (char *)0} 429156952Sume}; 430156952Sume 431156956Sumestatic const struct res_sym __p_update_section_syms[] = { 432156952Sume {S_ZONE, "ZONE", (char *)0}, 433156952Sume {S_PREREQ, "PREREQUISITE", (char *)0}, 434156952Sume {S_UPDATE, "UPDATE", (char *)0}, 435156952Sume {S_ADDT, "ADDITIONAL", (char *)0}, 436186090Sume {0, (char *)0, (char *)0} 437156952Sume}; 438156952Sume 439156952Sumeconst struct res_sym __p_key_syms[] = { 440156952Sume {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"}, 441156952Sume {NS_ALG_DH, "DH", "Diffie Hellman"}, 442156952Sume {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"}, 443156952Sume {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"}, 444156952Sume {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"}, 445156952Sume {0, NULL, NULL} 446156952Sume}; 447156952Sume 448156952Sumeconst struct res_sym __p_cert_syms[] = { 449156952Sume {cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"}, 450156952Sume {cert_t_spki, "SPKI", "SPKI certificate"}, 451156952Sume {cert_t_pgp, "PGP", "PGP certificate"}, 452156952Sume {cert_t_url, "URL", "URL Private"}, 453156952Sume {cert_t_oid, "OID", "OID Private"}, 454156952Sume {0, NULL, NULL} 455156952Sume}; 456156952Sume 457170244Sume/*% 458156952Sume * Names of RR types and qtypes. Types and qtypes are the same, except 459156952Sume * that T_ANY is a qtype but not a type. (You can ask for records of type 460156952Sume * T_ANY, but you can't have any records of that type in the database.) 461156952Sume */ 462156952Sumeconst struct res_sym __p_type_syms[] = { 463156952Sume {ns_t_a, "A", "address"}, 464156952Sume {ns_t_ns, "NS", "name server"}, 465156952Sume {ns_t_md, "MD", "mail destination (deprecated)"}, 466156952Sume {ns_t_mf, "MF", "mail forwarder (deprecated)"}, 467156952Sume {ns_t_cname, "CNAME", "canonical name"}, 468156952Sume {ns_t_soa, "SOA", "start of authority"}, 469156952Sume {ns_t_mb, "MB", "mailbox"}, 470156952Sume {ns_t_mg, "MG", "mail group member"}, 471156952Sume {ns_t_mr, "MR", "mail rename"}, 472156952Sume {ns_t_null, "NULL", "null"}, 473156952Sume {ns_t_wks, "WKS", "well-known service (deprecated)"}, 474156952Sume {ns_t_ptr, "PTR", "domain name pointer"}, 475156952Sume {ns_t_hinfo, "HINFO", "host information"}, 476156952Sume {ns_t_minfo, "MINFO", "mailbox information"}, 477156952Sume {ns_t_mx, "MX", "mail exchanger"}, 478156952Sume {ns_t_txt, "TXT", "text"}, 479156952Sume {ns_t_rp, "RP", "responsible person"}, 480156952Sume {ns_t_afsdb, "AFSDB", "DCE or AFS server"}, 481156952Sume {ns_t_x25, "X25", "X25 address"}, 482156952Sume {ns_t_isdn, "ISDN", "ISDN address"}, 483156952Sume {ns_t_rt, "RT", "router"}, 484156952Sume {ns_t_nsap, "NSAP", "nsap address"}, 485156952Sume {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"}, 486156952Sume {ns_t_sig, "SIG", "signature"}, 487156952Sume {ns_t_key, "KEY", "key"}, 488156952Sume {ns_t_px, "PX", "mapping information"}, 489156952Sume {ns_t_gpos, "GPOS", "geographical position (withdrawn)"}, 490156952Sume {ns_t_aaaa, "AAAA", "IPv6 address"}, 491156952Sume {ns_t_loc, "LOC", "location"}, 492156952Sume {ns_t_nxt, "NXT", "next valid name (unimplemented)"}, 493156952Sume {ns_t_eid, "EID", "endpoint identifier (unimplemented)"}, 494156952Sume {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"}, 495156952Sume {ns_t_srv, "SRV", "server selection"}, 496156952Sume {ns_t_atma, "ATMA", "ATM address (unimplemented)"}, 497270838Sume {ns_t_naptr, "NAPTR", "naptr"}, 498270838Sume {ns_t_kx, "KX", "key exchange"}, 499270838Sume {ns_t_cert, "CERT", "certificate"}, 500270838Sume {ns_t_a6, "A", "IPv6 address (experminental)"}, 501270838Sume {ns_t_dname, "DNAME", "non-terminal redirection"}, 502270838Sume {ns_t_opt, "OPT", "opt"}, 503270838Sume {ns_t_apl, "apl", "apl"}, 504270838Sume {ns_t_ds, "DS", "delegation signer"}, 505270838Sume {ns_t_sshfp, "SSFP", "SSH fingerprint"}, 506270838Sume {ns_t_ipseckey, "IPSECKEY", "IPSEC key"}, 507270838Sume {ns_t_rrsig, "RRSIG", "rrsig"}, 508270838Sume {ns_t_nsec, "NSEC", "nsec"}, 509270838Sume {ns_t_dnskey, "DNSKEY", "DNS key"}, 510270838Sume {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"}, 511270838Sume {ns_t_nsec3, "NSEC3", "nsec3"}, 512270838Sume {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"}, 513270838Sume {ns_t_hip, "HIP", "host identity protocol"}, 514270838Sume {ns_t_spf, "SPF", "sender policy framework"}, 515156952Sume {ns_t_tkey, "TKEY", "tkey"}, 516156952Sume {ns_t_tsig, "TSIG", "transaction signature"}, 517156952Sume {ns_t_ixfr, "IXFR", "incremental zone transfer"}, 518156952Sume {ns_t_axfr, "AXFR", "zone transfer"}, 519156952Sume {ns_t_zxfr, "ZXFR", "compressed zone transfer"}, 520156952Sume {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"}, 521156952Sume {ns_t_maila, "MAILA", "mail agent (deprecated)"}, 522156952Sume {ns_t_naptr, "NAPTR", "URN Naming Authority"}, 523156952Sume {ns_t_kx, "KX", "Key Exchange"}, 524156952Sume {ns_t_cert, "CERT", "Certificate"}, 525156952Sume {ns_t_a6, "A6", "IPv6 Address"}, 526156952Sume {ns_t_dname, "DNAME", "dname"}, 527156952Sume {ns_t_sink, "SINK", "Kitchen Sink (experimental)"}, 528156952Sume {ns_t_opt, "OPT", "EDNS Options"}, 529156952Sume {ns_t_any, "ANY", "\"any\""}, 530270838Sume {ns_t_dlv, "DLV", "DNSSEC look-aside validation"}, 531156952Sume {0, NULL, NULL} 532156952Sume}; 533156952Sume 534170244Sume/*% 535156952Sume * Names of DNS rcodes. 536156952Sume */ 537168441Skanconst struct res_sym __p_rcode_syms[] = { 538156952Sume {ns_r_noerror, "NOERROR", "no error"}, 539156952Sume {ns_r_formerr, "FORMERR", "format error"}, 540156952Sume {ns_r_servfail, "SERVFAIL", "server failed"}, 541156952Sume {ns_r_nxdomain, "NXDOMAIN", "no such domain name"}, 542156952Sume {ns_r_notimpl, "NOTIMP", "not implemented"}, 543156952Sume {ns_r_refused, "REFUSED", "refused"}, 544156952Sume {ns_r_yxdomain, "YXDOMAIN", "domain name exists"}, 545156952Sume {ns_r_yxrrset, "YXRRSET", "rrset exists"}, 546156952Sume {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"}, 547156952Sume {ns_r_notauth, "NOTAUTH", "not authoritative"}, 548156952Sume {ns_r_notzone, "NOTZONE", "Not in zone"}, 549156952Sume {ns_r_max, "", ""}, 550156952Sume {ns_r_badsig, "BADSIG", "bad signature"}, 551156952Sume {ns_r_badkey, "BADKEY", "bad key"}, 552156952Sume {ns_r_badtime, "BADTIME", "bad time"}, 553156952Sume {0, NULL, NULL} 554156952Sume}; 555156952Sume 556156952Sumeint 557156952Sumesym_ston(const struct res_sym *syms, const char *name, int *success) { 558156952Sume for ((void)NULL; syms->name != 0; syms++) { 559156952Sume if (strcasecmp (name, syms->name) == 0) { 560156952Sume if (success) 561156952Sume *success = 1; 562156952Sume return (syms->number); 563156952Sume } 564156952Sume } 565156952Sume if (success) 566156952Sume *success = 0; 567170244Sume return (syms->number); /*%< The default value. */ 568156952Sume} 569156952Sume 570156952Sumeconst char * 571156952Sumesym_ntos(const struct res_sym *syms, int number, int *success) { 572156952Sume char *unname = sym_ntos_unname; 573156952Sume 574156952Sume for ((void)NULL; syms->name != 0; syms++) { 575156952Sume if (number == syms->number) { 576156952Sume if (success) 577156952Sume *success = 1; 578156952Sume return (syms->name); 579156952Sume } 580156952Sume } 581156952Sume 582170244Sume sprintf(unname, "%d", number); /*%< XXX nonreentrant */ 583156952Sume if (success) 584156952Sume *success = 0; 585156952Sume return (unname); 586156952Sume} 587156952Sume 588156952Sumeconst char * 589156952Sumesym_ntop(const struct res_sym *syms, int number, int *success) { 590156952Sume char *unname = sym_ntop_unname; 591156952Sume 592156952Sume for ((void)NULL; syms->name != 0; syms++) { 593156952Sume if (number == syms->number) { 594156952Sume if (success) 595156952Sume *success = 1; 596156952Sume return (syms->humanname); 597156952Sume } 598156952Sume } 599170244Sume sprintf(unname, "%d", number); /*%< XXX nonreentrant */ 600156952Sume if (success) 601156952Sume *success = 0; 602156952Sume return (unname); 603156952Sume} 604156952Sume 605170244Sume/*% 606156952Sume * Return a string for the type. 607156952Sume */ 608156952Sumeconst char * 609156952Sumep_type(int type) { 610156952Sume int success; 611156952Sume const char *result; 612156952Sume static char typebuf[20]; 613156952Sume 614156952Sume result = sym_ntos(__p_type_syms, type, &success); 615156952Sume if (success) 616156952Sume return (result); 617156952Sume if (type < 0 || type > 0xffff) 618156952Sume return ("BADTYPE"); 619156952Sume sprintf(typebuf, "TYPE%d", type); 620156952Sume return (typebuf); 621156952Sume} 622156952Sume 623170244Sume/*% 624156952Sume * Return a string for the type. 625156952Sume */ 626156952Sumeconst char * 627156952Sumep_section(int section, int opcode) { 628156952Sume const struct res_sym *symbols; 629156952Sume 630156952Sume switch (opcode) { 631156952Sume case ns_o_update: 632156952Sume symbols = __p_update_section_syms; 633156952Sume break; 634156952Sume default: 635156952Sume symbols = __p_default_section_syms; 636156952Sume break; 637156952Sume } 638156952Sume return (sym_ntos(symbols, section, (int *)0)); 639156952Sume} 640156952Sume 641170244Sume/*% 642156952Sume * Return a mnemonic for class. 643156952Sume */ 644156952Sumeconst char * 645156952Sumep_class(int class) { 646156952Sume int success; 647156952Sume const char *result; 648156952Sume static char classbuf[20]; 649156952Sume 650156952Sume result = sym_ntos(__p_class_syms, class, &success); 651156952Sume if (success) 652156952Sume return (result); 653156952Sume if (class < 0 || class > 0xffff) 654156952Sume return ("BADCLASS"); 655156952Sume sprintf(classbuf, "CLASS%d", class); 656156952Sume return (classbuf); 657156952Sume} 658156952Sume 659170244Sume/*% 660156952Sume * Return a mnemonic for an option 661156952Sume */ 662156952Sumeconst char * 663156952Sumep_option(u_long option) { 664156952Sume char *nbuf = p_option_nbuf; 665156952Sume 666156952Sume switch (option) { 667156952Sume case RES_INIT: return "init"; 668156952Sume case RES_DEBUG: return "debug"; 669156952Sume case RES_AAONLY: return "aaonly(unimpl)"; 670156952Sume case RES_USEVC: return "usevc"; 671156952Sume case RES_PRIMARY: return "primry(unimpl)"; 672156952Sume case RES_IGNTC: return "igntc"; 673156952Sume case RES_RECURSE: return "recurs"; 674156952Sume case RES_DEFNAMES: return "defnam"; 675156952Sume case RES_STAYOPEN: return "styopn"; 676156952Sume case RES_DNSRCH: return "dnsrch"; 677156952Sume case RES_INSECURE1: return "insecure1"; 678156952Sume case RES_INSECURE2: return "insecure2"; 679156952Sume case RES_NOALIASES: return "noaliases"; 680156952Sume case RES_USE_INET6: return "inet6"; 681170244Sume#ifdef RES_USE_EDNS0 /*%< KAME extension */ 682156952Sume case RES_USE_EDNS0: return "edns0"; 683186090Sume case RES_NSID: return "nsid"; 684156952Sume#endif 685156952Sume#ifdef RES_USE_DNAME 686156952Sume case RES_USE_DNAME: return "dname"; 687156952Sume#endif 688156952Sume#ifdef RES_USE_DNSSEC 689156952Sume case RES_USE_DNSSEC: return "dnssec"; 690156952Sume#endif 691156952Sume#ifdef RES_NOTLDQUERY 692156952Sume case RES_NOTLDQUERY: return "no-tld-query"; 693156952Sume#endif 694156952Sume#ifdef RES_NO_NIBBLE2 695156952Sume case RES_NO_NIBBLE2: return "no-nibble2"; 696156952Sume#endif 697156952Sume /* XXX nonreentrant */ 698156952Sume default: sprintf(nbuf, "?0x%lx?", (u_long)option); 699156952Sume return (nbuf); 700156952Sume } 701156952Sume} 702156952Sume 703170244Sume/*% 704156952Sume * Return a mnemonic for a time to live. 705156952Sume */ 706156952Sumeconst char * 707156952Sumep_time(u_int32_t value) { 708156952Sume char *nbuf = p_time_nbuf; 709156952Sume 710156952Sume if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0) 711156952Sume sprintf(nbuf, "%u", value); 712156952Sume return (nbuf); 713156952Sume} 714156952Sume 715170244Sume/*% 716156952Sume * Return a string for the rcode. 717156952Sume */ 718156952Sumeconst char * 719156952Sumep_rcode(int rcode) { 720156952Sume return (sym_ntos(__p_rcode_syms, rcode, (int *)0)); 721156952Sume} 722156952Sume 723170244Sume/*% 724156952Sume * Return a string for a res_sockaddr_union. 725156952Sume */ 726156952Sumeconst char * 727156952Sumep_sockun(union res_sockaddr_union u, char *buf, size_t size) { 728156952Sume char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"]; 729156952Sume 730156952Sume switch (u.sin.sin_family) { 731156952Sume case AF_INET: 732156952Sume inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret); 733156952Sume break; 734156952Sume#ifdef HAS_INET6_STRUCTS 735156952Sume case AF_INET6: 736156952Sume inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret); 737156952Sume break; 738156952Sume#endif 739156952Sume default: 740156952Sume sprintf(ret, "[af%d]", u.sin.sin_family); 741156952Sume break; 742156952Sume } 743156952Sume if (size > 0U) { 744156952Sume strncpy(buf, ret, size - 1); 745156952Sume buf[size - 1] = '0'; 746156952Sume } 747156952Sume return (buf); 748156952Sume} 749156952Sume 750170244Sume/*% 751156952Sume * routines to convert between on-the-wire RR format and zone file format. 752156952Sume * Does not contain conversion to/from decimal degrees; divide or multiply 753156952Sume * by 60*60*1000 for that. 754156952Sume */ 755156952Sume 756156952Sumestatic unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, 757156952Sume 1000000,10000000,100000000,1000000000}; 758156952Sume 759170244Sume/*% takes an XeY precision/size value, returns a string representation. */ 760156952Sumestatic const char * 761156952Sumeprecsize_ntoa(prec) 762156952Sume u_int8_t prec; 763156952Sume{ 764156952Sume char *retbuf = precsize_ntoa_retbuf; 765156952Sume unsigned long val; 766156952Sume int mantissa, exponent; 767156952Sume 768156952Sume mantissa = (int)((prec >> 4) & 0x0f) % 10; 769156952Sume exponent = (int)((prec >> 0) & 0x0f) % 10; 770156952Sume 771156952Sume val = mantissa * poweroften[exponent]; 772156952Sume 773156952Sume (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100); 774156952Sume return (retbuf); 775156952Sume} 776156952Sume 777170244Sume/*% converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ 778156952Sumestatic u_int8_t 779156952Sumeprecsize_aton(const char **strptr) { 780156952Sume unsigned int mval = 0, cmval = 0; 781156952Sume u_int8_t retval = 0; 782156952Sume const char *cp; 783156952Sume int exponent; 784156952Sume int mantissa; 785156952Sume 786156952Sume cp = *strptr; 787156952Sume 788156952Sume while (isdigit((unsigned char)*cp)) 789156952Sume mval = mval * 10 + (*cp++ - '0'); 790156952Sume 791170244Sume if (*cp == '.') { /*%< centimeters */ 792156952Sume cp++; 793156952Sume if (isdigit((unsigned char)*cp)) { 794156952Sume cmval = (*cp++ - '0') * 10; 795156952Sume if (isdigit((unsigned char)*cp)) { 796156952Sume cmval += (*cp++ - '0'); 797156952Sume } 798156952Sume } 799156952Sume } 800156952Sume cmval = (mval * 100) + cmval; 801156952Sume 802156952Sume for (exponent = 0; exponent < 9; exponent++) 803156952Sume if (cmval < poweroften[exponent+1]) 804156952Sume break; 805156952Sume 806156952Sume mantissa = cmval / poweroften[exponent]; 807156952Sume if (mantissa > 9) 808156952Sume mantissa = 9; 809156952Sume 810156952Sume retval = (mantissa << 4) | exponent; 811156952Sume 812156952Sume *strptr = cp; 813156952Sume 814156952Sume return (retval); 815156952Sume} 816156952Sume 817170244Sume/*% converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ 818156952Sumestatic u_int32_t 819156952Sumelatlon2ul(const char **latlonstrptr, int *which) { 820156952Sume const char *cp; 821156952Sume u_int32_t retval; 822156952Sume int deg = 0, min = 0, secs = 0, secsfrac = 0; 823156952Sume 824156952Sume cp = *latlonstrptr; 825156952Sume 826156952Sume while (isdigit((unsigned char)*cp)) 827156952Sume deg = deg * 10 + (*cp++ - '0'); 828156952Sume 829156952Sume while (isspace((unsigned char)*cp)) 830156952Sume cp++; 831156952Sume 832156952Sume if (!(isdigit((unsigned char)*cp))) 833156952Sume goto fndhemi; 834156952Sume 835156952Sume while (isdigit((unsigned char)*cp)) 836156952Sume min = min * 10 + (*cp++ - '0'); 837156952Sume 838156952Sume while (isspace((unsigned char)*cp)) 839156952Sume cp++; 840156952Sume 841156952Sume if (!(isdigit((unsigned char)*cp))) 842156952Sume goto fndhemi; 843156952Sume 844156952Sume while (isdigit((unsigned char)*cp)) 845156952Sume secs = secs * 10 + (*cp++ - '0'); 846156952Sume 847170244Sume if (*cp == '.') { /*%< decimal seconds */ 848156952Sume cp++; 849156952Sume if (isdigit((unsigned char)*cp)) { 850156952Sume secsfrac = (*cp++ - '0') * 100; 851156952Sume if (isdigit((unsigned char)*cp)) { 852156952Sume secsfrac += (*cp++ - '0') * 10; 853156952Sume if (isdigit((unsigned char)*cp)) { 854156952Sume secsfrac += (*cp++ - '0'); 855156952Sume } 856156952Sume } 857156952Sume } 858156952Sume } 859156952Sume 860170244Sume while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */ 861156952Sume cp++; 862156952Sume 863156952Sume while (isspace((unsigned char)*cp)) 864156952Sume cp++; 865156952Sume 866156952Sume fndhemi: 867156952Sume switch (*cp) { 868156952Sume case 'N': case 'n': 869156952Sume case 'E': case 'e': 870156952Sume retval = ((unsigned)1<<31) 871156952Sume + (((((deg * 60) + min) * 60) + secs) * 1000) 872156952Sume + secsfrac; 873156952Sume break; 874156952Sume case 'S': case 's': 875156952Sume case 'W': case 'w': 876156952Sume retval = ((unsigned)1<<31) 877156952Sume - (((((deg * 60) + min) * 60) + secs) * 1000) 878156952Sume - secsfrac; 879156952Sume break; 880156952Sume default: 881170244Sume retval = 0; /*%< invalid value -- indicates error */ 882156952Sume break; 883156952Sume } 884156952Sume 885156952Sume switch (*cp) { 886156952Sume case 'N': case 'n': 887156952Sume case 'S': case 's': 888170244Sume *which = 1; /*%< latitude */ 889156952Sume break; 890156952Sume case 'E': case 'e': 891156952Sume case 'W': case 'w': 892170244Sume *which = 2; /*%< longitude */ 893156952Sume break; 894156952Sume default: 895170244Sume *which = 0; /*%< error */ 896156952Sume break; 897156952Sume } 898156952Sume 899170244Sume cp++; /*%< skip the hemisphere */ 900170244Sume while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */ 901156952Sume cp++; 902156952Sume 903170244Sume while (isspace((unsigned char)*cp)) /*%< move to next field */ 904156952Sume cp++; 905156952Sume 906156952Sume *latlonstrptr = cp; 907156952Sume 908156952Sume return (retval); 909156952Sume} 910156952Sume 911170244Sume/*% 912170244Sume * converts a zone file representation in a string to an RDATA on-the-wire 913156952Sume * representation. */ 914156952Sumeint 915156952Sumeloc_aton(ascii, binary) 916156952Sume const char *ascii; 917156952Sume u_char *binary; 918156952Sume{ 919156952Sume const char *cp, *maxcp; 920156952Sume u_char *bcp; 921156952Sume 922156952Sume u_int32_t latit = 0, longit = 0, alt = 0; 923156952Sume u_int32_t lltemp1 = 0, lltemp2 = 0; 924156952Sume int altmeters = 0, altfrac = 0, altsign = 1; 925170244Sume u_int8_t hp = 0x16; /*%< default = 1e6 cm = 10000.00m = 10km */ 926170244Sume u_int8_t vp = 0x13; /*%< default = 1e3 cm = 10.00m */ 927170244Sume u_int8_t siz = 0x12; /*%< default = 1e2 cm = 1.00m */ 928156952Sume int which1 = 0, which2 = 0; 929156952Sume 930156952Sume cp = ascii; 931156952Sume maxcp = cp + strlen(ascii); 932156952Sume 933156952Sume lltemp1 = latlon2ul(&cp, &which1); 934156952Sume 935156952Sume lltemp2 = latlon2ul(&cp, &which2); 936156952Sume 937156952Sume switch (which1 + which2) { 938170244Sume case 3: /*%< 1 + 2, the only valid combination */ 939170244Sume if ((which1 == 1) && (which2 == 2)) { /*%< normal case */ 940156952Sume latit = lltemp1; 941156952Sume longit = lltemp2; 942170244Sume } else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */ 943156952Sume longit = lltemp1; 944156952Sume latit = lltemp2; 945170244Sume } else { /*%< some kind of brokenness */ 946156952Sume return (0); 947156952Sume } 948156952Sume break; 949170244Sume default: /*%< we didn't get one of each */ 950156952Sume return (0); 951156952Sume } 952156952Sume 953156952Sume /* altitude */ 954156952Sume if (*cp == '-') { 955156952Sume altsign = -1; 956156952Sume cp++; 957156952Sume } 958270838Sume 959156952Sume if (*cp == '+') 960156952Sume cp++; 961156952Sume 962156952Sume while (isdigit((unsigned char)*cp)) 963156952Sume altmeters = altmeters * 10 + (*cp++ - '0'); 964156952Sume 965170244Sume if (*cp == '.') { /*%< decimal meters */ 966156952Sume cp++; 967156952Sume if (isdigit((unsigned char)*cp)) { 968156952Sume altfrac = (*cp++ - '0') * 10; 969156952Sume if (isdigit((unsigned char)*cp)) { 970156952Sume altfrac += (*cp++ - '0'); 971156952Sume } 972156952Sume } 973156952Sume } 974156952Sume 975156952Sume alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); 976156952Sume 977170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 978156952Sume cp++; 979156952Sume 980156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 981156952Sume cp++; 982156952Sume 983156952Sume if (cp >= maxcp) 984156952Sume goto defaults; 985156952Sume 986156952Sume siz = precsize_aton(&cp); 987270838Sume 988170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 989156952Sume cp++; 990156952Sume 991156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 992156952Sume cp++; 993156952Sume 994156952Sume if (cp >= maxcp) 995156952Sume goto defaults; 996156952Sume 997156952Sume hp = precsize_aton(&cp); 998156952Sume 999170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 1000156952Sume cp++; 1001156952Sume 1002156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 1003156952Sume cp++; 1004156952Sume 1005156952Sume if (cp >= maxcp) 1006156952Sume goto defaults; 1007156952Sume 1008156952Sume vp = precsize_aton(&cp); 1009156952Sume 1010156952Sume defaults: 1011156952Sume 1012156952Sume bcp = binary; 1013170244Sume *bcp++ = (u_int8_t) 0; /*%< version byte */ 1014156952Sume *bcp++ = siz; 1015156952Sume *bcp++ = hp; 1016156952Sume *bcp++ = vp; 1017156952Sume PUTLONG(latit,bcp); 1018156952Sume PUTLONG(longit,bcp); 1019156952Sume PUTLONG(alt,bcp); 1020270838Sume 1021170244Sume return (16); /*%< size of RR in octets */ 1022156952Sume} 1023156952Sume 1024170244Sume/*% takes an on-the-wire LOC RR and formats it in a human readable format. */ 1025156952Sumeconst char * 1026156952Sumeloc_ntoa(binary, ascii) 1027156952Sume const u_char *binary; 1028156952Sume char *ascii; 1029156952Sume{ 1030156952Sume static const char *error = "?"; 1031156952Sume static char tmpbuf[sizeof 1032156952Sume"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; 1033156952Sume const u_char *cp = binary; 1034156952Sume 1035156952Sume int latdeg, latmin, latsec, latsecfrac; 1036156952Sume int longdeg, longmin, longsec, longsecfrac; 1037156952Sume char northsouth, eastwest; 1038156952Sume const char *altsign; 1039156952Sume int altmeters, altfrac; 1040156952Sume 1041156952Sume const u_int32_t referencealt = 100000 * 100; 1042156952Sume 1043156952Sume int32_t latval, longval, altval; 1044156952Sume u_int32_t templ; 1045156952Sume u_int8_t sizeval, hpval, vpval, versionval; 1046270838Sume 1047156952Sume char *sizestr, *hpstr, *vpstr; 1048156952Sume 1049156952Sume versionval = *cp++; 1050156952Sume 1051156952Sume if (ascii == NULL) 1052156952Sume ascii = tmpbuf; 1053156952Sume 1054156952Sume if (versionval) { 1055156952Sume (void) sprintf(ascii, "; error: unknown LOC RR version"); 1056156952Sume return (ascii); 1057156952Sume } 1058156952Sume 1059156952Sume sizeval = *cp++; 1060156952Sume 1061156952Sume hpval = *cp++; 1062156952Sume vpval = *cp++; 1063156952Sume 1064156952Sume GETLONG(templ, cp); 1065156952Sume latval = (templ - ((unsigned)1<<31)); 1066156952Sume 1067156952Sume GETLONG(templ, cp); 1068156952Sume longval = (templ - ((unsigned)1<<31)); 1069156952Sume 1070156952Sume GETLONG(templ, cp); 1071170244Sume if (templ < referencealt) { /*%< below WGS 84 spheroid */ 1072156952Sume altval = referencealt - templ; 1073156952Sume altsign = "-"; 1074156952Sume } else { 1075156952Sume altval = templ - referencealt; 1076156952Sume altsign = ""; 1077156952Sume } 1078156952Sume 1079156952Sume if (latval < 0) { 1080156952Sume northsouth = 'S'; 1081156952Sume latval = -latval; 1082156952Sume } else 1083156952Sume northsouth = 'N'; 1084156952Sume 1085156952Sume latsecfrac = latval % 1000; 1086156952Sume latval = latval / 1000; 1087156952Sume latsec = latval % 60; 1088156952Sume latval = latval / 60; 1089156952Sume latmin = latval % 60; 1090156952Sume latval = latval / 60; 1091156952Sume latdeg = latval; 1092156952Sume 1093156952Sume if (longval < 0) { 1094156952Sume eastwest = 'W'; 1095156952Sume longval = -longval; 1096156952Sume } else 1097156952Sume eastwest = 'E'; 1098156952Sume 1099156952Sume longsecfrac = longval % 1000; 1100156952Sume longval = longval / 1000; 1101156952Sume longsec = longval % 60; 1102156952Sume longval = longval / 60; 1103156952Sume longmin = longval % 60; 1104156952Sume longval = longval / 60; 1105156952Sume longdeg = longval; 1106156952Sume 1107156952Sume altfrac = altval % 100; 1108156952Sume altmeters = (altval / 100); 1109156952Sume 1110156952Sume sizestr = strdup(precsize_ntoa(sizeval)); 1111156952Sume hpstr = strdup(precsize_ntoa(hpval)); 1112156952Sume vpstr = strdup(precsize_ntoa(vpval)); 1113156952Sume 1114156952Sume sprintf(ascii, 1115156952Sume "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm", 1116156952Sume latdeg, latmin, latsec, latsecfrac, northsouth, 1117156952Sume longdeg, longmin, longsec, longsecfrac, eastwest, 1118156952Sume altsign, altmeters, altfrac, 1119156952Sume (sizestr != NULL) ? sizestr : error, 1120156952Sume (hpstr != NULL) ? hpstr : error, 1121156952Sume (vpstr != NULL) ? vpstr : error); 1122156952Sume 1123156952Sume if (sizestr != NULL) 1124156952Sume free(sizestr); 1125156952Sume if (hpstr != NULL) 1126156952Sume free(hpstr); 1127156952Sume if (vpstr != NULL) 1128156952Sume free(vpstr); 1129156952Sume 1130156952Sume return (ascii); 1131156952Sume} 1132156952Sume 1133156952Sume 1134170244Sume/*% Return the number of DNS hierarchy levels in the name. */ 1135156952Sumeint 1136156952Sumedn_count_labels(const char *name) { 1137156952Sume int i, len, count; 1138156952Sume 1139156952Sume len = strlen(name); 1140156952Sume for (i = 0, count = 0; i < len; i++) { 1141156952Sume /* XXX need to check for \. or use named's nlabels(). */ 1142156952Sume if (name[i] == '.') 1143156952Sume count++; 1144156952Sume } 1145156952Sume 1146156952Sume /* don't count initial wildcard */ 1147156952Sume if (name[0] == '*') 1148156952Sume if (count) 1149156952Sume count--; 1150156952Sume 1151156952Sume /* don't count the null label for root. */ 1152156952Sume /* if terminating '.' not found, must adjust */ 1153156952Sume /* count to include last label */ 1154156952Sume if (len > 0 && name[len-1] != '.') 1155156952Sume count++; 1156156952Sume return (count); 1157156952Sume} 1158156952Sume 1159170244Sume/*% 1160270838Sume * Make dates expressed in seconds-since-Jan-1-1970 easy to read. 1161156952Sume * SIG records are required to be printed like this, by the Secure DNS RFC. 1162156952Sume */ 1163156952Sumechar * 1164156952Sumep_secstodate (u_long secs) { 1165156952Sume char *output = p_secstodate_output; 1166156952Sume time_t clock = secs; 1167156952Sume struct tm *time; 1168156952Sume#ifdef HAVE_TIME_R 1169156952Sume struct tm res; 1170270838Sume 1171156952Sume time = gmtime_r(&clock, &res); 1172156952Sume#else 1173156952Sume time = gmtime(&clock); 1174156952Sume#endif 1175156952Sume time->tm_year += 1900; 1176156952Sume time->tm_mon += 1; 1177156952Sume sprintf(output, "%04d%02d%02d%02d%02d%02d", 1178156952Sume time->tm_year, time->tm_mon, time->tm_mday, 1179156952Sume time->tm_hour, time->tm_min, time->tm_sec); 1180156952Sume return (output); 1181156952Sume} 1182156952Sume 1183156952Sumeu_int16_t 1184156952Sumeres_nametoclass(const char *buf, int *successp) { 1185156952Sume unsigned long result; 1186156952Sume char *endptr; 1187156952Sume int success; 1188156952Sume 1189156952Sume result = sym_ston(__p_class_syms, buf, &success); 1190156952Sume if (success) 1191156952Sume goto done; 1192156952Sume 1193156952Sume if (strncasecmp(buf, "CLASS", 5) != 0 || 1194156952Sume !isdigit((unsigned char)buf[5])) 1195156952Sume goto done; 1196156952Sume errno = 0; 1197156952Sume result = strtoul(buf + 5, &endptr, 10); 1198156952Sume if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1199156952Sume success = 1; 1200156952Sume done: 1201156952Sume if (successp) 1202156952Sume *successp = success; 1203156952Sume return (result); 1204156952Sume} 1205156952Sume 1206156952Sumeu_int16_t 1207156952Sumeres_nametotype(const char *buf, int *successp) { 1208156952Sume unsigned long result; 1209156952Sume char *endptr; 1210156952Sume int success; 1211156952Sume 1212156952Sume result = sym_ston(__p_type_syms, buf, &success); 1213156952Sume if (success) 1214156952Sume goto done; 1215156952Sume 1216156952Sume if (strncasecmp(buf, "type", 4) != 0 || 1217156952Sume !isdigit((unsigned char)buf[4])) 1218156952Sume goto done; 1219156952Sume errno = 0; 1220156952Sume result = strtoul(buf + 4, &endptr, 10); 1221156952Sume if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1222156952Sume success = 1; 1223156952Sume done: 1224156952Sume if (successp) 1225156952Sume *successp = success; 1226156952Sume return (result); 1227156952Sume} 1228156956Sume 1229156956Sume/* 1230156956Sume * Weak aliases for applications that use certain private entry points, 1231156956Sume * and fail to include <resolv.h>. 1232156956Sume */ 1233156956Sume#undef fp_resstat 1234156956Sume__weak_reference(__fp_resstat, fp_resstat); 1235156956Sume#undef p_fqnname 1236156956Sume__weak_reference(__p_fqnname, p_fqnname); 1237156956Sume#undef sym_ston 1238156956Sume__weak_reference(__sym_ston, sym_ston); 1239156956Sume#undef sym_ntos 1240156956Sume__weak_reference(__sym_ntos, sym_ntos); 1241156956Sume#undef sym_ntop 1242156956Sume__weak_reference(__sym_ntop, sym_ntop); 1243156956Sume#undef dn_count_labels 1244156956Sume__weak_reference(__dn_count_labels, dn_count_labels); 1245156956Sume#undef p_secstodate 1246156956Sume__weak_reference(__p_secstodate, p_secstodate); 1247170244Sume 1248170244Sume/*! \file */ 1249