4 * conversion routines from the host format
5 * to the presentation format (strings)
7 * a Net::DNS like library for C
9 * (c) NLnet Labs, 2004-2006
11 * See the file LICENSE for the license
13 #include <ldns/config.h>
15 #include <ldns/ldns.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 #include <sys/socket.h>
22 #ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
31 #ifndef INET_ADDRSTRLEN
32 #define INET_ADDRSTRLEN 16
34 #ifndef INET6_ADDRSTRLEN
35 #define INET6_ADDRSTRLEN 46
38 /* lookup tables for standard DNS stuff */
40 /* Taken from RFC 2535, section 7. */
41 ldns_lookup_table ldns_algorithms
[] = {
42 { LDNS_RSAMD5
, "RSAMD5" },
46 { LDNS_RSASHA1
, "RSASHA1" },
47 { LDNS_DSA_NSEC3
, "DSA-NSEC3-SHA1" },
48 { LDNS_RSASHA1_NSEC3
, "RSASHA1-NSEC3-SHA1" },
50 { LDNS_RSASHA256
, "RSASHA256"},
51 { LDNS_RSASHA512
, "RSASHA512"},
54 { LDNS_ECC_GOST
, "ECC-GOST"},
57 { LDNS_ECDSAP256SHA256
, "ECDSAP256SHA256"},
58 { LDNS_ECDSAP384SHA384
, "ECDSAP384SHA384"},
60 { LDNS_INDIRECT
, "INDIRECT" },
61 { LDNS_PRIVATEDNS
, "PRIVATEDNS" },
62 { LDNS_PRIVATEOID
, "PRIVATEOID" },
66 /* Taken from RFC 4398 */
67 ldns_lookup_table ldns_cert_algorithms
[] = {
68 { LDNS_CERT_PKIX
, "PKIX" },
69 { LDNS_CERT_SPKI
, "SPKI" },
70 { LDNS_CERT_PGP
, "PGP" },
71 { LDNS_CERT_IPKIX
, "IPKIX" },
72 { LDNS_CERT_ISPKI
, "ISPKI" },
73 { LDNS_CERT_IPGP
, "IPGP" },
74 { LDNS_CERT_ACPKIX
, "ACPKIX" },
75 { LDNS_CERT_IACPKIX
, "IACPKIX" },
76 { LDNS_CERT_URI
, "URI" },
77 { LDNS_CERT_OID
, "OID" },
82 ldns_lookup_table ldns_rr_classes
[] = {
83 { LDNS_RR_CLASS_IN
, "IN" },
84 { LDNS_RR_CLASS_CH
, "CH" },
85 { LDNS_RR_CLASS_HS
, "HS" },
86 { LDNS_RR_CLASS_NONE
, "NONE" },
87 { LDNS_RR_CLASS_ANY
, "ANY" },
91 /* if these are used elsewhere */
92 ldns_lookup_table ldns_rcodes
[] = {
93 { LDNS_RCODE_NOERROR
, "NOERROR" },
94 { LDNS_RCODE_FORMERR
, "FORMERR" },
95 { LDNS_RCODE_SERVFAIL
, "SERVFAIL" },
96 { LDNS_RCODE_NXDOMAIN
, "NXDOMAIN" },
97 { LDNS_RCODE_NOTIMPL
, "NOTIMPL" },
98 { LDNS_RCODE_REFUSED
, "REFUSED" },
99 { LDNS_RCODE_YXDOMAIN
, "YXDOMAIN" },
100 { LDNS_RCODE_YXRRSET
, "YXRRSET" },
101 { LDNS_RCODE_NXRRSET
, "NXRRSET" },
102 { LDNS_RCODE_NOTAUTH
, "NOTAUTH" },
103 { LDNS_RCODE_NOTZONE
, "NOTZONE" },
107 ldns_lookup_table ldns_opcodes
[] = {
108 { LDNS_PACKET_QUERY
, "QUERY" },
109 { LDNS_PACKET_IQUERY
, "IQUERY" },
110 { LDNS_PACKET_STATUS
, "STATUS" },
111 { LDNS_PACKET_NOTIFY
, "NOTIFY" },
112 { LDNS_PACKET_UPDATE
, "UPDATE" },
116 const ldns_output_format ldns_output_format_nocomments_record
= { 0, NULL
};
117 const ldns_output_format
*ldns_output_format_nocomments
118 = &ldns_output_format_nocomments_record
;
119 const ldns_output_format ldns_output_format_onlykeyids_record
= {
120 LDNS_COMMENT_KEY
, NULL
122 const ldns_output_format
*ldns_output_format_onlykeyids
123 = &ldns_output_format_onlykeyids_record
;
124 const ldns_output_format
*ldns_output_format_default
125 = &ldns_output_format_onlykeyids_record
;
127 const ldns_output_format ldns_output_format_bubblebabble_record
= {
128 LDNS_COMMENT_KEY
| LDNS_COMMENT_BUBBLEBABBLE
| LDNS_COMMENT_FLAGS
, NULL
130 const ldns_output_format
*ldns_output_format_bubblebabble
131 = &ldns_output_format_bubblebabble_record
;
134 ldns_pkt_opcode2buffer_str(ldns_buffer
*output
, ldns_pkt_opcode opcode
)
136 ldns_lookup_table
*lt
= ldns_lookup_by_id(ldns_opcodes
, opcode
);
137 if (lt
&& lt
->name
) {
138 ldns_buffer_printf(output
, "%s", lt
->name
);
140 ldns_buffer_printf(output
, "OPCODE%u", opcode
);
142 return ldns_buffer_status(output
);
146 ldns_pkt_rcode2buffer_str(ldns_buffer
*output
, ldns_pkt_rcode rcode
)
148 ldns_lookup_table
*lt
= ldns_lookup_by_id(ldns_rcodes
, rcode
);
149 if (lt
&& lt
->name
) {
150 ldns_buffer_printf(output
, "%s", lt
->name
);
152 ldns_buffer_printf(output
, "RCODE%u", rcode
);
154 return ldns_buffer_status(output
);
158 ldns_algorithm2buffer_str(ldns_buffer
*output
,
159 ldns_algorithm algorithm
)
161 ldns_lookup_table
*lt
= ldns_lookup_by_id(ldns_algorithms
,
163 if (lt
&& lt
->name
) {
164 ldns_buffer_printf(output
, "%s", lt
->name
);
166 ldns_buffer_printf(output
, "ALG%u", algorithm
);
168 return ldns_buffer_status(output
);
172 ldns_cert_algorithm2buffer_str(ldns_buffer
*output
,
173 ldns_cert_algorithm cert_algorithm
)
175 ldns_lookup_table
*lt
= ldns_lookup_by_id(ldns_cert_algorithms
,
177 if (lt
&& lt
->name
) {
178 ldns_buffer_printf(output
, "%s", lt
->name
);
180 ldns_buffer_printf(output
, "CERT_ALG%u",
183 return ldns_buffer_status(output
);
187 ldns_pkt_opcode2str(ldns_pkt_opcode opcode
)
192 buf
= ldns_buffer_new(12);
198 if (ldns_pkt_opcode2buffer_str(buf
, opcode
) == LDNS_STATUS_OK
) {
199 str
= ldns_buffer_export2str(buf
);
202 ldns_buffer_free(buf
);
207 ldns_pkt_rcode2str(ldns_pkt_rcode rcode
)
212 buf
= ldns_buffer_new(10);
218 if (ldns_pkt_rcode2buffer_str(buf
, rcode
) == LDNS_STATUS_OK
) {
219 str
= ldns_buffer_export2str(buf
);
222 ldns_buffer_free(buf
);
227 ldns_pkt_algorithm2str(ldns_algorithm algorithm
)
232 buf
= ldns_buffer_new(10);
238 if (ldns_algorithm2buffer_str(buf
, algorithm
)
240 str
= ldns_buffer_export2str(buf
);
243 ldns_buffer_free(buf
);
248 ldns_pkt_cert_algorithm2str(ldns_cert_algorithm cert_algorithm
)
253 buf
= ldns_buffer_new(10);
259 if (ldns_cert_algorithm2buffer_str(buf
, cert_algorithm
)
261 str
= ldns_buffer_export2str(buf
);
264 ldns_buffer_free(buf
);
269 /* do NOT pass compressed data here :p */
271 ldns_rdf2buffer_str_dname(ldns_buffer
*output
, const ldns_rdf
*dname
)
273 /* can we do with 1 pos var? or without at all? */
280 data
= (uint8_t*)ldns_rdf_data(dname
);
283 if (ldns_rdf_size(dname
) > LDNS_MAX_DOMAINLEN
) {
284 /* too large, return */
285 return LDNS_STATUS_DOMAINNAME_OVERFLOW
;
288 /* special case: root label */
289 if (1 == ldns_rdf_size(dname
)) {
290 ldns_buffer_printf(output
, ".");
292 while ((len
> 0) && src_pos
< ldns_rdf_size(dname
)) {
294 for(i
= 0; i
< len
; i
++) {
295 /* paranoia check for various 'strange'
298 c
= (unsigned char) data
[src_pos
];
299 if(c
== '.' || c
== ';' ||
300 c
== '(' || c
== ')' ||
302 ldns_buffer_printf(output
, "\\%c",
304 } else if (!(isascii(c
) && isgraph(c
))) {
305 ldns_buffer_printf(output
, "\\%03u",
308 ldns_buffer_printf(output
, "%c", data
[src_pos
]);
313 if (src_pos
< ldns_rdf_size(dname
)) {
314 ldns_buffer_printf(output
, ".");
319 return ldns_buffer_status(output
);
323 ldns_rdf2buffer_str_int8(ldns_buffer
*output
, const ldns_rdf
*rdf
)
325 uint8_t data
= ldns_rdf_data(rdf
)[0];
326 ldns_buffer_printf(output
, "%lu", (unsigned long) data
);
327 return ldns_buffer_status(output
);
331 ldns_rdf2buffer_str_int16(ldns_buffer
*output
, const ldns_rdf
*rdf
)
333 uint16_t data
= ldns_read_uint16(ldns_rdf_data(rdf
));
334 ldns_buffer_printf(output
, "%lu", (unsigned long) data
);
335 return ldns_buffer_status(output
);
339 ldns_rdf2buffer_str_int32(ldns_buffer
*output
, const ldns_rdf
*rdf
)
341 uint32_t data
= ldns_read_uint32(ldns_rdf_data(rdf
));
342 ldns_buffer_printf(output
, "%lu", (unsigned long) data
);
343 return ldns_buffer_status(output
);
347 ldns_rdf2buffer_str_time(ldns_buffer
*output
, const ldns_rdf
*rdf
)
349 /* create a YYYYMMDDHHMMSS string if possible */
353 memset(&tm
, 0, sizeof(tm
));
354 if (ldns_serial_arithmitics_gmtime_r(ldns_rdf2native_int32(rdf
), time(NULL
), &tm
)
355 && strftime(date_buf
, 15, "%Y%m%d%H%M%S", &tm
)) {
356 ldns_buffer_printf(output
, "%s", date_buf
);
358 return ldns_buffer_status(output
);
362 ldns_rdf2buffer_str_a(ldns_buffer
*output
, const ldns_rdf
*rdf
)
364 char str
[INET_ADDRSTRLEN
];
366 if (inet_ntop(AF_INET
, ldns_rdf_data(rdf
), str
, INET_ADDRSTRLEN
)) {
367 ldns_buffer_printf(output
, "%s", str
);
369 return ldns_buffer_status(output
);
373 ldns_rdf2buffer_str_aaaa(ldns_buffer
*output
, const ldns_rdf
*rdf
)
375 char str
[INET6_ADDRSTRLEN
];
377 if (inet_ntop(AF_INET6
, ldns_rdf_data(rdf
), str
, INET6_ADDRSTRLEN
)) {
378 ldns_buffer_printf(output
, "%s", str
);
381 return ldns_buffer_status(output
);
385 ldns_rdf2buffer_str_str(ldns_buffer
*output
, const ldns_rdf
*rdf
)
387 const uint8_t *data
= ldns_rdf_data(rdf
);
388 uint8_t length
= data
[0];
391 ldns_buffer_printf(output
, "\"");
392 for (i
= 1; i
<= length
; ++i
) {
393 char ch
= (char) data
[i
];
394 if (isprint((int)ch
) || ch
=='\t') {
395 if (ch
=='\"'||ch
=='\\')
396 ldns_buffer_printf(output
, "\\%c", ch
);
398 ldns_buffer_printf(output
, "%c", ch
);
400 ldns_buffer_printf(output
, "\\%03u",
401 (unsigned)(uint8_t) ch
);
404 ldns_buffer_printf(output
, "\"");
405 return ldns_buffer_status(output
);
409 ldns_rdf2buffer_str_b64(ldns_buffer
*output
, const ldns_rdf
*rdf
)
411 size_t size
= ldns_b64_ntop_calculate_size(ldns_rdf_size(rdf
));
412 char *b64
= LDNS_XMALLOC(char, size
);
413 if(!b64
) return LDNS_STATUS_MEM_ERR
;
414 if (ldns_b64_ntop(ldns_rdf_data(rdf
), ldns_rdf_size(rdf
), b64
, size
)) {
415 ldns_buffer_printf(output
, "%s", b64
);
418 return ldns_buffer_status(output
);
422 ldns_rdf2buffer_str_b32_ext(ldns_buffer
*output
, const ldns_rdf
*rdf
)
426 if(ldns_rdf_size(rdf
) == 0)
427 return LDNS_STATUS_OK
;
428 /* remove -1 for the b32-hash-len octet */
429 size
= ldns_b32_ntop_calculate_size(ldns_rdf_size(rdf
) - 1);
430 /* add one for the end nul for the string */
431 b32
= LDNS_XMALLOC(char, size
+ 1);
432 if(!b32
) return LDNS_STATUS_MEM_ERR
;
433 size
= (size_t) ldns_b32_ntop_extended_hex(ldns_rdf_data(rdf
) + 1,
434 ldns_rdf_size(rdf
) - 1, b32
, size
+1);
436 ldns_buffer_printf(output
, "%s", b32
);
439 return ldns_buffer_status(output
);
443 ldns_rdf2buffer_str_hex(ldns_buffer
*output
, const ldns_rdf
*rdf
)
446 for (i
= 0; i
< ldns_rdf_size(rdf
); i
++) {
447 ldns_buffer_printf(output
, "%02x", ldns_rdf_data(rdf
)[i
]);
450 return ldns_buffer_status(output
);
454 ldns_rdf2buffer_str_type(ldns_buffer
*output
, const ldns_rdf
*rdf
)
456 uint16_t data
= ldns_read_uint16(ldns_rdf_data(rdf
));
457 const ldns_rr_descriptor
*descriptor
;
459 descriptor
= ldns_rr_descript(data
);
460 if (descriptor
&& descriptor
->_name
) {
461 ldns_buffer_printf(output
, "%s", descriptor
->_name
);
463 ldns_buffer_printf(output
, "TYPE%u", data
);
465 return ldns_buffer_status(output
);
469 ldns_rdf2buffer_str_class(ldns_buffer
*output
, const ldns_rdf
*rdf
)
471 uint16_t data
= ldns_read_uint16(ldns_rdf_data(rdf
));
472 ldns_lookup_table
*lt
;
474 lt
= ldns_lookup_by_id(ldns_rr_classes
, (int) data
);
476 ldns_buffer_printf(output
, "\t%s", lt
->name
);
478 ldns_buffer_printf(output
, "\tCLASS%d", data
);
480 return ldns_buffer_status(output
);
484 ldns_rdf2buffer_str_cert_alg(ldns_buffer
*output
, const ldns_rdf
*rdf
)
486 uint16_t data
= ldns_read_uint16(ldns_rdf_data(rdf
));
487 ldns_lookup_table
*lt
;
488 lt
= ldns_lookup_by_id(ldns_cert_algorithms
, (int) data
);
490 ldns_buffer_printf(output
, "%s", lt
->name
);
492 ldns_buffer_printf(output
, "%d", data
);
494 return ldns_buffer_status(output
);
498 ldns_rdf2buffer_str_alg(ldns_buffer
*output
, const ldns_rdf
*rdf
)
500 /* don't use algorithm mnemonics in the presentation format
501 this kind of got sneaked into the rfc's */
502 uint8_t data
= ldns_rdf_data(rdf
)[0];
503 ldns_buffer_printf(output
, "%d", data
);
504 return ldns_buffer_status(output
);
508 loc_cm_print(ldns_buffer
*output
, uint8_t mantissa
, uint8_t exponent
)
511 /* is it 0.<two digits> ? */
515 ldns_buffer_printf(output
, "0.%02ld", (long)mantissa
);
518 /* always <digit><string of zeros> */
519 ldns_buffer_printf(output
, "%d", (int)mantissa
);
520 for(i
=0; i
<exponent
-2; i
++)
521 ldns_buffer_printf(output
, "0");
525 ldns_rr_type2buffer_str(ldns_buffer
*output
, const ldns_rr_type type
)
527 const ldns_rr_descriptor
*descriptor
;
529 descriptor
= ldns_rr_descript(type
);
531 if (descriptor
&& descriptor
->_name
) {
532 ldns_buffer_printf(output
, "%s", descriptor
->_name
);
534 /* exceptions for pseudotypes */
536 case LDNS_RR_TYPE_IXFR
:
537 ldns_buffer_printf(output
, "IXFR");
539 case LDNS_RR_TYPE_AXFR
:
540 ldns_buffer_printf(output
, "AXFR");
542 case LDNS_RR_TYPE_MAILA
:
543 ldns_buffer_printf(output
, "MAILA");
545 case LDNS_RR_TYPE_MAILB
:
546 ldns_buffer_printf(output
, "MAILB");
548 case LDNS_RR_TYPE_ANY
:
549 ldns_buffer_printf(output
, "ANY");
552 ldns_buffer_printf(output
, "TYPE%u", type
);
555 return ldns_buffer_status(output
);
559 ldns_rr_type2str(const ldns_rr_type type
)
564 buf
= ldns_buffer_new(10);
570 if (ldns_rr_type2buffer_str(buf
, type
) == LDNS_STATUS_OK
) {
571 str
= ldns_buffer_export2str(buf
);
574 ldns_buffer_free(buf
);
580 ldns_rr_class2buffer_str(ldns_buffer
*output
,
581 const ldns_rr_class klass
)
583 ldns_lookup_table
*lt
;
585 lt
= ldns_lookup_by_id(ldns_rr_classes
, klass
);
587 ldns_buffer_printf(output
, "%s", lt
->name
);
589 ldns_buffer_printf(output
, "CLASS%d", klass
);
591 return ldns_buffer_status(output
);
595 ldns_rr_class2str(const ldns_rr_class klass
)
600 buf
= ldns_buffer_new(10);
606 if (ldns_rr_class2buffer_str(buf
, klass
) == LDNS_STATUS_OK
) {
607 str
= ldns_buffer_export2str(buf
);
609 ldns_buffer_free(buf
);
614 ldns_rdf2buffer_str_loc(ldns_buffer
*output
, const ldns_rdf
*rdf
)
616 /* we could do checking (ie degrees < 90 etc)? */
617 uint8_t version
= ldns_rdf_data(rdf
)[0];
619 uint8_t horizontal_precision
;
620 uint8_t vertical_precision
;
630 uint32_t equator
= (uint32_t) ldns_power(2, 31);
633 size
= ldns_rdf_data(rdf
)[1];
634 horizontal_precision
= ldns_rdf_data(rdf
)[2];
635 vertical_precision
= ldns_rdf_data(rdf
)[3];
637 latitude
= ldns_read_uint32(&ldns_rdf_data(rdf
)[4]);
638 longitude
= ldns_read_uint32(&ldns_rdf_data(rdf
)[8]);
639 altitude
= ldns_read_uint32(&ldns_rdf_data(rdf
)[12]);
641 if (latitude
> equator
) {
643 latitude
= latitude
- equator
;
646 latitude
= equator
- latitude
;
648 h
= latitude
/ (1000 * 60 * 60);
649 latitude
= latitude
% (1000 * 60 * 60);
650 m
= latitude
/ (1000 * 60);
651 latitude
= latitude
% (1000 * 60);
652 s
= (double) latitude
/ 1000.0;
653 ldns_buffer_printf(output
, "%02u %02u %0.3f %c ",
654 h
, m
, s
, northerness
);
656 if (longitude
> equator
) {
658 longitude
= longitude
- equator
;
661 longitude
= equator
- longitude
;
663 h
= longitude
/ (1000 * 60 * 60);
664 longitude
= longitude
% (1000 * 60 * 60);
665 m
= longitude
/ (1000 * 60);
666 longitude
= longitude
% (1000 * 60);
667 s
= (double) longitude
/ (1000.0);
668 ldns_buffer_printf(output
, "%02u %02u %0.3f %c ",
669 h
, m
, s
, easterness
);
672 s
= ((double) altitude
) / 100;
675 if(altitude
%100 != 0)
676 ldns_buffer_printf(output
, "%.2f", s
);
678 ldns_buffer_printf(output
, "%.0f", s
);
680 ldns_buffer_printf(output
, "m ");
682 loc_cm_print(output
, (size
& 0xf0) >> 4, size
& 0x0f);
683 ldns_buffer_printf(output
, "m ");
685 loc_cm_print(output
, (horizontal_precision
& 0xf0) >> 4,
686 horizontal_precision
& 0x0f);
687 ldns_buffer_printf(output
, "m ");
689 loc_cm_print(output
, (vertical_precision
& 0xf0) >> 4,
690 vertical_precision
& 0x0f);
691 ldns_buffer_printf(output
, "m");
693 return ldns_buffer_status(output
);
695 return ldns_rdf2buffer_str_hex(output
, rdf
);
700 ldns_rdf2buffer_str_unknown(ldns_buffer
*output
, const ldns_rdf
*rdf
)
702 ldns_buffer_printf(output
, "\\# %u ", ldns_rdf_size(rdf
));
703 return ldns_rdf2buffer_str_hex(output
, rdf
);
707 ldns_rdf2buffer_str_nsap(ldns_buffer
*output
, const ldns_rdf
*rdf
)
709 ldns_buffer_printf(output
, "0x");
710 return ldns_rdf2buffer_str_hex(output
, rdf
);
714 ldns_rdf2buffer_str_atma(ldns_buffer
*output
, const ldns_rdf
*rdf
)
716 return ldns_rdf2buffer_str_hex(output
, rdf
);
720 ldns_rdf2buffer_str_wks(ldns_buffer
*output
, const ldns_rdf
*rdf
)
722 /* protocol, followed by bitmap of services */
723 struct protoent
*protocol
;
724 char *proto_name
= NULL
;
726 struct servent
*service
;
727 uint16_t current_service
;
729 protocol_nr
= ldns_rdf_data(rdf
)[0];
730 protocol
= getprotobynumber((int) protocol_nr
);
731 if (protocol
&& (protocol
->p_name
!= NULL
)) {
732 proto_name
= protocol
->p_name
;
733 ldns_buffer_printf(output
, "%s ", protocol
->p_name
);
735 ldns_buffer_printf(output
, "%u ", protocol_nr
);
738 #ifdef HAVE_ENDPROTOENT
742 for (current_service
= 0;
743 current_service
< ldns_rdf_size(rdf
) * 7; current_service
++) {
744 if (ldns_get_bit(&(ldns_rdf_data(rdf
)[1]), current_service
)) {
745 service
= getservbyport((int) htons(current_service
),
747 if (service
&& service
->s_name
) {
748 ldns_buffer_printf(output
, "%s ", service
->s_name
);
750 ldns_buffer_printf(output
, "%u ", current_service
);
752 #ifdef HAVE_ENDSERVENT
757 return ldns_buffer_status(output
);
761 ldns_rdf2buffer_str_nsec(ldns_buffer
*output
, const ldns_rdf
*rdf
)
763 /* Note: this code is duplicated in higher.c in
764 * ldns_nsec_type_check() function
766 uint8_t window_block_nr
;
767 uint8_t bitmap_length
;
771 uint8_t *data
= ldns_rdf_data(rdf
);
772 const ldns_rr_descriptor
*descriptor
;
774 while(pos
< ldns_rdf_size(rdf
)) {
775 window_block_nr
= data
[pos
];
776 bitmap_length
= data
[pos
+ 1];
779 for (bit_pos
= 0; bit_pos
< (bitmap_length
) * 8; bit_pos
++) {
780 if (ldns_get_bit(&data
[pos
], bit_pos
)) {
781 type
= 256 * (uint16_t) window_block_nr
+ bit_pos
;
782 descriptor
= ldns_rr_descript(type
);
784 if (descriptor
&& descriptor
->_name
) {
785 ldns_buffer_printf(output
, "%s ",
788 ldns_buffer_printf(output
, "TYPE%u ", type
);
793 pos
+= (uint16_t) bitmap_length
;
796 return ldns_buffer_status(output
);
800 ldns_rdf2buffer_str_nsec3_salt(ldns_buffer
*output
, const ldns_rdf
*rdf
)
805 uint8_t *data
= ldns_rdf_data(rdf
);
807 if(ldns_rdf_size(rdf
) == 0) {
808 output
->_status
= LDNS_STATUS_ERR
;
809 return ldns_buffer_status(output
);
811 salt_length
= data
[0];
812 /* from now there are variable length entries so remember pos */
813 if (salt_length
== 0 || ((size_t)salt_length
)+1 > ldns_rdf_size(rdf
)) {
814 ldns_buffer_printf(output
, "- ");
816 for (salt_pos
= 0; salt_pos
< salt_length
; salt_pos
++) {
817 ldns_buffer_printf(output
, "%02x", data
[1 + salt_pos
]);
819 ldns_buffer_printf(output
, " ");
822 return ldns_buffer_status(output
);
826 ldns_rdf2buffer_str_period(ldns_buffer
*output
, const ldns_rdf
*rdf
)
828 /* period is the number of seconds */
829 uint32_t p
= ldns_read_uint32(ldns_rdf_data(rdf
));
830 ldns_buffer_printf(output
, "%u", p
);
831 return ldns_buffer_status(output
);
835 ldns_rdf2buffer_str_tsigtime(ldns_buffer
*output
,const ldns_rdf
*rdf
)
837 /* tsigtime is 48 bits network order unsigned integer */
838 uint64_t tsigtime
= 0;
839 uint8_t *data
= ldns_rdf_data(rdf
);
841 if (ldns_rdf_size(rdf
) != 6) {
842 return LDNS_STATUS_ERR
;
845 tsigtime
= ldns_read_uint16(data
);
847 tsigtime
+= ldns_read_uint16(data
+2);
850 ldns_buffer_printf(output
, "%llu ", tsigtime
);
852 return ldns_buffer_status(output
);
856 ldns_rdf2buffer_str_apl(ldns_buffer
*output
, const ldns_rdf
*rdf
)
858 uint8_t *data
= ldns_rdf_data(rdf
);
859 uint16_t address_family
;
866 while (pos
< (unsigned int) ldns_rdf_size(rdf
)) {
867 if(pos
+ 3 >= (unsigned)ldns_rdf_size(rdf
))
868 return LDNS_STATUS_SYNTAX_RDATA_ERR
;
869 address_family
= ldns_read_uint16(&data
[pos
]);
870 prefix
= data
[pos
+ 2];
871 negation
= data
[pos
+ 3] & LDNS_APL_NEGATION
;
872 adf_length
= data
[pos
+ 3] & LDNS_APL_MASK
;
873 if (address_family
== LDNS_APL_IP4
) {
874 /* check if prefix < 32? */
876 ldns_buffer_printf(output
, "!");
878 ldns_buffer_printf(output
, "%u:", address_family
);
879 /* address is variable length 0 - 4 */
880 for (i
= 0; i
< 4; i
++) {
882 ldns_buffer_printf(output
, ".");
884 if (i
< (unsigned short) adf_length
) {
885 if(pos
+i
+4 >= ldns_rdf_size(rdf
))
886 return LDNS_STATUS_SYNTAX_RDATA_ERR
;
887 ldns_buffer_printf(output
, "%d",
890 ldns_buffer_printf(output
, "0");
893 ldns_buffer_printf(output
, "/%u ", prefix
);
894 } else if (address_family
== LDNS_APL_IP6
) {
895 /* check if prefix < 128? */
897 ldns_buffer_printf(output
, "!");
899 ldns_buffer_printf(output
, "%u:", address_family
);
900 /* address is variable length 0 - 16 */
901 for (i
= 0; i
< 16; i
++) {
902 if (i
% 2 == 0 && i
> 0) {
903 ldns_buffer_printf(output
, ":");
905 if (i
< (unsigned short) adf_length
) {
906 if(pos
+i
+4 >= ldns_rdf_size(rdf
))
907 return LDNS_STATUS_SYNTAX_RDATA_ERR
;
908 ldns_buffer_printf(output
, "%02x",
911 ldns_buffer_printf(output
, "00");
914 ldns_buffer_printf(output
, "/%u ", prefix
);
917 /* unknown address family */
918 ldns_buffer_printf(output
, "Unknown address family: %u data: ",
920 for (i
= 1; i
< (unsigned short) (4 + adf_length
); i
++) {
921 if(pos
+i
>= ldns_rdf_size(rdf
))
922 return LDNS_STATUS_SYNTAX_RDATA_ERR
;
923 ldns_buffer_printf(output
, "%02x", data
[i
]);
926 pos
+= 4 + adf_length
;
928 return ldns_buffer_status(output
);
932 ldns_rdf2buffer_str_int16_data(ldns_buffer
*output
, const ldns_rdf
*rdf
)
934 /* Subtract the size (2) of the number that specifies the length */
935 size_t size
= ldns_b64_ntop_calculate_size(ldns_rdf_size(rdf
) - 2);
936 char *b64
= LDNS_XMALLOC(char, size
);
938 return LDNS_STATUS_MEM_ERR
;
940 ldns_buffer_printf(output
, "%u ", ldns_rdf_size(rdf
) - 2);
942 if (ldns_rdf_size(rdf
) > 2 &&
943 ldns_b64_ntop(ldns_rdf_data(rdf
) + 2,
944 ldns_rdf_size(rdf
) - 2,
946 ldns_buffer_printf(output
, "%s", b64
);
949 return ldns_buffer_status(output
);
953 ldns_rdf2buffer_str_ipseckey(ldns_buffer
*output
, const ldns_rdf
*rdf
)
956 http://www.ietf.org/internet-drafts/draft-ietf-ipseckey-rr-12.txt
958 uint8_t *data
= ldns_rdf_data(rdf
);
960 uint8_t gateway_type
;
963 ldns_rdf
*gateway
= NULL
;
964 uint8_t *gateway_data
;
966 size_t public_key_size
;
967 uint8_t *public_key_data
;
968 ldns_rdf
*public_key
;
973 precedence
= data
[0];
974 gateway_type
= data
[1];
978 switch (gateway_type
) {
983 gateway_data
= LDNS_XMALLOC(uint8_t, LDNS_IP4ADDRLEN
);
985 return LDNS_STATUS_MEM_ERR
;
986 memcpy(gateway_data
, &data
[offset
], LDNS_IP4ADDRLEN
);
987 gateway
= ldns_rdf_new(LDNS_RDF_TYPE_A
, LDNS_IP4ADDRLEN
, gateway_data
);
988 offset
+= LDNS_IP4ADDRLEN
;
990 LDNS_FREE(gateway_data
);
991 return LDNS_STATUS_MEM_ERR
;
995 gateway_data
= LDNS_XMALLOC(uint8_t, LDNS_IP6ADDRLEN
);
997 return LDNS_STATUS_MEM_ERR
;
998 memcpy(gateway_data
, &data
[offset
], LDNS_IP6ADDRLEN
);
999 offset
+= LDNS_IP6ADDRLEN
;
1001 ldns_rdf_new(LDNS_RDF_TYPE_AAAA
, LDNS_IP6ADDRLEN
, gateway_data
);
1003 LDNS_FREE(gateway_data
);
1004 return LDNS_STATUS_MEM_ERR
;
1008 status
= ldns_wire2dname(&gateway
, data
, ldns_rdf_size(rdf
), &offset
);
1009 if(status
!= LDNS_STATUS_OK
)
1017 public_key_size
= ldns_rdf_size(rdf
) - offset
;
1018 public_key_data
= LDNS_XMALLOC(uint8_t, public_key_size
);
1019 if(!public_key_data
) {
1020 ldns_rdf_free(gateway
);
1021 return LDNS_STATUS_MEM_ERR
;
1023 memcpy(public_key_data
, &data
[offset
], public_key_size
);
1024 public_key
= ldns_rdf_new(LDNS_RDF_TYPE_B64
, public_key_size
, public_key_data
);
1026 LDNS_FREE(public_key_data
);
1027 ldns_rdf_free(gateway
);
1028 return LDNS_STATUS_MEM_ERR
;
1031 ldns_buffer_printf(output
, "%u %u %u ", precedence
, gateway_type
, algorithm
);
1033 (void) ldns_rdf2buffer_str(output
, gateway
);
1035 ldns_buffer_printf(output
, ".");
1036 ldns_buffer_printf(output
, " ");
1037 (void) ldns_rdf2buffer_str(output
, public_key
);
1039 ldns_rdf_free(gateway
);
1040 ldns_rdf_free(public_key
);
1042 return ldns_buffer_status(output
);
1046 ldns_rdf2buffer_str_tsig(ldns_buffer
*output
, const ldns_rdf
*rdf
)
1048 /* TSIG RRs have no presentation format, make them #size <data> */
1049 return ldns_rdf2buffer_str_unknown(output
, rdf
);
1054 ldns_rdf2buffer_str(ldns_buffer
*buffer
, const ldns_rdf
*rdf
)
1056 ldns_status res
= LDNS_STATUS_OK
;
1058 /*ldns_buffer_printf(buffer, "%u:", ldns_rdf_get_type(rdf));*/
1060 switch(ldns_rdf_get_type(rdf
)) {
1061 case LDNS_RDF_TYPE_NONE
:
1063 case LDNS_RDF_TYPE_DNAME
:
1064 res
= ldns_rdf2buffer_str_dname(buffer
, rdf
);
1066 case LDNS_RDF_TYPE_INT8
:
1067 res
= ldns_rdf2buffer_str_int8(buffer
, rdf
);
1069 case LDNS_RDF_TYPE_INT16
:
1070 res
= ldns_rdf2buffer_str_int16(buffer
, rdf
);
1072 case LDNS_RDF_TYPE_INT32
:
1073 res
= ldns_rdf2buffer_str_int32(buffer
, rdf
);
1075 case LDNS_RDF_TYPE_PERIOD
:
1076 res
= ldns_rdf2buffer_str_period(buffer
, rdf
);
1078 case LDNS_RDF_TYPE_TSIGTIME
:
1079 res
= ldns_rdf2buffer_str_tsigtime(buffer
, rdf
);
1081 case LDNS_RDF_TYPE_A
:
1082 res
= ldns_rdf2buffer_str_a(buffer
, rdf
);
1084 case LDNS_RDF_TYPE_AAAA
:
1085 res
= ldns_rdf2buffer_str_aaaa(buffer
, rdf
);
1087 case LDNS_RDF_TYPE_STR
:
1088 res
= ldns_rdf2buffer_str_str(buffer
, rdf
);
1090 case LDNS_RDF_TYPE_APL
:
1091 res
= ldns_rdf2buffer_str_apl(buffer
, rdf
);
1093 case LDNS_RDF_TYPE_B32_EXT
:
1094 res
= ldns_rdf2buffer_str_b32_ext(buffer
, rdf
);
1096 case LDNS_RDF_TYPE_B64
:
1097 res
= ldns_rdf2buffer_str_b64(buffer
, rdf
);
1099 case LDNS_RDF_TYPE_HEX
:
1100 res
= ldns_rdf2buffer_str_hex(buffer
, rdf
);
1102 case LDNS_RDF_TYPE_NSEC
:
1103 res
= ldns_rdf2buffer_str_nsec(buffer
, rdf
);
1105 case LDNS_RDF_TYPE_NSEC3_SALT
:
1106 res
= ldns_rdf2buffer_str_nsec3_salt(buffer
, rdf
);
1108 case LDNS_RDF_TYPE_TYPE
:
1109 res
= ldns_rdf2buffer_str_type(buffer
, rdf
);
1111 case LDNS_RDF_TYPE_CLASS
:
1112 res
= ldns_rdf2buffer_str_class(buffer
, rdf
);
1114 case LDNS_RDF_TYPE_CERT_ALG
:
1115 res
= ldns_rdf2buffer_str_cert_alg(buffer
, rdf
);
1117 case LDNS_RDF_TYPE_ALG
:
1118 res
= ldns_rdf2buffer_str_alg(buffer
, rdf
);
1120 case LDNS_RDF_TYPE_UNKNOWN
:
1121 res
= ldns_rdf2buffer_str_unknown(buffer
, rdf
);
1123 case LDNS_RDF_TYPE_TIME
:
1124 res
= ldns_rdf2buffer_str_time(buffer
, rdf
);
1126 case LDNS_RDF_TYPE_LOC
:
1127 res
= ldns_rdf2buffer_str_loc(buffer
, rdf
);
1129 case LDNS_RDF_TYPE_WKS
:
1130 case LDNS_RDF_TYPE_SERVICE
:
1131 res
= ldns_rdf2buffer_str_wks(buffer
, rdf
);
1133 case LDNS_RDF_TYPE_NSAP
:
1134 res
= ldns_rdf2buffer_str_nsap(buffer
, rdf
);
1136 case LDNS_RDF_TYPE_ATMA
:
1137 res
= ldns_rdf2buffer_str_atma(buffer
, rdf
);
1139 case LDNS_RDF_TYPE_IPSECKEY
:
1140 res
= ldns_rdf2buffer_str_ipseckey(buffer
, rdf
);
1142 case LDNS_RDF_TYPE_TSIG
:
1143 res
= ldns_rdf2buffer_str_tsig(buffer
, rdf
);
1145 case LDNS_RDF_TYPE_INT16_DATA
:
1146 res
= ldns_rdf2buffer_str_int16_data(buffer
, rdf
);
1148 case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER
:
1149 res
= ldns_rdf2buffer_str_b32_ext(buffer
, rdf
);
1153 /** This will write mangled RRs */
1154 ldns_buffer_printf(buffer
, "(null) ");
1155 res
= LDNS_STATUS_ERR
;
1161 ldns_b32_ext2dname(const ldns_rdf
*rdf
)
1166 if(ldns_rdf_size(rdf
) == 0)
1168 /* remove -1 for the b32-hash-len octet */
1169 size
= ldns_b32_ntop_calculate_size(ldns_rdf_size(rdf
) - 1);
1170 /* add one for the end nul for the string */
1171 b32
= LDNS_XMALLOC(char, size
+ 2);
1173 if (ldns_b32_ntop_extended_hex(ldns_rdf_data(rdf
) + 1,
1174 ldns_rdf_size(rdf
) - 1, b32
, size
+1) > 0) {
1177 if (ldns_str2rdf_dname(&out
, b32
) == LDNS_STATUS_OK
) {
1188 ldns_rr2buffer_str_fmt(ldns_buffer
*output
,
1189 const ldns_output_format
*fmt
, const ldns_rr
*rr
)
1192 ldns_status status
= LDNS_STATUS_OK
;
1195 fmt
= ldns_output_format_default
;
1198 if (LDNS_COMMENT_NULLS
& fmt
->flags
) {
1199 ldns_buffer_printf(output
, "; (null)\n");
1201 return ldns_buffer_status(output
);
1203 if (ldns_rr_owner(rr
)) {
1204 status
= ldns_rdf2buffer_str_dname(output
, ldns_rr_owner(rr
));
1206 if (status
!= LDNS_STATUS_OK
) {
1210 /* TTL should NOT be printed if it is a question */
1211 if (!ldns_rr_is_question(rr
)) {
1212 ldns_buffer_printf(output
, "\t%d", ldns_rr_ttl(rr
));
1215 ldns_buffer_printf(output
, "\t");
1216 status
= ldns_rr_class2buffer_str(output
, ldns_rr_get_class(rr
));
1217 if (status
!= LDNS_STATUS_OK
) {
1220 ldns_buffer_printf(output
, "\t");
1222 status
= ldns_rr_type2buffer_str(output
, ldns_rr_get_type(rr
));
1223 if (status
!= LDNS_STATUS_OK
) {
1227 if (ldns_rr_rd_count(rr
) > 0) {
1228 ldns_buffer_printf(output
, "\t");
1229 } else if (!ldns_rr_is_question(rr
)) {
1230 ldns_buffer_printf(output
, "\t\\# 0");
1233 for (i
= 0; i
< ldns_rr_rd_count(rr
); i
++) {
1234 /* ldns_rdf2buffer_str handles NULL input fine! */
1235 if ((fmt
->flags
& LDNS_FMT_ZEROIZE_RRSIGS
) &&
1236 (ldns_rr_get_type(rr
) == LDNS_RR_TYPE_RRSIG
) &&
1237 ((/* inception */ i
== 4 &&
1238 ldns_rdf_get_type(ldns_rr_rdf(rr
, 4)) ==
1239 LDNS_RDF_TYPE_TIME
) ||
1240 (/* expiration */ i
== 5 &&
1241 ldns_rdf_get_type(ldns_rr_rdf(rr
, 5)) ==
1242 LDNS_RDF_TYPE_TIME
) ||
1243 (/* signature */ i
== 8 &&
1244 ldns_rdf_get_type(ldns_rr_rdf(rr
, 8)) ==
1245 LDNS_RDF_TYPE_B64
))) {
1247 ldns_buffer_printf(output
, "(null)");
1248 status
= ldns_buffer_status(output
);
1249 } else if ((fmt
->flags
& LDNS_FMT_PAD_SOA_SERIAL
) &&
1250 (ldns_rr_get_type(rr
) == LDNS_RR_TYPE_SOA
) &&
1251 /* serial */ i
== 2 &&
1252 ldns_rdf_get_type(ldns_rr_rdf(rr
, 2)) ==
1253 LDNS_RDF_TYPE_INT32
) {
1254 ldns_buffer_printf(output
, "%10lu",
1255 (unsigned long) ldns_read_uint32(
1256 ldns_rdf_data(ldns_rr_rdf(rr
, 2))));
1257 status
= ldns_buffer_status(output
);
1259 status
= ldns_rdf2buffer_str(output
,
1260 ldns_rr_rdf(rr
, i
));
1262 if(status
!= LDNS_STATUS_OK
)
1264 if (i
< ldns_rr_rd_count(rr
) - 1) {
1265 ldns_buffer_printf(output
, " ");
1268 /* per RR special comments - handy for DNSSEC types */
1269 /* check to prevent question sec. rr from
1271 if (ldns_rr_rd_count(rr
) > 0) {
1272 switch (ldns_rr_get_type(rr
)) {
1273 case LDNS_RR_TYPE_DNSKEY
:
1274 /* if ldns_rr_rd_count(rr) > 0
1275 then ldns_rr_rdf(rr, 0) exists! */
1276 if (! (fmt
->flags
& LDNS_COMMENT_KEY
)) {
1279 flags
= ldns_rdf2native_int16(
1280 ldns_rr_rdf(rr
, 0));
1281 ldns_buffer_printf(output
, " ;{");
1282 if (fmt
->flags
& LDNS_COMMENT_KEY_ID
) {
1283 ldns_buffer_printf(output
, "id = %u",
1285 ldns_calc_keytag(rr
));
1287 if ((fmt
->flags
& LDNS_COMMENT_KEY_TYPE
)
1288 && (flags
& LDNS_KEY_ZONE_KEY
)){
1289 if (flags
& LDNS_KEY_SEP_KEY
) {
1297 if (fmt
->flags
& LDNS_COMMENT_KEY_SIZE
){
1301 } else if (fmt
->flags
1302 & (LDNS_COMMENT_KEY_ID
1303 |LDNS_COMMENT_KEY_SIZE
)) {
1304 ldns_buffer_printf( output
, ", ");
1306 if (fmt
->flags
& LDNS_COMMENT_KEY_SIZE
) {
1307 ldns_buffer_printf(output
, "size = %db",
1308 ldns_rr_dnskey_key_size(rr
));
1310 ldns_buffer_printf(output
, "}");
1312 case LDNS_RR_TYPE_RRSIG
:
1313 if ((fmt
->flags
& LDNS_COMMENT_KEY
)
1315 & LDNS_COMMENT_RRSIGS
)
1316 && ldns_rr_rdf(rr
, 6) != NULL
) {
1317 ldns_buffer_printf(output
1319 , ldns_rdf2native_int16(
1320 ldns_rr_rdf(rr
, 6)));
1323 case LDNS_RR_TYPE_DS
:
1324 if ((fmt
->flags
& LDNS_COMMENT_BUBBLEBABBLE
)
1325 && ldns_rr_rdf(rr
, 3) != NULL
) {
1326 uint8_t *data
= ldns_rdf_data(
1327 ldns_rr_rdf(rr
, 3));
1328 size_t len
= ldns_rdf_size(
1329 ldns_rr_rdf(rr
, 3));
1330 char *babble
= ldns_bubblebabble(
1333 ldns_buffer_printf(output
1334 , " ;{%s}", babble
);
1339 case LDNS_RR_TYPE_NSEC3
:
1340 if (! (fmt
->flags
& LDNS_COMMENT_FLAGS
) &&
1341 ! (fmt
->flags
& LDNS_COMMENT_NSEC3_CHAIN
)) {
1344 ldns_buffer_printf(output
, " ;{");
1345 if ((fmt
->flags
& LDNS_COMMENT_FLAGS
)) {
1346 if (ldns_nsec3_optout(rr
)) {
1347 ldns_buffer_printf(output
,
1350 ldns_buffer_printf(output
,
1353 if (fmt
->flags
& LDNS_COMMENT_NSEC3_CHAIN
1354 && fmt
->data
!= NULL
) {
1355 ldns_buffer_printf(output
, ", ");
1358 if (fmt
->flags
& LDNS_COMMENT_NSEC3_CHAIN
1359 && fmt
->data
!= NULL
) {
1360 ldns_rbnode_t
*node
;
1361 ldns_rdf
*key
= ldns_dname_label(
1362 ldns_rr_owner(rr
), 0);
1364 node
= ldns_rbtree_search(
1373 ldns_rdf2buffer_str(
1380 key
= ldns_b32_ext2dname(
1381 ldns_nsec3_next_owner(rr
));
1383 node
= ldns_rbtree_search(
1392 ldns_rdf2buffer_str(
1400 ldns_buffer_printf(output
, "}");
1408 ldns_buffer_printf(output
, "\n");
1409 return ldns_buffer_status(output
);
1413 ldns_rr2buffer_str(ldns_buffer
*output
, const ldns_rr
*rr
)
1415 return ldns_rr2buffer_str_fmt(output
, ldns_output_format_default
, rr
);
1419 ldns_rr_list2buffer_str_fmt(ldns_buffer
*output
,
1420 const ldns_output_format
*fmt
, const ldns_rr_list
*list
)
1424 for(i
= 0; i
< ldns_rr_list_rr_count(list
); i
++) {
1425 (void) ldns_rr2buffer_str_fmt(output
, fmt
,
1426 ldns_rr_list_rr(list
, i
));
1428 return ldns_buffer_status(output
);
1432 ldns_rr_list2buffer_str(ldns_buffer
*output
, const ldns_rr_list
*list
)
1434 return ldns_rr_list2buffer_str_fmt(
1435 output
, ldns_output_format_default
, list
);
1439 ldns_pktheader2buffer_str(ldns_buffer
*output
, const ldns_pkt
*pkt
)
1441 ldns_lookup_table
*opcode
= ldns_lookup_by_id(ldns_opcodes
,
1442 (int) ldns_pkt_get_opcode(pkt
));
1443 ldns_lookup_table
*rcode
= ldns_lookup_by_id(ldns_rcodes
,
1444 (int) ldns_pkt_get_rcode(pkt
));
1446 ldns_buffer_printf(output
, ";; ->>HEADER<<- ");
1448 ldns_buffer_printf(output
, "opcode: %s, ", opcode
->name
);
1450 ldns_buffer_printf(output
, "opcode: ?? (%u), ",
1451 ldns_pkt_get_opcode(pkt
));
1454 ldns_buffer_printf(output
, "rcode: %s, ", rcode
->name
);
1456 ldns_buffer_printf(output
, "rcode: ?? (%u), ", ldns_pkt_get_rcode(pkt
));
1458 ldns_buffer_printf(output
, "id: %d\n", ldns_pkt_id(pkt
));
1459 ldns_buffer_printf(output
, ";; flags: ");
1461 if (ldns_pkt_qr(pkt
)) {
1462 ldns_buffer_printf(output
, "qr ");
1464 if (ldns_pkt_aa(pkt
)) {
1465 ldns_buffer_printf(output
, "aa ");
1467 if (ldns_pkt_tc(pkt
)) {
1468 ldns_buffer_printf(output
, "tc ");
1470 if (ldns_pkt_rd(pkt
)) {
1471 ldns_buffer_printf(output
, "rd ");
1473 if (ldns_pkt_cd(pkt
)) {
1474 ldns_buffer_printf(output
, "cd ");
1476 if (ldns_pkt_ra(pkt
)) {
1477 ldns_buffer_printf(output
, "ra ");
1479 if (ldns_pkt_ad(pkt
)) {
1480 ldns_buffer_printf(output
, "ad ");
1482 ldns_buffer_printf(output
, "; ");
1483 ldns_buffer_printf(output
, "QUERY: %u, ", ldns_pkt_qdcount(pkt
));
1484 ldns_buffer_printf(output
, "ANSWER: %u, ", ldns_pkt_ancount(pkt
));
1485 ldns_buffer_printf(output
, "AUTHORITY: %u, ", ldns_pkt_nscount(pkt
));
1486 ldns_buffer_printf(output
, "ADDITIONAL: %u ", ldns_pkt_arcount(pkt
));
1487 return ldns_buffer_status(output
);
1491 ldns_pkt2buffer_str_fmt(ldns_buffer
*output
,
1492 const ldns_output_format
*fmt
, const ldns_pkt
*pkt
)
1495 ldns_status status
= LDNS_STATUS_OK
;
1497 struct timeval time
;
1501 ldns_buffer_printf(output
, "null");
1502 return LDNS_STATUS_OK
;
1505 if (ldns_buffer_status_ok(output
)) {
1506 status
= ldns_pktheader2buffer_str(output
, pkt
);
1507 if (status
!= LDNS_STATUS_OK
) {
1511 ldns_buffer_printf(output
, "\n");
1513 ldns_buffer_printf(output
, ";; QUESTION SECTION:\n;; ");
1516 for (i
= 0; i
< ldns_pkt_qdcount(pkt
); i
++) {
1517 status
= ldns_rr2buffer_str_fmt(output
, fmt
,
1519 ldns_pkt_question(pkt
), i
));
1520 if (status
!= LDNS_STATUS_OK
) {
1524 ldns_buffer_printf(output
, "\n");
1526 ldns_buffer_printf(output
, ";; ANSWER SECTION:\n");
1527 for (i
= 0; i
< ldns_pkt_ancount(pkt
); i
++) {
1528 status
= ldns_rr2buffer_str_fmt(output
, fmt
,
1530 ldns_pkt_answer(pkt
), i
));
1531 if (status
!= LDNS_STATUS_OK
) {
1536 ldns_buffer_printf(output
, "\n");
1538 ldns_buffer_printf(output
, ";; AUTHORITY SECTION:\n");
1540 for (i
= 0; i
< ldns_pkt_nscount(pkt
); i
++) {
1541 status
= ldns_rr2buffer_str_fmt(output
, fmt
,
1543 ldns_pkt_authority(pkt
), i
));
1544 if (status
!= LDNS_STATUS_OK
) {
1548 ldns_buffer_printf(output
, "\n");
1550 ldns_buffer_printf(output
, ";; ADDITIONAL SECTION:\n");
1551 for (i
= 0; i
< ldns_pkt_arcount(pkt
); i
++) {
1552 status
= ldns_rr2buffer_str_fmt(output
, fmt
,
1554 ldns_pkt_additional(pkt
), i
));
1555 if (status
!= LDNS_STATUS_OK
) {
1560 ldns_buffer_printf(output
, "\n");
1561 /* add some futher fields */
1562 ldns_buffer_printf(output
, ";; Query time: %d msec\n",
1563 ldns_pkt_querytime(pkt
));
1564 if (ldns_pkt_edns(pkt
)) {
1565 ldns_buffer_printf(output
,
1566 ";; EDNS: version %u; flags:",
1567 ldns_pkt_edns_version(pkt
));
1568 if (ldns_pkt_edns_do(pkt
)) {
1569 ldns_buffer_printf(output
, " do");
1571 /* the extended rcode is the value set, shifted four bits,
1572 * and or'd with the original rcode */
1573 if (ldns_pkt_edns_extended_rcode(pkt
)) {
1574 ldns_buffer_printf(output
, " ; ext-rcode: %d",
1575 (ldns_pkt_edns_extended_rcode(pkt
) << 4 | ldns_pkt_get_rcode(pkt
)));
1577 ldns_buffer_printf(output
, " ; udp: %u\n",
1578 ldns_pkt_edns_udp_size(pkt
));
1580 if (ldns_pkt_edns_data(pkt
)) {
1581 ldns_buffer_printf(output
, ";; Data: ");
1582 (void)ldns_rdf2buffer_str(output
,
1583 ldns_pkt_edns_data(pkt
));
1584 ldns_buffer_printf(output
, "\n");
1587 if (ldns_pkt_tsig(pkt
)) {
1588 ldns_buffer_printf(output
, ";; TSIG:\n;; ");
1589 (void) ldns_rr2buffer_str_fmt(
1590 output
, fmt
, ldns_pkt_tsig(pkt
));
1591 ldns_buffer_printf(output
, "\n");
1593 if (ldns_pkt_answerfrom(pkt
)) {
1594 tmp
= ldns_rdf2str(ldns_pkt_answerfrom(pkt
));
1595 ldns_buffer_printf(output
, ";; SERVER: %s\n", tmp
);
1598 time
= ldns_pkt_timestamp(pkt
);
1599 time_tt
= (time_t)time
.tv_sec
;
1600 ldns_buffer_printf(output
, ";; WHEN: %s",
1601 (char*)ctime(&time_tt
));
1603 ldns_buffer_printf(output
, ";; MSG SIZE rcvd: %d\n",
1604 (int)ldns_pkt_size(pkt
));
1606 return ldns_buffer_status(output
);
1612 ldns_pkt2buffer_str(ldns_buffer
*output
, const ldns_pkt
*pkt
)
1614 return ldns_pkt2buffer_str_fmt(output
, ldns_output_format_default
, pkt
);
1620 ldns_hmac_key2buffer_str(ldns_buffer
*output
, const ldns_key
*k
)
1624 ldns_rdf
*b64_bignum
;
1626 ldns_buffer_printf(output
, "Key: ");
1628 i
= ldns_key_hmac_size(k
);
1629 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, ldns_key_hmac_key(k
));
1630 status
= ldns_rdf2buffer_str(output
, b64_bignum
);
1631 ldns_rdf_deep_free(b64_bignum
);
1632 ldns_buffer_printf(output
, "\n");
1637 #if defined(HAVE_SSL) && defined(USE_GOST)
1639 ldns_gost_key2buffer_str(ldns_buffer
*output
, EVP_PKEY
*p
)
1641 unsigned char* pp
= NULL
;
1643 ldns_rdf
*b64_bignum
;
1646 ldns_buffer_printf(output
, "GostAsn1: ");
1648 ret
= i2d_PrivateKey(p
, &pp
);
1649 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, (size_t)ret
, pp
);
1650 status
= ldns_rdf2buffer_str(output
, b64_bignum
);
1652 ldns_rdf_deep_free(b64_bignum
);
1654 ldns_buffer_printf(output
, "\n");
1660 ldns_key2buffer_str(ldns_buffer
*output
, const ldns_key
*k
)
1662 ldns_status status
= LDNS_STATUS_OK
;
1663 unsigned char *bignum
;
1668 /* not used when ssl is not defined */
1670 ldns_rdf
*b64_bignum
= NULL
;
1674 #endif /* HAVE_SSL */
1677 return LDNS_STATUS_ERR
;
1680 bignum
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1682 return LDNS_STATUS_ERR
;
1685 if (ldns_buffer_status_ok(output
)) {
1687 switch(ldns_key_algorithm(k
)) {
1688 case LDNS_SIGN_RSASHA1
:
1689 case LDNS_SIGN_RSASHA1_NSEC3
:
1690 case LDNS_SIGN_RSASHA256
:
1691 case LDNS_SIGN_RSASHA512
:
1692 case LDNS_SIGN_RSAMD5
:
1693 /* copied by looking at dnssec-keygen output */
1695 rsa
= ldns_key_rsa_key(k
);
1697 ldns_buffer_printf(output
,"Private-key-format: v1.2\n");
1698 switch(ldns_key_algorithm(k
)) {
1699 case LDNS_SIGN_RSAMD5
:
1700 ldns_buffer_printf(output
,
1701 "Algorithm: %u (RSA)\n",
1704 case LDNS_SIGN_RSASHA1
:
1705 ldns_buffer_printf(output
,
1706 "Algorithm: %u (RSASHA1)\n",
1709 case LDNS_SIGN_RSASHA1_NSEC3
:
1710 ldns_buffer_printf(output
,
1711 "Algorithm: %u (RSASHA1_NSEC3)\n",
1712 LDNS_RSASHA1_NSEC3
);
1715 case LDNS_SIGN_RSASHA256
:
1716 ldns_buffer_printf(output
,
1717 "Algorithm: %u (RSASHA256)\n",
1720 case LDNS_SIGN_RSASHA512
:
1721 ldns_buffer_printf(output
,
1722 "Algorithm: %u (RSASHA512)\n",
1727 fprintf(stderr
, "Warning: unknown signature ");
1729 "algorithm type %u\n",
1730 ldns_key_algorithm(k
));
1731 ldns_buffer_printf(output
,
1732 "Algorithm: %u (Unknown)\n",
1733 ldns_key_algorithm(k
));
1737 /* print to buf, convert to bin, convert to b64,
1739 ldns_buffer_printf(output
, "Modulus: ");
1741 i
= (uint16_t)BN_bn2bin(rsa
->n
, bignum
);
1742 if (i
> LDNS_MAX_KEYLEN
) {
1745 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1746 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1747 ldns_rdf_deep_free(b64_bignum
);
1750 ldns_rdf_deep_free(b64_bignum
);
1751 ldns_buffer_printf(output
, "\n");
1752 ldns_buffer_printf(output
, "PublicExponent: ");
1753 i
= (uint16_t)BN_bn2bin(rsa
->e
, bignum
);
1754 if (i
> LDNS_MAX_KEYLEN
) {
1757 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1758 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1759 ldns_rdf_deep_free(b64_bignum
);
1762 ldns_rdf_deep_free(b64_bignum
);
1763 ldns_buffer_printf(output
, "\n");
1765 ldns_buffer_printf(output
, "PrivateExponent: ");
1767 i
= (uint16_t)BN_bn2bin(rsa
->d
, bignum
);
1768 if (i
> LDNS_MAX_KEYLEN
) {
1771 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1772 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1773 ldns_rdf_deep_free(b64_bignum
);
1776 ldns_rdf_deep_free(b64_bignum
);
1777 ldns_buffer_printf(output
, "\n");
1779 ldns_buffer_printf(output
, "(Not available)\n");
1782 ldns_buffer_printf(output
, "Prime1: ");
1784 i
= (uint16_t)BN_bn2bin(rsa
->p
, bignum
);
1785 if (i
> LDNS_MAX_KEYLEN
) {
1788 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1789 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1790 ldns_rdf_deep_free(b64_bignum
);
1793 ldns_rdf_deep_free(b64_bignum
);
1794 ldns_buffer_printf(output
, "\n");
1796 ldns_buffer_printf(output
, "(Not available)\n");
1799 ldns_buffer_printf(output
, "Prime2: ");
1801 i
= (uint16_t)BN_bn2bin(rsa
->q
, bignum
);
1802 if (i
> LDNS_MAX_KEYLEN
) {
1805 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1806 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1807 ldns_rdf_deep_free(b64_bignum
);
1810 ldns_rdf_deep_free(b64_bignum
);
1811 ldns_buffer_printf(output
, "\n");
1813 ldns_buffer_printf(output
, "(Not available)\n");
1816 ldns_buffer_printf(output
, "Exponent1: ");
1818 i
= (uint16_t)BN_bn2bin(rsa
->dmp1
, bignum
);
1819 if (i
> LDNS_MAX_KEYLEN
) {
1822 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1823 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1824 ldns_rdf_deep_free(b64_bignum
);
1827 ldns_rdf_deep_free(b64_bignum
);
1828 ldns_buffer_printf(output
, "\n");
1830 ldns_buffer_printf(output
, "(Not available)\n");
1833 ldns_buffer_printf(output
, "Exponent2: ");
1835 i
= (uint16_t)BN_bn2bin(rsa
->dmq1
, bignum
);
1836 if (i
> LDNS_MAX_KEYLEN
) {
1839 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1840 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1841 ldns_rdf_deep_free(b64_bignum
);
1844 ldns_rdf_deep_free(b64_bignum
);
1845 ldns_buffer_printf(output
, "\n");
1847 ldns_buffer_printf(output
, "(Not available)\n");
1850 ldns_buffer_printf(output
, "Coefficient: ");
1852 i
= (uint16_t)BN_bn2bin(rsa
->iqmp
, bignum
);
1853 if (i
> LDNS_MAX_KEYLEN
) {
1856 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1857 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1858 ldns_rdf_deep_free(b64_bignum
);
1861 ldns_rdf_deep_free(b64_bignum
);
1862 ldns_buffer_printf(output
, "\n");
1864 ldns_buffer_printf(output
, "(Not available)\n");
1871 case LDNS_SIGN_DSA_NSEC3
:
1872 dsa
= ldns_key_dsa_key(k
);
1874 ldns_buffer_printf(output
,"Private-key-format: v1.2\n");
1875 if (ldns_key_algorithm(k
) == LDNS_SIGN_DSA
) {
1876 ldns_buffer_printf(output
,"Algorithm: 3 (DSA)\n");
1877 } else if (ldns_key_algorithm(k
) == LDNS_SIGN_DSA_NSEC3
) {
1878 ldns_buffer_printf(output
,"Algorithm: 6 (DSA_NSEC3)\n");
1881 /* print to buf, convert to bin, convert to b64,
1883 ldns_buffer_printf(output
, "Prime(p): ");
1886 i
= (uint16_t)BN_bn2bin(dsa
->p
, bignum
);
1887 if (i
> LDNS_MAX_KEYLEN
) {
1890 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1891 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1892 ldns_rdf_deep_free(b64_bignum
);
1895 ldns_rdf_deep_free(b64_bignum
);
1896 ldns_buffer_printf(output
, "\n");
1898 printf("(Not available)\n");
1901 ldns_buffer_printf(output
, "Subprime(q): ");
1903 i
= (uint16_t)BN_bn2bin(dsa
->q
, bignum
);
1904 if (i
> LDNS_MAX_KEYLEN
) {
1907 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1908 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1909 ldns_rdf_deep_free(b64_bignum
);
1912 ldns_rdf_deep_free(b64_bignum
);
1913 ldns_buffer_printf(output
, "\n");
1915 printf("(Not available)\n");
1918 ldns_buffer_printf(output
, "Base(g): ");
1920 i
= (uint16_t)BN_bn2bin(dsa
->g
, bignum
);
1921 if (i
> LDNS_MAX_KEYLEN
) {
1924 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1925 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1926 ldns_rdf_deep_free(b64_bignum
);
1929 ldns_rdf_deep_free(b64_bignum
);
1930 ldns_buffer_printf(output
, "\n");
1932 printf("(Not available)\n");
1935 ldns_buffer_printf(output
, "Private_value(x): ");
1936 if (dsa
->priv_key
) {
1937 i
= (uint16_t)BN_bn2bin(dsa
->priv_key
, bignum
);
1938 if (i
> LDNS_MAX_KEYLEN
) {
1941 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1942 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1943 ldns_rdf_deep_free(b64_bignum
);
1946 ldns_rdf_deep_free(b64_bignum
);
1947 ldns_buffer_printf(output
, "\n");
1949 printf("(Not available)\n");
1952 ldns_buffer_printf(output
, "Public_value(y): ");
1954 i
= (uint16_t)BN_bn2bin(dsa
->pub_key
, bignum
);
1955 if (i
> LDNS_MAX_KEYLEN
) {
1958 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
1959 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
1960 ldns_rdf_deep_free(b64_bignum
);
1963 ldns_rdf_deep_free(b64_bignum
);
1964 ldns_buffer_printf(output
, "\n");
1966 printf("(Not available)\n");
1970 case LDNS_SIGN_ECC_GOST
:
1971 /* no format defined, use blob */
1972 #if defined(HAVE_SSL) && defined(USE_GOST)
1973 ldns_buffer_printf(output
, "Private-key-format: v1.2\n");
1974 ldns_buffer_printf(output
, "Algorithm: %d (ECC-GOST)\n", LDNS_SIGN_ECC_GOST
);
1975 status
= ldns_gost_key2buffer_str(output
,
1986 case LDNS_SIGN_ECDSAP256SHA256
:
1987 case LDNS_SIGN_ECDSAP384SHA384
:
1989 ldns_buffer_printf(output
, "Private-key-format: v1.2\n");
1990 ldns_buffer_printf(output
, "Algorithm: %d (", ldns_key_algorithm(k
));
1991 status
=ldns_algorithm2buffer_str(output
, (ldns_algorithm
)ldns_key_algorithm(k
));
1993 ldns_buffer_printf(output
, ")\n");
1995 EC_KEY
* ec
= EVP_PKEY_get1_EC_KEY(k
->_key
.key
);
1996 const BIGNUM
* b
= EC_KEY_get0_private_key(ec
);
1997 ldns_buffer_printf(output
, "PrivateKey: ");
1998 i
= (uint16_t)BN_bn2bin(b
, bignum
);
1999 if (i
> LDNS_MAX_KEYLEN
) {
2002 b64_bignum
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, i
, bignum
);
2003 if (ldns_rdf2buffer_str(output
, b64_bignum
) != LDNS_STATUS_OK
) {
2004 ldns_rdf_deep_free(b64_bignum
);
2007 ldns_rdf_deep_free(b64_bignum
);
2008 ldns_buffer_printf(output
, "\n");
2009 /* down reference count in EC_KEY
2010 * its still assigned to the PKEY */
2018 case LDNS_SIGN_HMACMD5
:
2019 /* there's not much of a format defined for TSIG */
2020 /* It's just a binary blob, Same for all algorithms */
2021 ldns_buffer_printf(output
, "Private-key-format: v1.2\n");
2022 ldns_buffer_printf(output
, "Algorithm: 157 (HMAC_MD5)\n");
2023 status
= ldns_hmac_key2buffer_str(output
, k
);
2025 case LDNS_SIGN_HMACSHA1
:
2026 ldns_buffer_printf(output
, "Private-key-format: v1.2\n");
2027 ldns_buffer_printf(output
, "Algorithm: 158 (HMAC_SHA1)\n");
2028 status
= ldns_hmac_key2buffer_str(output
, k
);
2030 case LDNS_SIGN_HMACSHA256
:
2031 ldns_buffer_printf(output
, "Private-key-format: v1.2\n");
2032 ldns_buffer_printf(output
, "Algorithm: 159 (HMAC_SHA256)\n");
2033 status
= ldns_hmac_key2buffer_str(output
, k
);
2036 #endif /* HAVE_SSL */
2039 return ldns_buffer_status(output
);
2045 /* compiles warn the label isn't used */
2048 return LDNS_STATUS_ERR
;
2049 #endif /* HAVE_SSL */
2054 * Zero terminate the buffer and copy data.
2057 ldns_buffer2str(ldns_buffer
*buffer
)
2061 /* check if buffer ends with \0, if not, and
2062 if there is space, add it */
2063 if (*(ldns_buffer_at(buffer
, ldns_buffer_position(buffer
))) != 0) {
2064 if (!ldns_buffer_reserve(buffer
, 1)) {
2067 ldns_buffer_write_u8(buffer
, (uint8_t) '\0');
2068 if (!ldns_buffer_set_capacity(buffer
, ldns_buffer_position(buffer
))) {
2073 str
= strdup((const char *)ldns_buffer_begin(buffer
));
2081 * Zero terminate the buffer and export data.
2084 ldns_buffer_export2str(ldns_buffer
*buffer
)
2086 /* Append '\0' as string terminator */
2087 if (! ldns_buffer_reserve(buffer
, 1)) {
2090 ldns_buffer_write_u8(buffer
, 0);
2092 /* reallocate memory to the size of the string and export */
2093 ldns_buffer_set_capacity(buffer
, ldns_buffer_position(buffer
));
2094 return ldns_buffer_export(buffer
);
2098 ldns_rdf2str(const ldns_rdf
*rdf
)
2100 char *result
= NULL
;
2101 ldns_buffer
*tmp_buffer
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
2106 if (ldns_rdf2buffer_str(tmp_buffer
, rdf
) == LDNS_STATUS_OK
) {
2107 /* export and return string, destroy rest */
2108 result
= ldns_buffer_export2str(tmp_buffer
);
2110 ldns_buffer_free(tmp_buffer
);
2115 ldns_rr2str_fmt(const ldns_output_format
*fmt
, const ldns_rr
*rr
)
2117 char *result
= NULL
;
2118 ldns_buffer
*tmp_buffer
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
2123 if (ldns_rr2buffer_str_fmt(tmp_buffer
, fmt
, rr
)
2124 == LDNS_STATUS_OK
) {
2125 /* export and return string, destroy rest */
2126 result
= ldns_buffer_export2str(tmp_buffer
);
2128 ldns_buffer_free(tmp_buffer
);
2133 ldns_rr2str(const ldns_rr
*rr
)
2135 return ldns_rr2str_fmt(ldns_output_format_default
, rr
);
2139 ldns_pkt2str_fmt(const ldns_output_format
*fmt
, const ldns_pkt
*pkt
)
2141 char *result
= NULL
;
2142 ldns_buffer
*tmp_buffer
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
2147 if (ldns_pkt2buffer_str_fmt(tmp_buffer
, fmt
, pkt
)
2148 == LDNS_STATUS_OK
) {
2149 /* export and return string, destroy rest */
2150 result
= ldns_buffer_export2str(tmp_buffer
);
2153 ldns_buffer_free(tmp_buffer
);
2158 ldns_pkt2str(const ldns_pkt
*pkt
)
2160 return ldns_pkt2str_fmt(ldns_output_format_default
, pkt
);
2164 ldns_key2str(const ldns_key
*k
)
2166 char *result
= NULL
;
2167 ldns_buffer
*tmp_buffer
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
2172 if (ldns_key2buffer_str(tmp_buffer
, k
) == LDNS_STATUS_OK
) {
2173 /* export and return string, destroy rest */
2174 result
= ldns_buffer_export2str(tmp_buffer
);
2176 ldns_buffer_free(tmp_buffer
);
2181 ldns_rr_list2str_fmt(const ldns_output_format
*fmt
, const ldns_rr_list
*list
)
2183 char *result
= NULL
;
2184 ldns_buffer
*tmp_buffer
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
2190 if (ldns_rr_list2buffer_str_fmt(
2191 tmp_buffer
, fmt
, list
)
2192 == LDNS_STATUS_OK
) {
2196 fmt
= ldns_output_format_default
;
2198 if (fmt
->flags
& LDNS_COMMENT_NULLS
) {
2199 ldns_buffer_printf(tmp_buffer
, "; (null)\n");
2203 /* export and return string, destroy rest */
2204 result
= ldns_buffer_export2str(tmp_buffer
);
2205 ldns_buffer_free(tmp_buffer
);
2210 ldns_rr_list2str(const ldns_rr_list
*list
)
2212 return ldns_rr_list2str_fmt(ldns_output_format_default
, list
);
2216 ldns_rdf_print(FILE *output
, const ldns_rdf
*rdf
)
2218 char *str
= ldns_rdf2str(rdf
);
2220 fprintf(output
, "%s", str
);
2222 fprintf(output
, ";Unable to convert rdf to string\n");
2228 ldns_rr_print_fmt(FILE *output
,
2229 const ldns_output_format
*fmt
, const ldns_rr
*rr
)
2231 char *str
= ldns_rr2str_fmt(fmt
, rr
);
2233 fprintf(output
, "%s", str
);
2235 fprintf(output
, ";Unable to convert rr to string\n");
2241 ldns_rr_print(FILE *output
, const ldns_rr
*rr
)
2243 ldns_rr_print_fmt(output
, ldns_output_format_default
, rr
);
2247 ldns_pkt_print_fmt(FILE *output
,
2248 const ldns_output_format
*fmt
, const ldns_pkt
*pkt
)
2250 char *str
= ldns_pkt2str_fmt(fmt
, pkt
);
2252 fprintf(output
, "%s", str
);
2254 fprintf(output
, ";Unable to convert packet to string\n");
2260 ldns_pkt_print(FILE *output
, const ldns_pkt
*pkt
)
2262 ldns_pkt_print_fmt(output
, ldns_output_format_default
, pkt
);
2266 ldns_rr_list_print_fmt(FILE *output
,
2267 const ldns_output_format
*fmt
, const ldns_rr_list
*lst
)
2270 for (i
= 0; i
< ldns_rr_list_rr_count(lst
); i
++) {
2271 ldns_rr_print_fmt(output
, fmt
, ldns_rr_list_rr(lst
, i
));
2276 ldns_rr_list_print(FILE *output
, const ldns_rr_list
*lst
)
2278 ldns_rr_list_print_fmt(output
, ldns_output_format_default
, lst
);
2282 ldns_resolver_print_fmt(FILE *output
,
2283 const ldns_output_format
*fmt
, const ldns_resolver
*r
)
2292 n
= ldns_resolver_nameservers(r
);
2293 s
= ldns_resolver_searchlist(r
);
2294 rtt
= ldns_resolver_rtt(r
);
2296 fprintf(output
, "port: %d\n", (int)ldns_resolver_port(r
));
2297 fprintf(output
, "edns0 size: %d\n", (int)ldns_resolver_edns_udp_size(r
));
2298 fprintf(output
, "use ip6: %d\n", (int)ldns_resolver_ip6(r
));
2300 fprintf(output
, "recursive: %d\n", ldns_resolver_recursive(r
));
2301 fprintf(output
, "usevc: %d\n", ldns_resolver_usevc(r
));
2302 fprintf(output
, "igntc: %d\n", ldns_resolver_igntc(r
));
2303 fprintf(output
, "fail: %d\n", ldns_resolver_fail(r
));
2304 fprintf(output
, "retry: %d\n", (int)ldns_resolver_retry(r
));
2305 fprintf(output
, "retrans: %d\n", (int)ldns_resolver_retrans(r
));
2306 fprintf(output
, "fallback: %d\n", ldns_resolver_fallback(r
));
2307 fprintf(output
, "random: %d\n", ldns_resolver_random(r
));
2308 fprintf(output
, "timeout: %d\n", (int)ldns_resolver_timeout(r
).tv_sec
);
2309 fprintf(output
, "dnssec: %d\n", ldns_resolver_dnssec(r
));
2310 fprintf(output
, "dnssec cd: %d\n", ldns_resolver_dnssec_cd(r
));
2311 fprintf(output
, "trust anchors (%d listed):\n",
2312 (int)ldns_rr_list_rr_count(ldns_resolver_dnssec_anchors(r
)));
2313 ldns_rr_list_print_fmt(output
, fmt
, ldns_resolver_dnssec_anchors(r
));
2314 fprintf(output
, "tsig: %s %s\n",
2315 ldns_resolver_tsig_keyname(r
)?ldns_resolver_tsig_keyname(r
):"-",
2316 ldns_resolver_tsig_algorithm(r
)?ldns_resolver_tsig_algorithm(r
):"-");
2317 fprintf(output
, "debug: %d\n", ldns_resolver_debug(r
));
2319 fprintf(output
, "default domain: ");
2320 ldns_rdf_print(output
, ldns_resolver_domain(r
));
2321 fprintf(output
, "\n");
2322 fprintf(output
, "apply default domain: %d\n", ldns_resolver_defnames(r
));
2324 fprintf(output
, "searchlist (%d listed):\n", (int)ldns_resolver_searchlist_count(r
));
2325 for (i
= 0; i
< ldns_resolver_searchlist_count(r
); i
++) {
2326 fprintf(output
, "\t");
2327 ldns_rdf_print(output
, s
[i
]);
2328 fprintf(output
, "\n");
2330 fprintf(output
, "apply search list: %d\n", ldns_resolver_dnsrch(r
));
2332 fprintf(output
, "nameservers (%d listed):\n", (int)ldns_resolver_nameserver_count(r
));
2333 for (i
= 0; i
< ldns_resolver_nameserver_count(r
); i
++) {
2334 fprintf(output
, "\t");
2335 ldns_rdf_print(output
, n
[i
]);
2337 switch ((int)rtt
[i
]) {
2338 case LDNS_RESOLV_RTT_MIN
:
2339 fprintf(output
, " - reachable\n");
2341 case LDNS_RESOLV_RTT_INF
:
2342 fprintf(output
, " - unreachable\n");
2349 ldns_resolver_print(FILE *output
, const ldns_resolver
*r
)
2351 ldns_resolver_print_fmt(output
, ldns_output_format_default
, r
);
2355 ldns_zone_print_fmt(FILE *output
,
2356 const ldns_output_format
*fmt
, const ldns_zone
*z
)
2358 if(ldns_zone_soa(z
))
2359 ldns_rr_print_fmt(output
, fmt
, ldns_zone_soa(z
));
2360 ldns_rr_list_print_fmt(output
, fmt
, ldns_zone_rrs(z
));
2363 ldns_zone_print(FILE *output
, const ldns_zone
*z
)
2365 ldns_zone_print_fmt(output
, ldns_output_format_default
, z
);