X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=7a01bd0ffae8f921f7e3b28457e26f6198b7b6b2;hb=f31267783fccb4e17d914c64243b0cba4e5632c4;hp=a9988855e9493a77492852559e4ad4df4affc002;hpb=bcd1979454cd14087394f0c0a983205f6fbfcaf4;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index a9988855..7a01bd0f 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -516,7 +516,7 @@ static bool try_bind(int port) { static int check_port(meshlink_handle_t *mesh) { for(int i = 0; i < 1000; i++) { - int port = 0x1000 + (rand() & 0x7fff); + int port = 0x1000 + prng(mesh, 0x8000); if(try_bind(port)) { free(mesh->myport); @@ -1286,6 +1286,8 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { mesh->log_cb = global_log_cb; mesh->log_level = global_log_level; + randomize(&mesh->prng_state, sizeof(mesh->prng_state)); + memcpy(mesh->dev_class_traits, default_class_traits, sizeof(default_class_traits)); if(usingname) { @@ -1615,10 +1617,12 @@ void meshlink_close(meshlink_handle_t *mesh) { free(mesh->confbase); free(mesh->config_key); ecdsa_free(mesh->private_key); - pthread_mutex_destroy(&(mesh->mesh_mutex)); main_config_unlock(mesh); + pthread_mutex_unlock(&mesh->mesh_mutex); + pthread_mutex_destroy(&mesh->mesh_mutex); + memset(mesh, 0, sizeof(*mesh)); free(mesh); @@ -1714,6 +1718,17 @@ void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, me } } +void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb) { + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + pthread_mutex_lock(&(mesh->mesh_mutex)); + mesh->error_cb = cb; + pthread_mutex_unlock(&(mesh->mesh_mutex)); +} + bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) { meshlink_packethdr_t *hdr; @@ -1773,8 +1788,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) { @@ -3519,11 +3535,24 @@ void handle_network_change(meshlink_handle_t *mesh, bool online) { retry(mesh); } +void call_error_cb(meshlink_handle_t *mesh, meshlink_errno_t meshlink_errno) { + // We should only call the callback function if we are in the background thread. + if(!mesh->error_cb) { + return; + } + + if(!mesh->threadstarted) { + return; + } + + if(mesh->thread == pthread_self()) { + mesh->error_cb(mesh, meshlink_errno); + } +} + + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); - unsigned int seed; - randomize(&seed, sizeof(seed)); - srand(seed); } static void __attribute__((destructor)) meshlink_exit(void) {