Commit 85d90d14 authored by Daniel Salzman's avatar Daniel Salzman

libknot: replace knot_edns_default_padding_size with knot_pkt_default_padding_size

parent 494e9e29
......@@ -51,15 +51,17 @@ enum {
KNOT_PF_NOCANON = 1 << 5, /*!< Don't canonicalize rrsets during parsing. */
};
typedef struct knot_pkt knot_pkt_t;
/*!
* \brief Packet section.
* Points to RRSet and RRSet info arrays in the packet.
* This structure is required for random access to packet sections.
*/
typedef struct {
struct knot_pkt *pkt; /*!< Owner. */
uint16_t pos; /*!< Position in the rr/rrinfo fields in packet. */
uint16_t count; /*!< Number of RRSets in this section. */
knot_pkt_t *pkt; /*!< Owner. */
uint16_t pos; /*!< Position in the rr/rrinfo fields in packet. */
uint16_t count; /*!< Number of RRSets in this section. */
} knot_pktsection_t;
/*!
......@@ -380,4 +382,24 @@ static inline uint8_t *knot_pkt_edns_option(const knot_pkt_t *pkt, uint16_t code
}
}
/*!
* \brief Computes a reasonable Padding data length for a given packet and opt RR.
*
* \param pkt DNS Packet prepared and otherwise ready to go, no OPT yet added.
* \param opt_rr OPT RR, not yet including padding.
*
* \return Required padding length or -1 if padding not required.
*/
static inline int knot_pkt_default_padding_size(const knot_pkt_t *pkt,
const knot_rrset_t *opt_rr)
{
if (knot_wire_get_qr(pkt->wire)) {
return knot_edns_alignment_size(pkt->size, knot_rrset_size(opt_rr),
KNOT_EDNS_ALIGNMENT_RESPONSE_DEFAULT);
} else {
return knot_edns_alignment_size(pkt->size, knot_rrset_size(opt_rr),
KNOT_EDNS_ALIGNMENT_QUERY_DEFALT);
}
}
/*! @} */
......@@ -30,7 +30,7 @@
#include "contrib/wire_ctx.h"
/*! \brief Some implementation-related constants. */
enum knot_edns_private_consts {
enum {
/*! \brief Byte offset of the extended RCODE field in TTL. */
EDNS_OFFSET_RCODE = 0,
/*! \brief Byte offset of the version field in TTL. */
......@@ -44,9 +44,6 @@ enum knot_edns_private_consts {
EDNS_OFFSET_CLIENT_SUBNET_DST_MASK = 3,
/*! \brief Byte offset of the address field in option data. */
EDNS_OFFSET_CLIENT_SUBNET_ADDR = 4,
EDNS_DEFAULT_QUERY_ALIGNMENT_SIZE = 128,
EDNS_DEFAULT_RESPONSE_ALIGNMENT_SIZE = 468,
};
_public_
......@@ -280,29 +277,33 @@ int knot_edns_get_options(knot_rrset_t *opt_rr, knot_edns_options_t **out,
}
_public_
int knot_edns_default_padding_size(const knot_pkt_t *pkt,
const knot_rrset_t *opt_rr)
int knot_edns_alignment_size(size_t current_pkt_size,
size_t current_opt_size,
size_t block_size)
{
if (knot_wire_get_qr(pkt->wire)) {
return knot_edns_alignment_size(pkt->size, knot_rrset_size(opt_rr),
EDNS_DEFAULT_RESPONSE_ALIGNMENT_SIZE);
} else {
return knot_edns_alignment_size(pkt->size, knot_rrset_size(opt_rr),
EDNS_DEFAULT_QUERY_ALIGNMENT_SIZE);
if (current_opt_size == 0 || block_size == 0) {
return -1;
}
size_t current_size = current_pkt_size + current_opt_size;
if (current_size % block_size == 0) {
return -1;
}
size_t modulo = (current_size + KNOT_EDNS_OPTION_HDRLEN) % block_size;
return (modulo == 0) ? 0 : block_size - modulo;
}
/*!
* \brief EDNS Client Subnet family data.
*/
struct ecs_family {
typedef struct {
int platform; //!< Platform family identifier.
uint16_t iana; //!< IANA family identifier.
size_t offset; //!< Socket address offset.
size_t size; //!< Socket address size.
};
typedef struct ecs_family ecs_family_t;
} ecs_family_t;
#define ECS_INIT(platform, iana, type, member) \
{ platform, iana, offsetof(type, member), sizeof(((type *)0)->member) }
......
......@@ -31,11 +31,8 @@
#include "libknot/rrset.h"
#include "libknot/wire.h"
/* Forward decls */
typedef struct knot_pkt knot_pkt_t;
/*! \brief Constants related to EDNS. */
enum knot_edns_const {
enum {
/*! \brief Supported EDNS version. */
KNOT_EDNS_VERSION = 0,
......@@ -56,11 +53,14 @@ enum knot_edns_const {
/*! \brief EDNS OPTION header size. */
KNOT_EDNS_OPTION_HDRLEN = 4,
/*! \brief Maximal edns client subnet data size (IPv6). */
KNOT_EDNS_MAX_OPTION_CLIENT_SUBNET = 20,
/*! \brief Maximal size of EDNS client subnet address in bytes (IPv6). */
KNOT_EDNS_CLIENT_SUBNET_ADDRESS_MAXLEN = 16,
/*! \brief Default EDNS alignment size for a query. */
KNOT_EDNS_ALIGNMENT_QUERY_DEFALT = 128,
/*! \brief Default EDNS alignment size for a response. */
KNOT_EDNS_ALIGNMENT_RESPONSE_DEFAULT = 468,
/*! \brief EDNS client cookie size. */
KNOT_EDNS_COOKIE_CLNT_SIZE = 8,
/*! \brief EDNS minimum server cookie size. */
......@@ -373,17 +373,6 @@ static inline uint8_t *knot_edns_opt_get_data(uint8_t *opt)
return opt + KNOT_EDNS_OPTION_HDRLEN;
}
/*!
* \brief Computes a reasonable Padding data length for a given packet and opt RR.
*
* \param pkt DNS Packet prepared and otherwise ready to go, no OPT yet added.
* \param opt_rr OPT RR, not yet including padding.
*
* \return Required padding length or -1 if padding not required.
*/
int knot_edns_default_padding_size(const knot_pkt_t *pkt,
const knot_rrset_t *opt_rr);
/*!
* \brief Computes additional Padding data length for required packet alignment.
*
......@@ -393,22 +382,9 @@ int knot_edns_default_padding_size(const knot_pkt_t *pkt,
*
* \return Required padding length or -1 if padding not required.
*/
static inline int knot_edns_alignment_size(size_t current_pkt_size,
size_t current_opt_size,
size_t block_size)
{
assert(current_opt_size > 0);
assert(block_size > 0);
size_t current_size = current_pkt_size + current_opt_size;
if (current_size % block_size == 0) {
return -1;
}
size_t modulo = (current_size + KNOT_EDNS_OPTION_HDRLEN) % block_size;
return (modulo == 0) ? 0 : block_size - modulo;
}
int knot_edns_alignment_size(size_t current_pkt_size,
size_t current_opt_size,
size_t block_size);
/*!
* \brief EDNS Client Subnet content.
......
......@@ -310,7 +310,7 @@ static int add_query_edns(knot_pkt_t *packet, const query_t *query, uint16_t max
knot_rrset_size(&opt_rr),
query->alignment);
} else if (query->padding == -2 || (query->padding == -1 && query->tls.enable)) {
padding = knot_edns_default_padding_size(packet, &opt_rr);
padding = knot_pkt_default_padding_size(packet, &opt_rr);
}
if (padding > -1) {
uint8_t zeros[padding];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment