From c46f56a8b8bb865dd8951441b5acf4701b5b5b09 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 1 Dec 2004 20:06:05 +0000 Subject: [PATCH] subnet-up/down hooks --- src/net_setup.c | 8 +++++++- src/protocol_subnet.c | 6 ++++++ src/subnet.c | 48 +++++++++++++++++++++++++++++++++++++++++++ src/subnet.h | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/net_setup.c b/src/net_setup.c index 0c9f27c3..3751537e 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -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); diff --git a/src/protocol_subnet.c b/src/protocol_subnet.c index 4a761b7b..efe375a6 100644 --- a/src/protocol_subnet.c +++ b/src/protocol_subnet.c @@ -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; diff --git a/src/subnet.c b/src/subnet.c index aca1912b..984cc7f8 100644 --- a/src/subnet.c +++ b/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]; diff --git a/src/subnet.h b/src/subnet.h index 2d40dc3a..824d582b 100644 --- a/src/subnet.h +++ b/src/subnet.h @@ -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 *); -- 2.39.5