diff options
author | Vladimír Čunát <vladimir.cunat@nic.cz> | 2020-10-14 18:28:26 +0200 |
---|---|---|
committer | Vladimír Čunát <vladimir.cunat@nic.cz> | 2020-11-10 17:16:46 +0100 |
commit | 382c60a8bebb97e707945764b6961db658518d22 (patch) | |
tree | 9b17134442f8c68633d87a7bf6e44beb062e0f22 /daemon/bindings | |
parent | XDP: add backend parts (diff) | |
download | knot-resolver-382c60a8bebb97e707945764b6961db658518d22.tar.xz knot-resolver-382c60a8bebb97e707945764b6961db658518d22.zip |
daemon/bindings net_listen_addrs(): pack parameters
They're starting to be too many. Also improve comments.
Diffstat (limited to 'daemon/bindings')
-rw-r--r-- | daemon/bindings/net.c | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c index 93fbd57b..511f5587 100644 --- a/daemon/bindings/net.c +++ b/daemon/bindings/net.c @@ -95,9 +95,9 @@ static int net_list(lua_State *L) } /** Listen on an address list represented by the top of lua stack. - * \note kind ownership is not transferred + * \note flags.kind ownership is not transferred, and flags.sock_type doesn't make sense * \return success */ -static bool net_listen_addrs(lua_State *L, int port, bool tls, bool http, const char *kind, bool freebind) +static bool net_listen_addrs(lua_State *L, int port, endpoint_flags_t flags) { /* Case: table with 'addr' field; only follow that field directly. */ lua_getfield(L, -1, "addr"); @@ -112,17 +112,16 @@ static bool net_listen_addrs(lua_State *L, int port, bool tls, bool http, const if (str != NULL) { struct network *net = &the_worker->engine->net; int ret = 0; - endpoint_flags_t flags = { .tls = tls, .http = http, .freebind = freebind }; - if (!kind && !flags.tls) { /* normal UDP */ + if (!flags.kind && !flags.tls) { /* normal UDP */ flags.sock_type = SOCK_DGRAM; ret = network_listen(net, str, port, flags); } - if (!kind && ret == 0) { /* common for TCP, DoT and DoH (v2) */ + if (!flags.kind && ret == 0) { /* common for TCP, DoT and DoH (v2) */ flags.sock_type = SOCK_STREAM; ret = network_listen(net, str, port, flags); } - if (kind) { - flags.kind = strdup(kind); + if (flags.kind) { + flags.kind = strdup(flags.kind); flags.sock_type = SOCK_STREAM; /* TODO: allow to override this? */ ret = network_listen(net, str, port, flags); } @@ -144,7 +143,7 @@ static bool net_listen_addrs(lua_State *L, int port, bool tls, bool http, const lua_error_p(L, "bad type for address"); lua_pushnil(L); while (lua_next(L, -2)) { - if (!net_listen_addrs(L, port, tls, http, kind, freebind)) + if (!net_listen_addrs(L, port, flags)) return false; lua_pop(L, 1); } @@ -183,31 +182,32 @@ static int net_listen(lua_State *L) } } - bool tls = (port == KR_DNS_TLS_PORT); - bool http = false; - if (port == KR_DNS_DOH_PORT) { - http = tls = true; + endpoint_flags_t flags = { 0 }; + if (port == KR_DNS_TLS_PORT) { + flags.tls = true; + } else if (port == KR_DNS_DOH_PORT) { + flags.http = flags.tls = true; } - bool freebind = false; - const char *kind = NULL; if (n > 2 && !lua_isnil(L, 3)) { if (!lua_istable(L, 3)) lua_error_p(L, "wrong type of third parameter (table expected)"); - tls = table_get_flag(L, 3, "tls", tls); - freebind = table_get_flag(L, 3, "freebind", tls); + flags.tls = table_get_flag(L, 3, "tls", flags.tls); + flags.freebind = table_get_flag(L, 3, "freebind", flags.tls); lua_getfield(L, 3, "kind"); const char *k = lua_tostring(L, -1); if (k && strcasecmp(k, "dns") == 0) { - tls = http = false; + flags.tls = flags.http = false; + } else if (k && strcasecmp(k, "xdp") == 0) { + flags.tls = flags.http = false; } else if (k && strcasecmp(k, "tls") == 0) { - tls = true; - http = false; + flags.tls = true; + flags.http = false; } else if (k && strcasecmp(k, "doh2") == 0) { - tls = http = true; + flags.tls = flags.http = true; } else if (k) { - kind = k; + flags.kind = k; if (strcasecmp(k, "doh") == 0) { kr_log_deprecate( "kind=\"doh\" is an obsolete DoH implementation, use kind=\"doh2\" instead\n"); @@ -217,16 +217,16 @@ static int net_listen(lua_State *L) /* Memory management of `kind` string is difficult due to longjmp etc. * Pop will unreference the lua value, so we store it on C stack instead (!) */ - const int kind_alen = kind ? strlen(kind) + 1 : 1 /* 0 length isn't C standard */; + const int kind_alen = flags.kind ? strlen(flags.kind) + 1 : 1 /* 0 length isn't C standard */; char kind_buf[kind_alen]; - if (kind) { - memcpy(kind_buf, kind, kind_alen); - kind = kind_buf; + if (flags.kind) { + memcpy(kind_buf, flags.kind, kind_alen); + flags.kind = kind_buf; } /* Now focus on the first argument. */ lua_settop(L, 1); - if (!net_listen_addrs(L, port, tls, http, kind, freebind)) + if (!net_listen_addrs(L, port, flags)) lua_error_p(L, "net.listen() failed to bind"); lua_pushboolean(L, true); return 1; |