Allow dumping either directed or undirected graphs.

Internally, tinc maintains a directed graph of the meta connections between
nodes. However, this causes graphviz to draw two lines between nodes, which is
not always desirable. The "dump graph" command now defaults to dumping an
undirected graph, the "dump digraph" command will dump a directed graph.
This commit is contained in:
Guus Sliepen 2012-09-26 23:52:36 +02:00
parent d6388d782e
commit 1f312137d5

View file

@ -138,7 +138,7 @@ static void usage(bool status) {
" edges - all known connections in the VPN\n" " edges - all known connections in the VPN\n"
" subnets - all known subnets in the VPN\n" " subnets - all known subnets in the VPN\n"
" connections - all meta connections with ourself\n" " connections - all meta connections with ourself\n"
" graph - graph of the VPN in dotty format\n" " [di]graph - graph of the VPN in dotty format\n"
" info NODE|SUBNET|ADDRESS Give information about a particular NODE, SUBNET or ADDRESS.\n" " info NODE|SUBNET|ADDRESS Give information about a particular NODE, SUBNET or ADDRESS.\n"
" purge Purge unreachable nodes\n" " purge Purge unreachable nodes\n"
" debug N Set debug level\n" " debug N Set debug level\n"
@ -822,7 +822,7 @@ static int cmd_dump(int argc, char *argv[]) {
if(!connect_tincd(true)) if(!connect_tincd(true))
return 1; return 1;
bool do_graph = false; int do_graph = 0;
if(!strcasecmp(argv[1], "nodes")) if(!strcasecmp(argv[1], "nodes"))
sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES); sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
@ -835,14 +835,20 @@ static int cmd_dump(int argc, char *argv[]) {
else if(!strcasecmp(argv[1], "graph")) { else if(!strcasecmp(argv[1], "graph")) {
sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES); sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES); sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
do_graph = true; do_graph = 1;
} else if(!strcasecmp(argv[1], "digraph")) {
sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
do_graph = 2;
} else { } else {
fprintf(stderr, "Unknown dump type '%s'.\n", argv[1]); fprintf(stderr, "Unknown dump type '%s'.\n", argv[1]);
usage(true); usage(true);
return 1; return 1;
} }
if(do_graph) if(do_graph == 1)
printf("graph {\n");
else if(do_graph == 2)
printf("digraph {\n"); printf("digraph {\n");
while(recvline(fd, line, sizeof line)) { while(recvline(fd, line, sizeof line)) {
@ -919,10 +925,14 @@ static int cmd_dump(int argc, char *argv[]) {
} else { } else {
if(req == REQ_DUMP_NODES) if(req == REQ_DUMP_NODES)
printf(" %s [label = \"%s\"];\n", node1, node1); printf(" %s [label = \"%s\"];\n", node1, node1);
else else {
if(do_graph == 1 && strcmp(node1, node2) > 0)
printf(" %s -- %s;\n", node1, node2);
else if(do_graph == 2)
printf(" %s -> %s;\n", node1, node2); printf(" %s -> %s;\n", node1, node2);
} }
} }
}
fprintf(stderr, "Error receiving dump.\n"); fprintf(stderr, "Error receiving dump.\n");
return 1; return 1;