blob: 41eb93e7c2cb4a068f937951dbce1c275cdaeccd [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $ */
2
3/*
4 * Portions Copyright (C) 2004, 2005, 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 1996-2003 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/*
21 * Copyright (c) 1985
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
53/*
54 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies, and that
59 * the name of Digital Equipment Corporation not be used in advertising or
60 * publicity pertaining to distribution of the document or software without
61 * specific, written prior permission.
62 *
63 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
64 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
65 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
66 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
67 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
69 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70 * SOFTWARE.
71 */
72
73/*
74 * Portions Copyright (c) 1995 by International Business Machines, Inc.
75 *
76 * International Business Machines, Inc. (hereinafter called IBM) grants
77 * permission under its copyrights to use, copy, modify, and distribute this
78 * Software with or without fee, provided that the above copyright notice and
79 * all paragraphs of this notice appear in all copies, and that the name of IBM
80 * not be used in connection with the marketing of any product incorporating
81 * the Software or modifications thereof, without specific, written prior
82 * permission.
83 *
84 * To the extent it has a right to do so, IBM grants an immunity from suit
85 * under its patents, if any, for the use, sale or manufacture of products to
86 * the extent that such products are used for performing Domain Name System
87 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
88 * granted for any product per se or for any other function of any product.
89 *
90 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
91 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
92 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
93 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
94 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
95 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
96 */
97
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098#include <sys/param.h>
99#include <sys/socket.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900100#include <sys/types.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900101
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900102#include <arpa/inet.h>
103#include <arpa/nameser.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900104#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105
106#include <ctype.h>
107#include <errno.h>
108#include <math.h>
109#include <netdb.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900110#include <stdio.h>
111#include <stdlib.h>
112#include <string.h>
113#include <strings.h>
114#include <time.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900115#include "resolv_private.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900116
Bernie Innocentiafaacf72018-08-30 07:34:37 +0900117// NetBSD uses _DIAGASSERT to null-check arguments and the like,
118// but it's clear from the number of mistakes in their assertions
119// that they don't actually test or ship with this.
120#define _DIAGASSERT(e) /* nothing */
121
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122extern const char* const _res_opcodes[];
123extern const char* const _res_sectioncodes[];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900124
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900125static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag,
126 FILE* file) {
127 int n, sflag, rrnum;
128 int buflen = 2048;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900129 ns_opcode opcode;
130 ns_rr rr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900131
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132 /*
133 * Print answer records.
134 */
135 sflag = (int) (statp->pfcode & pflag);
136 if (statp->pfcode && !sflag) return;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900137
Bernie Innocenti9c575932018-09-07 21:10:25 +0900138 char* buf = (char*) malloc((size_t) buflen);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900139 if (buf == NULL) {
140 fprintf(file, ";; memory allocation failure\n");
141 return;
142 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900143
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900144 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
145 rrnum = 0;
146 for (;;) {
147 if (ns_parserr(handle, section, rrnum, &rr)) {
148 if (errno != ENODEV)
149 fprintf(file, ";; ns_parserr: %s\n", strerror(errno));
150 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
151 putc('\n', file);
152 goto cleanup;
153 }
154 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
155 fprintf(file, ";; %s SECTION:\n", p_section(section, opcode));
156 if (section == ns_s_qd)
157 fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)),
158 p_class(ns_rr_class(rr)));
159 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
160 size_t rdatalen, ttl;
161 uint16_t optcode, optlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900162
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900163 rdatalen = ns_rr_rdlen(rr);
164 ttl = ns_rr_ttl(rr);
165 fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
166 ns_rr_class(rr), ttl & 0xffff);
167 while (rdatalen >= 4) {
168 const u_char* cp = ns_rr_rdata(rr);
169 int i;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900170
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900171 GETSHORT(optcode, cp);
172 GETSHORT(optlen, cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900173
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174 if (optcode == NS_OPT_NSID) {
175 fputs("; NSID: ", file);
176 if (optlen == 0) {
177 fputs("; NSID\n", file);
178 } else {
179 fputs("; NSID: ", file);
180 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
181 fputs(" (", file);
182 for (i = 0; i < optlen; i++)
183 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
184 fputs(")\n", file);
185 }
186 } else {
187 if (optlen == 0) {
188 fprintf(file, "; OPT=%u\n", optcode);
189 } else {
190 fprintf(file, "; OPT=%u: ", optcode);
191 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
192 fputs(" (", file);
193 for (i = 0; i < optlen; i++)
194 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
195 fputs(")\n", file);
196 }
197 }
198 rdatalen -= 4 + optlen;
199 }
200 } else {
201 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
202 if (n < 0) {
203 if (errno == ENOSPC) {
204 free(buf);
205 buf = NULL;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900206 if (buflen < 131072) {
207 buf = (char*) malloc((size_t)(buflen += 1024));
208 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900209 if (buf == NULL) {
210 fprintf(file, ";; memory allocation failure\n");
211 return;
212 }
213 continue;
214 }
215 fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
216 goto cleanup;
217 }
218 fputs(buf, file);
219 fputc('\n', file);
220 }
221 rrnum++;
222 }
223cleanup:
Bernie Innocenti9c575932018-09-07 21:10:25 +0900224 free(buf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900225}
226
227/*
228 * Print the contents of a query.
229 * This is intended to be primarily a debugging routine.
230 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900231void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) {
232 ns_msg handle;
233 int qdcount, ancount, nscount, arcount;
234 u_int opcode, rcode, id;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900235
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900236 if (ns_initparse(msg, len, &handle) < 0) {
237 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
238 return;
239 }
240 opcode = ns_msg_getflag(handle, ns_f_opcode);
241 rcode = ns_msg_getflag(handle, ns_f_rcode);
242 id = ns_msg_id(handle);
243 qdcount = ns_msg_count(handle, ns_s_qd);
244 ancount = ns_msg_count(handle, ns_s_an);
245 nscount = ns_msg_count(handle, ns_s_ns);
246 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900247
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900248 /*
249 * Print header fields.
250 */
251 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
252 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
253 p_rcode((int) rcode), id);
254 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file);
255 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
256 fprintf(file, "; flags:");
257 if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr");
258 if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa");
259 if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc");
260 if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd");
261 if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra");
262 if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??");
263 if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad");
264 if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd");
265 }
266 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
267 fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount);
268 fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount);
269 fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount);
270 fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount);
271 }
272 if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
273 putc('\n', file);
274 }
275 /*
276 * Print the various sections.
277 */
278 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
279 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
280 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
281 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
282 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900283}
284
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900285const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
286 char name[MAXDNAME];
287 int n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900288
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900289 if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
290 if (name[0] == '\0')
291 putc('.', file);
292 else
293 fputs(name, file);
294 return (cp + n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900295}
296
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900297const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
298 return (p_cdnname(cp, msg, PACKETSZ, file));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900299}
300
301/* Return a fully-qualified domain name from a compressed name (with
302 length supplied). */
303
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900304const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
305 int n;
306 size_t newlen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900307
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900308 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
309 newlen = strlen(name);
310 if (newlen == 0 || name[newlen - 1] != '.') {
311 if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
312 return (NULL);
313 else
314 strcpy(name + newlen, ".");
315 }
316 return (cp + n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900317}
318
319/* XXX: the rest of these functions need to become length-limited, too. */
320
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900321const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
322 char name[MAXDNAME];
323 const u_char* n;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900324
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900325 n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
326 if (n == NULL) return (NULL);
327 fputs(name, file);
328 return (n);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900329}
330
331/*
332 * Names of RR classes and qclasses. Classes and qclasses are the same, except
333 * that C_ANY is a qclass but not a class. (You can ask for records of class
334 * C_ANY, but you can't have any records of that class in the database.)
335 */
336const struct res_sym __p_class_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900337 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
338 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
339 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900340
341/*
342 * Names of message sections.
343 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900344static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
345 {ns_s_an, "ANSWER", (char*) 0},
346 {ns_s_ns, "AUTHORITY", (char*) 0},
347 {ns_s_ar, "ADDITIONAL", (char*) 0},
348 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900349
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900350static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
351 {S_PREREQ, "PREREQUISITE", (char*) 0},
352 {S_UPDATE, "UPDATE", (char*) 0},
353 {S_ADDT, "ADDITIONAL", (char*) 0},
354 {0, (char*) 0, (char*) 0}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900355
356const struct res_sym __p_key_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900357 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
358 {NS_ALG_DH, "DH", "Diffie Hellman"},
359 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
360 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
361 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
362 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900363
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900364const struct res_sym __p_cert_syms[] = {{cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
365 {cert_t_spki, "SPKI", "SPKI certificate"},
366 {cert_t_pgp, "PGP", "PGP certificate"},
367 {cert_t_url, "URL", "URL Private"},
368 {cert_t_oid, "OID", "OID Private"},
369 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900370
371/*
372 * Names of RR types and qtypes. Types and qtypes are the same, except
373 * that T_ANY is a qtype but not a type. (You can ask for records of type
374 * T_ANY, but you can't have any records of that type in the database.)
375 */
376const struct res_sym __p_type_syms[] = {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900377 {ns_t_a, "A", "address"},
378 {ns_t_ns, "NS", "name server"},
379 {ns_t_md, "MD", "mail destination (deprecated)"},
380 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
381 {ns_t_cname, "CNAME", "canonical name"},
382 {ns_t_soa, "SOA", "start of authority"},
383 {ns_t_mb, "MB", "mailbox"},
384 {ns_t_mg, "MG", "mail group member"},
385 {ns_t_mr, "MR", "mail rename"},
386 {ns_t_null, "NULL", "null"},
387 {ns_t_wks, "WKS", "well-known service (deprecated)"},
388 {ns_t_ptr, "PTR", "domain name pointer"},
389 {ns_t_hinfo, "HINFO", "host information"},
390 {ns_t_minfo, "MINFO", "mailbox information"},
391 {ns_t_mx, "MX", "mail exchanger"},
392 {ns_t_txt, "TXT", "text"},
393 {ns_t_rp, "RP", "responsible person"},
394 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
395 {ns_t_x25, "X25", "X25 address"},
396 {ns_t_isdn, "ISDN", "ISDN address"},
397 {ns_t_rt, "RT", "router"},
398 {ns_t_nsap, "NSAP", "nsap address"},
399 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
400 {ns_t_sig, "SIG", "signature"},
401 {ns_t_key, "KEY", "key"},
402 {ns_t_px, "PX", "mapping information"},
403 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
404 {ns_t_aaaa, "AAAA", "IPv6 address"},
405 {ns_t_loc, "LOC", "location"},
406 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
407 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
408 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
409 {ns_t_srv, "SRV", "server selection"},
410 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
411 {ns_t_naptr, "NAPTR", "naptr"},
412 {ns_t_kx, "KX", "key exchange"},
413 {ns_t_cert, "CERT", "certificate"},
414 {ns_t_a6, "A", "IPv6 address (experminental)"},
415 {ns_t_dname, "DNAME", "non-terminal redirection"},
416 {ns_t_opt, "OPT", "opt"},
417 {ns_t_apl, "apl", "apl"},
418 {ns_t_ds, "DS", "delegation signer"},
419 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
420 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
421 {ns_t_rrsig, "RRSIG", "rrsig"},
422 {ns_t_nsec, "NSEC", "nsec"},
423 {ns_t_dnskey, "DNSKEY", "DNS key"},
424 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
425 {ns_t_nsec3, "NSEC3", "nsec3"},
426 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
427 {ns_t_hip, "HIP", "host identity protocol"},
428 {ns_t_spf, "SPF", "sender policy framework"},
429 {ns_t_tkey, "TKEY", "tkey"},
430 {ns_t_tsig, "TSIG", "transaction signature"},
431 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
432 {ns_t_axfr, "AXFR", "zone transfer"},
433 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
434 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
435 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
436 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
437 {ns_t_kx, "KX", "Key Exchange"},
438 {ns_t_cert, "CERT", "Certificate"},
439 {ns_t_a6, "A6", "IPv6 Address"},
440 {ns_t_dname, "DNAME", "dname"},
441 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
442 {ns_t_opt, "OPT", "EDNS Options"},
443 {ns_t_any, "ANY", "\"any\""},
444 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
445 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900446
447/*
448 * Names of DNS rcodes.
449 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900450const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
451 {ns_r_formerr, "FORMERR", "format error"},
452 {ns_r_servfail, "SERVFAIL", "server failed"},
453 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
454 {ns_r_notimpl, "NOTIMP", "not implemented"},
455 {ns_r_refused, "REFUSED", "refused"},
456 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
457 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
458 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
459 {ns_r_notauth, "NOTAUTH", "not authoritative"},
460 {ns_r_notzone, "NOTZONE", "Not in zone"},
461 {ns_r_max, "", ""},
462 {ns_r_badsig, "BADSIG", "bad signature"},
463 {ns_r_badkey, "BADKEY", "bad key"},
464 {ns_r_badtime, "BADTIME", "bad time"},
465 {0, NULL, NULL}};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900466
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900467int sym_ston(const struct res_sym* syms, const char* name, int* success) {
468 for (; syms->name != 0; syms++) {
469 if (strcasecmp(name, syms->name) == 0) {
470 if (success) *success = 1;
471 return (syms->number);
472 }
473 }
474 if (success) *success = 0;
475 return (syms->number); /* The default value. */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900476}
477
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900478const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
479 static char unname[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900480
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900481 for (; syms->name != 0; syms++) {
482 if (number == syms->number) {
483 if (success) *success = 1;
484 return (syms->name);
485 }
486 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900487
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900488 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
489 if (success) *success = 0;
490 return (unname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900491}
492
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900493const char* sym_ntop(const struct res_sym* syms, int number, int* success) {
494 static char unname[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900495
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900496 for (; syms->name != 0; syms++) {
497 if (number == syms->number) {
498 if (success) *success = 1;
499 return (syms->humanname);
500 }
501 }
502 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
503 if (success) *success = 0;
504 return (unname);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900505}
506
507/*
508 * Return a string for the type.
509 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900510const char* p_type(int type) {
511 int success;
512 const char* result;
513 static char typebuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900514
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900515 result = sym_ntos(__p_type_syms, type, &success);
516 if (success) return (result);
517 if (type < 0 || type > 0xffff) return ("BADTYPE");
518 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
519 return (typebuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900520}
521
522/*
523 * Return a string for the type.
524 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900525const char* p_section(int section, int opcode) {
526 const struct res_sym* symbols;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900527
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900528 switch (opcode) {
529 case ns_o_update:
530 symbols = __p_update_section_syms;
531 break;
532 default:
533 symbols = __p_default_section_syms;
534 break;
535 }
536 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900537}
538
539/*
540 * Return a mnemonic for class.
541 */
Bernie Innocenti9c575932018-09-07 21:10:25 +0900542const char* p_class(int cl) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900543 int success;
544 const char* result;
545 static char classbuf[20];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900546
Bernie Innocenti9c575932018-09-07 21:10:25 +0900547 result = sym_ntos(__p_class_syms, cl, &success);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900548 if (success) return (result);
Bernie Innocenti9c575932018-09-07 21:10:25 +0900549 if (cl < 0 || cl > 0xffff) return ("BADCLASS");
550 snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900551 return (classbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900552}
553
554/*
555 * Return a mnemonic for an option
556 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900557const char* p_option(u_long option) {
558 static char nbuf[40];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900559
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900560 switch (option) {
561 case RES_INIT:
562 return "init";
563 case RES_DEBUG:
564 return "debug";
565 case RES_AAONLY:
566 return "aaonly(unimpl)";
567 case RES_USEVC:
568 return "usevc";
569 case RES_PRIMARY:
570 return "primry(unimpl)";
571 case RES_IGNTC:
572 return "igntc";
573 case RES_RECURSE:
574 return "recurs";
575 case RES_DEFNAMES:
576 return "defnam";
577 case RES_STAYOPEN:
578 return "styopn";
579 case RES_DNSRCH:
580 return "dnsrch";
581 case RES_INSECURE1:
582 return "insecure1";
583 case RES_INSECURE2:
584 return "insecure2";
585 case RES_NOALIASES:
586 return "noaliases";
587 case RES_USE_INET6:
588 return "inet6";
589#ifdef RES_USE_EDNS0 /* KAME extension */
590 case RES_USE_EDNS0:
591 return "edns0";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900592#endif
593#ifdef RES_USE_DNAME
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900594 case RES_USE_DNAME:
595 return "dname";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900596#endif
597#ifdef RES_USE_DNSSEC
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900598 case RES_USE_DNSSEC:
599 return "dnssec";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900600#endif
601#ifdef RES_NOTLDQUERY
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900602 case RES_NOTLDQUERY:
603 return "no-tld-query";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900604#endif
605#ifdef RES_NO_NIBBLE2
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900606 case RES_NO_NIBBLE2:
607 return "no-nibble2";
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900608#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900609 /* XXX nonreentrant */
610 default:
611 snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long) option);
612 return (nbuf);
613 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900614}
615
616/*
617 * Return a mnemonic for a time to live.
618 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900619const char* p_time(u_int32_t value) {
620 static char nbuf[40]; /* XXX nonreentrant */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900621
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900622 if (ns_format_ttl((u_long) value, nbuf, sizeof nbuf) < 0)
623 snprintf(nbuf, sizeof(nbuf), "%u", value);
624 return (nbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900625}
626
627/*
628 * Return a string for the rcode.
629 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900630const char* p_rcode(int rcode) {
631 return (sym_ntos(__p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900632}
633
634/*
635 * Return a string for a res_sockaddr_union.
636 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900637const char* p_sockun(union res_sockaddr_union u, char* buf, size_t size) {
638 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900639
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900640 switch (u.sin.sin_family) {
641 case AF_INET:
642 inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t) sizeof ret);
643 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900644 case AF_INET6:
645 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
646 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900647 default:
648 snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family);
649 break;
650 }
651 if (size > 0U) {
652 strncpy(buf, ret, size - 1);
653 buf[size - 1] = '0';
654 }
655 return (buf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900656}
657
658/*
659 * routines to convert between on-the-wire RR format and zone file format.
660 * Does not contain conversion to/from decimal degrees; divide or multiply
661 * by 60*60*1000 for that.
662 */
663
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900664static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000,
665 100000, 1000000, 10000000, 100000000, 1000000000};
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900666
667/* takes an XeY precision/size value, returns a string representation. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900668static const char* precsize_ntoa(u_int32_t prec) {
669 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
670 unsigned long val;
671 int mantissa, exponent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900672
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900673 mantissa = (int) ((prec >> 4) & 0x0f) % 10;
674 exponent = (int) ((prec >> 0) & 0x0f) % 10;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900675
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900676 val = mantissa * poweroften[exponent];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900677
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900678 (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val / 100, val % 100);
679 return (retbuf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900680}
681
682/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900683static u_int8_t precsize_aton(const char** strptr) {
684 unsigned int mval = 0, cmval = 0;
685 u_int8_t retval = 0;
686 const char* cp;
687 int exponent;
688 int mantissa;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900689
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900690 cp = *strptr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900691
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900692 while (isdigit((unsigned char) *cp)) mval = mval * 10 + (*cp++ - '0');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900693
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900694 if (*cp == '.') { /* centimeters */
695 cp++;
696 if (isdigit((unsigned char) *cp)) {
697 cmval = (*cp++ - '0') * 10;
698 if (isdigit((unsigned char) *cp)) {
699 cmval += (*cp++ - '0');
700 }
701 }
702 }
703 cmval = (mval * 100) + cmval;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900704
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900705 for (exponent = 0; exponent < 9; exponent++)
706 if (cmval < poweroften[exponent + 1]) break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900707
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900708 mantissa = cmval / poweroften[exponent];
709 if (mantissa > 9) mantissa = 9;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900710
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900711 retval = (mantissa << 4) | exponent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900712
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900713 *strptr = cp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900714
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900715 return (retval);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900716}
717
718/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900719static u_int32_t latlon2ul(const char** latlonstrptr, int* which) {
720 const char* cp;
721 u_int32_t retval;
722 int deg = 0, min = 0, secs = 0, secsfrac = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900723
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900724 cp = *latlonstrptr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900725
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900726 while (isdigit((unsigned char) *cp)) deg = deg * 10 + (*cp++ - '0');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900727
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900728 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900729
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900730 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900731
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900732 while (isdigit((unsigned char) *cp)) min = min * 10 + (*cp++ - '0');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900733
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900734 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900735
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900736 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900737
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900738 while (isdigit((unsigned char) *cp)) secs = secs * 10 + (*cp++ - '0');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900739
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900740 if (*cp == '.') { /* decimal seconds */
741 cp++;
742 if (isdigit((unsigned char) *cp)) {
743 secsfrac = (*cp++ - '0') * 100;
744 if (isdigit((unsigned char) *cp)) {
745 secsfrac += (*cp++ - '0') * 10;
746 if (isdigit((unsigned char) *cp)) {
747 secsfrac += (*cp++ - '0');
748 }
749 }
750 }
751 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900752
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900753 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
754 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900755
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900756 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900757
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900758fndhemi:
759 switch (*cp) {
760 case 'N':
761 case 'n':
762 case 'E':
763 case 'e':
764 retval = ((unsigned) 1 << 31) + (((((deg * 60) + min) * 60) + secs) * 1000) + secsfrac;
765 break;
766 case 'S':
767 case 's':
768 case 'W':
769 case 'w':
770 retval = ((unsigned) 1 << 31) - (((((deg * 60) + min) * 60) + secs) * 1000) - secsfrac;
771 break;
772 default:
773 retval = 0; /* invalid value -- indicates error */
774 break;
775 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900776
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900777 switch (*cp) {
778 case 'N':
779 case 'n':
780 case 'S':
781 case 's':
782 *which = 1; /* latitude */
783 break;
784 case 'E':
785 case 'e':
786 case 'W':
787 case 'w':
788 *which = 2; /* longitude */
789 break;
790 default:
791 *which = 0; /* error */
792 break;
793 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900794
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900795 cp++; /* skip the hemisphere */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900796
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900797 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
798 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900799
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900800 while (isspace((unsigned char) *cp)) /* move to next field */
801 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900802
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900803 *latlonstrptr = cp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900804
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900805 return (retval);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900806}
807
808/* converts a zone file representation in a string to an RDATA on-the-wire
809 * representation. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900810int loc_aton(const char* ascii, u_char* binary) {
811 const char *cp, *maxcp;
812 u_char* bcp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900813
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900814 u_int32_t latit = 0, longit = 0, alt = 0;
815 u_int32_t lltemp1 = 0, lltemp2 = 0;
816 int altmeters = 0, altfrac = 0, altsign = 1;
817 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
818 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
819 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
820 int which1 = 0, which2 = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900821
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900822 cp = ascii;
823 maxcp = cp + strlen(ascii);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900824
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900825 lltemp1 = latlon2ul(&cp, &which1);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900826
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900827 lltemp2 = latlon2ul(&cp, &which2);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900828
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900829 switch (which1 + which2) {
830 case 3: /* 1 + 2, the only valid combination */
831 if ((which1 == 1) && (which2 == 2)) { /* normal case */
832 latit = lltemp1;
833 longit = lltemp2;
834 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
835 longit = lltemp1;
836 latit = lltemp2;
837 } else { /* some kind of brokenness */
838 return (0);
839 }
840 break;
841 default: /* we didn't get one of each */
842 return (0);
843 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900844
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900845 /* altitude */
846 if (*cp == '-') {
847 altsign = -1;
848 cp++;
849 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900850
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900851 if (*cp == '+') cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900852
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900853 while (isdigit((unsigned char) *cp)) altmeters = altmeters * 10 + (*cp++ - '0');
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900854
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900855 if (*cp == '.') { /* decimal meters */
856 cp++;
857 if (isdigit((unsigned char) *cp)) {
858 altfrac = (*cp++ - '0') * 10;
859 if (isdigit((unsigned char) *cp)) {
860 altfrac += (*cp++ - '0');
861 }
862 }
863 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900864
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900865 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900866
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900867 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
868 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900869
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900870 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900871
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900872 if (cp >= maxcp) goto defaults;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900873
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900874 siz = precsize_aton(&cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900875
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900876 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
877 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900878
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900879 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900880
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900881 if (cp >= maxcp) goto defaults;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900882
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900883 hp = precsize_aton(&cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900884
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900885 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
886 cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900887
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900888 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900889
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900890 if (cp >= maxcp) goto defaults;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900891
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900892 vp = precsize_aton(&cp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900893
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900894defaults:
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900895
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900896 bcp = binary;
897 *bcp++ = (u_int8_t) 0; /* version byte */
898 *bcp++ = siz;
899 *bcp++ = hp;
900 *bcp++ = vp;
901 PUTLONG(latit, bcp);
902 PUTLONG(longit, bcp);
903 PUTLONG(alt, bcp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900904
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900905 return (16); /* size of RR in octets */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900906}
907
908/* takes an on-the-wire LOC RR and formats it in a human readable format. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900909const char* loc_ntoa(const u_char* binary, char* ascii, size_t bufsiz) {
910 static const char* error = "?";
911 static char tmpbuf[sizeof
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900912"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900913 const u_char* cp = binary;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900914
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900915 int latdeg, latmin, latsec, latsecfrac;
916 int longdeg, longmin, longsec, longsecfrac;
917 char northsouth, eastwest;
918 const char* altsign;
919 int altmeters, altfrac;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900920
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900921 const u_int32_t referencealt = 100000 * 100;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900922
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900923 int32_t latval, longval, altval;
924 u_int32_t templ;
925 u_int8_t sizeval, hpval, vpval, versionval;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900926
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900927 char *sizestr, *hpstr, *vpstr;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900928
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900929 versionval = *cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900930
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900931 if (ascii == NULL) ascii = tmpbuf;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900932
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900933 if (versionval) {
934 (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version");
935 return (ascii);
936 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900937
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900938 sizeval = *cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900939
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900940 hpval = *cp++;
941 vpval = *cp++;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900942
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900943 GETLONG(templ, cp);
944 latval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900945
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900946 GETLONG(templ, cp);
947 longval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900948
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900949 GETLONG(templ, cp);
950 if (templ < referencealt) { /* below WGS 84 spheroid */
951 altval = referencealt - templ;
952 altsign = "-";
953 } else {
954 altval = templ - referencealt;
955 altsign = "";
956 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900957
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900958 if (latval < 0) {
959 northsouth = 'S';
960 latval = -latval;
961 } else
962 northsouth = 'N';
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900963
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900964 latsecfrac = latval % 1000;
965 latval = latval / 1000;
966 latsec = latval % 60;
967 latval = latval / 60;
968 latmin = latval % 60;
969 latval = latval / 60;
970 latdeg = latval;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900971
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900972 if (longval < 0) {
973 eastwest = 'W';
974 longval = -longval;
975 } else
976 eastwest = 'E';
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900977
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900978 longsecfrac = longval % 1000;
979 longval = longval / 1000;
980 longsec = longval % 60;
981 longval = longval / 60;
982 longmin = longval % 60;
983 longval = longval / 60;
984 longdeg = longval;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900985
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900986 altfrac = altval % 100;
987 altmeters = (altval / 100);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900988
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900989 sizestr = strdup(precsize_ntoa((u_int32_t) sizeval));
990 hpstr = strdup(precsize_ntoa((u_int32_t) hpval));
991 vpstr = strdup(precsize_ntoa((u_int32_t) vpval));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900992
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900993 snprintf(ascii, bufsiz, "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
994 latdeg, latmin, latsec, latsecfrac, northsouth, longdeg, longmin, longsec, longsecfrac,
995 eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error,
996 (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900997
Bernie Innocenti9c575932018-09-07 21:10:25 +0900998 free(sizestr);
999 free(hpstr);
1000 free(vpstr);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001001
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001002 return (ascii);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001003}
1004
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001005/* Return the number of DNS hierarchy levels in the name. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001006int dn_count_labels(const char* name) {
1007 size_t len, i, count;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001008
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001009 len = strlen(name);
1010 for (i = 0, count = 0; i < len; i++) {
1011 /* XXX need to check for \. or use named's nlabels(). */
1012 if (name[i] == '.') count++;
1013 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001014
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001015 /* don't count initial wildcard */
1016 if (name[0] == '*')
1017 if (count) count--;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001018
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001019 /* don't count the null label for root. */
1020 /* if terminating '.' not found, must adjust */
1021 /* count to include last label */
1022 if (len > 0 && name[len - 1] != '.') count++;
1023 _DIAGASSERT(__type_fit(int, count));
1024 return (int) count;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001025}
1026
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001027/*
1028 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1029 * SIG records are required to be printed like this, by the Secure DNS RFC.
1030 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001031char* p_secstodate(u_long secs) {
1032 /* XXX nonreentrant */
1033 static char output[15]; /* YYYYMMDDHHMMSS and null */
1034 time_t myclock = secs;
1035 struct tm* mytime;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001036#ifdef HAVE_TIME_R
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001037 struct tm res;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001038
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001039 mytime = gmtime_r(&myclock, &res);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001040#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001041 mytime = gmtime(&myclock);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001042#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001043 mytime->tm_year += 1900;
1044 mytime->tm_mon += 1;
1045 snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d", mytime->tm_year, mytime->tm_mon,
1046 mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
1047 return (output);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001048}
1049
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001050u_int16_t res_nametoclass(const char* buf, int* successp) {
1051 unsigned long result;
1052 char* endptr;
1053 int success;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001054
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001055 result = sym_ston(__p_class_syms, buf, &success);
1056 if (success) goto done;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001057
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001058 if (strncasecmp(buf, "CLASS", 5) != 0 || !isdigit((unsigned char) buf[5])) goto done;
1059 errno = 0;
1060 result = strtoul(buf + 5, &endptr, 10);
1061 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1062done:
1063 if (successp) *successp = success;
1064 return (u_int16_t)(result);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001065}
1066
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001067u_int16_t res_nametotype(const char* buf, int* successp) {
1068 unsigned long result;
1069 char* endptr;
1070 int success;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001071
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001072 result = sym_ston(__p_type_syms, buf, &success);
1073 if (success) goto done;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001074
Bernie Innocenti8ad893f2018-08-31 14:09:46 +09001075 if (strncasecmp(buf, "type", 4) != 0 || !isdigit((unsigned char) buf[4])) goto done;
1076 errno = 0;
1077 result = strtoul(buf + 4, &endptr, 10);
1078 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1079done:
1080 if (successp) *successp = success;
1081 return (u_int16_t)(result);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001082}