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