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