utils: add support for passings address family to network_get_endpoint()
authorFelix Fietkau <nbd@nbd.name>
Thu, 15 Sep 2022 20:02:09 +0000 (22:02 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 16 Sep 2022 16:55:17 +0000 (18:55 +0200)
Can be used to limit results to IPv4 addresses

Signed-off-by: Felix Fietkau <nbd@nbd.name>
cli.c
host.c
pex.c
ubus.c
utils.c
utils.h

diff --git a/cli.c b/cli.c
index 24a06b31bbfee228187403958f8e75bc9f920050..e3654f60bc7e5231a5e6258bc2b3d451fda30cb5 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -274,7 +274,7 @@ static int cmd_sync(const char *endpoint, int argc, char **argv)
                        return 1;
        }
 
-       if (network_get_endpoint(&ep, endpoint, UNETD_GLOBAL_PEX_PORT, 0) < 0) {
+       if (network_get_endpoint(&ep, AF_UNSPEC, endpoint, UNETD_GLOBAL_PEX_PORT, 0) < 0) {
                INFO("Invalid hostname/port %s\n", endpoint);
                return 1;
        }
diff --git a/host.c b/host.c
index 9fea39c08a10b2a2b0e7744035e02cb0af1280c0..e0c5827b5578638c8a1cc7d25398817b10eacd87 100644 (file)
--- a/host.c
+++ b/host.c
@@ -383,7 +383,7 @@ network_hosts_connect_cb(struct uloop_timeout *t)
 
                ep = &peer->state.next_endpoint;
                if (peer->endpoint &&
-                   network_get_endpoint(ep, peer->endpoint, peer->port,
+                   network_get_endpoint(ep, AF_UNSPEC, peer->endpoint, peer->port,
                                         peer->state.connect_attempt++))
                        continue;
 
diff --git a/pex.c b/pex.c
index 1f831a0e1da3bc27e71e406045f5d41588b4b762..c8b073104029f060d1c4b17e8aabfba397affc24 100644 (file)
--- a/pex.c
+++ b/pex.c
@@ -715,7 +715,7 @@ network_pex_open_auth_connect(struct network *net)
                if (!peer->endpoint || peer->dynamic)
                        continue;
 
-               if (network_get_endpoint(&ep, peer->endpoint,
+               if (network_get_endpoint(&ep, AF_UNSPEC, peer->endpoint,
                                         UNETD_GLOBAL_PEX_PORT, 0) < 0)
                        continue;
 
@@ -729,7 +729,7 @@ network_pex_open_auth_connect(struct network *net)
        blobmsg_for_each_attr(cur, net->config.auth_connect, rem) {
                union network_endpoint ep = {};
 
-               if (network_get_endpoint(&ep, blobmsg_get_string(cur),
+               if (network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur),
                                         UNETD_GLOBAL_PEX_PORT, 0) < 0)
                        continue;
 
diff --git a/ubus.c b/ubus.c
index 4cac0842911e61959bef281ec75533c7162918dd..439b40166b00c1795360111653126eabe5382913 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -234,7 +234,7 @@ ubus_network_connect(struct ubus_context *ctx, struct ubus_object *obj,
                return UBUS_STATUS_INVALID_ARGUMENT;
 
        if ((cur = tb[CONNECT_ATTR_ADDRESS]) == NULL ||
-           network_get_endpoint(&ep, blobmsg_get_string(cur), UNETD_GLOBAL_PEX_PORT, 0) < 0 ||
+           network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur), UNETD_GLOBAL_PEX_PORT, 0) < 0 ||
            !ep.in.sin_port)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
diff --git a/utils.c b/utils.c
index 0c7067173f733681493e4f2c95aeb70c829b8e0d..704bc57d332de928b5afe59c97a5cd71eaa71448 100644 (file)
--- a/utils.c
+++ b/utils.c
 
 #include "unetd.h"
 
-int network_get_endpoint(union network_endpoint *dest, const char *str,
+int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
                         int default_port, int idx)
 {
        struct addrinfo hints = {
                .ai_flags = AI_ADDRCONFIG,
-               .ai_family = AF_UNSPEC,
+               .ai_family = af,
        };
        char *buf = strdup(str);
        char *host = buf, *port;
@@ -33,6 +33,9 @@ int network_get_endpoint(union network_endpoint *dest, const char *str,
        memset(dest, 0, sizeof(*dest));
 
        if (*host == '[') {
+               if (af == AF_INET)
+                       goto out;
+
                host++;
                port = strchr(host, ']');
                if (!port)
diff --git a/utils.h b/utils.h
index 164070af8df9851962b2215da54722424a257d69..c7fc2804f41099bd17266115da461ff0d47e2709 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -53,7 +53,7 @@ network_endpoint_addr_equal(union network_endpoint *ep1, union network_endpoint
        return !memcmp(a1, a2, len);
 }
 
-int network_get_endpoint(union network_endpoint *dest, const char *str,
+int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
                         int default_port, int idx);
 int network_get_subnet(int af, union network_addr *addr, int *mask,
                       const char *str);