Attribution for Timothy Redaelli.
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000-2009 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 "list.h"
27 #include "logger.h"
28 #include "net.h"                                /* Don't ask. */
29 #include "netutl.h"
30 #include "subnet.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 avl_tree_t *connection_tree;    /* Meta connections */
35 connection_t *broadcast;
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 = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
43         broadcast = new_connection();
44         broadcast->name = xstrdup("everyone");
45         broadcast->hostname = xstrdup("BROADCAST");
46 }
47
48 void exit_connections(void) {
49         avl_delete_tree(connection_tree);
50         free_connection(broadcast);
51 }
52
53 connection_t *new_connection(void) {
54         connection_t *c;
55
56         c = xmalloc_and_zero(sizeof(connection_t));
57
58         if(!c)
59                 return NULL;
60
61         gettimeofday(&c->start, NULL);
62
63         return c;
64 }
65
66 void free_connection(connection_t *c) {
67         if(c->name)
68                 free(c->name);
69
70         if(c->hostname)
71                 free(c->hostname);
72
73         if(c->inkey)
74                 free(c->inkey);
75
76         if(c->outkey)
77                 free(c->outkey);
78
79         if(c->inctx) {
80                 EVP_CIPHER_CTX_cleanup(c->inctx);
81                 free(c->inctx);
82         }
83
84         if(c->outctx) {
85                 EVP_CIPHER_CTX_cleanup(c->outctx);
86                 free(c->outctx);
87         }
88
89         if(c->mychallenge)
90                 free(c->mychallenge);
91
92         if(c->hischallenge)
93                 free(c->hischallenge);
94
95         if(c->config_tree)
96                 exit_configuration(&c->config_tree);
97
98         if(c->outbuf)
99                 free(c->outbuf);
100
101         if(c->rsa_key)
102                 RSA_free(c->rsa_key);
103
104         free(c);
105 }
106
107 void connection_add(connection_t *c) {
108         avl_insert(connection_tree, c);
109 }
110
111 void connection_del(connection_t *c) {
112         avl_delete(connection_tree, c);
113 }
114
115 void dump_connections(void) {
116         avl_node_t *node;
117         connection_t *c;
118
119         logger(LOG_DEBUG, "Connections:");
120
121         for(node = connection_tree->head; node; node = node->next) {
122                 c = node->data;
123                 logger(LOG_DEBUG, " %s at %s options %x socket %d status %04x outbuf %d/%d/%d",
124                            c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
125                            c->outbufsize, c->outbufstart, c->outbuflen);
126         }
127
128         logger(LOG_DEBUG, "End of connections.");
129 }