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