2 info.c -- Show information about a node, subnet or address
3 Copyright (C) 2012 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"
29 void logger(int level, int priority, const char *format, ...) {
32 vfprintf(stderr, format, ap);
36 char *strip_weight(char *netstr) {
37 int len = strlen(netstr);
38 if(len >= 3 && !strcmp(netstr + len - 3, "#10"))
43 static int info_node(int fd, const char *item) {
44 // Check the list of nodes
45 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
58 int code, req, cipher, digest, maclength, compression, distance;
59 short int pmtu, minmtu, maxmtu;
62 long int last_state_change;
64 while(recvline(fd, line, sizeof line)) {
65 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);
71 fprintf(stderr, "Unable to parse node dump from tincd.\n");
75 if(!strcmp(node, item)) {
82 fprintf(stderr, "Unknown node %s.\n", item);
86 while(recvline(fd, line, sizeof line)) {
87 if(sscanf(line, "%d %d %s", &code, &req, node) == 2)
91 printf("Node: %s\n", item);
92 printf("Address: %s port %s\n", host, port);
94 char timestr[32] = "never";
96 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&last_state_change));
99 printf("Online since: %s\n", timestr);
101 printf("Last seen: %s\n", timestr);
109 printf(" reachable");
117 if(options & OPTION_INDIRECT)
119 if(options & OPTION_TCPONLY)
121 if(options & OPTION_PMTU_DISCOVERY)
122 printf(" pmtu_discovery");
123 if(options & OPTION_CLAMP_MSS)
124 printf(" clamp_mss");
126 printf("Protocol: %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
127 printf("Reachability: ");
128 if(!strcmp(host, "MYSELF"))
129 printf("can reach itself\n");
130 else if(!status.reachable)
131 printf("unreachable\n");
132 else if(strcmp(via, item))
133 printf("indirectly via %s\n", via);
134 else if(!status.validkey)
137 printf("directly with UDP\nPMTU: %d\n", pmtu);
138 else if(!strcmp(nexthop, item))
139 printf("directly with TCP\n");
141 printf("none, forwarded via %s\n", nexthop);
145 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
146 while(recvline(fd, line, sizeof line)) {
147 int n = sscanf(line, "%d %d %s %s", &code, &req, from, to);
151 fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
154 if(!strcmp(from, item))
161 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
162 while(recvline(fd, line, sizeof line)) {
163 int n = sscanf(line, "%d %d %s %s", &code, &req, subnet, from);
167 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
170 if(!strcmp(from, item))
171 printf(" %s", strip_weight(subnet));
178 static int info_subnet(int fd, const char *item) {
179 subnet_t subnet, find;
181 if(!str2net(&find, item)) {
182 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
186 bool address = !strchr(item, '/');
187 bool weight = strchr(item, '#');
196 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
197 while(recvline(fd, line, sizeof line)) {
198 int n = sscanf(line, "%d %d %s %s", &code, &req, netstr, owner);
202 if(n != 4 || !str2net(&subnet, netstr)) {
203 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
207 if(find.type != subnet.type)
211 if(find.weight != subnet.weight)
215 if(find.type == SUBNET_IPV4) {
217 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength))
220 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength)
222 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof subnet.net.ipv4))
225 } else if(find.type == SUBNET_IPV6) {
227 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength))
230 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength)
232 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof subnet.net.ipv6))
235 } if(find.type == SUBNET_MAC) {
236 if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof subnet.net.mac))
241 printf("Subnet: %s\n", strip_weight(netstr));
242 printf("Owner: %s\n", owner);
247 fprintf(stderr, "Unknown address %s.\n", item);
249 fprintf(stderr, "Unknown subnet %s.\n", item);
256 int info(int fd, const char *item) {
258 return info_node(fd, item);
259 if(strchr(item, '.') || strchr(item, ':'))
260 return info_subnet(fd, item);
262 fprintf(stderr, "Argument is not a node name, subnet or address.\n");