1 /*
2     info.c -- Show information about a node, subnet or address
3     Copyright (C) 2012-2017 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 "utils.h"
28 #include "xalloc.h"
29 
logger(int level,int priority,const char * format,...)30 void logger(int level, int priority, const char *format, ...) {
31 	(void)level;
32 	(void)priority;
33 	va_list ap;
34 
35 	va_start(ap, format);
36 	vfprintf(stderr, format, ap);
37 	va_end(ap);
38 
39 	fputc('\n', stderr);
40 }
41 
strip_weight(char * netstr)42 char *strip_weight(char *netstr) {
43 	int len = strlen(netstr);
44 
45 	if(len >= 3 && !strcmp(netstr + len - 3, "#10")) {
46 		netstr[len - 3] = 0;
47 	}
48 
49 	return netstr;
50 }
51 
info_node(int fd,const char * item)52 static int info_node(int fd, const char *item) {
53 	// Check the list of nodes
54 	sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_NODES, item);
55 
56 	bool found = false;
57 	char line[4096];
58 
59 	char node[4096];
60 	char id[4096];
61 	char from[4096];
62 	char to[4096];
63 	char subnet[4096];
64 	char host[4096];
65 	char port[4096];
66 	char via[4096];
67 	char nexthop[4096];
68 	int code, req, cipher, digest, maclength, compression, distance;
69 	short int pmtu, minmtu, maxmtu;
70 	unsigned int options;
71 	union {
72 		node_status_t bits;
73 		uint32_t raw;
74 	} status_union;
75 	node_status_t status;
76 	long int last_state_change;
77 	int udp_ping_rtt;
78 	uint64_t in_packets, in_bytes, out_packets, out_bytes;
79 
80 	while(recvline(fd, line, sizeof(line))) {
81 		int n = sscanf(line, "%d %d %4095s %4095s %4095s port %4095s %d %d %d %d %x %"PRIx32" %4095s %4095s %d %hd %hd %hd %ld %d %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_union.raw, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change, &udp_ping_rtt, &in_packets, &in_bytes, &out_packets, &out_bytes);
82 
83 		if(n == 2) {
84 			break;
85 		}
86 
87 		if(n != 24) {
88 			fprintf(stderr, "Unable to parse node dump from tincd.\n");
89 			return 1;
90 		}
91 
92 		if(!strcmp(node, item)) {
93 			found = true;
94 			break;
95 		}
96 	}
97 
98 	if(!found) {
99 		fprintf(stderr, "Unknown node %s.\n", item);
100 		return 1;
101 	}
102 
103 	while(recvline(fd, line, sizeof(line))) {
104 		if(sscanf(line, "%d %d %4095s", &code, &req, node) == 2) {
105 			break;
106 		}
107 	}
108 
109 	printf("Node:         %s\n", item);
110 	printf("Node ID:      %s\n", id);
111 	printf("Address:      %s port %s\n", host, port);
112 
113 	char timestr[32] = "never";
114 	time_t lsc_time = last_state_change;
115 
116 	if(last_state_change) {
117 		strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&lsc_time));
118 	}
119 
120 	status = status_union.bits;
121 
122 	if(status.reachable) {
123 		printf("Online since: %s\n", timestr);
124 	} else {
125 		printf("Last seen:    %s\n", timestr);
126 	}
127 
128 	printf("Status:      ");
129 
130 	if(status.validkey) {
131 		printf(" validkey");
132 	}
133 
134 	if(status.visited) {
135 		printf(" visited");
136 	}
137 
138 	if(status.reachable) {
139 		printf(" reachable");
140 	}
141 
142 	if(status.indirect) {
143 		printf(" indirect");
144 	}
145 
146 	if(status.sptps) {
147 		printf(" sptps");
148 	}
149 
150 	if(status.udp_confirmed) {
151 		printf(" udp_confirmed");
152 	}
153 
154 	printf("\n");
155 
156 	printf("Options:     ");
157 
158 	if(options & OPTION_INDIRECT) {
159 		printf(" indirect");
160 	}
161 
162 	if(options & OPTION_TCPONLY) {
163 		printf(" tcponly");
164 	}
165 
166 	if(options & OPTION_PMTU_DISCOVERY) {
167 		printf(" pmtu_discovery");
168 	}
169 
170 	if(options & OPTION_CLAMP_MSS) {
171 		printf(" clamp_mss");
172 	}
173 
174 	printf("\n");
175 	printf("Protocol:     %d.%d\n", PROT_MAJOR, OPTION_VERSION(options));
176 	printf("Reachability: ");
177 
178 	if(!strcmp(host, "MYSELF")) {
179 		printf("can reach itself\n");
180 	} else if(!status.reachable) {
181 		printf("unreachable\n");
182 	} else if(strcmp(via, item)) {
183 		printf("indirectly via %s\n", via);
184 	} else if(!status.validkey) {
185 		printf("unknown\n");
186 	} else if(minmtu > 0) {
187 		printf("directly with UDP\nPMTU:         %d\n", pmtu);
188 
189 		if(udp_ping_rtt != -1) {
190 			printf("RTT:          %d.%03d\n", udp_ping_rtt / 1000, udp_ping_rtt % 1000);
191 		}
192 	} else if(!strcmp(nexthop, item)) {
193 		printf("directly with TCP\n");
194 	} else {
195 		printf("none, forwarded via %s\n", nexthop);
196 	}
197 
198 	printf("RX:           %"PRIu64" packets  %"PRIu64" bytes\n", in_packets, in_bytes);
199 	printf("TX:           %"PRIu64" packets  %"PRIu64" bytes\n", out_packets, out_bytes);
200 
201 	// List edges
202 	printf("Edges:       ");
203 	sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_EDGES, item);
204 
205 	while(recvline(fd, line, sizeof(line))) {
206 		int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, from, to);
207 
208 		if(n == 2) {
209 			break;
210 		}
211 
212 		if(n != 4) {
213 			fprintf(stderr, "Unable to parse edge dump from tincd.\n%s\n", line);
214 			return 1;
215 		}
216 
217 		if(!strcmp(from, item)) {
218 			printf(" %s", to);
219 		}
220 	}
221 
222 	printf("\n");
223 
224 	// List subnets
225 	printf("Subnets:     ");
226 	sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
227 
228 	while(recvline(fd, line, sizeof(line))) {
229 		int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, subnet, from);
230 
231 		if(n == 2) {
232 			break;
233 		}
234 
235 		if(n != 4) {
236 			fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
237 			return 1;
238 		}
239 
240 		if(!strcmp(from, item)) {
241 			printf(" %s", strip_weight(subnet));
242 		}
243 	}
244 
245 	printf("\n");
246 
247 	return 0;
248 }
249 
info_subnet(int fd,const char * item)250 static int info_subnet(int fd, const char *item) {
251 	subnet_t subnet, find;
252 
253 	if(!str2net(&find, item)) {
254 		fprintf(stderr, "Could not parse subnet or address '%s'.\n", item);
255 		return 1;
256 	}
257 
258 	bool address = !strchr(item, '/');
259 	bool weight = strchr(item, '#');
260 	bool found = false;
261 
262 	char line[4096];
263 	char netstr[4096];
264 	char owner[4096];
265 
266 	int code, req;
267 
268 	sendline(fd, "%d %d %s", CONTROL, REQ_DUMP_SUBNETS, item);
269 
270 	while(recvline(fd, line, sizeof(line))) {
271 		int n = sscanf(line, "%d %d %4095s %4095s", &code, &req, netstr, owner);
272 
273 		if(n == 2) {
274 			break;
275 		}
276 
277 		if(n != 4 || !str2net(&subnet, netstr)) {
278 			fprintf(stderr, "Unable to parse subnet dump from tincd.\n");
279 			return 1;
280 		}
281 
282 		if(find.type != subnet.type) {
283 			continue;
284 		}
285 
286 		if(weight) {
287 			if(find.weight != subnet.weight) {
288 				continue;
289 			}
290 		}
291 
292 		if(find.type == SUBNET_IPV4) {
293 			if(address) {
294 				if(maskcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, subnet.net.ipv4.prefixlength)) {
295 					continue;
296 				}
297 			} else {
298 				if(find.net.ipv4.prefixlength != subnet.net.ipv4.prefixlength) {
299 					continue;
300 				}
301 
302 				if(memcmp(&find.net.ipv4.address, &subnet.net.ipv4.address, sizeof(subnet.net.ipv4))) {
303 					continue;
304 				}
305 			}
306 		} else if(find.type == SUBNET_IPV6) {
307 			if(address) {
308 				if(maskcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, subnet.net.ipv6.prefixlength)) {
309 					continue;
310 				}
311 			} else {
312 				if(find.net.ipv6.prefixlength != subnet.net.ipv6.prefixlength) {
313 					continue;
314 				}
315 
316 				if(memcmp(&find.net.ipv6.address, &subnet.net.ipv6.address, sizeof(subnet.net.ipv6))) {
317 					continue;
318 				}
319 			}
320 		}
321 
322 		if(find.type == SUBNET_MAC) {
323 			if(memcmp(&find.net.mac.address, &subnet.net.mac.address, sizeof(subnet.net.mac))) {
324 				continue;
325 			}
326 		}
327 
328 		found = true;
329 		printf("Subnet: %s\n", strip_weight(netstr));
330 		printf("Owner:  %s\n", owner);
331 	}
332 
333 	if(!found) {
334 		if(address) {
335 			fprintf(stderr, "Unknown address %s.\n", item);
336 		} else {
337 			fprintf(stderr, "Unknown subnet %s.\n", item);
338 		}
339 
340 		return 1;
341 	}
342 
343 	return 0;
344 }
345 
info(int fd,const char * item)346 int info(int fd, const char *item) {
347 	if(check_id(item)) {
348 		return info_node(fd, item);
349 	}
350 
351 	if(strchr(item, '.') || strchr(item, ':')) {
352 		return info_subnet(fd, item);
353 	}
354 
355 	fprintf(stderr, "Argument is not a node name, subnet or address.\n");
356 	return 1;
357 }
358