]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix PMTU test case.
[meshlink] / src / meshlink.c
index 4cc285deec1d8270624bf7ba3b7374213da083ec..95880e31fbd91d70c509be18cc2cf3a1b32a088e 100644 (file)
@@ -30,6 +30,7 @@
 #include "node.h"
 #include "submesh.h"
 #include "packmsg.h"
+#include "pmtu.h"
 #include "prf.h"
 #include "protocol.h"
 #include "route.h"
@@ -1021,14 +1022,6 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) {
        return true;
 }
 
-static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
-       if(a->tv_sec == b->tv_sec) {
-               return a->tv_nsec < b->tv_nsec;
-       } else {
-               return a->tv_sec < b->tv_sec;
-       }
-}
-
 static struct timespec idle(event_loop_t *loop, void *data) {
        (void)loop;
        meshlink_handle_t *mesh = data;
@@ -1723,11 +1716,19 @@ static void *meshlink_main_loop(void *arg) {
                abort();
        }
 
+       if(mesh->thread_status_cb) {
+               mesh->thread_status_cb(mesh, true);
+       }
+
        logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
        pthread_cond_broadcast(&mesh->cond);
        main_loop(mesh);
        logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
 
+       if(mesh->thread_status_cb) {
+               mesh->thread_status_cb(mesh, false);
+       }
+
        pthread_mutex_unlock(&mesh->mutex);
 
        // Stop discovery
@@ -2166,6 +2167,22 @@ void meshlink_set_blacklisted_cb(struct meshlink_handle *mesh, meshlink_blacklis
        pthread_mutex_unlock(&mesh->mutex);
 }
 
+void meshlink_set_thread_status_cb(struct meshlink_handle *mesh, meshlink_thread_status_cb_t cb) {
+       logger(mesh, MESHLINK_DEBUG, "meshlink_set_thread_status_cb(%p)", (void *)(intptr_t)cb);
+
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       mesh->thread_status_cb = cb;
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
 static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) {
        meshlink_packethdr_t *hdr;
 
@@ -2184,7 +2201,6 @@ static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination
 
        // Prepare the packet
        packet->probe = false;
-       packet->tcp = false;
        packet->len = len + sizeof(*hdr);
 
        hdr = (meshlink_packethdr_t *)packet->data;
@@ -2270,8 +2286,6 @@ void meshlink_send_from_queue(event_loop_t *loop, void *data) {
 
        for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) {
                logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len);
-               mesh->self->in_packets++;
-               mesh->self->in_bytes += packet->len;
                route(mesh, mesh->self, packet);
                free(packet);
        }
@@ -2562,6 +2576,25 @@ dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t
        return devclass;
 }
 
+bool meshlink_get_node_tiny(meshlink_handle_t *mesh, meshlink_node_t *node) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return -1;
+       }
+
+       bool tiny;
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       tiny = ((node_t *)node)->status.tiny;
+
+       pthread_mutex_unlock(&mesh->mutex);
+
+       return tiny;
+}
+
 bool meshlink_get_node_blacklisted(meshlink_handle_t *mesh, meshlink_node_t *node) {
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -3599,6 +3632,7 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) {
        n->mtu = 0;
        n->minmtu = 0;
        n->maxmtu = MTU;
+       n->udpprobes = 0;
        n->mtuprobes = 0;
        n->status.udp_confirmed = false;
 
@@ -4005,10 +4039,9 @@ static void channel_retransmit(struct utcp_connection *utcp_connection) {
        node_t *n = utcp_connection->utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
 
-       if(n->mtuprobes == 31 && n->mtutimeout.cb) {
-               timeout_set(&mesh->loop, &n->mtutimeout, &(struct timespec) {
-                       0, 0
-               });
+       if(!n->udpprobes) {
+               timespec_clear(&n->last_udp_probe_sent);
+               keepalive(mesh, n, false);
        }
 }
 
@@ -4034,7 +4067,7 @@ void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t
        channel->receive_cb = cb;
 }
 
-static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
+void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
        (void)mesh;
        node_t *n = (node_t *)source;
 
@@ -4188,7 +4221,6 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac
        }
 
        mesh->channel_accept_cb = cb;
-       mesh->receive_cb = channel_receive;
 
        for splay_each(node_t, n, mesh->nodes) {
                if(!n->utcp && n != mesh->self) {
@@ -4283,7 +4315,6 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
                n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
                utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t));
                utcp_set_retransmit_cb(n->utcp, channel_retransmit);
-               mesh->receive_cb = channel_receive;
 
                if(!n->utcp) {
                        meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;