]> git.meshlink.io Git - meshlink/blob - src/connection.c
Avoid allocating packet buffers unnecessarily.
[meshlink] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000-2013 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "list.h"
23 #include "conf.h"
24 #include "connection.h"
25 #include "list.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 void init_connections(meshlink_handle_t *mesh) {
32         assert(!mesh->connections);
33         assert(!mesh->everyone);
34
35         mesh->connections = list_alloc((list_action_t) free_connection);
36         mesh->everyone = new_connection();
37         mesh->everyone->name = xstrdup("mesh->everyone");
38 }
39
40 void exit_connections(meshlink_handle_t *mesh) {
41         if(mesh->connections) {
42                 list_delete_list(mesh->connections);
43         }
44
45         if(mesh->everyone) {
46                 free_connection(mesh->everyone);
47         }
48
49         mesh->connections = NULL;
50         mesh->everyone = NULL;
51 }
52
53 connection_t *new_connection(void) {
54         return xzalloc(sizeof(connection_t));
55 }
56
57 void free_connection(connection_t *c) {
58         assert(c);
59
60         sptps_stop(&c->sptps);
61         ecdsa_free(c->ecdsa);
62
63         buffer_clear(&c->inbuf);
64         buffer_clear(&c->outbuf);
65
66         if(c->io.cb) {
67                 abort();
68         }
69
70         if(c->socket > 0) {
71                 closesocket(c->socket);
72         }
73
74         free(c->name);
75
76         free(c);
77 }
78
79 void connection_add(meshlink_handle_t *mesh, connection_t *c) {
80         assert(c);
81
82         c->mesh = mesh;
83         list_insert_tail(mesh->connections, c);
84 }
85
86 void connection_del(meshlink_handle_t *mesh, connection_t *c) {
87         assert(c);
88
89         io_del(&mesh->loop, &c->io);
90         list_delete(mesh->connections, c);
91 }