From 2eba7933053d7d21bf82e647978ee90abe98dc3a Mon Sep 17 00:00:00 2001
From: Guus Sliepen <guus@tinc-vpn.org>
Date: Fri, 5 Jul 2013 21:36:51 +0200
Subject: [PATCH] Set $NAME when calling host-up/down and subnet-up/down
 scripts.

---
 src/graph.c     | 6 +++---
 src/net_setup.c | 6 ++----
 src/subnet.c    | 6 ++++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/graph.c b/src/graph.c
index 302056a3..b4c01bb5 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -235,7 +235,7 @@ static void check_reachability(void) {
 			char *name;
 			char *address;
 			char *port;
-			char *envp[7];
+			char *envp[8] = {NULL};
 
 			xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
 			xasprintf(&envp[1], "DEVICE=%s", device ? : "");
@@ -244,7 +244,7 @@ static void check_reachability(void) {
 			sockaddr2str(&n->address, &address, &port);
 			xasprintf(&envp[4], "REMOTEADDRESS=%s", address);
 			xasprintf(&envp[5], "REMOTEPORT=%s", port);
-			envp[6] = NULL;
+			xasprintf(&envp[6], "NAME=%s", myself->name);
 
 			execute_script(n->status.reachable ? "host-up" : "host-down", envp);
 
@@ -255,7 +255,7 @@ static void check_reachability(void) {
 			free(address);
 			free(port);
 
-			for(int i = 0; i < 6; i++)
+			for(int i = 0; i < 7; i++)
 				free(envp[i]);
 
 			subnet_update(n, NULL, n->status.reachable);
diff --git a/src/net_setup.c b/src/net_setup.c
index ee8296cd..8ae1e72b 100644
--- a/src/net_setup.c
+++ b/src/net_setup.c
@@ -828,12 +828,11 @@ static bool setup_myself(void) {
 		io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
 
 	/* Run tinc-up script to further initialize the tap interface */
-	char *envp[5];
+	char *envp[5] = {NULL};
 	xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
 	xasprintf(&envp[1], "DEVICE=%s", device ? : "");
 	xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
 	xasprintf(&envp[3], "NAME=%s", myself->name);
-	envp[4] = NULL;
 
 	execute_script("tinc-up", envp);
 
@@ -1065,12 +1064,11 @@ void close_network_connections(void) {
 	close(unix_socket.fd);
 #endif
 
-	char *envp[5];
+	char *envp[5] = {NULL};
 	xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
 	xasprintf(&envp[1], "DEVICE=%s", device ? : "");
 	xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
 	xasprintf(&envp[3], "NAME=%s", myself->name);
-	envp[4] = NULL;
 
 	exit_requests();
 	exit_edges();
diff --git a/src/subnet.c b/src/subnet.c
index bf4300e4..3b980303 100644
--- a/src/subnet.c
+++ b/src/subnet.c
@@ -204,7 +204,7 @@ void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
 
 	// Prepare environment variables to be passed to the script
 
-	char *envp[9] = {NULL};
+	char *envp[10] = {NULL};
 	xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
 	xasprintf(&envp[1], "DEVICE=%s", device ? : "");
 	xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
@@ -219,6 +219,8 @@ void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
 		free(address);
 	}
 
+	xasprintf(&envp[8], "NAME=%s", myself->name);
+
 	name = up ? "subnet-up" : "subnet-down";
 
 	if(!subnet) {
@@ -260,7 +262,7 @@ void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
 		}
 	}
 
-	for(int i = 0; envp[i] && i < 8; i++)
+	for(int i = 0; envp[i] && i < 9; i++)
 		free(envp[i]);
 }