]> git.meshlink.io Git - meshlink/blob - src/connection.c
Merge branch 'master' into 1.1
[meshlink] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000-2012 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5                   2008      Max Rijevski <maksuf@gmail.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "splay_tree.h"
25 #include "cipher.h"
26 #include "conf.h"
27 #include "control_common.h"
28 #include "list.h"
29 #include "logger.h"
30 #include "subnet.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 splay_tree_t *connection_tree;  /* Meta connections */
35 connection_t *everyone;
36
37 static int connection_compare(const connection_t *a, const connection_t *b) {
38         return a < b ? -1 : a == b ? 0 : 1;
39 }
40
41 void init_connections(void) {
42         connection_tree = splay_alloc_tree((splay_compare_t) connection_compare, (splay_action_t) free_connection);
43         everyone = new_connection();
44         everyone->name = xstrdup("everyone");
45         everyone->hostname = xstrdup("BROADCAST");
46 }
47
48 void exit_connections(void) {
49         splay_delete_tree(connection_tree);
50         free_connection(everyone);
51 }
52
53 connection_t *new_connection(void) {
54         return xmalloc_and_zero(sizeof(connection_t));
55 }
56
57 void free_connection_partially(connection_t *c) {
58         cipher_close(&c->incipher);
59         digest_close(&c->indigest);
60         cipher_close(&c->outcipher);
61         digest_close(&c->outdigest);
62
63         sptps_stop(&c->sptps);
64         ecdsa_free(&c->ecdsa);
65         rsa_free(&c->rsa);
66
67         if(c->hischallenge) {
68                 free(c->hischallenge);
69                 c->hischallenge = NULL;
70         }
71
72         buffer_clear(&c->inbuf);
73         buffer_clear(&c->outbuf);
74         
75         if(event_initialized(&c->inevent))
76                 event_del(&c->inevent);
77
78         if(event_initialized(&c->outevent))
79                 event_del(&c->outevent);
80
81         if(c->socket > 0)
82                 closesocket(c->socket);
83
84         c->socket = -1;
85
86         c->protocol_major = 0;
87         c->protocol_minor = 0;
88         c->allow_request = 0;
89 }
90
91 void free_connection(connection_t *c) {
92         if(!c)
93                 return;
94
95         free_connection_partially(c);
96
97         free(c->name);
98         free(c->hostname);
99
100         if(c->config_tree)
101                 exit_configuration(&c->config_tree);
102
103         free(c);
104 }
105
106 void connection_add(connection_t *c) {
107         splay_insert(connection_tree, c);
108 }
109
110 void connection_del(connection_t *c) {
111         splay_delete(connection_tree, c);
112 }
113
114 bool dump_connections(connection_t *cdump) {
115         splay_node_t *node;
116         connection_t *c;
117
118         for(node = connection_tree->head; node; node = node->next) {
119                 c = node->data;
120                 send_request(cdump, "%d %d %s %s %x %d %x",
121                                 CONTROL, REQ_DUMP_CONNECTIONS,
122                                 c->name, c->hostname, c->options, c->socket,
123                                 bitfield_to_int(&c->status, sizeof c->status));
124         }
125
126         return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
127 }