2 node.c -- node tree management
3 Copyright (C) 2001-2013 Guus Sliepen <guus@tinc-vpn.org>,
4 2001-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.
28 #include "splay_tree.h"
32 splay_tree_t *node_tree;
33 static hash_t *node_udp_cache;
37 static int node_compare(const node_t *a, const node_t *b) {
38 return strcmp(a->name, b->name);
41 void init_nodes(void) {
42 node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
43 node_udp_cache = hash_alloc(0x100, sizeof(sockaddr_t));
46 void exit_nodes(void) {
47 hash_free(node_udp_cache);
48 splay_delete_tree(node_tree);
51 node_t *new_node(void) {
52 node_t *n = xzalloc(sizeof *n);
54 if(replaywin) n->late = xzalloc(replaywin);
55 n->edge_tree = new_edge_tree();
62 void free_node(node_t *n) {
64 free_edge_tree(n->edge_tree);
66 sockaddrfree(&n->address);
68 cipher_close(n->incipher);
69 digest_close(n->indigest);
70 cipher_close(n->outcipher);
71 digest_close(n->outdigest);
74 sptps_stop(&n->sptps);
76 timeout_del(&n->mtutimeout);
90 void node_add(node_t *n) {
91 splay_insert(node_tree, n);
94 void node_del(node_t *n) {
95 for splay_each(edge_t, e, n->edge_tree)
98 splay_delete(node_tree, n);
101 node_t *lookup_node(char *name) {
106 return splay_search(node_tree, &n);
109 node_t *lookup_node_udp(const sockaddr_t *sa) {
110 return hash_search(node_udp_cache, sa);
113 void update_node_udp(node_t *n, const sockaddr_t *sa) {
115 logger(DEBUG_ALWAYS, LOG_WARNING, "Trying to update UDP address of myself!");
119 hash_insert(node_udp_cache, &n->address, NULL);
124 for(int i = 0; i < listen_sockets; i++) {
125 if(listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
130 hash_insert(node_udp_cache, sa, n);
132 n->hostname = sockaddr2hostname(&n->address);
133 logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);