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 static int info_node(int fd, const char *item) {
37 // Check the list of nodes
38 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
51 int code, req, cipher, digest, maclength, compression, distance;
52 short int pmtu, minmtu, maxmtu;
56 while(recvline(fd, line, sizeof line)) {
57 int n = sscanf(line, "%d %d %s at %s port %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd)", &code, &req, node, host, port, &cipher, &digest, &maclength, &compression, &options, (unsigned *)&status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu);
64 n = sscanf(line, "%d %d %s at %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %hd (min %hd max %hd)", &code, &req, node, host, &cipher, &digest, &maclength, &compression, &options, (unsigned *)&status, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu);
67 fprintf(stderr, "Unable to parse node dump from tincd.\n");
72 if(!strcmp(node, item)) {
79 fprintf(stderr, "Unknown node %s.\n", item);
83 while(recvline(fd, line, sizeof line)) {
84 if(sscanf(line, "%d %d %s", &code, &req, node) == 2)
88 printf("Node: %s\n", item);
90 printf("Address: %s port %s\n", host, port);
104 if(options & OPTION_INDIRECT)
106 if(options & OPTION_TCPONLY)
108 if(options & OPTION_PMTU_DISCOVERY)
109 printf(" pmtu_discovery");
110 if(options & OPTION_CLAMP_MSS)
111 printf(" clamp_mss");
113 printf("Reachability: ");
115 printf("can reach itself\n");
116 else if(!status.reachable)
117 printf("unreachable\n");
118 else if(strcmp(via, item))
119 printf("indirectly via %s\n", via);
120 else if(!status.validkey)
123 printf("directly with UDP\nPMTU: %d\n", pmtu);
124 else if(!strcmp(nexthop, item))
125 printf("directly with TCP\n");
127 printf("none, forwarded via %s\n", nexthop);
131 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
132 while(recvline(fd, line, sizeof line)) {
133 int n = sscanf(line, "%d %d %s to %s", &code, &req, from, to);
137 fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
140 if(!strcmp(from, item))
147 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
148 while(recvline(fd, line, sizeof line)) {
149 int n = sscanf(line, "%d %d %s owner %s", &code, &req, subnet, from);
153 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
156 if(!strcmp(from, item))
157 printf(" %s", subnet);
164 static int info_subnet(int fd, const char *item) {
165 subnet_t subnet, find;
167 if(!str2net(&find, item)) {
168 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
172 bool address = !strchr(item, '/');
173 bool weight = strchr(item, '#');
182 sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
183 while(recvline(fd, line, sizeof line)) {
184 int n = sscanf(line, "%d %d %s owner %s", &code, &req, netstr, owner);
188 if(n != 4 || !str2net(&subnet, netstr)) {
189 fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
193 if(find.type != subnet.type)
197 if(find.weight != subnet.weight)
201 if(find.type == SUBNET_IPV4) {
203 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength))
206 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength)
208 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof subnet.net.ipv4))
211 } else if(find.type == SUBNET_IPV6) {
213 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength))
216 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength)
218 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof subnet.net.ipv6))
221 } if(find.type == SUBNET_MAC) {
222 if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof subnet.net.mac))
227 printf("Subnet: %s\n", netstr);
228 printf("Owner: %s\n", owner);
233 fprintf(stderr, "Unknown address %s.\n", item);
235 fprintf(stderr, "Unknown subnet %s.\n", item);
242 int info(int fd, const char *item) {
244 return info_node(fd, item);
245 if(strchr(item, '.') || strchr(item, ':'))
246 return info_subnet(fd, item);
248 fprintf(stderr, "Argument is not a node name, subnet or address.\n");