Skip to content
Snippets Groups Projects
Commit f121fe55 authored by Vladimír Čunát's avatar Vladimír Čunát
Browse files

Merge !704: daemon/lua: add support for resizing packets

parents 3de76924 48e1143a
No related branches found
No related tags found
1 merge request!704daemon/lua: add support for resizing packets
......@@ -318,6 +318,7 @@ uint64_t kr_now();
void lru_free_items_impl(struct lru *);
struct lru *lru_create_impl(unsigned int, knot_mm_t *, knot_mm_t *);
void *lru_get_impl(struct lru *, const char *, unsigned int, unsigned int, _Bool, _Bool *);
void *mm_realloc(knot_mm_t *, void *, size_t, size_t);
knot_rrset_t *kr_ta_get(map_t *, const knot_dname_t *);
int kr_ta_add(map_t *, const knot_dname_t *, uint16_t, uint32_t, const uint8_t *, uint16_t);
int kr_ta_del(map_t *, const knot_dname_t *);
......
......@@ -172,6 +172,7 @@ EOF
lru_free_items_impl
lru_create_impl
lru_get_impl
mm_realloc
# Trust anchors
kr_ta_get
kr_ta_add
......
......@@ -696,6 +696,15 @@ ffi.metatype( knot_pkt_t, {
if ret ~= 0 then return nil, knot_error_t(ret) end
return true
end,
-- Resize packet wire to a new size
resize = function (pkt, new_size)
assert(ffi.istype(knot_pkt_t, pkt))
local ptr = C.mm_realloc(pkt.mm, pkt.wire, new_size, pkt.max_size)
if ptr == nil then return end
pkt.wire = ptr
pkt.max_size = new_size
return true
end,
},
})
-- Metatype for query
......
......@@ -53,6 +53,24 @@ static isaac_ctx ISAAC;
static bool isaac_seeded = false;
#define SEED_SIZE 256
void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
{
if (mm) {
void *p = mm->alloc(mm->ctx, size);
if (p == NULL) {
return NULL;
} else {
if (what) {
memcpy(p, what,
prev_size < size ? prev_size : size);
}
mm_free(mm, what);
return p;
}
} else {
return realloc(what, size);
}
}
void *mm_malloc(void *ctx, size_t n)
{
......
......@@ -111,24 +111,10 @@ static inline void mm_free(knot_mm_t *mm, const void *what)
}
else free((void *)what);
}
static inline void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size)
{
if (mm) {
void *p = mm->alloc(mm->ctx, size);
if (p == NULL) {
return NULL;
} else {
if (what) {
memcpy(p, what,
prev_size < size ? prev_size : size);
}
mm_free(mm, what);
return p;
}
} else {
return realloc(what, size);
}
}
/** Realloc implementation using memory context. */
KR_EXPORT
void *mm_realloc(knot_mm_t *mm, void *what, size_t size, size_t prev_size);
/** Trivial malloc() wrapper. */
void *mm_malloc(void *ctx, size_t n);
......
......@@ -99,7 +99,7 @@ local function test_packet_functions()
-- Test manipulating sections
ok(pkt:begin(kres.section.ANSWER), 'switching sections works')
local res, err = pkt:put(nil, 0, 0, 0, '')
isnt(res, 'inserting nil entry doesnt work')
isnt(res, true, 'inserting nil entry doesnt work')
isnt(err.code, 0, 'error code is non-zero')
isnt(tostring(res), '', 'inserting nil returns invalid parameter')
ok(pkt:put(pkt:qname(), 900, pkt:qclass(), kres.type.A, '\1\2\3\4'), 'adding rrsets works')
......@@ -131,13 +131,15 @@ local function test_packet_functions()
same(parsed:tostring(), pkt:tostring(), 'parsed packet is equal to source packet')
-- Test adding RR sets directly
local copy = kres.packet(512)
local copy = kres.packet(23)
copy:question(todname('hello'), kres.class.IN, kres.type.A)
copy:begin(kres.section.ANSWER)
local rr = kres.rrset(pkt:qname(), kres.type.A, kres.class.IN, 66)
rr:add_rdata('\4\3\2\1', 4)
ok(not copy:put_rr(rr), 'adding RR sets checks for available space')
ok(copy:resize(512), 'resizing packet works')
ok(copy:put_rr(rr), 'adding RR sets directly works')
ok(copy:recycle())
ok(copy:recycle(), 'recycling packet works')
-- Test recycling of packets
-- Clear_payload keeps header + question intact
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment