7c8cd0ba83e3c326a47505f8f8cd175403926bfe
[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
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "avl_tree.h"
24 #include "conf.h"
25 #include "list.h"
26 #include "logger.h"
27 #include "net.h"                                /* Don't ask. */
28 #include "netutl.h"
29 #include "subnet.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 avl_tree_t *connection_tree;    /* Meta connections */
34 connection_t *broadcast;
35
36 static int connection_compare(const connection_t *a, const connection_t *b) {
37         return a < b ? -1 : a == b ? 0 : 1;
38 }
39
40 void init_connections(void) {
41         connection_tree = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
42         broadcast = new_connection();
43         broadcast->name = xstrdup(_("everyone"));
44         broadcast->hostname = xstrdup(_("BROADCAST"));
45 }
46
47 void exit_connections(void) {
48         avl_delete_tree(connection_tree);
49         free_connection(broadcast);
50 }
51
52 connection_t *new_connection(void) {
53         connection_t *c;
54
55         c = xmalloc_and_zero(sizeof(connection_t));
56
57         if(!c)
58                 return NULL;
59
60         gettimeofday(&c->start, NULL);
61
62         return c;
63 }
64
65 void free_connection(connection_t *c) {
66         if(c->name)
67                 free(c->name);
68
69         if(c->hostname)
70                 free(c->hostname);
71
72         if(c->inkey)
73                 free(c->inkey);
74
75         if(c->outkey)
76                 free(c->outkey);
77
78         if(c->inctx) {
79                 EVP_CIPHER_CTX_cleanup(c->inctx);
80                 free(c->inctx);
81         }
82
83         if(c->outctx) {
84                 EVP_CIPHER_CTX_cleanup(c->outctx);
85                 free(c->outctx);
86         }
87
88         if(c->mychallenge)
89                 free(c->mychallenge);
90
91         if(c->hischallenge)
92                 free(c->hischallenge);
93
94         if(c->config_tree)
95                 exit_configuration(&c->config_tree);
96
97         if(c->outbuf)
98                 free(c->outbuf);
99
100         if(c->rsa_key)
101                 RSA_free(c->rsa_key);
102
103         free(c);
104 }
105
106 void connection_add(connection_t *c) {
107         avl_insert(connection_tree, c);
108 }
109
110 void connection_del(connection_t *c) {
111         avl_delete(connection_tree, c);
112 }
113
114 void dump_connections(void) {
115         avl_node_t *node;
116         connection_t *c;
117
118         logger(LOG_DEBUG, _("Connections:"));
119
120         for(node = connection_tree->head; node; node = node->next) {
121                 c = node->data;
122                 logger(LOG_DEBUG, _(" %s at %s options %lx socket %d status %04x outbuf %d/%d/%d"),
123                            c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
124                            c->outbufsize, c->outbufstart, c->outbuflen);
125         }
126
127         logger(LOG_DEBUG, _("End of connections."));
128 }
129
130 bool read_connection_config(connection_t *c) {
131         char *fname;
132         int x;
133
134         xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
135         x = read_config_file(c->config_tree, fname);
136         free(fname);
137
138         return x == 0;
139 }