pex: keep active pex hosts after the specified timeout
authorFelix Fietkau <nbd@nbd.name>
Sat, 10 Sep 2022 10:33:03 +0000 (12:33 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 16 Sep 2022 16:55:17 +0000 (18:55 +0200)
Keep them as long as they have sent us a valid message in the last minute

Signed-off-by: Felix Fietkau <nbd@nbd.name>
pex.c
pex.h
unetd.h

diff --git a/pex.c b/pex.c
index 64e2bd244baae7f9a4de1a30a70d44a6732a2115..7a76cf7e4f76f842a541aee270099ffaadd99627 100644 (file)
--- a/pex.c
+++ b/pex.c
@@ -795,12 +795,16 @@ void network_pex_close(struct network *net)
 {
        struct network_pex *pex = &net->pex;
        struct network_pex_host *host, *tmp;
+       uint64_t now = unet_gettime();
 
        uloop_timeout_cancel(&pex->request_update_timer);
        list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
                if (host->timeout)
                        continue;
 
+               if (host->last_active + UNETD_PEX_HOST_ACITVE_TIMEOUT >= now)
+                       continue;
+
                list_del(&host->list);
                free(host);
        }
@@ -837,6 +841,20 @@ global_pex_find_network(const uint8_t *id)
        return NULL;
 }
 
+static void
+global_pex_set_active(struct network *net, struct sockaddr_in6 *addr)
+{
+       struct network_pex *pex = &net->pex;
+       struct network_pex_host *host;
+
+       list_for_each_entry(host, &pex->hosts, list) {
+               if (memcmp(&host->endpoint.in6, addr, sizeof(*addr)) != 0)
+                       continue;
+
+               host->last_active = unet_gettime();
+       }
+}
+
 static void
 global_pex_recv(struct pex_hdr *hdr, struct sockaddr_in6 *addr)
 {
@@ -856,6 +874,8 @@ global_pex_recv(struct pex_hdr *hdr, struct sockaddr_in6 *addr)
 
        *(uint64_t *)hdr->id ^= pex_network_hash(net->config.auth_key, ehdr->nonce);
 
+       global_pex_set_active(net, addr);
+
        D("PEX global rx op=%d", hdr->opcode);
        switch (hdr->opcode) {
        case PEX_MSG_HELLO:
diff --git a/pex.h b/pex.h
index f16f77caabb5e3f37eb5dc4bba4725c1dca0f5ff..d5d08878791591019ba67357e23de86cf53639dd 100644 (file)
--- a/pex.h
+++ b/pex.h
@@ -12,6 +12,7 @@ struct network;
 struct network_pex_host {
        struct list_head list;
        uint64_t timeout;
+       uint64_t last_active;
        union network_endpoint endpoint;
 };
 
diff --git a/unetd.h b/unetd.h
index 6b9ce926293f51c934a7ecf4e10cc218abf1b2a2..365e738c6b22dedefd0dae7006353467ae89c976 100644 (file)
--- a/unetd.h
+++ b/unetd.h
@@ -45,6 +45,8 @@ extern int global_pex_port;
 
 #define UNETD_DATA_UPDATE_DELAY        (10 * 1000)
 
+#define UNETD_PEX_HOST_ACITVE_TIMEOUT  60
+
 void unetd_write_hosts(void);
 int unetd_attach_mssfix(int ifindex, int mtu);