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>
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.
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.
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.
24 #include "splay_tree.h"
27 #include "control_common.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) {
44 connection_tree = splay_alloc_tree((splay_compare_t) connection_compare, (splay_action_t) free_connection);
45 broadcast = new_connection();
46 broadcast->name = xstrdup("everyone");
47 broadcast->hostname = xstrdup("BROADCAST");
50 void exit_connections(void) {
51 splay_delete_tree(connection_tree);
52 free_connection(broadcast);
55 connection_t *new_connection(void) {
56 return xmalloc_and_zero(sizeof(connection_t));
59 void free_connection(connection_t *c) {
69 cipher_close(&c->incipher);
70 cipher_close(&c->outcipher);
73 free(c->hischallenge);
76 exit_configuration(&c->config_tree);
79 bufferevent_free(c->buffer);
81 if(event_initialized(&c->inevent))
82 event_del(&c->inevent);
87 void connection_add(connection_t *c) {
88 splay_insert(connection_tree, c);
91 void connection_del(connection_t *c) {
92 splay_delete(connection_tree, c);
95 bool dump_connections(connection_t *cdump) {
99 for(node = connection_tree->head; node; node = node->next) {
101 send_request(cdump, "%d %d %s at %s options %x socket %d status %04x",
102 CONTROL, REQ_DUMP_CONNECTIONS,
103 c->name, c->hostname, c->options, c->socket,
104 bitfield_to_int(&c->status, sizeof c->status));
107 return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);