2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2017 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.
23 #include "splay_tree.h"
24 #include "control_common.h"
34 /* lists type of subnet */
36 splay_tree_t subnet_tree = {
37 .compare = (splay_compare_t) subnet_compare,
38 .delete = (splay_action_t) free_subnet,
41 /* Subnet lookup cache */
43 static hash_t *ipv4_cache;
44 static hash_t *ipv6_cache;
45 static hash_t *mac_cache;
47 void subnet_cache_flush(void) {
48 hash_clear(ipv4_cache);
49 hash_clear(ipv6_cache);
50 hash_clear(mac_cache);
53 /* Initialising trees */
55 void init_subnets(void) {
56 ipv4_cache = hash_alloc(0x100, sizeof(ipv4_t));
57 ipv6_cache = hash_alloc(0x100, sizeof(ipv6_t));
58 mac_cache = hash_alloc(0x100, sizeof(mac_t));
61 void exit_subnets(void) {
62 splay_empty_tree(&subnet_tree);
64 hash_free(ipv4_cache);
65 hash_free(ipv6_cache);
69 splay_tree_t *new_subnet_tree(void) {
70 return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
73 void free_subnet_tree(splay_tree_t *subnet_tree) {
74 splay_delete_tree(subnet_tree);
77 /* Allocating and freeing space for subnets */
79 subnet_t *new_subnet(void) {
80 return xzalloc(sizeof(subnet_t));
83 void free_subnet(subnet_t *subnet) {
87 /* Adding and removing subnets */
89 void subnet_add(node_t *n, subnet_t *subnet) {
92 splay_insert(&subnet_tree, subnet);
95 splay_insert(n->subnet_tree, subnet);
101 void subnet_del(node_t *n, subnet_t *subnet) {
103 splay_delete(n->subnet_tree, subnet);
106 splay_delete(&subnet_tree, subnet);
108 subnet_cache_flush();
111 /* Subnet lookup routines */
113 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
114 return splay_search(owner->subnet_tree, subnet);
117 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
120 // Check if this address is cached
122 if((r = hash_search(mac_cache, address))) {
126 // Search all subnets for a matching one
128 for splay_each(subnet_t, p, owner ? owner->subnet_tree : &subnet_tree) {
129 if(!p || p->type != SUBNET_MAC) {
133 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
136 if(!p->owner || p->owner->status.reachable) {
145 hash_insert(mac_cache, address, r);
151 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
154 // Check if this address is cached
156 if((r = hash_search(ipv4_cache, address))) {
160 // Search all subnets for a matching one
162 for splay_each(subnet_t, p, &subnet_tree) {
163 if(!p || p->type != SUBNET_IPV4) {
167 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
170 if(!p->owner || p->owner->status.reachable) {
179 hash_insert(ipv4_cache, address, r);
185 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
188 // Check if this address is cached
190 if((r = hash_search(ipv6_cache, address))) {
194 // Search all subnets for a matching one
196 for splay_each(subnet_t, p, &subnet_tree) {
197 if(!p || p->type != SUBNET_IPV6) {
201 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
204 if(!p->owner || p->owner->status.reachable) {
213 hash_insert(ipv6_cache, address, r);
219 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
220 char netstr[MAXNETSTR];
221 char *name, *address, *port;
224 // Prepare environment variables to be passed to the script
227 environment_init(&env);
228 environment_add(&env, "NODE=%s", owner->name);
230 if(owner != myself) {
231 sockaddr2str(&owner->address, &address, &port);
232 environment_add(&env, "REMOTEADDRESS=%s", address);
233 environment_add(&env, "REMOTEPORT=%s", port);
238 int env_subnet = environment_add(&env, NULL);
239 int env_weight = environment_add(&env, NULL);
241 name = up ? "subnet-up" : "subnet-down";
244 for splay_each(subnet_t, subnet, owner->subnet_tree) {
245 if(!net2str(netstr, sizeof(netstr), subnet)) {
249 // Strip the weight from the subnet, and put it in its own environment variable
250 char *weight = strchr(netstr, '#');
258 // Prepare the SUBNET and WEIGHT variables
259 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
260 environment_update(&env, env_weight, "WEIGHT=%s", weight);
262 execute_script(name, &env);
265 if(net2str(netstr, sizeof(netstr), subnet)) {
266 // Strip the weight from the subnet, and put it in its own environment variable
267 char *weight = strchr(netstr, '#');
275 // Prepare the SUBNET and WEIGHT variables
276 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
277 environment_update(&env, env_weight, "WEIGHT=%s", weight);
279 execute_script(name, &env);
283 environment_exit(&env);
286 bool dump_subnets(connection_t *c) {
287 for splay_each(subnet_t, subnet, &subnet_tree) {
288 char netstr[MAXNETSTR];
290 if(!net2str(netstr, sizeof(netstr), subnet)) {
294 send_request(c, "%d %d %s %s",
295 CONTROL, REQ_DUMP_SUBNETS,
296 netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
299 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);