Skip to content

Commit e88f2cd

Browse files
committed
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 <[email protected]>
1 parent dda15ea commit e88f2cd

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

cli.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static int cmd_sync(const char *endpoint, int argc, char **argv)
274274
return 1;
275275
}
276276

277-
if (network_get_endpoint(&ep, endpoint, UNETD_GLOBAL_PEX_PORT, 0) < 0) {
277+
if (network_get_endpoint(&ep, AF_UNSPEC, endpoint, UNETD_GLOBAL_PEX_PORT, 0) < 0) {
278278
INFO("Invalid hostname/port %s\n", endpoint);
279279
return 1;
280280
}

host.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ network_hosts_connect_cb(struct uloop_timeout *t)
383383

384384
ep = &peer->state.next_endpoint;
385385
if (peer->endpoint &&
386-
network_get_endpoint(ep, peer->endpoint, peer->port,
386+
network_get_endpoint(ep, AF_UNSPEC, peer->endpoint, peer->port,
387387
peer->state.connect_attempt++))
388388
continue;
389389

pex.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ network_pex_open_auth_connect(struct network *net)
715715
if (!peer->endpoint || peer->dynamic)
716716
continue;
717717

718-
if (network_get_endpoint(&ep, peer->endpoint,
718+
if (network_get_endpoint(&ep, AF_UNSPEC, peer->endpoint,
719719
UNETD_GLOBAL_PEX_PORT, 0) < 0)
720720
continue;
721721

@@ -729,7 +729,7 @@ network_pex_open_auth_connect(struct network *net)
729729
blobmsg_for_each_attr(cur, net->config.auth_connect, rem) {
730730
union network_endpoint ep = {};
731731

732-
if (network_get_endpoint(&ep, blobmsg_get_string(cur),
732+
if (network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur),
733733
UNETD_GLOBAL_PEX_PORT, 0) < 0)
734734
continue;
735735

ubus.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ ubus_network_connect(struct ubus_context *ctx, struct ubus_object *obj,
234234
return UBUS_STATUS_INVALID_ARGUMENT;
235235

236236
if ((cur = tb[CONNECT_ATTR_ADDRESS]) == NULL ||
237-
network_get_endpoint(&ep, blobmsg_get_string(cur), UNETD_GLOBAL_PEX_PORT, 0) < 0 ||
237+
network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur), UNETD_GLOBAL_PEX_PORT, 0) < 0 ||
238238
!ep.in.sin_port)
239239
return UBUS_STATUS_INVALID_ARGUMENT;
240240

utils.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
#include "unetd.h"
1919

20-
int network_get_endpoint(union network_endpoint *dest, const char *str,
20+
int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
2121
int default_port, int idx)
2222
{
2323
struct addrinfo hints = {
2424
.ai_flags = AI_ADDRCONFIG,
25-
.ai_family = AF_UNSPEC,
25+
.ai_family = af,
2626
};
2727
char *buf = strdup(str);
2828
char *host = buf, *port;
@@ -33,6 +33,9 @@ int network_get_endpoint(union network_endpoint *dest, const char *str,
3333
memset(dest, 0, sizeof(*dest));
3434

3535
if (*host == '[') {
36+
if (af == AF_INET)
37+
goto out;
38+
3639
host++;
3740
port = strchr(host, ']');
3841
if (!port)

utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ network_endpoint_addr_equal(union network_endpoint *ep1, union network_endpoint
5353
return !memcmp(a1, a2, len);
5454
}
5555

56-
int network_get_endpoint(union network_endpoint *dest, const char *str,
56+
int network_get_endpoint(union network_endpoint *dest, int af, const char *str,
5757
int default_port, int idx);
5858
int network_get_subnet(int af, union network_addr *addr, int *mask,
5959
const char *str);

0 commit comments

Comments
 (0)