2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000-2013 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"
37 /* lists type of subnet */
39 splay_tree_t *subnet_tree;
41 /* Subnet lookup 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 subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet);
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));
63 void exit_subnets(void) {
64 splay_delete_tree(subnet_tree);
66 hash_free(ipv4_cache);
67 hash_free(ipv6_cache);
71 splay_tree_t *new_subnet_tree(void) {
72 return splay_alloc_tree((splay_compare_t) subnet_compare, NULL);
75 void free_subnet_tree(splay_tree_t *subnet_tree) {
76 splay_delete_tree(subnet_tree);
79 /* Allocating and freeing space for subnets */
81 subnet_t *new_subnet(void) {
82 return xzalloc(sizeof(subnet_t));
85 void free_subnet(subnet_t *subnet) {
89 /* Adding and removing subnets */
91 void subnet_add(node_t *n, subnet_t *subnet) {
94 splay_insert(subnet_tree, subnet);
96 splay_insert(n->subnet_tree, subnet);
101 void subnet_del(node_t *n, subnet_t *subnet) {
103 splay_delete(n->subnet_tree, subnet);
104 splay_delete(subnet_tree, subnet);
106 subnet_cache_flush();
109 /* Subnet lookup routines */
111 subnet_t *lookup_subnet(const node_t *owner, const subnet_t *subnet) {
112 return splay_search(owner->subnet_tree, subnet);
115 subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) {
118 // Check if this address is cached
120 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)
129 if(!memcmp(address, &p->net.mac.address, sizeof *address)) {
131 if(!p->owner || p->owner->status.reachable)
139 hash_insert(mac_cache, address, r);
144 subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
147 // Check if this address is cached
149 if((r = hash_search(ipv4_cache, address)))
152 // Search all subnets for a matching one
154 for splay_each(subnet_t, p, subnet_tree) {
155 if(!p || p->type != SUBNET_IPV4)
158 if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
160 if(!p->owner || p->owner->status.reachable)
168 hash_insert(ipv4_cache, address, r);
173 subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
176 // Check if this address is cached
178 if((r = hash_search(ipv6_cache, address)))
181 // Search all subnets for a matching one
183 for splay_each(subnet_t, p, subnet_tree) {
184 if(!p || p->type != SUBNET_IPV6)
187 if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
189 if(!p->owner || p->owner->status.reachable)
197 hash_insert(ipv6_cache, address, r);
202 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
203 char netstr[MAXNETSTR];
204 char *name, *address, *port;
207 // Prepare environment variables to be passed to the script
209 char *envp[10] = {NULL};
210 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
211 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
212 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
213 xasprintf(&envp[3], "NODE=%s", owner->name);
215 if(owner != myself) {
216 sockaddr2str(&owner->address, &address, &port);
217 // 4 and 5 are reserved for SUBNET and WEIGHT
218 xasprintf(&envp[6], "REMOTEADDRESS=%s", address);
219 xasprintf(&envp[7], "REMOTEPORT=%s", port);
224 xasprintf(&envp[8], "NAME=%s", myself->name);
226 name = up ? "subnet-up" : "subnet-down";
229 for splay_each(subnet_t, subnet, owner->subnet_tree) {
230 if(!net2str(netstr, sizeof netstr, subnet))
233 // Strip the weight from the subnet, and put it in its own environment variable
234 char *weight = strchr(netstr, '#');
240 // Prepare the SUBNET and WEIGHT variables
245 xasprintf(&envp[4], "SUBNET=%s", netstr);
246 xasprintf(&envp[5], "WEIGHT=%s", weight);
248 execute_script(name, envp);
251 if(net2str(netstr, sizeof netstr, subnet)) {
252 // Strip the weight from the subnet, and put it in its own environment variable
253 char *weight = strchr(netstr, '#');
259 // Prepare the SUBNET and WEIGHT variables
260 xasprintf(&envp[4], "SUBNET=%s", netstr);
261 xasprintf(&envp[5], "WEIGHT=%s", weight);
263 execute_script(name, envp);
267 for(int i = 0; envp[i] && i < 9; i++)
271 bool dump_subnets(connection_t *c) {
272 for splay_each(subnet_t, subnet, subnet_tree) {
273 char netstr[MAXNETSTR];
275 if(!net2str(netstr, sizeof netstr, subnet))
278 send_request(c, "%d %d %s %s",
279 CONTROL, REQ_DUMP_SUBNETS,
280 netstr, subnet->owner ? subnet->owner->name : "(broadcast)");
283 return send_request(c, "%d %d", CONTROL, REQ_DUMP_SUBNETS);