Skip to content

Commit

Permalink
utils: add support for passings address family to network_get_endpoint()
Browse files Browse the repository at this point in the history
Can be used to limit results to IPv4 addresses

Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
nbd168 committed Sep 16, 2022
1 parent dda15ea commit e88f2cd
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions pex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ubus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e88f2cd

Please sign in to comment.