1 /* 2 * wire2str.c 3 * 4 * conversion routines from the wire format 5 * to the presentation format (strings) 6 * 7 * (c) NLnet Labs, 2004-2006 8 * 9 * See the file LICENSE for the license 10 */ 11 /** 12 * \file 13 * 14 * Contains functions to translate the wireformat to text 15 * representation, as well as functions to print them. 16 */ 17 #include "config.h" 18 #include "sldns/wire2str.h" 19 #include "sldns/str2wire.h" 20 #include "sldns/rrdef.h" 21 #include "sldns/pkthdr.h" 22 #include "sldns/parseutil.h" 23 #include "sldns/sbuffer.h" 24 #include "sldns/keyraw.h" 25 #include "util/data/dname.h" 26 #ifdef HAVE_TIME_H 27 #include <time.h> 28 #endif 29 #include <sys/time.h> 30 #include <stdarg.h> 31 #include <ctype.h> 32 #ifdef HAVE_NETDB_H 33 #include <netdb.h> 34 #endif 35 36 /* lookup tables for standard DNS stuff */ 37 /* Taken from RFC 2535, section 7. */ 38 static sldns_lookup_table sldns_algorithms_data[] = { 39 { LDNS_RSAMD5, "RSAMD5" }, 40 { LDNS_DH, "DH" }, 41 { LDNS_DSA, "DSA" }, 42 { LDNS_ECC, "ECC" }, 43 { LDNS_RSASHA1, "RSASHA1" }, 44 { LDNS_DSA_NSEC3, "DSA-NSEC3-SHA1" }, 45 { LDNS_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" }, 46 { LDNS_RSASHA256, "RSASHA256"}, 47 { LDNS_RSASHA512, "RSASHA512"}, 48 { LDNS_ECC_GOST, "ECC-GOST"}, 49 { LDNS_ECDSAP256SHA256, "ECDSAP256SHA256"}, 50 { LDNS_ECDSAP384SHA384, "ECDSAP384SHA384"}, 51 { LDNS_ED25519, "ED25519"}, 52 { LDNS_ED448, "ED448"}, 53 { LDNS_INDIRECT, "INDIRECT" }, 54 { LDNS_PRIVATEDNS, "PRIVATEDNS" }, 55 { LDNS_PRIVATEOID, "PRIVATEOID" }, 56 { 0, NULL } 57 }; 58 sldns_lookup_table* sldns_algorithms = sldns_algorithms_data; 59 60 /* hash algorithms in DS record */ 61 static sldns_lookup_table sldns_hashes_data[] = { 62 { LDNS_SHA1, "SHA1" }, 63 { LDNS_SHA256, "SHA256" }, 64 { LDNS_HASH_GOST, "HASH-GOST" }, 65 { LDNS_SHA384, "SHA384" }, 66 { 0, NULL } 67 }; 68 sldns_lookup_table* sldns_hashes = sldns_hashes_data; 69 70 /* Taken from RFC 4398 */ 71 static sldns_lookup_table sldns_cert_algorithms_data[] = { 72 { LDNS_CERT_PKIX, "PKIX" }, 73 { LDNS_CERT_SPKI, "SPKI" }, 74 { LDNS_CERT_PGP, "PGP" }, 75 { LDNS_CERT_IPKIX, "IPKIX" }, 76 { LDNS_CERT_ISPKI, "ISPKI" }, 77 { LDNS_CERT_IPGP, "IPGP" }, 78 { LDNS_CERT_ACPKIX, "ACPKIX" }, 79 { LDNS_CERT_IACPKIX, "IACPKIX" }, 80 { LDNS_CERT_URI, "URI" }, 81 { LDNS_CERT_OID, "OID" }, 82 { 0, NULL } 83 }; 84 sldns_lookup_table* sldns_cert_algorithms = sldns_cert_algorithms_data; 85 86 /* if these are used elsewhere */ 87 static sldns_lookup_table sldns_rcodes_data[] = { 88 { LDNS_RCODE_NOERROR, "NOERROR" }, 89 { LDNS_RCODE_FORMERR, "FORMERR" }, 90 { LDNS_RCODE_SERVFAIL, "SERVFAIL" }, 91 { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" }, 92 { LDNS_RCODE_NOTIMPL, "NOTIMPL" }, 93 { LDNS_RCODE_REFUSED, "REFUSED" }, 94 { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" }, 95 { LDNS_RCODE_YXRRSET, "YXRRSET" }, 96 { LDNS_RCODE_NXRRSET, "NXRRSET" }, 97 { LDNS_RCODE_NOTAUTH, "NOTAUTH" }, 98 { LDNS_RCODE_NOTZONE, "NOTZONE" }, 99 { 0, NULL } 100 }; 101 sldns_lookup_table* sldns_rcodes = sldns_rcodes_data; 102 103 static sldns_lookup_table sldns_opcodes_data[] = { 104 { LDNS_PACKET_QUERY, "QUERY" }, 105 { LDNS_PACKET_IQUERY, "IQUERY" }, 106 { LDNS_PACKET_STATUS, "STATUS" }, 107 { LDNS_PACKET_NOTIFY, "NOTIFY" }, 108 { LDNS_PACKET_UPDATE, "UPDATE" }, 109 { 0, NULL } 110 }; 111 sldns_lookup_table* sldns_opcodes = sldns_opcodes_data; 112 113 static sldns_lookup_table sldns_wireparse_errors_data[] = { 114 { LDNS_WIREPARSE_ERR_OK, "no parse error" }, 115 { LDNS_WIREPARSE_ERR_GENERAL, "parse error" }, 116 { LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, "Domainname length overflow" }, 117 { LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, "Domainname length underflow (zero length)" }, 118 { LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, "buffer too small" }, 119 { LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, "Label length overflow" }, 120 { LDNS_WIREPARSE_ERR_EMPTY_LABEL, "Empty label" }, 121 { LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, "Syntax error, bad escape sequence" }, 122 { LDNS_WIREPARSE_ERR_SYNTAX, "Syntax error, could not parse the RR" }, 123 { LDNS_WIREPARSE_ERR_SYNTAX_TTL, "Syntax error, could not parse the RR's TTL" }, 124 { LDNS_WIREPARSE_ERR_SYNTAX_TYPE, "Syntax error, could not parse the RR's type" }, 125 { LDNS_WIREPARSE_ERR_SYNTAX_CLASS, "Syntax error, could not parse the RR's class" }, 126 { LDNS_WIREPARSE_ERR_SYNTAX_RDATA, "Syntax error, could not parse the RR's rdata" }, 127 { LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE, "Syntax error, value expected" }, 128 { LDNS_WIREPARSE_ERR_INVALID_STR, "Conversion error, string expected" }, 129 { LDNS_WIREPARSE_ERR_SYNTAX_B64, "Conversion error, b64 encoding expected" }, 130 { LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT, "Conversion error, b32 ext encoding expected" }, 131 { LDNS_WIREPARSE_ERR_SYNTAX_HEX, "Conversion error, hex encoding expected" }, 132 { LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM, "Bad algorithm type for CERT record" }, 133 { LDNS_WIREPARSE_ERR_SYNTAX_TIME, "Conversion error, time encoding expected" }, 134 { LDNS_WIREPARSE_ERR_SYNTAX_PERIOD, "Conversion error, time period encoding expected" }, 135 { LDNS_WIREPARSE_ERR_SYNTAX_ILNP64, "Conversion error, 4 colon separated hex numbers expected" }, 136 { LDNS_WIREPARSE_ERR_SYNTAX_EUI48, 137 "Conversion error, 6 two character hex numbers " 138 "separated by dashes expected (i.e. xx-xx-xx-xx-xx-xx" }, 139 { LDNS_WIREPARSE_ERR_SYNTAX_EUI64, 140 "Conversion error, 8 two character hex numbers " 141 "separated by dashes expected (i.e. xx-xx-xx-xx-xx-xx-xx-xx" }, 142 { LDNS_WIREPARSE_ERR_SYNTAX_TAG, 143 "Conversion error, a non-zero sequence of US-ASCII letters " 144 "and numbers in lower case expected" }, 145 { LDNS_WIREPARSE_ERR_NOT_IMPL, "not implemented" }, 146 { LDNS_WIREPARSE_ERR_SYNTAX_INT, "Conversion error, integer expected" }, 147 { LDNS_WIREPARSE_ERR_SYNTAX_IP4, "Conversion error, ip4 addr expected" }, 148 { LDNS_WIREPARSE_ERR_SYNTAX_IP6, "Conversion error, ip6 addr expected" }, 149 { LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW, "Syntax error, integer overflow" }, 150 { LDNS_WIREPARSE_ERR_INCLUDE, "$INCLUDE directive was seen in the zone" }, 151 { LDNS_WIREPARSE_ERR_PARENTHESIS, "Parse error, parenthesis mismatch" }, 152 { LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY, "Unknown SvcParamKey"}, 153 { LDNS_WIREPARSE_ERR_SVCB_MISSING_PARAM, "SvcParam is missing a SvcParamValue"}, 154 { LDNS_WIREPARSE_ERR_SVCB_DUPLICATE_KEYS, "Duplicate SVCB key found"}, 155 { LDNS_WIREPARSE_ERR_SVCB_MANDATORY_TOO_MANY_KEYS, "Too many keys in mandatory" }, 156 { LDNS_WIREPARSE_ERR_SVCB_TOO_MANY_PARAMS, 157 "Too many SvcParams. Unbound only allows 63 entries" }, 158 { LDNS_WIREPARSE_ERR_SVCB_MANDATORY_MISSING_PARAM, 159 "Mandatory SvcParamKey is missing"}, 160 { LDNS_WIREPARSE_ERR_SVCB_MANDATORY_DUPLICATE_KEY, 161 "Keys in SvcParam mandatory MUST be unique" }, 162 { LDNS_WIREPARSE_ERR_SVCB_MANDATORY_IN_MANDATORY, 163 "mandatory MUST not be included as mandatory parameter" }, 164 { LDNS_WIREPARSE_ERR_SVCB_PORT_VALUE_SYNTAX, 165 "Could not parse port SvcParamValue" }, 166 { LDNS_WIREPARSE_ERR_SVCB_IPV4_TOO_MANY_ADDRESSES, 167 "Too many IPv4 addresses in ipv4hint" }, 168 { LDNS_WIREPARSE_ERR_SVCB_IPV6_TOO_MANY_ADDRESSES, 169 "Too many IPv6 addresses in ipv6hint" }, 170 { LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE, 171 "Alpn strings need to be smaller than 255 chars"}, 172 { LDNS_WIREPARSE_ERR_SVCB_NO_DEFAULT_ALPN_VALUE, 173 "No-default-alpn should not have a value" }, 174 { LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA, 175 "General SVCParam error" }, 176 { 0, NULL } 177 }; 178 sldns_lookup_table* sldns_wireparse_errors = sldns_wireparse_errors_data; 179 180 static sldns_lookup_table sldns_edns_flags_data[] = { 181 { 3600, "do"}, 182 { 0, NULL} 183 }; 184 sldns_lookup_table* sldns_edns_flags = sldns_edns_flags_data; 185 186 static sldns_lookup_table sldns_edns_options_data[] = { 187 { 1, "LLQ" }, 188 { 2, "UL" }, 189 { 3, "NSID" }, 190 /* 4 draft-cheshire-edns0-owner-option */ 191 { 5, "DAU" }, 192 { 6, "DHU" }, 193 { 7, "N3U" }, 194 { 8, "edns-client-subnet" }, 195 { 10, "COOKIE" }, 196 { 11, "edns-tcp-keepalive"}, 197 { 12, "Padding" }, 198 { 15, "EDE"}, 199 { 0, NULL} 200 }; 201 sldns_lookup_table* sldns_edns_options = sldns_edns_options_data; 202 203 /* From RFC8914 5.2 Table 3, the "Extended DNS Error Codes" registry. */ 204 static sldns_lookup_table sldns_edns_ede_codes_data[] = { 205 { LDNS_EDE_NONE, "None" }, 206 { LDNS_EDE_OTHER, "Other Error" }, 207 { LDNS_EDE_UNSUPPORTED_DNSKEY_ALG, "Unsupported DNSKEY Algorithm" }, 208 { LDNS_EDE_UNSUPPORTED_DS_DIGEST, "Unsupported DS Digest Type" }, 209 { LDNS_EDE_STALE_ANSWER, "Stale Answer" }, 210 { LDNS_EDE_FORGED_ANSWER, "Forged Answer" }, 211 { LDNS_EDE_DNSSEC_INDETERMINATE, "DNSSEC Indeterminate" }, 212 { LDNS_EDE_DNSSEC_BOGUS, "DNSSEC Bogus" }, 213 { LDNS_EDE_SIGNATURE_EXPIRED, "Signature Expired" }, 214 { LDNS_EDE_SIGNATURE_NOT_YET_VALID, "Signature Not Yet Valid" }, 215 { LDNS_EDE_DNSKEY_MISSING, "DNSKEY Missing" }, 216 { LDNS_EDE_RRSIGS_MISSING, "RRSIGs Missing" }, 217 { LDNS_EDE_NO_ZONE_KEY_BIT_SET, "No Zone Key Bit Set" }, 218 { LDNS_EDE_NSEC_MISSING, "NSEC Missing" }, 219 { LDNS_EDE_CACHED_ERROR, "Cached Error" }, 220 { LDNS_EDE_NOT_READY, "Not Ready" }, 221 { LDNS_EDE_BLOCKED, "Blocked" }, 222 { LDNS_EDE_CENSORED, "Censored" }, 223 { LDNS_EDE_FILTERED, "Filtered" }, 224 { LDNS_EDE_PROHIBITED, "Prohibited" }, 225 { LDNS_EDE_STALE_NXDOMAIN_ANSWER, "Stale NXDOMAIN Answer" }, 226 { LDNS_EDE_NOT_AUTHORITATIVE, "Not Authoritative" }, 227 { LDNS_EDE_NOT_SUPPORTED, "Not Supported" }, 228 { LDNS_EDE_NO_REACHABLE_AUTHORITY, "No Reachable Authority" }, 229 { LDNS_EDE_NETWORK_ERROR, "Network Error" }, 230 { LDNS_EDE_INVALID_DATA, "Invalid Data" }, 231 { LDNS_EDE_SIGNATURE_EXPIRED_BEFORE_VALID, "Signature Expired Before Valid" }, 232 { LDNS_EDE_TOO_EARLY, "Non-Replayable Transactions Received in 0-RTT Data" }, 233 { LDNS_EDE_UNSUPPORTED_NSEC3_ITERATIONS, "Unsupported NSEC3 Iterations Value" }, 234 { LDNS_EDE_BADPROXYPOLICY, "Unable to Conform to Policy" }, 235 { LDNS_EDE_SYNTHESIZED, "Synthesized Answer" }, 236 { 0, NULL} 237 }; 238 sldns_lookup_table* sldns_edns_ede_codes = sldns_edns_ede_codes_data; 239 240 static sldns_lookup_table sldns_tsig_errors_data[] = { 241 { LDNS_TSIG_ERROR_NOERROR, "NOERROR" }, 242 { LDNS_RCODE_FORMERR, "FORMERR" }, 243 { LDNS_RCODE_SERVFAIL, "SERVFAIL" }, 244 { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" }, 245 { LDNS_RCODE_NOTIMPL, "NOTIMPL" }, 246 { LDNS_RCODE_REFUSED, "REFUSED" }, 247 { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" }, 248 { LDNS_RCODE_YXRRSET, "YXRRSET" }, 249 { LDNS_RCODE_NXRRSET, "NXRRSET" }, 250 { LDNS_RCODE_NOTAUTH, "NOTAUTH" }, 251 { LDNS_RCODE_NOTZONE, "NOTZONE" }, 252 { LDNS_TSIG_ERROR_BADSIG, "BADSIG" }, 253 { LDNS_TSIG_ERROR_BADKEY, "BADKEY" }, 254 { LDNS_TSIG_ERROR_BADTIME, "BADTIME" }, 255 { LDNS_TSIG_ERROR_BADMODE, "BADMODE" }, 256 { LDNS_TSIG_ERROR_BADNAME, "BADNAME" }, 257 { LDNS_TSIG_ERROR_BADALG, "BADALG" }, 258 { 0, NULL } 259 }; 260 sldns_lookup_table* sldns_tsig_errors = sldns_tsig_errors_data; 261 262 /* draft-ietf-dnsop-svcb-https-06: 6. Initial SvcParamKeys */ 263 const char *svcparamkey_strs[] = { 264 "mandatory", "alpn", "no-default-alpn", "port", 265 "ipv4hint", "ech", "ipv6hint", "dohpath" 266 }; 267 268 char* sldns_wire2str_pkt(uint8_t* data, size_t len) 269 { 270 size_t slen = (size_t)sldns_wire2str_pkt_buf(data, len, NULL, 0); 271 char* result = (char*)malloc(slen+1); 272 if(!result) return NULL; 273 sldns_wire2str_pkt_buf(data, len, result, slen+1); 274 return result; 275 } 276 277 char* sldns_wire2str_rr(uint8_t* rr, size_t len) 278 { 279 size_t slen = (size_t)sldns_wire2str_rr_buf(rr, len, NULL, 0); 280 char* result = (char*)malloc(slen+1); 281 if(!result) return NULL; 282 sldns_wire2str_rr_buf(rr, len, result, slen+1); 283 return result; 284 } 285 286 char* sldns_wire2str_type(uint16_t rrtype) 287 { 288 char buf[16]; 289 sldns_wire2str_type_buf(rrtype, buf, sizeof(buf)); 290 return strdup(buf); 291 } 292 293 char* sldns_wire2str_class(uint16_t rrclass) 294 { 295 char buf[16]; 296 sldns_wire2str_class_buf(rrclass, buf, sizeof(buf)); 297 return strdup(buf); 298 } 299 300 char* sldns_wire2str_dname(uint8_t* dname, size_t dname_len) 301 { 302 size_t slen=(size_t)sldns_wire2str_dname_buf(dname, dname_len, NULL, 0); 303 char* result = (char*)malloc(slen+1); 304 if(!result) return NULL; 305 sldns_wire2str_dname_buf(dname, dname_len, result, slen+1); 306 return result; 307 } 308 309 char* sldns_wire2str_rcode(int rcode) 310 { 311 char buf[16]; 312 sldns_wire2str_rcode_buf(rcode, buf, sizeof(buf)); 313 return strdup(buf); 314 } 315 316 int sldns_wire2str_pkt_buf(uint8_t* d, size_t dlen, char* s, size_t slen) 317 { 318 /* use arguments as temporary variables */ 319 return sldns_wire2str_pkt_scan(&d, &dlen, &s, &slen); 320 } 321 322 int sldns_wire2str_rr_buf(uint8_t* d, size_t dlen, char* s, size_t slen) 323 { 324 /* use arguments as temporary variables */ 325 return sldns_wire2str_rr_scan(&d, &dlen, &s, &slen, NULL, 0, NULL); 326 } 327 328 int sldns_wire2str_rrquestion_buf(uint8_t* d, size_t dlen, char* s, size_t slen) 329 { 330 /* use arguments as temporary variables */ 331 return sldns_wire2str_rrquestion_scan(&d, &dlen, &s, &slen, NULL, 0, NULL); 332 } 333 334 int sldns_wire2str_rdata_buf(uint8_t* rdata, size_t rdata_len, char* str, 335 size_t str_len, uint16_t rrtype) 336 { 337 /* use arguments as temporary variables */ 338 return sldns_wire2str_rdata_scan(&rdata, &rdata_len, &str, &str_len, 339 rrtype, NULL, 0, NULL); 340 } 341 342 int sldns_wire2str_rr_unknown_buf(uint8_t* d, size_t dlen, char* s, size_t slen) 343 { 344 /* use arguments as temporary variables */ 345 return sldns_wire2str_rr_unknown_scan(&d, &dlen, &s, &slen, NULL, 0, NULL); 346 } 347 348 int sldns_wire2str_rr_comment_buf(uint8_t* rr, size_t rrlen, size_t dname_len, 349 char* s, size_t slen) 350 { 351 uint16_t rrtype = sldns_wirerr_get_type(rr, rrlen, dname_len); 352 return sldns_wire2str_rr_comment_print(&s, &slen, rr, rrlen, dname_len, 353 rrtype); 354 } 355 356 int sldns_wire2str_type_buf(uint16_t rrtype, char* s, size_t slen) 357 { 358 /* use arguments as temporary variables */ 359 return sldns_wire2str_type_print(&s, &slen, rrtype); 360 } 361 362 int sldns_wire2str_class_buf(uint16_t rrclass, char* s, size_t slen) 363 { 364 /* use arguments as temporary variables */ 365 return sldns_wire2str_class_print(&s, &slen, rrclass); 366 } 367 368 int sldns_wire2str_rcode_buf(int rcode, char* s, size_t slen) 369 { 370 /* use arguments as temporary variables */ 371 return sldns_wire2str_rcode_print(&s, &slen, rcode); 372 } 373 374 int sldns_wire2str_opcode_buf(int opcode, char* s, size_t slen) 375 { 376 /* use arguments as temporary variables */ 377 return sldns_wire2str_opcode_print(&s, &slen, opcode); 378 } 379 380 int sldns_wire2str_dname_buf(uint8_t* d, size_t dlen, char* s, size_t slen) 381 { 382 /* use arguments as temporary variables */ 383 return sldns_wire2str_dname_scan(&d, &dlen, &s, &slen, NULL, 0, NULL); 384 } 385 386 int sldns_str_vprint(char** str, size_t* slen, const char* format, va_list args) 387 { 388 int w = vsnprintf(*str, *slen, format, args); 389 if(w < 0) { 390 /* error in printout */ 391 return 0; 392 } else if((size_t)w >= *slen) { 393 *str = NULL; /* we do not want str to point outside of buffer*/ 394 *slen = 0; 395 } else { 396 *str += w; 397 *slen -= w; 398 } 399 return w; 400 } 401 402 int sldns_str_print(char** str, size_t* slen, const char* format, ...) 403 { 404 int w; 405 va_list args; 406 va_start(args, format); 407 w = sldns_str_vprint(str, slen, format, args); 408 va_end(args); 409 return w; 410 } 411 412 /** print hex format into text buffer for specified length */ 413 static int print_hex_buf(char** s, size_t* slen, uint8_t* buf, size_t len) 414 { 415 const char* hex = "0123456789ABCDEF"; 416 size_t i; 417 for(i=0; i<len; i++) { 418 (void)sldns_str_print(s, slen, "%c%c", hex[(buf[i]&0xf0)>>4], 419 hex[buf[i]&0x0f]); 420 } 421 return (int)len*2; 422 } 423 424 /** print remainder of buffer in hex format with prefixed text */ 425 static int print_remainder_hex(const char* pref, uint8_t** d, size_t* dlen, 426 char** s, size_t* slen) 427 { 428 int w = 0; 429 w += sldns_str_print(s, slen, "%s", pref); 430 w += print_hex_buf(s, slen, *d, *dlen); 431 *d += *dlen; 432 *dlen = 0; 433 return w; 434 } 435 436 int sldns_wire2str_pkt_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen) 437 { 438 int w = 0, comprloop = 0; 439 unsigned qdcount, ancount, nscount, arcount, i; 440 uint8_t* pkt = *d; 441 size_t pktlen = *dlen; 442 if(*dlen >= LDNS_HEADER_SIZE) { 443 qdcount = (unsigned)LDNS_QDCOUNT(*d); 444 ancount = (unsigned)LDNS_ANCOUNT(*d); 445 nscount = (unsigned)LDNS_NSCOUNT(*d); 446 arcount = (unsigned)LDNS_ARCOUNT(*d); 447 } else { 448 qdcount = ancount = nscount = arcount = 0; 449 } 450 w += sldns_wire2str_header_scan(d, dlen, s, slen); 451 w += sldns_str_print(s, slen, "\n"); 452 w += sldns_str_print(s, slen, ";; QUESTION SECTION:\n"); 453 for(i=0; i<qdcount; i++) { 454 w += sldns_wire2str_rrquestion_scan(d, dlen, s, slen, 455 pkt, pktlen, &comprloop); 456 if(!*dlen) break; 457 } 458 w += sldns_str_print(s, slen, "\n"); 459 w += sldns_str_print(s, slen, ";; ANSWER SECTION:\n"); 460 for(i=0; i<ancount; i++) { 461 w += sldns_wire2str_rr_scan(d, dlen, s, slen, pkt, pktlen, &comprloop); 462 if(!*dlen) break; 463 } 464 w += sldns_str_print(s, slen, "\n"); 465 w += sldns_str_print(s, slen, ";; AUTHORITY SECTION:\n"); 466 for(i=0; i<nscount; i++) { 467 w += sldns_wire2str_rr_scan(d, dlen, s, slen, pkt, pktlen, &comprloop); 468 if(!*dlen) break; 469 } 470 w += sldns_str_print(s, slen, "\n"); 471 w += sldns_str_print(s, slen, ";; ADDITIONAL SECTION:\n"); 472 for(i=0; i<arcount; i++) { 473 w += sldns_wire2str_rr_scan(d, dlen, s, slen, pkt, pktlen, &comprloop); 474 if(!*dlen) break; 475 } 476 /* other fields: WHEN(time), SERVER(IP) not available here. */ 477 w += sldns_str_print(s, slen, ";; MSG SIZE rcvd: %d\n", (int)pktlen); 478 if(*dlen > 0) { 479 w += print_remainder_hex(";; trailing garbage 0x", 480 d, dlen, s, slen); 481 w += sldns_str_print(s, slen, "\n"); 482 } 483 return w; 484 } 485 486 /** scan type, class and ttl and printout, for rr */ 487 static int sldns_rr_tcttl_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 488 { 489 int w = 0; 490 uint16_t t, c; 491 uint32_t ttl; 492 if(*dl < 8) { 493 if(*dl < 4) 494 return w + print_remainder_hex("; Error malformed 0x", 495 d, dl, s, sl); 496 /* these print values or 0x.. if none left */ 497 t = sldns_read_uint16(*d); 498 c = sldns_read_uint16((*d)+2); 499 (*d)+=4; 500 (*dl)-=4; 501 w += sldns_wire2str_class_print(s, sl, c); 502 w += sldns_str_print(s, sl, "\t"); 503 w += sldns_wire2str_type_print(s, sl, t); 504 if(*dl == 0) 505 return w + sldns_str_print(s, sl, "; Error no ttl"); 506 return w + print_remainder_hex( 507 "; Error malformed ttl 0x", d, dl, s, sl); 508 } 509 t = sldns_read_uint16(*d); 510 c = sldns_read_uint16((*d)+2); 511 ttl = sldns_read_uint32((*d)+4); 512 (*d)+=8; 513 (*dl)-=8; 514 w += sldns_str_print(s, sl, "%lu\t", (unsigned long)ttl); 515 w += sldns_wire2str_class_print(s, sl, c); 516 w += sldns_str_print(s, sl, "\t"); 517 w += sldns_wire2str_type_print(s, sl, t); 518 return w; 519 } 520 521 int sldns_wire2str_rr_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen, 522 uint8_t* pkt, size_t pktlen, int* comprloop) 523 { 524 int w = 0; 525 uint8_t* rr = *d; 526 size_t rrlen = *dlen, dname_off, rdlen, ordlen; 527 uint16_t rrtype = 0; 528 529 if(*dlen >= 3 && (*d)[0]==0 && 530 sldns_read_uint16((*d)+1)==LDNS_RR_TYPE_OPT) { 531 /* perform EDNS OPT processing */ 532 return sldns_wire2str_edns_scan(d, dlen, s, slen, pkt, pktlen); 533 } 534 535 /* try to scan the rdata with pretty-printing, but if that fails, then 536 * scan the rdata as an unknown RR type */ 537 w += sldns_wire2str_dname_scan(d, dlen, s, slen, pkt, pktlen, comprloop); 538 w += sldns_str_print(s, slen, "\t"); 539 dname_off = rrlen-(*dlen); 540 if(*dlen == 4) { 541 /* like a question-RR */ 542 uint16_t t = sldns_read_uint16(*d); 543 uint16_t c = sldns_read_uint16((*d)+2); 544 (*d)+=4; 545 (*dlen)-=4; 546 w += sldns_wire2str_class_print(s, slen, c); 547 w += sldns_str_print(s, slen, "\t"); 548 w += sldns_wire2str_type_print(s, slen, t); 549 w += sldns_str_print(s, slen, " ; Error no ttl,rdata\n"); 550 return w; 551 } 552 if(*dlen < 8) { 553 if(*dlen == 0) 554 return w + sldns_str_print(s, slen, ";Error missing RR\n"); 555 w += print_remainder_hex(";Error partial RR 0x", d, dlen, s, slen); 556 return w + sldns_str_print(s, slen, "\n"); 557 } 558 rrtype = sldns_read_uint16(*d); 559 w += sldns_rr_tcttl_scan(d, dlen, s, slen); 560 w += sldns_str_print(s, slen, "\t"); 561 562 /* rdata */ 563 if(*dlen < 2) { 564 if(*dlen == 0) 565 return w + sldns_str_print(s, slen, ";Error missing rdatalen\n"); 566 w += print_remainder_hex(";Error missing rdatalen 0x", 567 d, dlen, s, slen); 568 return w + sldns_str_print(s, slen, "\n"); 569 } 570 rdlen = sldns_read_uint16(*d); 571 ordlen = rdlen; 572 (*d)+=2; 573 (*dlen)-=2; 574 if(*dlen < rdlen) { 575 w += sldns_str_print(s, slen, "\\# %u ", (unsigned)rdlen); 576 if(*dlen == 0) 577 return w + sldns_str_print(s, slen, ";Error missing rdata\n"); 578 w += print_remainder_hex(";Error partial rdata 0x", d, dlen, s, slen); 579 return w + sldns_str_print(s, slen, "\n"); 580 } 581 w += sldns_wire2str_rdata_scan(d, &rdlen, s, slen, rrtype, pkt, pktlen, 582 comprloop); 583 (*dlen) -= (ordlen-rdlen); 584 585 /* default comment */ 586 w += sldns_wire2str_rr_comment_print(s, slen, rr, rrlen, dname_off, 587 rrtype); 588 w += sldns_str_print(s, slen, "\n"); 589 return w; 590 } 591 592 int sldns_wire2str_rrquestion_scan(uint8_t** d, size_t* dlen, char** s, 593 size_t* slen, uint8_t* pkt, size_t pktlen, int* comprloop) 594 { 595 int w = 0; 596 uint16_t t, c; 597 w += sldns_wire2str_dname_scan(d, dlen, s, slen, pkt, pktlen, comprloop); 598 w += sldns_str_print(s, slen, "\t"); 599 if(*dlen < 4) { 600 if(*dlen == 0) 601 return w + sldns_str_print(s, slen, "Error malformed\n"); 602 w += print_remainder_hex("Error malformed 0x", d, dlen, s, slen); 603 return w + sldns_str_print(s, slen, "\n"); 604 } 605 t = sldns_read_uint16(*d); 606 c = sldns_read_uint16((*d)+2); 607 (*d)+=4; 608 (*dlen)-=4; 609 w += sldns_wire2str_class_print(s, slen, c); 610 w += sldns_str_print(s, slen, "\t"); 611 w += sldns_wire2str_type_print(s, slen, t); 612 w += sldns_str_print(s, slen, "\n"); 613 return w; 614 } 615 616 int sldns_wire2str_rr_unknown_scan(uint8_t** d, size_t* dlen, char** s, 617 size_t* slen, uint8_t* pkt, size_t pktlen, int* comprloop) 618 { 619 size_t rdlen, ordlen; 620 int w = 0; 621 w += sldns_wire2str_dname_scan(d, dlen, s, slen, pkt, pktlen, comprloop); 622 w += sldns_str_print(s, slen, "\t"); 623 w += sldns_rr_tcttl_scan(d, dlen, s, slen); 624 w += sldns_str_print(s, slen, "\t"); 625 if(*dlen < 2) { 626 if(*dlen == 0) 627 return w + sldns_str_print(s, slen, ";Error missing rdatalen\n"); 628 w += print_remainder_hex(";Error missing rdatalen 0x", 629 d, dlen, s, slen); 630 return w + sldns_str_print(s, slen, "\n"); 631 } 632 rdlen = sldns_read_uint16(*d); 633 ordlen = rdlen; 634 (*d) += 2; 635 (*dlen) -= 2; 636 if(*dlen < rdlen) { 637 w += sldns_str_print(s, slen, "\\# %u ", (unsigned)rdlen); 638 if(*dlen == 0) 639 return w + sldns_str_print(s, slen, ";Error missing rdata\n"); 640 w += print_remainder_hex(";Error partial rdata 0x", d, dlen, s, slen); 641 return w + sldns_str_print(s, slen, "\n"); 642 } 643 w += sldns_wire2str_rdata_unknown_scan(d, &rdlen, s, slen); 644 (*dlen) -= (ordlen-rdlen); 645 w += sldns_str_print(s, slen, "\n"); 646 return w; 647 } 648 649 /** print rr comment for type DNSKEY */ 650 static int rr_comment_dnskey(char** s, size_t* slen, uint8_t* rr, 651 size_t rrlen, size_t dname_off) 652 { 653 size_t rdlen; 654 uint8_t* rdata; 655 int flags, w = 0; 656 if(rrlen < dname_off + 10) return 0; 657 rdlen = sldns_read_uint16(rr+dname_off+8); 658 if(rrlen < dname_off + 10 + rdlen) return 0; 659 if(rdlen < 2) return 0; 660 rdata = rr + dname_off + 10; 661 flags = (int)sldns_read_uint16(rdata); 662 w += sldns_str_print(s, slen, " ;{"); 663 664 /* id */ 665 w += sldns_str_print(s, slen, "id = %u", 666 sldns_calc_keytag_raw(rdata, rdlen)); 667 668 /* flags */ 669 if((flags&LDNS_KEY_ZONE_KEY)) { 670 if((flags&LDNS_KEY_SEP_KEY)) 671 w += sldns_str_print(s, slen, " (ksk)"); 672 else w += sldns_str_print(s, slen, " (zsk)"); 673 } 674 675 /* keysize */ 676 if(rdlen > 4) { 677 w += sldns_str_print(s, slen, ", "); 678 w += sldns_str_print(s, slen, "size = %db", 679 (int)sldns_rr_dnskey_key_size_raw( 680 (unsigned char*)rdata+4, rdlen-4, (int)(rdata[3]))); 681 } 682 683 w += sldns_str_print(s, slen, "}"); 684 return w; 685 } 686 687 /** print rr comment for type RRSIG */ 688 static int rr_comment_rrsig(char** s, size_t* slen, uint8_t* rr, 689 size_t rrlen, size_t dname_off) 690 { 691 size_t rdlen; 692 uint8_t* rdata; 693 if(rrlen < dname_off + 10) return 0; 694 rdlen = sldns_read_uint16(rr+dname_off+8); 695 if(rrlen < dname_off + 10 + rdlen) return 0; 696 rdata = rr + dname_off + 10; 697 if(rdlen < 18) return 0; 698 return sldns_str_print(s, slen, " ;{id = %d}", 699 (int)sldns_read_uint16(rdata+16)); 700 } 701 702 /** print rr comment for type NSEC3 */ 703 static int rr_comment_nsec3(char** s, size_t* slen, uint8_t* rr, 704 size_t rrlen, size_t dname_off) 705 { 706 size_t rdlen; 707 uint8_t* rdata; 708 int w = 0; 709 if(rrlen < dname_off + 10) return 0; 710 rdlen = sldns_read_uint16(rr+dname_off+8); 711 if(rrlen < dname_off + 10 + rdlen) return 0; 712 rdata = rr + dname_off + 10; 713 if(rdlen < 2) return 0; 714 if((rdata[1] & LDNS_NSEC3_VARS_OPTOUT_MASK)) 715 w += sldns_str_print(s, slen, " ;{flags: optout}"); 716 return w; 717 } 718 719 int sldns_wire2str_rr_comment_print(char** s, size_t* slen, uint8_t* rr, 720 size_t rrlen, size_t dname_off, uint16_t rrtype) 721 { 722 if(rrtype == LDNS_RR_TYPE_DNSKEY) { 723 return rr_comment_dnskey(s, slen, rr, rrlen, dname_off); 724 } else if(rrtype == LDNS_RR_TYPE_RRSIG) { 725 return rr_comment_rrsig(s, slen, rr, rrlen, dname_off); 726 } else if(rrtype == LDNS_RR_TYPE_NSEC3) { 727 return rr_comment_nsec3(s, slen, rr, rrlen, dname_off); 728 } 729 return 0; 730 } 731 732 int sldns_wire2str_header_scan(uint8_t** d, size_t* dlen, char** s, 733 size_t* slen) 734 { 735 int w = 0; 736 int opcode, rcode; 737 w += sldns_str_print(s, slen, ";; ->>HEADER<<- "); 738 if(*dlen == 0) 739 return w+sldns_str_print(s, slen, "Error empty packet"); 740 if(*dlen < 4) 741 return w+print_remainder_hex("Error header too short 0x", d, dlen, s, slen); 742 opcode = (int)LDNS_OPCODE_WIRE(*d); 743 rcode = (int)LDNS_RCODE_WIRE(*d); 744 w += sldns_str_print(s, slen, "opcode: "); 745 w += sldns_wire2str_opcode_print(s, slen, opcode); 746 w += sldns_str_print(s, slen, ", "); 747 w += sldns_str_print(s, slen, "rcode: "); 748 w += sldns_wire2str_rcode_print(s, slen, rcode); 749 w += sldns_str_print(s, slen, ", "); 750 w += sldns_str_print(s, slen, "id: %d\n", (int)LDNS_ID_WIRE(*d)); 751 w += sldns_str_print(s, slen, ";; flags:"); 752 if(LDNS_QR_WIRE(*d)) w += sldns_str_print(s, slen, " qr"); 753 if(LDNS_AA_WIRE(*d)) w += sldns_str_print(s, slen, " aa"); 754 if(LDNS_TC_WIRE(*d)) w += sldns_str_print(s, slen, " tc"); 755 if(LDNS_RD_WIRE(*d)) w += sldns_str_print(s, slen, " rd"); 756 if(LDNS_CD_WIRE(*d)) w += sldns_str_print(s, slen, " cd"); 757 if(LDNS_RA_WIRE(*d)) w += sldns_str_print(s, slen, " ra"); 758 if(LDNS_AD_WIRE(*d)) w += sldns_str_print(s, slen, " ad"); 759 if(LDNS_Z_WIRE(*d)) w += sldns_str_print(s, slen, " z"); 760 w += sldns_str_print(s, slen, " ; "); 761 if(*dlen < LDNS_HEADER_SIZE) 762 return w+print_remainder_hex("Error header too short 0x", d, dlen, s, slen); 763 w += sldns_str_print(s, slen, "QUERY: %d, ", (int)LDNS_QDCOUNT(*d)); 764 w += sldns_str_print(s, slen, "ANSWER: %d, ", (int)LDNS_ANCOUNT(*d)); 765 w += sldns_str_print(s, slen, "AUTHORITY: %d, ", (int)LDNS_NSCOUNT(*d)); 766 w += sldns_str_print(s, slen, "ADDITIONAL: %d ", (int)LDNS_ARCOUNT(*d)); 767 *d += LDNS_HEADER_SIZE; 768 *dlen -= LDNS_HEADER_SIZE; 769 return w; 770 } 771 772 int sldns_wire2str_rdata_scan(uint8_t** d, size_t* dlen, char** s, 773 size_t* slen, uint16_t rrtype, uint8_t* pkt, size_t pktlen, 774 int* comprloop) 775 { 776 /* try to prettyprint, but if that fails, use unknown format */ 777 uint8_t* origd = *d; 778 char* origs = *s; 779 size_t origdlen = *dlen, origslen = *slen; 780 size_t r_cnt, r_max; 781 sldns_rdf_type rdftype; 782 int w = 0, n; 783 784 const sldns_rr_descriptor *desc = sldns_rr_descript(rrtype); 785 if(!desc) /* unknown format */ 786 return sldns_wire2str_rdata_unknown_scan(d, dlen, s, slen); 787 /* dlen equals the rdatalen for the rdata */ 788 789 r_max = sldns_rr_descriptor_maximum(desc); 790 for(r_cnt=0; r_cnt < r_max; r_cnt++) { 791 if(*dlen == 0) { 792 if(r_cnt < sldns_rr_descriptor_minimum(desc)) 793 goto failed; 794 break; /* nothing more to print */ 795 } 796 rdftype = sldns_rr_descriptor_field_type(desc, r_cnt); 797 if(r_cnt != 0) 798 w += sldns_str_print(s, slen, " "); 799 n = sldns_wire2str_rdf_scan(d, dlen, s, slen, rdftype, 800 pkt, pktlen, comprloop); 801 if(n == -1) { 802 failed: 803 /* failed, use unknown format */ 804 *d = origd; *s = origs; 805 *dlen = origdlen; *slen = origslen; 806 return sldns_wire2str_rdata_unknown_scan(d, dlen, 807 s, slen); 808 } 809 w += n; 810 } 811 if(*dlen != 0) { 812 goto failed; 813 } 814 return w; 815 } 816 817 int sldns_wire2str_rdata_unknown_scan(uint8_t** d, size_t* dlen, char** s, 818 size_t* slen) 819 { 820 int w = 0; 821 822 /* print length */ 823 w += sldns_str_print(s, slen, "\\# %u", (unsigned)*dlen); 824 825 /* print rdlen in hex */ 826 if(*dlen != 0) 827 w += sldns_str_print(s, slen, " "); 828 w += print_hex_buf(s, slen, *d, *dlen); 829 (*d) += *dlen; 830 (*dlen) = 0; 831 return w; 832 } 833 834 /** print and escape one character for a domain dname */ 835 static int dname_char_print(char** s, size_t* slen, uint8_t c) 836 { 837 if(c == '.' || c == ';' || c == '(' || c == ')' || c == '\\') 838 return sldns_str_print(s, slen, "\\%c", c); 839 else if(!(isascii((unsigned char)c) && isgraph((unsigned char)c))) 840 return sldns_str_print(s, slen, "\\%03u", (unsigned)c); 841 /* plain printout */ 842 if(*slen) { 843 **s = (char)c; 844 (*s)++; 845 (*slen)--; 846 } 847 return 1; 848 } 849 850 int sldns_wire2str_dname_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen, 851 uint8_t* pkt, size_t pktlen, int* comprloop) 852 { 853 int w = 0; 854 /* spool labels onto the string, use compression if its there */ 855 uint8_t* pos = *d; 856 unsigned i, counter=0; 857 unsigned maxcompr = MAX_COMPRESS_PTRS; /* loop detection, max compr ptrs */ 858 int in_buf = 1; 859 size_t dname_len = 0; 860 if(comprloop) { 861 if(*comprloop != 0) 862 maxcompr = 30; /* for like ipv6 reverse name, per label */ 863 if(*comprloop > 4) 864 maxcompr = 4; /* just don't want to spend time, any more */ 865 } 866 if(*dlen == 0) return sldns_str_print(s, slen, "ErrorMissingDname"); 867 if(*pos == 0) { 868 (*d)++; 869 (*dlen)--; 870 return sldns_str_print(s, slen, "."); 871 } 872 while((!pkt || pos < pkt+pktlen) && *pos) { 873 /* read label length */ 874 uint8_t labellen = *pos++; 875 if(in_buf) { (*d)++; (*dlen)--; } 876 877 /* find out what sort of label we have */ 878 if((labellen&0xc0) == 0xc0) { 879 /* compressed */ 880 uint16_t target = 0; 881 if(in_buf && *dlen == 0) 882 return w + sldns_str_print(s, slen, 883 "ErrorPartialDname"); 884 else if(!in_buf && pos+1 > pkt+pktlen) 885 return w + sldns_str_print(s, slen, 886 "ErrorPartialDname"); 887 target = ((labellen&0x3f)<<8) | *pos; 888 if(in_buf) { (*d)++; (*dlen)--; } 889 /* move to target, if possible */ 890 if(!pkt || target >= pktlen) 891 return w + sldns_str_print(s, slen, 892 "ErrorComprPtrOutOfBounds"); 893 if(counter++ > maxcompr) { 894 if(comprloop && *comprloop < 10) 895 (*comprloop)++; 896 return w + sldns_str_print(s, slen, 897 "ErrorComprPtrLooped"); 898 } 899 in_buf = 0; 900 pos = pkt+target; 901 continue; 902 } else if((labellen&0xc0)) { 903 /* notimpl label type */ 904 w += sldns_str_print(s, slen, 905 "ErrorLABELTYPE%xIsUnknown", 906 (int)(labellen&0xc0)); 907 return w; 908 } 909 910 /* spool label characters, end with '.' */ 911 if(in_buf && *dlen < (size_t)labellen) 912 labellen = (uint8_t)*dlen; 913 else if(!in_buf && pos+(size_t)labellen > pkt+pktlen) 914 labellen = (uint8_t)(pkt + pktlen - pos); 915 dname_len += ((size_t)labellen)+1; 916 if(dname_len > LDNS_MAX_DOMAINLEN) { 917 /* dname_len counts the uncompressed length we have 918 * seen so far, and the domain name has become too 919 * long, prevent the loop from printing overly long 920 * content. */ 921 w += sldns_str_print(s, slen, 922 "ErrorDomainNameTooLong"); 923 return w; 924 } 925 for(i=0; i<(unsigned)labellen; i++) { 926 w += dname_char_print(s, slen, *pos++); 927 } 928 if(in_buf) { 929 (*d) += labellen; 930 (*dlen) -= labellen; 931 if(*dlen == 0) break; 932 } 933 w += sldns_str_print(s, slen, "."); 934 } 935 /* skip over final root label */ 936 if(in_buf && *dlen > 0) { (*d)++; (*dlen)--; } 937 /* in case we printed no labels, terminate dname */ 938 if(w == 0) w += sldns_str_print(s, slen, "."); 939 return w; 940 } 941 942 int sldns_wire2str_opcode_print(char** s, size_t* slen, int opcode) 943 { 944 sldns_lookup_table *lt = sldns_lookup_by_id(sldns_opcodes, opcode); 945 if (lt && lt->name) { 946 return sldns_str_print(s, slen, "%s", lt->name); 947 } 948 return sldns_str_print(s, slen, "OPCODE%u", (unsigned)opcode); 949 } 950 951 int sldns_wire2str_rcode_print(char** s, size_t* slen, int rcode) 952 { 953 sldns_lookup_table *lt = sldns_lookup_by_id(sldns_rcodes, rcode); 954 if (lt && lt->name) { 955 return sldns_str_print(s, slen, "%s", lt->name); 956 } 957 return sldns_str_print(s, slen, "RCODE%u", (unsigned)rcode); 958 } 959 960 int sldns_wire2str_class_print(char** s, size_t* slen, uint16_t rrclass) 961 { 962 sldns_lookup_table *lt = sldns_lookup_by_id(sldns_rr_classes, 963 (int)rrclass); 964 if (lt && lt->name) { 965 return sldns_str_print(s, slen, "%s", lt->name); 966 } 967 return sldns_str_print(s, slen, "CLASS%u", (unsigned)rrclass); 968 } 969 970 int sldns_wire2str_type_print(char** s, size_t* slen, uint16_t rrtype) 971 { 972 const sldns_rr_descriptor *descriptor = sldns_rr_descript(rrtype); 973 if (descriptor && descriptor->_name) { 974 return sldns_str_print(s, slen, "%s", descriptor->_name); 975 } 976 return sldns_str_print(s, slen, "TYPE%u", (unsigned)rrtype); 977 } 978 979 int sldns_wire2str_edns_option_code_print(char** s, size_t* slen, 980 uint16_t opcode) 981 { 982 sldns_lookup_table *lt = sldns_lookup_by_id(sldns_edns_options, 983 (int)opcode); 984 if (lt && lt->name) { 985 return sldns_str_print(s, slen, "%s", lt->name); 986 } 987 return sldns_str_print(s, slen, "OPT%u", (unsigned)opcode); 988 } 989 990 int sldns_wire2str_class_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen) 991 { 992 uint16_t c; 993 if(*dlen == 0) return 0; 994 if(*dlen < 2) return print_remainder_hex("Error malformed 0x", d, dlen, s, slen); 995 c = sldns_read_uint16(*d); 996 (*d)+=2; 997 (*dlen)-=2; 998 return sldns_wire2str_class_print(s, slen, c); 999 } 1000 1001 int sldns_wire2str_type_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen) 1002 { 1003 uint16_t t; 1004 if(*dlen == 0) return 0; 1005 if(*dlen < 2) return print_remainder_hex("Error malformed 0x", d, dlen, s, slen); 1006 t = sldns_read_uint16(*d); 1007 (*d)+=2; 1008 (*dlen)-=2; 1009 return sldns_wire2str_type_print(s, slen, t); 1010 } 1011 1012 int sldns_wire2str_ttl_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen) 1013 { 1014 uint32_t ttl; 1015 if(*dlen == 0) return 0; 1016 if(*dlen < 4) return print_remainder_hex("Error malformed 0x", d, dlen, s, slen); 1017 ttl = sldns_read_uint32(*d); 1018 (*d)+=4; 1019 (*dlen)-=4; 1020 return sldns_str_print(s, slen, "%u", (unsigned)ttl); 1021 } 1022 1023 static int 1024 sldns_print_svcparamkey(char** s, size_t* slen, uint16_t svcparamkey) 1025 { 1026 if (svcparamkey < SVCPARAMKEY_COUNT) { 1027 return sldns_str_print(s, slen, "%s", svcparamkey_strs[svcparamkey]); 1028 } 1029 else { 1030 return sldns_str_print(s, slen, "key%d", (int)svcparamkey); 1031 } 1032 } 1033 1034 static int sldns_wire2str_svcparam_port2str(char** s, 1035 size_t* slen, uint16_t data_len, uint8_t* data) 1036 { 1037 int w = 0; 1038 1039 if (data_len != 2) 1040 return -1; /* wireformat error, a short is 2 bytes */ 1041 w = sldns_str_print(s, slen, "=%d", (int)sldns_read_uint16(data)); 1042 1043 return w; 1044 } 1045 1046 static int sldns_wire2str_svcparam_ipv4hint2str(char** s, 1047 size_t* slen, uint16_t data_len, uint8_t* data) 1048 { 1049 char ip_str[INET_ADDRSTRLEN + 1]; 1050 1051 int w = 0; 1052 1053 assert(data_len > 0); 1054 1055 if ((data_len % LDNS_IP4ADDRLEN) == 0) { 1056 if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str)) == NULL) 1057 return -1; /* wireformat error, incorrect size or inet family */ 1058 1059 w += sldns_str_print(s, slen, "=%s", ip_str); 1060 data += LDNS_IP4ADDRLEN; 1061 1062 while ((data_len -= LDNS_IP4ADDRLEN) > 0) { 1063 if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str)) == NULL) 1064 return -1; /* wireformat error, incorrect size or inet family */ 1065 1066 w += sldns_str_print(s, slen, ",%s", ip_str); 1067 data += LDNS_IP4ADDRLEN; 1068 } 1069 } else 1070 return -1; 1071 1072 return w; 1073 } 1074 1075 static int sldns_wire2str_svcparam_ipv6hint2str(char** s, 1076 size_t* slen, uint16_t data_len, uint8_t* data) 1077 { 1078 char ip_str[INET6_ADDRSTRLEN + 1]; 1079 1080 int w = 0; 1081 1082 assert(data_len > 0); 1083 1084 if ((data_len % LDNS_IP6ADDRLEN) == 0) { 1085 if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str)) == NULL) 1086 return -1; /* wireformat error, incorrect size or inet family */ 1087 1088 w += sldns_str_print(s, slen, "=%s", ip_str); 1089 data += LDNS_IP6ADDRLEN; 1090 1091 while ((data_len -= LDNS_IP6ADDRLEN) > 0) { 1092 if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str)) == NULL) 1093 return -1; /* wireformat error, incorrect size or inet family */ 1094 1095 w += sldns_str_print(s, slen, ",%s", ip_str); 1096 data += LDNS_IP6ADDRLEN; 1097 } 1098 } else 1099 return -1; 1100 1101 return w; 1102 } 1103 1104 static int sldns_wire2str_svcparam_mandatory2str(char** s, 1105 size_t* slen, uint16_t data_len, uint8_t* data) 1106 { 1107 int w = 0; 1108 1109 assert(data_len > 0); 1110 1111 if (data_len % sizeof(uint16_t)) 1112 return -1; /* wireformat error, data_len must be multiple of shorts */ 1113 w += sldns_str_print(s, slen, "="); 1114 w += sldns_print_svcparamkey(s, slen, sldns_read_uint16(data)); 1115 data += 2; 1116 1117 while ((data_len -= sizeof(uint16_t))) { 1118 w += sldns_str_print(s, slen, ","); 1119 w += sldns_print_svcparamkey(s, slen, sldns_read_uint16(data)); 1120 data += 2; 1121 } 1122 1123 return w; 1124 } 1125 1126 static int sldns_wire2str_svcparam_alpn2str(char** s, 1127 size_t* slen, uint16_t data_len, uint8_t* data) 1128 { 1129 uint8_t *dp = (void *)data; 1130 int w = 0; 1131 1132 assert(data_len > 0); /* Guaranteed by sldns_wire2str_svcparam_scan */ 1133 1134 w += sldns_str_print(s, slen, "=\""); 1135 while (data_len) { 1136 /* alpn is list of length byte (str_len) followed by a string of that size */ 1137 uint8_t i, str_len = *dp++; 1138 1139 if (str_len > --data_len) 1140 return -1; 1141 1142 for (i = 0; i < str_len; i++) { 1143 if (dp[i] == '"' || dp[i] == '\\') 1144 w += sldns_str_print(s, slen, "\\\\\\%c", dp[i]); 1145 1146 else if (dp[i] == ',') 1147 w += sldns_str_print(s, slen, "\\\\%c", dp[i]); 1148 1149 else if (!isprint(dp[i])) 1150 w += sldns_str_print(s, slen, "\\%03u", (unsigned) dp[i]); 1151 1152 else 1153 w += sldns_str_print(s, slen, "%c", dp[i]); 1154 } 1155 dp += str_len; 1156 if ((data_len -= str_len)) 1157 w += sldns_str_print(s, slen, "%s", ","); 1158 } 1159 w += sldns_str_print(s, slen, "\""); 1160 1161 return w; 1162 } 1163 1164 static int sldns_wire2str_svcparam_ech2str(char** s, 1165 size_t* slen, uint16_t data_len, uint8_t* data) 1166 { 1167 int size; 1168 int w = 0; 1169 1170 assert(data_len > 0); /* Guaranteed by sldns_wire2str_svcparam_scan */ 1171 1172 w += sldns_str_print(s, slen, "=\""); 1173 1174 if ((size = sldns_b64_ntop(data, data_len, *s, *slen)) < 0) 1175 return -1; 1176 1177 (*s) += size; 1178 (*slen) -= size; 1179 1180 w += sldns_str_print(s, slen, "\""); 1181 1182 return w + size; 1183 } 1184 1185 int sldns_wire2str_svcparam_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen) 1186 { 1187 uint8_t ch; 1188 uint16_t svcparamkey, data_len; 1189 int written_chars = 0; 1190 int r, i; 1191 1192 /* verify that we have enough data to read svcparamkey and data_len */ 1193 if(*dlen < 4) 1194 return -1; 1195 1196 svcparamkey = sldns_read_uint16(*d); 1197 data_len = sldns_read_uint16(*d+2); 1198 *d += 4; 1199 *dlen -= 4; 1200 1201 /* verify that we have data_len data */ 1202 if (data_len > *dlen) 1203 return -1; 1204 1205 written_chars += sldns_print_svcparamkey(s, slen, svcparamkey); 1206 if (!data_len) { 1207 1208 /* Some SvcParams MUST have values */ 1209 switch (svcparamkey) { 1210 case SVCB_KEY_ALPN: 1211 case SVCB_KEY_PORT: 1212 case SVCB_KEY_IPV4HINT: 1213 case SVCB_KEY_IPV6HINT: 1214 case SVCB_KEY_MANDATORY: 1215 case SVCB_KEY_DOHPATH: 1216 return -1; 1217 default: 1218 return written_chars; 1219 } 1220 } 1221 1222 switch (svcparamkey) { 1223 case SVCB_KEY_PORT: 1224 r = sldns_wire2str_svcparam_port2str(s, slen, data_len, *d); 1225 break; 1226 case SVCB_KEY_IPV4HINT: 1227 r = sldns_wire2str_svcparam_ipv4hint2str(s, slen, data_len, *d); 1228 break; 1229 case SVCB_KEY_IPV6HINT: 1230 r = sldns_wire2str_svcparam_ipv6hint2str(s, slen, data_len, *d); 1231 break; 1232 case SVCB_KEY_MANDATORY: 1233 r = sldns_wire2str_svcparam_mandatory2str(s, slen, data_len, *d); 1234 break; 1235 case SVCB_KEY_NO_DEFAULT_ALPN: 1236 return -1; /* wireformat error, should not have a value */ 1237 case SVCB_KEY_ALPN: 1238 r = sldns_wire2str_svcparam_alpn2str(s, slen, data_len, *d); 1239 break; 1240 case SVCB_KEY_ECH: 1241 r = sldns_wire2str_svcparam_ech2str(s, slen, data_len, *d); 1242 break; 1243 case SVCB_KEY_DOHPATH: 1244 ATTR_FALLTHROUGH 1245 /* fallthrough */ 1246 default: 1247 r = sldns_str_print(s, slen, "=\""); 1248 1249 for (i = 0; i < data_len; i++) { 1250 ch = (*d)[i]; 1251 1252 if (ch == '"' || ch == '\\') 1253 r += sldns_str_print(s, slen, "\\%c", ch); 1254 1255 else if (!isprint(ch)) 1256 r += sldns_str_print(s, slen, "\\%03u", (unsigned) ch); 1257 1258 else 1259 r += sldns_str_print(s, slen, "%c", ch); 1260 1261 } 1262 r += sldns_str_print(s, slen, "\""); 1263 break; 1264 } 1265 if (r <= 0) 1266 return -1; /* wireformat error */ 1267 1268 written_chars += r; 1269 *d += data_len; 1270 *dlen -= data_len; 1271 return written_chars; 1272 } 1273 1274 int sldns_wire2str_rdf_scan(uint8_t** d, size_t* dlen, char** s, size_t* slen, 1275 int rdftype, uint8_t* pkt, size_t pktlen, int* comprloop) 1276 { 1277 if(*dlen == 0) return 0; 1278 switch(rdftype) { 1279 case LDNS_RDF_TYPE_NONE: 1280 return 0; 1281 case LDNS_RDF_TYPE_DNAME: 1282 return sldns_wire2str_dname_scan(d, dlen, s, slen, pkt, pktlen, comprloop); 1283 case LDNS_RDF_TYPE_INT8: 1284 return sldns_wire2str_int8_scan(d, dlen, s, slen); 1285 case LDNS_RDF_TYPE_INT16: 1286 return sldns_wire2str_int16_scan(d, dlen, s, slen); 1287 case LDNS_RDF_TYPE_INT32: 1288 return sldns_wire2str_int32_scan(d, dlen, s, slen); 1289 case LDNS_RDF_TYPE_PERIOD: 1290 return sldns_wire2str_period_scan(d, dlen, s, slen); 1291 case LDNS_RDF_TYPE_TSIGTIME: 1292 return sldns_wire2str_tsigtime_scan(d, dlen, s, slen); 1293 case LDNS_RDF_TYPE_A: 1294 return sldns_wire2str_a_scan(d, dlen, s, slen); 1295 case LDNS_RDF_TYPE_AAAA: 1296 return sldns_wire2str_aaaa_scan(d, dlen, s, slen); 1297 case LDNS_RDF_TYPE_STR: 1298 return sldns_wire2str_str_scan(d, dlen, s, slen); 1299 case LDNS_RDF_TYPE_APL: 1300 return sldns_wire2str_apl_scan(d, dlen, s, slen); 1301 case LDNS_RDF_TYPE_B32_EXT: 1302 return sldns_wire2str_b32_ext_scan(d, dlen, s, slen); 1303 case LDNS_RDF_TYPE_B64: 1304 return sldns_wire2str_b64_scan(d, dlen, s, slen); 1305 case LDNS_RDF_TYPE_HEX: 1306 return sldns_wire2str_hex_scan(d, dlen, s, slen); 1307 case LDNS_RDF_TYPE_NSEC: 1308 return sldns_wire2str_nsec_scan(d, dlen, s, slen); 1309 case LDNS_RDF_TYPE_NSEC3_SALT: 1310 return sldns_wire2str_nsec3_salt_scan(d, dlen, s, slen); 1311 case LDNS_RDF_TYPE_TYPE: 1312 return sldns_wire2str_type_scan(d, dlen, s, slen); 1313 case LDNS_RDF_TYPE_CLASS: 1314 return sldns_wire2str_class_scan(d, dlen, s, slen); 1315 case LDNS_RDF_TYPE_CERT_ALG: 1316 return sldns_wire2str_cert_alg_scan(d, dlen, s, slen); 1317 case LDNS_RDF_TYPE_ALG: 1318 return sldns_wire2str_alg_scan(d, dlen, s, slen); 1319 case LDNS_RDF_TYPE_UNKNOWN: 1320 return sldns_wire2str_unknown_scan(d, dlen, s, slen); 1321 case LDNS_RDF_TYPE_TIME: 1322 return sldns_wire2str_time_scan(d, dlen, s, slen); 1323 case LDNS_RDF_TYPE_LOC: 1324 return sldns_wire2str_loc_scan(d, dlen, s, slen); 1325 case LDNS_RDF_TYPE_WKS: 1326 case LDNS_RDF_TYPE_SERVICE: 1327 return sldns_wire2str_wks_scan(d, dlen, s, slen); 1328 case LDNS_RDF_TYPE_NSAP: 1329 return sldns_wire2str_nsap_scan(d, dlen, s, slen); 1330 case LDNS_RDF_TYPE_ATMA: 1331 return sldns_wire2str_atma_scan(d, dlen, s, slen); 1332 case LDNS_RDF_TYPE_IPSECKEY: 1333 return sldns_wire2str_ipseckey_scan(d, dlen, s, slen, pkt, 1334 pktlen, comprloop); 1335 case LDNS_RDF_TYPE_HIP: 1336 return sldns_wire2str_hip_scan(d, dlen, s, slen); 1337 case LDNS_RDF_TYPE_INT16_DATA: 1338 return sldns_wire2str_int16_data_scan(d, dlen, s, slen); 1339 case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER: 1340 return sldns_wire2str_b32_ext_scan(d, dlen, s, slen); 1341 case LDNS_RDF_TYPE_ILNP64: 1342 return sldns_wire2str_ilnp64_scan(d, dlen, s, slen); 1343 case LDNS_RDF_TYPE_EUI48: 1344 return sldns_wire2str_eui48_scan(d, dlen, s, slen); 1345 case LDNS_RDF_TYPE_EUI64: 1346 return sldns_wire2str_eui64_scan(d, dlen, s, slen); 1347 case LDNS_RDF_TYPE_TAG: 1348 return sldns_wire2str_tag_scan(d, dlen, s, slen); 1349 case LDNS_RDF_TYPE_LONG_STR: 1350 return sldns_wire2str_long_str_scan(d, dlen, s, slen); 1351 case LDNS_RDF_TYPE_SVCPARAM: 1352 return sldns_wire2str_svcparam_scan(d, dlen, s, slen); 1353 case LDNS_RDF_TYPE_TSIGERROR: 1354 return sldns_wire2str_tsigerror_scan(d, dlen, s, slen); 1355 } 1356 /* unknown rdf type */ 1357 return -1; 1358 } 1359 1360 int sldns_wire2str_int8_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1361 { 1362 int w; 1363 if(*dl < 1) return -1; 1364 w = sldns_str_print(s, sl, "%u", (unsigned)**d); 1365 (*d)++; 1366 (*dl)--; 1367 return w; 1368 } 1369 1370 int sldns_wire2str_int16_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1371 { 1372 int w; 1373 if(*dl < 2) return -1; 1374 w = sldns_str_print(s, sl, "%lu", (unsigned long)sldns_read_uint16(*d)); 1375 (*d)+=2; 1376 (*dl)-=2; 1377 return w; 1378 } 1379 1380 int sldns_wire2str_int32_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1381 { 1382 int w; 1383 if(*dl < 4) return -1; 1384 w = sldns_str_print(s, sl, "%lu", (unsigned long)sldns_read_uint32(*d)); 1385 (*d)+=4; 1386 (*dl)-=4; 1387 return w; 1388 } 1389 1390 int sldns_wire2str_period_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1391 { 1392 int w; 1393 if(*dl < 4) return -1; 1394 w = sldns_str_print(s, sl, "%u", (unsigned)sldns_read_uint32(*d)); 1395 (*d)+=4; 1396 (*dl)-=4; 1397 return w; 1398 } 1399 1400 int sldns_wire2str_tsigtime_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1401 { 1402 /* tsigtime is 48 bits network order unsigned integer */ 1403 int w; 1404 uint64_t tsigtime = 0; 1405 uint64_t d0, d1, d2, d3, d4, d5; 1406 if(*dl < 6) return -1; 1407 d0 = (*d)[0]; /* cast to uint64 for shift operations */ 1408 d1 = (*d)[1]; 1409 d2 = (*d)[2]; 1410 d3 = (*d)[3]; 1411 d4 = (*d)[4]; 1412 d5 = (*d)[5]; 1413 tsigtime = (d0<<40) | (d1<<32) | (d2<<24) | (d3<<16) | (d4<<8) | d5; 1414 #ifndef USE_WINSOCK 1415 w = sldns_str_print(s, sl, "%llu", (long long)tsigtime); 1416 #else 1417 w = sldns_str_print(s, sl, "%I64u", (long long)tsigtime); 1418 #endif 1419 (*d)+=6; 1420 (*dl)-=6; 1421 return w; 1422 } 1423 1424 int sldns_wire2str_a_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1425 { 1426 char buf[32]; 1427 int w; 1428 if(*dl < 4) return -1; 1429 if(!inet_ntop(AF_INET, *d, buf, (socklen_t)sizeof(buf))) 1430 return -1; 1431 w = sldns_str_print(s, sl, "%s", buf); 1432 (*d)+=4; 1433 (*dl)-=4; 1434 return w; 1435 } 1436 1437 int sldns_wire2str_aaaa_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1438 { 1439 #ifdef AF_INET6 1440 char buf[64]; 1441 int w; 1442 if(*dl < 16) return -1; 1443 if(!inet_ntop(AF_INET6, *d, buf, (socklen_t)sizeof(buf))) 1444 return -1; 1445 w = sldns_str_print(s, sl, "%s", buf); 1446 (*d)+=16; 1447 (*dl)-=16; 1448 return w; 1449 #else 1450 return -1; 1451 #endif 1452 } 1453 1454 /** printout escaped TYPE_STR character */ 1455 static int str_char_print(char** s, size_t* sl, uint8_t c) 1456 { 1457 if(isprint((unsigned char)c) || c == '\t') { 1458 if(c == '\"' || c == '\\') 1459 return sldns_str_print(s, sl, "\\%c", c); 1460 if(*sl) { 1461 **s = (char)c; 1462 (*s)++; 1463 (*sl)--; 1464 } 1465 return 1; 1466 } 1467 return sldns_str_print(s, sl, "\\%03u", (unsigned)c); 1468 } 1469 1470 int sldns_wire2str_str_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1471 { 1472 int w = 0; 1473 size_t i, len; 1474 if(*dl < 1) return -1; 1475 len = **d; 1476 if(*dl < 1+len) return -1; 1477 (*d)++; 1478 (*dl)--; 1479 w += sldns_str_print(s, sl, "\""); 1480 for(i=0; i<len; i++) 1481 w += str_char_print(s, sl, (*d)[i]); 1482 w += sldns_str_print(s, sl, "\""); 1483 (*d)+=len; 1484 (*dl)-=len; 1485 return w; 1486 } 1487 1488 int sldns_wire2str_apl_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1489 { 1490 int i, w = 0; 1491 uint16_t family; 1492 uint8_t negation, prefix, adflength; 1493 if(*dl < 4) return -1; 1494 family = sldns_read_uint16(*d); 1495 prefix = (*d)[2]; 1496 negation = ((*d)[3] & LDNS_APL_NEGATION); 1497 adflength = ((*d)[3] & LDNS_APL_MASK); 1498 if(*dl < 4+(size_t)adflength) return -1; 1499 if(family != LDNS_APL_IP4 && family != LDNS_APL_IP6) 1500 return -1; /* unknown address family */ 1501 if(negation) 1502 w += sldns_str_print(s, sl, "!"); 1503 w += sldns_str_print(s, sl, "%u:", (unsigned)family); 1504 if(family == LDNS_APL_IP4) { 1505 /* check if prefix <32 ? */ 1506 /* address is variable length 0 - 4 */ 1507 for(i=0; i<4; i++) { 1508 if(i > 0) 1509 w += sldns_str_print(s, sl, "."); 1510 if(i < (int)adflength) 1511 w += sldns_str_print(s, sl, "%d", (*d)[4+i]); 1512 else w += sldns_str_print(s, sl, "0"); 1513 } 1514 } else if(family == LDNS_APL_IP6) { 1515 /* check if prefix <128 ? */ 1516 /* address is variable length 0 - 16 */ 1517 for(i=0; i<16; i++) { 1518 if(i%2 == 0 && i>0) 1519 w += sldns_str_print(s, sl, ":"); 1520 if(i < (int)adflength) 1521 w += sldns_str_print(s, sl, "%02x", (*d)[4+i]); 1522 else w += sldns_str_print(s, sl, "00"); 1523 } 1524 } 1525 w += sldns_str_print(s, sl, "/%u", (unsigned)prefix); 1526 (*d) += 4+adflength; 1527 (*dl) -= 4+adflength; 1528 return w; 1529 } 1530 1531 int sldns_wire2str_b32_ext_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1532 { 1533 size_t datalen; 1534 size_t sz; 1535 if(*dl < 1) return -1; 1536 datalen = (*d)[0]; 1537 if(*dl < 1+datalen) return -1; 1538 sz = sldns_b32_ntop_calculate_size(datalen); 1539 if(*sl < sz+1) { 1540 (*d) += datalen+1; 1541 (*dl) -= (datalen+1); 1542 return (int)sz; /* out of space really, but would need buffer 1543 in order to truncate the output */ 1544 } 1545 sldns_b32_ntop_extended_hex((*d)+1, datalen, *s, *sl); 1546 (*d) += datalen+1; 1547 (*dl) -= (datalen+1); 1548 (*s) += sz; 1549 (*sl) -= sz; 1550 return (int)sz; 1551 } 1552 1553 /** scan number of bytes from wire into b64 presentation format */ 1554 static int sldns_wire2str_b64_scan_num(uint8_t** d, size_t* dl, char** s, 1555 size_t* sl, size_t num) 1556 { 1557 /* b64_ntop_calculate size includes null at the end */ 1558 size_t sz = sldns_b64_ntop_calculate_size(num)-1; 1559 if(*sl < sz+1) { 1560 (*d) += num; 1561 (*dl) -= num; 1562 return (int)sz; /* out of space really, but would need buffer 1563 in order to truncate the output */ 1564 } 1565 sldns_b64_ntop(*d, num, *s, *sl); 1566 (*d) += num; 1567 (*dl) -= num; 1568 (*s) += sz; 1569 (*sl) -= sz; 1570 return (int)sz; 1571 } 1572 1573 int sldns_wire2str_b64_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1574 { 1575 if(*dl == 0) { 1576 return sldns_str_print(s, sl, "0"); 1577 } 1578 return sldns_wire2str_b64_scan_num(d, dl, s, sl, *dl); 1579 } 1580 1581 int sldns_wire2str_hex_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1582 { 1583 if(*dl == 0) { 1584 return sldns_str_print(s, sl, "0"); 1585 } 1586 return print_remainder_hex("", d, dl, s, sl); 1587 } 1588 1589 int sldns_wire2str_nsec_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1590 { 1591 uint8_t* p = *d; 1592 size_t pl = *dl; 1593 unsigned i, bit, window, block_len; 1594 uint16_t t; 1595 int w = 0; 1596 1597 /* check for errors */ 1598 while(pl) { 1599 if(pl < 2) return -1; 1600 block_len = (unsigned)p[1]; 1601 if(pl < 2+block_len) return -1; 1602 p += block_len+2; 1603 pl -= block_len+2; 1604 } 1605 1606 /* do it */ 1607 p = *d; 1608 pl = *dl; 1609 while(pl) { 1610 if(pl < 2) return -1; /* cannot happen */ 1611 window = (unsigned)p[0]; 1612 block_len = (unsigned)p[1]; 1613 if(pl < 2+block_len) return -1; /* cannot happen */ 1614 p += 2; 1615 for(i=0; i<block_len; i++) { 1616 if(p[i] == 0) continue; 1617 /* base type number for this octet */ 1618 t = ((window)<<8) | (i << 3); 1619 for(bit=0; bit<8; bit++) { 1620 if((p[i]&(0x80>>bit))) { 1621 if(w) w += sldns_str_print(s, sl, " "); 1622 w += sldns_wire2str_type_print(s, sl, 1623 t+bit); 1624 } 1625 } 1626 } 1627 p += block_len; 1628 pl -= block_len+2; 1629 } 1630 (*d) += *dl; 1631 (*dl) = 0; 1632 return w; 1633 } 1634 1635 int sldns_wire2str_nsec3_salt_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1636 { 1637 size_t salt_len; 1638 int w; 1639 if(*dl < 1) return -1; 1640 salt_len = (size_t)(*d)[0]; 1641 if(*dl < 1+salt_len) return -1; 1642 (*d)++; 1643 (*dl)--; 1644 if(salt_len == 0) { 1645 return sldns_str_print(s, sl, "-"); 1646 } 1647 w = print_hex_buf(s, sl, *d, salt_len); 1648 (*dl)-=salt_len; 1649 (*d)+=salt_len; 1650 return w; 1651 } 1652 1653 int sldns_wire2str_cert_alg_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1654 { 1655 sldns_lookup_table *lt; 1656 int data, w; 1657 if(*dl < 2) return -1; 1658 data = (int)sldns_read_uint16(*d); 1659 lt = sldns_lookup_by_id(sldns_cert_algorithms, data); 1660 if(lt && lt->name) 1661 w = sldns_str_print(s, sl, "%s", lt->name); 1662 else w = sldns_str_print(s, sl, "%d", data); 1663 (*dl)-=2; 1664 (*d)+=2; 1665 return w; 1666 } 1667 1668 int sldns_wire2str_alg_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1669 { 1670 /* don't use algorithm mnemonics in the presentation format 1671 * this kind of got sneaked into the rfc's */ 1672 return sldns_wire2str_int8_scan(d, dl, s, sl); 1673 } 1674 1675 int sldns_wire2str_unknown_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1676 { 1677 return sldns_wire2str_rdata_unknown_scan(d, dl, s, sl); 1678 } 1679 1680 int sldns_wire2str_time_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1681 { 1682 /* create a YYYYMMDDHHMMSS string if possible */ 1683 struct tm tm; 1684 char date_buf[16]; 1685 uint32_t t; 1686 memset(&tm, 0, sizeof(tm)); 1687 if(*dl < 4) return -1; 1688 t = sldns_read_uint32(*d); 1689 date_buf[15]=0; 1690 if(sldns_serial_arithmetics_gmtime_r(t, time(NULL), &tm) && 1691 strftime(date_buf, 15, "%Y%m%d%H%M%S", &tm)) { 1692 (*d) += 4; 1693 (*dl) -= 4; 1694 return sldns_str_print(s, sl, "%s", date_buf); 1695 } 1696 return -1; 1697 } 1698 1699 static int 1700 loc_cm_print(char** str, size_t* sl, uint8_t mantissa, uint8_t exponent) 1701 { 1702 int w = 0; 1703 uint8_t i; 1704 /* is it 0.<two digits> ? */ 1705 if(exponent < 2) { 1706 if(exponent == 1) 1707 mantissa *= 10; 1708 return sldns_str_print(str, sl, "0.%02ld", (long)mantissa); 1709 } 1710 /* always <digit><string of zeros> */ 1711 w += sldns_str_print(str, sl, "%d", (int)mantissa); 1712 for(i=0; i<exponent-2; i++) 1713 w += sldns_str_print(str, sl, "0"); 1714 return w; 1715 } 1716 1717 int sldns_wire2str_loc_scan(uint8_t** d, size_t* dl, char** str, size_t* sl) 1718 { 1719 /* we could do checking (ie degrees < 90 etc)? */ 1720 uint8_t version; 1721 uint8_t size; 1722 uint8_t horizontal_precision; 1723 uint8_t vertical_precision; 1724 uint32_t longitude; 1725 uint32_t latitude; 1726 uint32_t altitude; 1727 char northerness; 1728 char easterness; 1729 uint32_t h; 1730 uint32_t m; 1731 double s; 1732 uint32_t equator = (uint32_t)1 << 31; /* 2**31 */ 1733 int w = 0; 1734 1735 if(*dl < 16) return -1; 1736 version = (*d)[0]; 1737 if(version != 0) 1738 return sldns_wire2str_hex_scan(d, dl, str, sl); 1739 size = (*d)[1]; 1740 horizontal_precision = (*d)[2]; 1741 vertical_precision = (*d)[3]; 1742 1743 latitude = sldns_read_uint32((*d)+4); 1744 longitude = sldns_read_uint32((*d)+8); 1745 altitude = sldns_read_uint32((*d)+12); 1746 1747 if (latitude > equator) { 1748 northerness = 'N'; 1749 latitude = latitude - equator; 1750 } else { 1751 northerness = 'S'; 1752 latitude = equator - latitude; 1753 } 1754 h = latitude / (1000 * 60 * 60); 1755 latitude = latitude % (1000 * 60 * 60); 1756 m = latitude / (1000 * 60); 1757 latitude = latitude % (1000 * 60); 1758 s = (double) latitude / 1000.0; 1759 w += sldns_str_print(str, sl, "%02u %02u %06.3f %c ", 1760 h, m, s, northerness); 1761 1762 if (longitude > equator) { 1763 easterness = 'E'; 1764 longitude = longitude - equator; 1765 } else { 1766 easterness = 'W'; 1767 longitude = equator - longitude; 1768 } 1769 h = longitude / (1000 * 60 * 60); 1770 longitude = longitude % (1000 * 60 * 60); 1771 m = longitude / (1000 * 60); 1772 longitude = longitude % (1000 * 60); 1773 s = (double) longitude / (1000.0); 1774 w += sldns_str_print(str, sl, "%02u %02u %06.3f %c ", 1775 h, m, s, easterness); 1776 1777 s = ((double) altitude) / 100; 1778 s -= 100000; 1779 1780 if(altitude%100 != 0) 1781 w += sldns_str_print(str, sl, "%.2f", s); 1782 else 1783 w += sldns_str_print(str, sl, "%.0f", s); 1784 1785 w += sldns_str_print(str, sl, "m "); 1786 1787 w += loc_cm_print(str, sl, (size & 0xf0) >> 4, size & 0x0f); 1788 w += sldns_str_print(str, sl, "m "); 1789 1790 w += loc_cm_print(str, sl, (horizontal_precision & 0xf0) >> 4, 1791 horizontal_precision & 0x0f); 1792 w += sldns_str_print(str, sl, "m "); 1793 1794 w += loc_cm_print(str, sl, (vertical_precision & 0xf0) >> 4, 1795 vertical_precision & 0x0f); 1796 w += sldns_str_print(str, sl, "m"); 1797 1798 (*d)+=16; 1799 (*dl)-=16; 1800 return w; 1801 } 1802 1803 int sldns_wire2str_wks_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1804 { 1805 /* protocol, followed by bitmap of services */ 1806 const char* proto_name = NULL; 1807 struct protoent *protocol; 1808 struct servent *service; 1809 uint8_t protocol_nr; 1810 int bit, port, w = 0; 1811 size_t i; 1812 /* we cannot print with strings because they 1813 * are not portable, the presentation format may 1814 * not be able to be read in on another computer. */ 1815 int print_symbols = 0; 1816 1817 /* protocol */ 1818 if(*dl < 1) return -1; 1819 protocol_nr = (*d)[0]; 1820 (*d)++; 1821 (*dl)--; 1822 protocol = getprotobynumber((int)protocol_nr); 1823 if(protocol && (protocol->p_name != NULL)) { 1824 w += sldns_str_print(s, sl, "%s", protocol->p_name); 1825 proto_name = protocol->p_name; 1826 } else if(protocol_nr == 6) { 1827 w += sldns_str_print(s, sl, "tcp"); 1828 } else if(protocol_nr == 17) { 1829 w += sldns_str_print(s, sl, "udp"); 1830 } else { 1831 w += sldns_str_print(s, sl, "%u", (unsigned)protocol_nr); 1832 } 1833 1834 for(i=0; i<*dl; i++) { 1835 if((*d)[i] == 0) 1836 continue; 1837 for(bit=0; bit<8; bit++) { 1838 if(!(((*d)[i])&(0x80>>bit))) 1839 continue; 1840 port = (int)i*8 + bit; 1841 1842 if(!print_symbols) 1843 service = NULL; 1844 else 1845 service = getservbyport( 1846 (int)htons((uint16_t)port), proto_name); 1847 if(service && service->s_name) 1848 w += sldns_str_print(s, sl, " %s", 1849 service->s_name); 1850 else w += sldns_str_print(s, sl, " %u", 1851 (unsigned)port); 1852 } 1853 } 1854 1855 #ifdef HAVE_ENDSERVENT 1856 endservent(); 1857 #endif 1858 #ifdef HAVE_ENDPROTOENT 1859 endprotoent(); 1860 #endif 1861 (*d) += *dl; 1862 (*dl) = 0; 1863 return w; 1864 } 1865 1866 int sldns_wire2str_nsap_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1867 { 1868 return print_remainder_hex("0x", d, dl, s, sl); 1869 } 1870 1871 int sldns_wire2str_atma_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1872 { 1873 return print_remainder_hex("", d, dl, s, sl); 1874 } 1875 1876 /* internal scan routine that can modify arguments on failure */ 1877 static int sldns_wire2str_ipseckey_scan_internal(uint8_t** d, size_t* dl, 1878 char** s, size_t* sl, uint8_t* pkt, size_t pktlen, int* comprloop) 1879 { 1880 /* http://www.ietf.org/internet-drafts/draft-ietf-ipseckey-rr-12.txt*/ 1881 uint8_t precedence, gateway_type, algorithm; 1882 int w = 0; 1883 1884 if(*dl < 3) return -1; 1885 precedence = (*d)[0]; 1886 gateway_type = (*d)[1]; 1887 algorithm = (*d)[2]; 1888 if(gateway_type > 3) 1889 return -1; /* unknown */ 1890 (*d)+=3; 1891 (*dl)-=3; 1892 w += sldns_str_print(s, sl, "%d %d %d ", 1893 (int)precedence, (int)gateway_type, (int)algorithm); 1894 1895 switch(gateway_type) { 1896 case 0: /* no gateway */ 1897 w += sldns_str_print(s, sl, "."); 1898 break; 1899 case 1: /* ip4 */ 1900 w += sldns_wire2str_a_scan(d, dl, s, sl); 1901 break; 1902 case 2: /* ip6 */ 1903 w += sldns_wire2str_aaaa_scan(d, dl, s, sl); 1904 break; 1905 case 3: /* dname */ 1906 w += sldns_wire2str_dname_scan(d, dl, s, sl, pkt, pktlen, comprloop); 1907 break; 1908 default: /* unknown */ 1909 return -1; 1910 } 1911 1912 if(*dl < 1) 1913 return -1; 1914 w += sldns_str_print(s, sl, " "); 1915 w += sldns_wire2str_b64_scan_num(d, dl, s, sl, *dl); 1916 return w; 1917 } 1918 1919 int sldns_wire2str_ipseckey_scan(uint8_t** d, size_t* dl, char** s, size_t* sl, 1920 uint8_t* pkt, size_t pktlen, int* comprloop) 1921 { 1922 uint8_t* od = *d; 1923 char* os = *s; 1924 size_t odl = *dl, osl = *sl; 1925 int w=sldns_wire2str_ipseckey_scan_internal(d, dl, s, sl, pkt, pktlen, comprloop); 1926 if(w == -1) { 1927 *d = od; 1928 *s = os; 1929 *dl = odl; 1930 *sl = osl; 1931 return -1; 1932 } 1933 return w; 1934 } 1935 1936 int sldns_wire2str_hip_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1937 { 1938 int w; 1939 uint8_t algo, hitlen; 1940 uint16_t pklen; 1941 1942 /* read lengths */ 1943 if(*dl < 4) 1944 return -1; 1945 hitlen = (*d)[0]; 1946 algo = (*d)[1]; 1947 pklen = sldns_read_uint16((*d)+2); 1948 if(*dl < (size_t)4 + (size_t)hitlen + (size_t)pklen) 1949 return -1; 1950 1951 /* write: algo hit pubkey */ 1952 w = sldns_str_print(s, sl, "%u ", (unsigned)algo); 1953 w += print_hex_buf(s, sl, (*d)+4, hitlen); 1954 w += sldns_str_print(s, sl, " "); 1955 (*d)+=4+hitlen; 1956 (*dl)-= (4+hitlen); 1957 w += sldns_wire2str_b64_scan_num(d, dl, s, sl, pklen); 1958 return w; 1959 } 1960 1961 int sldns_wire2str_int16_data_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1962 { 1963 int w; 1964 uint16_t n; 1965 if(*dl < 2) 1966 return -1; 1967 n = sldns_read_uint16(*d); 1968 if(*dl < 2+(size_t)n) 1969 return -1; 1970 (*d)+=2; 1971 (*dl)-=2; 1972 if(n == 0) { 1973 return sldns_str_print(s, sl, "0"); 1974 } 1975 w = sldns_str_print(s, sl, "%u ", (unsigned)n); 1976 w += sldns_wire2str_b64_scan_num(d, dl, s, sl, n); 1977 return w; 1978 } 1979 1980 int sldns_wire2str_nsec3_next_owner_scan(uint8_t** d, size_t* dl, char** s, 1981 size_t* sl) 1982 { 1983 return sldns_wire2str_b32_ext_scan(d, dl, s, sl); 1984 } 1985 1986 int sldns_wire2str_ilnp64_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 1987 { 1988 int w; 1989 if(*dl < 8) 1990 return -1; 1991 w = sldns_str_print(s, sl, "%.4x:%.4x:%.4x:%.4x", 1992 sldns_read_uint16(*d), sldns_read_uint16((*d)+2), 1993 sldns_read_uint16((*d)+4), sldns_read_uint16((*d)+6)); 1994 (*d)+=8; 1995 (*dl)-=8; 1996 return w; 1997 } 1998 1999 int sldns_wire2str_eui48_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 2000 { 2001 int w; 2002 if(*dl < 6) 2003 return -1; 2004 w = sldns_str_print(s, sl, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", 2005 (*d)[0], (*d)[1], (*d)[2], (*d)[3], (*d)[4], (*d)[5]); 2006 (*d)+=6; 2007 (*dl)-=6; 2008 return w; 2009 } 2010 2011 int sldns_wire2str_eui64_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 2012 { 2013 int w; 2014 if(*dl < 8) 2015 return -1; 2016 w = sldns_str_print(s, sl, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x", 2017 (*d)[0], (*d)[1], (*d)[2], (*d)[3], (*d)[4], (*d)[5], 2018 (*d)[6], (*d)[7]); 2019 (*d)+=8; 2020 (*dl)-=8; 2021 return w; 2022 } 2023 2024 int sldns_wire2str_tag_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 2025 { 2026 size_t i, n; 2027 int w = 0; 2028 if(*dl < 1) 2029 return -1; 2030 n = (size_t)((*d)[0]); 2031 if(*dl < 1+n) 2032 return -1; 2033 for(i=0; i<n; i++) 2034 if(!isalnum((unsigned char)(*d)[i+1])) 2035 return -1; 2036 for(i=0; i<n; i++) 2037 w += sldns_str_print(s, sl, "%c", (char)(*d)[i+1]); 2038 (*d)+=n+1; 2039 (*dl)-=(n+1); 2040 return w; 2041 } 2042 2043 int sldns_wire2str_long_str_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 2044 { 2045 size_t i; 2046 int w = 0; 2047 w += sldns_str_print(s, sl, "\""); 2048 for(i=0; i<*dl; i++) 2049 w += str_char_print(s, sl, (*d)[i]); 2050 w += sldns_str_print(s, sl, "\""); 2051 (*d)+=*dl; 2052 (*dl)=0; 2053 return w; 2054 } 2055 2056 int sldns_wire2str_tsigerror_scan(uint8_t** d, size_t* dl, char** s, size_t* sl) 2057 { 2058 sldns_lookup_table *lt; 2059 int data, w; 2060 if(*dl < 2) return -1; 2061 data = (int)sldns_read_uint16(*d); 2062 lt = sldns_lookup_by_id(sldns_tsig_errors, data); 2063 if(lt && lt->name) 2064 w = sldns_str_print(s, sl, "%s", lt->name); 2065 else w = sldns_str_print(s, sl, "%d", data); 2066 (*dl)-=2; 2067 (*d)+=2; 2068 return w; 2069 } 2070 2071 int sldns_wire2str_edns_llq_print(char** s, size_t* sl, uint8_t* data, 2072 size_t len) 2073 { 2074 /* LLQ constants */ 2075 const char* llq_errors[] = {"NO-ERROR", "SERV-FULL", "STATIC", 2076 "FORMAT-ERR", "NO-SUCH-LLQ", "BAD-VERS", "UNKNOWN_ERR"}; 2077 const unsigned int llq_errors_num = 7; 2078 const char* llq_opcodes[] = {"LLQ-SETUP", "LLQ-REFRESH", "LLQ-EVENT"}; 2079 const unsigned int llq_opcodes_num = 3; 2080 uint16_t version, llq_opcode, error_code; 2081 uint64_t llq_id; 2082 uint32_t lease_life; /* Requested or granted life of LLQ, in seconds */ 2083 int w = 0; 2084 2085 /* read the record */ 2086 if(len != 18) { 2087 w += sldns_str_print(s, sl, "malformed LLQ "); 2088 w += print_hex_buf(s, sl, data, len); 2089 return w; 2090 } 2091 version = sldns_read_uint16(data); 2092 llq_opcode = sldns_read_uint16(data+2); 2093 error_code = sldns_read_uint16(data+4); 2094 memmove(&llq_id, data+6, sizeof(llq_id)); 2095 lease_life = sldns_read_uint32(data+14); 2096 2097 /* print it */ 2098 w += sldns_str_print(s, sl, "v%d ", (int)version); 2099 if(llq_opcode < llq_opcodes_num) 2100 w += sldns_str_print(s, sl, "%s", llq_opcodes[llq_opcode]); 2101 else w += sldns_str_print(s, sl, "opcode %d", (int)llq_opcode); 2102 if(error_code < llq_errors_num) 2103 w += sldns_str_print(s, sl, " %s", llq_errors[error_code]); 2104 else w += sldns_str_print(s, sl, " error %d", (int)error_code); 2105 #ifndef USE_WINSOCK 2106 w += sldns_str_print(s, sl, " id %llx lease-life %lu", 2107 (unsigned long long)llq_id, (unsigned long)lease_life); 2108 #else 2109 w += sldns_str_print(s, sl, " id %I64x lease-life %lu", 2110 (unsigned long long)llq_id, (unsigned long)lease_life); 2111 #endif 2112 return w; 2113 } 2114 2115 int sldns_wire2str_edns_ul_print(char** s, size_t* sl, uint8_t* data, 2116 size_t len) 2117 { 2118 uint32_t lease; 2119 int w = 0; 2120 if(len != 4) { 2121 w += sldns_str_print(s, sl, "malformed UL "); 2122 w += print_hex_buf(s, sl, data, len); 2123 return w; 2124 } 2125 lease = sldns_read_uint32(data); 2126 w += sldns_str_print(s, sl, "lease %lu", (unsigned long)lease); 2127 return w; 2128 } 2129 2130 int sldns_wire2str_edns_nsid_print(char** s, size_t* sl, uint8_t* data, 2131 size_t len) 2132 { 2133 int w = 0; 2134 size_t i, printed=0; 2135 w += print_hex_buf(s, sl, data, len); 2136 for(i=0; i<len; i++) { 2137 if(isprint((unsigned char)data[i]) || data[i] == '\t') { 2138 if(!printed) { 2139 w += sldns_str_print(s, sl, " ("); 2140 printed = 1; 2141 } 2142 w += sldns_str_print(s, sl, "%c", (char)data[i]); 2143 } 2144 } 2145 if(printed) 2146 w += sldns_str_print(s, sl, ")"); 2147 return w; 2148 } 2149 2150 int sldns_wire2str_edns_dau_print(char** s, size_t* sl, uint8_t* data, 2151 size_t len) 2152 { 2153 sldns_lookup_table *lt; 2154 size_t i; 2155 int w = 0; 2156 for(i=0; i<len; i++) { 2157 lt = sldns_lookup_by_id(sldns_algorithms, (int)data[i]); 2158 if(lt && lt->name) 2159 w += sldns_str_print(s, sl, " %s", lt->name); 2160 else w += sldns_str_print(s, sl, " %d", (int)data[i]); 2161 } 2162 return w; 2163 } 2164 2165 int sldns_wire2str_edns_dhu_print(char** s, size_t* sl, uint8_t* data, 2166 size_t len) 2167 { 2168 sldns_lookup_table *lt; 2169 size_t i; 2170 int w = 0; 2171 for(i=0; i<len; i++) { 2172 lt = sldns_lookup_by_id(sldns_hashes, (int)data[i]); 2173 if(lt && lt->name) 2174 w += sldns_str_print(s, sl, " %s", lt->name); 2175 else w += sldns_str_print(s, sl, " %d", (int)data[i]); 2176 } 2177 return w; 2178 } 2179 2180 int sldns_wire2str_edns_n3u_print(char** s, size_t* sl, uint8_t* data, 2181 size_t len) 2182 { 2183 size_t i; 2184 int w = 0; 2185 for(i=0; i<len; i++) { 2186 if(data[i] == 1) 2187 w += sldns_str_print(s, sl, " SHA1"); 2188 else w += sldns_str_print(s, sl, " %d", (int)data[i]); 2189 } 2190 return w; 2191 } 2192 2193 int sldns_wire2str_edns_subnet_print(char** s, size_t* sl, uint8_t* data, 2194 size_t len) 2195 { 2196 int w = 0; 2197 uint16_t family; 2198 uint8_t source, scope; 2199 if(len < 4) { 2200 w += sldns_str_print(s, sl, "malformed subnet "); 2201 w += print_hex_buf(s, sl, data, len); 2202 return w; 2203 } 2204 family = sldns_read_uint16(data); 2205 source = data[2]; 2206 scope = data[3]; 2207 if(family == 1) { 2208 /* IP4 */ 2209 char buf[64]; 2210 uint8_t ip4[4]; 2211 memset(ip4, 0, sizeof(ip4)); 2212 if(len-4 > 4) { 2213 w += sldns_str_print(s, sl, "trailingdata:"); 2214 w += print_hex_buf(s, sl, data+4+4, len-4-4); 2215 w += sldns_str_print(s, sl, " "); 2216 len = 4+4; 2217 } 2218 memmove(ip4, data+4, len-4); 2219 if(!inet_ntop(AF_INET, ip4, buf, (socklen_t)sizeof(buf))) { 2220 w += sldns_str_print(s, sl, "ip4ntoperror "); 2221 w += print_hex_buf(s, sl, data+4+4, len-4-4); 2222 } else { 2223 w += sldns_str_print(s, sl, "%s", buf); 2224 } 2225 } else if(family == 2) { 2226 /* IP6 */ 2227 char buf[64]; 2228 uint8_t ip6[16]; 2229 memset(ip6, 0, sizeof(ip6)); 2230 if(len-4 > 16) { 2231 w += sldns_str_print(s, sl, "trailingdata:"); 2232 w += print_hex_buf(s, sl, data+4+16, len-4-16); 2233 w += sldns_str_print(s, sl, " "); 2234 len = 4+16; 2235 } 2236 memmove(ip6, data+4, len-4); 2237 #ifdef AF_INET6 2238 if(!inet_ntop(AF_INET6, ip6, buf, (socklen_t)sizeof(buf))) { 2239 w += sldns_str_print(s, sl, "ip6ntoperror "); 2240 w += print_hex_buf(s, sl, data+4+4, len-4-4); 2241 } else { 2242 w += sldns_str_print(s, sl, "%s", buf); 2243 } 2244 #else 2245 w += print_hex_buf(s, sl, data+4+4, len-4-4); 2246 #endif 2247 } else { 2248 /* unknown */ 2249 w += sldns_str_print(s, sl, "family %d ", 2250 (int)family); 2251 w += print_hex_buf(s, sl, data, len); 2252 } 2253 w += sldns_str_print(s, sl, "/%d scope /%d", (int)source, (int)scope); 2254 return w; 2255 } 2256 2257 static int sldns_wire2str_edns_keepalive_print(char** s, size_t* sl, 2258 uint8_t* data, size_t len) 2259 { 2260 int w = 0; 2261 uint16_t timeout; 2262 if(!(len == 0 || len == 2)) { 2263 w += sldns_str_print(s, sl, "malformed keepalive "); 2264 w += print_hex_buf(s, sl, data, len); 2265 return w; 2266 } 2267 if(len == 0 ) { 2268 w += sldns_str_print(s, sl, "no timeout value (only valid for client option) "); 2269 } else { 2270 timeout = sldns_read_uint16(data); 2271 w += sldns_str_print(s, sl, "timeout value in units of 100ms %u", (int)timeout); 2272 } 2273 return w; 2274 } 2275 2276 int sldns_wire2str_edns_ede_print(char** s, size_t* sl, 2277 uint8_t* data, size_t len) 2278 { 2279 uint16_t ede_code; 2280 int w = 0; 2281 sldns_lookup_table *lt; 2282 size_t i; 2283 int printable; 2284 2285 if(len < 2) { 2286 w += sldns_str_print(s, sl, "malformed ede "); 2287 w += print_hex_buf(s, sl, data, len); 2288 return w; 2289 } 2290 2291 ede_code = sldns_read_uint16(data); 2292 lt = sldns_lookup_by_id(sldns_edns_ede_codes, (int)ede_code); 2293 if(lt && lt->name) 2294 w += sldns_str_print(s, sl, "%s", lt->name); 2295 else w += sldns_str_print(s, sl, "%d", (int)ede_code); 2296 2297 if(len == 2) 2298 return w; 2299 2300 w += sldns_str_print(s, sl, " "); 2301 2302 /* If it looks like text, show it as text. */ 2303 printable=1; 2304 for(i=2; i<len; i++) { 2305 if(isprint((unsigned char)data[i]) || data[i] == '\t') 2306 continue; 2307 printable = 0; 2308 break; 2309 } 2310 if(printable) { 2311 w += sldns_str_print(s, sl, "\""); 2312 for(i=2; i<len; i++) { 2313 w += str_char_print(s, sl, data[i]); 2314 } 2315 w += sldns_str_print(s, sl, "\""); 2316 } else { 2317 w += print_hex_buf(s, sl, data+2, len-2); 2318 } 2319 return w; 2320 } 2321 2322 int sldns_wire2str_edns_option_print(char** s, size_t* sl, 2323 uint16_t option_code, uint8_t* optdata, size_t optlen) 2324 { 2325 int w = 0; 2326 w += sldns_wire2str_edns_option_code_print(s, sl, option_code); 2327 w += sldns_str_print(s, sl, ": "); 2328 switch(option_code) { 2329 case LDNS_EDNS_LLQ: 2330 w += sldns_wire2str_edns_llq_print(s, sl, optdata, optlen); 2331 break; 2332 case LDNS_EDNS_UL: 2333 w += sldns_wire2str_edns_ul_print(s, sl, optdata, optlen); 2334 break; 2335 case LDNS_EDNS_NSID: 2336 w += sldns_wire2str_edns_nsid_print(s, sl, optdata, optlen); 2337 break; 2338 case LDNS_EDNS_DAU: 2339 w += sldns_wire2str_edns_dau_print(s, sl, optdata, optlen); 2340 break; 2341 case LDNS_EDNS_DHU: 2342 w += sldns_wire2str_edns_dhu_print(s, sl, optdata, optlen); 2343 break; 2344 case LDNS_EDNS_N3U: 2345 w += sldns_wire2str_edns_n3u_print(s, sl, optdata, optlen); 2346 break; 2347 case LDNS_EDNS_CLIENT_SUBNET: 2348 w += sldns_wire2str_edns_subnet_print(s, sl, optdata, optlen); 2349 break; 2350 case LDNS_EDNS_KEEPALIVE: 2351 w += sldns_wire2str_edns_keepalive_print(s, sl, optdata, optlen); 2352 break; 2353 case LDNS_EDNS_PADDING: 2354 w += print_hex_buf(s, sl, optdata, optlen); 2355 break; 2356 case LDNS_EDNS_EDE: 2357 w += sldns_wire2str_edns_ede_print(s, sl, optdata, optlen); 2358 break; 2359 default: 2360 /* unknown option code */ 2361 w += print_hex_buf(s, sl, optdata, optlen); 2362 break; 2363 } 2364 return w; 2365 } 2366 2367 /** print the edns options to string */ 2368 static int 2369 print_edns_opts(char** s, size_t* sl, uint8_t* rdata, size_t rdatalen) 2370 { 2371 uint16_t option_code, option_len; 2372 int w = 0; 2373 while(rdatalen > 0) { 2374 /* option name */ 2375 if(rdatalen < 4) { 2376 w += sldns_str_print(s, sl, " ; malformed: "); 2377 w += print_hex_buf(s, sl, rdata, rdatalen); 2378 return w; 2379 } 2380 option_code = sldns_read_uint16(rdata); 2381 option_len = sldns_read_uint16(rdata+2); 2382 rdata += 4; 2383 rdatalen -= 4; 2384 2385 /* option value */ 2386 if(rdatalen < (size_t)option_len) { 2387 w += sldns_str_print(s, sl, " ; malformed "); 2388 w += sldns_wire2str_edns_option_code_print(s, sl, 2389 option_code); 2390 w += sldns_str_print(s, sl, ": "); 2391 w += print_hex_buf(s, sl, rdata, rdatalen); 2392 return w; 2393 } 2394 w += sldns_str_print(s, sl, " ; "); 2395 w += sldns_wire2str_edns_option_print(s, sl, option_code, 2396 rdata, option_len); 2397 rdata += option_len; 2398 rdatalen -= option_len; 2399 } 2400 return w; 2401 } 2402 2403 int sldns_wire2str_edns_scan(uint8_t** data, size_t* data_len, char** str, 2404 size_t* str_len, uint8_t* pkt, size_t pktlen) 2405 { 2406 int w = 0; 2407 uint8_t ext_rcode, edns_version; 2408 uint16_t udpsize, edns_bits, rdatalen; 2409 w += sldns_str_print(str, str_len, "; EDNS:"); 2410 2411 /* some input checks, domain name */ 2412 if(*data_len < 1+10) 2413 return w + print_remainder_hex("Error malformed 0x", 2414 data, data_len, str, str_len); 2415 if(*data[0] != 0) { 2416 return w + print_remainder_hex("Error nonrootdname 0x", 2417 data, data_len, str, str_len); 2418 } 2419 (*data)++; 2420 (*data_len)--; 2421 2422 /* check type and read fixed contents */ 2423 if(sldns_read_uint16((*data)) != LDNS_RR_TYPE_OPT) { 2424 return w + print_remainder_hex("Error nottypeOPT 0x", 2425 data, data_len, str, str_len); 2426 } 2427 udpsize = sldns_read_uint16((*data)+2); 2428 ext_rcode = (*data)[4]; 2429 edns_version = (*data)[5]; 2430 edns_bits = sldns_read_uint16((*data)+6); 2431 rdatalen = sldns_read_uint16((*data)+8); 2432 (*data)+=10; 2433 (*data_len)-=10; 2434 2435 w += sldns_str_print(str, str_len, " version: %u;", 2436 (unsigned)edns_version); 2437 w += sldns_str_print(str, str_len, " flags:"); 2438 if((edns_bits & LDNS_EDNS_MASK_DO_BIT)) 2439 w += sldns_str_print(str, str_len, " do"); 2440 /* the extended rcode is the value set, shifted four bits, 2441 * and or'd with the original rcode */ 2442 if(ext_rcode) { 2443 int rc = ((int)ext_rcode)<<4; 2444 if(pkt && pktlen >= LDNS_HEADER_SIZE) 2445 rc |= LDNS_RCODE_WIRE(pkt); 2446 w += sldns_str_print(str, str_len, " ; ext-rcode: %d", rc); 2447 } 2448 w += sldns_str_print(str, str_len, " ; udp: %u", (unsigned)udpsize); 2449 2450 if(rdatalen) { 2451 if((size_t)*data_len < rdatalen) { 2452 w += sldns_str_print(str, str_len, 2453 " ; Error EDNS rdata too short; "); 2454 rdatalen = (uint16_t)*data_len; 2455 } 2456 w += print_edns_opts(str, str_len, *data, rdatalen); 2457 (*data) += rdatalen; 2458 (*data_len) -= rdatalen; 2459 } 2460 w += sldns_str_print(str, str_len, "\n"); 2461 return w; 2462 } 2463