Commit 625ed8d5 authored by Daniel Salzman's avatar Daniel Salzman

rrset-wire: replace knot_dname_parse with simplified implementation

parent e3690d4e
...@@ -124,42 +124,6 @@ int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp, ...@@ -124,42 +124,6 @@ int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
return wire_len; return wire_len;
} }
_public_
knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
knot_mm_t *mm)
{
if (pkt == NULL || pos == NULL) {
return NULL;
}
const uint8_t *name = pkt + *pos;
const uint8_t *endp = pkt + maxpos;
int parsed = knot_dname_wire_check(name, endp, pkt);
if (parsed < 0) {
return NULL;
}
/* Calculate decompressed length. */
size_t decompressed_len = knot_dname_realsize(name, pkt);
if (decompressed_len == 0) {
return NULL;
}
/* Allocate space for the name. */
knot_dname_t *res = mm_alloc(mm, decompressed_len);
if (res != NULL) {
/* Unpack name (expand compression pointers). */
if (knot_dname_unpack(res, name, decompressed_len, pkt) > 0) {
*pos += parsed;
} else {
mm_free(mm, res);
res = NULL;
}
}
return res;
}
_public_ _public_
size_t knot_dname_store(knot_dname_storage_t dst, const knot_dname_t *name) size_t knot_dname_store(knot_dname_storage_t dst, const knot_dname_t *name)
{ {
......
...@@ -54,20 +54,6 @@ _pure_ _mustcheck_ ...@@ -54,20 +54,6 @@ _pure_ _mustcheck_
int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp, int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
const uint8_t *pkt); const uint8_t *pkt);
/*!
* \brief Parse dname from wire.
*
* \param pkt Message in wire format.
* \param pos Position of the domain name on wire.
* \param maxpos Domain name length.
* \param mm Memory context.
*
* \return parsed domain name or NULL.
*/
_mustcheck_
knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
knot_mm_t *mm);
/*! /*!
* \brief Duplicates the given domain name to a local storage. * \brief Duplicates the given domain name to a local storage.
* *
......
...@@ -24,11 +24,10 @@ ...@@ -24,11 +24,10 @@
#include "libknot/rrtype/naptr.h" #include "libknot/rrtype/naptr.h"
#include "libknot/rrtype/rrsig.h" #include "libknot/rrtype/rrsig.h"
#include "contrib/macros.h" #include "contrib/macros.h"
#include "contrib/mempattern.h"
#include "contrib/tolower.h" #include "contrib/tolower.h"
#include "contrib/wire_ctx.h" #include "contrib/wire_ctx.h"
#define RR_HEADER_SIZE 10
/*! /*!
* \brief Get maximal size of a domain name in a wire with given capacity. * \brief Get maximal size of a domain name in a wire with given capacity.
*/ */
...@@ -572,44 +571,52 @@ int knot_rrset_to_wire_rotate(const knot_rrset_t *rrset, uint8_t *wire, ...@@ -572,44 +571,52 @@ int knot_rrset_to_wire_rotate(const knot_rrset_t *rrset, uint8_t *wire,
/*! /*!
* \brief Parse header of one RR from packet wireformat. * \brief Parse header of one RR from packet wireformat.
*/ */
static int parse_header(const uint8_t *pkt_wire, size_t *pos, size_t pkt_size, static int parse_header(const uint8_t *wire, size_t *pos, size_t pkt_size,
knot_mm_t *mm, knot_rrset_t *rrset, uint16_t *rdlen) knot_mm_t *mm, knot_rrset_t *rrset, uint16_t *rdlen)
{ {
assert(pkt_wire); assert(wire);
assert(pos); assert(pos);
assert(rrset); assert(rrset);
assert(rdlen); assert(rdlen);
knot_dname_t *owner = knot_dname_parse(pkt_wire, pos, pkt_size, mm); wire_ctx_t src = wire_ctx_init_const(wire, pkt_size);
if (owner == NULL) { wire_ctx_set_offset(&src, *pos);
int compr_size = knot_dname_wire_check(src.position, wire + pkt_size, wire);
if (compr_size <= 0) {
return KNOT_EMALF; return KNOT_EMALF;
} }
if (pkt_size - *pos < RR_HEADER_SIZE) { uint8_t buff[KNOT_DNAME_MAXLEN];
knot_dname_free(owner, mm); int decompr_size = knot_dname_unpack(buff, src.position, sizeof(buff), wire);
if (decompr_size <= 0) {
return KNOT_EMALF; return KNOT_EMALF;
} }
wire_ctx_t wire = wire_ctx_init_const(pkt_wire, pkt_size); knot_dname_t *owner = mm_alloc(mm, decompr_size);
wire_ctx_set_offset(&wire, *pos); if (owner == NULL) {
return KNOT_ENOMEM;
uint16_t type = wire_ctx_read_u16(&wire); }
uint16_t rclass = wire_ctx_read_u16(&wire); memcpy(owner, buff, decompr_size);
uint32_t ttl = wire_ctx_read_u32(&wire); wire_ctx_skip(&src, compr_size);
*rdlen = wire_ctx_read_u16(&wire);
*pos = wire_ctx_offset(&wire); uint16_t type = wire_ctx_read_u16(&src);
uint16_t rclass = wire_ctx_read_u16(&src);
uint32_t ttl = wire_ctx_read_u32(&src);
*rdlen = wire_ctx_read_u16(&src);
if (wire.error != KNOT_EOK) { if (src.error != KNOT_EOK) {
knot_dname_free(owner, mm); knot_dname_free(owner, mm);
return wire.error; return KNOT_EMALF;
} }
if (wire_ctx_available(&wire) < *rdlen) { if (wire_ctx_available(&src) < *rdlen) {
knot_dname_free(owner, mm); knot_dname_free(owner, mm);
return KNOT_EMALF; return KNOT_EMALF;
} }
*pos = wire_ctx_offset(&src);
knot_rrset_init(rrset, owner, type, rclass, ttl); knot_rrset_init(rrset, owner, type, rclass, ttl);
return KNOT_EOK; return KNOT_EOK;
......
...@@ -158,8 +158,6 @@ int main(int argc, char *argv[]) ...@@ -158,8 +158,6 @@ int main(int argc, char *argv[])
knot_dname_t *d = NULL, *d2 = NULL; knot_dname_t *d = NULL, *d2 = NULL;
const char *w = NULL, *t = NULL; const char *w = NULL, *t = NULL;
unsigned len = 0;
size_t pos = 0;
char *s = NULL; char *s = NULL;
/* DNAME WIRE CHECKS */ /* DNAME WIRE CHECKS */
...@@ -543,25 +541,6 @@ int main(int argc, char *argv[]) ...@@ -543,25 +541,6 @@ int main(int argc, char *argv[])
knot_dname_free(d, NULL); knot_dname_free(d, NULL);
knot_dname_free(d2, NULL); knot_dname_free(d2, NULL);
/* DNAME PARSE CHECKS */
/* parse from wire (valid) */
t = "\x04""abcd""\x03""efg";
len = 10;
pos = 0;
d = knot_dname_parse((const uint8_t *)t, &pos, len, NULL);
ok(d != NULL, "dname_parse: valid name");
ok(pos == len, "dname_parse: valid name (parsed length)");
knot_dname_free(d, NULL);
/* parse from wire (invalid) */
t = "\x08""dddd";
len = 5;
pos = 0;
d = knot_dname_parse((const uint8_t *)t, &pos, len, NULL);
ok(d == NULL, "dname_parse: bad name");
ok(pos == 0, "dname_parse: bad name (parsed length)");
/* DNAME EQUALITY CHECKS */ /* DNAME EQUALITY CHECKS */
t = "ab.cd.ef"; t = "ab.cd.ef";
......
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