]> git.meshlink.io Git - meshlink/blob - src/connection.c
Merge branch 'master' into 1.1
[meshlink] / 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 "splay_tree.h"
25 #include "cipher.h"
26 #include "conf.h"
27 #include "list.h"
28 #include "logger.h"
29 #include "net.h"                                /* Don't ask. */
30 #include "netutl.h"
31 #include "subnet.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 splay_tree_t *connection_tree;  /* Meta connections */
36 connection_t *broadcast;
37
38 static int connection_compare(const connection_t *a, const connection_t *b) {
39         return a < b ? -1 : a == b ? 0 : 1;
40 }
41
42 void init_connections(void) {
43         connection_tree = splay_alloc_tree((splay_compare_t) connection_compare, (splay_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         splay_delete_tree(connection_tree);
51         free_connection(broadcast);
52 }
53
54 connection_t *new_connection(void) {
55         return xmalloc_and_zero(sizeof(connection_t));
56 }
57
58 void free_connection(connection_t *c) {
59         if(!c)
60                 return;
61
62         if(c->name)
63                 free(c->name);
64
65         if(c->hostname)
66                 free(c->hostname);
67
68         cipher_close(&c->incipher);
69         cipher_close(&c->outcipher);
70
71         if(c->hischallenge)
72                 free(c->hischallenge);
73
74         if(c->config_tree)
75                 exit_configuration(&c->config_tree);
76
77         if(c->buffer)
78                 bufferevent_free(c->buffer);
79         
80         if(event_initialized(&c->inevent))
81                 event_del(&c->inevent);
82
83         free(c);
84 }
85
86 void connection_add(connection_t *c) {
87         splay_insert(connection_tree, c);
88 }
89
90 void connection_del(connection_t *c) {
91         splay_delete(connection_tree, c);
92 }
93
94 int dump_connections(struct evbuffer *out) {
95         splay_node_t *node;
96         connection_t *c;
97
98         for(node = connection_tree->head; node; node = node->next) {
99                 c = node->data;
100                 if(evbuffer_add_printf(out,
101                                    " %s at %s options %lx socket %d status %04x\n",
102                                    c->name, c->hostname, c->options, c->socket,
103                                    bitfield_to_int(&c->status, sizeof c->status)) == -1)
104                         return errno;
105         }
106
107         return 0;
108 }
109
110 bool read_connection_config(connection_t *c) {
111         char *fname;
112         int x;
113
114         xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
115         x = read_config_file(c->config_tree, fname);
116         free(fname);
117
118         return x == 0;
119 }