]> git.meshlink.io Git - meshlink/blobdiff - src/discovery.c
Keep track of interfaces and local addresses.
[meshlink] / src / discovery.c
index 323d22cfdf433438b51e44692132c46ca2455fb0..ece4fbe9e6d56dc3e90ad669df9dbb22e86cf7f8 100644 (file)
@@ -9,10 +9,22 @@
 #include <catta/alternative.h>
 #include <catta/error.h>
 
+#if defined(__APPLE__) || defined(__unix) && !defined(__linux)
+#include <net/route.h>
+#elif defined(__linux)
+#include <asm/types.h>
+#include <net/if.h>
+#include <linux/if_link.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#endif
+
 #include "meshlink_internal.h"
+#include "event.h"
 #include "discovery.h"
 #include "sockaddr.h"
 #include "logger.h"
+#include "netutl.h"
 #include "node.h"
 #include "connection.h"
 #include "xalloc.h"
@@ -80,7 +92,7 @@ static void discovery_create_services(meshlink_handle_t *mesh) {
 
        logger(mesh, MESHLINK_DEBUG, "Adding service\n");
 
-       /* Ifthis is the first time we're called, let's create a new entry group */
+       /* If this is the first time we're called, let's create a new entry group */
        if(!(mesh->catta_group = catta_s_entry_group_new(mesh->catta_server, discovery_entry_group_callback, mesh))) {
                logger(mesh, MESHLINK_ERROR, "catta_entry_group_new() failed: %s\n", catta_strerror(catta_server_errno(mesh->catta_server)));
                goto fail;
@@ -114,8 +126,6 @@ done:
        free(fingerprint);
        free(txt_name);
        free(txt_fingerprint);
-
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 static void discovery_server_callback(CattaServer *server, CattaServerState state, void *userdata) {
@@ -126,15 +136,18 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
 
        switch(state) {
        case CATTA_SERVER_RUNNING:
+
                /* The serve has startup successfully and registered its host
                 * name on the network, so it's time to create our services */
-               pthread_mutex_lock(&(mesh->mesh_mutex));
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
 
                if(!mesh->catta_group) {
                        discovery_create_services(mesh);
                }
 
-               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_mutex_unlock(&mesh->mutex);
 
                break;
 
@@ -143,7 +156,9 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
                char hostname[17];
                generate_rand_string(mesh, hostname, sizeof(hostname));
 
-               pthread_mutex_lock(&(mesh->mesh_mutex));
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
 
                assert(mesh->catta_server);
                assert(mesh->catta_poll);
@@ -154,12 +169,14 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
                        catta_simple_poll_quit(mesh->catta_poll);
                }
 
-               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_mutex_unlock(&mesh->mutex);
        }
        break;
 
        case CATTA_SERVER_REGISTERING:
-               pthread_mutex_lock(&(mesh->mesh_mutex));
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
 
                /* Let's drop our registered services. When the server is back
                 * in CATTA_SERVER_RUNNING state we will register them
@@ -169,12 +186,14 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
                        mesh->catta_group = NULL;
                }
 
-               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_mutex_unlock(&mesh->mutex);
 
                break;
 
        case CATTA_SERVER_FAILURE:
-               pthread_mutex_lock(&(mesh->mesh_mutex));
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
 
                assert(mesh->catta_server);
                assert(mesh->catta_poll);
@@ -182,7 +201,7 @@ static void discovery_server_callback(CattaServer *server, CattaServerState stat
                /* Terminate on failure */
                catta_simple_poll_quit(mesh->catta_poll);
 
-               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_mutex_unlock(&mesh->mutex);
                break;
 
        case CATTA_SERVER_INVALID:
@@ -217,7 +236,9 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
                char *node_fp = (char *)catta_string_list_get_text(node_fp_li) + strlen(MESHLINK_MDNS_FINGERPRINT_KEY);
 
                if(node_name[0] == '=' && node_fp[0] == '=') {
-                       pthread_mutex_lock(&(mesh->mesh_mutex));
+                       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                               abort();
+                       }
 
                        node_name += 1;
 
@@ -250,20 +271,22 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
                                }
 
                                if(naddress.unknown.family != AF_UNKNOWN) {
-                                       meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr *)&naddress);
-
                                        node_t *n = (node_t *)node;
+                                       connection_t *c = n->connection;
+
+                                       n->catta_address = naddress;
+                                       node_add_recent_address(mesh, n, &naddress);
 
-                                       if(n->connection && n->connection->outgoing) {
-                                               n->connection->outgoing->timeout = 0;
+                                       if(c && c->outgoing && !c->status.active) {
+                                               c->outgoing->timeout = 0;
 
-                                               if(n->connection->outgoing->ev.cb) {
-                                                       timeout_set(&mesh->loop, &n->connection->outgoing->ev, &(struct timeval) {
+                                               if(c->outgoing->ev.cb) {
+                                                       timeout_set(&mesh->loop, &c->outgoing->ev, &(struct timespec) {
                                                                0, 0
                                                        });
                                                }
 
-                                               n->connection->last_ping_time = 0;
+                                               c->last_ping_time = -3600;
                                        }
 
                                } else {
@@ -273,7 +296,7 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
                                logger(mesh, MESHLINK_WARNING, "Node %s is not part of the mesh network.\n", node_name);
                        }
 
-                       pthread_mutex_unlock(&(mesh->mesh_mutex));
+                       pthread_mutex_unlock(&mesh->mutex);
                }
        }
 
@@ -288,22 +311,31 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde
        /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
        switch(event) {
        case CATTA_BROWSER_FAILURE:
-               pthread_mutex_lock(&mesh->mesh_mutex);
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
+
                catta_simple_poll_quit(mesh->catta_poll);
-               pthread_mutex_unlock(&mesh->mesh_mutex);
+               pthread_mutex_unlock(&mesh->mutex);
                break;
 
        case CATTA_BROWSER_NEW:
-               pthread_mutex_lock(&mesh->mesh_mutex);
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
+
                catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh);
                handle_network_change(mesh, ++mesh->catta_interfaces);
-               pthread_mutex_unlock(&mesh->mesh_mutex);
+               pthread_mutex_unlock(&mesh->mutex);
                break;
 
        case CATTA_BROWSER_REMOVE:
-               pthread_mutex_lock(&mesh->mesh_mutex);
+               if(pthread_mutex_lock(&mesh->mutex) != 0) {
+                       abort();
+               }
+
                handle_network_change(mesh, --mesh->catta_interfaces);
-               pthread_mutex_unlock(&mesh->mesh_mutex);
+               pthread_mutex_unlock(&mesh->mutex);
                break;
 
        case CATTA_BROWSER_ALL_FOR_NOW:
@@ -343,6 +375,10 @@ static void *discovery_loop(void *userdata) {
        meshlink_handle_t *mesh = userdata;
        assert(mesh);
 
+       if(pthread_mutex_lock(&mesh->discovery_mutex) != 0) {
+               abort();
+       }
+
        // handle catta logs
        catta_set_log_function(discovery_log_cb);
 
@@ -390,15 +426,23 @@ static void *discovery_loop(void *userdata) {
        config.publish_hinfo = 0;
        config.publish_addresses = 1;
        config.publish_no_reverse = 1;
+       config.allow_point_to_point = 1;
 
        /* Allocate a new server */
        int error;
-       mesh->catta_server = catta_server_new(catta_simple_poll_get(mesh->catta_poll), &config, discovery_server_callback, mesh, &error);
+       const CattaPoll *poller = catta_simple_poll_get(mesh->catta_poll);
+
+       if(!poller) {
+               logger(mesh, MESHLINK_ERROR, "Failed to create discovery server: %s\n", catta_strerror(error));
+               goto fail;
+       }
+
+       mesh->catta_server = catta_server_new(poller, &config, discovery_server_callback, mesh, &error);
 
        /* Free the configuration data */
        catta_server_config_free(&config);
 
-       /* Check wether creating the server object succeeded */
+       /* Check whether creating the server object succeeded */
        if(!mesh->catta_server) {
                logger(mesh, MESHLINK_ERROR, "Failed to create discovery server: %s\n", catta_strerror(error));
                goto fail;
@@ -414,7 +458,6 @@ static void *discovery_loop(void *userdata) {
 
 fail:
 
-       pthread_mutex_lock(&mesh->discovery_mutex);
        pthread_cond_broadcast(&mesh->discovery_cond);
        pthread_mutex_unlock(&mesh->discovery_mutex);
 
@@ -451,6 +494,313 @@ fail:
        return NULL;
 }
 
+typedef struct discovery_address {
+       int index;
+       bool up;
+       sockaddr_t address;
+} discovery_address_t;
+
+static int iface_compare(const void *va, const void *vb) {
+       const int *a = va;
+       const int *b = vb;
+       return *a - *b;
+}
+
+static int address_compare(const void *va, const void *vb) {
+       const discovery_address_t *a = va;
+       const discovery_address_t *b = vb;
+
+       if(a->index != b->index) {
+               return a->index - b->index;
+       }
+
+       return sockaddrcmp_noport(&a->address, &b->address);
+}
+
+static void send_mdns_packet(meshlink_handle_t *mesh, const discovery_address_t *addr) {
+       char *host = NULL, *port = NULL;
+       sockaddr2str(&addr->address, &host, &port);
+       fprintf(stderr, "Sending on iface %d %s port %s\n", addr->index, host, port);
+       free(host);
+       free(port);
+}
+
+static void iface_up(meshlink_handle_t *mesh, int index) {
+       int *p = bsearch(&index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+
+       if(p) {
+               return;
+       }
+
+       fprintf(stderr, "iface %d up\n", index);
+       mesh->discovery_ifaces = xrealloc(mesh->discovery_ifaces, ++mesh->discovery_iface_count * sizeof(*p));
+       mesh->discovery_ifaces[mesh->discovery_iface_count - 1] = index;
+       qsort(mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+
+       for(int i = 0; i < mesh->discovery_iface_count; i++) {
+               fprintf(stderr, "%d", mesh->discovery_ifaces[i]);
+       }
+
+       // Send an announcement for all addresses associated with this interface
+       for(int i = 0; i < mesh->discovery_address_count; i++) {
+               if(mesh->discovery_addresses[i].index == index) {
+                       send_mdns_packet(mesh, &mesh->discovery_addresses[i]);
+               }
+       }
+}
+
+static void iface_down(meshlink_handle_t *const mesh, int index) {
+       int *p = bsearch(&index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(int), iface_compare);
+
+       if(!p) {
+               return;
+       }
+
+       fprintf(stderr, "iface %d down\n", index);
+       memmove(p, p + 1, --mesh->discovery_iface_count * sizeof(*p));
+}
+
+static void addr_add(meshlink_handle_t *mesh, const discovery_address_t *addr) {
+       discovery_address_t *p = bsearch(addr, mesh->discovery_addresses, mesh->discovery_address_count, sizeof(int), address_compare);
+
+       if(p) {
+               return;
+       }
+
+       bool up = bsearch(&addr->index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+       char *host = NULL, *port = NULL;
+       sockaddr2str(&addr->address, &host, &port);
+       fprintf(stderr, "address %d %s port %s up %d\n", addr->index, host, port, up);
+       free(host);
+       free(port);
+
+       mesh->discovery_addresses = xrealloc(mesh->discovery_addresses, ++mesh->discovery_address_count * sizeof(*p));
+       mesh->discovery_addresses[mesh->discovery_address_count - 1] = *addr;
+       qsort(mesh->discovery_addresses, mesh->discovery_address_count, sizeof(*p), address_compare);
+
+       mesh->discovery_addresses[mesh->discovery_address_count - 1].up = up;
+
+       if(up) {
+               send_mdns_packet(mesh, &mesh->discovery_addresses[mesh->discovery_address_count - 1]);
+       }
+}
+
+static void addr_del(meshlink_handle_t *mesh, const discovery_address_t *addr) {
+       discovery_address_t *p = bsearch(addr, mesh->discovery_addresses, mesh->discovery_address_count, sizeof(*p), address_compare);
+
+       if(!p) {
+               return;
+       }
+
+       char *host = NULL, *port = NULL;
+       sockaddr2str(&addr->address, &host, &port);
+       fprintf(stderr, "address %d %s port %s down\n", addr->index, host, port);
+       free(host);
+       free(port);
+
+       memmove(p, p + 1, --mesh->discovery_address_count * sizeof(*p));
+}
+
+#if defined(__linux)
+static void netlink_getlink(int fd) {
+       static const struct {
+               struct nlmsghdr nlm;
+               struct ifinfomsg ifi;
+       } msg = {
+               .nlm.nlmsg_len = NLMSG_LENGTH(sizeof(msg.ifi)),
+               .nlm.nlmsg_type = RTM_GETLINK,
+               .nlm.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
+               .nlm.nlmsg_seq = 1,
+               .ifi.ifi_family = AF_UNSPEC,
+       };
+       send(fd, &msg, msg.nlm.nlmsg_len, 0);
+}
+
+static void netlink_getaddr(int fd) {
+       static const struct {
+               struct nlmsghdr nlm;
+               struct ifaddrmsg ifa;
+       } msg = {
+               .nlm.nlmsg_len = NLMSG_LENGTH(sizeof(msg.ifa)),
+               .nlm.nlmsg_type = RTM_GETADDR,
+               .nlm.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
+               .nlm.nlmsg_seq = 2,
+               .ifa.ifa_family = AF_UNSPEC,
+       };
+       send(fd, &msg, msg.nlm.nlmsg_len, 0);
+}
+
+
+static void netlink_parse_link(meshlink_handle_t *mesh, const struct nlmsghdr *nlm) {
+       const struct ifinfomsg *ifi = (const struct ifinfomsg *)(nlm + 1);
+
+       if(ifi->ifi_flags & IFF_UP && ifi->ifi_flags & IFF_MULTICAST) {
+               iface_up(mesh, ifi->ifi_index);
+       } else {
+               iface_down(mesh, ifi->ifi_index);
+       }
+}
+
+static void netlink_parse_addr(meshlink_handle_t *mesh, const struct nlmsghdr *nlm) {
+       const struct ifaddrmsg *ifa = (const struct ifaddrmsg *)(nlm + 1);
+       const uint8_t *ptr = (const uint8_t *)(ifa + 1);
+       size_t len = nlm->nlmsg_len - (ptr - (const uint8_t *)nlm);
+
+       while(len >= sizeof(struct rtattr)) {
+               const struct rtattr *rta = (const struct rtattr *)ptr;
+
+               if(rta->rta_len <= 0 || rta->rta_len > len) {
+                       break;
+               }
+
+               if(rta->rta_type == IFA_ADDRESS) {
+                       discovery_address_t addr  = {
+                               .index = ifa->ifa_index,
+                       };
+
+                       if(rta->rta_len == 8) {
+                               addr.address.sa.sa_family = AF_INET;
+                               memcpy(&addr.address.in.sin_addr, ptr + 4, 4);
+                               addr.address.in.sin_port = 5353;
+                       } else if(rta->rta_len == 20) {
+                               addr.address.sa.sa_family = AF_INET6;
+                               memcpy(&addr.address.in6.sin6_addr, ptr + 4, 16);
+                               addr.address.in6.sin6_port = 5353;
+                               addr.address.in6.sin6_scope_id = ifa->ifa_index;
+                       } else {
+                               addr.address.sa.sa_family = AF_UNKNOWN;
+                       }
+
+                       if(addr.address.sa.sa_family != AF_UNKNOWN) {
+                               if(nlm->nlmsg_type == RTM_NEWADDR) {
+                                       addr_add(mesh, &addr);
+                               } else {
+                                       addr_del(mesh, &addr);
+                               }
+                       }
+               }
+
+               unsigned short rta_len = (rta->rta_len + 3) & ~3;
+               ptr += rta_len;
+               len -= rta_len;
+       }
+}
+
+static void netlink_parse(meshlink_handle_t *mesh, const void *data, size_t len) {
+       const uint8_t *ptr = data;
+
+       while(len >= sizeof(struct nlmsghdr)) {
+               const struct nlmsghdr *nlm = (const struct nlmsghdr *)ptr;
+
+               if(nlm->nlmsg_len > len) {
+                       break;
+               }
+
+               switch(nlm->nlmsg_type) {
+               case RTM_NEWLINK:
+               case RTM_DELLINK:
+                       netlink_parse_link(mesh, nlm);
+                       break;
+
+               case RTM_NEWADDR:
+               case RTM_DELADDR:
+                       netlink_parse_addr(mesh, nlm);
+               }
+
+               ptr += nlm->nlmsg_len;
+               len -= nlm->nlmsg_len;
+       }
+}
+
+static void netlink_io_handler(event_loop_t *loop, void *data, int flags) {
+       (void)flags;
+       static time_t prev_update;
+       meshlink_handle_t *mesh = data;
+
+       struct {
+               struct nlmsghdr nlm;
+               char data[16384];
+       } msg;
+
+       while(true) {
+               ssize_t result = recv(mesh->pfroute_io.fd, &msg, sizeof(msg), MSG_DONTWAIT);
+
+               if(result <= 0) {
+                       if(result == 0 || errno == EAGAIN || errno == EINTR) {
+                               break;
+                       }
+
+                       logger(mesh, MESHLINK_ERROR, "Reading from Netlink socket failed: %s\n", strerror(errno));
+                       io_set(loop, &mesh->pfroute_io, 0);
+               }
+
+               if((size_t)result < sizeof(msg.nlm)) {
+                       logger(mesh, MESHLINK_ERROR, "Invalid Netlink message\n");
+                       break;
+               }
+
+               if(msg.nlm.nlmsg_type == NLMSG_DONE) {
+                       if(msg.nlm.nlmsg_seq == 1) {
+                               // We just got the result of GETLINK, now send GETADDR.
+                               netlink_getaddr(mesh->pfroute_io.fd);
+                       }
+               } else {
+                       netlink_parse(mesh, &msg, result);
+
+                       if(loop->now.tv_sec > prev_update + 5) {
+                               prev_update = loop->now.tv_sec;
+                               handle_network_change(mesh, 1);
+                       }
+               }
+       }
+}
+#elif defined(RTM_NEWADDR)
+static void pfroute_io_handler(event_loop_t *loop, void *data, int flags) {
+       (void)flags;
+       static time_t prev_update;
+       meshlink_handle_t *mesh = data;
+
+       struct {
+               struct rt_msghdr rtm;
+               char data[2048];
+       } msg;
+
+       while(true) {
+               msg.rtm.rtm_version = 0;
+               ssize_t result = recv(mesh->pfroute_io.fd, &msg, sizeof(msg), MSG_DONTWAIT);
+
+               if(result <= 0) {
+                       if(result == 0 || errno == EAGAIN || errno == EINTR) {
+                               break;
+                       }
+
+                       logger(mesh, MESHLINK_ERROR, "Reading from PFROUTE socket failed: %s\n", strerror(errno));
+                       io_set(loop, &mesh->pfroute_io, 0);
+               }
+
+               if(msg.rtm.rtm_version != RTM_VERSION) {
+                       logger(mesh, MESHLINK_ERROR, "Invalid PFROUTE message version\n");
+                       break;
+               }
+
+               switch(msg.rtm.rtm_type) {
+               case RTM_IFINFO:
+               case RTM_NEWADDR:
+               case RTM_DELADDR:
+                       if(loop->now.tv_sec > prev_update + 5) {
+                               prev_update = loop->now.tv_sec;
+                               handle_network_change(mesh, 1);
+                       }
+
+                       break;
+
+               default:
+                       break;
+               }
+       }
+}
+#endif
+
 bool discovery_start(meshlink_handle_t *mesh) {
        logger(mesh, MESHLINK_DEBUG, "discovery_start called\n");
 
@@ -461,19 +811,53 @@ bool discovery_start(meshlink_handle_t *mesh) {
        assert(!mesh->discovery_threadstarted);
        assert(!mesh->catta_servicetype);
 
+       if(pthread_mutex_lock(&mesh->discovery_mutex) != 0) {
+               abort();
+       }
+
        // Start the discovery thread
        if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) {
+               pthread_mutex_unlock(&mesh->discovery_mutex);
                logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno));
                memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread);
                return false;
        }
 
-       pthread_mutex_lock(&mesh->discovery_mutex);
        pthread_cond_wait(&mesh->discovery_cond, &mesh->discovery_mutex);
        pthread_mutex_unlock(&mesh->discovery_mutex);
 
        mesh->discovery_threadstarted = true;
 
+#if defined(__linux)
+       int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
+
+       if(sock != -1) {
+               struct sockaddr_nl sa;
+               memset(&sa, 0, sizeof(sa));
+               sa.nl_family = AF_NETLINK;
+               sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
+
+               if(bind(sock, (struct sockaddr *)&sa, sizeof(sa)) != -1) {
+                       io_add(&mesh->loop, &mesh->pfroute_io, netlink_io_handler, mesh, sock, IO_READ);
+                       netlink_getlink(sock);
+               } else {
+                       logger(mesh, MESHLINK_WARNING, "Could not bind AF_NETLINK socket: %s", strerror(errno));
+               }
+       } else {
+               logger(mesh, MESHLINK_WARNING, "Could not open AF_NETLINK socket: %s", strerror(errno));
+       }
+
+#elif defined(RTM_NEWADDR)
+       int sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
+
+       if(sock != -1) {
+               io_add(&mesh->loop, &mesh->pfroute_io, pfroute_io_handler, mesh, sock, IO_READ);
+       } else {
+               logger(mesh, MESHLINK_WARNING, "Could not open PF_ROUTE socket: %s", strerror(errno));
+       }
+
+#endif
+
        return true;
 }
 
@@ -482,6 +866,16 @@ void discovery_stop(meshlink_handle_t *mesh) {
 
        assert(mesh);
 
+       free(mesh->discovery_ifaces);
+       free(mesh->discovery_addresses);
+       mesh->discovery_iface_count = 0;
+       mesh->discovery_address_count = 0;
+
+       if(mesh->pfroute_io.cb) {
+               close(mesh->pfroute_io.fd);
+               io_del(&mesh->loop, &mesh->pfroute_io);
+       }
+
        // Shut down
        if(mesh->catta_poll) {
                catta_simple_poll_quit(mesh->catta_poll);
@@ -489,7 +883,10 @@ void discovery_stop(meshlink_handle_t *mesh) {
 
        // Wait for the discovery thread to finish
        if(mesh->discovery_threadstarted == true) {
-               pthread_join(mesh->discovery_thread, NULL);
+               if(pthread_join(mesh->discovery_thread, NULL) != 0) {
+                       abort();
+               }
+
                mesh->discovery_threadstarted = false;
        }