]> git.meshlink.io Git - meshlink/blob - src/connection.c
Use the crypto wrappers again instead of calling OpenSSL directly.
[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 (void *)a - (void *)b;
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->buffer)
84                 bufferevent_free(c->buffer);
85         
86         if(event_initialized(&c->inevent))
87                 event_del(&c->inevent);
88
89         free(c);
90 }
91
92 void connection_add(connection_t *c) {
93         cp();
94
95         splay_insert(connection_tree, c);
96 }
97
98 void connection_del(connection_t *c) {
99         cp();
100
101         splay_delete(connection_tree, c);
102 }
103
104 int dump_connections(struct evbuffer *out) {
105         splay_node_t *node;
106         connection_t *c;
107
108         cp();
109
110         for(node = connection_tree->head; node; node = node->next) {
111                 c = node->data;
112                 if(evbuffer_add_printf(out,
113                                                            _(" %s at %s options %lx socket %d status %04x\n"),
114                                                            c->name, c->hostname, c->options, c->socket,
115                                                            c->status.value) == -1)
116                         return errno;
117         }
118
119         return 0;
120 }
121
122 bool read_connection_config(connection_t *c) {
123         char *fname;
124         int x;
125
126         cp();
127
128         asprintf(&fname, "%s/hosts/%s", confbase, c->name);
129         x = read_config_file(c->config_tree, fname);
130         free(fname);
131
132         return x == 0;
133 }