tinc/src/info.c

358 lines
7.8 KiB
C
Raw Normal View History

2019-08-26 11:44:49 +00:00
/*
info.c -- Show information about a node, subnet or address
2019-08-26 11:44:52 +00:00
Copyright (C) 2012-2017 Guus Sliepen <guus@tinc-vpn.org>
2019-08-26 11:44:49 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "system.h"
#include "control_common.h"
#include "list.h"
#include "subnet.h"
#include "tincctl.h"
#include "info.h"
2019-08-26 11:44:51 +00:00
#include "utils.h"
2019-08-26 11:44:49 +00:00
#include "xalloc.h"
void logger(int level, int priority, const char *format, ...) {
2019-08-26 11:44:53 +00:00
(void)level;
(void)priority;
2019-08-26 11:44:49 +00:00
va_list ap;
2019-08-26 11:44:53 +00:00
2019-08-26 11:44:49 +00:00
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
2019-08-26 11:44:53 +00:00
2019-08-26 11:44:51 +00:00
fputc('\n', stderr);
2019-08-26 11:44:49 +00:00
}
char *strip_weight(char *netstr) {
int len = strlen(netstr);
2019-08-26 11:44:53 +00:00
if(len >= 3 && !strcmp(netstr + len - 3, "#10")) {
2019-08-26 11:44:49 +00:00
netstr[len - 3] = 0;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
return netstr;
}
static int info_node(int fd, const char *item) {
// Check the list of nodes
sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
bool found = false;
char line[4096];
char node[4096];
2019-08-26 11:44:51 +00:00
char id[4096];
2019-08-26 11:44:49 +00:00
char from[4096];
char to[4096];
char subnet[4096];
char host[4096];
char port[4096];
char via[4096];
char nexthop[4096];
int code, req, cipher, digest, maclength, compression, distance;
short int pmtu, minmtu, maxmtu;
unsigned int options;
2019-08-26 11:44:50 +00:00
union {
node_status_t bits;
uint32_t raw;
} status_union;
2019-08-26 11:44:49 +00:00
node_status_t status;
long int last_state_change;
2019-08-26 11:44:53 +00:00
int udp_ping_rtt;
uint64_t in_packets, in_bytes, out_packets, out_bytes;
2019-08-26 11:44:49 +00:00
2019-08-26 11:44:53 +00:00
while(recvline(fd, line, sizeof(line))) {
int n = sscanf(line, "%d %d %4095s %4095s %4095s port %4095s %d %d %d %d %x %"PRIx32" %4095s %4095s %d %hd %hd %hd %ld %d %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_union.raw, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change, &udp_ping_rtt, &in_packets, &in_bytes, &out_packets, &out_bytes);
2019-08-26 11:44:49 +00:00
2019-08-26 11:44:53 +00:00
if(n == 2) {
2019-08-26 11:44:49 +00:00
break;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
2019-08-26 11:44:53 +00:00
if(n != 24) {
2019-08-26 11:44:49 +00:00
fprintf(stderr, "Unable to parse node dump from tincd.\n");
return 1;
}
if(!strcmp(node, item)) {
found = true;
break;
}
}
if(!found) {
fprintf(stderr, "Unknown node %s.\n", item);
return 1;
}
2019-08-26 11:44:53 +00:00
while(recvline(fd, line, sizeof(line))) {
if(sscanf(line, "%d %d %4095s", &code, &req, node) == 2) {
2019-08-26 11:44:49 +00:00
break;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
printf("Node: %s\n", item);
2019-08-26 11:44:51 +00:00
printf("Node ID: %s\n", id);
2019-08-26 11:44:49 +00:00
printf("Address: %s port %s\n", host, port);
char timestr[32] = "never";
2019-08-26 11:44:50 +00:00
time_t lsc_time = last_state_change;
2019-08-26 11:44:53 +00:00
if(last_state_change) {
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
}
2019-08-26 11:44:50 +00:00
status = status_union.bits;
2019-08-26 11:44:49 +00:00
2019-08-26 11:44:53 +00:00
if(status.reachable) {
2019-08-26 11:44:49 +00:00
printf("Online since: %s\n", timestr);
2019-08-26 11:44:53 +00:00
} else {
2019-08-26 11:44:49 +00:00
printf("Last seen: %s\n", timestr);
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
printf("Status: ");
2019-08-26 11:44:53 +00:00
if(status.validkey) {
2019-08-26 11:44:49 +00:00
printf(" validkey");
2019-08-26 11:44:53 +00:00
}
if(status.visited) {
2019-08-26 11:44:49 +00:00
printf(" visited");
2019-08-26 11:44:53 +00:00
}
if(status.reachable) {
2019-08-26 11:44:49 +00:00
printf(" reachable");
2019-08-26 11:44:53 +00:00
}
if(status.indirect) {
2019-08-26 11:44:49 +00:00
printf(" indirect");
2019-08-26 11:44:53 +00:00
}
if(status.sptps) {
2019-08-26 11:44:49 +00:00
printf(" sptps");
2019-08-26 11:44:53 +00:00
}
if(status.udp_confirmed) {
2019-08-26 11:44:49 +00:00
printf(" udp_confirmed");
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
printf("\n");
printf("Options: ");
2019-08-26 11:44:53 +00:00
if(options & OPTION_INDIRECT) {
2019-08-26 11:44:49 +00:00
printf(" indirect");
2019-08-26 11:44:53 +00:00
}
if(options & OPTION_TCPONLY) {
2019-08-26 11:44:49 +00:00
printf(" tcponly");
2019-08-26 11:44:53 +00:00
}
if(options & OPTION_PMTU_DISCOVERY) {
2019-08-26 11:44:49 +00:00
printf(" pmtu_discovery");
2019-08-26 11:44:53 +00:00
}
if(options & OPTION_CLAMP_MSS) {
2019-08-26 11:44:49 +00:00
printf(" clamp_mss");
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
printf("\n");
printf("Protocol: %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
printf("Reachability: ");
2019-08-26 11:44:53 +00:00
if(!strcmp(host, "MYSELF")) {
2019-08-26 11:44:49 +00:00
printf("can reach itself\n");
2019-08-26 11:44:53 +00:00
} else if(!status.reachable) {
2019-08-26 11:44:49 +00:00
printf("unreachable\n");
2019-08-26 11:44:53 +00:00
} else if(strcmp(via, item)) {
2019-08-26 11:44:49 +00:00
printf("indirectly via %s\n", via);
2019-08-26 11:44:53 +00:00
} else if(!status.validkey) {
2019-08-26 11:44:49 +00:00
printf("unknown\n");
2019-08-26 11:44:53 +00:00
} else if(minmtu > 0) {
2019-08-26 11:44:49 +00:00
printf("directly with UDP\nPMTU: %d\n", pmtu);
2019-08-26 11:44:53 +00:00
if(udp_ping_rtt != -1) {
printf("RTT: %d.%03d\n", udp_ping_rtt / 1000, udp_ping_rtt % 1000);
}
} else if(!strcmp(nexthop, item)) {
2019-08-26 11:44:49 +00:00
printf("directly with TCP\n");
2019-08-26 11:44:53 +00:00
} else {
2019-08-26 11:44:49 +00:00
printf("none, forwarded via %s\n", nexthop);
2019-08-26 11:44:53 +00:00
}
printf("RX: %"PRIu64" packets %"PRIu64" bytes\n", in_packets, in_bytes);
printf("TX: %"PRIu64" packets %"PRIu64" bytes\n", out_packets, out_bytes);
2019-08-26 11:44:49 +00:00
// List edges
printf("Edges: ");
sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
2019-08-26 11:44:53 +00:00
while(recvline(fd, line, sizeof(line))) {
2019-08-26 11:44:52 +00:00
int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, from, to);
2019-08-26 11:44:53 +00:00
if(n == 2) {
2019-08-26 11:44:49 +00:00
break;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
if(n != 4) {
fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
return 1;
}
2019-08-26 11:44:53 +00:00
if(!strcmp(from, item)) {
2019-08-26 11:44:49 +00:00
printf(" %s", to);
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
2019-08-26 11:44:53 +00:00
2019-08-26 11:44:49 +00:00
printf("\n");
// List subnets
printf("Subnets: ");
sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
2019-08-26 11:44:53 +00:00
while(recvline(fd, line, sizeof(line))) {
2019-08-26 11:44:52 +00:00
int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, subnet, from);
2019-08-26 11:44:53 +00:00
if(n == 2) {
2019-08-26 11:44:49 +00:00
break;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
if(n != 4) {
fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
return 1;
}
2019-08-26 11:44:53 +00:00
if(!strcmp(from, item)) {
2019-08-26 11:44:49 +00:00
printf(" %s", strip_weight(subnet));
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
2019-08-26 11:44:53 +00:00
2019-08-26 11:44:49 +00:00
printf("\n");
return 0;
}
static int info_subnet(int fd, const char *item) {
subnet_t subnet, find;
if(!str2net(&find, item)) {
fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
return 1;
}
bool address = !strchr(item, '/');
bool weight = strchr(item, '#');
bool found = false;
char line[4096];
char netstr[4096];
char owner[4096];
int code, req;
sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
2019-08-26 11:44:53 +00:00
while(recvline(fd, line, sizeof(line))) {
2019-08-26 11:44:52 +00:00
int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, netstr, owner);
2019-08-26 11:44:53 +00:00
if(n == 2) {
2019-08-26 11:44:49 +00:00
break;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
if(n != 4 || !str2net(&subnet, netstr)) {
fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
return 1;
}
2019-08-26 11:44:53 +00:00
if(find.type != subnet.type) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
if(weight) {
2019-08-26 11:44:53 +00:00
if(find.weight != subnet.weight) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
if(find.type == SUBNET_IPV4) {
if(address) {
2019-08-26 11:44:53 +00:00
if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength)) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
} else {
2019-08-26 11:44:53 +00:00
if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof(subnet.net.ipv4))) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
} else if(find.type == SUBNET_IPV6) {
if(address) {
2019-08-26 11:44:53 +00:00
if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength)) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
} else {
2019-08-26 11:44:53 +00:00
if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof(subnet.net.ipv6))) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
2019-08-26 11:44:53 +00:00
}
if(find.type == SUBNET_MAC) {
if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof(subnet.net.mac))) {
2019-08-26 11:44:49 +00:00
continue;
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
}
found = true;
printf("Subnet: %s\n", strip_weight(netstr));
printf("Owner: %s\n", owner);
}
if(!found) {
2019-08-26 11:44:53 +00:00
if(address) {
2019-08-26 11:44:49 +00:00
fprintf(stderr, "Unknown address %s.\n", item);
2019-08-26 11:44:53 +00:00
} else {
2019-08-26 11:44:49 +00:00
fprintf(stderr, "Unknown subnet %s.\n", item);
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
return 1;
}
return 0;
}
int info(int fd, const char *item) {
2019-08-26 11:44:53 +00:00
if(check_id(item)) {
2019-08-26 11:44:49 +00:00
return info_node(fd, item);
2019-08-26 11:44:53 +00:00
}
if(strchr(item, '.') || strchr(item, ':')) {
2019-08-26 11:44:49 +00:00
return info_subnet(fd, item);
2019-08-26 11:44:53 +00:00
}
2019-08-26 11:44:49 +00:00
fprintf(stderr, "Argument is not a node name, subnet or address.\n");
return 1;
}