2 info.c -- Show information about a node, subnet or address
3 Copyright (C) 2012-2017 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "control_common.h"
28 void logger(int level, int priority, const char *format, ...) {
34 vfprintf(stderr, format, ap);
40 char *strip_weight(char *netstr) {
41 size_t len = strlen(netstr);
43 if(len >= 3 && !strcmp(netstr + len - 3, "#10")) {
50 static int info_node(int fd, const char *item) {
51 // Check the list of nodes
52 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
66 int code, req, cipher, digest, maclength, compression, distance;
67 short int pmtu, minmtu, maxmtu;
74 long int last_state_change;
76 uint64_t in_packets, in_bytes, out_packets, out_bytes;
78 while(recvline(fd, line, sizeof(line))) {
79 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);
86 fprintf(stderr, "Unable to parse node dump from tincd.\n");
90 if(!strcmp(node, item)) {
97 fprintf(stderr, "Unknown node %s.\n", item);
101 while(recvline(fd, line, sizeof(line))) {
102 if(sscanf(line, "%d %d %4095s", &code, &req, node) == 2) {
107 printf("Node: %s\n", item);
108 printf("Node ID: %s\n", id);
109 printf("Address: %s port %s\n", host, port);
111 char timestr[32] = "never";
112 time_t lsc_time = last_state_change;
114 if(last_state_change) {
115 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
118 status = status_union.bits;
120 if(status.reachable) {
121 printf("Online since: %s\n", timestr);
123 printf("Last seen: %s\n", timestr);
128 if(status.validkey) {
136 if(status.reachable) {
137 printf(" reachable");
140 if(status.indirect) {
148 if(status.udp_confirmed) {
149 printf(" udp_confirmed");
156 if(options & OPTION_INDIRECT) {
160 if(options & OPTION_TCPONLY) {
164 if(options & OPTION_PMTU_DISCOVERY) {
165 printf(" pmtu_discovery");
168 if(options & OPTION_CLAMP_MSS) {
169 printf(" clamp_mss");
173 printf("Protocol: %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
174 printf("Reachability: ");
176 if(!strcmp(host, "MYSELF")) {
177 printf("can reach itself\n");
178 } else if(!status.reachable) {
179 printf("unreachable\n");
180 } else if(strcmp(via, item)) {
181 printf("indirectly via %s\n", via);
182 } else if(!status.validkey) {
184 } else if(minmtu > 0) {
185 printf("directly with UDP\nPMTU: %d\n", pmtu);
187 if(udp_ping_rtt != -1) {
188 printf("RTT: %d.%03d\n", udp_ping_rtt / 1000, udp_ping_rtt % 1000);
190 } else if(!strcmp(nexthop, item)) {
191 printf("directly with TCP\n");
193 printf("none, forwarded via %s\n", nexthop);
196 printf("RX: %"PRIu64" packets %"PRIu64" bytes\n", in_packets, in_bytes);
197 printf("TX: %"PRIu64" packets %"PRIu64" bytes\n", out_packets, out_bytes);
201 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
203 while(recvline(fd, line, sizeof(line))) {
204 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, from, to);
211 fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
215 if(!strcmp(from, item)) {
224 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
226 while(recvline(fd, line, sizeof(line))) {
227 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, subnet, from);
234 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
238 if(!strcmp(from, item)) {
239 printf(" %s", strip_weight(subnet));
248 static int info_subnet(int fd, const char *item) {
249 subnet_t subnet, find;
251 if(!str2net(&find, item)) {
252 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
256 bool address = !strchr(item, '/');
257 bool weight = strchr(item, '#');
266 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
268 while(recvline(fd, line, sizeof(line))) {
269 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, netstr, owner);
275 if(n != 4 || !str2net(&subnet, netstr)) {
276 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
280 if(find.type != subnet.type) {
285 if(find.weight != subnet.weight) {
290 if(find.type == SUBNET_IPV4) {
292 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength)) {
296 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength) {
300 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof(subnet.net.ipv4.address))) {
304 } else if(find.type == SUBNET_IPV6) {
306 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength)) {
310 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength) {
314 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof(subnet.net.ipv6.address))) {
320 if(find.type == SUBNET_MAC) {
321 if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof(subnet.net.mac.address))) {
327 printf("Subnet: %s\n", strip_weight(netstr));
328 printf("Owner: %s\n", owner);
333 fprintf(stderr, "Unknown address %s.\n", item);
335 fprintf(stderr, "Unknown subnet %s.\n", item);
344 int info(int fd, const char *item) {
346 return info_node(fd, item);
349 if(strchr(item, '.') || strchr(item, ':')) {
350 return info_subnet(fd, item);
353 fprintf(stderr, "Argument is not a node name, subnet or address.\n");