]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Don't use static variables when choosing a broadcast address.
[meshlink] / src / meshlink.c
index bee8eee3b44ab94d7acacd6db6ce2dc73b2ec96f..227df29ff3b102f36ce6aef7ec65d48509dde6fe 100644 (file)
@@ -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);
@@ -1773,8 +1777,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) {
@@ -3512,7 +3517,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;
        }
 
@@ -3521,9 +3526,6 @@ void handle_network_change(meshlink_handle_t *mesh, bool online) {
 
 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) {