Skip to content

Commit 0d37ca7

Browse files
committed
pex: automatically create host entries from incoming endpoint port notifications
Improves turnaround time on initial connect Signed-off-by: Felix Fietkau <[email protected]>
1 parent 3821221 commit 0d37ca7

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

pex.c

+20-8
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ network_pex_host_request_update(struct network *net, struct network_pex_host *ho
279279
network_pex_host_send_endpoint_notify(net, host);
280280
}
281281

282+
static void
283+
network_pex_free_host(struct network *net, struct network_pex_host *host)
284+
{
285+
struct network_pex *pex = &net->pex;
286+
287+
pex->num_hosts--;
288+
list_del(&host->list);
289+
free(host);
290+
}
291+
282292
static void
283293
network_pex_request_update_cb(struct uloop_timeout *t)
284294
{
@@ -294,8 +304,7 @@ network_pex_request_update_cb(struct uloop_timeout *t)
294304

295305
list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
296306
if (host->timeout && host->timeout < now) {
297-
list_del(&host->list);
298-
free(host);
307+
network_pex_free_host(net, host);
299308
continue;
300309
}
301310

@@ -725,6 +734,7 @@ void network_pex_create_host(struct network *net, union network_endpoint *ep,
725734
new_host = true;
726735
memcpy(&host->endpoint, ep, sizeof(host->endpoint));
727736
list_add_tail(&host->list, &pex->hosts);
737+
pex->num_hosts++;
728738

729739
out:
730740
if (timeout && (new_host || host->timeout))
@@ -837,8 +847,7 @@ void network_pex_close(struct network *net)
837847
if (host->last_active + UNETD_PEX_HOST_ACITVE_TIMEOUT >= now)
838848
continue;
839849

840-
list_del(&host->list);
841-
free(host);
850+
network_pex_free_host(net, host);
842851
}
843852

844853
if (pex->fd.fd < 0)
@@ -854,10 +863,8 @@ void network_pex_free(struct network *net)
854863
struct network_pex *pex = &net->pex;
855864
struct network_pex_host *host, *tmp;
856865

857-
list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
858-
list_del(&host->list);
859-
free(host);
860-
}
866+
list_for_each_entry_safe(host, tmp, &pex->hosts, list)
867+
network_pex_free_host(net, host);
861868
}
862869

863870
static struct network *
@@ -954,8 +961,13 @@ global_pex_recv(void *msg, size_t msg_len, struct sockaddr_in6 *addr)
954961
memcpy(&peer->state.next_endpoint, addr, sizeof(*addr));
955962
if (hdr->opcode == PEX_MSG_ENDPOINT_PORT_NOTIFY) {
956963
struct pex_endpoint_port_notify *port = data;
964+
union network_endpoint host_ep = {
965+
.in6 = *addr
966+
};
957967

958968
peer->state.next_endpoint.in.sin_port = port->port;
969+
if (net->pex.num_hosts < NETWORK_PEX_HOSTS_LIMIT)
970+
network_pex_create_host(net, &host_ep, 120);
959971
}
960972
break;
961973
}

pex.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <libubox/uloop.h>
1010
#include "stun.h"
1111

12+
#define NETWORK_PEX_HOSTS_LIMIT 128
13+
1214
struct network;
1315

1416
struct network_pex_host {
@@ -22,6 +24,7 @@ struct network_pex_host {
2224
struct network_pex {
2325
struct uloop_fd fd;
2426
struct list_head hosts;
27+
int num_hosts;
2528
struct uloop_timeout request_update_timer;
2629
};
2730

0 commit comments

Comments
 (0)