Skip to content

Commit

Permalink
Add functions to extract RFC 8914 extended errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jan 6, 2023
1 parent 1ce94b1 commit ec7ce66
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
39 changes: 39 additions & 0 deletions edns.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* See the file LICENSE for the license
*/

#include <ldns/config.h>
#include <ldns/ldns.h>

#define LDNS_OPTIONLIST_INIT 8
Expand Down Expand Up @@ -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)
{
Expand Down
15 changes: 15 additions & 0 deletions ldns/edns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion ldns/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit ec7ce66

Please sign in to comment.