diff --git a/include/dns_decoder.h b/include/dns_decoder.h index b41f7b9..074ab92 100644 --- a/include/dns_decoder.h +++ b/include/dns_decoder.h @@ -11,10 +11,6 @@ extern "C" #define DNS_MESSAGE_TOPIC "TOPIC_DNS_MESSAGE" -struct dns_message; -struct dns_query_question; -struct dns_resource_record; - /*. First call DNS_MESSAGE_TRANSACTION_BEGIN to create the transaction, then publish the transaction's DNS_MESSAGE_QUERY/DNS_MESSAGE_RESPONSE, @@ -29,8 +25,6 @@ enum dns_message_type DNS_MESSAGE_MAX }; -enum dns_message_type dns_message_type_get(struct dns_message *msg); - struct dns_flag { uint8_t qr:1; @@ -43,6 +37,12 @@ struct dns_flag uint8_t rcode:4; }; +struct dns_message; +struct dns_query_question; +struct dns_resource_record; + +enum dns_message_type dns_message_type_get(struct dns_message *msg); + int32_t dns_message_transaction_index_get(struct dns_message *msg); int32_t dns_message_header_id_get(struct dns_message *msg); @@ -53,11 +53,9 @@ const char *dns_query_question_qname_get0(struct dns_query_question *question); int32_t dns_query_question_qtype_get0(struct dns_query_question *question); int32_t dns_query_question_qclass_get0(struct dns_query_question *question); -void dns_message_answer_resource_record_get0(struct dns_message *msg, struct dns_resource_record **answer_rr, uint16_t *n_answer_rr); -void dns_message_authority_resource_record_get0(struct dns_message *msg, struct dns_resource_record **authority_rr, uint16_t *n_authority_rr); -void dns_message_additional_resource_record_get0(struct dns_message *msg, struct dns_resource_record **additional_rr, uint16_t *n_additional_rr); - -const char *dns_resource_record_json_exporter(struct dns_resource_record *rr_array, uint16_t n_rr); +int dns_message_resource_record_is_dnssec(struct dns_message *msg); +const char *dns_message_resource_record_json_exporter(struct dns_message *msg); +const char *dns_message_resource_record_cname_json_exporter(struct dns_message *msg); #ifdef __cplusplus } diff --git a/perf/byte_to_hex_highly_optimized b/perf/byte_to_hex_highly_optimized deleted file mode 100755 index 253ef7a..0000000 Binary files a/perf/byte_to_hex_highly_optimized and /dev/null differ diff --git a/src/dns_resource_record.h b/src/dns_resource_record.h index 6070b20..54998ac 100644 --- a/src/dns_resource_record.h +++ b/src/dns_resource_record.h @@ -217,3 +217,8 @@ struct dns_query_question size_t qname_sz; uint8_t qname[DNS_NAME_MAX]; }; + +int32_t dns_message_contains_resource_record(struct dns_message *msg); +void dns_message_answer_resource_record_get0(struct dns_message *msg, struct dns_resource_record **answer_rr, uint16_t *n_answer_rr); +void dns_message_authority_resource_record_get0(struct dns_message *msg, struct dns_resource_record **authority_rr, uint16_t *n_authority_rr); +void dns_message_additional_resource_record_get0(struct dns_message *msg, struct dns_resource_record **additional_rr, uint16_t *n_additional_rr); \ No newline at end of file diff --git a/src/dns_resource_record_exporter.cpp b/src/dns_resource_record_exporter.cpp index 7dd5900..4a0e38e 100644 --- a/src/dns_resource_record_exporter.cpp +++ b/src/dns_resource_record_exporter.cpp @@ -41,7 +41,7 @@ void dns_resource_record_dstring_append(cJSON *one_rr_object, struct dstring *ds } } -const char *dns_resource_record_json_exporter(struct dns_resource_record *rr_array, uint16_t n_rr) +cJSON *dns_resource_record_json_create(struct dns_resource_record *rr_array, uint16_t n_rr) { if(rr_array==NULL || n_rr==0) { @@ -222,9 +222,149 @@ const char *dns_resource_record_json_exporter(struct dns_resource_record *rr_arr cJSON_AddItemToArray(dns_rr_array, one_rr_object); } + + return dns_rr_array; +} - char *json_str=cJSON_PrintUnformatted(dns_rr_array); - cJSON_Delete(dns_rr_array); +const char *dns_message_resource_record_json_exporter(struct dns_message *msg) +{ + uint16_t n_answer_rr=0; + struct dns_resource_record *answer_rr=NULL; + dns_message_answer_resource_record_get0(msg, &answer_rr, &n_answer_rr); + + uint16_t n_authority_rr=0; + struct dns_resource_record *authority_rr=NULL; + dns_message_authority_resource_record_get0(msg, &authority_rr, &n_authority_rr); + + uint16_t n_additional_rr=0; + struct dns_resource_record *additional_rr=NULL; + dns_message_additional_resource_record_get0(msg, &additional_rr, &n_additional_rr); + + if(n_answer_rr==0 && n_authority_rr==0 && n_additional_rr==0) + { + return NULL; + } + + cJSON *answer=dns_resource_record_json_create(answer_rr, n_answer_rr); + cJSON *authority=dns_resource_record_json_create(authority_rr, n_authority_rr); + cJSON *additional=dns_resource_record_json_create(additional_rr, n_additional_rr); + + cJSON *rr_array=cJSON_CreateObject(); + if(answer!=NULL) + { + cJSON_AddItemToObject(rr_array, "answer", answer); + } + + if(authority!=NULL) + { + cJSON_AddItemToObject(rr_array, "authority", authority); + } + + if(additional!=NULL) + { + cJSON_AddItemToObject(rr_array, "additional", additional); + } + + char *json_str=cJSON_PrintUnformatted(rr_array); + cJSON_Delete(rr_array); return json_str; +} + +int dns_resource_record_is_dnssec(struct dns_resource_record *rr_array, uint16_t n_rr) +{ + for(uint16_t i=0; ifse.handle, tid, main_env->fse.id[PERF_TAG_QUESTION], &(main_env->fse.tag[PERF_TAG_QUESTION]), 1, time_diff_ns) } diff --git a/test/dns_decoder_test.cpp b/test/dns_decoder_test.cpp index 44931e5..0472ad5 100644 --- a/test/dns_decoder_test.cpp +++ b/test/dns_decoder_test.cpp @@ -85,46 +85,13 @@ void dns_decoder_test_message_cb(struct session *ss, int topic_id, const void *m cJSON_AddNumberToObject(real_result, "dns_rd", (double)(flag->rd)); } - uint16_t n_answer_rr=0; - struct dns_resource_record *answer_rr=NULL; - dns_message_answer_resource_record_get0(dns_msg, &answer_rr, &n_answer_rr); - - uint16_t n_authority_rr=0; - struct dns_resource_record *authority_rr=NULL; - dns_message_authority_resource_record_get0(dns_msg, &authority_rr, &n_authority_rr); - - uint16_t n_additional_rr=0; - struct dns_resource_record *additional_rr=NULL; - dns_message_additional_resource_record_get0(dns_msg, &additional_rr, &n_additional_rr); - - const char *answer=dns_resource_record_json_exporter(answer_rr, n_answer_rr); - const char *authority=dns_resource_record_json_exporter(authority_rr, n_authority_rr); - const char *additional=dns_resource_record_json_exporter(additional_rr, n_additional_rr); - - cJSON *rr_array=cJSON_CreateObject(); - if(answer!=NULL) + const char *resource_record_str=dns_message_resource_record_json_exporter(dns_msg); + if(resource_record_str!=NULL) { - cJSON *rr_object=cJSON_Parse(answer); - cJSON_AddItemToObject(rr_array, "answer", rr_object); - free((void *)answer); + cJSON *rr_array=cJSON_Parse(resource_record_str); + cJSON_AddItemToObject(real_result, "rr", rr_array); } - if(authority!=NULL) - { - cJSON *rr_object=cJSON_Parse(authority); - cJSON_AddItemToObject(rr_array, "authority", rr_object); - free((void *)authority); - } - - if(additional!=NULL) - { - cJSON *rr_object=cJSON_Parse(additional); - cJSON_AddItemToObject(rr_array, "additional", rr_object); - free((void *)additional); - } - - cJSON_AddItemToObject(real_result, "rr", rr_array); - if(plugin_env->write_result_enable==1) { char *real_result_str=cJSON_Print(real_result);