X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=5940c70e8e54fdac55e32c3d27e93a6a4bd7eb6c;hb=9457a9cdb66473ad36923845b0f5fb359eec2ef0;hp=958c23e4d83c14cc748fcf3cbb162195395234f0;hpb=359de4c8f0085f6b1af47b9d9e92b3ddc6c47cab;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 958c23e4..5940c70e 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -333,7 +333,7 @@ void remove_duplicate_hostnames(char *host[], char *port[], int n) { break; } - if(found) { + if(found || !is_valid_hostname(host[i])) { free(host[i]); free(port[i]); host[i] = NULL; @@ -417,7 +417,7 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) { char resolved_port[NI_MAXSERV]; err = getnameinfo(ai_in->ai_addr, ai_in->ai_addrlen, resolved_host, sizeof resolved_host, resolved_port, sizeof resolved_port, NI_NUMERICSERV); - if(err) { + if(err || !is_valid_hostname(resolved_host)) { freeaddrinfo(ai_in); continue; } @@ -514,7 +514,7 @@ static bool try_bind(int port) { return true; } -int check_port(meshlink_handle_t *mesh) { +static int check_port(meshlink_handle_t *mesh) { for(int i = 0; i < 1000; i++) { int port = 0x1000 + (rand() & 0x7fff); @@ -846,6 +846,7 @@ static struct timeval idle(event_loop_t *loop, void *data) { // Get our local address(es) by simulating connecting to an Internet host. static void add_local_addresses(meshlink_handle_t *mesh) { struct sockaddr_storage sn; + sn.ss_family = AF_UNKNOWN; socklen_t sl = sizeof(sn); // IPv4 example.org @@ -1013,13 +1014,14 @@ meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const ch if(!name || !*name) { logger(NULL, MESHLINK_ERROR, "No name given!\n"); - //return NULL; - } else { //check name only if there is a name != NULL - if(!check_id(name)) { - logger(NULL, MESHLINK_ERROR, "Invalid name given!\n"); - meshlink_errno = MESHLINK_EINVAL; - return NULL; - } + meshlink_errno = MESHLINK_EINVAL; + return NULL; + }; + + if(!check_id(name)) { + logger(NULL, MESHLINK_ERROR, "Invalid name given!\n"); + meshlink_errno = MESHLINK_EINVAL; + return NULL; } if(devclass < 0 || devclass >= DEV_CLASS_COUNT) { @@ -1602,6 +1604,12 @@ void meshlink_close(meshlink_handle_t *mesh) { close(mesh->netns); } + for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) { + free(packet); + } + + meshlink_queue_exit(&mesh->outpacketqueue); + free(mesh->name); free(mesh->appname); free(mesh->confbase); @@ -1765,8 +1773,9 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const return true; } -void meshlink_send_from_queue(event_loop_t *loop, meshlink_handle_t *mesh) { +void meshlink_send_from_queue(event_loop_t *loop, void *data) { (void)loop; + meshlink_handle_t *mesh = data; vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue); if(!packet) { @@ -1776,6 +1785,8 @@ void meshlink_send_from_queue(event_loop_t *loop, meshlink_handle_t *mesh) { mesh->self->in_packets++; mesh->self->in_bytes += packet->len; route(mesh, mesh->self, packet); + + free(packet); } ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) { @@ -1847,6 +1858,11 @@ meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) { pthread_mutex_lock(&(mesh->mesh_mutex)); node = (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const pthread_mutex_unlock(&(mesh->mesh_mutex)); + + if(!node) { + meshlink_errno = MESHLINK_ENOENT; + } + return node; } @@ -1861,6 +1877,11 @@ meshlink_submesh_t *meshlink_get_submesh(meshlink_handle_t *mesh, const char *na pthread_mutex_lock(&(mesh->mesh_mutex)); submesh = (meshlink_submesh_t *)lookup_submesh(mesh, name); pthread_mutex_unlock(&(mesh->mesh_mutex)); + + if(!submesh) { + meshlink_errno = MESHLINK_ENOENT; + } + return submesh; } @@ -2140,7 +2161,13 @@ int meshlink_get_port(meshlink_handle_t *mesh) { return -1; } - return atoi(mesh->myport); + int port; + + pthread_mutex_lock(&(mesh->mesh_mutex)); + port = atoi(mesh->myport); + pthread_mutex_unlock(&(mesh->mesh_mutex)); + + return port; } bool meshlink_set_port(meshlink_handle_t *mesh, int port) { @@ -2186,10 +2213,10 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { meshlink_errno = MESHLINK_ESTORAGE; free_node(mesh->self); mesh->self = NULL; + goto done; } else if(!setup_network(mesh)) { meshlink_errno = MESHLINK_ENETWORK; - } else { - rval = true; + goto done; } /* Rebuild our own list of recent addresses */ @@ -2199,9 +2226,7 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { /* Write meshlink.conf with the updated port number */ write_main_config_files(mesh); - if(!config_sync(mesh, "current")) { - return false; - } + rval = config_sync(mesh, "current"); done: pthread_mutex_unlock(&(mesh->mesh_mutex)); @@ -2737,6 +2762,8 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { pthread_mutex_unlock(&(mesh->mesh_mutex)); + free(buf); + if(!packmsg_done(&in)) { logger(mesh, MESHLINK_ERROR, "Invalid data\n"); meshlink_errno = MESHLINK_EPEER; @@ -3051,9 +3078,15 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { } void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) { - (void)mesh; + if(!mesh || !channel) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + pthread_mutex_lock(&mesh->mesh_mutex); channel->poll_cb = cb; utcp_set_poll_cb(channel->c, (cb || channel->aio_send) ? channel_poll : NULL); + pthread_mutex_unlock(&mesh->mesh_mutex); } void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { @@ -3111,6 +3144,8 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n return NULL; } + pthread_mutex_lock(&mesh->mesh_mutex); + node_t *n = (node_t *)node; if(!n->utcp) { @@ -3119,12 +3154,14 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n if(!n->utcp) { meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL; + pthread_mutex_unlock(&mesh->mesh_mutex); return NULL; } } if(n->status.blacklisted) { logger(mesh, MESHLINK_ERROR, "Cannot open a channel with blacklisted node\n"); + pthread_mutex_unlock(&mesh->mesh_mutex); return NULL; } @@ -3133,6 +3170,8 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n channel->receive_cb = cb; channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags); + pthread_mutex_unlock(&mesh->mesh_mutex); + if(!channel->c) { meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL; free(channel); @@ -3474,7 +3513,7 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla void handle_network_change(meshlink_handle_t *mesh, bool online) { (void)online; - if(!mesh->connections) { + if(!mesh->connections || !mesh->loop.running) { return; }