2 connection.c -- connection list management
3 Copyright (C) 2000-2009 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "net.h" /* Don't ask. */
33 avl_tree_t *connection_tree; /* Meta connections */
34 connection_t *broadcast;
36 static int connection_compare(const connection_t *a, const connection_t *b)
38 return a < b ? -1 : a == b ? 0 : 1;
41 void init_connections(void)
45 connection_tree = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
46 broadcast = new_connection();
47 broadcast->name = xstrdup(_("everyone"));
48 broadcast->hostname = xstrdup(_("BROADCAST"));
51 void exit_connections(void)
55 avl_delete_tree(connection_tree);
56 free_connection(broadcast);
59 connection_t *new_connection(void)
65 c = xmalloc_and_zero(sizeof(connection_t));
70 gettimeofday(&c->start, NULL);
75 void free_connection(connection_t *c)
92 EVP_CIPHER_CTX_cleanup(c->inctx);
97 EVP_CIPHER_CTX_cleanup(c->outctx);
102 free(c->mychallenge);
105 free(c->hischallenge);
108 exit_configuration(&c->config_tree);
114 RSA_free(c->rsa_key);
119 void connection_add(connection_t *c)
123 avl_insert(connection_tree, c);
126 void connection_del(connection_t *c)
130 avl_delete(connection_tree, c);
133 void dump_connections(void)
140 logger(LOG_DEBUG, _("Connections:"));
142 for(node = connection_tree->head; node; node = node->next) {
144 logger(LOG_DEBUG, _(" %s at %s options %lx socket %d status %04x outbuf %d/%d/%d"),
145 c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
146 c->outbufsize, c->outbufstart, c->outbuflen);
149 logger(LOG_DEBUG, _("End of connections."));
152 bool read_connection_config(connection_t *c)
159 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
160 x = read_config_file(c->config_tree, fname);