]> 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-2007 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     $Id$
21 */
22
23 #include "system.h"
24
25 #include "splay_tree.h"
26 #include "cipher.h"
27 #include "conf.h"
28 #include "list.h"
29 #include "logger.h"
30 #include "net.h"                                /* Don't ask. */
31 #include "netutl.h"
32 #include "subnet.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 splay_tree_t *connection_tree;  /* Meta connections */
37 connection_t *broadcast;
38
39 static int connection_compare(const connection_t *a, const connection_t *b) {
40         return a < b ? -1 : a == b ? 0 : 1;
41 }
42
43 void init_connections(void) {
44         cp();
45
46         connection_tree = splay_alloc_tree((splay_compare_t) connection_compare, (splay_action_t) free_connection);
47         broadcast = new_connection();
48         broadcast->name = xstrdup(_("everyone"));
49         broadcast->hostname = xstrdup(_("BROADCAST"));
50 }
51
52 void exit_connections(void) {
53         cp();
54
55         splay_delete_tree(connection_tree);
56         free_connection(broadcast);
57 }
58
59 connection_t *new_connection(void) {
60         cp();
61
62         return xmalloc_and_zero(sizeof(connection_t));
63 }
64
65 void free_connection(connection_t *c) {
66         cp();
67
68         if(!c)
69                 return;
70
71         if(c->name)
72                 free(c->name);
73
74         if(c->hostname)
75                 free(c->hostname);
76
77         cipher_close(&c->incipher);
78         cipher_close(&c->outcipher);
79
80         if(c->hischallenge)
81                 free(c->hischallenge);
82
83         if(c->config_tree)
84                 exit_configuration(&c->config_tree);
85
86         if(c->buffer)
87                 bufferevent_free(c->buffer);
88         
89         if(event_initialized(&c->inevent))
90                 event_del(&c->inevent);
91
92         free(c);
93 }
94
95 void connection_add(connection_t *c) {
96         cp();
97
98         splay_insert(connection_tree, c);
99 }
100
101 void connection_del(connection_t *c) {
102         cp();
103
104         splay_delete(connection_tree, c);
105 }
106
107 int dump_connections(struct evbuffer *out) {
108         splay_node_t *node;
109         connection_t *c;
110
111         cp();
112
113         for(node = connection_tree->head; node; node = node->next) {
114                 c = node->data;
115                 if(evbuffer_add_printf(out,
116                                    _(" %s at %s options %lx socket %d status %04x\n"),
117                                    c->name, c->hostname, c->options, c->socket,
118                                    bitfield_to_int(&c->status, sizeof c->status)) == -1)
119                         return errno;
120         }
121
122         return 0;
123 }
124
125 bool read_connection_config(connection_t *c) {
126         char *fname;
127         int x;
128
129         cp();
130
131         xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
132         x = read_config_file(c->config_tree, fname);
133         free(fname);
134
135         return x == 0;
136 }