Send packets back using the same socket as they were received on.
[tinc] / src / node.c
1 /*
2     node.c -- node tree management
3     Copyright (C) 2001-2011 Guus Sliepen <guus@tinc-vpn.org>,
4                   2001-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 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.
19 */
20
21 #include "system.h"
22
23 #include "avl_tree.h"
24 #include "logger.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 avl_tree_t *node_tree;                  /* Known nodes, sorted by name */
32 avl_tree_t *node_udp_tree;              /* Known nodes, sorted by address and port */
33
34 node_t *myself;
35
36 static int node_compare(const node_t *a, const node_t *b) {
37         return strcmp(a->name, b->name);
38 }
39
40 static int node_udp_compare(const node_t *a, const node_t *b) {
41        return sockaddrcmp(&a->address, &b->address);
42 }
43
44 void init_nodes(void) {
45         node_tree = avl_alloc_tree((avl_compare_t) node_compare, (avl_action_t) free_node);
46         node_udp_tree = avl_alloc_tree((avl_compare_t) node_udp_compare, NULL);
47 }
48
49 void exit_nodes(void) {
50         avl_delete_tree(node_udp_tree);
51         avl_delete_tree(node_tree);
52 }
53
54 node_t *new_node(void) {
55         node_t *n = xmalloc_and_zero(sizeof(*n));
56
57         if(replaywin) n->late = xmalloc_and_zero(replaywin);
58         n->subnet_tree = new_subnet_tree();
59         n->edge_tree = new_edge_tree();
60         EVP_CIPHER_CTX_init(&n->inctx);
61         EVP_CIPHER_CTX_init(&n->outctx);
62         n->mtu = MTU;
63         n->maxmtu = MTU;
64
65         return n;
66 }
67
68 void free_node(node_t *n) {
69         if(n->inkey)
70                 free(n->inkey);
71
72         if(n->outkey)
73                 free(n->outkey);
74
75         if(n->subnet_tree)
76                 free_subnet_tree(n->subnet_tree);
77
78         if(n->edge_tree)
79                 free_edge_tree(n->edge_tree);
80
81         sockaddrfree(&n->address);
82
83         EVP_CIPHER_CTX_cleanup(&n->inctx);
84         EVP_CIPHER_CTX_cleanup(&n->outctx);
85
86         if(n->mtuevent)
87                 event_del(n->mtuevent);
88         
89         if(n->hostname)
90                 free(n->hostname);
91
92         if(n->name)
93                 free(n->name);
94
95         if(n->late)
96                 free(n->late);
97
98         free(n);
99 }
100
101 void node_add(node_t *n) {
102         avl_insert(node_tree, n);
103 }
104
105 void node_del(node_t *n) {
106         avl_node_t *node, *next;
107         edge_t *e;
108         subnet_t *s;
109
110         for(node = n->subnet_tree->head; node; node = next) {
111                 next = node->next;
112                 s = node->data;
113                 subnet_del(n, s);
114         }
115
116         for(node = n->edge_tree->head; node; node = next) {
117                 next = node->next;
118                 e = node->data;
119                 edge_del(e);
120         }
121
122         avl_delete(node_udp_tree, n);
123         avl_delete(node_tree, n);
124 }
125
126 node_t *lookup_node(char *name) {
127         node_t n = {NULL};
128
129         n.name = name;
130
131         return avl_search(node_tree, &n);
132 }
133
134 node_t *lookup_node_udp(const sockaddr_t *sa) {
135         node_t n = {NULL};
136
137         n.address = *sa;
138         n.name = NULL;
139
140         return avl_search(node_udp_tree, &n);
141 }
142
143 void update_node_udp(node_t *n, const sockaddr_t *sa) {
144         if(n == myself) {
145                 logger(LOG_WARNING, "Trying to update UDP address of myself!");
146                 return;
147         }
148
149         avl_delete(node_udp_tree, n);
150
151         if(n->hostname)
152                 free(n->hostname);
153
154         if(sa) {
155                 n->address = *sa;
156                 n->hostname = sockaddr2hostname(&n->address);
157                 avl_insert(node_udp_tree, n);
158                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
159         } else {
160                 memset(&n->address, 0, sizeof n->address);
161                 n->hostname = NULL;
162                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s cleared", n->name);
163         }
164 }
165
166 void dump_nodes(void) {
167         avl_node_t *node;
168         node_t *n;
169
170         logger(LOG_DEBUG, "Nodes:");
171
172         for(node = node_tree->head; node; node = node->next) {
173                 n = node->data;
174                 logger(LOG_DEBUG, " %s at %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s pmtu %d (min %d max %d)",
175                            n->name, n->hostname, n->outcipher ? n->outcipher->nid : 0,
176                            n->outdigest ? n->outdigest->type : 0, n->outmaclength, n->outcompression,
177                            n->options, bitfield_to_int(&n->status, sizeof n->status), n->nexthop ? n->nexthop->name : "-",
178                            n->via ? n->via->name : "-", n->mtu, n->minmtu, n->maxmtu);
179         }
180
181         logger(LOG_DEBUG, "End of nodes.");
182 }