1156952Sume/* 2156952Sume * Copyright (c) 1985 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 4. Neither the name of the University nor the names of its contributors 14156952Sume * may be used to endorse or promote products derived from this software 15156952Sume * without specific prior written permission. 16156952Sume * 17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27156952Sume * SUCH DAMAGE. 28156952Sume */ 29156952Sume 30156952Sume/* 31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32156952Sume * 33156952Sume * Permission to use, copy, modify, and distribute this software for any 34156952Sume * purpose with or without fee is hereby granted, provided that the above 35156952Sume * copyright notice and this permission notice appear in all copies, and that 36156952Sume * the name of Digital Equipment Corporation not be used in advertising or 37156952Sume * publicity pertaining to distribution of the document or software without 38156952Sume * specific, written prior permission. 39156952Sume * 40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47156952Sume * SOFTWARE. 48156952Sume */ 49156952Sume 50156952Sume/* 51156952Sume * Portions Copyright (c) 1995 by International Business Machines, Inc. 52156952Sume * 53156952Sume * International Business Machines, Inc. (hereinafter called IBM) grants 54156952Sume * permission under its copyrights to use, copy, modify, and distribute this 55156952Sume * Software with or without fee, provided that the above copyright notice and 56156952Sume * all paragraphs of this notice appear in all copies, and that the name of IBM 57156952Sume * not be used in connection with the marketing of any product incorporating 58156952Sume * the Software or modifications thereof, without specific, written prior 59156952Sume * permission. 60156952Sume * 61156952Sume * To the extent it has a right to do so, IBM grants an immunity from suit 62156952Sume * under its patents, if any, for the use, sale or manufacture of products to 63156952Sume * the extent that such products are used for performing Domain Name System 64156952Sume * dynamic updates in TCP/IP networks by means of the Software. No immunity is 65156952Sume * granted for any product per se or for any other function of any product. 66156952Sume * 67156952Sume * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, 68156952Sume * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 69156952Sume * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, 70156952Sume * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING 71156952Sume * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN 72156952Sume * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. 73156952Sume */ 74156952Sume 75156952Sume/* 76156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 77156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 78156952Sume * 79156952Sume * Permission to use, copy, modify, and distribute this software for any 80156952Sume * purpose with or without fee is hereby granted, provided that the above 81156952Sume * copyright notice and this permission notice appear in all copies. 82156952Sume * 83156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 84156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 85156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 86156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 87156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 88156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 89156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 90156952Sume */ 91156952Sume 92156952Sume#if defined(LIBC_SCCS) && !defined(lint) 93156952Sumestatic const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; 94186090Sumestatic const char rcsid[] = "$Id: res_debug.c,v 1.10.18.6 2008/04/03 23:15:15 marka 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 } 330156952Sume 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)"}, 497156952Sume {ns_t_tkey, "TKEY", "tkey"}, 498156952Sume {ns_t_tsig, "TSIG", "transaction signature"}, 499156952Sume {ns_t_ixfr, "IXFR", "incremental zone transfer"}, 500156952Sume {ns_t_axfr, "AXFR", "zone transfer"}, 501156952Sume {ns_t_zxfr, "ZXFR", "compressed zone transfer"}, 502156952Sume {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"}, 503156952Sume {ns_t_maila, "MAILA", "mail agent (deprecated)"}, 504156952Sume {ns_t_naptr, "NAPTR", "URN Naming Authority"}, 505156952Sume {ns_t_kx, "KX", "Key Exchange"}, 506156952Sume {ns_t_cert, "CERT", "Certificate"}, 507156952Sume {ns_t_a6, "A6", "IPv6 Address"}, 508156952Sume {ns_t_dname, "DNAME", "dname"}, 509156952Sume {ns_t_sink, "SINK", "Kitchen Sink (experimental)"}, 510156952Sume {ns_t_opt, "OPT", "EDNS Options"}, 511156952Sume {ns_t_any, "ANY", "\"any\""}, 512156952Sume {0, NULL, NULL} 513156952Sume}; 514156952Sume 515170244Sume/*% 516156952Sume * Names of DNS rcodes. 517156952Sume */ 518168441Skanconst struct res_sym __p_rcode_syms[] = { 519156952Sume {ns_r_noerror, "NOERROR", "no error"}, 520156952Sume {ns_r_formerr, "FORMERR", "format error"}, 521156952Sume {ns_r_servfail, "SERVFAIL", "server failed"}, 522156952Sume {ns_r_nxdomain, "NXDOMAIN", "no such domain name"}, 523156952Sume {ns_r_notimpl, "NOTIMP", "not implemented"}, 524156952Sume {ns_r_refused, "REFUSED", "refused"}, 525156952Sume {ns_r_yxdomain, "YXDOMAIN", "domain name exists"}, 526156952Sume {ns_r_yxrrset, "YXRRSET", "rrset exists"}, 527156952Sume {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"}, 528156952Sume {ns_r_notauth, "NOTAUTH", "not authoritative"}, 529156952Sume {ns_r_notzone, "NOTZONE", "Not in zone"}, 530156952Sume {ns_r_max, "", ""}, 531156952Sume {ns_r_badsig, "BADSIG", "bad signature"}, 532156952Sume {ns_r_badkey, "BADKEY", "bad key"}, 533156952Sume {ns_r_badtime, "BADTIME", "bad time"}, 534156952Sume {0, NULL, NULL} 535156952Sume}; 536156952Sume 537156952Sumeint 538156952Sumesym_ston(const struct res_sym *syms, const char *name, int *success) { 539156952Sume for ((void)NULL; syms->name != 0; syms++) { 540156952Sume if (strcasecmp (name, syms->name) == 0) { 541156952Sume if (success) 542156952Sume *success = 1; 543156952Sume return (syms->number); 544156952Sume } 545156952Sume } 546156952Sume if (success) 547156952Sume *success = 0; 548170244Sume return (syms->number); /*%< The default value. */ 549156952Sume} 550156952Sume 551156952Sumeconst char * 552156952Sumesym_ntos(const struct res_sym *syms, int number, int *success) { 553156952Sume char *unname = sym_ntos_unname; 554156952Sume 555156952Sume for ((void)NULL; syms->name != 0; syms++) { 556156952Sume if (number == syms->number) { 557156952Sume if (success) 558156952Sume *success = 1; 559156952Sume return (syms->name); 560156952Sume } 561156952Sume } 562156952Sume 563170244Sume sprintf(unname, "%d", number); /*%< XXX nonreentrant */ 564156952Sume if (success) 565156952Sume *success = 0; 566156952Sume return (unname); 567156952Sume} 568156952Sume 569156952Sumeconst char * 570156952Sumesym_ntop(const struct res_sym *syms, int number, int *success) { 571156952Sume char *unname = sym_ntop_unname; 572156952Sume 573156952Sume for ((void)NULL; syms->name != 0; syms++) { 574156952Sume if (number == syms->number) { 575156952Sume if (success) 576156952Sume *success = 1; 577156952Sume return (syms->humanname); 578156952Sume } 579156952Sume } 580170244Sume sprintf(unname, "%d", number); /*%< XXX nonreentrant */ 581156952Sume if (success) 582156952Sume *success = 0; 583156952Sume return (unname); 584156952Sume} 585156952Sume 586170244Sume/*% 587156952Sume * Return a string for the type. 588156952Sume */ 589156952Sumeconst char * 590156952Sumep_type(int type) { 591156952Sume int success; 592156952Sume const char *result; 593156952Sume static char typebuf[20]; 594156952Sume 595156952Sume result = sym_ntos(__p_type_syms, type, &success); 596156952Sume if (success) 597156952Sume return (result); 598156952Sume if (type < 0 || type > 0xffff) 599156952Sume return ("BADTYPE"); 600156952Sume sprintf(typebuf, "TYPE%d", type); 601156952Sume return (typebuf); 602156952Sume} 603156952Sume 604170244Sume/*% 605156952Sume * Return a string for the type. 606156952Sume */ 607156952Sumeconst char * 608156952Sumep_section(int section, int opcode) { 609156952Sume const struct res_sym *symbols; 610156952Sume 611156952Sume switch (opcode) { 612156952Sume case ns_o_update: 613156952Sume symbols = __p_update_section_syms; 614156952Sume break; 615156952Sume default: 616156952Sume symbols = __p_default_section_syms; 617156952Sume break; 618156952Sume } 619156952Sume return (sym_ntos(symbols, section, (int *)0)); 620156952Sume} 621156952Sume 622170244Sume/*% 623156952Sume * Return a mnemonic for class. 624156952Sume */ 625156952Sumeconst char * 626156952Sumep_class(int class) { 627156952Sume int success; 628156952Sume const char *result; 629156952Sume static char classbuf[20]; 630156952Sume 631156952Sume result = sym_ntos(__p_class_syms, class, &success); 632156952Sume if (success) 633156952Sume return (result); 634156952Sume if (class < 0 || class > 0xffff) 635156952Sume return ("BADCLASS"); 636156952Sume sprintf(classbuf, "CLASS%d", class); 637156952Sume return (classbuf); 638156952Sume} 639156952Sume 640170244Sume/*% 641156952Sume * Return a mnemonic for an option 642156952Sume */ 643156952Sumeconst char * 644156952Sumep_option(u_long option) { 645156952Sume char *nbuf = p_option_nbuf; 646156952Sume 647156952Sume switch (option) { 648156952Sume case RES_INIT: return "init"; 649156952Sume case RES_DEBUG: return "debug"; 650156952Sume case RES_AAONLY: return "aaonly(unimpl)"; 651156952Sume case RES_USEVC: return "usevc"; 652156952Sume case RES_PRIMARY: return "primry(unimpl)"; 653156952Sume case RES_IGNTC: return "igntc"; 654156952Sume case RES_RECURSE: return "recurs"; 655156952Sume case RES_DEFNAMES: return "defnam"; 656156952Sume case RES_STAYOPEN: return "styopn"; 657156952Sume case RES_DNSRCH: return "dnsrch"; 658156952Sume case RES_INSECURE1: return "insecure1"; 659156952Sume case RES_INSECURE2: return "insecure2"; 660156952Sume case RES_NOALIASES: return "noaliases"; 661156952Sume case RES_USE_INET6: return "inet6"; 662170244Sume#ifdef RES_USE_EDNS0 /*%< KAME extension */ 663156952Sume case RES_USE_EDNS0: return "edns0"; 664186090Sume case RES_NSID: return "nsid"; 665156952Sume#endif 666156952Sume#ifdef RES_USE_DNAME 667156952Sume case RES_USE_DNAME: return "dname"; 668156952Sume#endif 669156952Sume#ifdef RES_USE_DNSSEC 670156952Sume case RES_USE_DNSSEC: return "dnssec"; 671156952Sume#endif 672156952Sume#ifdef RES_NOTLDQUERY 673156952Sume case RES_NOTLDQUERY: return "no-tld-query"; 674156952Sume#endif 675156952Sume#ifdef RES_NO_NIBBLE2 676156952Sume case RES_NO_NIBBLE2: return "no-nibble2"; 677156952Sume#endif 678156952Sume /* XXX nonreentrant */ 679156952Sume default: sprintf(nbuf, "?0x%lx?", (u_long)option); 680156952Sume return (nbuf); 681156952Sume } 682156952Sume} 683156952Sume 684170244Sume/*% 685156952Sume * Return a mnemonic for a time to live. 686156952Sume */ 687156952Sumeconst char * 688156952Sumep_time(u_int32_t value) { 689156952Sume char *nbuf = p_time_nbuf; 690156952Sume 691156952Sume if (ns_format_ttl(value, nbuf, sizeof nbuf) < 0) 692156952Sume sprintf(nbuf, "%u", value); 693156952Sume return (nbuf); 694156952Sume} 695156952Sume 696170244Sume/*% 697156952Sume * Return a string for the rcode. 698156952Sume */ 699156952Sumeconst char * 700156952Sumep_rcode(int rcode) { 701156952Sume return (sym_ntos(__p_rcode_syms, rcode, (int *)0)); 702156952Sume} 703156952Sume 704170244Sume/*% 705156952Sume * Return a string for a res_sockaddr_union. 706156952Sume */ 707156952Sumeconst char * 708156952Sumep_sockun(union res_sockaddr_union u, char *buf, size_t size) { 709156952Sume char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"]; 710156952Sume 711156952Sume switch (u.sin.sin_family) { 712156952Sume case AF_INET: 713156952Sume inet_ntop(AF_INET, &u.sin.sin_addr, ret, sizeof ret); 714156952Sume break; 715156952Sume#ifdef HAS_INET6_STRUCTS 716156952Sume case AF_INET6: 717156952Sume inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret); 718156952Sume break; 719156952Sume#endif 720156952Sume default: 721156952Sume sprintf(ret, "[af%d]", u.sin.sin_family); 722156952Sume break; 723156952Sume } 724156952Sume if (size > 0U) { 725156952Sume strncpy(buf, ret, size - 1); 726156952Sume buf[size - 1] = '0'; 727156952Sume } 728156952Sume return (buf); 729156952Sume} 730156952Sume 731170244Sume/*% 732156952Sume * routines to convert between on-the-wire RR format and zone file format. 733156952Sume * Does not contain conversion to/from decimal degrees; divide or multiply 734156952Sume * by 60*60*1000 for that. 735156952Sume */ 736156952Sume 737156952Sumestatic unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, 738156952Sume 1000000,10000000,100000000,1000000000}; 739156952Sume 740170244Sume/*% takes an XeY precision/size value, returns a string representation. */ 741156952Sumestatic const char * 742156952Sumeprecsize_ntoa(prec) 743156952Sume u_int8_t prec; 744156952Sume{ 745156952Sume char *retbuf = precsize_ntoa_retbuf; 746156952Sume unsigned long val; 747156952Sume int mantissa, exponent; 748156952Sume 749156952Sume mantissa = (int)((prec >> 4) & 0x0f) % 10; 750156952Sume exponent = (int)((prec >> 0) & 0x0f) % 10; 751156952Sume 752156952Sume val = mantissa * poweroften[exponent]; 753156952Sume 754156952Sume (void) sprintf(retbuf, "%lu.%.2lu", val/100, val%100); 755156952Sume return (retbuf); 756156952Sume} 757156952Sume 758170244Sume/*% converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ 759156952Sumestatic u_int8_t 760156952Sumeprecsize_aton(const char **strptr) { 761156952Sume unsigned int mval = 0, cmval = 0; 762156952Sume u_int8_t retval = 0; 763156952Sume const char *cp; 764156952Sume int exponent; 765156952Sume int mantissa; 766156952Sume 767156952Sume cp = *strptr; 768156952Sume 769156952Sume while (isdigit((unsigned char)*cp)) 770156952Sume mval = mval * 10 + (*cp++ - '0'); 771156952Sume 772170244Sume if (*cp == '.') { /*%< centimeters */ 773156952Sume cp++; 774156952Sume if (isdigit((unsigned char)*cp)) { 775156952Sume cmval = (*cp++ - '0') * 10; 776156952Sume if (isdigit((unsigned char)*cp)) { 777156952Sume cmval += (*cp++ - '0'); 778156952Sume } 779156952Sume } 780156952Sume } 781156952Sume cmval = (mval * 100) + cmval; 782156952Sume 783156952Sume for (exponent = 0; exponent < 9; exponent++) 784156952Sume if (cmval < poweroften[exponent+1]) 785156952Sume break; 786156952Sume 787156952Sume mantissa = cmval / poweroften[exponent]; 788156952Sume if (mantissa > 9) 789156952Sume mantissa = 9; 790156952Sume 791156952Sume retval = (mantissa << 4) | exponent; 792156952Sume 793156952Sume *strptr = cp; 794156952Sume 795156952Sume return (retval); 796156952Sume} 797156952Sume 798170244Sume/*% converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ 799156952Sumestatic u_int32_t 800156952Sumelatlon2ul(const char **latlonstrptr, int *which) { 801156952Sume const char *cp; 802156952Sume u_int32_t retval; 803156952Sume int deg = 0, min = 0, secs = 0, secsfrac = 0; 804156952Sume 805156952Sume cp = *latlonstrptr; 806156952Sume 807156952Sume while (isdigit((unsigned char)*cp)) 808156952Sume deg = deg * 10 + (*cp++ - '0'); 809156952Sume 810156952Sume while (isspace((unsigned char)*cp)) 811156952Sume cp++; 812156952Sume 813156952Sume if (!(isdigit((unsigned char)*cp))) 814156952Sume goto fndhemi; 815156952Sume 816156952Sume while (isdigit((unsigned char)*cp)) 817156952Sume min = min * 10 + (*cp++ - '0'); 818156952Sume 819156952Sume while (isspace((unsigned char)*cp)) 820156952Sume cp++; 821156952Sume 822156952Sume if (!(isdigit((unsigned char)*cp))) 823156952Sume goto fndhemi; 824156952Sume 825156952Sume while (isdigit((unsigned char)*cp)) 826156952Sume secs = secs * 10 + (*cp++ - '0'); 827156952Sume 828170244Sume if (*cp == '.') { /*%< decimal seconds */ 829156952Sume cp++; 830156952Sume if (isdigit((unsigned char)*cp)) { 831156952Sume secsfrac = (*cp++ - '0') * 100; 832156952Sume if (isdigit((unsigned char)*cp)) { 833156952Sume secsfrac += (*cp++ - '0') * 10; 834156952Sume if (isdigit((unsigned char)*cp)) { 835156952Sume secsfrac += (*cp++ - '0'); 836156952Sume } 837156952Sume } 838156952Sume } 839156952Sume } 840156952Sume 841170244Sume while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */ 842156952Sume cp++; 843156952Sume 844156952Sume while (isspace((unsigned char)*cp)) 845156952Sume cp++; 846156952Sume 847156952Sume fndhemi: 848156952Sume switch (*cp) { 849156952Sume case 'N': case 'n': 850156952Sume case 'E': case 'e': 851156952Sume retval = ((unsigned)1<<31) 852156952Sume + (((((deg * 60) + min) * 60) + secs) * 1000) 853156952Sume + secsfrac; 854156952Sume break; 855156952Sume case 'S': case 's': 856156952Sume case 'W': case 'w': 857156952Sume retval = ((unsigned)1<<31) 858156952Sume - (((((deg * 60) + min) * 60) + secs) * 1000) 859156952Sume - secsfrac; 860156952Sume break; 861156952Sume default: 862170244Sume retval = 0; /*%< invalid value -- indicates error */ 863156952Sume break; 864156952Sume } 865156952Sume 866156952Sume switch (*cp) { 867156952Sume case 'N': case 'n': 868156952Sume case 'S': case 's': 869170244Sume *which = 1; /*%< latitude */ 870156952Sume break; 871156952Sume case 'E': case 'e': 872156952Sume case 'W': case 'w': 873170244Sume *which = 2; /*%< longitude */ 874156952Sume break; 875156952Sume default: 876170244Sume *which = 0; /*%< error */ 877156952Sume break; 878156952Sume } 879156952Sume 880170244Sume cp++; /*%< skip the hemisphere */ 881170244Sume while (!isspace((unsigned char)*cp)) /*%< if any trailing garbage */ 882156952Sume cp++; 883156952Sume 884170244Sume while (isspace((unsigned char)*cp)) /*%< move to next field */ 885156952Sume cp++; 886156952Sume 887156952Sume *latlonstrptr = cp; 888156952Sume 889156952Sume return (retval); 890156952Sume} 891156952Sume 892170244Sume/*% 893170244Sume * converts a zone file representation in a string to an RDATA on-the-wire 894156952Sume * representation. */ 895156952Sumeint 896156952Sumeloc_aton(ascii, binary) 897156952Sume const char *ascii; 898156952Sume u_char *binary; 899156952Sume{ 900156952Sume const char *cp, *maxcp; 901156952Sume u_char *bcp; 902156952Sume 903156952Sume u_int32_t latit = 0, longit = 0, alt = 0; 904156952Sume u_int32_t lltemp1 = 0, lltemp2 = 0; 905156952Sume int altmeters = 0, altfrac = 0, altsign = 1; 906170244Sume u_int8_t hp = 0x16; /*%< default = 1e6 cm = 10000.00m = 10km */ 907170244Sume u_int8_t vp = 0x13; /*%< default = 1e3 cm = 10.00m */ 908170244Sume u_int8_t siz = 0x12; /*%< default = 1e2 cm = 1.00m */ 909156952Sume int which1 = 0, which2 = 0; 910156952Sume 911156952Sume cp = ascii; 912156952Sume maxcp = cp + strlen(ascii); 913156952Sume 914156952Sume lltemp1 = latlon2ul(&cp, &which1); 915156952Sume 916156952Sume lltemp2 = latlon2ul(&cp, &which2); 917156952Sume 918156952Sume switch (which1 + which2) { 919170244Sume case 3: /*%< 1 + 2, the only valid combination */ 920170244Sume if ((which1 == 1) && (which2 == 2)) { /*%< normal case */ 921156952Sume latit = lltemp1; 922156952Sume longit = lltemp2; 923170244Sume } else if ((which1 == 2) && (which2 == 1)) { /*%< reversed */ 924156952Sume longit = lltemp1; 925156952Sume latit = lltemp2; 926170244Sume } else { /*%< some kind of brokenness */ 927156952Sume return (0); 928156952Sume } 929156952Sume break; 930170244Sume default: /*%< we didn't get one of each */ 931156952Sume return (0); 932156952Sume } 933156952Sume 934156952Sume /* altitude */ 935156952Sume if (*cp == '-') { 936156952Sume altsign = -1; 937156952Sume cp++; 938156952Sume } 939156952Sume 940156952Sume if (*cp == '+') 941156952Sume cp++; 942156952Sume 943156952Sume while (isdigit((unsigned char)*cp)) 944156952Sume altmeters = altmeters * 10 + (*cp++ - '0'); 945156952Sume 946170244Sume if (*cp == '.') { /*%< decimal meters */ 947156952Sume cp++; 948156952Sume if (isdigit((unsigned char)*cp)) { 949156952Sume altfrac = (*cp++ - '0') * 10; 950156952Sume if (isdigit((unsigned char)*cp)) { 951156952Sume altfrac += (*cp++ - '0'); 952156952Sume } 953156952Sume } 954156952Sume } 955156952Sume 956156952Sume alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); 957156952Sume 958170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 959156952Sume cp++; 960156952Sume 961156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 962156952Sume cp++; 963156952Sume 964156952Sume if (cp >= maxcp) 965156952Sume goto defaults; 966156952Sume 967156952Sume siz = precsize_aton(&cp); 968156952Sume 969170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 970156952Sume cp++; 971156952Sume 972156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 973156952Sume cp++; 974156952Sume 975156952Sume if (cp >= maxcp) 976156952Sume goto defaults; 977156952Sume 978156952Sume hp = precsize_aton(&cp); 979156952Sume 980170244Sume while (!isspace((unsigned char)*cp) && (cp < maxcp)) /*%< if trailing garbage or m */ 981156952Sume cp++; 982156952Sume 983156952Sume while (isspace((unsigned char)*cp) && (cp < maxcp)) 984156952Sume cp++; 985156952Sume 986156952Sume if (cp >= maxcp) 987156952Sume goto defaults; 988156952Sume 989156952Sume vp = precsize_aton(&cp); 990156952Sume 991156952Sume defaults: 992156952Sume 993156952Sume bcp = binary; 994170244Sume *bcp++ = (u_int8_t) 0; /*%< version byte */ 995156952Sume *bcp++ = siz; 996156952Sume *bcp++ = hp; 997156952Sume *bcp++ = vp; 998156952Sume PUTLONG(latit,bcp); 999156952Sume PUTLONG(longit,bcp); 1000156952Sume PUTLONG(alt,bcp); 1001156952Sume 1002170244Sume return (16); /*%< size of RR in octets */ 1003156952Sume} 1004156952Sume 1005170244Sume/*% takes an on-the-wire LOC RR and formats it in a human readable format. */ 1006156952Sumeconst char * 1007156952Sumeloc_ntoa(binary, ascii) 1008156952Sume const u_char *binary; 1009156952Sume char *ascii; 1010156952Sume{ 1011156952Sume static const char *error = "?"; 1012156952Sume static char tmpbuf[sizeof 1013156952Sume"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; 1014156952Sume const u_char *cp = binary; 1015156952Sume 1016156952Sume int latdeg, latmin, latsec, latsecfrac; 1017156952Sume int longdeg, longmin, longsec, longsecfrac; 1018156952Sume char northsouth, eastwest; 1019156952Sume const char *altsign; 1020156952Sume int altmeters, altfrac; 1021156952Sume 1022156952Sume const u_int32_t referencealt = 100000 * 100; 1023156952Sume 1024156952Sume int32_t latval, longval, altval; 1025156952Sume u_int32_t templ; 1026156952Sume u_int8_t sizeval, hpval, vpval, versionval; 1027156952Sume 1028156952Sume char *sizestr, *hpstr, *vpstr; 1029156952Sume 1030156952Sume versionval = *cp++; 1031156952Sume 1032156952Sume if (ascii == NULL) 1033156952Sume ascii = tmpbuf; 1034156952Sume 1035156952Sume if (versionval) { 1036156952Sume (void) sprintf(ascii, "; error: unknown LOC RR version"); 1037156952Sume return (ascii); 1038156952Sume } 1039156952Sume 1040156952Sume sizeval = *cp++; 1041156952Sume 1042156952Sume hpval = *cp++; 1043156952Sume vpval = *cp++; 1044156952Sume 1045156952Sume GETLONG(templ, cp); 1046156952Sume latval = (templ - ((unsigned)1<<31)); 1047156952Sume 1048156952Sume GETLONG(templ, cp); 1049156952Sume longval = (templ - ((unsigned)1<<31)); 1050156952Sume 1051156952Sume GETLONG(templ, cp); 1052170244Sume if (templ < referencealt) { /*%< below WGS 84 spheroid */ 1053156952Sume altval = referencealt - templ; 1054156952Sume altsign = "-"; 1055156952Sume } else { 1056156952Sume altval = templ - referencealt; 1057156952Sume altsign = ""; 1058156952Sume } 1059156952Sume 1060156952Sume if (latval < 0) { 1061156952Sume northsouth = 'S'; 1062156952Sume latval = -latval; 1063156952Sume } else 1064156952Sume northsouth = 'N'; 1065156952Sume 1066156952Sume latsecfrac = latval % 1000; 1067156952Sume latval = latval / 1000; 1068156952Sume latsec = latval % 60; 1069156952Sume latval = latval / 60; 1070156952Sume latmin = latval % 60; 1071156952Sume latval = latval / 60; 1072156952Sume latdeg = latval; 1073156952Sume 1074156952Sume if (longval < 0) { 1075156952Sume eastwest = 'W'; 1076156952Sume longval = -longval; 1077156952Sume } else 1078156952Sume eastwest = 'E'; 1079156952Sume 1080156952Sume longsecfrac = longval % 1000; 1081156952Sume longval = longval / 1000; 1082156952Sume longsec = longval % 60; 1083156952Sume longval = longval / 60; 1084156952Sume longmin = longval % 60; 1085156952Sume longval = longval / 60; 1086156952Sume longdeg = longval; 1087156952Sume 1088156952Sume altfrac = altval % 100; 1089156952Sume altmeters = (altval / 100); 1090156952Sume 1091156952Sume sizestr = strdup(precsize_ntoa(sizeval)); 1092156952Sume hpstr = strdup(precsize_ntoa(hpval)); 1093156952Sume vpstr = strdup(precsize_ntoa(vpval)); 1094156952Sume 1095156952Sume sprintf(ascii, 1096156952Sume "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm", 1097156952Sume latdeg, latmin, latsec, latsecfrac, northsouth, 1098156952Sume longdeg, longmin, longsec, longsecfrac, eastwest, 1099156952Sume altsign, altmeters, altfrac, 1100156952Sume (sizestr != NULL) ? sizestr : error, 1101156952Sume (hpstr != NULL) ? hpstr : error, 1102156952Sume (vpstr != NULL) ? vpstr : error); 1103156952Sume 1104156952Sume if (sizestr != NULL) 1105156952Sume free(sizestr); 1106156952Sume if (hpstr != NULL) 1107156952Sume free(hpstr); 1108156952Sume if (vpstr != NULL) 1109156952Sume free(vpstr); 1110156952Sume 1111156952Sume return (ascii); 1112156952Sume} 1113156952Sume 1114156952Sume 1115170244Sume/*% Return the number of DNS hierarchy levels in the name. */ 1116156952Sumeint 1117156952Sumedn_count_labels(const char *name) { 1118156952Sume int i, len, count; 1119156952Sume 1120156952Sume len = strlen(name); 1121156952Sume for (i = 0, count = 0; i < len; i++) { 1122156952Sume /* XXX need to check for \. or use named's nlabels(). */ 1123156952Sume if (name[i] == '.') 1124156952Sume count++; 1125156952Sume } 1126156952Sume 1127156952Sume /* don't count initial wildcard */ 1128156952Sume if (name[0] == '*') 1129156952Sume if (count) 1130156952Sume count--; 1131156952Sume 1132156952Sume /* don't count the null label for root. */ 1133156952Sume /* if terminating '.' not found, must adjust */ 1134156952Sume /* count to include last label */ 1135156952Sume if (len > 0 && name[len-1] != '.') 1136156952Sume count++; 1137156952Sume return (count); 1138156952Sume} 1139156952Sume 1140170244Sume/*% 1141156952Sume * Make dates expressed in seconds-since-Jan-1-1970 easy to read. 1142156952Sume * SIG records are required to be printed like this, by the Secure DNS RFC. 1143156952Sume */ 1144156952Sumechar * 1145156952Sumep_secstodate (u_long secs) { 1146156952Sume char *output = p_secstodate_output; 1147156952Sume time_t clock = secs; 1148156952Sume struct tm *time; 1149156952Sume#ifdef HAVE_TIME_R 1150156952Sume struct tm res; 1151156952Sume 1152156952Sume time = gmtime_r(&clock, &res); 1153156952Sume#else 1154156952Sume time = gmtime(&clock); 1155156952Sume#endif 1156156952Sume time->tm_year += 1900; 1157156952Sume time->tm_mon += 1; 1158156952Sume sprintf(output, "%04d%02d%02d%02d%02d%02d", 1159156952Sume time->tm_year, time->tm_mon, time->tm_mday, 1160156952Sume time->tm_hour, time->tm_min, time->tm_sec); 1161156952Sume return (output); 1162156952Sume} 1163156952Sume 1164156952Sumeu_int16_t 1165156952Sumeres_nametoclass(const char *buf, int *successp) { 1166156952Sume unsigned long result; 1167156952Sume char *endptr; 1168156952Sume int success; 1169156952Sume 1170156952Sume result = sym_ston(__p_class_syms, buf, &success); 1171156952Sume if (success) 1172156952Sume goto done; 1173156952Sume 1174156952Sume if (strncasecmp(buf, "CLASS", 5) != 0 || 1175156952Sume !isdigit((unsigned char)buf[5])) 1176156952Sume goto done; 1177156952Sume errno = 0; 1178156952Sume result = strtoul(buf + 5, &endptr, 10); 1179156952Sume if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1180156952Sume success = 1; 1181156952Sume done: 1182156952Sume if (successp) 1183156952Sume *successp = success; 1184156952Sume return (result); 1185156952Sume} 1186156952Sume 1187156952Sumeu_int16_t 1188156952Sumeres_nametotype(const char *buf, int *successp) { 1189156952Sume unsigned long result; 1190156952Sume char *endptr; 1191156952Sume int success; 1192156952Sume 1193156952Sume result = sym_ston(__p_type_syms, buf, &success); 1194156952Sume if (success) 1195156952Sume goto done; 1196156952Sume 1197156952Sume if (strncasecmp(buf, "type", 4) != 0 || 1198156952Sume !isdigit((unsigned char)buf[4])) 1199156952Sume goto done; 1200156952Sume errno = 0; 1201156952Sume result = strtoul(buf + 4, &endptr, 10); 1202156952Sume if (errno == 0 && *endptr == '\0' && result <= 0xffffU) 1203156952Sume success = 1; 1204156952Sume done: 1205156952Sume if (successp) 1206156952Sume *successp = success; 1207156952Sume return (result); 1208156952Sume} 1209156956Sume 1210156956Sume/* 1211156956Sume * Weak aliases for applications that use certain private entry points, 1212156956Sume * and fail to include <resolv.h>. 1213156956Sume */ 1214156956Sume#undef fp_resstat 1215156956Sume__weak_reference(__fp_resstat, fp_resstat); 1216156956Sume#undef p_fqnname 1217156956Sume__weak_reference(__p_fqnname, p_fqnname); 1218156956Sume#undef sym_ston 1219156956Sume__weak_reference(__sym_ston, sym_ston); 1220156956Sume#undef sym_ntos 1221156956Sume__weak_reference(__sym_ntos, sym_ntos); 1222156956Sume#undef sym_ntop 1223156956Sume__weak_reference(__sym_ntop, sym_ntop); 1224156956Sume#undef dn_count_labels 1225156956Sume__weak_reference(__dn_count_labels, dn_count_labels); 1226156956Sume#undef p_secstodate 1227156956Sume__weak_reference(__p_secstodate, p_secstodate); 1228170244Sume 1229170244Sume/*! \file */ 1230