]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/lua: add support for resizing packets
authorMarek Vavruša <mvavrusa@cloudflare.com>
Mon, 18 Jun 2018 19:09:51 +0000 (12:09 -0700)
committerMarek Vavruša <mvavrusa@cloudflare.com>
Fri, 7 Sep 2018 17:45:21 +0000 (10:45 -0700)
daemon/lua/kres-gen.lua
daemon/lua/kres-gen.sh
daemon/lua/kres.lua
lib/utils.c
lib/utils.h
tests/config/basic.test.lua

index 6698839b64f1673e1cfa7c7596d2fcbea0a3d6ff..4a8c99dc2c4a3197b1b034b1d8cd0d898c36baf3 100644 (file)
@@ -339,6 +339,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 *);
index 3398622b786cc59dde80ea345ccf07014bcf853f..0231346b3671e23ea097846b1e56122e611c503c 100755 (executable)
@@ -185,6 +185,7 @@ EOF
        lru_free_items_impl
        lru_create_impl
        lru_get_impl
+       mm_realloc
 # Trust anchors
        kr_ta_get
        kr_ta_add
index 7c5c9873fc13e22d52c04b26469d2e1106379705..7a6e71226d2941ae4d98abc34cf82669eebf6ade 100644 (file)
@@ -698,6 +698,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
index fbd28faeec53c11c451182f974e3f4bcd42aee2c..847847620c88983b4a2e150b293f5b7c9017259d 100644 (file)
@@ -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)
 {
index 6fe408c405c42bfcdce3bbf5d5ac6c06201f0129..2d9fd54c55c2bdc9bdee8240d188e235e95748a9 100644 (file)
@@ -111,24 +111,10 @@ static inline void mm_free(knot_mm_t *mm, void *what)
        }
        else free(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);
index 7667ebab1f3891580d74c47ab5288ee9b9bfa24a..520f63e0d48e8c2c98052d1114c832a32dc7b0bb 100644 (file)
@@ -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