subnet-up/down hooks
This commit is contained in:
		
							parent
							
								
									f08baa3072
								
							
						
					
					
						commit
						c46f56a8b8
					
				
					 4 changed files with 62 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -462,6 +462,10 @@ bool setup_myself(void)
 | 
			
		|||
	for(i = 0; i < 5; i++)
 | 
			
		||||
		free(envp[i]);
 | 
			
		||||
 | 
			
		||||
	/* Run subnet-up scripts for our own subnets */
 | 
			
		||||
 | 
			
		||||
	subnet_update(myself, NULL, true);
 | 
			
		||||
 | 
			
		||||
	/* Open sockets */
 | 
			
		||||
 | 
			
		||||
	get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
 | 
			
		||||
| 
						 | 
				
			
			@ -568,8 +572,10 @@ void close_network_connections(void)
 | 
			
		|||
		terminate_connection(c, false);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if(myself && myself->connection)
 | 
			
		||||
	if(myself && myself->connection) {
 | 
			
		||||
		subnet_update(myself, NULL, false);
 | 
			
		||||
		terminate_connection(myself->connection, false);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for(i = 0; i < listen_sockets; i++) {
 | 
			
		||||
		close(listen_socket[i].tcp);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -134,6 +134,9 @@ bool add_subnet_h(connection_t *c)
 | 
			
		|||
	*(new = new_subnet()) = s;
 | 
			
		||||
	subnet_add(owner, new);
 | 
			
		||||
 | 
			
		||||
	if(owner->status.reachable)
 | 
			
		||||
		subnet_update(owner, new, true);
 | 
			
		||||
 | 
			
		||||
	/* Tell the rest */
 | 
			
		||||
 | 
			
		||||
	if(!tunnelserver)
 | 
			
		||||
| 
						 | 
				
			
			@ -229,6 +232,9 @@ bool del_subnet_h(connection_t *c)
 | 
			
		|||
 | 
			
		||||
	/* Finally, delete it. */
 | 
			
		||||
 | 
			
		||||
	if(owner->status.reachable)
 | 
			
		||||
		subnet_update(owner, find, false);
 | 
			
		||||
 | 
			
		||||
	subnet_del(owner, find);
 | 
			
		||||
 | 
			
		||||
	return true;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										48
									
								
								src/subnet.c
									
										
									
									
									
								
							
							
						
						
									
										48
									
								
								src/subnet.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -23,10 +23,12 @@
 | 
			
		|||
#include "system.h"
 | 
			
		||||
 | 
			
		||||
#include "avl_tree.h"
 | 
			
		||||
#include "device.h"
 | 
			
		||||
#include "logger.h"
 | 
			
		||||
#include "net.h"
 | 
			
		||||
#include "netutl.h"
 | 
			
		||||
#include "node.h"
 | 
			
		||||
#include "process.h"
 | 
			
		||||
#include "subnet.h"
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
#include "xalloc.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -385,6 +387,52 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address)
 | 
			
		|||
	return p;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
 | 
			
		||||
	avl_node_t *node;
 | 
			
		||||
	int i;
 | 
			
		||||
	char *envp[8];
 | 
			
		||||
	char netstr[MAXNETSTR + 7] = "SUBNET=";
 | 
			
		||||
	char *name, *address, *port;
 | 
			
		||||
 | 
			
		||||
	asprintf(&envp[0], "NETNAME=%s", netname ? : "");
 | 
			
		||||
	asprintf(&envp[1], "DEVICE=%s", device ? : "");
 | 
			
		||||
	asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
 | 
			
		||||
	asprintf(&envp[3], "NODE=%s", owner->name);
 | 
			
		||||
	if(owner != myself) {
 | 
			
		||||
		sockaddr2str(&owner->address, &address, &port);
 | 
			
		||||
		asprintf(&envp[4], "REMOTEADDRESS=%s", address);
 | 
			
		||||
		asprintf(&envp[5], "REMOTEPORT=%s", port);
 | 
			
		||||
		envp[6] = netstr;
 | 
			
		||||
		envp[7] = NULL;
 | 
			
		||||
	} else {
 | 
			
		||||
		envp[4] = netstr;
 | 
			
		||||
		envp[5] = NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	name = up ? "subnet-up" : "subnet-down";
 | 
			
		||||
 | 
			
		||||
	if(!subnet) {
 | 
			
		||||
		for(node = owner->subnet_tree->head; node; node = node->next) {
 | 
			
		||||
			subnet = node->data;
 | 
			
		||||
			if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
 | 
			
		||||
				continue;
 | 
			
		||||
			execute_script(name, envp);
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		if(net2str(netstr + 7, sizeof netstr - 7, subnet))
 | 
			
		||||
			execute_script(name, envp);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	net2str(netstr, sizeof netstr, subnet);
 | 
			
		||||
	envp[6] = envp[7] = NULL;
 | 
			
		||||
	
 | 
			
		||||
	for(i = 0; i < (owner != myself ? 6 : 4); i++)
 | 
			
		||||
		free(envp[i]);
 | 
			
		||||
 | 
			
		||||
	free(address);
 | 
			
		||||
	free(port);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void dump_subnets(void)
 | 
			
		||||
{
 | 
			
		||||
	char netstr[MAXNETSTR];
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -74,6 +74,7 @@ extern avl_tree_t *new_subnet_tree(void) __attribute__ ((__malloc__));
 | 
			
		|||
extern void free_subnet_tree(avl_tree_t *);
 | 
			
		||||
extern void subnet_add(struct node_t *, subnet_t *);
 | 
			
		||||
extern void subnet_del(struct node_t *, subnet_t *);
 | 
			
		||||
extern void subnet_update(struct node_t *, subnet_t *, bool);
 | 
			
		||||
extern bool net2str(char *, int, const subnet_t *);
 | 
			
		||||
extern bool str2net(subnet_t *, const char *);
 | 
			
		||||
extern subnet_t *lookup_subnet(const struct node_t *, const subnet_t *);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue