Skip to content

Commit

Permalink
pex: automatically create host entries from incoming endpoint port no…
Browse files Browse the repository at this point in the history
…tifications

Improves turnaround time on initial connect

Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
nbd168 committed Sep 16, 2022
1 parent 3821221 commit 0d37ca7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pex.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ network_pex_host_request_update(struct network *net, struct network_pex_host *ho
network_pex_host_send_endpoint_notify(net, host);
}

static void
network_pex_free_host(struct network *net, struct network_pex_host *host)
{
struct network_pex *pex = &net->pex;

pex->num_hosts--;
list_del(&host->list);
free(host);
}

static void
network_pex_request_update_cb(struct uloop_timeout *t)
{
Expand All @@ -294,8 +304,7 @@ network_pex_request_update_cb(struct uloop_timeout *t)

list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
if (host->timeout && host->timeout < now) {
list_del(&host->list);
free(host);
network_pex_free_host(net, host);
continue;
}

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

out:
if (timeout && (new_host || host->timeout))
Expand Down Expand Up @@ -837,8 +847,7 @@ void network_pex_close(struct network *net)
if (host->last_active + UNETD_PEX_HOST_ACITVE_TIMEOUT >= now)
continue;

list_del(&host->list);
free(host);
network_pex_free_host(net, host);
}

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

list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
list_del(&host->list);
free(host);
}
list_for_each_entry_safe(host, tmp, &pex->hosts, list)
network_pex_free_host(net, host);
}

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

peer->state.next_endpoint.in.sin_port = port->port;
if (net->pex.num_hosts < NETWORK_PEX_HOSTS_LIMIT)
network_pex_create_host(net, &host_ep, 120);
}
break;
}
Expand Down
3 changes: 3 additions & 0 deletions pex.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <libubox/uloop.h>
#include "stun.h"

#define NETWORK_PEX_HOSTS_LIMIT 128

struct network;

struct network_pex_host {
Expand All @@ -22,6 +24,7 @@ struct network_pex_host {
struct network_pex {
struct uloop_fd fd;
struct list_head hosts;
int num_hosts;
struct uloop_timeout request_update_timer;
};

Expand Down

0 comments on commit 0d37ca7

Please sign in to comment.