2 info.c -- Show information about a node, subnet or address
3 Copyright (C) 2012-2013 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);
37 char *strip_weight(char *netstr) {
38 int len = strlen(netstr);
39 if(len >= 3 && !strcmp(netstr + len - 3, "#10"))
44 static int info_node(int fd, const char *item) {
45 // Check the list of nodes
46 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
59 int code, req, cipher, digest, maclength, compression, distance;
60 short int pmtu, minmtu, maxmtu;
67 long int last_state_change;
69 while(recvline(fd, line, sizeof line)) {
70 int n = sscanf(line, "%d %d %s %s port %s %d %d %d %d %x %"PRIx32" %s %s %d %hd %hd %hd %ld", &code, &req, node, host, port, &cipher, &digest, &maclength, &compression, &options, &status_union.raw, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change);
76 fprintf(stderr, "Unable to parse node dump from tincd.\n");
80 if(!strcmp(node, item)) {
87 fprintf(stderr, "Unknown node %s.\n", item);
91 while(recvline(fd, line, sizeof line)) {
92 if(sscanf(line, "%d %d %s", &code, &req, node) == 2)
96 printf("Node: %s\n", item);
97 printf("Address: %s port %s\n", host, port);
99 char timestr[32] = "never";
100 time_t lsc_time = last_state_change;
102 if(last_state_change)
103 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
105 status = status_union.bits;
108 printf("Online since: %s\n", timestr);
110 printf("Last seen: %s\n", timestr);
118 printf(" reachable");
123 if(status.udp_confirmed)
124 printf(" udp_confirmed");
128 if(options & OPTION_INDIRECT)
130 if(options & OPTION_TCPONLY)
132 if(options & OPTION_PMTU_DISCOVERY)
133 printf(" pmtu_discovery");
134 if(options & OPTION_CLAMP_MSS)
135 printf(" clamp_mss");
137 printf("Protocol: %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
138 printf("Reachability: ");
139 if(!strcmp(host, "MYSELF"))
140 printf("can reach itself\n");
141 else if(!status.reachable)
142 printf("unreachable\n");
143 else if(strcmp(via, item))
144 printf("indirectly via %s\n", via);
145 else if(!status.validkey)
148 printf("directly with UDP\nPMTU: %d\n", pmtu);
149 else if(!strcmp(nexthop, item))
150 printf("directly with TCP\n");
152 printf("none, forwarded via %s\n", nexthop);
156 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
157 while(recvline(fd, line, sizeof line)) {
158 int n = sscanf(line, "%d %d %s %s", &code, &req, from, to);
162 fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
165 if(!strcmp(from, item))
172 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
173 while(recvline(fd, line, sizeof line)) {
174 int n = sscanf(line, "%d %d %s %s", &code, &req, subnet, from);
178 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
181 if(!strcmp(from, item))
182 printf(" %s", strip_weight(subnet));
189 static int info_subnet(int fd, const char *item) {
190 subnet_t subnet, find;
192 if(!str2net(&find, item)) {
193 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
197 bool address = !strchr(item, '/');
198 bool weight = strchr(item, '#');
207 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
208 while(recvline(fd, line, sizeof line)) {
209 int n = sscanf(line, "%d %d %s %s", &code, &req, netstr, owner);
213 if(n != 4 || !str2net(&subnet, netstr)) {
214 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
218 if(find.type != subnet.type)
222 if(find.weight != subnet.weight)
226 if(find.type == SUBNET_IPV4) {
228 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength))
231 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength)
233 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof subnet.net.ipv4))
236 } else if(find.type == SUBNET_IPV6) {
238 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength))
241 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength)
243 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof subnet.net.ipv6))
246 } if(find.type == SUBNET_MAC) {
247 if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof subnet.net.mac))
252 printf("Subnet: %s\n", strip_weight(netstr));
253 printf("Owner: %s\n", owner);
258 fprintf(stderr, "Unknown address %s.\n", item);
260 fprintf(stderr, "Unknown subnet %s.\n", item);
267 int info(int fd, const char *item) {
269 return info_node(fd, item);
270 if(strchr(item, '.') || strchr(item, ':'))
271 return info_subnet(fd, item);
273 fprintf(stderr, "Argument is not a node name, subnet or address.\n");