From: TCY16 Date: Fri, 1 Apr 2022 13:56:54 +0000 (+0200) Subject: add function to packet.c to create a list of structured EDNS options X-Git-Tag: 1.8.2-rc.1~3^2~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43db2a66cf7375b27b7575d825f50cb4893ede2a;p=thirdparty%2Fldns.git add function to packet.c to create a list of structured EDNS options --- diff --git a/ldns/packet.h b/ldns/packet.h index 73d04b09..f6ca360c 100644 --- a/ldns/packet.h +++ b/ldns/packet.h @@ -35,8 +35,8 @@ #include #include #include -#include #include +#include #ifdef __cplusplus extern "C" { @@ -258,10 +258,6 @@ struct ldns_struct_pkt uint16_t _edns_z; /** Arbitrary EDNS rdata */ ldns_rdf *_edns_data; - - /** Structured ENDS options */ - ldns_edns_option *_edns_options; // @TODO rewrite into "edns_option_list" struct - /** Question section */ ldns_rr_list *_question; /** Answer section */ @@ -512,7 +508,6 @@ ldns_rr_list *ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_p */ ldns_rr_list *ldns_pkt_rr_list_by_name_and_type(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_rr_type type, ldns_pkt_section sec); - /** * check to see if an rr exist in the packet * \param[in] pkt the packet to examine @@ -724,7 +719,7 @@ uint16_t ldns_pkt_edns_unassigned(const ldns_pkt *packet); void ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value); /** - * returns true if this packet needs an EDNS rr to be sent. + * returns true if this packet needs and EDNS rr to be sent. * At the moment the only reason is an expected packet * size larger than 512 bytes, but for instance dnssec would * be a good reason too. @@ -734,6 +729,14 @@ void ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value); */ bool ldns_pkt_edns(const ldns_pkt *packet); +/** + * Returns a list of structured EDNS options + * + * \param[in] packet the packet which contains the parsed EDNS data + * \return list of ldns_edns_option structs + */ +ldns_edns_option_list* ldns_pkt_edns_option_list(const ldns_pkt *packet); + /** * Set the packet's edns udp size * \param[in] packet the packet @@ -765,18 +768,6 @@ void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z); */ void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data); - -// * -// * Set the packet's edns data -// * \param[in] packet the packet -// * \param[in] data the data -// * \return 0 is there is no next EDNS option in the - -// uint16_t ldns_pkt_get_next_edns_option(ldns_pkt *packet, ); - - - - /** * allocates and initializes a ldns_pkt structure. * \return pointer to the new packet diff --git a/packet.c b/packet.c index 0cee38f1..27e711b7 100644 --- a/packet.c +++ b/packet.c @@ -738,7 +738,8 @@ ldns_pkt_safe_push_rr_list(ldns_pkt *p, ldns_pkt_section s, ldns_rr_list *list) } bool -ldns_pkt_edns(const ldns_pkt *pkt) { +ldns_pkt_edns(const ldns_pkt *pkt) +{ return (ldns_pkt_edns_udp_size(pkt) > 0 || ldns_pkt_edns_extended_rcode(pkt) > 0 || ldns_pkt_edns_data(pkt) || @@ -747,6 +748,54 @@ ldns_pkt_edns(const ldns_pkt *pkt) { ); } +ldns_edns_option_list* +ldns_pkt_edns_option_list(const ldns_pkt *packet) +{ + size_t pos = 0; + ldns_edns_option_list* edns_list; + size_t max = ldns_rdf_size(ldns_pkt_edns_data(packet)); // @TODO is this ugly? + const uint8_t* wire = ldns_rdf_data(ldns_pkt_edns_data(packet)); // @TODO is this ugly? + + + // @TODO do checks so we don't read into un-auth memory (max !< 4) + + + edns_list = ldns_edns_option_list_new(); + if (edns_list == NULL) { + return NULL; + } + + while (pos < max) { + ldns_edns_option* edns; + uint8_t *data; + ldns_edns_option_code code = ldns_read_uint16(&wire[pos]); + size_t size = ldns_read_uint16(&wire[pos+2]); + pos += 4; + + data = LDNS_XMALLOC(uint8_t, size); + + // @TODO think about commented-out code + // if (!data) { + // status = LDNS_STATUS_MEM_ERR; + // goto status_error; + // printf("HERE: DATA == NULL\n"); + // } + memcpy(data, &wire[pos], size); + pos += size; + + edns = ldns_edns_new(code, size, data); + + if (edns != NULL) { + ldns_edns_option_list_push(edns_list, edns); + } + + // @TODO what do we do in case of edns == NULL? + + } + + return edns_list; +} + /* Create/destroy/convert functions */ @@ -797,8 +846,6 @@ ldns_pkt_new(void) ldns_pkt_set_edns_z(packet, 0); ldns_pkt_set_edns_data(packet, NULL); packet->_edns_present = false; - - packet->_edns_options = NULL; //@TODO change this to set_function/list function ldns_pkt_set_tsig(packet, NULL);