From: Willem Toorop Date: Mon, 5 Sep 2011 15:04:30 +0000 (+0000) Subject: New versions of the functions that convert packets, rr's and rr-lists to string. X-Git-Tag: release-1.6.11rc1~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=415664a016637ca6ab5b0161b687fce3793205dd;p=thirdparty%2Fldns.git New versions of the functions that convert packets, rr's and rr-lists to string. Before, default comments were giving describing various aspects of the RR. With the new functions one can specify which comments are desirable. See RT#539 --- diff --git a/host2str.c b/host2str.c index c9d644f9..c217a0af 100644 --- a/host2str.c +++ b/host2str.c @@ -1139,124 +1139,162 @@ ldns_rdf2buffer_str(ldns_buffer *buffer, const ldns_rdf *rdf) return res; } + ldns_status -ldns_rr2buffer_str(ldns_buffer *output, const ldns_rr *rr) +ldns_rr2buffer_str_comments( + ldns_buffer *output, const ldns_rr *rr, int comments) { uint16_t i, flags; ldns_status status = LDNS_STATUS_OK; if (!rr) { - ldns_buffer_printf(output, "(null)\n"); - } else { - if (ldns_rr_owner(rr)) { - status = ldns_rdf2buffer_str_dname(output, ldns_rr_owner(rr)); - } - if (status != LDNS_STATUS_OK) { - return status; + if (LDNS_COMMENT_NULL & comments) { + ldns_buffer_printf(output, "; (null)\n"); } + return ldns_buffer_status(output); + } + if (ldns_rr_owner(rr)) { + status = ldns_rdf2buffer_str_dname(output, ldns_rr_owner(rr)); + } + if (status != LDNS_STATUS_OK) { + return status; + } - /* TTL should NOT be printed if it is a question */ - if (!ldns_rr_is_question(rr)) { - ldns_buffer_printf(output, "\t%d", ldns_rr_ttl(rr)); - } + /* TTL should NOT be printed if it is a question */ + if (!ldns_rr_is_question(rr)) { + ldns_buffer_printf(output, "\t%d", ldns_rr_ttl(rr)); + } - ldns_buffer_printf(output, "\t"); - status = ldns_rr_class2buffer_str(output, ldns_rr_get_class(rr)); - if (status != LDNS_STATUS_OK) { - return status; - } - ldns_buffer_printf(output, "\t"); + ldns_buffer_printf(output, "\t"); + status = ldns_rr_class2buffer_str(output, ldns_rr_get_class(rr)); + if (status != LDNS_STATUS_OK) { + return status; + } + ldns_buffer_printf(output, "\t"); - status = ldns_rr_type2buffer_str(output, ldns_rr_get_type(rr)); - if (status != LDNS_STATUS_OK) { - return status; - } + status = ldns_rr_type2buffer_str(output, ldns_rr_get_type(rr)); + if (status != LDNS_STATUS_OK) { + return status; + } - if (ldns_rr_rd_count(rr) > 0) { - ldns_buffer_printf(output, "\t"); - } else if (!ldns_rr_is_question(rr)) { - ldns_buffer_printf(output, "\t\\# 0"); - } + if (ldns_rr_rd_count(rr) > 0) { + ldns_buffer_printf(output, "\t"); + } else if (!ldns_rr_is_question(rr)) { + ldns_buffer_printf(output, "\t\\# 0"); + } - for (i = 0; i < ldns_rr_rd_count(rr); i++) { - /* ldns_rdf2buffer_str handles NULL input fine! */ - status = ldns_rdf2buffer_str(output, ldns_rr_rdf(rr, i)); - if(status != LDNS_STATUS_OK) - return status; - if (i < ldns_rr_rd_count(rr) - 1) { - ldns_buffer_printf(output, " "); - } + for (i = 0; i < ldns_rr_rd_count(rr); i++) { + /* ldns_rdf2buffer_str handles NULL input fine! */ + status = ldns_rdf2buffer_str(output, ldns_rr_rdf(rr, i)); + if(status != LDNS_STATUS_OK) + return status; + if (i < ldns_rr_rd_count(rr) - 1) { + ldns_buffer_printf(output, " "); } - /* per RR special comments - handy for DNSSEC types */ - /* check to prevent question sec. rr from - * getting here */ - if (ldns_rr_rd_count(rr) > 0) { - switch (ldns_rr_get_type(rr)) { - case LDNS_RR_TYPE_DNSKEY: - /* if ldns_rr_rd_count(rr) > 0 - then ldns_rr_rdf(rr, 0) exists! */ - flags = ldns_rdf2native_int16(ldns_rr_rdf(rr, 0)); - if (flags == 256 || flags == 384) { - ldns_buffer_printf(output, - " ;{id = %u (zsk), size = %db}", - (unsigned int) ldns_calc_keytag(rr), - ldns_rr_dnskey_key_size(rr)); - break; - } - if (flags == 257 || flags == 385) { - ldns_buffer_printf(output, - " ;{id = %u (ksk), size = %db}", - (unsigned int) ldns_calc_keytag(rr), - ldns_rr_dnskey_key_size(rr)); - break; - } - ldns_buffer_printf(output, " ;{id = %u, size = %db}", - (unsigned int) ldns_calc_keytag(rr), - ldns_rr_dnskey_key_size(rr)); + } + /* per RR special comments - handy for DNSSEC types */ + /* check to prevent question sec. rr from + * getting here */ + if (ldns_rr_rd_count(rr) > 0) { + switch (ldns_rr_get_type(rr)) { + case LDNS_RR_TYPE_DNSKEY: + /* if ldns_rr_rd_count(rr) > 0 + then ldns_rr_rdf(rr, 0) exists! */ + if (! (comments & LDNS_COMMENT_KEY)) { break; - case LDNS_RR_TYPE_RRSIG: - if (ldns_rr_rdf(rr, 6) != NULL) { - ldns_buffer_printf(output, " ;{id = %d}", - ldns_rdf2native_int16(ldns_rr_rdf(rr, 6))); + } + flags = ldns_rdf2native_int16(ldns_rr_rdf(rr, 0)); + ldns_buffer_printf(output, " ;{"); + if (comments & LDNS_COMMENT_KEY_ID) { + ldns_buffer_printf(output, "id = %u", + (unsigned int) + ldns_calc_keytag(rr)); + } + if ((comments & LDNS_COMMENT_KEY_TYPE) + && (flags & LDNS_KEY_ZONE_KEY)){ + if (flags & LDNS_KEY_SEP_KEY) { + ldns_buffer_printf( + output, " (ksk)"); } - break; - case LDNS_RR_TYPE_DS: - if (ldns_rr_rdf(rr, 3) != NULL) { - uint8_t *data = ldns_rdf_data(ldns_rr_rdf(rr, 3)); - size_t len = ldns_rdf_size(ldns_rr_rdf(rr, 3)); - char *babble = ldns_bubblebabble(data, len); - if(babble) - ldns_buffer_printf(output, " ; %s", babble); - LDNS_FREE(babble); + else { + ldns_buffer_printf( + output, " (zsk)"); } - break; - case LDNS_RR_TYPE_NSEC3: - if (ldns_nsec3_optout(rr)) { - ldns_buffer_printf(output, " ; flags: optout"); + if (comments & LDNS_COMMENT_KEY_SIZE) { + ldns_buffer_printf( + output, ", "); } - break; - default: - break; + } else if (comments + & (LDNS_COMMENT_KEY_ID + |LDNS_COMMENT_KEY_SIZE)) { + ldns_buffer_printf( output, ", "); + } + if (comments & LDNS_COMMENT_KEY_SIZE) { + ldns_buffer_printf(output, "size = %db", + ldns_rr_dnskey_key_size(rr)); + } + ldns_buffer_printf(output, "}"); + break; + case LDNS_RR_TYPE_RRSIG: + if ((comments & LDNS_COMMENT_KEY) + && ldns_rr_rdf(rr, 6) != NULL) { + ldns_buffer_printf(output, " ;{id = %d}", + ldns_rdf2native_int16(ldns_rr_rdf(rr, 6))); + } + break; + case LDNS_RR_TYPE_DS: + if ((comments & LDNS_COMMENT_BUBBLEBABBLE) + && ldns_rr_rdf(rr, 3) != NULL) { + uint8_t *data = ldns_rdf_data(ldns_rr_rdf(rr, 3)); + size_t len = ldns_rdf_size(ldns_rr_rdf(rr, 3)); + char *babble = ldns_bubblebabble(data, len); + if(babble) + ldns_buffer_printf(output, " ; %s", babble); + LDNS_FREE(babble); + } + break; + case LDNS_RR_TYPE_NSEC3: + if ((comments & LDNS_COMMENT_FLAGS) + && ldns_nsec3_optout(rr)) { + ldns_buffer_printf(output, " ; flags: optout"); + } + break; + default: + break; - } } - /* last */ - ldns_buffer_printf(output, "\n"); } + /* last */ + ldns_buffer_printf(output, "\n"); return ldns_buffer_status(output); } ldns_status -ldns_rr_list2buffer_str(ldns_buffer *output, const ldns_rr_list *list) +ldns_rr2buffer_str(ldns_buffer *output, const ldns_rr *rr) +{ + return ldns_rr2buffer_str_comments(output, rr, LDNS_COMMENT_DEFAULT); +} + +ldns_status +ldns_rr_list2buffer_str_comments( + ldns_buffer *output, const ldns_rr_list *list, int comments) { uint16_t i; for(i = 0; i < ldns_rr_list_rr_count(list); i++) { - (void) ldns_rr2buffer_str(output, ldns_rr_list_rr(list, i)); + (void) ldns_rr2buffer_str_comments( + output, ldns_rr_list_rr(list, i), comments); } return ldns_buffer_status(output); } +ldns_status +ldns_rr_list2buffer_str(ldns_buffer *output, const ldns_rr_list *list) +{ + return ldns_rr_list2buffer_str_comments( + output, list, LDNS_COMMENT_DEFAULT); +} + ldns_status ldns_pktheader2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) { @@ -1310,7 +1348,8 @@ ldns_pktheader2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) } ldns_status -ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) +ldns_pkt2buffer_str_comments( + ldns_buffer *output, const ldns_pkt *pkt, int comments) { uint16_t i; ldns_status status = LDNS_STATUS_OK; @@ -1335,8 +1374,9 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) for (i = 0; i < ldns_pkt_qdcount(pkt); i++) { - status = ldns_rr2buffer_str(output, - ldns_rr_list_rr(ldns_pkt_question(pkt), i)); + status = ldns_rr2buffer_str_comments(output, + ldns_rr_list_rr(ldns_pkt_question(pkt), i), + comments); if (status != LDNS_STATUS_OK) { return status; } @@ -1345,8 +1385,9 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) ldns_buffer_printf(output, ";; ANSWER SECTION:\n"); for (i = 0; i < ldns_pkt_ancount(pkt); i++) { - status = ldns_rr2buffer_str(output, - ldns_rr_list_rr(ldns_pkt_answer(pkt), i)); + status = ldns_rr2buffer_str_comments(output, + ldns_rr_list_rr(ldns_pkt_answer(pkt), i), + comments); if (status != LDNS_STATUS_OK) { return status; } @@ -1357,8 +1398,9 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) ldns_buffer_printf(output, ";; AUTHORITY SECTION:\n"); for (i = 0; i < ldns_pkt_nscount(pkt); i++) { - status = ldns_rr2buffer_str(output, - ldns_rr_list_rr(ldns_pkt_authority(pkt), i)); + status = ldns_rr2buffer_str_comments(output, + ldns_rr_list_rr(ldns_pkt_authority(pkt), i), + comments); if (status != LDNS_STATUS_OK) { return status; } @@ -1367,8 +1409,9 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) ldns_buffer_printf(output, ";; ADDITIONAL SECTION:\n"); for (i = 0; i < ldns_pkt_arcount(pkt); i++) { - status = ldns_rr2buffer_str(output, - ldns_rr_list_rr(ldns_pkt_additional(pkt), i)); + status = ldns_rr2buffer_str_comments(output, + ldns_rr_list_rr(ldns_pkt_additional(pkt), i), + comments); if (status != LDNS_STATUS_OK) { return status; } @@ -1403,7 +1446,8 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) } if (ldns_pkt_tsig(pkt)) { ldns_buffer_printf(output, ";; TSIG:\n;; "); - (void) ldns_rr2buffer_str(output, ldns_pkt_tsig(pkt)); + (void) ldns_rr2buffer_str_comments( + output, ldns_pkt_tsig(pkt), comments); ldns_buffer_printf(output, "\n"); } if (ldns_pkt_answerfrom(pkt)) { @@ -1424,6 +1468,13 @@ ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) return status; } +ldns_status +ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt) +{ + return ldns_pkt2buffer_str_comments(output, pkt, LDNS_COMMENT_DEFAULT); +} + + #ifdef HAVE_SSL static ldns_status ldns_hmac_key2buffer_str(ldns_buffer *output, const ldns_key *k) @@ -1894,7 +1945,7 @@ ldns_rdf2str(const ldns_rdf *rdf) } char * -ldns_rr2str(const ldns_rr *rr) +ldns_rr2str_comments(const ldns_rr *rr, int comments) { char *result = NULL; ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN); @@ -1902,7 +1953,8 @@ ldns_rr2str(const ldns_rr *rr) if (!tmp_buffer) { return NULL; } - if (ldns_rr2buffer_str(tmp_buffer, rr) == LDNS_STATUS_OK) { + if (ldns_rr2buffer_str_comments(tmp_buffer, rr, comments) + == LDNS_STATUS_OK) { /* export and return string, destroy rest */ result = ldns_buffer2str(tmp_buffer); } @@ -1911,7 +1963,13 @@ ldns_rr2str(const ldns_rr *rr) } char * -ldns_pkt2str(const ldns_pkt *pkt) +ldns_rr2str(const ldns_rr *rr) +{ + return ldns_rr2str_comments(rr, LDNS_COMMENT_DEFAULT); +} + +char * +ldns_pkt2str_comments(const ldns_pkt *pkt, int comments) { char *result = NULL; ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN); @@ -1919,7 +1977,8 @@ ldns_pkt2str(const ldns_pkt *pkt) if (!tmp_buffer) { return NULL; } - if (ldns_pkt2buffer_str(tmp_buffer, pkt) == LDNS_STATUS_OK) { + if (ldns_pkt2buffer_str_comments(tmp_buffer, pkt, comments) + == LDNS_STATUS_OK) { /* export and return string, destroy rest */ result = ldns_buffer2str(tmp_buffer); } @@ -1928,6 +1987,12 @@ ldns_pkt2str(const ldns_pkt *pkt) return result; } +char * +ldns_pkt2str(const ldns_pkt *pkt) +{ + return ldns_pkt2str_comments(pkt, LDNS_COMMENT_DEFAULT); +} + char * ldns_key2str(const ldns_key *k) { @@ -1946,7 +2011,7 @@ ldns_key2str(const ldns_key *k) } char * -ldns_rr_list2str(const ldns_rr_list *list) +ldns_rr_list2str_comments(const ldns_rr_list *list, int comments) { char *result = NULL; ldns_buffer *tmp_buffer = ldns_buffer_new(LDNS_MAX_PACKETLEN); @@ -1955,10 +2020,14 @@ ldns_rr_list2str(const ldns_rr_list *list) return NULL; } if (list) { - if (ldns_rr_list2buffer_str(tmp_buffer, list) == LDNS_STATUS_OK) { + if (ldns_rr_list2buffer_str_comments( + tmp_buffer, list, comments) + == LDNS_STATUS_OK) { } } else { - ldns_buffer_printf(tmp_buffer, "(null)\n"); + if (comments & LDNS_COMMENT_NULL) { + ldns_buffer_printf(tmp_buffer, "; (null)\n"); + } } /* export and return string, destroy rest */ @@ -1967,6 +2036,12 @@ ldns_rr_list2str(const ldns_rr_list *list) return result; } +char * +ldns_rr_list2str(const ldns_rr_list *list) +{ + return ldns_rr_list2str_comments(list, LDNS_COMMENT_DEFAULT); +} + void ldns_rdf_print(FILE *output, const ldns_rdf *rdf) { diff --git a/ldns/host2str.h b/ldns/host2str.h index 07180bbd..22ee4ca8 100644 --- a/ldns/host2str.h +++ b/ldns/host2str.h @@ -39,6 +39,21 @@ extern "C" { #define LDNS_APL_MASK 0x7f #define LDNS_APL_NEGATION 0x80 +#define LDNS_COMMENT_NULL 0x01 +#define LDNS_COMMENT_KEY_ID 0x02 +#define LDNS_COMMENT_KEY_TYPE 0x04 +#define LDNS_COMMENT_KEY_SIZE 0x08 +#define LDNS_COMMENT_KEY (LDNS_COMMENT_KEY_ID \ + |LDNS_COMMENT_KEY_TYPE\ + |LDNS_COMMENT_KEY_SIZE) +#define LDNS_COMMENT_BUBBLEBABBLE 0x10 +#define LDNS_COMMENT_FLAGS 0x20 +#define LDNS_COMMENT_DEFAULT (LDNS_COMMENT_NULL \ + |LDNS_COMMENT_KEY \ + |LDNS_COMMENT_BUBBLEBABBLE\ + |LDNS_COMMENT_FLAGS ) + + /** * Converts an ldns packet opcode value to its mnemonic, and adds that * to the output buffer @@ -352,7 +367,9 @@ ldns_status ldns_rdf2buffer_str(ldns_buffer *output, const ldns_rdf *rdf); /** * Converts the data in the resource record to presentation - * format (as char *) and appends it to the given buffer + * format (as char *) and appends it to the given buffer. + * The presentation format is annotated with comments giving + * additional information on the record. * * \param[in] output pointer to the buffer to append the data to * \param[in] rr the pointer to the rr field to convert @@ -360,6 +377,29 @@ ldns_status ldns_rdf2buffer_str(ldns_buffer *output, const ldns_rdf *rdf); */ ldns_status ldns_rr2buffer_str(ldns_buffer *output, const ldns_rr *rr); +/** + * Converts the data in the resource record to presentation + * format (as char *) and appends it to the given buffer + * + * \param[in] output pointer to the buffer to append the data to + * \param[in] rr the pointer to the rr field to convert + * \param[in] comments flags indicating what type of comments should + * annotate the presentation format. Can be any of + * LDNS_COMMENT_NULL (to also indicate an NULL rr, + * LDNS_COMMENT_KEY_ID (to show key identity), + * LDNS_COMMENT_KEY_TYPE (to show if the key is a ksk or a zsk), + * LDNS_COMMENT_KEY_SIZE (to show the key size), + * LDNS_COMMENT_KEY (to show all key annotations), + * LDNS_COMMENT_BUBBLEBABBLE (to show readable and communicable + * version of a blob of binary data + * (currently only with DS records)), + * LDNS_COMMENT_FLAGS (to show what flags are set (currently only + * if optout flag is set with NSEC3 records)) + * \return status + */ +ldns_status ldns_rr2buffer_str_comments( + ldns_buffer *output, const ldns_rr *rr, int comments); + /** * Converts the data in the DNS packet to presentation * format (as char *) and appends it to the given buffer @@ -369,6 +409,8 @@ ldns_status ldns_rr2buffer_str(ldns_buffer *output, const ldns_rr *rr); * \return status */ ldns_status ldns_pkt2buffer_str(ldns_buffer *output, const ldns_pkt *pkt); +ldns_status ldns_pkt2buffer_str_comments( + ldns_buffer *output, const ldns_pkt *pkt, int comments); /** * Converts an LDNS_RDF_TYPE_NSEC3_SALT rdata element to string format and adds it to the output buffer @@ -440,6 +482,7 @@ char *ldns_rdf2str(const ldns_rdf *rdf); * \return null terminated char * data, or NULL on error */ char *ldns_rr2str(const ldns_rr *rr); +char *ldns_rr2str_comments(const ldns_rr *rr, int comments); /** * Converts the data in the DNS packet to presentation format and @@ -450,6 +493,7 @@ char *ldns_rr2str(const ldns_rr *rr); * \return null terminated char * data, or NULL on error */ char *ldns_pkt2str(const ldns_pkt *pkt); +char *ldns_pkt2str_comments(const ldns_pkt *pkt, int comments); /** * Converts a private key to the test presentation fmt and @@ -470,6 +514,7 @@ char *ldns_key2str(const ldns_key *k); * \return null terminated char * data, or NULL on error */ char *ldns_rr_list2str(const ldns_rr_list *rr_list); +char *ldns_rr_list2str_comments(const ldns_rr_list *rr_list, int comments); /** * Returns the data in the buffer as a null terminated char * string @@ -518,6 +563,7 @@ void ldns_pkt_print(FILE *output, const ldns_pkt *pkt); * \return ldns_status */ ldns_status ldns_rr_list2buffer_str(ldns_buffer *output, const ldns_rr_list *list); +ldns_status ldns_rr_list2buffer_str_comments(ldns_buffer *output, const ldns_rr_list *list, int comments); /** * Converts the header of a packet to presentation format and appends it to diff --git a/ldns_symbols.def b/ldns_symbols.def index 1ec2e686..b93aae2d 100644 --- a/ldns_symbols.def +++ b/ldns_symbols.def @@ -263,8 +263,10 @@ ldns_nsec_type_check ldns_octet ldns_opcodes ldns_pkt2buffer_str +ldns_pkt2buffer_str_comments ldns_pkt2buffer_wire ldns_pkt2str +ldns_pkt2str_comments ldns_pkt2wire ldns_pkt_aa ldns_pkt_ad @@ -511,10 +513,12 @@ ldns_resolver_tsig_keydata ldns_resolver_tsig_keyname ldns_resolver_usevc ldns_rr2buffer_str +ldns_rr2buffer_str_comments ldns_rr2buffer_wire ldns_rr2buffer_wire_canonical ldns_rr2canonical ldns_rr2str +ldns_rr2str_comments ldns_rr2wire ldns_rr_a_address ldns_rr_a_set_address @@ -545,9 +549,11 @@ ldns_rr_get_class ldns_rr_get_type ldns_rr_label_count ldns_rr_list2buffer_str +ldns_rr_list2buffer_str_comments ldns_rr_list2buffer_wire ldns_rr_list2canonical ldns_rr_list2str +ldns_rr_list2str_comments ldns_rr_list_cat ldns_rr_list_cat_clone ldns_rr_list_clone