X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=8ad72c15573d0f0cf4da9a25e2cbe10cd6391782;hb=8e4f6238558c28cd05bd580cc947d7178d2bac90;hp=55b1e04fcd7d807d5e0304a2887026322e1f98c3;hpb=8d4b96efb7955eaa96174af4804597f92e124041;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 55b1e04f..8ad72c15 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -257,7 +257,7 @@ char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int fami } logger(mesh, MESHLINK_DEBUG, "Trying to discover externally visible hostname...\n"); - struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(host), xstrdup(port ? port : "80"), 5); + struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(host), xstrdup(port ? port : "80"), SOCK_STREAM, 5); char line[256]; char *hostname = NULL; @@ -501,7 +501,7 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) { } // Convert what we have to a sockaddr - struct addrinfo *ai_in = adns_blocking_request(mesh, xstrdup(hostname[i]), xstrdup(port[i]), 5); + struct addrinfo *ai_in = adns_blocking_request(mesh, xstrdup(hostname[i]), xstrdup(port[i]), SOCK_STREAM, 5); if(!ai_in) { continue; @@ -1456,12 +1456,17 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { } } - // initialize mutex + // initialize mutexes and conds pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&mesh->mutex, &attr); + pthread_mutex_init(&mesh->discovery_mutex, NULL); + pthread_cond_init(&mesh->discovery_cond, NULL); + + pthread_cond_init(&mesh->adns_cond, NULL); + mesh->threadstarted = false; event_loop_init(&mesh->loop); mesh->loop.data = mesh; @@ -1659,7 +1664,12 @@ bool meshlink_start(meshlink_handle_t *mesh) { event_loop_start(&mesh->loop); - if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) { + // Ensure we have a decent amount of stack space. Musl's default of 80 kB is too small. + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 1024 * 1024); + + if(pthread_create(&mesh->thread, &attr, meshlink_main_loop, mesh) != 0) { logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno)); memset(&mesh->thread, 0, sizeof(mesh)->thread); meshlink_errno = MESHLINK_EINTERNAL; @@ -2882,11 +2892,11 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { } // Connect to the meshlink daemon mentioned in the URL. - struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), 5); + struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), SOCK_STREAM, 5); if(ai) { for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) { - state.sock = socket_in_netns(aip->ai_family, aip->ai_socktype, aip->ai_protocol, mesh->netns); + state.sock = socket_in_netns(aip->ai_family, SOCK_STREAM, IPPROTO_TCP, mesh->netns); if(state.sock == -1) { logger(mesh, MESHLINK_DEBUG, "Could not open socket: %s\n", strerror(errno)); @@ -3465,7 +3475,7 @@ static bool aio_finish_one(meshlink_handle_t *mesh, meshlink_channel_t *channel, if(aio->data) { if(aio->cb.buffer) { - aio->cb.buffer(mesh, channel, aio->data, aio->len, aio->priv); + aio->cb.buffer(mesh, channel, aio->data, aio->done, aio->priv); } } else { if(aio->cb.fd) { @@ -3675,6 +3685,11 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { if(aio->data) { sent = utcp_send(connection, (char *)aio->data + aio->done, todo); } else { + /* Limit the amount we read at once to avoid stack overflows */ + if(todo > 65536) { + todo = 65536; + } + char buf[todo]; ssize_t result = read(aio->fd, buf, todo); @@ -3701,7 +3716,7 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { if(sent != (ssize_t)todo) { /* We should never get a partial send at this point */ - assert(sent < 0); + assert(sent <= 0); /* Sending failed, abort all outstanding AIO buffers and send a poll callback. */ if(!aio_abort(mesh, channel, &channel->aio_send)) {