From 5d79b88f00c13935f8f6387c5f8bff9436708f6d Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 29 Aug 2022 20:37:12 +0200 Subject: [PATCH] add support for overriding peer-exchange-port for individual hosts This can also be used to disable PEX completely for non-unetd host entries Signed-off-by: Felix Fietkau --- host.c | 6 ++++++ host.h | 1 + pex.c | 9 +++++---- scripts/unet-cli | 4 +++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/host.c b/host.c index 996dbcf..e7a8e83 100644 --- a/host.c +++ b/host.c @@ -92,6 +92,7 @@ network_host_create(struct network *net, struct blob_attr *attr) NETWORK_HOST_IPADDR, NETWORK_HOST_SUBNET, NETWORK_HOST_PORT, + NETWORK_HOST_PEX_PORT, NETWORK_HOST_ENDPOINT, NETWORK_HOST_GATEWAY, __NETWORK_HOST_MAX @@ -102,6 +103,7 @@ network_host_create(struct network *net, struct blob_attr *attr) [NETWORK_HOST_IPADDR] = { "ipaddr", BLOBMSG_TYPE_ARRAY }, [NETWORK_HOST_SUBNET] = { "subnet", BLOBMSG_TYPE_ARRAY }, [NETWORK_HOST_PORT] = { "port", BLOBMSG_TYPE_INT32 }, + [NETWORK_HOST_PEX_PORT] = { "peer-exchange-port", BLOBMSG_TYPE_INT32 }, [NETWORK_HOST_ENDPOINT] = { "endpoint", BLOBMSG_TYPE_STRING }, [NETWORK_HOST_GATEWAY] = { "gateway", BLOBMSG_TYPE_STRING }, }; @@ -164,6 +166,10 @@ network_host_create(struct network *net, struct blob_attr *attr) peer->port = blobmsg_get_u32(cur); else peer->port = net->net_config.port; + if ((cur = tb[NETWORK_HOST_PEX_PORT]) != NULL) + peer->pex_port = blobmsg_get_u32(cur); + else + peer->pex_port = net->net_config.pex_port; if (endpoint) peer->endpoint = strcpy(endpoint_buf, endpoint); if (gateway) diff --git a/host.h b/host.h index b802d77..2f8f881 100644 --- a/host.h +++ b/host.h @@ -13,6 +13,7 @@ struct network_peer { struct blob_attr *ipaddr; struct blob_attr *subnet; int port; + int pex_port; struct { int connect_attempt; diff --git a/pex.c b/pex.c index 1ec140d..62a30f4 100644 --- a/pex.c +++ b/pex.c @@ -57,7 +57,7 @@ pex_get_peer_addr(struct sockaddr_in6 *sin6, struct network *net, *sin6 = (struct sockaddr_in6){ .sin6_family = AF_INET6, .sin6_addr = peer->local_addr.in6, - .sin6_port = htons(net->net_config.pex_port), + .sin6_port = htons(peer->pex_port), }; } @@ -65,7 +65,8 @@ static void pex_msg_send(struct network *net, struct network_peer *peer) { struct sockaddr_in6 sin6 = {}; - if (!peer || peer == &net->net_config.local_host->peer) + if (!peer || peer == &net->net_config.local_host->peer || + !peer->pex_port) return; pex_get_peer_addr(&sin6, net, peer); @@ -661,7 +662,7 @@ int network_pex_open(struct network *net) network_pex_open_auth_connect(net); - if (!local_host || !net->net_config.pex_port) + if (!local_host || !local_host->peer.pex_port) return 0; local = &local_host->peer; @@ -675,7 +676,7 @@ int network_pex_open(struct network *net) sin6.sin6_family = AF_INET6; memcpy(&sin6.sin6_addr, &local->local_addr.in6, sizeof(local->local_addr.in6)); - sin6.sin6_port = htons(net->net_config.pex_port); + sin6.sin6_port = htons(local_host->peer.pex_port); if (bind(fd, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) { perror("bind"); diff --git a/scripts/unet-cli b/scripts/unet-cli index f77f113..535218a 100755 --- a/scripts/unet-cli +++ b/scripts/unet-cli @@ -50,11 +50,12 @@ Usage: ${basename(sourcepath())} [] [] [