Commit 39d1e63b authored by Vladimír Čunát's avatar Vladimír Čunát Committed by Petr Špaček

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.
parent d1a229ae
......@@ -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. */
......
......@@ -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);
......
......@@ -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;
}
}
......
......@@ -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
......
......@@ -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
......
......@@ -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,
......
......@@ -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
......
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