]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Add meshlink_get_node_reachability().
[meshlink] / src / meshlink.c
index fdc6fed874460271a5fe810722871238a8352cc3..2d4c2e1f2beb1c218eebd5343be2f6aa75cb8644 100644 (file)
@@ -612,7 +612,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
        }
 
        // Write host config files
-       while(count--) {
+       for(uint32_t i = 0; i < count; i++) {
                const void *data;
                uint32_t len = packmsg_get_bin_raw(&in, &data);
 
@@ -655,11 +655,24 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
                        return false;
                }
 
-               node_add(mesh, n);
+               if(i == 0) {
+                       /* The first host config file is of the inviter itself;
+                        * remember the address we are currently using for the invitation connection.
+                        */
+                       sockaddr_t sa;
+                       socklen_t salen = sizeof(sa);
 
-               if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
+                       if(getpeername(mesh->sock, &sa.sa, &salen) == 0) {
+                               node_add_recent_address(mesh, n, &sa);
+                       }
+               }
+
+               if(!node_write_config(mesh, n)) {
+                       free_node(n);
                        return false;
                }
+
+               node_add(mesh, n);
        }
 
        /* Ensure the configuration directory metadata is on disk */
@@ -2130,6 +2143,31 @@ meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_
        return s;
 }
 
+bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlink_node *node, time_t *last_reachable, time_t *last_unreachable) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
+
+       node_t *n = (node_t *)node;
+       bool reachable;
+
+       pthread_mutex_lock(&mesh->mutex);
+       reachable = n->status.reachable;
+
+       if(last_reachable) {
+               *last_reachable = n->last_reachable;
+       }
+
+       if(last_unreachable) {
+               *last_unreachable = n->last_unreachable;
+       }
+
+       pthread_mutex_unlock(&mesh->mutex);
+
+       return reachable;
+}
+
 bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) {
        if(!mesh || !data || !len || !signature || !siglen) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -3794,6 +3832,22 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla
        pthread_mutex_unlock(&mesh->mutex);
 }
 
+void meshlink_set_dev_class_fast_retry_period(meshlink_handle_t *mesh, dev_class_t devclass, int fast_retry_period) {
+       if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) {
+               meshlink_errno = EINVAL;
+               return;
+       }
+
+       if(fast_retry_period < 0) {
+               meshlink_errno = EINVAL;
+               return;
+       }
+
+       pthread_mutex_lock(&mesh->mutex);
+       mesh->dev_class_traits[devclass].fast_retry_period = fast_retry_period;
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
 void handle_network_change(meshlink_handle_t *mesh, bool online) {
        (void)online;