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

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

diff --git a/src/graph.c b/src/graph.c
index 203942db..a15e5c70 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -156,7 +156,7 @@ static void sssp_bfs(void) {
 	bool indirect;
 	char *name;
 	char *address, *port;
-	char *envp[7];
+	char *envp[8] = {NULL};
 	int i;
 
 	todo_list = list_alloc(NULL);
@@ -269,7 +269,7 @@ static void sssp_bfs(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);
 
@@ -282,7 +282,7 @@ static void sssp_bfs(void) {
 			free(address);
 			free(port);
 
-			for(i = 0; i < 6; i++)
+			for(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 5a7c6065..623ac383 100644
--- a/src/net_setup.c
+++ b/src/net_setup.c
@@ -334,7 +334,7 @@ static bool setup_myself(void) {
 	char *address = NULL;
 	char *proxy = NULL;
 	char *space;
-	char *envp[5];
+	char *envp[5] = {NULL};
 	struct addrinfo *ai, *aip, hint = {0};
 	bool choice;
 	int i, err;
@@ -692,11 +692,10 @@ static bool setup_myself(void) {
 	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);
 
-	for(i = 0; i < 5; i++)
+	for(i = 0; i < 4; i++)
 		free(envp[i]);
 
 	/* Run subnet-up scripts for our own subnets */
@@ -862,7 +861,7 @@ bool setup_network(void) {
 void close_network_connections(void) {
 	avl_node_t *node, *next;
 	connection_t *c;
-	char *envp[5];
+	char *envp[5] = {NULL};
 	int i;
 
 	for(node = connection_tree->head; node; node = next) {
@@ -896,7 +895,6 @@ void close_network_connections(void) {
 	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 d7b9f470..7b673529 100644
--- a/src/subnet.c
+++ b/src/subnet.c
@@ -523,7 +523,7 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
 void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
 	avl_node_t *node;
 	int i;
-	char *envp[9] = {NULL};
+	char *envp[10] = {NULL};
 	char netstr[MAXNETSTR];
 	char *name, *address, *port;
 	char empty[] = "";
@@ -544,6 +544,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) {
-- 
2.39.5