From: Vladimír Čunát Date: Mon, 8 Apr 2019 13:52:18 +0000 (+0200) Subject: lib/utils kr_straddr_socket(): support mempools X-Git-Tag: v4.0.0~10^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39d1e63b332fc5e82bb6cffe7bace0da27f7b422;p=thirdparty%2Fknot-resolver.git lib/utils kr_straddr_socket(): support mempools "Unfortunately", for FFI-bound C functions there it doesn't hold that missing parameters would be converted to nil/NULL. Still, this function seems unlikely to have been used outside the repo. --- diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c index 952990bc2..0b22ffcb6 100644 --- a/daemon/bindings/net.c +++ b/daemon/bindings/net.c @@ -598,7 +598,7 @@ static int net_tls_client(lua_State *L) uint16_t port = 853; const struct sockaddr *addr = NULL; if (kr_straddr_split(addr_str, buf, &port) == kr_ok()) - addr = kr_straddr_socket(buf, port); + addr = kr_straddr_socket(buf, port, NULL); /* Add newcfg into the C map, saving the original into oldcfg. */ if (!addr) lua_error_p(L, "address '%s' could not be converted", addr_str); @@ -642,7 +642,7 @@ int net_tls_client_clear(lua_State *L) uint16_t port = 853; const struct sockaddr *addr = NULL; if (kr_straddr_split(addr_str, buf, &port) == kr_ok()) - addr = kr_straddr_socket(buf, port); + addr = kr_straddr_socket(buf, port, NULL); if (!addr) lua_error_p(L, "invalid IP address"); /* Do the actual removal. */ diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index 4989c02c7..84db39ca0 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -320,7 +320,7 @@ int kr_straddr_family(const char *); int kr_straddr_subnet(void *, const char *); int kr_bitcmp(const char *, const char *, int); int kr_family_len(int); -struct sockaddr *kr_straddr_socket(const char *, int); +struct sockaddr *kr_straddr_socket(const char *, int, knot_mm_t *); int kr_straddr_split(const char *, char * restrict, uint16_t *); int kr_ranked_rrarray_add(ranked_rr_array_t *, const knot_rrset_t *, uint8_t, _Bool, uint32_t, knot_mm_t *); void kr_qflags_set(struct kr_qflags *, struct kr_qflags); diff --git a/lib/utils.c b/lib/utils.c index da74f9864..8f4aee6dd 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -455,24 +455,24 @@ int kr_family_len(int family) } } -struct sockaddr * kr_straddr_socket(const char *addr, int port) +struct sockaddr * kr_straddr_socket(const char *addr, int port, knot_mm_t *pool) { switch (kr_straddr_family(addr)) { case AF_INET: { - struct sockaddr_in *res = malloc(sizeof(*res)); + struct sockaddr_in *res = mm_alloc(pool, sizeof(*res)); if (uv_ip4_addr(addr, port, res) >= 0) { return (struct sockaddr *)res; } else { - free(res); + mm_free(pool, res); return NULL; } } case AF_INET6: { - struct sockaddr_in6 *res = malloc(sizeof(*res)); + struct sockaddr_in6 *res = mm_alloc(pool, sizeof(*res)); if (uv_ip6_addr(addr, port, res) >= 0) { return (struct sockaddr *)res; } else { - free(res); + mm_free(pool, res); return NULL; } } diff --git a/lib/utils.h b/lib/utils.h index 571a8c7c2..4d8530aa7 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -345,9 +345,11 @@ int kr_straddr_family(const char *addr); /** Return address length in given family (struct in*_addr). */ KR_EXPORT KR_CONST int kr_family_len(int family); + /** Create a sockaddr* from string+port representation (also accepts IPv6 link-local). */ KR_EXPORT -struct sockaddr * kr_straddr_socket(const char *addr, int port); +struct sockaddr * kr_straddr_socket(const char *addr, int port, knot_mm_t *pool); + /** Parse address and return subnet length (bits). * @warning 'dst' must be at least `sizeof(struct in6_addr)` long. */ KR_EXPORT diff --git a/modules/http/http_doh.lua b/modules/http/http_doh.lua index ea0d91cb7..95bf02e3d 100644 --- a/modules/http/http_doh.lua +++ b/modules/http/http_doh.lua @@ -12,7 +12,7 @@ local function convert_sockaddr(family, ipaddr, port) if not (family and ipaddr and port) then panic('failed to obtain peer IP address') end - return ffi.gc(ffi.C.kr_straddr_socket(ipaddr, port), ffi.C.free) + return ffi.gc(ffi.C.kr_straddr_socket(ipaddr, port, nil), ffi.C.free) end -- Trace execution of DNS queries diff --git a/modules/http/http_doh.test.lua b/modules/http/http_doh.test.lua index edf488452..37c6f6411 100644 --- a/modules/http/http_doh.test.lua +++ b/modules/http/http_doh.test.lua @@ -194,7 +194,7 @@ else local function test_dstaddr() local triggered = false - local exp_dstaddr = ffi.gc(ffi.C.kr_straddr_socket(host, port), ffi.C.free) + local exp_dstaddr = ffi.gc(ffi.C.kr_straddr_socket(host, port, nil), ffi.C.free) local function check_dstaddr(state, req) triggered = true same(ffi.C.kr_sockaddr_cmp(req.qsource.dst_addr, exp_dstaddr), 0, diff --git a/modules/policy/policy.lua b/modules/policy/policy.lua index 17352db41..b42b69d91 100644 --- a/modules/policy/policy.lua +++ b/modules/policy/policy.lua @@ -48,7 +48,7 @@ end -- String address@port -> sockaddr. local function addr2sock(target, default_port) local addr, port = addr_split_port(target, default_port) - local sock = ffi.gc(ffi.C.kr_straddr_socket(addr, port), ffi.C.free); + local sock = ffi.gc(ffi.C.kr_straddr_socket(addr, port, nil), ffi.C.free); if sock == nil then error("target '"..target..'" is not a valid IP address') end