root/tools/iphone-sdk/trunk/ldns_sources/host2str.c
@
81
| Revision 32, 42.8 kB (checked in by kjaleel, 5 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * host2str.c |
| 3 | * |
| 4 | * conversion routines from the host format |
| 5 | * to the presentation format (strings) |
| 6 | * |
| 7 | * a Net::DNS like library for C |
| 8 | * |
| 9 | * (c) NLnet Labs, 2004-2006 |
| 10 | * |
| 11 | * See the file LICENSE for the license |
| 12 | */ |
| 13 | #include "ldns/config.h" |
| 14 | |
| 15 | #include "ldns.h" |
| 16 | |
| 17 | #include <limits.h> |
| 18 | |
| 19 | #ifdef HAVE_SYS_SOCKET_H |
| 20 | #include <sys/socket.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_ARPA_INET_H |
| 23 | #include <arpa/inet.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_NETDB_H |
| 26 | #include <netdb.h> |
| 27 | #endif |
| 28 | #include <time.h> |
| 29 | #include <sys/time.h> |
| 30 | |
| 31 | #ifndef INET_ADDRSTRLEN |
| 32 | #define INET_ADDRSTRLEN 16 |
| 33 | #endif |
| 34 | #ifndef INET6_ADDRSTRLEN |
| 35 | #define INET6_ADDRSTRLEN 46 |
| 36 | #endif |
| 37 | |
| 38 | /* lookup tables for standard DNS stuff */ |
| 39 | |
| 40 | /* Taken from RFC 2535, section 7. */ |
| 41 | ldns_lookup_table ldns_algorithms[] = { |
| 42 | { LDNS_RSAMD5, "RSAMD5" }, |
| 43 | { LDNS_DH, "DH" }, |
| 44 | { LDNS_DSA, "DSA" }, |
| 45 | { LDNS_ECC, "ECC" }, |
| 46 | { LDNS_RSASHA1, "RSASHA1" }, |
| 47 | { LDNS_INDIRECT, "INDIRECT" }, |
| 48 | { LDNS_PRIVATEDNS, "PRIVATEDNS" }, |
| 49 | { LDNS_PRIVATEOID, "PRIVATEOID" }, |
| 50 | { 0, NULL } |
| 51 | }; |
| 52 | |
| 53 | /* Taken from RFC 2538 */ |
| 54 | ldns_lookup_table ldns_cert_algorithms[] = { |
| 55 | { LDNS_CERT_PKIX, "PKIX" }, |
| 56 | { LDNS_CERT_SPKI, "SPKI" }, |
| 57 | { LDNS_CERT_PGP, "PGP" }, |
| 58 | { LDNS_CERT_URI, "URI" }, |
| 59 | { LDNS_CERT_OID, "OID" }, |
| 60 | { 0, NULL } |
| 61 | }; |
| 62 | |
| 63 | /* classes */ |
| 64 | ldns_lookup_table ldns_rr_classes[] = { |
| 65 | { LDNS_RR_CLASS_IN, "IN" }, |
| 66 | { LDNS_RR_CLASS_CH, "CH" }, |
| 67 | { LDNS_RR_CLASS_HS, "HS" }, |
| 68 | { LDNS_RR_CLASS_ANY, "ANY" }, |
| 69 | { 0, NULL } |
| 70 | }; |
| 71 | |
| 72 | /* if these are used elsewhere */ |
| 73 | ldns_lookup_table ldns_rcodes[] = { |
| 74 | { LDNS_RCODE_NOERROR, "NOERROR" }, |
| 75 | { LDNS_RCODE_FORMERR, "FORMERR" }, |
| 76 | { LDNS_RCODE_SERVFAIL, "SERVFAIL" }, |
| 77 | { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" }, |
| 78 | { LDNS_RCODE_NOTIMPL, "NOTIMPL" }, |
| 79 | { LDNS_RCODE_REFUSED, "REFUSED" }, |
| 80 | { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" }, |
| 81 | { LDNS_RCODE_YXRRSET, "YXRRSET" }, |
| 82 | { LDNS_RCODE_NXRRSET, "NXRRSET" }, |
| 83 | { LDNS_RCODE_NOTAUTH, "NOTAUTH" }, |
| 84 | { LDNS_RCODE_NOTZONE, "NOTZONE" }, |
| 85 | { 0, NULL } |
| 86 | }; |
| 87 | |
| 88 | ldns_lookup_table ldns_opcodes[] = { |
| 89 | { LDNS_PACKET_QUERY, "QUERY" }, |
| 90 | { LDNS_PACKET_IQUERY, "IQUERY" }, |
| 91 | { LDNS_PACKET_STATUS, "STATUS" }, |
| 92 | { LDNS_PACKET_NOTIFY, "NOTIFY" }, |
| 93 | { LDNS_PACKET_UPDATE, "UPDATE" }, |
| 94 | { 0, NULL } |
| 95 | }; |
| 96 | |
| 97 | /* do NOT pass compressed data here :p */ |
| 98 | ldns_status |
| 99 | ldns_rdf2buffer_str_dname(ldns_buffer *output, const ldns_rdf *dname) |
| 100 | { |
| 101 | /* can we do with 1 pos var? or without at all? */ |
| 102 | uint8_t src_pos = 0; |
| 103 | uint8_t len; |
| 104 | uint8_t *data; |
| 105 | uint8_t i; |
| 106 | |
| 107 | data = (uint8_t*)ldns_rdf_data(dname); |
| 108 | len = data[src_pos]; |
| 109 | |
| 110 | if (ldns_rdf_size(dname) > LDNS_MAX_DOMAINLEN) { |
| 111 | /* too large, return */ |
| 112 | return LDNS_STATUS_DOMAINNAME_OVERFLOW; |
| 113 | } |
| 114 | |
| 115 | /* special case: root label */ |
| 116 | if (1 == ldns_rdf_size(dname)) { |
| 117 | ldns_buffer_printf(output, "."); |
| 118 | } else { |
| 119 | while ((len > 0) && src_pos < ldns_rdf_size(dname)) { |
| 120 | src_pos++; |
| 121 | for(i = 0; i < len; i++) { |
| 122 | /* paranoia check for various 'strange' |
| 123 | characters in dnames |
| 124 | */ |
| 125 | if (data[src_pos] == '.' || |
| 126 | data[src_pos] == '(' || data[src_pos] == ')') { |
| 127 | ldns_buffer_printf(output, "\\%c", |
| 128 | data[src_pos]); |
| 129 | /* isprint!? */ |
| 130 | } else if (!isprint((int) data[src_pos])) { |
| 131 | ldns_buffer_printf(output, "\\%03u", |
| 132 | data[src_pos]); |
| 133 | } else { |
| 134 | ldns_buffer_printf(output, "%c", data[src_pos]); |
| 135 | } |
| 136 | src_pos++; |
| 137 | } |
| 138 | |
| 139 | len = data[src_pos]; |
| 140 | ldns_buffer_printf(output, "."); |
| 141 | } |
| 142 | } |
| 143 | return ldns_buffer_status(output); |
| 144 | } |
| 145 | |
| 146 | ldns_status |
| 147 | ldns_rdf2buffer_str_int8(ldns_buffer *output, const ldns_rdf *rdf) |
| 148 | { |
| 149 | uint8_t data = ldns_rdf_data(rdf)[0]; |
| 150 | ldns_buffer_printf(output, "%lu", (unsigned long) data); |
| 151 | return ldns_buffer_status(output); |
| 152 | } |
| 153 | |
| 154 | ldns_status |
| 155 | ldns_rdf2buffer_str_int16(ldns_buffer *output, const ldns_rdf *rdf) |
| 156 | { |
| 157 | uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf)); |
| 158 | ldns_buffer_printf(output, "%lu", (unsigned long) data); |
| 159 | return ldns_buffer_status(output); |
| 160 | } |
| 161 | |
| 162 | ldns_status |
| 163 | ldns_rdf2buffer_str_int32(ldns_buffer *output, const ldns_rdf *rdf) |
| 164 | { |
| 165 | uint32_t data = ldns_read_uint32(ldns_rdf_data(rdf)); |
| 166 | ldns_buffer_printf(output, "%lu", (unsigned long) data); |
| 167 | return ldns_buffer_status(output); |
| 168 | } |
| 169 | |
| 170 | ldns_status |
| 171 | ldns_rdf2buffer_str_time(ldns_buffer *output, const ldns_rdf *rdf) |
| 172 | { |
| 173 | /* create a YYYYMMDDHHMMSS string if possible */ |
| 174 | uint32_t data = ldns_read_uint32(ldns_rdf_data(rdf)); |
| 175 | time_t data_time; |
| 176 | struct tm tm; |
| 177 | char date_buf[16]; |
| 178 | |
| 179 | data_time = 0; |
| 180 | memcpy(&data_time, &data, sizeof(uint32_t)); |
| 181 | |
| 182 | memset(&tm, 0, sizeof(tm)); |
| 183 | |
| 184 | if (gmtime_r(&data_time, &tm) && strftime(date_buf, 15, "%Y%m%d%H%M%S", &tm)) { |
| 185 | ldns_buffer_printf(output, "%s", date_buf); |
| 186 | } |
| 187 | return ldns_buffer_status(output); |
| 188 | } |
| 189 | |
| 190 | ldns_status |
| 191 | ldns_rdf2buffer_str_a(ldns_buffer *output, const ldns_rdf *rdf) |
| 192 | { |
| 193 | char str[INET_ADDRSTRLEN]; |
| 194 | |
| 195 | if (inet_ntop(AF_INET, ldns_rdf_data(rdf), str, INET_ADDRSTRLEN)) { |
| 196 | ldns_buffer_printf(output, "%s", str); |
| 197 | } |
| 198 | return ldns_buffer_status(output); |
| 199 | } |
| 200 | |
| 201 | ldns_status |
| 202 | ldns_rdf2buffer_str_aaaa(ldns_buffer *output, const ldns_rdf *rdf) |
| 203 | { |
| 204 | char str[INET6_ADDRSTRLEN]; |
| 205 | |
| 206 | if (inet_ntop(AF_INET6, ldns_rdf_data(rdf), str, INET6_ADDRSTRLEN)) { |
| 207 | ldns_buffer_printf(output, "%s", str); |
| 208 | } |
| 209 | |
| 210 | return ldns_buffer_status(output); |
| 211 | } |
| 212 | |
| 213 | ldns_status |
| 214 | ldns_rdf2buffer_str_str(ldns_buffer *output, const ldns_rdf *rdf) |
| 215 | { |
| 216 | const uint8_t *data = ldns_rdf_data(rdf); |
| 217 | uint8_t length = data[0]; |
| 218 | size_t i; |
| 219 | |
| 220 | ldns_buffer_printf(output, "\""); |
| 221 | for (i = 1; i <= length; ++i) { |
| 222 | char ch = (char) data[i]; |
| 223 | if (isprint(ch)) { |
| 224 | if (ch == '"' || ch == '\\') { |
| 225 | ldns_buffer_printf(output, "\\"); |
| 226 | } |
| 227 | ldns_buffer_printf(output, "%c", ch); |
| 228 | } else { |
| 229 | ldns_buffer_printf(output, "\\%03u", (unsigned) ch); |
| 230 | } |
| 231 | } |
| 232 | ldns_buffer_printf(output, "\""); |
| 233 | return ldns_buffer_status(output); |
| 234 | } |
| 235 | |
| 236 | ldns_status |
| 237 | ldns_rdf2buffer_str_b64(ldns_buffer *output, const ldns_rdf *rdf) |
| 238 | { |
| 239 | size_t size = b64_ntop_calculate_size(ldns_rdf_size(rdf)); |
| 240 | char *b64 = LDNS_XMALLOC(char, size); |
| 241 | if (b64_ntop(ldns_rdf_data(rdf), ldns_rdf_size(rdf), b64, size)) { |
| 242 | ldns_buffer_printf(output, "%s", b64); |
| 243 | } |
| 244 | LDNS_FREE(b64); |
| 245 | return ldns_buffer_status(output); |
| 246 | } |
| 247 | |
| 248 | ldns_status |
| 249 | ldns_rdf2buffer_str_b32_ext(ldns_buffer *output, const ldns_rdf *rdf) |
| 250 | { |
| 251 | size_t size = b32_ntop_calculate_size(ldns_rdf_size(rdf) - 1); |
| 252 | char *b32 = LDNS_XMALLOC(char, size + 1); |
| 253 | size = (size_t) b32_ntop_extended_hex(ldns_rdf_data(rdf) + 1, ldns_rdf_size(rdf) - 1, b32, size); |
| 254 | if (size > 0) { |
| 255 | ldns_buffer_printf(output, "%s", b32); |
| 256 | } |
| 257 | LDNS_FREE(b32); |
| 258 | return ldns_buffer_status(output); |
| 259 | } |
| 260 | |
| 261 | ldns_status |
| 262 | ldns_rdf2buffer_str_hex(ldns_buffer *output, const ldns_rdf *rdf) |
| 263 | { |
| 264 | size_t i; |
| 265 | for (i = 0; i < ldns_rdf_size(rdf); i++) { |
| 266 | ldns_buffer_printf(output, "%02x", ldns_rdf_data(rdf)[i]); |
| 267 | } |
| 268 | |
| 269 | return ldns_buffer_status(output); |
| 270 | } |
| 271 | |
| 272 | ldns_status |
| 273 | ldns_rdf2buffer_str_type(ldns_buffer *output, const ldns_rdf *rdf) |
| 274 | { |
| 275 | uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf)); |
| 276 | const ldns_rr_descriptor *descriptor; |
| 277 | |
| 278 | descriptor = ldns_rr_descript(data); |
| 279 | if (descriptor && descriptor->_name) { |
| 280 | ldns_buffer_printf(output, "%s", descriptor->_name); |
| 281 | } else { |
| 282 | ldns_buffer_printf(output, "TYPE%u", data); |
| 283 | } |
| 284 | return ldns_buffer_status(output); |
| 285 | } |
| 286 | |
| 287 | ldns_status |
| 288 | ldns_rdf2buffer_str_class(ldns_buffer *output, const ldns_rdf *rdf) |
| 289 | { |
| 290 | uint8_t data = ldns_rdf_data(rdf)[0]; |
| 291 | ldns_lookup_table *lt; |
| 292 | |
| 293 | lt = ldns_lookup_by_id(ldns_rr_classes, (int) data); |
| 294 | if (lt) { |
| 295 | ldns_buffer_printf(output, "\t%s", lt->name); |
| 296 | } else { |
| 297 | ldns_buffer_printf(output, "\tCLASS%d", data); |
| 298 | } |
| 299 | return ldns_buffer_status(output); |
| 300 | } |
| 301 | |
| 302 | ldns_status |
| 303 | ldns_rdf2buffer_str_cert_alg(ldns_buffer *output, const ldns_rdf *rdf) |
| 304 | { |
| 305 | uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf)); |
| 306 | ldns_lookup_table *lt; |
| 307 | lt = ldns_lookup_by_id(ldns_cert_algorithms, (int) data); |
| 308 | if (lt) { |
| 309 | ldns_buffer_printf(output, "%s", lt->name); |
| 310 | } else { |
| 311 | ldns_buffer_printf(output, "%d", data); |
| 312 | } |
| 313 | return ldns_buffer_status(output); |
| 314 | } |
| 315 | |
| 316 | ldns_status |
| 317 | ldns_rdf2buffer_str_alg(ldns_buffer *output, const ldns_rdf *rdf) |
| 318 | { |
| 319 | /* don't use algorithm mnemonics in the presentation format |
| 320 | this kind of got sneaked into the rfc's */ |
| 321 | uint8_t data = ldns_rdf_data(rdf)[0]; |
| 322 | /* |
| 323 | |
| 324 | ldns_lookup_table *lt; |
| 325 | |
| 326 | lt = ldns_lookup_by_id(ldns_algorithms, (int) data); |
| 327 | if (lt) { |
| 328 | ldns_buffer_printf(output, "%s", lt->name); |
| 329 | } else { |
| 330 | */ |
| 331 | ldns_buffer_printf(output, "%d", data); |
| 332 | /* |
| 333 | } |
| 334 | */ |
| 335 | return ldns_buffer_status(output); |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | loc_cm_print(ldns_buffer *output, uint8_t mantissa, uint8_t exponent) |
| 340 | { |
| 341 | uint8_t i; |
| 342 | /* is it 0.<two digits> ? */ |
| 343 | if(exponent < 2) { |
| 344 | if(exponent == 1) |
| 345 | mantissa *= 10; |
| 346 | ldns_buffer_printf(output, "0.%02ld", (long)mantissa); |
| 347 | return; |
| 348 | } |
| 349 | /* always <digit><string of zeros> */ |
| 350 | ldns_buffer_printf(output, "%d", (int)mantissa); |
| 351 | for(i=0; i<exponent-2; i++) |
| 352 | ldns_buffer_printf(output, "0"); |
| 353 | } |
| 354 | |
| 355 | ldns_status |
| 356 | ldns_rdf2buffer_str_loc(ldns_buffer *output, const ldns_rdf *rdf) |
| 357 | { |
| 358 | /* we could do checking (ie degrees < 90 etc)? */ |
| 359 | uint8_t version = ldns_rdf_data(rdf)[0]; |
| 360 | uint8_t size; |
| 361 | uint8_t horizontal_precision; |
| 362 | uint8_t vertical_precision; |
| 363 | uint32_t longitude; |
| 364 | uint32_t latitude; |
| 365 | long altitude; |
| 366 | char northerness; |
| 367 | char easterness; |
| 368 | uint32_t h; |
| 369 | uint32_t m; |
| 370 | double s; |
| 371 | |
| 372 | uint32_t equator = (uint32_t) ldns_power(2, 31); |
| 373 | |
| 374 | if (version == 0) { |
| 375 | size = ldns_rdf_data(rdf)[1]; |
| 376 | horizontal_precision = ldns_rdf_data(rdf)[2]; |
| 377 | vertical_precision = ldns_rdf_data(rdf)[3]; |
| 378 | |
| 379 | latitude = ldns_read_uint32(&ldns_rdf_data(rdf)[4]); |
| 380 | longitude = ldns_read_uint32(&ldns_rdf_data(rdf)[8]); |
| 381 | altitude = ldns_read_uint32(&ldns_rdf_data(rdf)[12]); |
| 382 | |
| 383 | if (latitude > equator) { |
| 384 | northerness = 'N'; |
| 385 | latitude = latitude - equator; |
| 386 | } else { |
| 387 | northerness = 'S'; |
| 388 | latitude = equator - latitude; |
| 389 | } |
| 390 | h = latitude / (1000 * 60 * 60); |
| 391 | latitude = latitude % (1000 * 60 * 60); |
| 392 | m = latitude / (1000 * 60); |
| 393 | latitude = latitude % (1000 * 60); |
| 394 | s = (double) latitude / 1000.0; |
| 395 | ldns_buffer_printf(output, "%02u %02u %0.3f %c ", |
| 396 | h, m, s, northerness); |
| 397 | |
| 398 | if (longitude > equator) { |
| 399 | easterness = 'E'; |
| 400 | longitude = longitude - equator; |
| 401 | } else { |
| 402 | easterness = 'W'; |
| 403 | longitude = equator - longitude; |
| 404 | } |
| 405 | h = longitude / (1000 * 60 * 60); |
| 406 | longitude = longitude % (1000 * 60 * 60); |
| 407 | m = longitude / (1000 * 60); |
| 408 | longitude = longitude % (1000 * 60); |
| 409 | s = (double) longitude / (1000.0); |
| 410 | ldns_buffer_printf(output, "%02u %02u %0.3f %c ", |
| 411 | h, m, s, easterness); |
| 412 | |
| 413 | ldns_buffer_printf(output, "%ld", altitude/100 - 100000); |
| 414 | if(altitude%100 != 0) |
| 415 | ldns_buffer_printf(output, ".%02ld", altitude%100); |
| 416 | ldns_buffer_printf(output, "m "); |
| 417 | |
| 418 | loc_cm_print(output, (size & 0xf0) >> 4, size & 0x0f); |
| 419 | ldns_buffer_printf(output, "m "); |
| 420 | |
| 421 | loc_cm_print(output, (horizontal_precision & 0xf0) >> 4, |
| 422 | horizontal_precision & 0x0f); |
| 423 | ldns_buffer_printf(output, "m "); |
| 424 | |
| 425 | loc_cm_print(output, (vertical_precision & 0xf0) >> 4, |
| 426 | vertical_precision & 0x0f); |
| 427 | ldns_buffer_printf(output, "m "); |
| 428 | |
| 429 | return ldns_buffer_status(output); |
| 430 | } else { |
| 431 | return ldns_rdf2buffer_str_hex(output, rdf); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | ldns_status |
| 436 | ldns_rdf2buffer_str_unknown(ldns_buffer *output, const ldns_rdf *rdf) |
| 437 | { |
| 438 | ldns_buffer_printf(output, "\\# %u ", ldns_rdf_size(rdf)); |
| 439 | return ldns_rdf2buffer_str_hex(output, rdf); |
| 440 | } |
| 441 | |
| 442 | ldns_status |
| 443 | ldns_rdf2buffer_str_nsap(ldns_buffer *output, const ldns_rdf *rdf) |
| 444 | { |
| 445 | ldns_buffer_printf(output, "0x"); |
| 446 | return ldns_rdf2buffer_str_hex(output, rdf); |
| 447 | } |
| 448 | |
| 449 | ldns_status |
| 450 | ldns_rdf2buffer_str_wks(ldns_buffer *output, const ldns_rdf *rdf) |
| 451 | { |
| 452 | /* protocol, followed by bitmap of services */ |
| 453 | struct protoent *protocol; |
| 454 | char *proto_name = NULL; |
| 455 | uint8_t protocol_nr; |
| 456 | struct servent *service; |
| 457 | uint16_t current_service; |
| 458 | |
| 459 | protocol_nr = ldns_rdf_data(rdf)[0]; |
| 460 | protocol = getprotobynumber((int) protocol_nr); |
| 461 | if (protocol && (protocol->p_name != NULL)) { |
| 462 | proto_name = protocol->p_name; |
| 463 | ldns_buffer_printf(output, "%s ", protocol->p_name); |
| 464 | } else { |
| 465 | ldns_buffer_printf(output, "%u ", protocol_nr); |
| 466 | } |
| 467 | |
| 468 | #ifdef HAVE_ENDPROTOENT |
| 469 | endprotoent(); |
| 470 | #endif |
| 471 | |
| 472 | for (current_service = 0; |
| 473 | current_service < ldns_rdf_size(rdf) * 7; current_service++) { |
| 474 | if (ldns_get_bit(&(ldns_rdf_data(rdf)[1]), current_service)) { |
| 475 | service = getservbyport((int) htons(current_service), |
| 476 | proto_name); |
| 477 | if (service && service->s_name) { |
| 478 | ldns_buffer_printf(output, "%s ", service->s_name); |
| 479 | } else { |
| 480 | ldns_buffer_printf(output, "%u ", current_service); |
| 481 | } |
| 482 | #ifdef HAVE_ENDSERVENT |
| 483 | endservent(); |
| 484 | #endif |
| 485 | } |
| 486 | } |
| 487 | return ldns_buffer_status(output); |
| 488 | } |
| 489 | |
| 490 | ldns_status |
| 491 | ldns_rdf2buffer_str_nsec(ldns_buffer *output, const ldns_rdf *rdf) |
| 492 | { |
| 493 | /* Note: this code is duplicated in higher.c in |
| 494 | * ldns_nsec_type_check() function |
| 495 | */ |
| 496 | uint8_t window_block_nr; |
| 497 | uint8_t bitmap_length; |
| 498 | uint16_t type; |
| 499 | uint16_t pos = 0; |
| 500 | uint16_t bit_pos; |
| 501 | uint8_t *data = ldns_rdf_data(rdf); |
| 502 | const ldns_rr_descriptor *descriptor; |
| 503 | |
| 504 | while(pos < ldns_rdf_size(rdf)) { |
| 505 | window_block_nr = data[pos]; |
| 506 | bitmap_length = data[pos + 1]; |
| 507 | pos += 2; |
| 508 | |
| 509 | for (bit_pos = 0; bit_pos < (bitmap_length) * 8; bit_pos++) { |
| 510 | if (ldns_get_bit(&data[pos], bit_pos)) { |
| 511 | type = 256 * (uint16_t) window_block_nr + bit_pos; |
| 512 | descriptor = ldns_rr_descript(type); |
| 513 | |
| 514 | if (descriptor && descriptor->_name) { |
| 515 | ldns_buffer_printf(output, "%s ", |
| 516 | descriptor->_name); |
| 517 | } else { |
| 518 | ldns_buffer_printf(output, "TYPE%u ", type); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | pos += (uint16_t) bitmap_length; |
| 524 | } |
| 525 | |
| 526 | return ldns_buffer_status(output); |
| 527 | } |
| 528 | |
| 529 | ldns_status |
| 530 | ldns_rdf2buffer_str_nsec3_salt(ldns_buffer *output, const ldns_rdf *rdf) |
| 531 | { |
| 532 | uint8_t salt_length; |
| 533 | uint8_t salt_pos; |
| 534 | |
| 535 | uint8_t *data = ldns_rdf_data(rdf); |
| 536 | size_t pos; |
| 537 | |
| 538 | salt_length = data[0]; |
| 539 | /* todo: length check needed/possible? */ |
| 540 | /* from now there are variable length entries so remember pos */ |
| 541 | pos = 1; |
| 542 | if (salt_length == 0) { |
| 543 | ldns_buffer_printf(output, "- "); |
| 544 | } else { |
| 545 | for (salt_pos = 0; salt_pos < salt_length; salt_pos++) { |
| 546 | ldns_buffer_printf(output, "%02x", data[1 + salt_pos]); |
| 547 | pos++; |
| 548 | } |
| 549 | ldns_buffer_printf(output, " "); |
| 550 | } |
| 551 | |
| 552 | return ldns_buffer_status(output); |
| 553 | } |
| 554 | |
| 555 | ldns_status |
| 556 | ldns_rdf2buffer_str_period(ldns_buffer *output, const ldns_rdf *rdf) |
| 557 | { |
| 558 | /* period is the number of seconds */ |
| 559 | uint32_t p = ldns_read_uint32(ldns_rdf_data(rdf)); |
| 560 | ldns_buffer_printf(output, "%u", p); |
| 561 | return ldns_buffer_status(output); |
| 562 | } |
| 563 | |
| 564 | ldns_status |
| 565 | ldns_rdf2buffer_str_tsigtime(ldns_buffer *output,const ldns_rdf *rdf) |
| 566 | { |
| 567 | /* tsigtime is 48 bits network order unsigned integer */ |
| 568 | uint64_t tsigtime = 0; |
| 569 | uint8_t *data = ldns_rdf_data(rdf); |
| 570 | |
| 571 | if (ldns_rdf_size(rdf) != 6) { |
| 572 | return LDNS_STATUS_ERR; |
| 573 | } |
| 574 | |
| 575 | tsigtime = ldns_read_uint16(data); |
| 576 | tsigtime *= 65536; |
| 577 | tsigtime += ldns_read_uint16(data+2); |
| 578 | tsigtime *= 65536; |
| 579 | |
| 580 | ldns_buffer_printf(output, "%llu ", tsigtime); |
| 581 | |
| 582 | return ldns_buffer_status(output); |
| 583 | } |
| 584 | |
| 585 | ldns_status |
| 586 | ldns_rdf2buffer_str_apl(ldns_buffer *output, const ldns_rdf *rdf) |
| 587 | { |
| 588 | uint8_t *data = ldns_rdf_data(rdf); |
| 589 | uint16_t address_family = ldns_read_uint16(data); |
| 590 | uint8_t prefix = data[2]; |
| 591 | bool negation; |
| 592 | uint8_t adf_length; |
| 593 | unsigned short i; |
| 594 | unsigned int pos = 0; |
| 595 | |
| 596 | while (pos < (unsigned int) ldns_rdf_size(rdf)) { |
| 597 | address_family = ldns_read_uint16(&data[pos]); |
| 598 | prefix = data[pos + 2]; |
| 599 | negation = data[pos + 3] & LDNS_APL_NEGATION; |
| 600 | adf_length = data[pos + 3] & LDNS_APL_MASK; |
| 601 | if (address_family == LDNS_APL_IP4) { |
| 602 | /* check if prefix < 32? */ |
| 603 | if (negation) { |
| 604 | ldns_buffer_printf(output, "!"); |
| 605 | } |
| 606 | ldns_buffer_printf(output, "%u:", address_family); |
| 607 | /* address is variable length 0 - 4 */ |
| 608 | for (i = 0; i < 4; i++) { |
| 609 | if (i > 0) { |
| 610 | ldns_buffer_printf(output, "."); |
| 611 | } |
| 612 | if (i < (unsigned short) adf_length) { |
| 613 | ldns_buffer_printf(output, "%d", |
| 614 | data[pos + i + 4]); |
| 615 | } else { |
| 616 | ldns_buffer_printf(output, "0"); |
| 617 | } |
| 618 | } |
| 619 | ldns_buffer_printf(output, "/%u ", prefix); |
| 620 | } else if (address_family == LDNS_APL_IP6) { |
| 621 | /* check if prefix < 128? */ |
| 622 | if (negation) { |
| 623 | ldns_buffer_printf(output, "!"); |
| 624 | } |
| 625 | ldns_buffer_printf(output, "%u:", address_family); |
| 626 | /* address is variable length 0 - 16 */ |
| 627 | for (i = 0; i < 16; i++) { |
| 628 | if (i % 2 == 0 && i > 0) { |
| 629 | ldns_buffer_printf(output, ":"); |
| 630 | } |
| 631 | if (i < (unsigned short) adf_length) { |
| 632 | ldns_buffer_printf(output, "%02x", |
| 633 | data[pos + i + 4]); |
| 634 | } else { |
| 635 | ldns_buffer_printf(output, "00"); |
| 636 | } |
| 637 | } |
| 638 | ldns_buffer_printf(output, "/%u ", prefix); |
| 639 | |
| 640 | } else { |
| 641 | /* unknown address family */ |
| 642 | ldns_buffer_printf(output, "Unknown address family: %u data: ", |
| 643 | address_family); |
| 644 | for (i = 1; i < (unsigned short) (4 + adf_length); i++) { |
| 645 | ldns_buffer_printf(output, "%02x", data[i]); |
| 646 | } |
| 647 | } |
| 648 | pos += 4 + adf_length; |
| 649 | } |
| 650 | return ldns_buffer_status(output); |
| 651 | } |
| 652 | |
| 653 | ldns_status |
| 654 | ldns_rdf2buffer_str_int16_data(ldns_buffer *output, const ldns_rdf *rdf) |
| 655 | { |
| 656 | /* Subtract the size (2) of the number that specifies the length */ |
| 657 | size_t size = b64_ntop_calculate_size(ldns_rdf_size(rdf) - 2); |
| 658 | char *b64 = LDNS_XMALLOC(char, size); |
| 659 | |
| 660 | ldns_buffer_printf(output, "%u ", ldns_rdf_size(rdf) - 2); |
| 661 | |
| 662 | if (ldns_rdf_size(rdf) > 2 && |
| 663 | b64_ntop(ldns_rdf_data(rdf) + 2, ldns_rdf_size(rdf) - 2, b64, size)) { |
| 664 | ldns_buffer_printf(output, "%s", b64); |
| 665 | } |
| 666 | LDNS_FREE(b64); |
| 667 | return ldns_buffer_status(output); |
| 668 | } |
| 669 | |
| 670 | ldns_status |
| 671 | ldns_rdf2buffer_str_ipseckey(ldns_buffer *output, const ldns_rdf *rdf) |
| 672 | { |
| 673 | /* wire format from |
| 674 | http://www.ietf.org/internet-drafts/draft-ietf-ipseckey-rr-12.txt |
| 675 | */ |
| 676 | uint8_t *data = ldns_rdf_data(rdf); |
| 677 | uint8_t precedence; |
| 678 | uint8_t gateway_type; |
| 679 | uint8_t algorithm; |
| 680 | |
| 681 | ldns_rdf *gateway = NULL; |
| 682 | uint8_t *gateway_data; |
| 683 | |
| 684 | size_t public_key_size; |
| 685 | uint8_t *public_key_data; |
| 686 | ldns_rdf *public_key; |
| 687 | |
| 688 | size_t offset = 0; |
| 689 | ldns_status status; |
| 690 | |
| 691 | |
| 692 | precedence = data[0]; |
| 693 | gateway_type = data[1]; |
| 694 | algorithm = data[2]; |
| 695 | offset = 3; |
| 696 | |
| 697 | switch (gateway_type) { |
| 698 | case 0: |
| 699 | /* no gateway */ |
| 700 | break; |
| 701 | case 1: |
| 702 | gateway_data = LDNS_XMALLOC(uint8_t, LDNS_IP4ADDRLEN); |
| 703 | memcpy(gateway_data, &data[offset], LDNS_IP4ADDRLEN); |
| 704 | gateway = ldns_rdf_new(LDNS_RDF_TYPE_A, LDNS_IP4ADDRLEN , gateway_data); |
| 705 | break; |
| 706 | case 2: |
| 707 | gateway_data = LDNS_XMALLOC(uint8_t, LDNS_IP6ADDRLEN); |
| 708 | memcpy(gateway_data, &data[offset], LDNS_IP6ADDRLEN); |
| 709 | gateway = |
| 710 | ldns_rdf_new(LDNS_RDF_TYPE_AAAA, LDNS_IP6ADDRLEN, gateway_data); |
| 711 | break; |
| 712 | case 3: |
| 713 | status = ldns_wire2dname(&gateway, data, ldns_rdf_size(rdf), &offset); |
| 714 | break; |
| 715 | default: |
| 716 | /* error? */ |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | public_key_size = ldns_rdf_size(rdf) - offset; |
| 721 | public_key_data = LDNS_XMALLOC(uint8_t, public_key_size); |
| 722 | memcpy(public_key_data, &data[offset], public_key_size); |
| 723 | public_key = ldns_rdf_new(LDNS_RDF_TYPE_B64, public_key_size, public_key_data); |
| 724 | |
| 725 | ldns_buffer_printf(output, "%u %u %u ", precedence, gateway_type, algorithm); |
| 726 | (void) ldns_rdf2buffer_str(output, gateway); |
| 727 | ldns_buffer_printf(output, " "); |
| 728 | (void) ldns_rdf2buffer_str(output, public_key); |
| 729 | |
| 730 | ldns_rdf_free(gateway); |
| 731 | ldns_rdf_free(public_key); |
| 732 | |
| 733 | return ldns_buffer_status(output); |
| 734 | } |
| 735 | |
| 736 | ldns_status |
| 737 | ldns_rdf2buffer_str_tsig(ldns_buffer *output, const ldns_rdf *rdf) |
| 738 | { |
| 739 | /* TSIG RRs have no presentation format, make them #size <data> */ |
| 740 | return ldns_rdf2buffer_str_unknown(output, rdf); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | ldns_status |
| 745 | ldns_rdf2buffer_str(ldns_buffer *buffer, const ldns_rdf *rdf) |
| 746 | { |
| 747 | ldns_status res; |
| 748 | |
| 749 | /*ldns_buffer_printf(buffer, "%u:", ldns_rdf_get_type(rdf));*/ |
| 750 | if (rdf) { |
| 751 | switch(ldns_rdf_get_type(rdf)) { |
| 752 | case LDNS_RDF_TYPE_NONE: |
| 753 | break; |
| 754 | case LDNS_RDF_TYPE_DNAME: |
| 755 | res = ldns_rdf2buffer_str_dname(buffer, rdf); |
| 756 | break; |
| 757 | case LDNS_RDF_TYPE_INT8: |
| 758 | res = ldns_rdf2buffer_str_int8(buffer, rdf); |
| 759 | break; |
| 760 | case LDNS_RDF_TYPE_INT16: |
| 761 | res = ldns_rdf2buffer_str_int16(buffer, rdf); |
| 762 | break; |
| 763 | case LDNS_RDF_TYPE_INT32: |
| 764 | res = ldns_rdf2buffer_str_int32(buffer, rdf); |
| 765 | break; |
| 766 | case LDNS_RDF_TYPE_PERIOD: |
| 767 | res = ldns_rdf2buffer_str_period(buffer, rdf); |
| 768 | break; |
| 769 | case LDNS_RDF_TYPE_TSIGTIME: |
| 770 | res = ldns_rdf2buffer_str_tsigtime(buffer, rdf); |
| 771 | break; |
| 772 | case LDNS_RDF_TYPE_A: |
| 773 | res = ldns_rdf2buffer_str_a(buffer, rdf); |
| 774 | break; |
| 775 | case LDNS_RDF_TYPE_AAAA: |
| 776 | res = ldns_rdf2buffer_str_aaaa(buffer, rdf); |
| 777 | break; |
| 778 | case LDNS_RDF_TYPE_STR: |
| 779 | res = ldns_rdf2buffer_str_str(buffer, rdf); |
| 780 | break; |
| 781 | case LDNS_RDF_TYPE_APL: |
| 782 | res = ldns_rdf2buffer_str_apl(buffer, rdf); |
| 783 | break; |
| 784 | case LDNS_RDF_TYPE_B32_EXT: |
| 785 | res = ldns_rdf2buffer_str_b32_ext(buffer, rdf); |
| 786 | break; |
| 787 | case LDNS_RDF_TYPE_B64: |
| 788 | res = ldns_rdf2buffer_str_b64(buffer, rdf); |
| 789 | break; |
| 790 | case LDNS_RDF_TYPE_HEX: |
| 791 | res = ldns_rdf2buffer_str_hex(buffer, rdf); |
| 792 | break; |
| 793 | case LDNS_RDF_TYPE_NSEC: |
| 794 | res = ldns_rdf2buffer_str_nsec(buffer, rdf); |
| 795 | break; |
| 796 | case LDNS_RDF_TYPE_NSEC3_SALT: |
| 797 | res = ldns_rdf2buffer_str_nsec3_salt(buffer, rdf); |
| 798 | break; |
| 799 | case LDNS_RDF_TYPE_TYPE: |
| 800 | res = ldns_rdf2buffer_str_type(buffer, rdf); |
| 801 | break; |
| 802 | case LDNS_RDF_TYPE_CLASS: |
| 803 | res = ldns_rdf2buffer_str_class(buffer, rdf); |
| 804 | break; |
| 805 | case LDNS_RDF_TYPE_CERT_ALG: |
| 806 | res = ldns_rdf2buffer_str_cert_alg(buffer, rdf); |
| 807 | break; |
| 808 | case LDNS_RDF_TYPE_ALG: |
| 809 | res = ldns_rdf2buffer_str_alg(buffer, rdf); |
| 810 | break; |
| 811 | case LDNS_RDF_TYPE_UNKNOWN: |
| 812 | res = ldns_rdf2buffer_str_unknown(buffer, rdf); |
| 813 | break; |
| 814 | case LDNS_RDF_TYPE_TIME: |
| 815 | res = ldns_rdf2buffer_str_time(buffer, rdf); |
| 816 | break; |
| 817 | case LDNS_RDF_TYPE_LOC: |
| 818 | res = ldns_rdf2buffer_str_loc(buffer, rdf); |
| 819 | break; |
| 820 | case LDNS_RDF_TYPE_WKS: |
| 821 | case LDNS_RDF_TYPE_SERVICE: |
| 822 | res = ldns_rdf2buffer_str_wks(buffer, rdf); |
| 823 | break; |
| 824 | case LDNS_RDF_TYPE_NSAP: |
| 825 | res = ldns_rdf2buffer_str_nsap(buffer, rdf); |
| 826 | break; |
| 827 | case LDNS_RDF_TYPE_IPSECKEY: |
| 828 | res = ldns_rdf2buffer_str_ipseckey(buffer, rdf); |
| 829 | break; |
| 830 | case LDNS_RDF_TYPE_TSIG: |
| 831 | res = ldns_rdf2buffer_str_tsig(buffer, rdf); |
| 832 | break; |
| 833 | case LDNS_RDF_TYPE_INT16_DATA: |
| 834 | res = ldns_rdf2buffer_str_int16_data(buffer, rdf); |
| 835 | break; |
| 836 | case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER: |
| 837 | res = ldns_rdf2buffer_str_b32_ext(buffer, rdf); |
| 838 | break; |
| 839 | } |
| 840 | } else { |
| 841 | ldns_buffer_printf(buffer, "(null) "); |
| 842 | } |
| 843 | return LDNS_STATUS_OK; |
| 844 | } |
| 845 | |
| 846 | ldns_status |
| 847 | ldns_rr2buffer_str(ldns_buffer *output, const ldns_rr *rr) |
| 848 | { |
| 849 | uint16_t i; |
| 850 | ldns_status status = LDNS_STATUS_OK; |
| 851 | ldns_lookup_table *lt; |
| 852 | const ldns_rr_descriptor *descriptor; |
| 853 | |
| 854 | if (!rr) { |
| 855 | ldns_buffer_printf(output, "(null)\n"); |
| 856 | } else { |
| 857 | if (ldns_rr_owner(rr)) { |
| 858 | status = ldns_rdf2buffer_str_dname(output, ldns_rr_owner(rr)); |
| 859 | } |
| 860 | if (status != LDNS_STATUS_OK) { |
| 861 | return status; |
| 862 | } |
| 863 | |
| 864 | /* TTL should NOT be printed if it is a question, |
| 865 | * but we don't know that anymore... (do we?) |
| 866 | * if the rd count is 0 we deal with a question sec. RR |
| 867 | */ |
| 868 | if (ldns_rr_rd_count(rr) > 0) { |
| 869 | ldns_buffer_printf(output, "\t%d", ldns_rr_ttl(rr)); |
| 870 | } |
| 871 | |
| 872 | lt = ldns_lookup_by_id(ldns_rr_classes, ldns_rr_get_class(rr)); |
| 873 | if (lt) { |
| 874 | ldns_buffer_printf(output, "\t%s\t", lt->name); |
| 875 | } else { |
| 876 | ldns_buffer_printf(output, "\tCLASS%d\t", |
| 877 | ldns_rr_get_class(rr)); |
| 878 | } |
| 879 | |
| 880 | descriptor = ldns_rr_descript(ldns_rr_get_type(rr)); |
| 881 | |
| 882 | if (descriptor && descriptor->_name) { |
| 883 | ldns_buffer_printf(output, "%s", descriptor->_name); |
| 884 | } else { |
| 885 | /* exceptions for qtype */ |
| 886 | if (ldns_rr_get_type(rr) == 251) { |
| 887 | ldns_buffer_printf(output, "IXFR "); |
| 888 | } else if (ldns_rr_get_type(rr) == 252) { |
| 889 | ldns_buffer_printf(output, "AXFR "); |
| 890 | } else if (ldns_rr_get_type(rr) == 253) { |
| 891 | ldns_buffer_printf(output, "MAILB "); |
| 892 | } else if (ldns_rr_get_type(rr) == 254) { |
| 893 | ldns_buffer_printf(output, "MAILA "); |
| 894 | } else if (ldns_rr_get_type(rr) == 255) { |
| 895 | ldns_buffer_printf(output, "ANY "); |
| 896 | } else { |
| 897 | ldns_buffer_printf(output, "TYPE%u", |
| 898 | ldns_rr_get_type(rr)); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | if (ldns_rr_rd_count(rr) > 0) { |
| 903 | ldns_buffer_printf(output, "\t"); |
| 904 | } |
| 905 | for (i = 0; i < ldns_rr_rd_count(rr); i++) { |
| 906 | status = ldns_rdf2buffer_str(output, ldns_rr_rdf(rr, i)); |
| 907 | if (i < ldns_rr_rd_count(rr) - 1) { |
| 908 | ldns_buffer_printf(output, " "); |
| 909 | } |
| 910 | } |
| 911 | /* per RR special comments - handy for DNSSEC types */ |
| 912 | /* check to prevent question sec. rr from |
| 913 | * getting here */ |
| 914 | if (ldns_rr_rd_count(rr) > 0) { |
| 915 | switch (ldns_rr_get_type(rr)) { |
| 916 | case LDNS_RR_TYPE_DNSKEY: |
| 917 | #ifdef HAVE_SSL |
| 918 | if (ldns_rdf2native_int16(ldns_rr_rdf(rr, 0)) == 256) { |
| 919 | ldns_buffer_printf(output, |
| 920 | " ;{id = %d (zsk), size = %db}", |
| 921 | ldns_calc_keytag(rr), |
| 922 | ldns_rr_dnskey_key_size(rr)); |
| 923 | break; |
| 924 | } |
| 925 | if (ldns_rdf2native_int16(ldns_rr_rdf(rr, 0)) == 257) { |
| 926 | ldns_buffer_printf(output, |
| 927 | " ;{id = %d (ksk), size = %db}", |
| 928 | ldns_calc_keytag(rr), |
| 929 | ldns_rr_dnskey_key_size(rr)); |
| 930 | break; |
| 931 | } |
| 932 | ldns_buffer_printf(output, " ;{id = %d, size = %db}", |
| 933 | ldns_calc_keytag(rr), |
| 934 | ldns_rr_dnskey_key_size(rr)); |
| 935 | #endif /* HAVE_SSL */ |
| 936 | break; |
| 937 | case LDNS_RR_TYPE_RRSIG: |
| 938 | ldns_buffer_printf(output, " ;{id = %d}", |
| 939 | ldns_rdf2native_int16(ldns_rr_rdf(rr, 6))); |
| 940 | break; |
| 941 | default: |
| 942 | break; |
| 943 | |
| 944 | } |
| 945 | } |
| 946 | /* last */ |
| 947 | ldns_buffer_printf(output, "\n"); |
| 948 | } |
| 949 | return ldns_buffer_status(output); |
| 950 | } |
| 951 | |
| 952 | ldns_status |
| 953 | ldns_rr_list2buffer_str(ldns_buffer *output, const ldns_rr_list *list) |
| 954 | { |
| 955 | uint16_t i; |
| 956 | |
| 957 | for(i = 0; i < ldns_rr_list_rr_count(list); i++) { |
| 958 | (void) ldns_rr2buffer_str(output, ldns_rr_list_rr(list, i)); |
| 959 | } |
| 960 | return ldns_buffer_status(output); |
| 961 | } |
| 962 | |
| 963 | ldns_status |
| 964 | ldns_pktheader2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) |
| 965 | { |
| 966 | ldns_lookup_table *opcode = ldns_lookup_by_id(ldns_opcodes, |
| 967 | (int) ldns_pkt_get_opcode(pkt)); |
| 968 | ldns_lookup_table *rcode = ldns_lookup_by_id(ldns_rcodes, |
| 969 | (int) ldns_pkt_get_rcode(pkt)); |
| 970 | |
| 971 | ldns_buffer_printf(output, ";; ->>HEADER<<- "); |
| 972 | if (opcode) { |
| 973 | ldns_buffer_printf(output, "opcode: %s, ", opcode->name); |
| 974 | } else { |
| 975 | ldns_buffer_printf(output, "opcode: ?? (%u), ", |
| 976 | ldns_pkt_get_opcode(pkt)); |
| 977 | } |
| 978 | if (rcode) { |
| 979 | ldns_buffer_printf(output, "rcode: %s, ", rcode->name); |
| 980 | } else { |
| 981 | ldns_buffer_printf(output, "rcode: ?? (%u), ", ldns_pkt_get_rcode(pkt)); |
| 982 | } |
| 983 | ldns_buffer_printf(output, "id: %d\n", ldns_pkt_id(pkt)); |
| 984 | ldns_buffer_printf(output, ";; flags: "); |
| 985 | |
| 986 | if (ldns_pkt_qr(pkt)) { |
| 987 | ldns_buffer_printf(output, "qr "); |
| 988 | } |
| 989 | if (ldns_pkt_aa(pkt)) { |
| 990 | ldns_buffer_printf(output, "aa "); |
| 991 | } |
| 992 | if (ldns_pkt_tc(pkt)) { |
| 993 | ldns_buffer_printf(output, "tc "); |
| 994 | } |
| 995 | if (ldns_pkt_rd(pkt)) { |
| 996 | ldns_buffer_printf(output, "rd "); |
| 997 | } |
| 998 | if (ldns_pkt_cd(pkt)) { |
| 999 | ldns_buffer_printf(output, "cd "); |
| 1000 | } |
| 1001 | if (ldns_pkt_ra(pkt)) { |
| 1002 | ldns_buffer_printf(output, "ra "); |
| 1003 | } |
| 1004 | if (ldns_pkt_ad(pkt)) { |
| 1005 | ldns_buffer_printf(output, "ad "); |
| 1006 | } |
| 1007 | ldns_buffer_printf(output, "; "); |
| 1008 | ldns_buffer_printf(output, "QUERY: %u, ", ldns_pkt_qdcount(pkt)); |
| 1009 | ldns_buffer_printf(output, "ANSWER: %u, ", ldns_pkt_ancount(pkt)); |
| 1010 | ldns_buffer_printf(output, "AUTHORITY: %u, ", ldns_pkt_nscount(pkt)); |
| 1011 | ldns_buffer_printf(output, "ADDITIONAL: %u ", ldns_pkt_arcount(pkt)); |
| 1012 | return ldns_buffer_status(output); |
| 1013 | } |
| 1014 | |
| 1015 | ldns_status |
| 1016 | ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) |
| 1017 | { |
| 1018 | uint16_t i; |
| 1019 | ldns_status status = LDNS_STATUS_OK; |
| 1020 | char *tmp; |
| 1021 | struct timeval time; |
| 1022 | time_t time_tt; |
| 1023 | |
| 1024 | if (!pkt) { |
| 1025 | ldns_buffer_printf(output, "null"); |
| 1026 | return LDNS_STATUS_OK; |
| 1027 | } |
| 1028 | |
| 1029 | if (ldns_buffer_status_ok(output)) { |
| 1030 | status = ldns_pktheader2buffer_str(output, pkt); |
| 1031 | if (status != LDNS_STATUS_OK) { |
| 1032 | return status; |
| 1033 | } |
| 1034 | |
| 1035 | ldns_buffer_printf(output, "\n"); |
| 1036 | |
| 1037 | ldns_buffer_printf(output, ";; QUESTION SECTION:\n;; "); |
| 1038 | |
| 1039 | |
| 1040 | for (i = 0; i < ldns_pkt_qdcount(pkt); i++) { |
| 1041 | status = ldns_rr2buffer_str(output, |
| 1042 | ldns_rr_list_rr(ldns_pkt_question(pkt), i)); |
| 1043 | if (status != LDNS_STATUS_OK) { |
| 1044 | return status; |
| 1045 | } |
| 1046 | } |
| 1047 | ldns_buffer_printf(output, "\n"); |
| 1048 | |
| 1049 | ldns_buffer_printf(output, ";; ANSWER SECTION:\n"); |
| 1050 | for (i = 0; i < ldns_pkt_ancount(pkt); i++) { |
| 1051 | status = ldns_rr2buffer_str(output, |
| 1052 | ldns_rr_list_rr(ldns_pkt_answer(pkt), i)); |
| 1053 | if (status != LDNS_STATUS_OK) { |
| 1054 | return status; |
| 1055 | } |
| 1056 | |
| 1057 | } |
| 1058 | ldns_buffer_printf(output, "\n"); |
| 1059 | |
| 1060 | ldns_buffer_printf(output, ";; AUTHORITY SECTION:\n"); |
| 1061 | |
| 1062 | for (i = 0; i < ldns_pkt_nscount(pkt); i++) { |
| 1063 | status = ldns_rr2buffer_str(output, |
| 1064 | ldns_rr_list_rr(ldns_pkt_authority(pkt), i)); |
| 1065 | if (status != LDNS_STATUS_OK) { |
| 1066 | return status; |
| 1067 | } |
| 1068 | } |
| 1069 | ldns_buffer_printf(output, "\n"); |
| 1070 | |
| 1071 | ldns_buffer_printf(output, ";; ADDITIONAL SECTION:\n"); |
| 1072 | for (i = 0; i < ldns_pkt_arcount(pkt); i++) { |
| 1073 | status = ldns_rr2buffer_str(output, |
| 1074 | ldns_rr_list_rr(ldns_pkt_additional(pkt), i)); |
| 1075 | if (status != LDNS_STATUS_OK) { |
| 1076 | return status; |
| 1077 | } |
| 1078 | |
| 1079 | } |
| 1080 | ldns_buffer_printf(output, "\n"); |
| 1081 | /* add some futher fields */ |
| 1082 | ldns_buffer_printf(output, ";; Query time: %d msec\n", |
| 1083 | ldns_pkt_querytime(pkt)); |
| 1084 | if (ldns_pkt_edns(pkt)) { |
| 1085 | ldns_buffer_printf(output, |
| 1086 | ";; EDNS: version %u; flags:", |
| 1087 | ldns_pkt_edns_version(pkt)); |
| 1088 | if (ldns_pkt_edns_do(pkt)) { |
| 1089 | ldns_buffer_printf(output, " do"); |
| 1090 | } |
| 1091 | if (ldns_pkt_edns_extended_rcode(pkt)) { |
| 1092 | ldns_buffer_printf(output, " ; ext-rcode: %d", |
| 1093 | ldns_pkt_edns_extended_rcode(pkt)); |
| 1094 | } |
| 1095 | ldns_buffer_printf(output, " ; udp: %u\n", |
| 1096 | ldns_pkt_edns_udp_size(pkt)); |
| 1097 | |
| 1098 | if (ldns_pkt_edns_data(pkt)) { |
| 1099 | ldns_buffer_printf(output, ";; Data: "); |
| 1100 | (void)ldns_rdf2buffer_str(output, |
| 1101 | ldns_pkt_edns_data(pkt)); |
| 1102 | ldns_buffer_printf(output, "\n"); |
| 1103 | } |
| 1104 | } |
| 1105 | if (ldns_pkt_tsig(pkt)) { |
| 1106 | ldns_buffer_printf(output, ";; TSIG:\n;; "); |
| 1107 | (void) ldns_rr2buffer_str(output, ldns_pkt_tsig(pkt)); |
| 1108 | ldns_buffer_printf(output, "\n"); |
| 1109 | } |
| 1110 | if (ldns_pkt_answerfrom(pkt)) { |
| 1111 | tmp = ldns_rdf2str(ldns_pkt_answerfrom(pkt)); |
| 1112 | ldns_buffer_printf(output, ";; SERVER: %s\n", tmp); |
| 1113 | LDNS_FREE(tmp); |
| 1114 | } |
| 1115 | time = ldns_pkt_timestamp(pkt); |
| 1116 | time_tt = (time_t)time.tv_sec; |
| 1117 | ldns_buffer_printf(output, ";; WHEN: %s", |
| 1118 | (char*)ctime(&time_tt)); |
| 1119 | |
| 1120 | ldns_buffer_printf(output, ";; MSG SIZE rcvd: %d\n", |
| 1121 | (int)ldns_pkt_size(pkt)); |
| 1122 | } else { |
| 1123 | return ldns_buffer_status(output); |
| 1124 | } |
| 1125 | return status; |
| 1126 | } |
| 1127 | |
| 1128 | ldns_status |
| 1129 | ldns_key2buffer_str(ldns_buffer *output, const ldns_key *k) |
| 1130 | { |
| 1131 | ldns_status status = LDNS_STATUS_OK; |
| 1132 | unsigned char *bignum; |
| 1133 | |
| 1134 | #ifdef HAVE_SSL |
| 1135 | /* not used when ssl is not defined */ |
| 1136 | ldns_rdf *b64_bignum = NULL; |
| 1137 | uint16_t i; |
| 1138 | |
| 1139 | RSA *rsa; |
| 1140 | DSA *dsa; |
| 1141 | #endif /* HAVE_SSL */ |
| 1142 | |
| 1143 | if (!k) { |
| 1144 | return LDNS_STATUS_ERR; |
| 1145 | } |
| 1146 | |
| 1147 | bignum = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN); |
| 1148 | if (!bignum) { |
| 1149 | return LDNS_STATUS_ERR; |
| 1150 | } |
| 1151 | |
| 1152 | if (ldns_buffer_status_ok(output)) { |
| 1153 | #ifdef HAVE_SSL |
| 1154 | switch(ldns_key_algorithm(k)) { |
| 1155 | case LDNS_SIGN_RSASHA1: |
| 1156 | case LDNS_SIGN_RSASHA1_NSEC3: |
| 1157 | case LDNS_SIGN_RSASHA256: |
| 1158 | case LDNS_SIGN_RSASHA512: |
| 1159 | case LDNS_SIGN_RSAMD5: |
| 1160 | /* copied by looking at dnssec-keygen output */ |
| 1161 | /* header */ |
| 1162 | rsa = ldns_key_rsa_key(k); |
| 1163 | |
| 1164 | ldns_buffer_printf(output,"Private-key-format: v1.2\n"); |
| 1165 | switch(ldns_key_algorithm(k)) { |
| 1166 | case LDNS_SIGN_RSAMD5: |
| 1167 | ldns_buffer_printf(output, |
| 1168 | "Algorithm: %u (RSA)\n", |
| 1169 | LDNS_RSAMD5); |
| 1170 | break; |
| 1171 | case LDNS_SIGN_RSASHA1: |
| 1172 | case LDNS_SIGN_RSASHA1_NSEC3: |
| 1173 | ldns_buffer_printf(output, |
| 1174 | "Algorithm: %u (RSASHA1)\n", |
| 1175 | LDNS_RSASHA1); |
| 1176 | break; |
| 1177 | #ifdef USE_SHA2 |
| 1178 | case LDNS_SIGN_RSASHA256: |
| 1179 | ldns_buffer_printf(output, |
| 1180 | "Algorithm: %u (RSASHA256)\n", |
| 1181 | LDNS_RSASHA256); |
| 1182 | break; |
| 1183 | case LDNS_SIGN_RSASHA512: |
| 1184 | ldns_buffer_printf(output, |
| 1185 | "Algorithm: %u (RSASHA512)\n", |
| 1186 | LDNS_RSASHA512); |
| 1187 | break; |
| 1188 | #endif |
| 1189 | default: |
| 1190 | fprintf(stderr, "Warning: unknown signature "); |
| 1191 | fprintf(stderr, |
| 1192 | "algorithm type %u\n", |
| 1193 | ldns_key_algorithm(k)); |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | /* print to buf, convert to bin, convert to b64, |
| 1200 | * print to buf */ |
| 1201 | ldns_buffer_printf(output, "Modulus: "); |
| 1202 | i = (uint16_t)BN_bn2bin(rsa->n, bignum); |
| 1203 | if (i > LDNS_MAX_KEYLEN) { |
| 1204 | goto error; |
| 1205 | } |
| 1206 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1207 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1208 | goto error; |
| 1209 | } |
| 1210 | ldns_rdf_deep_free(b64_bignum); |
| 1211 | ldns_buffer_printf(output, "\n"); |
| 1212 | ldns_buffer_printf(output, "PublicExponent: "); |
| 1213 | i = (uint16_t)BN_bn2bin(rsa->e, bignum); |
| 1214 | if (i > LDNS_MAX_KEYLEN) { |
| 1215 | goto error; |
| 1216 | } |
| 1217 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1218 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1219 | goto error; |
| 1220 | } |
| 1221 | ldns_rdf_deep_free(b64_bignum); |
| 1222 | ldns_buffer_printf(output, "\n"); |
| 1223 | |
| 1224 | ldns_buffer_printf(output, "PrivateExponent: "); |
| 1225 | if (rsa->d) { |
| 1226 | i = (uint16_t)BN_bn2bin(rsa->d, bignum); |
| 1227 | if (i > LDNS_MAX_KEYLEN) { |
| 1228 | goto error; |
| 1229 | } |
| 1230 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1231 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1232 | goto error; |
| 1233 | } |
| 1234 | ldns_rdf_deep_free(b64_bignum); |
| 1235 | ldns_buffer_printf(output, "\n"); |
| 1236 | } else { |
| 1237 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1238 | } |
| 1239 | |
| 1240 | ldns_buffer_printf(output, "Prime1: "); |
| 1241 | if (rsa->p) { |
| 1242 | i = (uint16_t)BN_bn2bin(rsa->p, bignum); |
| 1243 | if (i > LDNS_MAX_KEYLEN) { |
| 1244 | goto error; |
| 1245 | } |
| 1246 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1247 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1248 | goto error; |
| 1249 | } |
| 1250 | ldns_rdf_deep_free(b64_bignum); |
| 1251 | ldns_buffer_printf(output, "\n"); |
| 1252 | } else { |
| 1253 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1254 | } |
| 1255 | |
| 1256 | ldns_buffer_printf(output, "Prime2: "); |
| 1257 | if (rsa->q) { |
| 1258 | i = (uint16_t)BN_bn2bin(rsa->q, bignum); |
| 1259 | if (i > LDNS_MAX_KEYLEN) { |
| 1260 | goto error; |
| 1261 | } |
| 1262 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1263 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1264 | goto error; |
| 1265 | } |
| 1266 | ldns_rdf_deep_free(b64_bignum); |
| 1267 | ldns_buffer_printf(output, "\n"); |
| 1268 | } else { |
| 1269 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1270 | } |
| 1271 | |
| 1272 | ldns_buffer_printf(output, "Exponent1: "); |
| 1273 | if (rsa->dmp1) { |
| 1274 | i = (uint16_t)BN_bn2bin(rsa->dmp1, bignum); |
| 1275 | if (i > LDNS_MAX_KEYLEN) { |
| 1276 | goto error; |
| 1277 | } |
| 1278 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1279 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1280 | goto error; |
| 1281 | } |
| 1282 | ldns_rdf_deep_free(b64_bignum); |
| 1283 | ldns_buffer_printf(output, "\n"); |
| 1284 | } else { |
| 1285 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1286 | } |
| 1287 | |
| 1288 | ldns_buffer_printf(output, "Exponent2: "); |
| 1289 | if (rsa->dmq1) { |
| 1290 | i = (uint16_t)BN_bn2bin(rsa->dmq1, bignum); |
| 1291 | if (i > LDNS_MAX_KEYLEN) { |
| 1292 | goto error; |
| 1293 | } |
| 1294 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1295 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1296 | goto error; |
| 1297 | } |
| 1298 | ldns_rdf_deep_free(b64_bignum); |
| 1299 | ldns_buffer_printf(output, "\n"); |
| 1300 | } else { |
| 1301 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1302 | } |
| 1303 | |
| 1304 | ldns_buffer_printf(output, "Coefficient: "); |
| 1305 | if (rsa->iqmp) { |
| 1306 | i = (uint16_t)BN_bn2bin(rsa->iqmp, bignum); |
| 1307 | if (i > LDNS_MAX_KEYLEN) { |
| 1308 | goto error; |
| 1309 | } |
| 1310 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1311 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1312 | goto error; |
| 1313 | } |
| 1314 | ldns_rdf_deep_free(b64_bignum); |
| 1315 | ldns_buffer_printf(output, "\n"); |
| 1316 | } else { |
| 1317 | ldns_buffer_printf(output, "(Not available)\n"); |
| 1318 | } |
| 1319 | |
| 1320 | RSA_free(rsa); |
| 1321 | break; |
| 1322 | case LDNS_SIGN_DSA: |
| 1323 | case LDNS_SIGN_DSA_NSEC3: |
| 1324 | dsa = ldns_key_dsa_key(k); |
| 1325 | |
| 1326 | ldns_buffer_printf(output,"Private-key-format: v1.2\n"); |
| 1327 | ldns_buffer_printf(output,"Algorithm: 3 (DSA)\n"); |
| 1328 | |
| 1329 | /* print to buf, convert to bin, convert to b64, |
| 1330 | * print to buf */ |
| 1331 | ldns_buffer_printf(output, "Prime(p): "); |
| 1332 | if (dsa->p) { |
| 1333 | i = (uint16_t)BN_bn2bin(dsa->p, bignum); |
| 1334 | if (i > LDNS_MAX_KEYLEN) { |
| 1335 | goto error; |
| 1336 | } |
| 1337 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1338 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1339 | goto error; |
| 1340 | } |
| 1341 | ldns_rdf_deep_free(b64_bignum); |
| 1342 | ldns_buffer_printf(output, "\n"); |
| 1343 | } else { |
| 1344 | printf("(Not available)\n"); |
| 1345 | } |
| 1346 | |
| 1347 | ldns_buffer_printf(output, "Subprime(q): "); |
| 1348 | if (dsa->q) { |
| 1349 | i = (uint16_t)BN_bn2bin(dsa->q, bignum); |
| 1350 | if (i > LDNS_MAX_KEYLEN) { |
| 1351 | goto error; |
| 1352 | } |
| 1353 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1354 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1355 | goto error; |
| 1356 | } |
| 1357 | ldns_rdf_deep_free(b64_bignum); |
| 1358 | ldns_buffer_printf(output, "\n"); |
| 1359 | } else { |
| 1360 | printf("(Not available)\n"); |
| 1361 | } |
| 1362 | |
| 1363 | ldns_buffer_printf(output, "Base(g): "); |
| 1364 | if (dsa->g) { |
| 1365 | i = (uint16_t)BN_bn2bin(dsa->g, bignum); |
| 1366 | if (i > LDNS_MAX_KEYLEN) { |
| 1367 | goto error; |
| 1368 | } |
| 1369 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1370 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1371 | goto error; |
| 1372 | } |
| 1373 | ldns_rdf_deep_free(b64_bignum); |
| 1374 | ldns_buffer_printf(output, "\n"); |
| 1375 | } else { |
| 1376 | printf("(Not available)\n"); |
| 1377 | } |
| 1378 | |
| 1379 | ldns_buffer_printf(output, "Private_value(x): "); |
| 1380 | if (dsa->priv_key) { |
| 1381 | i = (uint16_t)BN_bn2bin(dsa->priv_key, bignum); |
| 1382 | if (i > LDNS_MAX_KEYLEN) { |
| 1383 | goto error; |
| 1384 | } |
| 1385 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1386 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1387 | goto error; |
| 1388 | } |
| 1389 | ldns_rdf_deep_free(b64_bignum); |
| 1390 | ldns_buffer_printf(output, "\n"); |
| 1391 | } else { |
| 1392 | printf("(Not available)\n"); |
| 1393 | } |
| 1394 | |
| 1395 | ldns_buffer_printf(output, "Public_value(y): "); |
| 1396 | if (dsa->pub_key) { |
| 1397 | i = (uint16_t)BN_bn2bin(dsa->pub_key, bignum); |
| 1398 | if (i > LDNS_MAX_KEYLEN) { |
| 1399 | goto error; |
| 1400 | } |
| 1401 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum); |
| 1402 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1403 | goto error; |
| 1404 | } |
| 1405 | ldns_rdf_deep_free(b64_bignum); |
| 1406 | ldns_buffer_printf(output, "\n"); |
| 1407 | } else { |
| 1408 | printf("(Not available)\n"); |
| 1409 | } |
| 1410 | break; |
| 1411 | case LDNS_SIGN_HMACMD5: |
| 1412 | /* is the filefmt specified for TSIG.. don't know */ |
| 1413 | ldns_buffer_printf(output, "Private-key-format: v1.2\n"); |
| 1414 | ldns_buffer_printf(output, "Algorithm: 157 (HMAC_MD5)\n"); |
| 1415 | ldns_buffer_printf(output, "Key: "); |
| 1416 | i = ldns_key_hmac_size(k); |
| 1417 | b64_bignum = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, ldns_key_hmac_key(k)); |
| 1418 | if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) { |
| 1419 | goto error; |
| 1420 | } |
| 1421 | ldns_rdf_deep_free(b64_bignum); |
| 1422 | ldns_buffer_printf(output, "\n"); |
| 1423 | break; |
| 1424 | } |
| 1425 | #endif /* HAVE_SSL */ |
| 1426 | } else { |
| 1427 | #ifdef HAVE_SSL |
| 1428 | LDNS_FREE(b64_bignum); |
| 1429 | #endif |
| 1430 | LDNS_FREE(bignum); |
| 1431 | return ldns_buffer_status(output); |
| 1432 | } |
| 1433 | LDNS_FREE(bignum); |
| 1434 | return status; |
| 1435 | |
| 1436 | #ifdef HAVE_SSL |
| 1437 | /* compiles warn the label isn't used */ |
| 1438 | error: |
| 1439 | LDNS_FREE(bignum); |
| 1440 | return LDNS_STATUS_ERR; |
| 1441 | #endif /* HAVE_SSL */ |
| 1442 | |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Zero terminate the buffer and fix it to the size of the string. |
| 1447 | */ |
| 1448 | char * |
| 1449 | buffer2str(ldns_buffer *buffer) |
| 1450 | { |
| 1451 | char *tmp_str; |
| 1452 | char *str; |
| 1453 | |
| 1454 | /* check if buffer ends with \0, if not, and |
| 1455 | if there is space, add it */ |
| 1456 | if (*(ldns_buffer_at(buffer, ldns_buffer_position(buffer))) != 0) { |
| 1457 | if (!ldns_buffer_reserve(buffer, 1)) { |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | ldns_buffer_write_u8(buffer, (uint8_t) '\0'); |
| 1461 | if (!ldns_buffer_set_capacity(buffer, ldns_buffer_position(buffer))) { |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | tmp_str = ldns_buffer_export(buffer); |
| 1467 | str = LDNS_XMALLOC(char, strlen(tmp_str) + 1); |
| 1468 | memcpy(str, tmp_str, strlen(tmp_str) + 1); |
| 1469 | |
| 1470 | return str; |
| 1471 | } |
| 1472 | |
| 1473 | char * |
| 1474 | ldns_rdf2str(const ldns_rdf *rdf) |
| 1475 | { |
| 1476 | char *result = NULL; |
| 1477 | ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MIN_BUFLEN); |
| 1478 | |
| 1479 | if (ldns_rdf2buffer_str(tmp_buffer, rdf) == LDNS_STATUS_OK) { |
| 1480 | /* export and return string, destroy rest */ |
| 1481 | result = buffer2str(tmp_buffer); |
| 1482 | } |
| 1483 | |
| 1484 | ldns_buffer_free(tmp_buffer); |
| 1485 | return result; |
| 1486 | } |
| 1487 | |
| 1488 | char * |
| 1489 | ldns_rr2str(const ldns_rr *rr) |
| 1490 | { |
| 1491 | char *result = NULL; |
| 1492 | ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MIN_BUFLEN); |
| 1493 | |
| 1494 | if (ldns_rr2buffer_str(tmp_buffer, rr) == LDNS_STATUS_OK) { |
| 1495 | /* export and return string, destroy rest */ |
| 1496 | result = buffer2str(tmp_buffer); |
| 1497 | } |
| 1498 | |
| 1499 | ldns_buffer_free(tmp_buffer); |
| 1500 | return result; |
| 1501 | } |
| 1502 | |
| 1503 | char * |
| 1504 | ldns_pkt2str(const ldns_pkt *pkt) |
| 1505 | { |
| 1506 | char *result = NULL; |
| 1507 | ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN); |
| 1508 | |
| 1509 | if (ldns_pkt2buffer_str(tmp_buffer, pkt) == LDNS_STATUS_OK) { |
| 1510 | /* export and return string, destroy rest */ |
| 1511 | result = buffer2str(tmp_buffer); |
| 1512 | } |
| 1513 | |
| 1514 | ldns_buffer_free(tmp_buffer); |
| 1515 | return result; |
| 1516 | } |
| 1517 | |
| 1518 | char * |
| 1519 | ldns_key2str(const ldns_key *k) |
| 1520 | { |
| 1521 | char *result = NULL; |
| 1522 | ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MIN_BUFLEN); |
| 1523 | if (ldns_key2buffer_str(tmp_buffer, k) == LDNS_STATUS_OK) { |
| 1524 | /* export and return string, destroy rest */ |
| 1525 | result = buffer2str(tmp_buffer); |
| 1526 | } |
| 1527 | ldns_buffer_free(tmp_buffer); |
| 1528 | return result; |
| 1529 | } |
| 1530 | |
| 1531 | char * |
| 1532 | ldns_rr_list2str(const ldns_rr_list *list) |
| 1533 | { |
| 1534 | char *result = NULL; |
| 1535 | ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MIN_BUFLEN); |
| 1536 | |
| 1537 | if (list) { |
| 1538 | if (ldns_rr_list2buffer_str(tmp_buffer, list) == LDNS_STATUS_OK) { |
| 1539 | } |
| 1540 | } else { |
| 1541 | ldns_buffer_printf(tmp_buffer, "(null)\n"); |
| 1542 | } |
| 1543 | |
| 1544 | /* export and return string, destroy rest */ |
| 1545 | result = buffer2str(tmp_buffer); |
| 1546 | ldns_buffer_free(tmp_buffer); |
| 1547 | return result; |
| 1548 | } |
| 1549 | |
| 1550 | void |
| 1551 | ldns_rdf_print(FILE *output, const ldns_rdf *rdf) |
| 1552 | { |
| 1553 | char *str = ldns_rdf2str(rdf); |
| 1554 | if (str) { |
| 1555 | fprintf(output, "%s", str); |
| 1556 | } else { |
| 1557 | fprintf(output, "Unable to convert rdf to string\n"); |
| 1558 | } |
| 1559 | LDNS_FREE(str); |
| 1560 | } |
| 1561 | |
| 1562 | void |
| 1563 | ldns_rr_print(FILE *output, const ldns_rr *rr) |
| 1564 | { |
| 1565 | char *str = ldns_rr2str(rr); |
| 1566 | if (str) { |
| 1567 | fprintf(output, "%s", str); |
| 1568 | } else { |
| 1569 | fprintf(output, "Unable to convert rr to string\n"); |
| 1570 | } |
| 1571 | LDNS_FREE(str); |
| 1572 | } |
| 1573 | |
| 1574 | void |
| 1575 | ldns_pkt_print(FILE *output, const ldns_pkt *pkt) |
| 1576 | { |
| 1577 | char *str = ldns_pkt2str(pkt); |
| 1578 | if (str) { |
| 1579 | fprintf(output, "%s", str); |
| 1580 | } else { |
| 1581 | fprintf(output, "Unable to convert packet to string\n"); |
| 1582 | } |
| 1583 | LDNS_FREE(str); |
| 1584 | } |
| 1585 | |
| 1586 | void |
| 1587 | ldns_rr_list_print(FILE *output, const ldns_rr_list *lst) |
| 1588 | { |
| 1589 | size_t i; |
| 1590 | for (i = 0; i < ldns_rr_list_rr_count(lst); i++) { |
| 1591 | ldns_rr_print(output, ldns_rr_list_rr(lst, i)); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | void |
| 1596 | ldns_resolver_print(FILE *output, const ldns_resolver *r) |
| 1597 | { |
| 1598 | uint16_t i; |
| 1599 | ldns_rdf **n; |
| 1600 | ldns_rdf **s; |
| 1601 | size_t *rtt; |
| 1602 | if (!r) { |
| 1603 | return; |
| 1604 | } |
| 1605 | n = ldns_resolver_nameservers(r); |
| 1606 | s = ldns_resolver_searchlist(r); |
| 1607 | rtt = ldns_resolver_rtt(r); |
| 1608 | |
| 1609 | fprintf(output, "port: %d\n", (int)ldns_resolver_port(r)); |
| 1610 | fprintf(output, "edns0 size: %d\n", (int)ldns_resolver_edns_udp_size(r)); |
| 1611 | fprintf(output, "use ip6: %d\n", (int)ldns_resolver_ip6(r)); |
| 1612 | |
| 1613 | fprintf(output, "recursive: %d\n", ldns_resolver_recursive(r)); |
| 1614 | fprintf(output, "usevc: %d\n", ldns_resolver_usevc(r)); |
| 1615 | fprintf(output, "igntc: %d\n", ldns_resolver_igntc(r)); |
| 1616 | fprintf(output, "fail: %d\n", ldns_resolver_fail(r)); |
| 1617 | fprintf(output, "retry: %d\n", (int)ldns_resolver_retry(r)); |
| 1618 | fprintf(output, "timeout: %d\n", (int)ldns_resolver_timeout(r).tv_sec); |
| 1619 | |
| 1620 | fprintf(output, "default domain: "); |
| 1621 | ldns_rdf_print(output, ldns_resolver_domain(r)); |
| 1622 | fprintf(output, "\n"); |
| 1623 | |
| 1624 | fprintf(output, "searchlist:\n"); |
| 1625 | for (i = 0; i < ldns_resolver_searchlist_count(r); i++) { |
| 1626 | fprintf(output, "\t"); |
| 1627 | ldns_rdf_print(output, s[i]); |
| 1628 | fprintf(output, "\n"); |
| 1629 | } |
| 1630 | |
| 1631 | fprintf(output, "nameservers:\n"); |
| 1632 | for (i = 0; i < ldns_resolver_nameserver_count(r); i++) { |
| 1633 | fprintf(output, "\t"); |
| 1634 | ldns_rdf_print(output, n[i]); |
| 1635 | |
| 1636 | switch ((int)rtt[i]) { |
| 1637 | case LDNS_RESOLV_RTT_MIN: |
| 1638 | fprintf(output, " - reachable\n"); |
| 1639 | break; |
| 1640 | case LDNS_RESOLV_RTT_INF: |
| 1641 | fprintf(output, " - unreachable\n"); |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | void |
| 1648 | ldns_zone_print(FILE *output, const ldns_zone *z) |
| 1649 | { |
| 1650 | ldns_rr_print(output, ldns_zone_soa(z)); |
| 1651 | ldns_rr_list_print(output, ldns_zone_rrs(z)); |
| 1652 | } |
Note: See TracBrowser
for help on using the browser.








