]> git.meshlink.io Git - meshlink/blobdiff - src/connection.c
Avoid allocating packet buffers unnecessarily.
[meshlink] / src / connection.c
index 55a7c981d3e7d46ef91e5a6aafe36474d42d8fbe..9cc64914cd4ba217a85d824f9ff378e5f41884b1 100644 (file)
@@ -1,8 +1,6 @@
 /*
     connection.c -- connection list management
-    Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
-                  2000-2005 Ivo Timmermans
-                  2008      Max Rijevski <maksuf@gmail.com>
+    Copyright (C) 2000-2013 Guus Sliepen <guus@meshlink.io>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include "system.h"
 
 #include "list.h"
-#include "cipher.h"
 #include "conf.h"
 #include "connection.h"
 #include "list.h"
 #include "logger.h"
-#include "rsa.h"
+#include "meshlink_internal.h"
 #include "utils.h"
 #include "xalloc.h"
 
-list_t *connection_list;
-connection_t *everyone;
+void init_connections(meshlink_handle_t *mesh) {
+       assert(!mesh->connections);
+       assert(!mesh->everyone);
 
-void init_connections(void) {
-       connection_list = list_alloc((list_action_t) free_connection);
-       everyone = new_connection();
-       everyone->name = xstrdup("everyone");
-       everyone->hostname = xstrdup("BROADCAST");
+       mesh->connections = list_alloc((list_action_t) free_connection);
+       mesh->everyone = new_connection();
+       mesh->everyone->name = xstrdup("mesh->everyone");
 }
 
-void exit_connections(void) {
-       list_delete_list(connection_list);
-       free_connection(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) {
@@ -51,41 +55,37 @@ connection_t *new_connection(void) {
 }
 
 void free_connection(connection_t *c) {
-       if(!c)
-               return;
-
-       cipher_close(c->incipher);
-       digest_close(c->indigest);
-       cipher_close(c->outcipher);
-       digest_close(c->outdigest);
+       assert(c);
 
        sptps_stop(&c->sptps);
        ecdsa_free(c->ecdsa);
-       rsa_free(c->rsa);
-
-       free(c->hischallenge);
 
        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) {
-       list_insert_tail(connection_list, 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) {
-       list_delete(connection_list, c);
+void connection_del(meshlink_handle_t *mesh, connection_t *c) {
+       assert(c);
+
+       io_del(&mesh->loop, &c->io);
+       list_delete(mesh->connections, c);
 }