]> git.meshlink.io Git - meshlink-tiny/commitdiff
Remove asynchronous DNS support.
authorGuus Sliepen <guus@meshlink.io>
Sat, 19 Jun 2021 13:59:24 +0000 (15:59 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sat, 19 Jun 2021 13:59:24 +0000 (15:59 +0200)
src/Makefile.am
src/adns.c [deleted file]
src/adns.h [deleted file]
src/devtools.c
src/devtools.h
src/meshlink.c
src/meshlink_internal.h
src/net_socket.c

index 51a42e9688ab8824e3f24ec42ef9e0b047a12c48..d6ebe5e4109cdfa231a7e0ac9da9f664c7ef176e 100644 (file)
@@ -37,7 +37,6 @@ pkginclude_HEADERS = meshlink-tiny++.h meshlink-tiny.h
 libmeshlink_tiny_la_LDFLAGS = -export-symbols $(srcdir)/meshlink.sym
 
 libmeshlink_tiny_la_SOURCES = \
-       adns.c adns.h \
        buffer.c buffer.h \
        conf.c conf.h \
        connection.c connection.h \
diff --git a/src/adns.c b/src/adns.c
deleted file mode 100644 (file)
index 758c080..0000000
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
-    dns.c -- hostname resolving functions
-    Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License along
-    with this program; if not, write to the Free Software Foundation, Inc.,
-    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-
-#include "system.h"
-
-#include <pthread.h>
-
-#include "adns.h"
-#include "devtools.h"
-#include "logger.h"
-#include "xalloc.h"
-
-typedef struct adns_item {
-       adns_cb_t cb;
-       void *data;
-       time_t deadline;
-       struct addrinfo *ai;
-       int err;
-       char *host;
-       char *serv;
-} adns_item_t;
-
-static void *adns_loop(void *data) {
-       meshlink_handle_t *mesh = data;
-
-       while(true) {
-               adns_item_t *item = meshlink_queue_pop_cond(&mesh->adns_queue, &mesh->adns_cond);
-
-               if(!item) {
-                       break;
-               }
-
-               if(time(NULL) < item->deadline) {
-                       logger(mesh, MESHLINK_DEBUG, "Resolving %s port %s", item->host, item->serv);
-                       devtool_adns_resolve_probe();
-                       int result = getaddrinfo(item->host, item->serv, NULL, &item->ai);
-
-                       if(result) {
-                               item->ai = NULL;
-                               item->err = errno;
-                       }
-               } else {
-                       logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", item->host, item->serv);
-                       item->ai = NULL;
-                       item->err = ETIMEDOUT;
-               }
-
-               if(meshlink_queue_push(&mesh->adns_done_queue, item)) {
-                       signal_trigger(&mesh->loop, &mesh->adns_signal);
-               } else {
-                       free(item->host);
-                       free(item->serv);
-                       free(item);
-               }
-       }
-
-       return NULL;
-}
-
-static void adns_cb_handler(event_loop_t *loop, void *data) {
-       (void)loop;
-       meshlink_handle_t *mesh = data;
-
-       for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_done_queue));) {
-               item->cb(mesh, item->host, item->serv, item->data, item->ai, item->err);
-               free(item);
-       }
-}
-
-void init_adns(meshlink_handle_t *mesh) {
-       meshlink_queue_init(&mesh->adns_queue);
-       meshlink_queue_init(&mesh->adns_done_queue);
-       signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
-       pthread_create(&mesh->adns_thread, NULL, adns_loop, mesh);
-}
-
-void exit_adns(meshlink_handle_t *mesh) {
-       if(!mesh->adns_signal.cb) {
-               return;
-       }
-
-       /* Drain the queue of any pending ADNS requests */
-       for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_queue));) {
-               free(item->host);
-               free(item->serv);
-               free(item);
-       }
-
-       /* Signal the ADNS thread to stop */
-       if(!meshlink_queue_push(&mesh->adns_queue, NULL)) {
-               abort();
-       }
-
-       pthread_cond_signal(&mesh->adns_cond);
-
-       pthread_join(mesh->adns_thread, NULL);
-       meshlink_queue_exit(&mesh->adns_queue);
-       signal_del(&mesh->loop, &mesh->adns_signal);
-}
-
-void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, void *data, int timeout) {
-       adns_item_t *item = xmalloc(sizeof(*item));
-       item->cb = cb;
-       item->data = data;
-       item->deadline = time(NULL) + timeout;
-       item->host = host;
-       item->serv = serv;
-
-       logger(mesh, MESHLINK_DEBUG, "Enqueueing DNS request for %s port %s", item->host, item->serv);
-
-       if(!meshlink_queue_push(&mesh->adns_queue, item)) {
-               abort();
-       }
-
-       pthread_cond_signal(&mesh->adns_cond);
-}
-
-struct adns_blocking_info {
-       meshlink_handle_t *mesh;
-       pthread_mutex_t mutex;
-       pthread_cond_t cond;
-       char *host;
-       char *serv;
-       struct addrinfo *ai;
-       int socktype;
-       bool done;
-};
-
-static void *adns_blocking_handler(void *data) {
-       struct adns_blocking_info *info = data;
-
-       logger(info->mesh, MESHLINK_DEBUG, "Resolving %s port %s", info->host, info->serv);
-       devtool_adns_resolve_probe();
-
-       struct addrinfo hint = {
-               .ai_family = AF_UNSPEC,
-               .ai_socktype = info->socktype,
-       };
-
-       if(getaddrinfo(info->host, info->serv, &hint, &info->ai)) {
-               info->ai = NULL;
-       }
-
-       if(pthread_mutex_lock(&info->mutex) != 0) {
-               abort();
-       }
-
-       bool cleanup = info->done;
-
-       if(!info->done) {
-               info->done = true;
-               pthread_cond_signal(&info->cond);
-       }
-
-       pthread_mutex_unlock(&info->mutex);
-
-       if(cleanup) {
-               free(info->host);
-               free(info->serv);
-               free(info);
-       }
-
-       return NULL;
-}
-
-struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout) {
-       struct adns_blocking_info *info = xzalloc(sizeof(*info));
-
-       info->mesh = mesh;
-       info->host = host;
-       info->serv = serv;
-       info->socktype = socktype;
-       pthread_mutex_init(&info->mutex, NULL);
-       pthread_cond_init(&info->cond, NULL);
-
-       struct timespec deadline;
-       clock_gettime(CLOCK_REALTIME, &deadline);
-       deadline.tv_sec += timeout;
-
-       pthread_t thread;
-
-       if(pthread_create(&thread, NULL, adns_blocking_handler, info)) {
-               free(info->host);
-               free(info->serv);
-               free(info);
-               return NULL;
-       } else {
-               pthread_detach(thread);
-       }
-
-       if(pthread_mutex_lock(&info->mutex) != 0) {
-               abort();
-       }
-
-       pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
-
-       struct addrinfo *result = NULL;
-       bool cleanup = info->done;
-
-       if(info->done) {
-               result = info->ai;
-       } else {
-               logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", host, serv);
-               info->done = true;
-       }
-
-       pthread_mutex_unlock(&info->mutex);
-
-       if(cleanup) {
-               free(info->host);
-               free(info->serv);
-               free(info);
-       }
-
-       return result;
-}
diff --git a/src/adns.h b/src/adns.h
deleted file mode 100644 (file)
index ff3166d..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-#ifndef MESHLINK_ADNS_H
-#define MESHLINK_ADNS_H
-
-/*
-    adns.h -- header file for adns.c
-    Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License along
-    with this program; if not, write to the Free Software Foundation, Inc.,
-    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-
-#include "meshlink_internal.h"
-
-typedef void (*adns_cb_t)(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err);
-
-void init_adns(meshlink_handle_t *mesh);
-void exit_adns(meshlink_handle_t *mesh);
-void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, void *data, int timeout);
-struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout);
-
-#endif
index d14d1d81370cdadf7d18232585b02bdcd2861003..820b9620c6da221bbc4ce56f151820c584f55db3 100644 (file)
@@ -52,7 +52,6 @@ static void sptps_renewal_nop_probe(meshlink_node_t *node) {
 void (*devtool_trybind_probe)(void) = nop_probe;
 void (*devtool_keyrotate_probe)(int stage) = keyrotate_nop_probe;
 void (*devtool_set_inviter_commits_first)(bool inviter_commited_first) = inviter_commits_first_nop_probe;
-void (*devtool_adns_resolve_probe)(void) = nop_probe;
 void (*devtool_sptps_renewal_probe)(meshlink_node_t *node) = sptps_renewal_nop_probe;
 
 void devtool_get_node_status(meshlink_handle_t *mesh, meshlink_node_t *node, devtool_node_status_t *status) {
index c9335cf5450d00ca5366d4ffaef9563dafd4f50f..fc09d823f28062e102511b720af4f78bef7c67c3 100644 (file)
@@ -111,9 +111,6 @@ extern void (*devtool_trybind_probe)(void);
  */
 extern void (*devtool_keyrotate_probe)(int stage);
 
-/// Debug function pointer variable for asynchronous DNS resolving
-extern void (*devtool_adns_resolve_probe)(void);
-
 /// Debug function pointer variable for SPTPS key renewal
 /** This function pointer variable is a userspace tracepoint or debugger callback for
  *  SPTPS key renewal.
index abec7f49994b104623a86abd2a8d8a73e3b02fb4..9bfaa22b854b7333281c5d14f930aa6235a72ce4 100644 (file)
@@ -20,7 +20,6 @@
 #include "system.h"
 #include <pthread.h>
 
-#include "adns.h"
 #include "crypto.h"
 #include "ecdsagen.h"
 #include "logger.h"
@@ -244,7 +243,7 @@ char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int fami
        }
 
        logger(mesh, MESHLINK_DEBUG, "Trying to discover externally visible hostname...\n");
-       struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(host), xstrdup(port ? port : "80"), SOCK_STREAM, 5);
+       struct addrinfo *ai = str2addrinfo(host, port ? port : "80", SOCK_STREAM);
        char line[256];
        char *hostname = NULL;
 
@@ -1391,8 +1390,6 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
        pthread_mutex_init(&mesh->mutex, &attr);
        pthread_cond_init(&mesh->cond, NULL);
 
-       pthread_cond_init(&mesh->adns_cond, NULL);
-
        mesh->threadstarted = false;
        event_loop_init(&mesh->loop);
        mesh->loop.data = mesh;
@@ -1586,7 +1583,6 @@ bool meshlink_start(meshlink_handle_t *mesh) {
        }
 
        init_outgoings(mesh);
-       init_adns(mesh);
 
        // Start the main thread
 
@@ -1655,7 +1651,6 @@ void meshlink_stop(meshlink_handle_t *mesh) {
                }
        }
 
-       exit_adns(mesh);
        exit_outgoings(mesh);
 
        // Try to write out any changed node config files, ignore errors at this point.
@@ -2684,7 +2679,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                }
 
                // Connect to the meshlink daemon mentioned in the URL.
-               struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), SOCK_STREAM, 30);
+               struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
 
                if(ai) {
                        for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
index 10849117c2d45ddf3d4bd88044d317c51fa0ebf7..9b506f03dba0eaf7d80ae5a6b3999cfecb3c9f0f 100644 (file)
@@ -161,13 +161,6 @@ struct meshlink_handle {
        pthread_t thread;
        pthread_cond_t cond;
        bool threadstarted;
-
-       // ADNS
-       pthread_t adns_thread;
-       pthread_cond_t adns_cond;
-       meshlink_queue_t adns_queue;
-       meshlink_queue_t adns_done_queue;
-       signal_t adns_signal;
 };
 
 /// A handle for a MeshLink node.
index 29e37254986b2ab96b2f79ec09b2393f481951fe..7f575dcf8183464c5c89afa0eb1e68aa433213cf 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "system.h"
 
-#include "adns.h"
 #include "conf.h"
 #include "connection.h"
 #include "list.h"
@@ -214,28 +213,6 @@ static void free_known_addresses(struct addrinfo *ai) {
        }
 }
 
-static void canonical_resolve_cb(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err) {
-       (void)serv;
-       (void)err;
-       node_t *n = data;
-
-       free(host);
-       free(serv);
-
-       for list_each(outgoing_t, outgoing, mesh->outgoings) {
-               if(outgoing->node == n) {
-                       if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
-                               outgoing->ai = ai;
-                               outgoing->aip = NULL;
-                               outgoing->state = OUTGOING_CANONICAL;
-                               do_outgoing_connection(mesh, outgoing);
-                       }
-
-                       return;
-               }
-       }
-}
-
 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
        (void)mesh;
 
@@ -255,15 +232,15 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
 
                        if(port) {
                                *port++ = 0;
-                               port = xstrdup(port);
-                               adns_queue(mesh, address, port, canonical_resolve_cb, outgoing->node, 2);
-                               return false;
+                               outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
+                               outgoing->aip = NULL;
+                               outgoing->state = OUTGOING_CANONICAL;
                        } else {
                                logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name);
-                               free(address);
                                outgoing->state = OUTGOING_RECENT;
                        }
 
+                       free(address);
                } else {
                        outgoing->state = OUTGOING_RECENT;
                }