Nexthop calculation should always use the shortest path.
[tinc] / src / graph.c
index 586aee8..9592f98 100644 (file)
@@ -1,6 +1,6 @@
 /*
     graph.c -- graph algorithms
-    Copyright (C) 2001-2012 Guus Sliepen <guus@tinc-vpn.org>,
+    Copyright (C) 2001-2013 Guus Sliepen <guus@tinc-vpn.org>,
                   2001-2005 Ivo Timmermans
 
     This program is free software; you can redistribute it and/or modify
@@ -45,7 +45,7 @@
 #include "system.h"
 
 #include "avl_tree.h"
-#include "config.h"
+#include "conf.h"
 #include "connection.h"
 #include "device.h"
 #include "edge.h"
@@ -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);
@@ -212,9 +212,13 @@ static void sssp_bfs(void) {
                           && (!e->to->status.indirect || indirect))
                                continue;
 
+                       // Only update nexthop the first time we visit this node.
+
+                       if(!e->to->status.visited)
+                               e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
+
                        e->to->status.visited = true;
                        e->to->status.indirect = indirect;
-                       e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
                        e->to->prevedge = e;
                        e->to->via = indirect ? n->via : e->to;
                        e->to->options = e->options;
@@ -269,7 +273,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 +286,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);
@@ -318,7 +322,7 @@ void dump_graph(void) {
        node_t *n;
        edge_t *e;
        char *filename = NULL, *tmpname = NULL;
-       FILE *file, *pipe;
+       FILE *file, *pipe = NULL;
        
        if(!graph_changed || !get_config_string(lookup_config(config_tree, "GraphDumpFile"), &filename))
                return;
@@ -356,7 +360,7 @@ void dump_graph(void) {
 
        fprintf(file, "}\n");   
        
-       if(filename[0] == '|') {
+       if(pipe) {
                pclose(pipe);
        } else {
                fclose(file);