]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Add an asynchronous DNS thread.
[meshlink] / src / meshlink.c
index 38ef57508b7e13540644bdababfc27f2d1042602..16f01c281899e044d226b6df073a0385b3f7f0ca 100644 (file)
 #include "system.h"
 #include <pthread.h>
 
+#include "adns.h"
 #include "crypto.h"
 #include "ecdsagen.h"
 #include "logger.h"
 #include "meshlink_internal.h"
+#include "net.h"
 #include "netutl.h"
 #include "node.h"
 #include "submesh.h"
@@ -1456,6 +1458,7 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
        mesh->submeshes = NULL;
        mesh->log_cb = global_log_cb;
        mesh->log_level = global_log_level;
+       mesh->packet = xmalloc(sizeof(vpn_packet_t));
 
        randomize(&mesh->prng_state, sizeof(mesh->prng_state));
 
@@ -1671,6 +1674,7 @@ bool meshlink_start(meshlink_handle_t *mesh) {
        }
 
        init_outgoings(mesh);
+       init_adns(mesh);
 
        // Start the main thread
 
@@ -1741,6 +1745,7 @@ void meshlink_stop(meshlink_handle_t *mesh) {
                }
        }
 
+       exit_adns(mesh);
        exit_outgoings(mesh);
 
        // Ensure we are considered unreachable
@@ -1805,6 +1810,7 @@ void meshlink_close(meshlink_handle_t *mesh) {
        free(mesh->confbase);
        free(mesh->config_key);
        free(mesh->external_address_url);
+       free(mesh->packet);
        ecdsa_free(mesh->private_key);
 
        if(mesh->invitation_addresses) {
@@ -1962,12 +1968,12 @@ void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb)
        pthread_mutex_unlock(&mesh->mutex);
 }
 
-static vpn_packet_t *prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
+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;
 
        if(len >= MAXSIZE - sizeof(*hdr)) {
                meshlink_errno = MESHLINK_EINVAL;
-               return NULL;
+               return false;
        }
 
        node_t *n = (node_t *)destination;
@@ -1975,17 +1981,10 @@ static vpn_packet_t *prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *de
        if(n->status.blacklisted) {
                logger(mesh, MESHLINK_ERROR, "Node %s blacklisted, dropping packet\n", n->name);
                meshlink_errno = MESHLINK_EBLACKLISTED;
-               return NULL;
+               return false;
        }
 
        // Prepare the packet
-       vpn_packet_t *packet = malloc(sizeof(*packet));
-
-       if(!packet) {
-               meshlink_errno = MESHLINK_ENOMEM;
-               return NULL;
-       }
-
        packet->probe = false;
        packet->tcp = false;
        packet->len = len + sizeof(*hdr);
@@ -1999,7 +1998,7 @@ static vpn_packet_t *prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *de
 
        memcpy(packet->data + sizeof(*hdr), data, len);
 
-       return packet;
+       return true;
 }
 
 static bool meshlink_send_immediate(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
@@ -2009,15 +2008,12 @@ static bool meshlink_send_immediate(meshlink_handle_t *mesh, meshlink_node_t *de
        assert(len);
 
        // Prepare the packet
-       vpn_packet_t *packet = prepare_packet(mesh, destination, data, len);
-
-       if(!packet) {
+       if(!prepare_packet(mesh, destination, data, len, mesh->packet)) {
                return false;
        }
 
        // Send it immediately
-       route(mesh, mesh->self, packet);
-       free(packet);
+       route(mesh, mesh->self, mesh->packet);
 
        return true;
 }
@@ -2039,12 +2035,17 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
        }
 
        // Prepare the packet
-       vpn_packet_t *packet = prepare_packet(mesh, destination, data, len);
+       vpn_packet_t *packet = malloc(sizeof(*packet));
 
        if(!packet) {
+               meshlink_errno = MESHLINK_ENOMEM;
                return false;
        }
 
+       if(!prepare_packet(mesh, destination, data, len, packet)) {
+               free(packet);
+       }
+
        // Queue it
        if(!meshlink_queue_push(&mesh->outpacketqueue, packet)) {
                free(packet);
@@ -4164,6 +4165,15 @@ void meshlink_set_external_address_discovery_url(struct meshlink_handle *mesh, c
        pthread_mutex_unlock(&mesh->mutex);
 }
 
+void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity) {
+       if(!mesh || granularity < 0) {
+               meshlink_errno = EINVAL;
+               return;
+       }
+
+       utcp_set_clock_granularity(granularity);
+}
+
 void handle_network_change(meshlink_handle_t *mesh, bool online) {
        (void)online;
 
@@ -4191,6 +4201,7 @@ void call_error_cb(meshlink_handle_t *mesh, meshlink_errno_t meshlink_errno) {
 
 static void __attribute__((constructor)) meshlink_init(void) {
        crypto_init();
+       utcp_set_clock_granularity(10000);
 }
 
 static void __attribute__((destructor)) meshlink_exit(void) {