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"
30 void logger(int level, int priority, const char *format, ...) {
33 vfprintf(stderr, format, ap);
38 char *strip_weight(char *netstr) {
39 int len = strlen(netstr);
41 if(len >= 3 && !strcmp(netstr + len - 3, "#10")) {
48 static int info_node(int fd, const char *item) {
49 // Check the list of nodes
50 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
64 int code, req, cipher, digest, maclength, compression, distance;
65 short int pmtu, minmtu, maxmtu;
72 long int last_state_change;
74 uint64_t in_packets, in_bytes, out_packets, out_bytes;
76 while(recvline(fd, line, sizeof(line))) {
77 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);
84 fprintf(stderr, "Unable to parse node dump from tincd.\n");
88 if(!strcmp(node, item)) {
95 fprintf(stderr, "Unknown node %s.\n", item);
99 while(recvline(fd, line, sizeof(line))) {
100 if(sscanf(line, "%d %d %4095s", &code, &req, node) == 2) {
105 printf("Node: %s\n", item);
106 printf("Node ID: %s\n", id);
107 printf("Address: %s port %s\n", host, port);
109 char timestr[32] = "never";
110 time_t lsc_time = last_state_change;
112 if(last_state_change) {
113 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
116 status = status_union.bits;
118 if(status.reachable) {
119 printf("Online since: %s\n", timestr);
121 printf("Last seen: %s\n", timestr);
126 if(status.validkey) {
134 if(status.reachable) {
135 printf(" reachable");
138 if(status.indirect) {
146 if(status.udp_confirmed) {
147 printf(" udp_confirmed");
154 if(options & OPTION_INDIRECT) {
158 if(options & OPTION_TCPONLY) {
162 if(options & OPTION_PMTU_DISCOVERY) {
163 printf(" pmtu_discovery");
166 if(options & OPTION_CLAMP_MSS) {
167 printf(" clamp_mss");
171 printf("Protocol: %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
172 printf("Reachability: ");
174 if(!strcmp(host, "MYSELF")) {
175 printf("can reach itself\n");
176 } else if(!status.reachable) {
177 printf("unreachable\n");
178 } else if(strcmp(via, item)) {
179 printf("indirectly via %s\n", via);
180 } else if(!status.validkey) {
182 } else if(minmtu > 0) {
183 printf("directly with UDP\nPMTU: %d\n", pmtu);
185 if(udp_ping_rtt != -1) {
186 printf("RTT: %d.%03d\n", udp_ping_rtt / 1000, udp_ping_rtt % 1000);
188 } else if(!strcmp(nexthop, item)) {
189 printf("directly with TCP\n");
191 printf("none, forwarded via %s\n", nexthop);
194 printf("RX: %"PRIu64" packets %"PRIu64" bytes\n", in_packets, in_bytes);
195 printf("TX: %"PRIu64" packets %"PRIu64" bytes\n", out_packets, out_bytes);
199 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
201 while(recvline(fd, line, sizeof(line))) {
202 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, from, to);
209 fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
213 if(!strcmp(from, item)) {
222 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
224 while(recvline(fd, line, sizeof(line))) {
225 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, subnet, from);
232 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
236 if(!strcmp(from, item)) {
237 printf(" %s", strip_weight(subnet));
246 static int info_subnet(int fd, const char *item) {
247 subnet_t subnet, find;
249 if(!str2net(&find, item)) {
250 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
254 bool address = !strchr(item, '/');
255 bool weight = strchr(item, '#');
264 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
266 while(recvline(fd, line, sizeof(line))) {
267 int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, netstr, owner);
273 if(n != 4 || !str2net(&subnet, netstr)) {
274 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
278 if(find.type != subnet.type) {
283 if(find.weight != subnet.weight) {
288 if(find.type == SUBNET_IPV4) {
290 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength)) {
294 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength) {
298 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof(subnet.net.ipv4))) {
302 } else if(find.type == SUBNET_IPV6) {
304 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength)) {
308 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength) {
312 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof(subnet.net.ipv6))) {
318 if(find.type == SUBNET_MAC) {
319 if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof(subnet.net.mac))) {
325 printf("Subnet: %s\n", strip_weight(netstr));
326 printf("Owner: %s\n", owner);
331 fprintf(stderr, "Unknown address %s.\n", item);
333 fprintf(stderr, "Unknown subnet %s.\n", item);
342 int info(int fd, const char *item) {
344 return info_node(fd, item);
347 if(strchr(item, '.') || strchr(item, ':')) {
348 return info_subnet(fd, item);
351 fprintf(stderr, "Argument is not a node name, subnet or address.\n");