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);
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
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;
/* 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);
/* 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);