Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 98 | #include <sys/param.h> |
| 99 | #include <sys/socket.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 100 | #include <sys/types.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 101 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | #include <arpa/inet.h> |
| 103 | #include <arpa/nameser.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 104 | #include <netinet/in.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 105 | |
| 106 | #include <ctype.h> |
| 107 | #include <errno.h> |
| 108 | #include <math.h> |
| 109 | #include <netdb.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | #include <stdio.h> |
| 111 | #include <stdlib.h> |
| 112 | #include <string.h> |
| 113 | #include <strings.h> |
| 114 | #include <time.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 115 | #include "resolv_private.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 116 | |
Bernie Innocenti | afaacf7 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 117 | // 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 Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | extern const char* const _res_opcodes[]; |
| 123 | extern const char* const _res_sectioncodes[]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 124 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 125 | static void do_section(const res_state statp, ns_msg* handle, ns_sect section, int pflag, |
| 126 | FILE* file) { |
| 127 | int n, sflag, rrnum; |
| 128 | int buflen = 2048; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 129 | ns_opcode opcode; |
| 130 | ns_rr rr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 131 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 132 | /* |
| 133 | * Print answer records. |
| 134 | */ |
| 135 | sflag = (int) (statp->pfcode & pflag); |
| 136 | if (statp->pfcode && !sflag) return; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 138 | char* buf = (char*) malloc((size_t) buflen); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 139 | if (buf == NULL) { |
| 140 | fprintf(file, ";; memory allocation failure\n"); |
| 141 | return; |
| 142 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 143 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 144 | opcode = (ns_opcode) ns_msg_getflag(*handle, ns_f_opcode); |
| 145 | rrnum = 0; |
| 146 | for (;;) { |
| 147 | if (ns_parserr(handle, section, rrnum, &rr)) { |
| 148 | if (errno != ENODEV) |
| 149 | fprintf(file, ";; ns_parserr: %s\n", strerror(errno)); |
| 150 | else if (rrnum > 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1)) |
| 151 | putc('\n', file); |
| 152 | goto cleanup; |
| 153 | } |
| 154 | if (rrnum == 0 && sflag != 0 && (statp->pfcode & RES_PRF_HEAD1)) |
| 155 | fprintf(file, ";; %s SECTION:\n", p_section(section, opcode)); |
| 156 | if (section == ns_s_qd) |
| 157 | fprintf(file, ";;\t%s, type = %s, class = %s\n", ns_rr_name(rr), p_type(ns_rr_type(rr)), |
| 158 | p_class(ns_rr_class(rr))); |
| 159 | else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) { |
| 160 | size_t rdatalen, ttl; |
| 161 | uint16_t optcode, optlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 162 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 163 | rdatalen = ns_rr_rdlen(rr); |
| 164 | ttl = ns_rr_ttl(rr); |
| 165 | fprintf(file, "; EDNS: version: %zu, udp=%u, flags=%04zx\n", (ttl >> 16) & 0xff, |
| 166 | ns_rr_class(rr), ttl & 0xffff); |
| 167 | while (rdatalen >= 4) { |
| 168 | const u_char* cp = ns_rr_rdata(rr); |
| 169 | int i; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 170 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 171 | GETSHORT(optcode, cp); |
| 172 | GETSHORT(optlen, cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 173 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 174 | if (optcode == NS_OPT_NSID) { |
| 175 | fputs("; NSID: ", file); |
| 176 | if (optlen == 0) { |
| 177 | fputs("; NSID\n", file); |
| 178 | } else { |
| 179 | fputs("; NSID: ", file); |
| 180 | for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]); |
| 181 | fputs(" (", file); |
| 182 | for (i = 0; i < optlen; i++) |
| 183 | fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.'); |
| 184 | fputs(")\n", file); |
| 185 | } |
| 186 | } else { |
| 187 | if (optlen == 0) { |
| 188 | fprintf(file, "; OPT=%u\n", optcode); |
| 189 | } else { |
| 190 | fprintf(file, "; OPT=%u: ", optcode); |
| 191 | for (i = 0; i < optlen; i++) fprintf(file, "%02x ", cp[i]); |
| 192 | fputs(" (", file); |
| 193 | for (i = 0; i < optlen; i++) |
| 194 | fprintf(file, "%c", isprint(cp[i]) ? cp[i] : '.'); |
| 195 | fputs(")\n", file); |
| 196 | } |
| 197 | } |
| 198 | rdatalen -= 4 + optlen; |
| 199 | } |
| 200 | } else { |
| 201 | n = ns_sprintrr(handle, &rr, NULL, NULL, buf, (u_int) buflen); |
| 202 | if (n < 0) { |
| 203 | if (errno == ENOSPC) { |
| 204 | free(buf); |
| 205 | buf = NULL; |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 206 | if (buflen < 131072) { |
| 207 | buf = (char*) malloc((size_t)(buflen += 1024)); |
| 208 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 209 | if (buf == NULL) { |
| 210 | fprintf(file, ";; memory allocation failure\n"); |
| 211 | return; |
| 212 | } |
| 213 | continue; |
| 214 | } |
| 215 | fprintf(file, ";; ns_sprintrr: %s\n", strerror(errno)); |
| 216 | goto cleanup; |
| 217 | } |
| 218 | fputs(buf, file); |
| 219 | fputc('\n', file); |
| 220 | } |
| 221 | rrnum++; |
| 222 | } |
| 223 | cleanup: |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 224 | free(buf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Print the contents of a query. |
| 229 | * This is intended to be primarily a debugging routine. |
| 230 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 231 | void res_pquery(const res_state statp, const u_char* msg, int len, FILE* file) { |
| 232 | ns_msg handle; |
| 233 | int qdcount, ancount, nscount, arcount; |
| 234 | u_int opcode, rcode, id; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 235 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 236 | if (ns_initparse(msg, len, &handle) < 0) { |
| 237 | fprintf(file, ";; ns_initparse: %s\n", strerror(errno)); |
| 238 | return; |
| 239 | } |
| 240 | opcode = ns_msg_getflag(handle, ns_f_opcode); |
| 241 | rcode = ns_msg_getflag(handle, ns_f_rcode); |
| 242 | id = ns_msg_id(handle); |
| 243 | qdcount = ns_msg_count(handle, ns_s_qd); |
| 244 | ancount = ns_msg_count(handle, ns_s_an); |
| 245 | nscount = ns_msg_count(handle, ns_s_ns); |
| 246 | arcount = ns_msg_count(handle, ns_s_ar); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 247 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 248 | /* |
| 249 | * Print header fields. |
| 250 | */ |
| 251 | if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX) || rcode) |
| 252 | fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d\n", _res_opcodes[opcode], |
| 253 | p_rcode((int) rcode), id); |
| 254 | if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEADX)) putc(';', file); |
| 255 | if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD2)) { |
| 256 | fprintf(file, "; flags:"); |
| 257 | if (ns_msg_getflag(handle, ns_f_qr)) fprintf(file, " qr"); |
| 258 | if (ns_msg_getflag(handle, ns_f_aa)) fprintf(file, " aa"); |
| 259 | if (ns_msg_getflag(handle, ns_f_tc)) fprintf(file, " tc"); |
| 260 | if (ns_msg_getflag(handle, ns_f_rd)) fprintf(file, " rd"); |
| 261 | if (ns_msg_getflag(handle, ns_f_ra)) fprintf(file, " ra"); |
| 262 | if (ns_msg_getflag(handle, ns_f_z)) fprintf(file, " ??"); |
| 263 | if (ns_msg_getflag(handle, ns_f_ad)) fprintf(file, " ad"); |
| 264 | if (ns_msg_getflag(handle, ns_f_cd)) fprintf(file, " cd"); |
| 265 | } |
| 266 | if ((!statp->pfcode) || (statp->pfcode & RES_PRF_HEAD1)) { |
| 267 | fprintf(file, "; %s: %d", p_section(ns_s_qd, (int) opcode), qdcount); |
| 268 | fprintf(file, ", %s: %d", p_section(ns_s_an, (int) opcode), ancount); |
| 269 | fprintf(file, ", %s: %d", p_section(ns_s_ns, (int) opcode), nscount); |
| 270 | fprintf(file, ", %s: %d", p_section(ns_s_ar, (int) opcode), arcount); |
| 271 | } |
| 272 | if ((!statp->pfcode) || (statp->pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { |
| 273 | putc('\n', file); |
| 274 | } |
| 275 | /* |
| 276 | * Print the various sections. |
| 277 | */ |
| 278 | do_section(statp, &handle, ns_s_qd, RES_PRF_QUES, file); |
| 279 | do_section(statp, &handle, ns_s_an, RES_PRF_ANS, file); |
| 280 | do_section(statp, &handle, ns_s_ns, RES_PRF_AUTH, file); |
| 281 | do_section(statp, &handle, ns_s_ar, RES_PRF_ADD, file); |
| 282 | if (qdcount == 0 && ancount == 0 && nscount == 0 && arcount == 0) putc('\n', file); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 285 | const u_char* p_cdnname(const u_char* cp, const u_char* msg, int len, FILE* file) { |
| 286 | char name[MAXDNAME]; |
| 287 | int n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 288 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 289 | if ((n = dn_expand(msg, msg + len, cp, name, (int) sizeof name)) < 0) return (NULL); |
| 290 | if (name[0] == '\0') |
| 291 | putc('.', file); |
| 292 | else |
| 293 | fputs(name, file); |
| 294 | return (cp + n); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 295 | } |
| 296 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 297 | const u_char* p_cdname(const u_char* cp, const u_char* msg, FILE* file) { |
| 298 | return (p_cdnname(cp, msg, PACKETSZ, file)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* Return a fully-qualified domain name from a compressed name (with |
| 302 | length supplied). */ |
| 303 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 304 | const u_char* p_fqnname(const u_char* cp, const u_char* msg, int msglen, char* name, int namelen) { |
| 305 | int n; |
| 306 | size_t newlen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 307 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 308 | if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) return (NULL); |
| 309 | newlen = strlen(name); |
| 310 | if (newlen == 0 || name[newlen - 1] != '.') { |
| 311 | if ((int) newlen + 1 >= namelen) /* Lack space for final dot */ |
| 312 | return (NULL); |
| 313 | else |
| 314 | strcpy(name + newlen, "."); |
| 315 | } |
| 316 | return (cp + n); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* XXX: the rest of these functions need to become length-limited, too. */ |
| 320 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 321 | const u_char* p_fqname(const u_char* cp, const u_char* msg, FILE* file) { |
| 322 | char name[MAXDNAME]; |
| 323 | const u_char* n; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 324 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 325 | n = p_fqnname(cp, msg, MAXCDNAME, name, (int) sizeof name); |
| 326 | if (n == NULL) return (NULL); |
| 327 | fputs(name, file); |
| 328 | return (n); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Names of RR classes and qclasses. Classes and qclasses are the same, except |
| 333 | * that C_ANY is a qclass but not a class. (You can ask for records of class |
| 334 | * C_ANY, but you can't have any records of that class in the database.) |
| 335 | */ |
| 336 | const struct res_sym __p_class_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 337 | {C_IN, "IN", (char*) 0}, {C_CHAOS, "CH", (char*) 0}, {C_CHAOS, "CHAOS", (char*) 0}, |
| 338 | {C_HS, "HS", (char*) 0}, {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0}, |
| 339 | {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Names of message sections. |
| 343 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 344 | static const struct res_sym __p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0}, |
| 345 | {ns_s_an, "ANSWER", (char*) 0}, |
| 346 | {ns_s_ns, "AUTHORITY", (char*) 0}, |
| 347 | {ns_s_ar, "ADDITIONAL", (char*) 0}, |
| 348 | {0, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 349 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 350 | static const struct res_sym __p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0}, |
| 351 | {S_PREREQ, "PREREQUISITE", (char*) 0}, |
| 352 | {S_UPDATE, "UPDATE", (char*) 0}, |
| 353 | {S_ADDT, "ADDITIONAL", (char*) 0}, |
| 354 | {0, (char*) 0, (char*) 0}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 355 | |
| 356 | const struct res_sym __p_key_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 357 | {NS_ALG_MD5RSA, "RSA", "RSA KEY with MD5 hash"}, |
| 358 | {NS_ALG_DH, "DH", "Diffie Hellman"}, |
| 359 | {NS_ALG_DSA, "DSA", "Digital Signature Algorithm"}, |
| 360 | {NS_ALG_EXPIRE_ONLY, "EXPIREONLY", "No algorithm"}, |
| 361 | {NS_ALG_PRIVATE_OID, "PRIVATE", "Algorithm obtained from OID"}, |
| 362 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 363 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 364 | const struct res_sym __p_cert_syms[] = {{cert_t_pkix, "PKIX", "PKIX (X.509v3) Certificate"}, |
| 365 | {cert_t_spki, "SPKI", "SPKI certificate"}, |
| 366 | {cert_t_pgp, "PGP", "PGP certificate"}, |
| 367 | {cert_t_url, "URL", "URL Private"}, |
| 368 | {cert_t_oid, "OID", "OID Private"}, |
| 369 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Names of RR types and qtypes. Types and qtypes are the same, except |
| 373 | * that T_ANY is a qtype but not a type. (You can ask for records of type |
| 374 | * T_ANY, but you can't have any records of that type in the database.) |
| 375 | */ |
| 376 | const struct res_sym __p_type_syms[] = { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 377 | {ns_t_a, "A", "address"}, |
| 378 | {ns_t_ns, "NS", "name server"}, |
| 379 | {ns_t_md, "MD", "mail destination (deprecated)"}, |
| 380 | {ns_t_mf, "MF", "mail forwarder (deprecated)"}, |
| 381 | {ns_t_cname, "CNAME", "canonical name"}, |
| 382 | {ns_t_soa, "SOA", "start of authority"}, |
| 383 | {ns_t_mb, "MB", "mailbox"}, |
| 384 | {ns_t_mg, "MG", "mail group member"}, |
| 385 | {ns_t_mr, "MR", "mail rename"}, |
| 386 | {ns_t_null, "NULL", "null"}, |
| 387 | {ns_t_wks, "WKS", "well-known service (deprecated)"}, |
| 388 | {ns_t_ptr, "PTR", "domain name pointer"}, |
| 389 | {ns_t_hinfo, "HINFO", "host information"}, |
| 390 | {ns_t_minfo, "MINFO", "mailbox information"}, |
| 391 | {ns_t_mx, "MX", "mail exchanger"}, |
| 392 | {ns_t_txt, "TXT", "text"}, |
| 393 | {ns_t_rp, "RP", "responsible person"}, |
| 394 | {ns_t_afsdb, "AFSDB", "DCE or AFS server"}, |
| 395 | {ns_t_x25, "X25", "X25 address"}, |
| 396 | {ns_t_isdn, "ISDN", "ISDN address"}, |
| 397 | {ns_t_rt, "RT", "router"}, |
| 398 | {ns_t_nsap, "NSAP", "nsap address"}, |
| 399 | {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"}, |
| 400 | {ns_t_sig, "SIG", "signature"}, |
| 401 | {ns_t_key, "KEY", "key"}, |
| 402 | {ns_t_px, "PX", "mapping information"}, |
| 403 | {ns_t_gpos, "GPOS", "geographical position (withdrawn)"}, |
| 404 | {ns_t_aaaa, "AAAA", "IPv6 address"}, |
| 405 | {ns_t_loc, "LOC", "location"}, |
| 406 | {ns_t_nxt, "NXT", "next valid name (unimplemented)"}, |
| 407 | {ns_t_eid, "EID", "endpoint identifier (unimplemented)"}, |
| 408 | {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"}, |
| 409 | {ns_t_srv, "SRV", "server selection"}, |
| 410 | {ns_t_atma, "ATMA", "ATM address (unimplemented)"}, |
| 411 | {ns_t_naptr, "NAPTR", "naptr"}, |
| 412 | {ns_t_kx, "KX", "key exchange"}, |
| 413 | {ns_t_cert, "CERT", "certificate"}, |
| 414 | {ns_t_a6, "A", "IPv6 address (experminental)"}, |
| 415 | {ns_t_dname, "DNAME", "non-terminal redirection"}, |
| 416 | {ns_t_opt, "OPT", "opt"}, |
| 417 | {ns_t_apl, "apl", "apl"}, |
| 418 | {ns_t_ds, "DS", "delegation signer"}, |
| 419 | {ns_t_sshfp, "SSFP", "SSH fingerprint"}, |
| 420 | {ns_t_ipseckey, "IPSECKEY", "IPSEC key"}, |
| 421 | {ns_t_rrsig, "RRSIG", "rrsig"}, |
| 422 | {ns_t_nsec, "NSEC", "nsec"}, |
| 423 | {ns_t_dnskey, "DNSKEY", "DNS key"}, |
| 424 | {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"}, |
| 425 | {ns_t_nsec3, "NSEC3", "nsec3"}, |
| 426 | {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"}, |
| 427 | {ns_t_hip, "HIP", "host identity protocol"}, |
| 428 | {ns_t_spf, "SPF", "sender policy framework"}, |
| 429 | {ns_t_tkey, "TKEY", "tkey"}, |
| 430 | {ns_t_tsig, "TSIG", "transaction signature"}, |
| 431 | {ns_t_ixfr, "IXFR", "incremental zone transfer"}, |
| 432 | {ns_t_axfr, "AXFR", "zone transfer"}, |
| 433 | {ns_t_zxfr, "ZXFR", "compressed zone transfer"}, |
| 434 | {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"}, |
| 435 | {ns_t_maila, "MAILA", "mail agent (deprecated)"}, |
| 436 | {ns_t_naptr, "NAPTR", "URN Naming Authority"}, |
| 437 | {ns_t_kx, "KX", "Key Exchange"}, |
| 438 | {ns_t_cert, "CERT", "Certificate"}, |
| 439 | {ns_t_a6, "A6", "IPv6 Address"}, |
| 440 | {ns_t_dname, "DNAME", "dname"}, |
| 441 | {ns_t_sink, "SINK", "Kitchen Sink (experimental)"}, |
| 442 | {ns_t_opt, "OPT", "EDNS Options"}, |
| 443 | {ns_t_any, "ANY", "\"any\""}, |
| 444 | {ns_t_dlv, "DLV", "DNSSEC look-aside validation"}, |
| 445 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * Names of DNS rcodes. |
| 449 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 450 | const struct res_sym __p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"}, |
| 451 | {ns_r_formerr, "FORMERR", "format error"}, |
| 452 | {ns_r_servfail, "SERVFAIL", "server failed"}, |
| 453 | {ns_r_nxdomain, "NXDOMAIN", "no such domain name"}, |
| 454 | {ns_r_notimpl, "NOTIMP", "not implemented"}, |
| 455 | {ns_r_refused, "REFUSED", "refused"}, |
| 456 | {ns_r_yxdomain, "YXDOMAIN", "domain name exists"}, |
| 457 | {ns_r_yxrrset, "YXRRSET", "rrset exists"}, |
| 458 | {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"}, |
| 459 | {ns_r_notauth, "NOTAUTH", "not authoritative"}, |
| 460 | {ns_r_notzone, "NOTZONE", "Not in zone"}, |
| 461 | {ns_r_max, "", ""}, |
| 462 | {ns_r_badsig, "BADSIG", "bad signature"}, |
| 463 | {ns_r_badkey, "BADKEY", "bad key"}, |
| 464 | {ns_r_badtime, "BADTIME", "bad time"}, |
| 465 | {0, NULL, NULL}}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 466 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 467 | int sym_ston(const struct res_sym* syms, const char* name, int* success) { |
| 468 | for (; syms->name != 0; syms++) { |
| 469 | if (strcasecmp(name, syms->name) == 0) { |
| 470 | if (success) *success = 1; |
| 471 | return (syms->number); |
| 472 | } |
| 473 | } |
| 474 | if (success) *success = 0; |
| 475 | return (syms->number); /* The default value. */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 476 | } |
| 477 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 478 | const char* sym_ntos(const struct res_sym* syms, int number, int* success) { |
| 479 | static char unname[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 480 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 481 | for (; syms->name != 0; syms++) { |
| 482 | if (number == syms->number) { |
| 483 | if (success) *success = 1; |
| 484 | return (syms->name); |
| 485 | } |
| 486 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 487 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 488 | snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */ |
| 489 | if (success) *success = 0; |
| 490 | return (unname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 491 | } |
| 492 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 493 | const char* sym_ntop(const struct res_sym* syms, int number, int* success) { |
| 494 | static char unname[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 495 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 496 | for (; syms->name != 0; syms++) { |
| 497 | if (number == syms->number) { |
| 498 | if (success) *success = 1; |
| 499 | return (syms->humanname); |
| 500 | } |
| 501 | } |
| 502 | snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */ |
| 503 | if (success) *success = 0; |
| 504 | return (unname); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Return a string for the type. |
| 509 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 510 | const char* p_type(int type) { |
| 511 | int success; |
| 512 | const char* result; |
| 513 | static char typebuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 514 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 515 | result = sym_ntos(__p_type_syms, type, &success); |
| 516 | if (success) return (result); |
| 517 | if (type < 0 || type > 0xffff) return ("BADTYPE"); |
| 518 | snprintf(typebuf, sizeof(typebuf), "TYPE%d", type); |
| 519 | return (typebuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Return a string for the type. |
| 524 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 525 | const char* p_section(int section, int opcode) { |
| 526 | const struct res_sym* symbols; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 527 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 528 | switch (opcode) { |
| 529 | case ns_o_update: |
| 530 | symbols = __p_update_section_syms; |
| 531 | break; |
| 532 | default: |
| 533 | symbols = __p_default_section_syms; |
| 534 | break; |
| 535 | } |
| 536 | return (sym_ntos(symbols, section, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Return a mnemonic for class. |
| 541 | */ |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 542 | const char* p_class(int cl) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 543 | int success; |
| 544 | const char* result; |
| 545 | static char classbuf[20]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 546 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 547 | result = sym_ntos(__p_class_syms, cl, &success); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 548 | if (success) return (result); |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 549 | if (cl < 0 || cl > 0xffff) return ("BADCLASS"); |
| 550 | snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 551 | return (classbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Return a mnemonic for an option |
| 556 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 557 | const char* p_option(u_long option) { |
| 558 | static char nbuf[40]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 559 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 560 | switch (option) { |
| 561 | case RES_INIT: |
| 562 | return "init"; |
| 563 | case RES_DEBUG: |
| 564 | return "debug"; |
| 565 | case RES_AAONLY: |
| 566 | return "aaonly(unimpl)"; |
| 567 | case RES_USEVC: |
| 568 | return "usevc"; |
| 569 | case RES_PRIMARY: |
| 570 | return "primry(unimpl)"; |
| 571 | case RES_IGNTC: |
| 572 | return "igntc"; |
| 573 | case RES_RECURSE: |
| 574 | return "recurs"; |
| 575 | case RES_DEFNAMES: |
| 576 | return "defnam"; |
| 577 | case RES_STAYOPEN: |
| 578 | return "styopn"; |
| 579 | case RES_DNSRCH: |
| 580 | return "dnsrch"; |
| 581 | case RES_INSECURE1: |
| 582 | return "insecure1"; |
| 583 | case RES_INSECURE2: |
| 584 | return "insecure2"; |
| 585 | case RES_NOALIASES: |
| 586 | return "noaliases"; |
| 587 | case RES_USE_INET6: |
| 588 | return "inet6"; |
| 589 | #ifdef RES_USE_EDNS0 /* KAME extension */ |
| 590 | case RES_USE_EDNS0: |
| 591 | return "edns0"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 592 | #endif |
| 593 | #ifdef RES_USE_DNAME |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 594 | case RES_USE_DNAME: |
| 595 | return "dname"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 596 | #endif |
| 597 | #ifdef RES_USE_DNSSEC |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 598 | case RES_USE_DNSSEC: |
| 599 | return "dnssec"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 600 | #endif |
| 601 | #ifdef RES_NOTLDQUERY |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 602 | case RES_NOTLDQUERY: |
| 603 | return "no-tld-query"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 604 | #endif |
| 605 | #ifdef RES_NO_NIBBLE2 |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 606 | case RES_NO_NIBBLE2: |
| 607 | return "no-nibble2"; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 608 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 609 | /* XXX nonreentrant */ |
| 610 | default: |
| 611 | snprintf(nbuf, sizeof(nbuf), "?0x%lx?", (u_long) option); |
| 612 | return (nbuf); |
| 613 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Return a mnemonic for a time to live. |
| 618 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 619 | const char* p_time(u_int32_t value) { |
| 620 | static char nbuf[40]; /* XXX nonreentrant */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 621 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 622 | if (ns_format_ttl((u_long) value, nbuf, sizeof nbuf) < 0) |
| 623 | snprintf(nbuf, sizeof(nbuf), "%u", value); |
| 624 | return (nbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Return a string for the rcode. |
| 629 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 630 | const char* p_rcode(int rcode) { |
| 631 | return (sym_ntos(__p_rcode_syms, rcode, (int*) 0)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Return a string for a res_sockaddr_union. |
| 636 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 637 | const char* p_sockun(union res_sockaddr_union u, char* buf, size_t size) { |
| 638 | char ret[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:123.123.123.123"]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 639 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 640 | switch (u.sin.sin_family) { |
| 641 | case AF_INET: |
| 642 | inet_ntop(AF_INET, &u.sin.sin_addr, ret, (socklen_t) sizeof ret); |
| 643 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 644 | case AF_INET6: |
| 645 | inet_ntop(AF_INET6, &u.sin6.sin6_addr, ret, sizeof ret); |
| 646 | break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 647 | default: |
| 648 | snprintf(ret, sizeof(ret), "[af%d]", u.sin.sin_family); |
| 649 | break; |
| 650 | } |
| 651 | if (size > 0U) { |
| 652 | strncpy(buf, ret, size - 1); |
| 653 | buf[size - 1] = '0'; |
| 654 | } |
| 655 | return (buf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
| 659 | * routines to convert between on-the-wire RR format and zone file format. |
| 660 | * Does not contain conversion to/from decimal degrees; divide or multiply |
| 661 | * by 60*60*1000 for that. |
| 662 | */ |
| 663 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 664 | static const unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, |
| 665 | 100000, 1000000, 10000000, 100000000, 1000000000}; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 666 | |
| 667 | /* takes an XeY precision/size value, returns a string representation. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 668 | static const char* precsize_ntoa(u_int32_t prec) { |
| 669 | static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */ |
| 670 | unsigned long val; |
| 671 | int mantissa, exponent; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 672 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 673 | mantissa = (int) ((prec >> 4) & 0x0f) % 10; |
| 674 | exponent = (int) ((prec >> 0) & 0x0f) % 10; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 675 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 676 | val = mantissa * poweroften[exponent]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 677 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 678 | (void) snprintf(retbuf, sizeof(retbuf), "%lu.%.2lu", val / 100, val % 100); |
| 679 | return (retbuf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 683 | static u_int8_t precsize_aton(const char** strptr) { |
| 684 | unsigned int mval = 0, cmval = 0; |
| 685 | u_int8_t retval = 0; |
| 686 | const char* cp; |
| 687 | int exponent; |
| 688 | int mantissa; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 689 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 690 | cp = *strptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 691 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 692 | while (isdigit((unsigned char) *cp)) mval = mval * 10 + (*cp++ - '0'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 693 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 694 | if (*cp == '.') { /* centimeters */ |
| 695 | cp++; |
| 696 | if (isdigit((unsigned char) *cp)) { |
| 697 | cmval = (*cp++ - '0') * 10; |
| 698 | if (isdigit((unsigned char) *cp)) { |
| 699 | cmval += (*cp++ - '0'); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | cmval = (mval * 100) + cmval; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 704 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 705 | for (exponent = 0; exponent < 9; exponent++) |
| 706 | if (cmval < poweroften[exponent + 1]) break; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 707 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 708 | mantissa = cmval / poweroften[exponent]; |
| 709 | if (mantissa > 9) mantissa = 9; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 710 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 711 | retval = (mantissa << 4) | exponent; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 712 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 713 | *strptr = cp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 714 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 715 | return (retval); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 719 | static u_int32_t latlon2ul(const char** latlonstrptr, int* which) { |
| 720 | const char* cp; |
| 721 | u_int32_t retval; |
| 722 | int deg = 0, min = 0, secs = 0, secsfrac = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 723 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 724 | cp = *latlonstrptr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 725 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 726 | while (isdigit((unsigned char) *cp)) deg = deg * 10 + (*cp++ - '0'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 727 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 728 | while (isspace((unsigned char) *cp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 729 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 730 | if (!(isdigit((unsigned char) *cp))) goto fndhemi; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 731 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 732 | while (isdigit((unsigned char) *cp)) min = min * 10 + (*cp++ - '0'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 733 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 734 | while (isspace((unsigned char) *cp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 735 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 736 | if (!(isdigit((unsigned char) *cp))) goto fndhemi; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 737 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 738 | while (isdigit((unsigned char) *cp)) secs = secs * 10 + (*cp++ - '0'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 739 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 740 | if (*cp == '.') { /* decimal seconds */ |
| 741 | cp++; |
| 742 | if (isdigit((unsigned char) *cp)) { |
| 743 | secsfrac = (*cp++ - '0') * 100; |
| 744 | if (isdigit((unsigned char) *cp)) { |
| 745 | secsfrac += (*cp++ - '0') * 10; |
| 746 | if (isdigit((unsigned char) *cp)) { |
| 747 | secsfrac += (*cp++ - '0'); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 752 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 753 | while (!isspace((unsigned char) *cp)) /* if any trailing garbage */ |
| 754 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 755 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 756 | while (isspace((unsigned char) *cp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 757 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 758 | fndhemi: |
| 759 | switch (*cp) { |
| 760 | case 'N': |
| 761 | case 'n': |
| 762 | case 'E': |
| 763 | case 'e': |
| 764 | retval = ((unsigned) 1 << 31) + (((((deg * 60) + min) * 60) + secs) * 1000) + secsfrac; |
| 765 | break; |
| 766 | case 'S': |
| 767 | case 's': |
| 768 | case 'W': |
| 769 | case 'w': |
| 770 | retval = ((unsigned) 1 << 31) - (((((deg * 60) + min) * 60) + secs) * 1000) - secsfrac; |
| 771 | break; |
| 772 | default: |
| 773 | retval = 0; /* invalid value -- indicates error */ |
| 774 | break; |
| 775 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 776 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 777 | switch (*cp) { |
| 778 | case 'N': |
| 779 | case 'n': |
| 780 | case 'S': |
| 781 | case 's': |
| 782 | *which = 1; /* latitude */ |
| 783 | break; |
| 784 | case 'E': |
| 785 | case 'e': |
| 786 | case 'W': |
| 787 | case 'w': |
| 788 | *which = 2; /* longitude */ |
| 789 | break; |
| 790 | default: |
| 791 | *which = 0; /* error */ |
| 792 | break; |
| 793 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 794 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 795 | cp++; /* skip the hemisphere */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 796 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 797 | while (!isspace((unsigned char) *cp)) /* if any trailing garbage */ |
| 798 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 799 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 800 | while (isspace((unsigned char) *cp)) /* move to next field */ |
| 801 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 802 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 803 | *latlonstrptr = cp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 804 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 805 | return (retval); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* converts a zone file representation in a string to an RDATA on-the-wire |
| 809 | * representation. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 810 | int loc_aton(const char* ascii, u_char* binary) { |
| 811 | const char *cp, *maxcp; |
| 812 | u_char* bcp; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 813 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 814 | u_int32_t latit = 0, longit = 0, alt = 0; |
| 815 | u_int32_t lltemp1 = 0, lltemp2 = 0; |
| 816 | int altmeters = 0, altfrac = 0, altsign = 1; |
| 817 | u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */ |
| 818 | u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */ |
| 819 | u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */ |
| 820 | int which1 = 0, which2 = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 821 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 822 | cp = ascii; |
| 823 | maxcp = cp + strlen(ascii); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 824 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 825 | lltemp1 = latlon2ul(&cp, &which1); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 826 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 827 | lltemp2 = latlon2ul(&cp, &which2); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 828 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 829 | switch (which1 + which2) { |
| 830 | case 3: /* 1 + 2, the only valid combination */ |
| 831 | if ((which1 == 1) && (which2 == 2)) { /* normal case */ |
| 832 | latit = lltemp1; |
| 833 | longit = lltemp2; |
| 834 | } else if ((which1 == 2) && (which2 == 1)) { /* reversed */ |
| 835 | longit = lltemp1; |
| 836 | latit = lltemp2; |
| 837 | } else { /* some kind of brokenness */ |
| 838 | return (0); |
| 839 | } |
| 840 | break; |
| 841 | default: /* we didn't get one of each */ |
| 842 | return (0); |
| 843 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 844 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | /* altitude */ |
| 846 | if (*cp == '-') { |
| 847 | altsign = -1; |
| 848 | cp++; |
| 849 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 850 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 851 | if (*cp == '+') cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 852 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 853 | while (isdigit((unsigned char) *cp)) altmeters = altmeters * 10 + (*cp++ - '0'); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 854 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 855 | if (*cp == '.') { /* decimal meters */ |
| 856 | cp++; |
| 857 | if (isdigit((unsigned char) *cp)) { |
| 858 | altfrac = (*cp++ - '0') * 10; |
| 859 | if (isdigit((unsigned char) *cp)) { |
| 860 | altfrac += (*cp++ - '0'); |
| 861 | } |
| 862 | } |
| 863 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 864 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 865 | alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 866 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 867 | while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */ |
| 868 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 869 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 870 | while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 871 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 872 | if (cp >= maxcp) goto defaults; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 873 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 874 | siz = precsize_aton(&cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 875 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 876 | while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */ |
| 877 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 878 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 879 | while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 880 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 881 | if (cp >= maxcp) goto defaults; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 882 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 883 | hp = precsize_aton(&cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 884 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 885 | while (!isspace((unsigned char) *cp) && (cp < maxcp)) /* if trailing garbage or m */ |
| 886 | cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 887 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 888 | while (isspace((unsigned char) *cp) && (cp < maxcp)) cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 889 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 890 | if (cp >= maxcp) goto defaults; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 891 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 892 | vp = precsize_aton(&cp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 893 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 894 | defaults: |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 895 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 896 | bcp = binary; |
| 897 | *bcp++ = (u_int8_t) 0; /* version byte */ |
| 898 | *bcp++ = siz; |
| 899 | *bcp++ = hp; |
| 900 | *bcp++ = vp; |
| 901 | PUTLONG(latit, bcp); |
| 902 | PUTLONG(longit, bcp); |
| 903 | PUTLONG(alt, bcp); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 904 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 905 | return (16); /* size of RR in octets */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* takes an on-the-wire LOC RR and formats it in a human readable format. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 909 | const char* loc_ntoa(const u_char* binary, char* ascii, size_t bufsiz) { |
| 910 | static const char* error = "?"; |
| 911 | static char tmpbuf[sizeof |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 912 | "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 913 | const u_char* cp = binary; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 914 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 915 | int latdeg, latmin, latsec, latsecfrac; |
| 916 | int longdeg, longmin, longsec, longsecfrac; |
| 917 | char northsouth, eastwest; |
| 918 | const char* altsign; |
| 919 | int altmeters, altfrac; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 920 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 921 | const u_int32_t referencealt = 100000 * 100; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 922 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 923 | int32_t latval, longval, altval; |
| 924 | u_int32_t templ; |
| 925 | u_int8_t sizeval, hpval, vpval, versionval; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 926 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 927 | char *sizestr, *hpstr, *vpstr; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 928 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 929 | versionval = *cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 930 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 931 | if (ascii == NULL) ascii = tmpbuf; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 932 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 933 | if (versionval) { |
| 934 | (void) snprintf(ascii, bufsiz, "; error: unknown LOC RR version"); |
| 935 | return (ascii); |
| 936 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 937 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 938 | sizeval = *cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 939 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 940 | hpval = *cp++; |
| 941 | vpval = *cp++; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 942 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 943 | GETLONG(templ, cp); |
| 944 | latval = (templ - ((unsigned) 1 << 31)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 945 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 946 | GETLONG(templ, cp); |
| 947 | longval = (templ - ((unsigned) 1 << 31)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 948 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 949 | GETLONG(templ, cp); |
| 950 | if (templ < referencealt) { /* below WGS 84 spheroid */ |
| 951 | altval = referencealt - templ; |
| 952 | altsign = "-"; |
| 953 | } else { |
| 954 | altval = templ - referencealt; |
| 955 | altsign = ""; |
| 956 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 957 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 958 | if (latval < 0) { |
| 959 | northsouth = 'S'; |
| 960 | latval = -latval; |
| 961 | } else |
| 962 | northsouth = 'N'; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 963 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 964 | latsecfrac = latval % 1000; |
| 965 | latval = latval / 1000; |
| 966 | latsec = latval % 60; |
| 967 | latval = latval / 60; |
| 968 | latmin = latval % 60; |
| 969 | latval = latval / 60; |
| 970 | latdeg = latval; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 971 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 972 | if (longval < 0) { |
| 973 | eastwest = 'W'; |
| 974 | longval = -longval; |
| 975 | } else |
| 976 | eastwest = 'E'; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 977 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 978 | longsecfrac = longval % 1000; |
| 979 | longval = longval / 1000; |
| 980 | longsec = longval % 60; |
| 981 | longval = longval / 60; |
| 982 | longmin = longval % 60; |
| 983 | longval = longval / 60; |
| 984 | longdeg = longval; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 985 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 986 | altfrac = altval % 100; |
| 987 | altmeters = (altval / 100); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 988 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 989 | sizestr = strdup(precsize_ntoa((u_int32_t) sizeval)); |
| 990 | hpstr = strdup(precsize_ntoa((u_int32_t) hpval)); |
| 991 | vpstr = strdup(precsize_ntoa((u_int32_t) vpval)); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 992 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 993 | snprintf(ascii, bufsiz, "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %s%d.%.2dm %sm %sm %sm", |
| 994 | latdeg, latmin, latsec, latsecfrac, northsouth, longdeg, longmin, longsec, longsecfrac, |
| 995 | eastwest, altsign, altmeters, altfrac, (sizestr != NULL) ? sizestr : error, |
| 996 | (hpstr != NULL) ? hpstr : error, (vpstr != NULL) ? vpstr : error); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 997 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame^] | 998 | free(sizestr); |
| 999 | free(hpstr); |
| 1000 | free(vpstr); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1001 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1002 | return (ascii); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1003 | } |
| 1004 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1005 | /* Return the number of DNS hierarchy levels in the name. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1006 | int dn_count_labels(const char* name) { |
| 1007 | size_t len, i, count; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1008 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1009 | len = strlen(name); |
| 1010 | for (i = 0, count = 0; i < len; i++) { |
| 1011 | /* XXX need to check for \. or use named's nlabels(). */ |
| 1012 | if (name[i] == '.') count++; |
| 1013 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1014 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1015 | /* don't count initial wildcard */ |
| 1016 | if (name[0] == '*') |
| 1017 | if (count) count--; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1018 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1019 | /* don't count the null label for root. */ |
| 1020 | /* if terminating '.' not found, must adjust */ |
| 1021 | /* count to include last label */ |
| 1022 | if (len > 0 && name[len - 1] != '.') count++; |
| 1023 | _DIAGASSERT(__type_fit(int, count)); |
| 1024 | return (int) count; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1025 | } |
| 1026 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1027 | /* |
| 1028 | * Make dates expressed in seconds-since-Jan-1-1970 easy to read. |
| 1029 | * SIG records are required to be printed like this, by the Secure DNS RFC. |
| 1030 | */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1031 | char* p_secstodate(u_long secs) { |
| 1032 | /* XXX nonreentrant */ |
| 1033 | static char output[15]; /* YYYYMMDDHHMMSS and null */ |
| 1034 | time_t myclock = secs; |
| 1035 | struct tm* mytime; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1036 | #ifdef HAVE_TIME_R |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1037 | struct tm res; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1038 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1039 | mytime = gmtime_r(&myclock, &res); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1040 | #else |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1041 | mytime = gmtime(&myclock); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1042 | #endif |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1043 | mytime->tm_year += 1900; |
| 1044 | mytime->tm_mon += 1; |
| 1045 | snprintf(output, sizeof(output), "%04d%02d%02d%02d%02d%02d", mytime->tm_year, mytime->tm_mon, |
| 1046 | mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
| 1047 | return (output); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1048 | } |
| 1049 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1050 | u_int16_t res_nametoclass(const char* buf, int* successp) { |
| 1051 | unsigned long result; |
| 1052 | char* endptr; |
| 1053 | int success; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1054 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1055 | result = sym_ston(__p_class_syms, buf, &success); |
| 1056 | if (success) goto done; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1057 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1058 | if (strncasecmp(buf, "CLASS", 5) != 0 || !isdigit((unsigned char) buf[5])) goto done; |
| 1059 | errno = 0; |
| 1060 | result = strtoul(buf + 5, &endptr, 10); |
| 1061 | if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1; |
| 1062 | done: |
| 1063 | if (successp) *successp = success; |
| 1064 | return (u_int16_t)(result); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1065 | } |
| 1066 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1067 | u_int16_t res_nametotype(const char* buf, int* successp) { |
| 1068 | unsigned long result; |
| 1069 | char* endptr; |
| 1070 | int success; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1071 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1072 | result = sym_ston(__p_type_syms, buf, &success); |
| 1073 | if (success) goto done; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1074 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1075 | if (strncasecmp(buf, "type", 4) != 0 || !isdigit((unsigned char) buf[4])) goto done; |
| 1076 | errno = 0; |
| 1077 | result = strtoul(buf + 4, &endptr, 10); |
| 1078 | if (errno == 0 && *endptr == '\0' && result <= 0xffffU) success = 1; |
| 1079 | done: |
| 1080 | if (successp) *successp = success; |
| 1081 | return (u_int16_t)(result); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1082 | } |