blob: 931ffde63bfa2d9d1a7ea9666ea402b91870a4bf [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
Bernie Innocenti55864192018-08-30 04:05:20 +090098#include <sys/param.h>
99#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900100#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900101
Bernie Innocenti55864192018-08-30 04:05:20 +0900102#include <arpa/inet.h>
103#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900104#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900105
106#include <ctype.h>
107#include <errno.h>
108#include <math.h>
109#include <netdb.h>
Bernie Innocenti55864192018-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 Innocentif12d5bb2018-08-31 14:09:46 +0900115#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900116
Bernie Innocentif89b3512018-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 Innocentif12d5bb2018-08-31 14:09:46 +0900122extern const char* const _res_opcodes[];
123extern const char* const _res_sectioncodes[];
Bernie Innocenti55864192018-08-30 04:05:20 +0900124
Bernie Innocentif12d5bb2018-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;
129 char* buf;
130 ns_opcode opcode;
131 ns_rr rr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900132
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900133 /*
134 * Print answer records.
135 */
136 sflag = (int) (statp->pfcode & pflag);
137 if (statp->pfcode && !sflag) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900138
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 buf = malloc((size_t) buflen);
140 if (buf == NULL) {
141 fprintf(file, ";; memory allocation failure\n");
142 return;
143 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900144
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode);
146 rrnum = 0;
147 for (;;) {
148 if (ns_parserr(handle, section, rrnum, &rr)) {
149 if (errno != ENODEV)
150 fprintf(file, ";; ns_parserr: %s\n", strerror(errno));
151 else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
152 putc('\n', file);
153 goto cleanup;
154 }
155 if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1))
156 fprintf(file, ";; %s SECTION:\n", p_section(section, opcode));
157 if (section == ns_s_qd)
158 fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)),
159 p_class(ns_rr_class(rr)));
160 else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
161 size_t rdatalen, ttl;
162 uint16_t optcode, optlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900163
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 rdatalen = ns_rr_rdlen(rr);
165 ttl = ns_rr_ttl(rr);
166 fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff,
167 ns_rr_class(rr), ttl & 0xffff);
168 while (rdatalen >= 4) {
169 const u_char* cp = ns_rr_rdata(rr);
170 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900171
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900172 GETSHORT(optcode, cp);
173 GETSHORT(optlen, cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175 if (optcode == NS_OPT_NSID) {
176 fputs("; NSID: ", file);
177 if (optlen == 0) {
178 fputs("; NSID\n", file);
179 } else {
180 fputs("; NSID: ", file);
181 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
182 fputs(" (", file);
183 for (i = 0; i < optlen; i++)
184 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
185 fputs(")\n", file);
186 }
187 } else {
188 if (optlen == 0) {
189 fprintf(file, "; OPT=%u\n", optcode);
190 } else {
191 fprintf(file, "; OPT=%u: ", optcode);
192 for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]);
193 fputs(" (", file);
194 for (i = 0; i < optlen; i++)
195 fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.');
196 fputs(")\n", file);
197 }
198 }
199 rdatalen -= 4 + optlen;
200 }
201 } else {
202 n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen);
203 if (n < 0) {
204 if (errno == ENOSPC) {
205 free(buf);
206 buf = NULL;
207 if (buflen < 131072) buf = malloc((size_t)(buflen += 1024));
208 if (buf == NULL) {
209 fprintf(file, ";; memory allocation failure\n");
210 return;
211 }
212 continue;
213 }
214 fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno));
215 goto cleanup;
216 }
217 fputs(buf, file);
218 fputc('\n', file);
219 }
220 rrnum++;
221 }
222cleanup:
223 if (buf != NULL) free(buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900224}
225
226/*
227 * Print the contents of a query.
228 * This is intended to be primarily a debugging routine.
229 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900230void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) {
231 ns_msg handle;
232 int qdcount, ancount, nscount, arcount;
233 u_int opcode, rcode, id;
Bernie Innocenti55864192018-08-30 04:05:20 +0900234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900235 if (ns_initparse(msg, len, &handle) < 0) {
236 fprintf(file, ";; ns_initparse: %s\n", strerror(errno));
237 return;
238 }
239 opcode = ns_msg_getflag(handle, ns_f_opcode);
240 rcode = ns_msg_getflag(handle, ns_f_rcode);
241 id = ns_msg_id(handle);
242 qdcount = ns_msg_count(handle, ns_s_qd);
243 ancount = ns_msg_count(handle, ns_s_an);
244 nscount = ns_msg_count(handle, ns_s_ns);
245 arcount = ns_msg_count(handle, ns_s_ar);
Bernie Innocenti55864192018-08-30 04:05:20 +0900246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247 /*
248 * Print header fields.
249 */
250 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode)
251 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode],
252 p_rcode((int) rcode), id);
253 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file);
254 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) {
255 fprintf(file, "; flags:");
256 if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr");
257 if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa");
258 if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc");
259 if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd");
260 if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra");
261 if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??");
262 if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad");
263 if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd");
264 }
265 if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) {
266 fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount);
267 fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount);
268 fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount);
269 fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount);
270 }
271 if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
272 putc('\n', file);
273 }
274 /*
275 * Print the various sections.
276 */
277 do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file);
278 do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file);
279 do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file);
280 do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file);
281 if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900282}
283
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) {
285 char name[MAXDNAME];
286 int n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900288 if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL);
289 if (name[0] == '\0')
290 putc('.', file);
291 else
292 fputs(name, file);
293 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900294}
295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) {
297 return (p_cdnname(cp, msg, PACKETSZ, file));
Bernie Innocenti55864192018-08-30 04:05:20 +0900298}
299
300/* Return a fully-qualified domain name from a compressed name (with
301 length supplied). */
302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900303const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) {
304 int n;
305 size_t newlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL);
308 newlen = strlen(name);
309 if (newlen == 0 || name[newlen - 1] != '.') {
310 if ((int) newlen + 1 >= namelen) /* Lack space for final dot */
311 return (NULL);
312 else
313 strcpy(name + newlen, ".");
314 }
315 return (cp + n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
318/* XXX: the rest of these functions need to become length-limited, too. */
319
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900320const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) {
321 char name[MAXDNAME];
322 const u_char* n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900323
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900324 n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name);
325 if (n == NULL) return (NULL);
326 fputs(name, file);
327 return (n);
Bernie Innocenti55864192018-08-30 04:05:20 +0900328}
329
330/*
331 * Names of RR classes and qclasses. Classes and qclasses are the same, except
332 * that C_ANY is a qclass but not a class. (You can ask for records of class
333 * C_ANY, but you can't have any records of that class in the database.)
334 */
335const struct res_sym __p_class_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0},
337 {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
338 {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900339
340/*
341 * Names of message sections.
342 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
344 {ns_s_an, "ANSWER", (char*) 0},
345 {ns_s_ns, "AUTHORITY", (char*) 0},
346 {ns_s_ar, "ADDITIONAL", (char*) 0},
347 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900348
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
350 {S_PREREQ, "PREREQUISITE", (char*) 0},
351 {S_UPDATE, "UPDATE", (char*) 0},
352 {S_ADDT, "ADDITIONAL", (char*) 0},
353 {0, (char*) 0, (char*) 0}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
355const struct res_sym __p_key_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900356 {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"},
357 {NS_ALG_DH, "DH", "Diffie Hellman"},
358 {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"},
359 {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"},
360 {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"},
361 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900362
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900363const struct res_sym __p_cert_syms[] = {{cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"},
364 {cert_t_spki, "SPKI", "SPKI certificate"},
365 {cert_t_pgp, "PGP", "PGP certificate"},
366 {cert_t_url, "URL", "URL Private"},
367 {cert_t_oid, "OID", "OID Private"},
368 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900369
370/*
371 * Names of RR types and qtypes. Types and qtypes are the same, except
372 * that T_ANY is a qtype but not a type. (You can ask for records of type
373 * T_ANY, but you can't have any records of that type in the database.)
374 */
375const struct res_sym __p_type_syms[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900376 {ns_t_a, "A", "address"},
377 {ns_t_ns, "NS", "name server"},
378 {ns_t_md, "MD", "mail destination (deprecated)"},
379 {ns_t_mf, "MF", "mail forwarder (deprecated)"},
380 {ns_t_cname, "CNAME", "canonical name"},
381 {ns_t_soa, "SOA", "start of authority"},
382 {ns_t_mb, "MB", "mailbox"},
383 {ns_t_mg, "MG", "mail group member"},
384 {ns_t_mr, "MR", "mail rename"},
385 {ns_t_null, "NULL", "null"},
386 {ns_t_wks, "WKS", "well-known service (deprecated)"},
387 {ns_t_ptr, "PTR", "domain name pointer"},
388 {ns_t_hinfo, "HINFO", "host information"},
389 {ns_t_minfo, "MINFO", "mailbox information"},
390 {ns_t_mx, "MX", "mail exchanger"},
391 {ns_t_txt, "TXT", "text"},
392 {ns_t_rp, "RP", "responsible person"},
393 {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
394 {ns_t_x25, "X25", "X25 address"},
395 {ns_t_isdn, "ISDN", "ISDN address"},
396 {ns_t_rt, "RT", "router"},
397 {ns_t_nsap, "NSAP", "nsap address"},
398 {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
399 {ns_t_sig, "SIG", "signature"},
400 {ns_t_key, "KEY", "key"},
401 {ns_t_px, "PX", "mapping information"},
402 {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
403 {ns_t_aaaa, "AAAA", "IPv6 address"},
404 {ns_t_loc, "LOC", "location"},
405 {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
406 {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
407 {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
408 {ns_t_srv, "SRV", "server selection"},
409 {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
410 {ns_t_naptr, "NAPTR", "naptr"},
411 {ns_t_kx, "KX", "key exchange"},
412 {ns_t_cert, "CERT", "certificate"},
413 {ns_t_a6, "A", "IPv6 address (experminental)"},
414 {ns_t_dname, "DNAME", "non-terminal redirection"},
415 {ns_t_opt, "OPT", "opt"},
416 {ns_t_apl, "apl", "apl"},
417 {ns_t_ds, "DS", "delegation signer"},
418 {ns_t_sshfp, "SSFP", "SSH fingerprint"},
419 {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
420 {ns_t_rrsig, "RRSIG", "rrsig"},
421 {ns_t_nsec, "NSEC", "nsec"},
422 {ns_t_dnskey, "DNSKEY", "DNS key"},
423 {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
424 {ns_t_nsec3, "NSEC3", "nsec3"},
425 {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
426 {ns_t_hip, "HIP", "host identity protocol"},
427 {ns_t_spf, "SPF", "sender policy framework"},
428 {ns_t_tkey, "TKEY", "tkey"},
429 {ns_t_tsig, "TSIG", "transaction signature"},
430 {ns_t_ixfr, "IXFR", "incremental zone transfer"},
431 {ns_t_axfr, "AXFR", "zone transfer"},
432 {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
433 {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
434 {ns_t_maila, "MAILA", "mail agent (deprecated)"},
435 {ns_t_naptr, "NAPTR", "URN Naming Authority"},
436 {ns_t_kx, "KX", "Key Exchange"},
437 {ns_t_cert, "CERT", "Certificate"},
438 {ns_t_a6, "A6", "IPv6 Address"},
439 {ns_t_dname, "DNAME", "dname"},
440 {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
441 {ns_t_opt, "OPT", "EDNS Options"},
442 {ns_t_any, "ANY", "\"any\""},
443 {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
444 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900445
446/*
447 * Names of DNS rcodes.
448 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
450 {ns_r_formerr, "FORMERR", "format error"},
451 {ns_r_servfail, "SERVFAIL", "server failed"},
452 {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
453 {ns_r_notimpl, "NOTIMP", "not implemented"},
454 {ns_r_refused, "REFUSED", "refused"},
455 {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
456 {ns_r_yxrrset, "YXRRSET", "rrset exists"},
457 {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
458 {ns_r_notauth, "NOTAUTH", "not authoritative"},
459 {ns_r_notzone, "NOTZONE", "Not in zone"},
460 {ns_r_max, "", ""},
461 {ns_r_badsig, "BADSIG", "bad signature"},
462 {ns_r_badkey, "BADKEY", "bad key"},
463 {ns_r_badtime, "BADTIME", "bad time"},
464 {0, NULL, NULL}};
Bernie Innocenti55864192018-08-30 04:05:20 +0900465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466int sym_ston(const struct res_sym* syms, const char* name, int* success) {
467 for (; syms->name != 0; syms++) {
468 if (strcasecmp(name, syms->name) == 0) {
469 if (success) *success = 1;
470 return (syms->number);
471 }
472 }
473 if (success) *success = 0;
474 return (syms->number); /* The default value. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900475}
476
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900477const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
478 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900479
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 for (; syms->name != 0; syms++) {
481 if (number == syms->number) {
482 if (success) *success = 1;
483 return (syms->name);
484 }
485 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900486
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900487 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
488 if (success) *success = 0;
489 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900490}
491
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492const char* sym_ntop(const struct res_sym* syms, int number, int* success) {
493 static char unname[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900494
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 for (; syms->name != 0; syms++) {
496 if (number == syms->number) {
497 if (success) *success = 1;
498 return (syms->humanname);
499 }
500 }
501 snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
502 if (success) *success = 0;
503 return (unname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900504}
505
506/*
507 * Return a string for the type.
508 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509const char* p_type(int type) {
510 int success;
511 const char* result;
512 static char typebuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900513
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900514 result = sym_ntos(__p_type_syms, type, &success);
515 if (success) return (result);
516 if (type < 0 || type > 0xffff) return ("BADTYPE");
517 snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
518 return (typebuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900519}
520
521/*
522 * Return a string for the type.
523 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900524const char* p_section(int section, int opcode) {
525 const struct res_sym* symbols;
Bernie Innocenti55864192018-08-30 04:05:20 +0900526
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900527 switch (opcode) {
528 case ns_o_update:
529 symbols = __p_update_section_syms;
530 break;
531 default:
532 symbols = __p_default_section_syms;
533 break;
534 }
535 return (sym_ntos(symbols, section, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900536}
537
538/*
539 * Return a mnemonic for class.
540 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541const char* p_class(int class) {
542 int success;
543 const char* result;
544 static char classbuf[20];
Bernie Innocenti55864192018-08-30 04:05:20 +0900545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 result = sym_ntos(__p_class_syms, class, &success);
547 if (success) return (result);
548 if (class < 0 || class > 0xffff) return ("BADCLASS");
549 snprintf(classbuf, sizeof(classbuf), "CLASS%d", class);
550 return (classbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900551}
552
553/*
554 * Return a mnemonic for an option
555 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900556const char* p_option(u_long option) {
557 static char nbuf[40];
Bernie Innocenti55864192018-08-30 04:05:20 +0900558
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900559 switch (option) {
560 case RES_INIT:
561 return "init";
562 case RES_DEBUG:
563 return "debug";
564 case RES_AAONLY:
565 return "aaonly(unimpl)";
566 case RES_USEVC:
567 return "usevc";
568 case RES_PRIMARY:
569 return "primry(unimpl)";
570 case RES_IGNTC:
571 return "igntc";
572 case RES_RECURSE:
573 return "recurs";
574 case RES_DEFNAMES:
575 return "defnam";
576 case RES_STAYOPEN:
577 return "styopn";
578 case RES_DNSRCH:
579 return "dnsrch";
580 case RES_INSECURE1:
581 return "insecure1";
582 case RES_INSECURE2:
583 return "insecure2";
584 case RES_NOALIASES:
585 return "noaliases";
586 case RES_USE_INET6:
587 return "inet6";
588#ifdef RES_USE_EDNS0 /* KAME extension */
589 case RES_USE_EDNS0:
590 return "edns0";
Bernie Innocenti55864192018-08-30 04:05:20 +0900591#endif
592#ifdef RES_USE_DNAME
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593 case RES_USE_DNAME:
594 return "dname";
Bernie Innocenti55864192018-08-30 04:05:20 +0900595#endif
596#ifdef RES_USE_DNSSEC
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 case RES_USE_DNSSEC:
598 return "dnssec";
Bernie Innocenti55864192018-08-30 04:05:20 +0900599#endif
600#ifdef RES_NOTLDQUERY
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 case RES_NOTLDQUERY:
602 return "no-tld-query";
Bernie Innocenti55864192018-08-30 04:05:20 +0900603#endif
604#ifdef RES_NO_NIBBLE2
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 case RES_NO_NIBBLE2:
606 return "no-nibble2";
Bernie Innocenti55864192018-08-30 04:05:20 +0900607#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 /* XXX nonreentrant */
609 default:
610 snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long) option);
611 return (nbuf);
612 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900613}
614
615/*
616 * Return a mnemonic for a time to live.
617 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900618const char* p_time(u_int32_t value) {
619 static char nbuf[40]; /* XXX nonreentrant */
Bernie Innocenti55864192018-08-30 04:05:20 +0900620
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900621 if (ns_format_ttl((u_long) value, nbuf, sizeof nbuf) < 0)
622 snprintf(nbuf, sizeof(nbuf), "%u", value);
623 return (nbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900624}
625
626/*
627 * Return a string for the rcode.
628 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629const char* p_rcode(int rcode) {
630 return (sym_ntos(__p_rcode_syms, rcode, (int*) 0));
Bernie Innocenti55864192018-08-30 04:05:20 +0900631}
632
633/*
634 * Return a string for a res_sockaddr_union.
635 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900636const char* p_sockun(union res_sockaddr_union u, char* buf, size_t size) {
637 char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"];
Bernie Innocenti55864192018-08-30 04:05:20 +0900638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 switch (u.sin.sin_family) {
640 case AF_INET:
641 inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t) sizeof ret);
642 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900643#ifdef HAS_INET6_STRUCTS
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900644 case AF_INET6:
645 inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret);
646 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 default:
649 snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family);
650 break;
651 }
652 if (size > 0U) {
653 strncpy(buf, ret, size - 1);
654 buf[size - 1] = '0';
655 }
656 return (buf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900657}
658
659/*
660 * routines to convert between on-the-wire RR format and zone file format.
661 * Does not contain conversion to/from decimal degrees; divide or multiply
662 * by 60*60*1000 for that.
663 */
664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900665static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000,
666 100000, 1000000, 10000000, 100000000, 1000000000};
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
668/* takes an XeY precision/size value, returns a string representation. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669static const char* precsize_ntoa(u_int32_t prec) {
670 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
671 unsigned long val;
672 int mantissa, exponent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900674 mantissa = (int) ((prec >> 4) & 0x0f) % 10;
675 exponent = (int) ((prec >> 0) & 0x0f) % 10;
Bernie Innocenti55864192018-08-30 04:05:20 +0900676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 val = mantissa * poweroften[exponent];
Bernie Innocenti55864192018-08-30 04:05:20 +0900678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val / 100, val % 100);
680 return (retbuf);
Bernie Innocenti55864192018-08-30 04:05:20 +0900681}
682
683/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684static u_int8_t precsize_aton(const char** strptr) {
685 unsigned int mval = 0, cmval = 0;
686 u_int8_t retval = 0;
687 const char* cp;
688 int exponent;
689 int mantissa;
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 cp = *strptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 while (isdigit((unsigned char) *cp)) mval = mval * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900694
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 if (*cp == '.') { /* centimeters */
696 cp++;
697 if (isdigit((unsigned char) *cp)) {
698 cmval = (*cp++ - '0') * 10;
699 if (isdigit((unsigned char) *cp)) {
700 cmval += (*cp++ - '0');
701 }
702 }
703 }
704 cmval = (mval * 100) + cmval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900706 for (exponent = 0; exponent < 9; exponent++)
707 if (cmval < poweroften[exponent + 1]) break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 mantissa = cmval / poweroften[exponent];
710 if (mantissa > 9) mantissa = 9;
Bernie Innocenti55864192018-08-30 04:05:20 +0900711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900712 retval = (mantissa << 4) | exponent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 *strptr = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900715
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900716 return (retval);
Bernie Innocenti55864192018-08-30 04:05:20 +0900717}
718
719/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900720static u_int32_t latlon2ul(const char** latlonstrptr, int* which) {
721 const char* cp;
722 u_int32_t retval;
723 int deg = 0, min = 0, secs = 0, secsfrac = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 cp = *latlonstrptr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 while (isdigit((unsigned char) *cp)) deg = deg * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900728
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900729 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti55864192018-08-30 04:05:20 +0900732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900733 while (isdigit((unsigned char) *cp)) min = min * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900734
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900735 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 if (!(isdigit((unsigned char) *cp))) goto fndhemi;
Bernie Innocenti55864192018-08-30 04:05:20 +0900738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739 while (isdigit((unsigned char) *cp)) secs = secs * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900740
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900741 if (*cp == '.') { /* decimal seconds */
742 cp++;
743 if (isdigit((unsigned char) *cp)) {
744 secsfrac = (*cp++ - '0') * 100;
745 if (isdigit((unsigned char) *cp)) {
746 secsfrac += (*cp++ - '0') * 10;
747 if (isdigit((unsigned char) *cp)) {
748 secsfrac += (*cp++ - '0');
749 }
750 }
751 }
752 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
755 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900756
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757 while (isspace((unsigned char) *cp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900758
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900759fndhemi:
760 switch (*cp) {
761 case 'N':
762 case 'n':
763 case 'E':
764 case 'e':
765 retval = ((unsigned) 1 << 31) + (((((deg * 60) + min) * 60) + secs) * 1000) + secsfrac;
766 break;
767 case 'S':
768 case 's':
769 case 'W':
770 case 'w':
771 retval = ((unsigned) 1 << 31) - (((((deg * 60) + min) * 60) + secs) * 1000) - secsfrac;
772 break;
773 default:
774 retval = 0; /* invalid value -- indicates error */
775 break;
776 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900777
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900778 switch (*cp) {
779 case 'N':
780 case 'n':
781 case 'S':
782 case 's':
783 *which = 1; /* latitude */
784 break;
785 case 'E':
786 case 'e':
787 case 'W':
788 case 'w':
789 *which = 2; /* longitude */
790 break;
791 default:
792 *which = 0; /* error */
793 break;
794 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900795
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900796 cp++; /* skip the hemisphere */
Bernie Innocenti55864192018-08-30 04:05:20 +0900797
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900798 while (!isspace((unsigned char) *cp)) /* if any trailing garbage */
799 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 while (isspace((unsigned char) *cp)) /* move to next field */
802 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900803
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900804 *latlonstrptr = cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900805
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806 return (retval);
Bernie Innocenti55864192018-08-30 04:05:20 +0900807}
808
809/* converts a zone file representation in a string to an RDATA on-the-wire
810 * representation. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811int loc_aton(const char* ascii, u_char* binary) {
812 const char *cp, *maxcp;
813 u_char* bcp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815 u_int32_t latit = 0, longit = 0, alt = 0;
816 u_int32_t lltemp1 = 0, lltemp2 = 0;
817 int altmeters = 0, altfrac = 0, altsign = 1;
818 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
819 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
820 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
821 int which1 = 0, which2 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900822
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900823 cp = ascii;
824 maxcp = cp + strlen(ascii);
Bernie Innocenti55864192018-08-30 04:05:20 +0900825
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 lltemp1 = latlon2ul(&cp, &which1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 lltemp2 = latlon2ul(&cp, &which2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900829
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900830 switch (which1 + which2) {
831 case 3: /* 1 + 2, the only valid combination */
832 if ((which1 == 1) && (which2 == 2)) { /* normal case */
833 latit = lltemp1;
834 longit = lltemp2;
835 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
836 longit = lltemp1;
837 latit = lltemp2;
838 } else { /* some kind of brokenness */
839 return (0);
840 }
841 break;
842 default: /* we didn't get one of each */
843 return (0);
844 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 /* altitude */
847 if (*cp == '-') {
848 altsign = -1;
849 cp++;
850 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 if (*cp == '+') cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900853
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900854 while (isdigit((unsigned char) *cp)) altmeters = altmeters * 10 + (*cp++ - '0');
Bernie Innocenti55864192018-08-30 04:05:20 +0900855
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900856 if (*cp == '.') { /* decimal meters */
857 cp++;
858 if (isdigit((unsigned char) *cp)) {
859 altfrac = (*cp++ - '0') * 10;
860 if (isdigit((unsigned char) *cp)) {
861 altfrac += (*cp++ - '0');
862 }
863 }
864 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900865
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900867
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
869 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900870
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900874
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 siz = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900876
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900877 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
878 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900879
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900880 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900881
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900883
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900884 hp = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900885
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900886 while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */
887 cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900888
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889 while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900890
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900891 if (cp >= maxcp) goto defaults;
Bernie Innocenti55864192018-08-30 04:05:20 +0900892
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900893 vp = precsize_aton(&cp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900894
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900895defaults:
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897 bcp = binary;
898 *bcp++ = (u_int8_t) 0; /* version byte */
899 *bcp++ = siz;
900 *bcp++ = hp;
901 *bcp++ = vp;
902 PUTLONG(latit, bcp);
903 PUTLONG(longit, bcp);
904 PUTLONG(alt, bcp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900905
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900906 return (16); /* size of RR in octets */
Bernie Innocenti55864192018-08-30 04:05:20 +0900907}
908
909/* takes an on-the-wire LOC RR and formats it in a human readable format. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910const char* loc_ntoa(const u_char* binary, char* ascii, size_t bufsiz) {
911 static const char* error = "?";
912 static char tmpbuf[sizeof
Bernie Innocenti55864192018-08-30 04:05:20 +0900913"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 +0900914 const u_char* cp = binary;
Bernie Innocenti55864192018-08-30 04:05:20 +0900915
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900916 int latdeg, latmin, latsec, latsecfrac;
917 int longdeg, longmin, longsec, longsecfrac;
918 char northsouth, eastwest;
919 const char* altsign;
920 int altmeters, altfrac;
Bernie Innocenti55864192018-08-30 04:05:20 +0900921
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900922 const u_int32_t referencealt = 100000 * 100;
Bernie Innocenti55864192018-08-30 04:05:20 +0900923
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900924 int32_t latval, longval, altval;
925 u_int32_t templ;
926 u_int8_t sizeval, hpval, vpval, versionval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900927
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900928 char *sizestr, *hpstr, *vpstr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900929
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900930 versionval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900931
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900932 if (ascii == NULL) ascii = tmpbuf;
Bernie Innocenti55864192018-08-30 04:05:20 +0900933
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900934 if (versionval) {
935 (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version");
936 return (ascii);
937 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900938
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900939 sizeval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900940
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900941 hpval = *cp++;
942 vpval = *cp++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900943
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900944 GETLONG(templ, cp);
945 latval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 GETLONG(templ, cp);
948 longval = (templ - ((unsigned) 1 << 31));
Bernie Innocenti55864192018-08-30 04:05:20 +0900949
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900950 GETLONG(templ, cp);
951 if (templ < referencealt) { /* below WGS 84 spheroid */
952 altval = referencealt - templ;
953 altsign = "-";
954 } else {
955 altval = templ - referencealt;
956 altsign = "";
957 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900958
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 if (latval < 0) {
960 northsouth = 'S';
961 latval = -latval;
962 } else
963 northsouth = 'N';
Bernie Innocenti55864192018-08-30 04:05:20 +0900964
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900965 latsecfrac = latval % 1000;
966 latval = latval / 1000;
967 latsec = latval % 60;
968 latval = latval / 60;
969 latmin = latval % 60;
970 latval = latval / 60;
971 latdeg = latval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900972
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 if (longval < 0) {
974 eastwest = 'W';
975 longval = -longval;
976 } else
977 eastwest = 'E';
Bernie Innocenti55864192018-08-30 04:05:20 +0900978
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900979 longsecfrac = longval % 1000;
980 longval = longval / 1000;
981 longsec = longval % 60;
982 longval = longval / 60;
983 longmin = longval % 60;
984 longval = longval / 60;
985 longdeg = longval;
Bernie Innocenti55864192018-08-30 04:05:20 +0900986
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900987 altfrac = altval % 100;
988 altmeters = (altval / 100);
Bernie Innocenti55864192018-08-30 04:05:20 +0900989
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900990 sizestr = strdup(precsize_ntoa((u_int32_t) sizeval));
991 hpstr = strdup(precsize_ntoa((u_int32_t) hpval));
992 vpstr = strdup(precsize_ntoa((u_int32_t) vpval));
Bernie Innocenti55864192018-08-30 04:05:20 +0900993
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900994 snprintf(ascii, bufsiz, "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm",
995 latdeg, latmin, latsec, latsecfrac, northsouth, longdeg, longmin, longsec, longsecfrac,
996 eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error,
997 (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900998
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900999 if (sizestr != NULL) free(sizestr);
1000 if (hpstr != NULL) free(hpstr);
1001 if (vpstr != NULL) free(vpstr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001002
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001003 return (ascii);
Bernie Innocenti55864192018-08-30 04:05:20 +09001004}
1005
Bernie Innocenti55864192018-08-30 04:05:20 +09001006/* Return the number of DNS hierarchy levels in the name. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001007int dn_count_labels(const char* name) {
1008 size_t len, i, count;
Bernie Innocenti55864192018-08-30 04:05:20 +09001009
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001010 len = strlen(name);
1011 for (i = 0, count = 0; i < len; i++) {
1012 /* XXX need to check for \. or use named's nlabels(). */
1013 if (name[i] == '.') count++;
1014 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001015
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016 /* don't count initial wildcard */
1017 if (name[0] == '*')
1018 if (count) count--;
Bernie Innocenti55864192018-08-30 04:05:20 +09001019
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001020 /* don't count the null label for root. */
1021 /* if terminating '.' not found, must adjust */
1022 /* count to include last label */
1023 if (len > 0 && name[len - 1] != '.') count++;
1024 _DIAGASSERT(__type_fit(int, count));
1025 return (int) count;
Bernie Innocenti55864192018-08-30 04:05:20 +09001026}
1027
Bernie Innocenti55864192018-08-30 04:05:20 +09001028/*
1029 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1030 * SIG records are required to be printed like this, by the Secure DNS RFC.
1031 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001032char* p_secstodate(u_long secs) {
1033 /* XXX nonreentrant */
1034 static char output[15]; /* YYYYMMDDHHMMSS and null */
1035 time_t myclock = secs;
1036 struct tm* mytime;
Bernie Innocenti55864192018-08-30 04:05:20 +09001037#ifdef HAVE_TIME_R
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001038 struct tm res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001039
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001040 mytime = gmtime_r(&myclock, &res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001041#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 mytime = gmtime(&myclock);
Bernie Innocenti55864192018-08-30 04:05:20 +09001043#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001044 mytime->tm_year += 1900;
1045 mytime->tm_mon += 1;
1046 snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d", mytime->tm_year, mytime->tm_mon,
1047 mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec);
1048 return (output);
Bernie Innocenti55864192018-08-30 04:05:20 +09001049}
1050
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001051u_int16_t res_nametoclass(const char* buf, int* successp) {
1052 unsigned long result;
1053 char* endptr;
1054 int success;
Bernie Innocenti55864192018-08-30 04:05:20 +09001055
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001056 result = sym_ston(__p_class_syms, buf, &success);
1057 if (success) goto done;
Bernie Innocenti55864192018-08-30 04:05:20 +09001058
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001059 if (strncasecmp(buf, "CLASS", 5) != 0 || !isdigit((unsigned char) buf[5])) goto done;
1060 errno = 0;
1061 result = strtoul(buf + 5, &endptr, 10);
1062 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1063done:
1064 if (successp) *successp = success;
1065 return (u_int16_t)(result);
Bernie Innocenti55864192018-08-30 04:05:20 +09001066}
1067
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001068u_int16_t res_nametotype(const char* buf, int* successp) {
1069 unsigned long result;
1070 char* endptr;
1071 int success;
Bernie Innocenti55864192018-08-30 04:05:20 +09001072
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073 result = sym_ston(__p_type_syms, buf, &success);
1074 if (success) goto done;
Bernie Innocenti55864192018-08-30 04:05:20 +09001075
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001076 if (strncasecmp(buf, "type", 4) != 0 || !isdigit((unsigned char) buf[4])) goto done;
1077 errno = 0;
1078 result = strtoul(buf + 4, &endptr, 10);
1079 if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1;
1080done:
1081 if (successp) *successp = success;
1082 return (u_int16_t)(result);
Bernie Innocenti55864192018-08-30 04:05:20 +09001083}