]> git.meshlink.io Git - meshlink/commitdiff
subnet-up/down hooks
authorGuus Sliepen <guus@tinc-vpn.org>
Wed, 1 Dec 2004 20:06:05 +0000 (20:06 +0000)
committerGuus Sliepen <guus@tinc-vpn.org>
Wed, 1 Dec 2004 20:06:05 +0000 (20:06 +0000)
src/net_setup.c
src/protocol_subnet.c
src/subnet.c
src/subnet.h

index 0c9f27c388821edb00d3aeac63a228199f2e6bfa..3751537e7f1c6febeed73675e584362fa067cc20 100644 (file)
@@ -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);
index 4a761b7b2c439bdeba325477eb361753b3496c7c..efe375a6cfc1c8214a209d4de608a66d4f43cc49 100644 (file)
@@ -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;
index aca1912b5be32b0a882af2fbf31b4d0c16275dbd..984cc7f8ece644ff2f2ec80cd6054366efe3fba1 100644 (file)
 #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];
index 2d40dc3af6bf2897afbd6d2f773757377599adee..824d582b73794280fb31cae7bb6d16f59bf92ee5 100644 (file)
@@ -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 *);