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 void init_subnet_tree(splay_tree_t *tree) {
70 memset(tree, 0, sizeof(*tree));
71 tree->compare = (splay_compare_t) subnet_compare;
74 /* Allocating and freeing space for subnets */
76 subnet_t *new_subnet(void) {
77 return xzalloc(sizeof(subnet_t));
80 void free_subnet(subnet_t *subnet) {
84 /* Adding and removing subnets */
86 void subnet_add(node_t *n, subnet_t *subnet) {
89 splay_insert(&subnet_tree, subnet);
92 splay_insert(&n->subnet_tree, subnet);
98 void subnet_del(node_t *n, subnet_t *subnet) {
100 splay_delete(&n->subnet_tree, subnet);
103 splay_delete(&subnet_tree, subnet);
105 subnet_cache_flush();
108 /* Subnet lookup routines */
110 subnet_t *lookup_subnet(node_t *owner, const subnet_t *subnet) {
111 return splay_search(&owner->subnet_tree, subnet);
114 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
117 // Check if this address is cached
119 if((r = hash_search(mac_cache, address))) {
123 // Search all subnets for a matching one
125 for splay_each(subnet_t, p, owner ? &owner->subnet_tree : &subnet_tree) {
126 if(!p || p->type != SUBNET_MAC) {
130 if(!memcmp(address, &p->net.mac.address, sizeof(*address))) {
133 if(!p->owner || p->owner->status.reachable) {
142 hash_insert(mac_cache, address, r);
148 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
151 // Check if this address is cached
153 if((r = hash_search(ipv4_cache, address))) {
157 // Search all subnets for a matching one
159 for splay_each(subnet_t, p, &subnet_tree) {
160 if(!p || p->type != SUBNET_IPV4) {
164 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
167 if(!p->owner || p->owner->status.reachable) {
176 hash_insert(ipv4_cache, address, r);
182 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
185 // Check if this address is cached
187 if((r = hash_search(ipv6_cache, address))) {
191 // Search all subnets for a matching one
193 for splay_each(subnet_t, p, &subnet_tree) {
194 if(!p || p->type != SUBNET_IPV6) {
198 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
201 if(!p->owner || p->owner->status.reachable) {
210 hash_insert(ipv6_cache, address, r);
216 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
217 char netstr[MAXNETSTR];
218 char *name, *address, *port;
221 // Prepare environment variables to be passed to the script
224 environment_init(&env);
225 environment_add(&env, "NODE=%s", owner->name);
227 if(owner != myself) {
228 sockaddr2str(&owner->address, &address, &port);
229 environment_add(&env, "REMOTEADDRESS=%s", address);
230 environment_add(&env, "REMOTEPORT=%s", port);
235 int env_subnet = environment_add(&env, NULL);
236 int env_weight = environment_add(&env, NULL);
238 name = up ? "subnet-up" : "subnet-down";
241 for splay_each(subnet_t, subnet, &owner->subnet_tree) {
242 if(!net2str(netstr, sizeof(netstr), subnet)) {
246 // Strip the weight from the subnet, and put it in its own environment variable
247 char *weight = strchr(netstr, '#');
255 // Prepare the SUBNET and WEIGHT variables
256 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
257 environment_update(&env, env_weight, "WEIGHT=%s", weight);
259 execute_script(name, &env);
262 if(net2str(netstr, sizeof(netstr), subnet)) {
263 // Strip the weight from the subnet, and put it in its own environment variable
264 char *weight = strchr(netstr, '#');
272 // Prepare the SUBNET and WEIGHT variables
273 environment_update(&env, env_subnet, "SUBNET=%s", netstr);
274 environment_update(&env, env_weight, "WEIGHT=%s", weight);
276 execute_script(name, &env);
280 environment_exit(&env);
283 bool dump_subnets(connection_t *c) {
284 for splay_each(subnet_t, subnet, &subnet_tree) {
285 char netstr[MAXNETSTR];
287 if(!net2str(netstr, sizeof(netstr), subnet)) {
291 send_request(c, "%d %d %s %s",
292 CONTROL, REQ_DUMP_SUBNETS,
293 netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
296 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);