From: Felix Fietkau Date: Thu, 15 Sep 2022 20:02:09 +0000 (+0200) Subject: utils: add support for passings address family to network_get_endpoint() X-Git-Url: http://git.openwrt.org/feed/packages.git%5Ecd5c448758f30868770b9ebf8b656c1a4211a240?a=commitdiff_plain;h=e88f2cd4d3f0e93ebce1bbd3cd53eb2888b18961;p=project%2Funetd.git utils: add support for passings address family to network_get_endpoint() Can be used to limit results to IPv4 addresses Signed-off-by: Felix Fietkau --- diff --git a/cli.c b/cli.c index 24a06b3..e3654f6 100644 --- 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 9fea39c..e0c5827 100644 --- 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 1f831a0..c8b0731 100644 --- 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 4cac084..439b401 100644 --- 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 0c70671..704bc57 100644 --- a/utils.c +++ b/utils.c @@ -17,12 +17,12 @@ #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 164070a..c7fc280 100644 --- 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);