From: Guus Sliepen Date: Wed, 26 Sep 2012 21:18:32 +0000 (+0200) Subject: Let tincctl parse and format dumps. X-Git-Tag: release-1.1pre3~46 X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=d6388d782ede1bbe49a5c2643362e2e0f383fa89;p=tinc Let tincctl parse and format dumps. At the moment it just reproduces the old format. --- diff --git a/src/connection.c b/src/connection.c index 29ad04ee..fafec5db 100644 --- a/src/connection.c +++ b/src/connection.c @@ -117,7 +117,7 @@ bool dump_connections(connection_t *cdump) { for(node = connection_tree->head; node; node = node->next) { c = node->data; - send_request(cdump, "%d %d %s at %s options %x socket %d status %04x", + send_request(cdump, "%d %d %s %s %x %d %x", CONTROL, REQ_DUMP_CONNECTIONS, c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status)); diff --git a/src/edge.c b/src/edge.c index f5aa0994..cee75a7a 100644 --- a/src/edge.c +++ b/src/edge.c @@ -117,7 +117,7 @@ bool dump_edges(connection_t *c) { for(node2 = n->edge_tree->head; node2; node2 = node2->next) { e = node2->data; address = sockaddr2hostname(&e->address); - send_request(c, "%d %d %s to %s at %s options %x weight %d", + send_request(c, "%d %d %s %s %s %x %d", CONTROL, REQ_DUMP_EDGES, e->from->name, e->to->name, address, e->options, e->weight); diff --git a/src/info.c b/src/info.c index ee60a3fe..93be24e4 100644 --- a/src/info.c +++ b/src/info.c @@ -62,19 +62,14 @@ static int info_node(int fd, const char *item) { long int last_state_change; while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s at %s port %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd) %ld", &code, &req, node, host, port, &cipher, &digest, &maclength, &compression, &options, (unsigned *)&status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); + int n = sscanf(line, "%d %d %s %s port %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", &code, &req, node, host, port, &cipher, &digest, &maclength, &compression, &options, (unsigned *)&status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); if(n == 2) break; if(n != 18) { - *port = 0; - n = sscanf(line, "%d %d %s at %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd) %ld", &code, &req, node, host, &cipher, &digest, &maclength, &compression, &options, (unsigned *)&status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); - - if(n != 17) { - fprintf(stderr, "Unable to parse node dump from tincd.\n"); - return 1; - } + fprintf(stderr, "Unable to parse node dump from tincd.\n"); + return 1; } if(!strcmp(node, item)) { @@ -94,8 +89,7 @@ static int info_node(int fd, const char *item) { } printf("Node: %s\n", item); - if(*port) - printf("Address: %s port %s\n", host, port); + printf("Address: %s port %s\n", host, port); char timestr[32] = "never"; if(last_state_change) @@ -150,7 +144,7 @@ static int info_node(int fd, const char *item) { printf("Edges: "); sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s to %s", &code, &req, from, to); + int n = sscanf(line, "%d %d %s %s", &code, &req, from, to); if(n == 2) break; if(n != 4) { @@ -166,7 +160,7 @@ static int info_node(int fd, const char *item) { printf("Subnets: "); sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s owner %s", &code, &req, subnet, from); + int n = sscanf(line, "%d %d %s %s", &code, &req, subnet, from); if(n == 2) break; if(n != 4) { @@ -201,7 +195,7 @@ static int info_subnet(int fd, const char *item) { sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item); while(recvline(fd, line, sizeof line)) { - int n = sscanf(line, "%d %d %s owner %s", &code, &req, netstr, owner); + int n = sscanf(line, "%d %d %s %s", &code, &req, netstr, owner); if(n == 2) break; diff --git a/src/net_setup.c b/src/net_setup.c index 3991bd2b..ecffed95 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -574,8 +574,11 @@ static bool setup_myself(void) { myself = new_node(); myself->connection = new_connection(); - myself->hostname = xstrdup("MYSELF"); - myself->connection->hostname = xstrdup("MYSELF"); + if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) + myport = xstrdup("655"); + + xasprintf(&myself->hostname, "MYSELF port %s", myport); + myself->connection->hostname = xstrdup(myself->hostname); myself->connection->options = 0; myself->connection->protocol_major = PROT_MAJOR; @@ -603,9 +606,6 @@ static bool setup_myself(void) { if(!read_rsa_private_key()) return false; - if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) - myport = xstrdup("655"); - if(!atoi(myport)) { struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM); sockaddr_t sa; diff --git a/src/node.c b/src/node.c index 8c81d481..318af59f 100644 --- a/src/node.c +++ b/src/node.c @@ -152,8 +152,8 @@ bool dump_nodes(connection_t *c) { for(node = node_tree->head; node; node = node->next) { n = node->data; - send_request(c, "%d %d %s at %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd) %ld", CONTROL, REQ_DUMP_NODES, - n->name, n->hostname, cipher_get_nid(&n->outcipher), + send_request(c, "%d %d %s %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", CONTROL, REQ_DUMP_NODES, + n->name, n->hostname ?: "unknown port unknown", cipher_get_nid(&n->outcipher), digest_get_nid(&n->outdigest), (int)digest_length(&n->outdigest), n->outcompression, n->options, bitfield_to_int(&n->status, sizeof n->status), n->nexthop ? n->nexthop->name : "-", n->via ? n->via->name ?: "-" : "-", n->distance, n->mtu, n->minmtu, n->maxmtu, (long)n->last_state_change); diff --git a/src/subnet.c b/src/subnet.c index b2c7e7c4..59e4e824 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -283,7 +283,7 @@ bool dump_subnets(connection_t *c) { subnet = node->data; if(!net2str(netstr, sizeof netstr, subnet)) continue; - send_request(c, "%d %d %s owner %s", + send_request(c, "%d %d %s %s", CONTROL, REQ_DUMP_SUBNETS, netstr, subnet->owner->name); } diff --git a/src/tincctl.c b/src/tincctl.c index 00d8eb90..78e2dc14 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -847,7 +847,7 @@ static int cmd_dump(int argc, char *argv[]) { while(recvline(fd, line, sizeof line)) { char node1[4096], node2[4096]; - int n = sscanf(line, "%d %d %s to %s", &code, &req, node1, node2); + int n = sscanf(line, "%d %d %s %s", &code, &req, node1, node2); if(n == 2) { if(do_graph && req == REQ_DUMP_NODES) continue; @@ -860,9 +860,63 @@ static int cmd_dump(int argc, char *argv[]) { if(n < 2) break; - if(!do_graph) - printf("%s\n", line + 5); - else { + if(!do_graph) { + char node[4096]; + char from[4096]; + char to[4096]; + char subnet[4096]; + char host[4096]; + char port[4096]; + char via[4096]; + char nexthop[4096]; + int cipher, digest, maclength, compression, distance, socket, weight; + short int pmtu, minmtu, maxmtu; + unsigned int options, status; + long int last_state_change; + + switch(req) { + case REQ_DUMP_NODES: { + int n = sscanf(line, "%*d %*d %s %s port %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", node, host, port, &cipher, &digest, &maclength, &compression, &options, &status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change); + if(n != 16) { + fprintf(stderr, "Unable to parse node dump from tincd: %s\n", line); + return 1; + } + printf("%s at %s port %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd)\n", + node, host, port, cipher, digest, maclength, compression, options, status, nexthop, via, distance, pmtu, minmtu, maxmtu); + } break; + + case REQ_DUMP_EDGES: { + int n = sscanf(line, "%*d %*d %s %s %s port %s %x %d", from, to, host, port, &options, &weight); + if(n != 6) { + fprintf(stderr, "Unable to parse edge dump from tincd.\n"); + return 1; + } + printf("%s to %s at %s port %s options %x weight %d\n", from, to, host, port, options, weight); + } break; + + case REQ_DUMP_SUBNETS: { + int n = sscanf(line, "%*d %*d %s %s", subnet, node); + if(n != 2) { + fprintf(stderr, "Unable to parse subnet dump from tincd.\n"); + return 1; + } + printf("%s owner %s\n", strip_weight(subnet), node); + } break; + + case REQ_DUMP_CONNECTIONS: { + int n = sscanf(line, "%*d %*d %s %s port %s %x %d %x", node, host, port, &options, &socket, &status); + if(n != 6) { + fprintf(stderr, "Unable to parse connection dump from tincd.\n"); + return 1; + } + printf("%s at %s port %s options %x socket %d status %x\n", node, host, port, options, socket, status); + } break; + + default: + fprintf(stderr, "Unable to parse dump from tincd.\n"); + return 1; + } + } else { if(req == REQ_DUMP_NODES) printf(" %s [label = \"%s\"];\n", node1, node1); else