]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/bindings net_listen_addrs(): pack parameters
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 14 Oct 2020 16:28:26 +0000 (18:28 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 10 Nov 2020 16:16:46 +0000 (17:16 +0100)
They're starting to be too many.  Also improve comments.

daemon/bindings/net.c
daemon/network.h

index 93fbd57b0e88169c7d56bb62950cc7fbbb2ae3a0..511f5587511afb6af0201f973206d034808662ed 100644 (file)
@@ -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;
index 66d2578013ea7e2d151913709fce335237bd1777..6a0e4a3e007b6e2607e47f207bbc8f395d1c97e7 100644 (file)
 
 struct engine;
 
-/** Ways to listen on a socket. */
+/** Ways to listen on a socket (which may exist already). */
 typedef struct {
        int sock_type;    /**< SOCK_DGRAM or SOCK_STREAM */
-       bool tls;         /**< only used together with .kind == NULL and .tcp */
-       bool http;        /**< only used together with .kind == NULL and .tcp */
-       const char *kind; /**< tag for other types than the three usual */
-       bool freebind;    /**< used for binding to non-local address **/
+       bool tls;         /**< only used together with .kind == NULL and SOCK_STREAM */
+       bool http;        /**< DoH2, implies .tls (in current implementation) */
+       const char *kind; /**< tag for other types: "control" or module-handled kinds */
+       bool freebind;    /**< used for binding to non-local address */
 } endpoint_flags_t;
 
 static inline bool endpoint_flags_eq(endpoint_flags_t f1, endpoint_flags_t f2)