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