]> git.meshlink.io Git - meshlink/blobdiff - src/net_setup.c
Remove temporary files at startup.
[meshlink] / src / net_setup.c
index d0e450039913c995298f304f3b4003730ad606f8..269b46ed0d4921d08bad76c9478d301b213ed95a 100644 (file)
@@ -93,6 +93,11 @@ bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
        // While we are at it, read known address information
        if(!n->canonical_address) {
                n->canonical_address = packmsg_get_str_dup(&in);
+
+               if(!*n->canonical_address) {
+                       free(n->canonical_address);
+                       n->canonical_address = NULL;
+               }
        } else {
                packmsg_skip_element(&in);
        }
@@ -191,6 +196,12 @@ bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *c
        }
 
        n->canonical_address = packmsg_get_str_dup(&in);
+
+       if(!*n->canonical_address) {
+               free(n->canonical_address);
+               n->canonical_address = NULL;
+       }
+
        uint32_t count = packmsg_get_array(&in);
 
        for(uint32_t i = 0; i < count; i++) {
@@ -267,6 +278,15 @@ static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
        (void)priv;
 
        if(!check_id(name)) {
+               // Check if this is a temporary file, if so remove it
+               const char *suffix = strstr(name, ".tmp");
+
+               if(suffix && !suffix[4]) {
+                       char filename[PATH_MAX];
+                       snprintf(filename, sizeof(filename), "%s" SLASH "current" SLASH "hosts", mesh->confbase);
+                       unlink(filename);
+               }
+
                return true;
        }
 
@@ -301,7 +321,7 @@ static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
        return true;
 }
 
-static int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
        int nfd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
 
        if(nfd == -1) {
@@ -339,7 +359,7 @@ static int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinf
        return nfd;
 }
 
-static int setup_udp_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+int setup_udp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
        int nfd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
 
        if(nfd == -1) {
@@ -422,7 +442,7 @@ static bool add_listen_sockets(meshlink_handle_t *mesh) {
                .ai_family = AF_UNSPEC,
                .ai_socktype = SOCK_STREAM,
                .ai_protocol = IPPROTO_TCP,
-               .ai_flags = AI_PASSIVE,
+               .ai_flags = AI_PASSIVE | AI_NUMERICSERV,
        };
 
        int err = getaddrinfo(NULL, mesh->myport, &hint, &ai);
@@ -470,7 +490,7 @@ static bool add_listen_sockets(meshlink_handle_t *mesh) {
 
                /* If TCP worked, then we require that UDP works as well. */
 
-               int udp_fd = setup_udp_socket(mesh, aip);
+               int udp_fd = setup_udp_listen_socket(mesh, aip);
 
                if(udp_fd == -1) {
                        closesocket(tcp_fd);
@@ -521,13 +541,7 @@ static bool add_listen_sockets(meshlink_handle_t *mesh) {
 /*
   Configure node_t mesh->self and set up the local sockets (listen only)
 */
-bool setup_myself(meshlink_handle_t *mesh) {
-       /* Set some defaults */
-
-       mesh->maxtimeout = 900;
-
-       /* Done */
-
+static bool setup_myself(meshlink_handle_t *mesh) {
        mesh->self->nexthop = mesh->self;
 
        node_add(mesh, mesh->self);
@@ -565,7 +579,6 @@ bool setup_myself(meshlink_handle_t *mesh) {
 
        /* Done. */
 
-       mesh->last_config_check = mesh->loop.now.tv_sec;
        mesh->last_unreachable = mesh->loop.now.tv_sec;
 
        return true;