"tincctl info" gives more human readable information about nodes or subnets.
[tinc] / src / info.c
1 /*
2     info.c -- Show information about a node, subnet or address
3     Copyright (C) 2012 Guus Sliepen <guus@tinc-vpn.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "control_common.h"
23 #include "list.h"
24 #include "subnet.h"
25 #include "tincctl.h"
26 #include "info.h"
27 #include "xalloc.h"
28
29 void logger(int level, int priority, const char *format, ...) {
30         va_list ap;
31         va_start(ap, format);
32         vfprintf(stderr, format, ap);
33         va_end(ap);
34 }
35
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);
39
40         bool found = false;
41         char line[4096];
42
43         char node[4096];
44         char from[4096];
45         char to[4096];
46         char subnet[4096];
47         char host[4096];
48         char port[4096];
49         char via[4096];
50         char nexthop[4096];
51         int code, req, cipher, digest, maclength, compression, distance;
52         short int pmtu, minmtu, maxmtu;
53         unsigned int options;
54         node_status_t status;
55
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);
58
59                 if(n == 2)
60                         break;
61
62                 if(n != 17) {
63                         *port = 0;
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);
65
66                         if(n != 16) {
67                                 fprintf(stderr, "Unable to parse node dump from tincd.\n");
68                                 return 1;
69                         }
70                 }
71
72                 if(!strcmp(node, item)) {
73                         found = true;
74                         break;
75                 }
76         }
77
78         if(!found) {
79                 fprintf(stderr, "Unknown node %s.\n", item);
80                 return 1;
81         }
82
83         while(recvline(fd, line, sizeof line)) {
84                 if(sscanf(line, "%d %d %s", &code, &req, node) == 2)
85                         break;
86         }
87         
88         printf("Node:         %s\n", item);
89         if(*port)
90                 printf("Address:      %s port %s\n", host, port);
91         printf("Status:      ");
92         if(status.validkey)
93                 printf(" validkey");
94         if(status.visited)
95                 printf(" visited");
96         if(status.reachable)
97                 printf(" reachable");
98         if(status.indirect)
99                 printf(" indirect");
100         if(status.ecdh)
101                 printf(" ecdh");
102         printf("\n");
103         printf("Options:     ");
104         if(options & OPTION_INDIRECT)
105                 printf(" indirect");
106         if(options & OPTION_TCPONLY)
107                 printf(" tcponly");
108         if(options & OPTION_PMTU_DISCOVERY)
109                 printf(" pmtu_discovery");
110         if(options & OPTION_CLAMP_MSS)
111                 printf(" clamp_mss");
112         printf("\n");
113         printf("Reachability: ");
114         if(!*port)
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)
121                 printf("unknown\n");
122         else if(minmtu > 0)
123                 printf("directly with UDP\nPMTU:         %d\n", pmtu);
124         else if(!strcmp(nexthop, item))
125                 printf("directly with TCP\n");
126         else
127                 printf("none, forwarded via %s\n", nexthop);
128
129         // List edges
130         printf("Edges:       ");
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);
134                 if(n == 2)
135                         break;
136                 if(n != 4) {
137                         fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
138                         return 1;
139                 }
140                 if(!strcmp(from, item))
141                         printf(" %s", to);
142         }
143         printf("\n");
144
145         // List subnets
146         printf("Subnets:     ");
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);
150                 if(n == 2)
151                         break;
152                 if(n != 4) {
153                         fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
154                         return 1;
155                 }
156                 if(!strcmp(from, item))
157                         printf(" %s", subnet);
158         }
159         printf("\n");
160
161         return 0;
162 }
163
164 static int info_subnet(int fd, const char *item) {
165         subnet_t subnet, find;
166
167         if(!str2net(&find, item))
168                 return 1;
169
170         bool address = !strchr(item, '/');
171         bool weight = strchr(item, '#');
172         bool found = false;
173
174         char line[4096];
175         char netstr[4096];
176         char owner[4096];
177
178         int code, req;
179
180         sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
181         while(recvline(fd, line, sizeof line)) {
182                 int n = sscanf(line, "%d %d %s owner %s", &code, &req, netstr, owner);
183                 if(n == 2)
184                         break;
185
186                 if(n != 4 || !str2net(&subnet, netstr)) {
187                         fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
188                         return 1;
189                 }
190
191                 if(find.type != subnet.type)
192                         continue;
193
194                 if(weight) {
195                         if(find.weight != subnet.weight)
196                                 continue;
197                 }
198
199                 if(find.type == SUBNET_IPV4) {
200                         if(address) {
201                                 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength))
202                                         continue;
203                         } else {
204                                 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength)
205                                         continue;
206                                 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof subnet.net.ipv4))
207                                         continue;
208                         }
209                 } else if(find.type == SUBNET_IPV6) {
210                         if(address) {
211                                 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength))
212                                         continue;
213                         } else {
214                                 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength)
215                                         continue;
216                                 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof subnet.net.ipv6))
217                                         continue;
218                         }
219                 } if(find.type == SUBNET_MAC) {
220                         if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof subnet.net.mac))
221                                 continue;
222                 }
223
224                 found = true;
225                 printf("Subnet: %s\n", netstr);
226                 printf("Owner:  %s\n", owner);
227         }
228
229         if(!found) {
230                 if(address)
231                         fprintf(stderr, "Unknown address %s.\n", item);
232                 else
233                         fprintf(stderr, "Unknown subnet %s.\n", item);
234                 return 1;
235         }
236
237         return 0;
238 }
239
240 int info(int fd, const char *item) {
241         if(check_id(item))
242                 return info_node(fd, item);
243         if(strchr(item, '.') || strchr(item, ':'))
244                 return info_subnet(fd, item);
245
246         fprintf(stderr, "Argument is not a node name, subnet or address.\n");
247         return 1;
248 }