07960940649532f68b6302574f599cbab2d3a959
[oweals/tinc.git] / 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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 {
38         return a < b ? -1 : a == b ? 0 : 1;
39 }
40
41 void init_connections(void)
42 {
43         cp();
44
45         connection_tree = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
46         broadcast = new_connection();
47         broadcast->name = xstrdup(_("everyone"));
48         broadcast->hostname = xstrdup(_("BROADCAST"));
49 }
50
51 void exit_connections(void)
52 {
53         cp();
54
55         avl_delete_tree(connection_tree);
56         free_connection(broadcast);
57 }
58
59 connection_t *new_connection(void)
60 {
61         connection_t *c;
62
63         cp();
64
65         c = xmalloc_and_zero(sizeof(connection_t));
66
67         if(!c)
68                 return NULL;
69
70         gettimeofday(&c->start, NULL);
71
72         return c;
73 }
74
75 void free_connection(connection_t *c)
76 {
77         cp();
78
79         if(c->name)
80                 free(c->name);
81
82         if(c->hostname)
83                 free(c->hostname);
84
85         if(c->inkey)
86                 free(c->inkey);
87
88         if(c->outkey)
89                 free(c->outkey);
90
91         if(c->inctx) {
92                 EVP_CIPHER_CTX_cleanup(c->inctx);
93                 free(c->inctx);
94         }
95
96         if(c->outctx) {
97                 EVP_CIPHER_CTX_cleanup(c->outctx);
98                 free(c->outctx);
99         }
100
101         if(c->mychallenge)
102                 free(c->mychallenge);
103
104         if(c->hischallenge)
105                 free(c->hischallenge);
106
107         if(c->config_tree)
108                 exit_configuration(&c->config_tree);
109
110         if(c->outbuf)
111                 free(c->outbuf);
112
113         if(c->rsa_key)
114                 RSA_free(c->rsa_key);
115
116         free(c);
117 }
118
119 void connection_add(connection_t *c)
120 {
121         cp();
122
123         avl_insert(connection_tree, c);
124 }
125
126 void connection_del(connection_t *c)
127 {
128         cp();
129
130         avl_delete(connection_tree, c);
131 }
132
133 void dump_connections(void)
134 {
135         avl_node_t *node;
136         connection_t *c;
137
138         cp();
139
140         logger(LOG_DEBUG, _("Connections:"));
141
142         for(node = connection_tree->head; node; node = node->next) {
143                 c = node->data;
144                 logger(LOG_DEBUG, _(" %s at %s options %lx socket %d status %04x outbuf %d/%d/%d"),
145                            c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
146                            c->outbufsize, c->outbufstart, c->outbuflen);
147         }
148
149         logger(LOG_DEBUG, _("End of connections."));
150 }
151
152 bool read_connection_config(connection_t *c)
153 {
154         char *fname;
155         int x;
156
157         cp();
158
159         xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
160         x = read_config_file(c->config_tree, fname);
161         free(fname);
162
163         return x == 0;
164 }