blob: 331d0c4134d8fc3c7df51d5545298f2cd998542c [file] [log] [blame]
Bernie Innocenti55864192018-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
98#include <sys/cdefs.h>
99#if defined(LIBC_SCCS) && !defined(lint)
100#ifdef notdef
101static const char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
102static const char rcsid[] = "Id: res_debug.c,v 1.19 2009/02/26 11:20:20 tbox Exp";
103#else
104__RCSID("$NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $");
105#endif
106#endif /* LIBC_SCCS and not lint */
107
Bernie Innocenti55864192018-08-30 04:05:20 +0900108#include <sys/param.h>
109#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900110#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900111
Bernie Innocenti55864192018-08-30 04:05:20 +0900112#include <arpa/inet.h>
113#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900115
116#include <ctype.h>
117#include <errno.h>
118#include <math.h>
119#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900120#include <stdio.h>
121#include <stdlib.h>
122#include <string.h>
123#include <strings.h>
124#include <time.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900125#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900126
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127extern const char* const _res_opcodes[];
128extern const char* const _res_sectioncodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +0900129
130#ifndef _LIBC
131/*
132 * Print the current options.
133 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134void fp_resstat(const res_state statp, FILE* file) {
135 u_long mask;
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900137 fprintf(file, ";; res options:");
138 for (mask = 1; mask != 0U; mask <<= 1)
139 if (statp->options & mask) fprintf(file, " %s", p_option(mask));
140 putc('\n', file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900141}
142#endif
143
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag,
145 FILE* file) {
146 int n, sflag, rrnum;
147 int buflen = 2048;
148 char* buf;
149 ns_opcode opcode;
150 ns_rr rr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900151
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 /*
153 * Print answer records.
154 */
155 sflag = (int) (statp->pfcode & pflag);
156 if (statp->pfcode && !sflag) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900157
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158 buf = malloc((size_t) buflen);
159 if (buf == NULL) {
160 fprintf(file, ";; memory allocation failure\n");
161 return;
162 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900163
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
165 rrnum = 0;
166 for (;;) {
167 if (ns_parserr(handle, section, rrnum, &rr)) {
168 if (errno != ENODEV)
169 fprintf(file, ";; ns_parserr: %s\n", strerror(errno));
170 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
171 putc('\n', file);
172 goto cleanup;
173 }
174 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
175 fprintf(file, ";; %s SECTION:\n", p_section(section, opcode));
176 if (section == ns_s_qd)
177 fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)),
178 p_class(ns_rr_class(rr)));
179 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
180 size_t rdatalen, ttl;
181 uint16_t optcode, optlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900182
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 rdatalen = ns_rr_rdlen(rr);
184 ttl = ns_rr_ttl(rr);
185 fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
186 ns_rr_class(rr), ttl & 0xffff);
187 while (rdatalen >= 4) {
188 const u_char* cp = ns_rr_rdata(rr);
189 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191 GETSHORT(optcode, cp);
192 GETSHORT(optlen, cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900193
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900194 if (optcode == NS_OPT_NSID) {
195 fputs("; NSID: ", file);
196 if (optlen == 0) {
197 fputs("; NSID\n", file);
198 } else {
199 fputs("; NSID: ", file);
200 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
201 fputs(" (", file);
202 for (i = 0; i < optlen; i++)
203 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
204 fputs(")\n", file);
205 }
206 } else {
207 if (optlen == 0) {
208 fprintf(file, "; OPT=%u\n", optcode);
209 } else {
210 fprintf(file, "; OPT=%u: ", optcode);
211 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
212 fputs(" (", file);
213 for (i = 0; i < optlen; i++)
214 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
215 fputs(")\n", file);
216 }
217 }
218 rdatalen -= 4 + optlen;
219 }
220 } else {
221 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
222 if (n < 0) {
223 if (errno == ENOSPC) {
224 free(buf);
225 buf = NULL;
226 if (buflen < 131072) buf = malloc((size_t)(buflen += 1024));
227 if (buf == NULL) {
228 fprintf(file, ";; memory allocation failure\n");
229 return;
230 }
231 continue;
232 }
233 fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
234 goto cleanup;
235 }
236 fputs(buf, file);
237 fputc('\n', file);
238 }
239 rrnum++;
240 }
241cleanup:
242 if (buf != NULL) free(buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900243}
244
245/*
246 * Print the contents of a query.
247 * This is intended to be primarily a debugging routine.
248 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) {
250 ns_msg handle;
251 int qdcount, ancount, nscount, arcount;
252 u_int opcode, rcode, id;
Bernie Innocenti55864192018-08-30 04:05:20 +0900253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 if (ns_initparse(msg, len, &handle) < 0) {
255 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
256 return;
257 }
258 opcode = ns_msg_getflag(handle, ns_f_opcode);
259 rcode = ns_msg_getflag(handle, ns_f_rcode);
260 id = ns_msg_id(handle);
261 qdcount = ns_msg_count(handle, ns_s_qd);
262 ancount = ns_msg_count(handle, ns_s_an);
263 nscount = ns_msg_count(handle, ns_s_ns);
264 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti55864192018-08-30 04:05:20 +0900265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 /*
267 * Print header fields.
268 */
269 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
270 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
271 p_rcode((int) rcode), id);
272 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file);
273 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
274 fprintf(file, "; flags:");
275 if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr");
276 if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa");
277 if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc");
278 if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd");
279 if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra");
280 if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??");
281 if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad");
282 if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd");
283 }
284 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
285 fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount);
286 fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount);
287 fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount);
288 fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount);
289 }
290 if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
291 putc('\n', file);
292 }
293 /*
294 * Print the various sections.
295 */
296 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
297 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
298 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
299 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
300 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900301}
302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900303const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
304 char name[MAXDNAME];
305 int n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307 if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
308 if (name[0] == '\0')
309 putc('.', file);
310 else
311 fputs(name, file);
312 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900313}
314
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900315const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
316 return (p_cdnname(cp, msg, PACKETSZ, file));
Bernie Innocenti55864192018-08-30 04:05:20 +0900317}
318
319/* Return a fully-qualified domain name from a compressed name (with
320 length supplied). */
321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900322const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
323 int n;
324 size_t newlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900325
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900326 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
327 newlen = strlen(name);
328 if (newlen == 0 || name[newlen - 1] != '.') {
329 if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
330 return (NULL);
331 else
332 strcpy(name + newlen, ".");
333 }
334 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900335}
336
337/* XXX: the rest of these functions need to become length-limited, too. */
338
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900339const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
340 char name[MAXDNAME];
341 const u_char* n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900342
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343 n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
344 if (n == NULL) return (NULL);
345 fputs(name, file);
346 return (n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900347}
348
349/*
350 * Names of RR classes and qclasses. Classes and qclasses are the same, except
351 * that C_ANY is a qclass but not a class. (You can ask for records of class
352 * C_ANY, but you can't have any records of that class in the database.)
353 */
354const struct res_sym __p_class_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900355 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
356 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
357 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
359/*
360 * Names of message sections.
361 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
363 {ns_s_an, "ANSWER", (char*) 0},
364 {ns_s_ns, "AUTHORITY", (char*) 0},
365 {ns_s_ar, "ADDITIONAL", (char*) 0},
366 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900367
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900368static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
369 {S_PREREQ, "PREREQUISITE", (char*) 0},
370 {S_UPDATE, "UPDATE", (char*) 0},
371 {S_ADDT, "ADDITIONAL", (char*) 0},
372 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900373
374const struct res_sym __p_key_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900375 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
376 {NS_ALG_DH, "DH", "Diffie Hellman"},
377 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
378 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
379 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
380 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900381
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382const struct res_sym __p_cert_syms[] = {{cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
383 {cert_t_spki, "SPKI", "SPKI certificate"},
384 {cert_t_pgp, "PGP", "PGP certificate"},
385 {cert_t_url, "URL", "URL Private"},
386 {cert_t_oid, "OID", "OID Private"},
387 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900388
389/*
390 * Names of RR types and qtypes. Types and qtypes are the same, except
391 * that T_ANY is a qtype but not a type. (You can ask for records of type
392 * T_ANY, but you can't have any records of that type in the database.)
393 */
394const struct res_sym __p_type_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900395 {ns_t_a, "A", "address"},
396 {ns_t_ns, "NS", "name server"},
397 {ns_t_md, "MD", "mail destination (deprecated)"},
398 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
399 {ns_t_cname, "CNAME", "canonical name"},
400 {ns_t_soa, "SOA", "start of authority"},
401 {ns_t_mb, "MB", "mailbox"},
402 {ns_t_mg, "MG", "mail group member"},
403 {ns_t_mr, "MR", "mail rename"},
404 {ns_t_null, "NULL", "null"},
405 {ns_t_wks, "WKS", "well-known service (deprecated)"},
406 {ns_t_ptr, "PTR", "domain name pointer"},
407 {ns_t_hinfo, "HINFO", "host information"},
408 {ns_t_minfo, "MINFO", "mailbox information"},
409 {ns_t_mx, "MX", "mail exchanger"},
410 {ns_t_txt, "TXT", "text"},
411 {ns_t_rp, "RP", "responsible person"},
412 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
413 {ns_t_x25, "X25", "X25 address"},
414 {ns_t_isdn, "ISDN", "ISDN address"},
415 {ns_t_rt, "RT", "router"},
416 {ns_t_nsap, "NSAP", "nsap address"},
417 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
418 {ns_t_sig, "SIG", "signature"},
419 {ns_t_key, "KEY", "key"},
420 {ns_t_px, "PX", "mapping information"},
421 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
422 {ns_t_aaaa, "AAAA", "IPv6 address"},
423 {ns_t_loc, "LOC", "location"},
424 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
425 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
426 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
427 {ns_t_srv, "SRV", "server selection"},
428 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
429 {ns_t_naptr, "NAPTR", "naptr"},
430 {ns_t_kx, "KX", "key exchange"},
431 {ns_t_cert, "CERT", "certificate"},
432 {ns_t_a6, "A", "IPv6 address (experminental)"},
433 {ns_t_dname, "DNAME", "non-terminal redirection"},
434 {ns_t_opt, "OPT", "opt"},
435 {ns_t_apl, "apl", "apl"},
436 {ns_t_ds, "DS", "delegation signer"},
437 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
438 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
439 {ns_t_rrsig, "RRSIG", "rrsig"},
440 {ns_t_nsec, "NSEC", "nsec"},
441 {ns_t_dnskey, "DNSKEY", "DNS key"},
442 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
443 {ns_t_nsec3, "NSEC3", "nsec3"},
444 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
445 {ns_t_hip, "HIP", "host identity protocol"},
446 {ns_t_spf, "SPF", "sender policy framework"},
447 {ns_t_tkey, "TKEY", "tkey"},
448 {ns_t_tsig, "TSIG", "transaction signature"},
449 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
450 {ns_t_axfr, "AXFR", "zone transfer"},
451 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
452 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
453 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
454 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
455 {ns_t_kx, "KX", "Key Exchange"},
456 {ns_t_cert, "CERT", "Certificate"},
457 {ns_t_a6, "A6", "IPv6 Address"},
458 {ns_t_dname, "DNAME", "dname"},
459 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
460 {ns_t_opt, "OPT", "EDNS Options"},
461 {ns_t_any, "ANY", "\"any\""},
462 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
463 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900464
465/*
466 * Names of DNS rcodes.
467 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
469 {ns_r_formerr, "FORMERR", "format error"},
470 {ns_r_servfail, "SERVFAIL", "server failed"},
471 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
472 {ns_r_notimpl, "NOTIMP", "not implemented"},
473 {ns_r_refused, "REFUSED", "refused"},
474 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
475 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
476 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
477 {ns_r_notauth, "NOTAUTH", "not authoritative"},
478 {ns_r_notzone, "NOTZONE", "Not in zone"},
479 {ns_r_max, "", ""},
480 {ns_r_badsig, "BADSIG", "bad signature"},
481 {ns_r_badkey, "BADKEY", "bad key"},
482 {ns_r_badtime, "BADTIME", "bad time"},
483 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485int sym_ston(const struct res_sym* syms, const char* name, int* success) {
486 for (; syms->name != 0; syms++) {
487 if (strcasecmp(name, syms->name) == 0) {
488 if (success) *success = 1;
489 return (syms->number);
490 }
491 }
492 if (success) *success = 0;
493 return (syms->number); /* The default value. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900494}
495
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
497 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900498
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 for (; syms->name != 0; syms++) {
500 if (number == syms->number) {
501 if (success) *success = 1;
502 return (syms->name);
503 }
504 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
507 if (success) *success = 0;
508 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900509}
510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511const char* sym_ntop(const struct res_sym* syms, int number, int* success) {
512 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 for (; syms->name != 0; syms++) {
515 if (number == syms->number) {
516 if (success) *success = 1;
517 return (syms->humanname);
518 }
519 }
520 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
521 if (success) *success = 0;
522 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900523}
524
525/*
526 * Return a string for the type.
527 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528const char* p_type(int type) {
529 int success;
530 const char* result;
531 static char typebuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900532
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900533 result = sym_ntos(__p_type_syms, type, &success);
534 if (success) return (result);
535 if (type < 0 || type > 0xffff) return ("BADTYPE");
536 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
537 return (typebuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900538}
539
540/*
541 * Return a string for the type.
542 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900543const char* p_section(int section, int opcode) {
544 const struct res_sym* symbols;
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 switch (opcode) {
547 case ns_o_update:
548 symbols = __p_update_section_syms;
549 break;
550 default:
551 symbols = __p_default_section_syms;
552 break;
553 }
554 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900555}
556
557/*
558 * Return a mnemonic for class.
559 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900560const char* p_class(int class) {
561 int success;
562 const char* result;
563 static char classbuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900564
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 result = sym_ntos(__p_class_syms, class, &success);
566 if (success) return (result);
567 if (class < 0 || class > 0xffff) return ("BADCLASS");
568 snprintf(classbuf, sizeof(classbuf), "CLASS%d", class);
569 return (classbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900570}
571
572/*
573 * Return a mnemonic for an option
574 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900575const char* p_option(u_long option) {
576 static char nbuf[40];
Bernie Innocenti55864192018-08-30 04:05:20 +0900577
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 switch (option) {
579 case RES_INIT:
580 return "init";
581 case RES_DEBUG:
582 return "debug";
583 case RES_AAONLY:
584 return "aaonly(unimpl)";
585 case RES_USEVC:
586 return "usevc";
587 case RES_PRIMARY:
588 return "primry(unimpl)";
589 case RES_IGNTC:
590 return "igntc";
591 case RES_RECURSE:
592 return "recurs";
593 case RES_DEFNAMES:
594 return "defnam";
595 case RES_STAYOPEN:
596 return "styopn";
597 case RES_DNSRCH:
598 return "dnsrch";
599 case RES_INSECURE1:
600 return "insecure1";
601 case RES_INSECURE2:
602 return "insecure2";
603 case RES_NOALIASES:
604 return "noaliases";
605 case RES_USE_INET6:
606 return "inet6";
607#ifdef RES_USE_EDNS0 /* KAME extension */
608 case RES_USE_EDNS0:
609 return "edns0";
Bernie Innocenti55864192018-08-30 04:05:20 +0900610#endif
611#ifdef RES_USE_DNAME
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612 case RES_USE_DNAME:
613 return "dname";
Bernie Innocenti55864192018-08-30 04:05:20 +0900614#endif
615#ifdef RES_USE_DNSSEC
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616 case RES_USE_DNSSEC:
617 return "dnssec";
Bernie Innocenti55864192018-08-30 04:05:20 +0900618#endif
619#ifdef RES_NOTLDQUERY
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 case RES_NOTLDQUERY:
621 return "no-tld-query";
Bernie Innocenti55864192018-08-30 04:05:20 +0900622#endif
623#ifdef RES_NO_NIBBLE2
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 case RES_NO_NIBBLE2:
625 return "no-nibble2";
Bernie Innocenti55864192018-08-30 04:05:20 +0900626#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900627 /* XXX nonreentrant */
628 default:
629 snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long) option);
630 return (nbuf);
631 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900632}
633
634/*
635 * Return a mnemonic for a time to live.
636 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637const char* p_time(u_int32_t value) {
638 static char nbuf[40]; /* XXX nonreentrant */
Bernie Innocenti55864192018-08-30 04:05:20 +0900639
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640 if (ns_format_ttl((u_long) value, nbuf, sizeof nbuf) < 0)
641 snprintf(nbuf, sizeof(nbuf), "%u", value);
642 return (nbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900643}
644
645/*
646 * Return a string for the rcode.
647 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648const char* p_rcode(int rcode) {
649 return (sym_ntos(__p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900650}
651
652/*
653 * Return a string for a res_sockaddr_union.
654 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900655const char* p_sockun(union res_sockaddr_union u, char* buf, size_t size) {
656 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
Bernie Innocenti55864192018-08-30 04:05:20 +0900657
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900658 switch (u.sin.sin_family) {
659 case AF_INET:
660 inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t) sizeof ret);
661 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900662#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 case AF_INET6:
664 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
665 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900666#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 default:
668 snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family);
669 break;
670 }
671 if (size > 0U) {
672 strncpy(buf, ret, size - 1);
673 buf[size - 1] = '0';
674 }
675 return (buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900676}
677
678/*
679 * routines to convert between on-the-wire RR format and zone file format.
680 * Does not contain conversion to/from decimal degrees; divide or multiply
681 * by 60*60*1000 for that.
682 */
683
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000,
685 100000, 1000000, 10000000, 100000000, 1000000000};
Bernie Innocenti55864192018-08-30 04:05:20 +0900686
687/* takes an XeY precision/size value, returns a string representation. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688static const char* precsize_ntoa(u_int32_t prec) {
689 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
690 unsigned long val;
691 int mantissa, exponent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 mantissa = (int) ((prec >> 4) & 0x0f) % 10;
694 exponent = (int) ((prec >> 0) & 0x0f) % 10;
Bernie Innocenti55864192018-08-30 04:05:20 +0900695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696 val = mantissa * poweroften[exponent];
Bernie Innocenti55864192018-08-30 04:05:20 +0900697
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val / 100, val % 100);
699 return (retbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900700}
701
702/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703static u_int8_t precsize_aton(const char** strptr) {
704 unsigned int mval = 0, cmval = 0;
705 u_int8_t retval = 0;
706 const char* cp;
707 int exponent;
708 int mantissa;
Bernie Innocenti55864192018-08-30 04:05:20 +0900709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 cp = *strptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 while (isdigit((unsigned char) *cp)) mval = mval * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 if (*cp == '.') { /* centimeters */
715 cp++;
716 if (isdigit((unsigned char) *cp)) {
717 cmval = (*cp++ - '0') * 10;
718 if (isdigit((unsigned char) *cp)) {
719 cmval += (*cp++ - '0');
720 }
721 }
722 }
723 cmval = (mval * 100) + cmval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 for (exponent = 0; exponent < 9; exponent++)
726 if (cmval < poweroften[exponent + 1]) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900728 mantissa = cmval / poweroften[exponent];
729 if (mantissa > 9) mantissa = 9;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 retval = (mantissa << 4) | exponent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 *strptr = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 return (retval);
Bernie Innocenti55864192018-08-30 04:05:20 +0900736}
737
738/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739static u_int32_t latlon2ul(const char** latlonstrptr, int* which) {
740 const char* cp;
741 u_int32_t retval;
742 int deg = 0, min = 0, secs = 0, secsfrac = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 cp = *latlonstrptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900745
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746 while (isdigit((unsigned char) *cp)) deg = deg * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900749
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900750 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti55864192018-08-30 04:05:20 +0900751
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 while (isdigit((unsigned char) *cp)) min = min * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 while (isdigit((unsigned char) *cp)) secs = secs * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 if (*cp == '.') { /* decimal seconds */
761 cp++;
762 if (isdigit((unsigned char) *cp)) {
763 secsfrac = (*cp++ - '0') * 100;
764 if (isdigit((unsigned char) *cp)) {
765 secsfrac += (*cp++ - '0') * 10;
766 if (isdigit((unsigned char) *cp)) {
767 secsfrac += (*cp++ - '0');
768 }
769 }
770 }
771 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
774 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778fndhemi:
779 switch (*cp) {
780 case 'N':
781 case 'n':
782 case 'E':
783 case 'e':
784 retval = ((unsigned) 1 << 31) + (((((deg * 60) + min) * 60) + secs) * 1000) + secsfrac;
785 break;
786 case 'S':
787 case 's':
788 case 'W':
789 case 'w':
790 retval = ((unsigned) 1 << 31) - (((((deg * 60) + min) * 60) + secs) * 1000) - secsfrac;
791 break;
792 default:
793 retval = 0; /* invalid value -- indicates error */
794 break;
795 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 switch (*cp) {
798 case 'N':
799 case 'n':
800 case 'S':
801 case 's':
802 *which = 1; /* latitude */
803 break;
804 case 'E':
805 case 'e':
806 case 'W':
807 case 'w':
808 *which = 2; /* longitude */
809 break;
810 default:
811 *which = 0; /* error */
812 break;
813 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815 cp++; /* skip the hemisphere */
Bernie Innocenti55864192018-08-30 04:05:20 +0900816
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
818 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900819
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900820 while (isspace((unsigned char) *cp)) /* move to next field */
821 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900822
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900823 *latlonstrptr = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825 return (retval);
Bernie Innocenti55864192018-08-30 04:05:20 +0900826}
827
828/* converts a zone file representation in a string to an RDATA on-the-wire
829 * representation. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900830int loc_aton(const char* ascii, u_char* binary) {
831 const char *cp, *maxcp;
832 u_char* bcp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900833
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900834 u_int32_t latit = 0, longit = 0, alt = 0;
835 u_int32_t lltemp1 = 0, lltemp2 = 0;
836 int altmeters = 0, altfrac = 0, altsign = 1;
837 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
838 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
839 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
840 int which1 = 0, which2 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900841
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900842 cp = ascii;
843 maxcp = cp + strlen(ascii);
Bernie Innocenti55864192018-08-30 04:05:20 +0900844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 lltemp1 = latlon2ul(&cp, &which1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900846
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 lltemp2 = latlon2ul(&cp, &which2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900848
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900849 switch (which1 + which2) {
850 case 3: /* 1 + 2, the only valid combination */
851 if ((which1 == 1) && (which2 == 2)) { /* normal case */
852 latit = lltemp1;
853 longit = lltemp2;
854 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
855 longit = lltemp1;
856 latit = lltemp2;
857 } else { /* some kind of brokenness */
858 return (0);
859 }
860 break;
861 default: /* we didn't get one of each */
862 return (0);
863 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900864
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900865 /* altitude */
866 if (*cp == '-') {
867 altsign = -1;
868 cp++;
869 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900870
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 if (*cp == '+') cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873 while (isdigit((unsigned char) *cp)) altmeters = altmeters * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900874
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 if (*cp == '.') { /* decimal meters */
876 cp++;
877 if (isdigit((unsigned char) *cp)) {
878 altfrac = (*cp++ - '0') * 10;
879 if (isdigit((unsigned char) *cp)) {
880 altfrac += (*cp++ - '0');
881 }
882 }
883 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900884
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900886
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900887 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
888 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900889
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900890 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900891
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900893
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900894 siz = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900895
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900896 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
897 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900898
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900900
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900901 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900902
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 hp = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900904
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900905 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
906 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912 vp = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914defaults:
Bernie Innocenti55864192018-08-30 04:05:20 +0900915
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 bcp = binary;
917 *bcp++ = (u_int8_t) 0; /* version byte */
918 *bcp++ = siz;
919 *bcp++ = hp;
920 *bcp++ = vp;
921 PUTLONG(latit, bcp);
922 PUTLONG(longit, bcp);
923 PUTLONG(alt, bcp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900924
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900925 return (16); /* size of RR in octets */
Bernie Innocenti55864192018-08-30 04:05:20 +0900926}
927
928/* takes an on-the-wire LOC RR and formats it in a human readable format. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900929const char* loc_ntoa(const u_char* binary, char* ascii, size_t bufsiz) {
930 static const char* error = "?";
931 static char tmpbuf[sizeof
Bernie Innocenti55864192018-08-30 04:05:20 +0900932"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900933 const u_char* cp = binary;
Bernie Innocenti55864192018-08-30 04:05:20 +0900934
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900935 int latdeg, latmin, latsec, latsecfrac;
936 int longdeg, longmin, longsec, longsecfrac;
937 char northsouth, eastwest;
938 const char* altsign;
939 int altmeters, altfrac;
Bernie Innocenti55864192018-08-30 04:05:20 +0900940
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 const u_int32_t referencealt = 100000 * 100;
Bernie Innocenti55864192018-08-30 04:05:20 +0900942
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900943 int32_t latval, longval, altval;
944 u_int32_t templ;
945 u_int8_t sizeval, hpval, vpval, versionval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 char *sizestr, *hpstr, *vpstr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900948
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900949 versionval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900950
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 if (ascii == NULL) ascii = tmpbuf;
Bernie Innocenti55864192018-08-30 04:05:20 +0900952
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900953 if (versionval) {
954 (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version");
955 return (ascii);
956 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900957
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900958 sizeval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900959
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900960 hpval = *cp++;
961 vpval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900962
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900963 GETLONG(templ, cp);
964 latval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti55864192018-08-30 04:05:20 +0900965
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900966 GETLONG(templ, cp);
967 longval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti55864192018-08-30 04:05:20 +0900968
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900969 GETLONG(templ, cp);
970 if (templ < referencealt) { /* below WGS 84 spheroid */
971 altval = referencealt - templ;
972 altsign = "-";
973 } else {
974 altval = templ - referencealt;
975 altsign = "";
976 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900977
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 if (latval < 0) {
979 northsouth = 'S';
980 latval = -latval;
981 } else
982 northsouth = 'N';
Bernie Innocenti55864192018-08-30 04:05:20 +0900983
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900984 latsecfrac = latval % 1000;
985 latval = latval / 1000;
986 latsec = latval % 60;
987 latval = latval / 60;
988 latmin = latval % 60;
989 latval = latval / 60;
990 latdeg = latval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900991
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900992 if (longval < 0) {
993 eastwest = 'W';
994 longval = -longval;
995 } else
996 eastwest = 'E';
Bernie Innocenti55864192018-08-30 04:05:20 +0900997
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900998 longsecfrac = longval % 1000;
999 longval = longval / 1000;
1000 longsec = longval % 60;
1001 longval = longval / 60;
1002 longmin = longval % 60;
1003 longval = longval / 60;
1004 longdeg = longval;
Bernie Innocenti55864192018-08-30 04:05:20 +09001005
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001006 altfrac = altval % 100;
1007 altmeters = (altval / 100);
Bernie Innocenti55864192018-08-30 04:05:20 +09001008
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001009 sizestr = strdup(precsize_ntoa((u_int32_t) sizeval));
1010 hpstr = strdup(precsize_ntoa((u_int32_t) hpval));
1011 vpstr = strdup(precsize_ntoa((u_int32_t) vpval));
Bernie Innocenti55864192018-08-30 04:05:20 +09001012
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001013 snprintf(ascii, bufsiz, "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
1014 latdeg, latmin, latsec, latsecfrac, northsouth, longdeg, longmin, longsec, longsecfrac,
1015 eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error,
1016 (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error);
Bernie Innocenti55864192018-08-30 04:05:20 +09001017
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001018 if (sizestr != NULL) free(sizestr);
1019 if (hpstr != NULL) free(hpstr);
1020 if (vpstr != NULL) free(vpstr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001021
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001022 return (ascii);
Bernie Innocenti55864192018-08-30 04:05:20 +09001023}
1024
Bernie Innocenti55864192018-08-30 04:05:20 +09001025/* Return the number of DNS hierarchy levels in the name. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001026int dn_count_labels(const char* name) {
1027 size_t len, i, count;
Bernie Innocenti55864192018-08-30 04:05:20 +09001028
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001029 len = strlen(name);
1030 for (i = 0, count = 0; i < len; i++) {
1031 /* XXX need to check for \. or use named's nlabels(). */
1032 if (name[i] == '.') count++;
1033 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001034
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001035 /* don't count initial wildcard */
1036 if (name[0] == '*')
1037 if (count) count--;
Bernie Innocenti55864192018-08-30 04:05:20 +09001038
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001039 /* don't count the null label for root. */
1040 /* if terminating '.' not found, must adjust */
1041 /* count to include last label */
1042 if (len > 0 && name[len - 1] != '.') count++;
1043 _DIAGASSERT(__type_fit(int, count));
1044 return (int) count;
Bernie Innocenti55864192018-08-30 04:05:20 +09001045}
1046
Bernie Innocenti55864192018-08-30 04:05:20 +09001047/*
1048 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1049 * SIG records are required to be printed like this, by the Secure DNS RFC.
1050 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001051char* p_secstodate(u_long secs) {
1052 /* XXX nonreentrant */
1053 static char output[15]; /* YYYYMMDDHHMMSS and null */
1054 time_t myclock = secs;
1055 struct tm* mytime;
Bernie Innocenti55864192018-08-30 04:05:20 +09001056#ifdef HAVE_TIME_R
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001057 struct tm res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001058
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001059 mytime = gmtime_r(&myclock, &res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001060#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001061 mytime = gmtime(&myclock);
Bernie Innocenti55864192018-08-30 04:05:20 +09001062#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001063 mytime->tm_year += 1900;
1064 mytime->tm_mon += 1;
1065 snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d", mytime->tm_year, mytime->tm_mon,
1066 mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
1067 return (output);
Bernie Innocenti55864192018-08-30 04:05:20 +09001068}
1069
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001070u_int16_t res_nametoclass(const char* buf, int* successp) {
1071 unsigned long result;
1072 char* endptr;
1073 int success;
Bernie Innocenti55864192018-08-30 04:05:20 +09001074
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001075 result = sym_ston(__p_class_syms, buf, &success);
1076 if (success) goto done;
Bernie Innocenti55864192018-08-30 04:05:20 +09001077
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001078 if (strncasecmp(buf, "CLASS", 5) != 0 || !isdigit((unsigned char) buf[5])) goto done;
1079 errno = 0;
1080 result = strtoul(buf + 5, &endptr, 10);
1081 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1082done:
1083 if (successp) *successp = success;
1084 return (u_int16_t)(result);
Bernie Innocenti55864192018-08-30 04:05:20 +09001085}
1086
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001087u_int16_t res_nametotype(const char* buf, int* successp) {
1088 unsigned long result;
1089 char* endptr;
1090 int success;
Bernie Innocenti55864192018-08-30 04:05:20 +09001091
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001092 result = sym_ston(__p_type_syms, buf, &success);
1093 if (success) goto done;
Bernie Innocenti55864192018-08-30 04:05:20 +09001094
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001095 if (strncasecmp(buf, "type", 4) != 0 || !isdigit((unsigned char) buf[4])) goto done;
1096 errno = 0;
1097 result = strtoul(buf + 4, &endptr, 10);
1098 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1099done:
1100 if (successp) *successp = success;
1101 return (u_int16_t)(result);
Bernie Innocenti55864192018-08-30 04:05:20 +09001102}