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