Update THANKS.
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000-2016 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 "avl_tree.h"
25 #include "conf.h"
26 #include "logger.h"
27 #include "subnet.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 avl_tree_t *connection_tree;    /* Meta connections */
32 connection_t *everyone;
33
34 static int connection_compare(const connection_t *a, const connection_t *b) {
35         return a < b ? -1 : a == b ? 0 : 1;
36 }
37
38 void init_connections(void) {
39         connection_tree = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
40         everyone = new_connection();
41         everyone->name = xstrdup("everyone");
42         everyone->hostname = xstrdup("BROADCAST");
43 }
44
45 void exit_connections(void) {
46         avl_delete_tree(connection_tree);
47         free_connection(everyone);
48 }
49
50 connection_t *new_connection(void) {
51         connection_t *c;
52
53         c = xmalloc_and_zero(sizeof(connection_t));
54
55         if(!c) {
56                 return NULL;
57         }
58
59         gettimeofday(&c->start, NULL);
60
61         return c;
62 }
63
64 void free_connection_partially(connection_t *c) {
65         free(c->inkey);
66         free(c->outkey);
67         free(c->mychallenge);
68         free(c->hischallenge);
69         free(c->outbuf);
70
71         c->inkey = NULL;
72         c->outkey = NULL;
73         c->mychallenge = NULL;
74         c->hischallenge = NULL;
75         c->outbuf = NULL;
76
77         c->status.pinged = false;
78         c->status.active = false;
79         c->status.connecting = false;
80         c->status.timeout = false;
81         c->status.encryptout = false;
82         c->status.decryptin = false;
83         c->status.mst = false;
84
85         c->options = 0;
86         c->buflen = 0;
87         c->reqlen = 0;
88         c->tcplen = 0;
89         c->allow_request = 0;
90         c->outbuflen = 0;
91         c->outbufsize = 0;
92         c->outbufstart = 0;
93         c->last_ping_time = 0;
94         c->last_flushed_time = 0;
95         c->inbudget = 0;
96         c->outbudget = 0;
97
98         if(c->inctx) {
99                 EVP_CIPHER_CTX_reset(c->inctx);
100                 free(c->inctx);
101                 c->inctx = NULL;
102         }
103
104         if(c->outctx) {
105                 EVP_CIPHER_CTX_reset(c->outctx);
106                 free(c->outctx);
107                 c->outctx = NULL;
108         }
109
110         if(c->rsa_key) {
111                 RSA_free(c->rsa_key);
112                 c->rsa_key = NULL;
113         }
114 }
115
116 void free_connection(connection_t *c) {
117         free_connection_partially(c);
118
119         free(c->name);
120         free(c->hostname);
121
122         if(c->config_tree) {
123                 exit_configuration(&c->config_tree);
124         }
125
126         free(c);
127 }
128
129 void connection_add(connection_t *c) {
130         avl_insert(connection_tree, c);
131 }
132
133 void connection_del(connection_t *c) {
134         avl_delete(connection_tree, c);
135 }
136
137 void dump_connections(void) {
138         avl_node_t *node;
139         connection_t *c;
140
141         logger(LOG_DEBUG, "Connections:");
142
143         for(node = connection_tree->head; node; node = node->next) {
144                 c = node->data;
145                 logger(LOG_DEBUG, " %s at %s options %x socket %d status %04x outbuf %d/%d/%d",
146                        c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof(c->status)),
147                        c->outbufsize, c->outbufstart, c->outbuflen);
148         }
149
150         logger(LOG_DEBUG, "End of connections.");
151 }