]> git.meshlink.io Git - meshlink/blob - src/subnet.c
Remove files not used by MeshLink.
[meshlink] / src / subnet.c
1 /*
2     subnet.c -- handle subnet lookups and lists
3     Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-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 "splay_tree.h"
24 #include "control_common.h"
25 #include "hash.h"
26 #include "logger.h"
27 #include "names.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "script.h"
32 #include "subnet.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 /* lists type of subnet */
37
38 splay_tree_t *subnet_tree;
39
40 /* Subnet lookup cache */
41
42 hash_t *ipv4_cache;
43 hash_t *ipv6_cache;
44 hash_t *mac_cache;
45
46 void subnet_cache_flush(void) {
47         hash_clear(ipv4_cache);
48         hash_clear(ipv6_cache);
49         hash_clear(mac_cache);
50 }
51
52 /* Initialising trees */
53
54 void init_subnets(void) {
55         subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
56
57         ipv4_cache = hash_alloc(0x100, sizeof(ipv4_t));
58         ipv6_cache = hash_alloc(0x100, sizeof(ipv6_t));
59         mac_cache = hash_alloc(0x100, sizeof(mac_t));
60 }
61
62 void exit_subnets(void) {
63         splay_delete_tree(subnet_tree);
64
65         hash_free(ipv4_cache);
66         hash_free(ipv6_cache);
67         hash_free(mac_cache);
68 }
69
70 splay_tree_t *new_subnet_tree(void) {
71         return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
72 }
73
74 void free_subnet_tree(splay_tree_t *subnet_tree) {
75         splay_delete_tree(subnet_tree);
76 }
77
78 /* Allocating and freeing space for subnets */
79
80 subnet_t *new_subnet(void) {
81         return xzalloc(sizeof(subnet_t));
82 }
83
84 void free_subnet(subnet_t *subnet) {
85         free(subnet);
86 }
87
88 /* Adding and removing subnets */
89
90 void subnet_add(node_t *n, subnet_t *subnet) {
91         subnet->owner = n;
92
93         splay_insert(subnet_tree, subnet);
94         splay_insert(n->subnet_tree, subnet);
95
96         subnet_cache_flush();
97 }
98
99 void subnet_del(node_t *n, subnet_t *subnet) {
100         splay_delete(n->subnet_tree, subnet);
101         splay_delete(subnet_tree, subnet);
102
103         subnet_cache_flush();
104 }
105
106 /* Subnet lookup routines */
107
108 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
109         return splay_search(owner->subnet_tree, subnet);
110 }
111
112 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
113         subnet_t *r = NULL;
114
115         // Check if this address is cached
116
117         if((r = hash_search(mac_cache, address)))
118                 return r;
119
120         // Search all subnets for a matching one
121
122         for splay_each(subnet_t, p, owner ? owner->subnet_tree : subnet_tree) {
123                 if(!p || p->type != SUBNET_MAC)
124                         continue;
125
126                 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
127                         r = p;
128                         if(p->owner->status.reachable)
129                                 break;
130                 }
131         }
132
133         // Cache the result
134
135         if(r)
136                 hash_insert(mac_cache, address, r);
137
138         return r;
139 }
140
141 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
142         subnet_t *r = NULL;
143
144         // Check if this address is cached
145
146         if((r = hash_search(ipv4_cache, address)))
147                 return r;
148
149         // Search all subnets for a matching one
150
151         for splay_each(subnet_t, p, subnet_tree) {
152                 if(!p || p->type != SUBNET_IPV4)
153                         continue;
154
155                 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
156                         r = p;
157                         if(p->owner->status.reachable)
158                                 break;
159                 }
160         }
161
162         // Cache the result
163
164         if(r)
165                 hash_insert(ipv4_cache, address, r);
166
167         return r;
168 }
169
170 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
171         subnet_t *r = NULL;
172
173         // Check if this address is cached
174
175         if((r = hash_search(ipv6_cache, address)))
176                 return r;
177
178         // Search all subnets for a matching one
179
180         for splay_each(subnet_t, p, subnet_tree) {
181                 if(!p || p->type != SUBNET_IPV6)
182                         continue;
183
184                 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
185                         r = p;
186                         if(p->owner->status.reachable)
187                                 break;
188                 }
189         }
190
191         // Cache the result
192
193         if(r)
194                 hash_insert(ipv6_cache, address, r);
195
196         return r;
197 }
198
199 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
200         char netstr[MAXNETSTR];
201         char *name, *address, *port;
202         char empty[] = "";
203
204         // Prepare environment variables to be passed to the script
205
206         char *envp[10] = {NULL};
207         xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
208         xasprintf(&envp[3], "NODE=%s", owner->name);
209
210         if(owner != myself) {
211                 sockaddr2str(&owner->address, &address, &port);
212                 // 4 and 5 are reserved for SUBNET and WEIGHT
213                 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
214                 xasprintf(&envp[7], "REMOTEPORT=%s", port);
215                 free(port);
216                 free(address);
217         }
218
219         xasprintf(&envp[8], "NAME=%s", myself->name);
220
221         name = up ? "subnet-up" : "subnet-down";
222
223         if(!subnet) {
224                 for splay_each(subnet_t, subnet, owner->subnet_tree) {
225                         if(!net2str(netstr, sizeof netstr, subnet))
226                                 continue;
227
228                         // Strip the weight from the subnet, and put it in its own environment variable
229                         char *weight = strchr(netstr, '#');
230                         if(weight)
231                                 *weight++ = 0;
232                         else
233                                 weight = empty;
234
235                         // Prepare the SUBNET and WEIGHT variables
236                         if(envp[4])
237                                 free(envp[4]);
238                         if(envp[5])
239                                 free(envp[5]);
240                         xasprintf(&envp[4], "SUBNET=%s", netstr);
241                         xasprintf(&envp[5], "WEIGHT=%s", weight);
242
243                         execute_script(name, envp);
244                 }
245         } else {
246                 if(net2str(netstr, sizeof netstr, subnet)) {
247                         // Strip the weight from the subnet, and put it in its own environment variable
248                         char *weight = strchr(netstr, '#');
249                         if(weight)
250                                 *weight++ = 0;
251                         else
252                                 weight = empty;
253
254                         // Prepare the SUBNET and WEIGHT variables
255                         xasprintf(&envp[4], "SUBNET=%s", netstr);
256                         xasprintf(&envp[5], "WEIGHT=%s", weight);
257
258                         execute_script(name, envp);
259                 }
260         }
261
262         for(int i = 0; envp[i] && i < 9; i++)
263                 free(envp[i]);
264 }
265
266 bool dump_subnets(connection_t *c) {
267         for splay_each(subnet_t, subnet, subnet_tree) {
268                 char netstr[MAXNETSTR];
269
270                 if(!net2str(netstr, sizeof netstr, subnet))
271                         continue;
272
273                 send_request(c, "%d %d %s %s",
274                                 CONTROL, REQ_DUMP_SUBNETS,
275                                 netstr, subnet->owner->name);
276         }
277
278         return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
279 }