X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=src%2Fconnection.c;h=9cc64914cd4ba217a85d824f9ff378e5f41884b1;hp=3db7a2711ba62b8b8f293e93cbc1074d5228eb5b;hb=963c5055505f2fc117cd5efa06eaa02c9b2bf85d;hpb=5216839fa30dfbf5204b274b4e8becbb34e6ca21 diff --git a/src/connection.c b/src/connection.c index 3db7a271..9cc64914 100644 --- a/src/connection.c +++ b/src/connection.c @@ -28,16 +28,26 @@ #include "utils.h" #include "xalloc.h" -void init_connections(void) { +void init_connections(meshlink_handle_t *mesh) { + assert(!mesh->connections); + assert(!mesh->everyone); + mesh->connections = list_alloc((list_action_t) free_connection); mesh->everyone = new_connection(); mesh->everyone->name = xstrdup("mesh->everyone"); - mesh->everyone->hostname = xstrdup("BROADCAST"); } -void exit_connections(void) { - list_delete_list(mesh->connections); - free_connection(mesh->everyone); +void exit_connections(meshlink_handle_t *mesh) { + if(mesh->connections) { + list_delete_list(mesh->connections); + } + + if(mesh->everyone) { + free_connection(mesh->everyone); + } + + mesh->connections = NULL; + mesh->everyone = NULL; } connection_t *new_connection(void) { @@ -45,8 +55,7 @@ connection_t *new_connection(void) { } void free_connection(connection_t *c) { - if(!c) - return; + assert(c); sptps_stop(&c->sptps); ecdsa_free(c->ecdsa); @@ -54,24 +63,29 @@ void free_connection(connection_t *c) { buffer_clear(&c->inbuf); buffer_clear(&c->outbuf); - io_del(&c->io); + if(c->io.cb) { + abort(); + } - if(c->socket > 0) + if(c->socket > 0) { closesocket(c->socket); + } free(c->name); - free(c->hostname); - - if(c->config_tree) - exit_configuration(&c->config_tree); free(c); } -void connection_add(connection_t *c) { +void connection_add(meshlink_handle_t *mesh, connection_t *c) { + assert(c); + + c->mesh = mesh; list_insert_tail(mesh->connections, c); } -void connection_del(connection_t *c) { +void connection_del(meshlink_handle_t *mesh, connection_t *c) { + assert(c); + + io_del(&mesh->loop, &c->io); list_delete(mesh->connections, c); }