]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Set meshlink_errno when trying to create a channel to a blacklisted node.
[meshlink] / src / meshlink.c
index e82ea4f11a384f639f3c6298a42bfa15aecf3e8a..98e02f7db623d8c8b4e837eb96e77f23580fddd8 100644 (file)
@@ -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;
        }
 
@@ -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)) {
@@ -2854,9 +2863,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 +2884,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 +3167,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 +3193,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 +3201,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);