]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Add missing calls to fflush().
[meshlink] / src / meshlink.c
index 54187a59ed9673a63b166eab73b88b44dbce85bd..dd571190d86f53b46965e7e2f2aa5be0dd62a174 100644 (file)
@@ -666,7 +666,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
                return false;
        }
 
-       sptps_send_record(&(mesh->sptps), 1, ecdsa_get_public_key(mesh->private_key), 32);
+       sptps_send_record(&mesh->sptps, 1, ecdsa_get_public_key(mesh->private_key), 32);
 
        logger(mesh, MESHLINK_DEBUG, "Configuration stored in: %s\n", mesh->confbase);
 
@@ -699,7 +699,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint
 
        switch(type) {
        case SPTPS_HANDSHAKE:
-               return sptps_send_record(&(mesh->sptps), 0, mesh->cookie, sizeof(mesh)->cookie);
+               return sptps_send_record(&mesh->sptps, 0, mesh->cookie, sizeof(mesh)->cookie);
 
        case 0:
                return finalize_join(mesh, msg, len);
@@ -796,6 +796,7 @@ static const char *errstr[] = {
        [MESHLINK_EPEER] = "Error communicating with peer",
        [MESHLINK_ENOTSUP] = "Operation not supported",
        [MESHLINK_EBUSY] = "MeshLink instance already in use",
+       [MESHLINK_EBLACKLISTED] = "Node is blacklisted",
 };
 
 const char *meshlink_strerror(meshlink_errno_t err) {
@@ -889,6 +890,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) {
        mesh->self->name = xstrdup(mesh->name);
        mesh->self->devclass = mesh->devclass;
        mesh->self->ecdsa = ecdsa_set_public_key(ecdsa_get_public_key(mesh->private_key));
+       mesh->self->session_id = mesh->session_id;
 
        if(!write_main_config_files(mesh)) {
                logger(mesh, MESHLINK_ERROR, "Could not write main config files into %s/current: %s\n", mesh->confbase, strerror(errno));
@@ -967,6 +969,7 @@ static bool meshlink_read_config(meshlink_handle_t *mesh) {
        mesh->self = new_node();
        mesh->self->name = xstrdup(name);
        mesh->self->devclass = mesh->devclass;
+       mesh->self->session_id = mesh->session_id;
 
        if(!node_read_public_key(mesh, mesh->self)) {
                logger(NULL, MESHLINK_ERROR, "Could not read our host configuration file!");
@@ -1288,6 +1291,10 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
 
        randomize(&mesh->prng_state, sizeof(mesh->prng_state));
 
+       do {
+               randomize(&mesh->session_id, sizeof(mesh->session_id));
+       } while(mesh->session_id == 0);
+
        memcpy(mesh->dev_class_traits, default_class_traits, sizeof(default_class_traits));
 
        if(usingname) {
@@ -1751,6 +1758,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
 
        if(n->status.blacklisted) {
                logger(mesh, MESHLINK_ERROR, "Node %s blacklisted, dropping packet\n", n->name);
+               meshlink_errno = MESHLINK_EBLACKLISTED;
                return false;
        }
 
@@ -1783,7 +1791,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
        }
 
        // Notify event loop
-       signal_trigger(&(mesh->loop), &(mesh->datafromapp));
+       signal_trigger(&mesh->loop, &mesh->datafromapp);
 
        return true;
 }
@@ -2221,6 +2229,7 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) {
        mesh->self = new_node();
        mesh->self->name = xstrdup(mesh->name);
        mesh->self->devclass = mesh->devclass;
+       mesh->self->session_id = mesh->session_id;
        xasprintf(&mesh->myport, "%d", port);
 
        if(!node_read_public_key(mesh, mesh->self)) {
@@ -2822,10 +2831,12 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 
        logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n", node->name);
 
-       //Immediately terminate any connections we have with the blacklisted node
+       /* Immediately shut down any connections we have with the blacklisted node.
+        * We can't call terminate_connection(), because we might be called from a callback function.
+        */
        for list_each(connection_t, c, mesh->connections) {
                if(c->node == n) {
-                       terminate_connection(mesh, c, c->status.active);
+                       shutdown(c->socket, SHUT_RDWR);
                }
        }
 
@@ -2837,10 +2848,6 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
        n->mtuprobes = 0;
        n->status.udp_confirmed = false;
 
-       if(n->status.reachable) {
-               update_node_status(mesh, n);
-       }
-
        pthread_mutex_unlock(&mesh->mutex);
 }
 
@@ -2854,9 +2861,15 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 
        node_t *n = (node_t *)node;
 
+       if(n == mesh->self) {
+               logger(mesh, MESHLINK_ERROR, "%s whitelisting itself?\n", node->name);
+               meshlink_errno = MESHLINK_EINVAL;
+               pthread_mutex_unlock(&mesh->mutex);
+               return;
+       }
+
        if(!n->status.blacklisted) {
                logger(mesh, MESHLINK_DEBUG, "Node %s was already whitelisted\n", node->name);
-               meshlink_errno = MESHLINK_EINVAL;
                pthread_mutex_unlock(&mesh->mutex);
                return;
        }
@@ -2869,6 +2882,8 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
                update_node_status(mesh, n);
        }
 
+       logger(mesh, MESHLINK_DEBUG, "Whitelisted %s.\n", node->name);
+
        pthread_mutex_unlock(&mesh->mutex);
        return;
 }
@@ -3150,7 +3165,7 @@ void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *ch
 }
 
 meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags) {
-       if(data || len) {
+       if(data && len) {
                abort();        // TODO: handle non-NULL data
        }
 
@@ -3176,6 +3191,7 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
 
        if(n->status.blacklisted) {
                logger(mesh, MESHLINK_ERROR, "Cannot open a channel with blacklisted node\n");
+               meshlink_errno = MESHLINK_EBLACKLISTED;
                pthread_mutex_unlock(&mesh->mutex);
                return NULL;
        }
@@ -3183,6 +3199,11 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
        meshlink_channel_t *channel = xzalloc(sizeof(*channel));
        channel->node = n;
        channel->receive_cb = cb;
+
+       if(data && !len) {
+               channel->priv = (void *)data;
+       }
+
        channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags);
 
        pthread_mutex_unlock(&mesh->mutex);