X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fsubnet.c;h=3652cd3a8946afa341f5cb750badeb370ca86d1a;hp=aca1912b5be32b0a882af2fbf31b4d0c16275dbd;hb=92c4a28d7d43b68a324cf2eca741298ed6b692d6;hpb=396ac4be802f8b75c5a2ab5925925427c61c1da3 diff --git a/src/subnet.c b/src/subnet.c index aca1912b..3652cd3a 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" @@ -245,6 +247,11 @@ bool net2str(char *netstr, int len, const subnet_t *subnet) { cp(); + if(!netstr || !subnet) { + logger(LOG_ERR, _("net2str() was called with netstr=%p, subnet=%p!\n"), netstr, subnet); + return false; + } + switch (subnet->type) { case SUBNET_MAC: snprintf(netstr, len, "%hx:%hx:%hx:%hx:%hx:%hx", @@ -385,6 +392,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); + } + + for(i = 0; i < (owner != myself ? 6 : 4); i++) + free(envp[i]); + + if(owner != myself) { + free(address); + free(port); + } +} + void dump_subnets(void) { char netstr[MAXNETSTR];