From ec7ce66e6214a1fb5e6089862750b75b16b157f6 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 6 Jan 2023 15:54:01 +0000 Subject: [PATCH 1/2] Add functions to extract RFC 8914 extended errors --- edns.c | 39 +++++++++++++++++++++++++++++++++++++++ ldns/edns.h | 15 +++++++++++++++ ldns/error.h | 4 +++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/edns.c b/edns.c index 3d002d67..904f173d 100644 --- a/edns.c +++ b/edns.c @@ -10,6 +10,7 @@ * See the file LICENSE for the license */ +#include #include #define LDNS_OPTIONLIST_INIT 8 @@ -41,6 +42,44 @@ ldns_edns_get_data(const ldns_edns_option *edns) return edns->_data; } +ldns_status +ldns_edns_ede_get_code(const ldns_edns_option *edns, uint16_t *ede_code) +{ + assert(edns != NULL); + assert(ede_code != NULL); + + if (edns->_code != LDNS_EDNS_EDE) return LDNS_STATUS_NOT_EDE; + + if (edns->_size < 2) return LDNS_STATUS_EDE_OPTION_MALFORMED; + + *ede_code = (uint16_t) ntohs(*((uint16_t*) edns->_data)); + + return LDNS_STATUS_OK; +} + +ldns_status +ldns_edns_ede_get_text(const ldns_edns_option* edns, char **ede_text) +{ + assert(edns != NULL); + assert(ede_text != NULL); + + if (edns->_code != LDNS_EDNS_EDE) return LDNS_STATUS_NOT_EDE; + + if (edns->_size < 2) return LDNS_STATUS_EDE_OPTION_MALFORMED; + + *ede_text = NULL; + + if (edns->_size > 2) + { + *ede_text = (char*) malloc((edns->_size - 1) * sizeof(char)); + + memset(*ede_text, 0, edns->_size - 1); + memcpy(*ede_text, &edns->_data[2], edns->_size - 2); + } + + return LDNS_STATUS_OK; +} + ldns_buffer * ldns_edns_get_wireformat_buffer(const ldns_edns_option *edns) { diff --git a/ldns/edns.h b/ldns/edns.h index 594fda22..a33ed055 100644 --- a/ldns/edns.h +++ b/ldns/edns.h @@ -140,6 +140,21 @@ ldns_edns_option_code ldns_edns_get_code(const ldns_edns_option *edns); */ uint8_t *ldns_edns_get_data(const ldns_edns_option *edns); +/** + * extract the RFC 8914 extended error code value. + * \param[in] *edns the EDNS option to extract the extended error code from + * \param[inout] *ede_code pointer to an uint16_t in which to store the extended error code + * \return LDNS_STATUS_OK or an ldns_status message with the error (LDNS_STATUS_NOT_EDE or LDNS_STATUS_EDE_OPTION_MALFORMED) + */ +ldns_status ldns_edns_ede_get_code(const ldns_edns_option *edns, uint16_t *ede_code); + +/** + * extract the optional RFC 8914 extended error code text. + * \param[in] *edns the EDNS option to extract the extended error code from + * \param[inout] **ede_text pointer to a char* in which to store the extended error text; allocated buffer must be freed by the caller, assigns NULL if no text was provided in the EDNS option + * \return LDNS_STATUS_OK or an ldns_status message with the error (LDNS_STATUS_NOT_EDE or LDNS_STATUS_EDE_OPTION_MALFORMED) + */ +ldns_status ldns_edns_ede_get_text(const ldns_edns_option* edns, char **ede_text); /** * serialise the EDNS option into wireformat. diff --git a/ldns/error.h b/ldns/error.h index 2429b770..011df284 100644 --- a/ldns/error.h +++ b/ldns/error.h @@ -141,7 +141,9 @@ enum ldns_enum_status { LDNS_STATUS_RESERVED_SVCPARAM_KEY, LDNS_STATUS_NO_SVCPARAM_VALUE_EXPECTED, LDNS_STATUS_SVCPARAM_KEY_MORE_THAN_ONCE, - LDNS_STATUS_INVALID_SVCPARAM_VALUE + LDNS_STATUS_INVALID_SVCPARAM_VALUE, + LDNS_STATUS_NOT_EDE, + LDNS_STATUS_EDE_OPTION_MALFORMED }; typedef enum ldns_enum_status ldns_status; From 47bd63ad8467589dfad62ced7b648325ca0bc8ef Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 13 Jan 2023 15:59:11 +0000 Subject: [PATCH 2/2] Fixed review comment for missing error strings --- error.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/error.c b/error.c index e3fd1211..50cdc630 100644 --- a/error.c +++ b/error.c @@ -184,6 +184,11 @@ ldns_lookup_table ldns_error_str[] = { { LDNS_STATUS_INVALID_SVCPARAM_VALUE, "Invalid wireformat of a value " "in the ServiceParam rdata field of SVCB or HTTPS RR" }, + { LDNS_STATUS_NOT_EDE, + "The EDNS option is not an extended error code" }, + { LDNS_STATUS_EDE_OPTION_MALFORMED, + "The extended error code option is malformed, expected " + "at least 2 bytes of option data" }, { 0, NULL } };