summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVladimír Čunát <vladimir.cunat@nic.cz>2019-02-25 11:49:32 +0100
committerVladimír Čunát <vladimir.cunat@nic.cz>2019-02-25 13:10:46 +0100
commit7bd892c9b21d9423452e04f105b18288b877124e (patch)
tree2978e5b7f830e7f8dc532cfcfa7e40ea3294025d
parentlib/utils kr_straddr_split(): simplify the API (diff)
downloadknot-resolver-7bd892c9b21d9423452e04f105b18288b877124e.tar.xz
knot-resolver-7bd892c9b21d9423452e04f105b18288b877124e.zip
modules/policy: support '#' for separating port numbers
and deduplicate the parsing logic.
-rw-r--r--NEWS1
-rw-r--r--daemon/lua/kres-gen.lua1
-rwxr-xr-xdaemon/lua/kres-gen.sh1
-rw-r--r--daemon/lua/kres.lua2
-rw-r--r--modules/policy/policy.lua20
5 files changed, 13 insertions, 12 deletions
diff --git a/NEWS b/NEWS
index d5e194d9..1fa781ea 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,7 @@ Bugfixes
- trust_anchors: don't do 5011-style updates on anchors from files
that were loaded as unmanaged trust anchors (!753)
- trust_anchors.add(): include these TAs in .summary() (!753)
+- policy module: support '#' for separating port numbers, for consistency
Improvements
------------
diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua
index eeb8ff7c..5e40a610 100644
--- a/daemon/lua/kres-gen.lua
+++ b/daemon/lua/kres-gen.lua
@@ -308,6 +308,7 @@ 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);
+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);
void kr_qflags_clear(struct kr_qflags *, struct kr_qflags);
diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh
index 538fe239..ea0f7af6 100755
--- a/daemon/lua/kres-gen.sh
+++ b/daemon/lua/kres-gen.sh
@@ -162,6 +162,7 @@ EOF
kr_bitcmp
kr_family_len
kr_straddr_socket
+ kr_straddr_split
kr_ranked_rrarray_add
kr_qflags_set
kr_qflags_clear
diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua
index 8cb00461..6e48026c 100644
--- a/daemon/lua/kres.lua
+++ b/daemon/lua/kres.lua
@@ -239,7 +239,7 @@ local timeval_t = ffi.typeof('struct timeval')
-- Metatype for sockaddr
local addr_buf = ffi.new('char[16]')
-local str_addr_buf = ffi.new('char[46 + 1 + 6 + 1]') -- IPv6 + #port + \0
+local str_addr_buf = ffi.new('char[46 + 1 + 6 + 1]') -- INET6_ADDRSTRLEN + #port + \0
local str_addr_buf_len = ffi.sizeof(str_addr_buf)
local sockaddr_t = ffi.typeof('struct sockaddr')
ffi.metatype( sockaddr_t, {
diff --git a/modules/policy/policy.lua b/modules/policy/policy.lua
index 3ec51a40..fb892986 100644
--- a/modules/policy/policy.lua
+++ b/modules/policy/policy.lua
@@ -33,18 +33,16 @@ if has_socket then
end
end
+-- Split address and port from a combined string.
local function addr_split_port(target, default_port)
- assert(default_port)
- assert(type(default_port) == 'number')
- local addr, port = target:match '([^@]*)@?(.*)'
- local nport
- if port ~= "" then
- nport = tonumber(port)
- if not nport or nport < 1 or nport > 65535 then
- error('port "'.. port ..'" is not valid')
- end
- end
- return addr, nport or default_port
+ assert(default_port and type(default_port) == 'number')
+ local port = ffi.new('uint16_t[1]', default_port)
+ local addr = ffi.new('char[47]') -- INET6_ADDRSTRLEN + 1
+ local ret = ffi.C.kr_straddr_split(target, addr, port)
+ if ret ~= 0 then
+ error('failed to parse address ' .. target)
+ end
+ return addr, tonumber(port[0])
end
-- String address@port -> sockaddr.