X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=a8f8c928876b034fb169a4c92821faebd851226f;hb=HEAD;hp=71263b910696a4e9531672afec7c8252c4223b82;hpb=88b24fcfccc5802db199d3fac2cec659ac48ca78;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 71263b91..a1a2ec40 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -651,7 +651,7 @@ static bool try_bind(meshlink_handle_t *mesh, int port) { int check_port(meshlink_handle_t *mesh) { for(int i = 0; i < 1000; i++) { - int port = 0x1000 + prng(mesh, 0x8000); + int port = 0x1000 + prng(mesh, 0x7000); if(try_bind(mesh, port)) { free(mesh->myport); @@ -1723,11 +1723,19 @@ static void *meshlink_main_loop(void *arg) { abort(); } + if(mesh->thread_status_cb) { + mesh->thread_status_cb(mesh, true); + } + logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n"); pthread_cond_broadcast(&mesh->cond); main_loop(mesh); logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n"); + if(mesh->thread_status_cb) { + mesh->thread_status_cb(mesh, false); + } + pthread_mutex_unlock(&mesh->mutex); // Stop discovery @@ -2166,6 +2174,22 @@ void meshlink_set_blacklisted_cb(struct meshlink_handle *mesh, meshlink_blacklis pthread_mutex_unlock(&mesh->mutex); } +void meshlink_set_thread_status_cb(struct meshlink_handle *mesh, meshlink_thread_status_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_thread_status_cb(%p)", (void *)(intptr_t)cb); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->thread_status_cb = cb; + pthread_mutex_unlock(&mesh->mutex); +} + static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) { meshlink_packethdr_t *hdr; @@ -2270,8 +2294,6 @@ void meshlink_send_from_queue(event_loop_t *loop, void *data) { for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) { logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len); - mesh->self->in_packets++; - mesh->self->in_bytes += packet->len; route(mesh, mesh->self, packet); free(packet); } @@ -4493,7 +4515,7 @@ bool meshlink_channel_aio_send(meshlink_handle_t *mesh, meshlink_channel_t *chan /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - size_t todo = MIN(len, utcp_get_rcvbuf_free(channel->c)); + size_t todo = MIN(len, utcp_get_sndbuf_free(channel->c)); if(todo) { channel_poll(channel->c, todo); @@ -4538,7 +4560,7 @@ bool meshlink_channel_aio_fd_send(meshlink_handle_t *mesh, meshlink_channel_t *c /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - size_t left = utcp_get_rcvbuf_free(channel->c); + size_t left = utcp_get_sndbuf_free(channel->c); if(left) { channel_poll(channel->c, left);