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