X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=98e02f7db623d8c8b4e837eb96e77f23580fddd8;hb=5678b294c5bbb61b983a7a269bc85fd6ea6590ff;hp=54187a59ed9673a63b166eab73b88b44dbce85bd;hpb=a5a8005b2d89712e124ab7295165a3e229abdad5;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 54187a59..98e02f7d 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -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)) { @@ -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);