From: Marek VavruĊĦa Date: Mon, 18 Jun 2018 19:09:51 +0000 (-0700) Subject: daemon/lua: add support for resizing packets X-Git-Tag: v3.2.0~26^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48e1143a29f19b405f8ecf63fcaf9cf85c89b731;p=thirdparty%2Fknot-resolver.git daemon/lua: add support for resizing packets --- diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index 7f687c6fc..6e2011b9f 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -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 *); diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index 73e16b46c..9b06a1562 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -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 diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 4eeb0da58..4a63c7048 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -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 diff --git a/lib/utils.c b/lib/utils.c index 9a7a69e6f..b818aefa9 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -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) { diff --git a/lib/utils.h b/lib/utils.h index b61b4f76c..7d517898d 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -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); diff --git a/tests/config/basic.test.lua b/tests/config/basic.test.lua index 7667ebab1..520f63e0d 100644 --- a/tests/config/basic.test.lua +++ b/tests/config/basic.test.lua @@ -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