2 connection.c -- connection list management
3 Copyright (C) 2000-2007 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
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.
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.
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.
25 #include "splay_tree.h"
30 #include "net.h" /* Don't ask. */
36 splay_tree_t *connection_tree; /* Meta connections */
37 connection_t *broadcast;
39 static int connection_compare(const connection_t *a, const connection_t *b) {
40 return a < b ? -1 : a == b ? 0 : 1;
43 void init_connections(void) {
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"));
52 void exit_connections(void) {
55 splay_delete_tree(connection_tree);
56 free_connection(broadcast);
59 connection_t *new_connection(void) {
62 return xmalloc_and_zero(sizeof(connection_t));
65 void free_connection(connection_t *c) {
77 cipher_close(&c->incipher);
78 cipher_close(&c->outcipher);
81 free(c->hischallenge);
84 exit_configuration(&c->config_tree);
87 bufferevent_free(c->buffer);
89 if(event_initialized(&c->inevent))
90 event_del(&c->inevent);
95 void connection_add(connection_t *c) {
98 splay_insert(connection_tree, c);
101 void connection_del(connection_t *c) {
104 splay_delete(connection_tree, c);
107 int dump_connections(struct evbuffer *out) {
113 for(node = connection_tree->head; node; node = node->next) {
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)
125 bool read_connection_config(connection_t *c) {
131 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
132 x = read_config_file(c->config_tree, fname);