Commit 7bcb2983 authored by Vladimír Čunát's avatar Vladimír Čunát Committed by Daniel Salzman

knot_rrset_to_wire: add parameter to rotate the RRset

parent c9794112
......@@ -490,8 +490,8 @@ int knot_pkt_put_question(knot_pkt_t *pkt, const knot_dname_t *qname, uint16_t q
}
_public_
int knot_pkt_put(knot_pkt_t *pkt, uint16_t compr_hint, const knot_rrset_t *rr,
uint16_t flags)
int knot_pkt_put_rotate(knot_pkt_t *pkt, uint16_t compr_hint, const knot_rrset_t *rr,
uint16_t rotate, uint16_t flags)
{
if (pkt == NULL || rr == NULL) {
return KNOT_EINVAL;
......@@ -534,7 +534,7 @@ int knot_pkt_put(knot_pkt_t *pkt, uint16_t compr_hint, const knot_rrset_t *rr,
size_t maxlen = pkt_remaining(pkt);
/* Write RRSet to wireformat. */
ret = knot_rrset_to_wire(rr, pos, maxlen, compr);
ret = knot_rrset_to_wire_rotate(rr, pos, maxlen, rotate, compr);
if (ret < 0) {
/* Truncate packet if required. */
if (ret == KNOT_ESPACE && !(flags & KNOT_PF_NOTRUNC)) {
......
......@@ -239,15 +239,24 @@ int knot_pkt_put_question(knot_pkt_t *pkt, const knot_dname_t *qname,
* \note Available flags: PF_FREE, KNOT_PF_CHECKDUP, KNOT_PF_NOTRUNC
*
* \param pkt
* \param compr_hint Compression hint, see enum knot_compr_hint or absolute
* position.
* \param rr Given RRSet.
* \param flags RRSet flags (set PF_FREE if you want RRSet to be freed with the
* packet).
* \param compr_hint Compression hint, see enum knot_compr_hint or absolute
* position.
* \param rr Given RRSet.
* \param rotate Rotate the RRSet order by this count.
* \param flags RRSet flags (set PF_FREE if you want RRSet to be freed
* with the packet).
*
* \return KNOT_EOK, KNOT_ESPACE, various errors
*/
int knot_pkt_put(knot_pkt_t *pkt, uint16_t compr_hint, const knot_rrset_t *rr,
uint16_t flags);
int knot_pkt_put_rotate(knot_pkt_t *pkt, uint16_t compr_hint, const knot_rrset_t *rr,
uint16_t rotate, uint16_t flags);
/*! \brief Same as knot_pkt_put_rotate but without rrset rotation. */
static inline int knot_pkt_put(knot_pkt_t *pkt, uint16_t compr_hint,
const knot_rrset_t *rr, uint16_t flags)
{
return knot_pkt_put_rotate(pkt, compr_hint, rr, 0, flags);
}
/*! \brief Get description of the given packet section. */
static inline const knot_pktsection_t *knot_pkt_section(const knot_pkt_t *pkt,
......
......@@ -646,22 +646,28 @@ static int write_rr(const knot_rrset_t *rrset, uint16_t rrset_index,
return write_rdata(rrset, rrset_index, dst, dst_avail, compr);
}
/*!
* \brief Write RR Set content to a wire.
*/
_public_
int knot_rrset_to_wire(const knot_rrset_t *rrset, uint8_t *wire, uint16_t max_size,
knot_compr_t *compr)
int knot_rrset_to_wire_rotate(const knot_rrset_t *rrset, uint8_t *wire,
uint16_t max_size, uint16_t rotate,
knot_compr_t *compr)
{
if (!rrset || !wire) {
if (rrset == NULL || wire == NULL) {
return KNOT_EINVAL;
}
if (rrset->rrs.rr_count == 0) {
return 0;
}
if (rotate != 0) {
rotate %= rrset->rrs.rr_count;
}
uint8_t *write = wire;
size_t capacity = max_size;
for (uint16_t i = 0; i < rrset->rrs.rr_count; i++) {
int ret = write_rr(rrset, i, &write, &capacity, compr);
uint16_t count = rrset->rrs.rr_count;
for (uint16_t i = rotate; i < count + rotate; i++) {
uint16_t pos = (i < count) ? i : (i - count);
int ret = write_rr(rrset, pos, &write, &capacity, compr);
if (ret != KNOT_EOK) {
return ret;
}
......
......@@ -33,12 +33,21 @@
* \param rrset RRSet to be converted.
* \param wire Output wire buffer.
* \param max_size Capacity of wire buffer.
* \param rotate Rotate the RR order by this count.
* \param compr Compression context.
*
* \return Output size, negative number on error (KNOT_E*).
*/
int knot_rrset_to_wire(const knot_rrset_t *rrset, uint8_t *wire, uint16_t max_size,
struct knot_compr *compr);
int knot_rrset_to_wire_rotate(const knot_rrset_t *rrset, uint8_t *wire,
uint16_t max_size, uint16_t rotate,
knot_compr_t *compr);
/*! \brief Same as knot_rrset_to_wire but without rrset rotation. */
static inline int knot_rrset_to_wire(const knot_rrset_t *rrset, uint8_t *wire,
uint16_t max_size, knot_compr_t *compr)
{
return knot_rrset_to_wire_rotate(rrset, wire, max_size, 0, compr);
}
/*!
* \brief Creates one RR from wire, stores it into \a rrset.
......
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