Update copyright notices.
[tinc] / src / info.c
1 /*
2     info.c -- Show information about a node, subnet or address
3     Copyright (C) 2012-2013 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         fputc('\n', stderr);
35 }
36
37 char *strip_weight(char *netstr) {
38         int len = strlen(netstr);
39         if(len >= 3 && !strcmp(netstr + len - 3, "#10"))
40                 netstr[len - 3] = 0;
41         return netstr;
42 }
43
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);
47
48         bool found = false;
49         char line[4096];
50
51         char node[4096];
52         char from[4096];
53         char to[4096];
54         char subnet[4096];
55         char host[4096];
56         char port[4096];
57         char via[4096];
58         char nexthop[4096];
59         int code, req, cipher, digest, maclength, compression, distance;
60         short int pmtu, minmtu, maxmtu;
61         unsigned int options;
62         union {
63                 node_status_t bits;
64                 uint32_t raw;
65         } status_union;
66         node_status_t status;
67         long int last_state_change;
68
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);
71
72                 if(n == 2)
73                         break;
74
75                 if(n != 18) {
76                         fprintf(stderr, "Unable to parse node dump from tincd.\n");
77                         return 1;
78                 }
79
80                 if(!strcmp(node, item)) {
81                         found = true;
82                         break;
83                 }
84         }
85
86         if(!found) {
87                 fprintf(stderr, "Unknown node %s.\n", item);
88                 return 1;
89         }
90
91         while(recvline(fd, line, sizeof line)) {
92                 if(sscanf(line, "%d %d %s", &code, &req, node) == 2)
93                         break;
94         }
95
96         printf("Node:         %s\n", item);
97         printf("Address:      %s port %s\n", host, port);
98
99         char timestr[32] = "never";
100         time_t lsc_time = last_state_change;
101
102         if(last_state_change)
103                 strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
104
105         status = status_union.bits;
106
107         if(status.reachable)
108                 printf("Online since: %s\n", timestr);
109         else
110                 printf("Last seen:    %s\n", timestr);
111
112         printf("Status:      ");
113         if(status.validkey)
114                 printf(" validkey");
115         if(status.visited)
116                 printf(" visited");
117         if(status.reachable)
118                 printf(" reachable");
119         if(status.indirect)
120                 printf(" indirect");
121         if(status.sptps)
122                 printf(" sptps");
123         if(status.udp_confirmed)
124                 printf(" udp_confirmed");
125         printf("\n");
126
127         printf("Options:     ");
128         if(options & OPTION_INDIRECT)
129                 printf(" indirect");
130         if(options & OPTION_TCPONLY)
131                 printf(" tcponly");
132         if(options & OPTION_PMTU_DISCOVERY)
133                 printf(" pmtu_discovery");
134         if(options & OPTION_CLAMP_MSS)
135                 printf(" clamp_mss");
136         printf("\n");
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)
146                 printf("unknown\n");
147         else if(minmtu > 0)
148                 printf("directly with UDP\nPMTU:         %d\n", pmtu);
149         else if(!strcmp(nexthop, item))
150                 printf("directly with TCP\n");
151         else
152                 printf("none, forwarded via %s\n", nexthop);
153
154         // List edges
155         printf("Edges:       ");
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);
159                 if(n == 2)
160                         break;
161                 if(n != 4) {
162                         fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
163                         return 1;
164                 }
165                 if(!strcmp(from, item))
166                         printf(" %s", to);
167         }
168         printf("\n");
169
170         // List subnets
171         printf("Subnets:     ");
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);
175                 if(n == 2)
176                         break;
177                 if(n != 4) {
178                         fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
179                         return 1;
180                 }
181                 if(!strcmp(from, item))
182                         printf(" %s", strip_weight(subnet));
183         }
184         printf("\n");
185
186         return 0;
187 }
188
189 static int info_subnet(int fd, const char *item) {
190         subnet_t subnet, find;
191
192         if(!str2net(&find, item)) {
193                 fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
194                 return 1;
195         }
196
197         bool address = !strchr(item, '/');
198         bool weight = strchr(item, '#');
199         bool found = false;
200
201         char line[4096];
202         char netstr[4096];
203         char owner[4096];
204
205         int code, req;
206
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);
210                 if(n == 2)
211                         break;
212
213                 if(n != 4 || !str2net(&subnet, netstr)) {
214                         fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
215                         return 1;
216                 }
217
218                 if(find.type != subnet.type)
219                         continue;
220
221                 if(weight) {
222                         if(find.weight != subnet.weight)
223                                 continue;
224                 }
225
226                 if(find.type == SUBNET_IPV4) {
227                         if(address) {
228                                 if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength))
229                                         continue;
230                         } else {
231                                 if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength)
232                                         continue;
233                                 if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof subnet.net.ipv4))
234                                         continue;
235                         }
236                 } else if(find.type == SUBNET_IPV6) {
237                         if(address) {
238                                 if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength))
239                                         continue;
240                         } else {
241                                 if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength)
242                                         continue;
243                                 if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof subnet.net.ipv6))
244                                         continue;
245                         }
246                 } if(find.type == SUBNET_MAC) {
247                         if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof subnet.net.mac))
248                                 continue;
249                 }
250
251                 found = true;
252                 printf("Subnet: %s\n", strip_weight(netstr));
253                 printf("Owner:  %s\n", owner);
254         }
255
256         if(!found) {
257                 if(address)
258                         fprintf(stderr, "Unknown address %s.\n", item);
259                 else
260                         fprintf(stderr, "Unknown subnet %s.\n", item);
261                 return 1;
262         }
263
264         return 0;
265 }
266
267 int info(int fd, const char *item) {
268         if(check_id(item))
269                 return info_node(fd, item);
270         if(strchr(item, '.') || strchr(item, ':'))
271                 return info_subnet(fd, item);
272
273         fprintf(stderr, "Argument is not a node name, subnet or address.\n");
274         return 1;
275 }