20fc991fd88899570ad16218069f228598586d83
[tinc] / src / tincctl.c
1 /*
2     tincctl.c -- Controlling a running tincd
3     Copyright (C) 2007-2011 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 <getopt.h>
23
24 #include "xalloc.h"
25 #include "protocol.h"
26 #include "control_common.h"
27 #include "rsagen.h"
28 #include "utils.h"
29 #include "tincctl.h"
30 #include "top.h"
31
32 /* The name this program was run with. */
33 static char *program_name = NULL;
34
35 /* If nonzero, display usage information and exit. */
36 static bool show_help = false;
37
38 /* If nonzero, print the version on standard output and exit.  */
39 static bool show_version = false;
40
41 /* If nonzero, it will attempt to kill a running tincd and exit. */
42 static int kill_tincd = 0;
43
44 /* If nonzero, generate public/private keypair for this host/net. */
45 static int generate_keys = 0;
46
47 static char *name = NULL;
48 static char *identname = NULL;                          /* program name for syslog */
49 static char *controlcookiename = NULL;                  /* cookie file location */
50 static char controlcookie[1024];
51 char *netname = NULL;
52 char *confbase = NULL;
53 static char *host = NULL;
54
55 #ifdef HAVE_MINGW
56 static struct WSAData wsa_state;
57 #endif
58
59 static struct option const long_options[] = {
60         {"config", required_argument, NULL, 'c'},
61         {"host", required_argument, NULL, 'h'},
62         {"net", required_argument, NULL, 'n'},
63         {"help", no_argument, NULL, 1},
64         {"version", no_argument, NULL, 2},
65         {"controlcookie", required_argument, NULL, 5},
66         {NULL, 0, NULL, 0}
67 };
68
69 static void usage(bool status) {
70         if(status)
71                 fprintf(stderr, "Try `%s --help\' for more information.\n",
72                                 program_name);
73         else {
74                 printf("Usage: %s [options] command\n\n", program_name);
75                 printf("Valid options are:\n"
76                                 "  -c, --config=DIR              Read configuration options from DIR.\n"
77                                 "  -n, --net=NETNAME             Connect to net NETNAME.\n"
78                                 "      --controlcookie=FILENAME  Read control socket from FILENAME.\n"
79                                 "      --help                    Display this help and exit.\n"
80                                 "      --version                 Output version information and exit.\n"
81                                 "\n"
82                                 "Valid commands are:\n"
83                                 "  start                      Start tincd.\n"
84                                 "  stop                       Stop tincd.\n"
85                                 "  restart                    Restart tincd.\n"
86                                 "  reload                     Reload configuration of running tincd.\n"
87                                 "  pid                        Show PID of currently running tincd.\n"
88                                 "  generate-keys [bits]       Generate a new public/private keypair.\n"
89                                 "  dump                       Dump a list of one of the following things:\n"
90                                 "    nodes                    - all known nodes in the VPN\n"
91                                 "    edges                    - all known connections in the VPN\n"
92                                 "    subnets                  - all known subnets in the VPN\n"
93                                 "    connections              - all meta connections with ourself\n"
94                                 "    graph                    - graph of the VPN in dotty format\n"
95                                 "  purge                      Purge unreachable nodes\n"
96                                 "  debug N                    Set debug level\n"
97                                 "  retry                      Retry all outgoing connections\n"
98                                 "  reload                     Partial reload of configuration\n"
99                                 "  disconnect NODE            Close meta connection with NODE\n"
100 #ifdef HAVE_CURSES
101                                 "  top                        Show real-time statistics\n"
102 #endif
103                                 "  pcap                       Dump traffic in pcap format\n"
104                                 "\n");
105                 printf("Report bugs to tinc@tinc-vpn.org.\n");
106         }
107 }
108
109 static bool parse_options(int argc, char **argv) {
110         int r;
111         int option_index = 0;
112
113         while((r = getopt_long(argc, argv, "c:n:h:", long_options, &option_index)) != EOF) {
114                 switch (r) {
115                         case 0:                         /* long option */
116                                 break;
117
118                         case 'c':                               /* config file */
119                                 confbase = xstrdup(optarg);
120                                 break;
121
122                         case 'h':                               /* alternative host to connect to */
123                                 host = xstrdup(optarg);
124                                 break;
125
126                         case 'n':                               /* net name given */
127                                 netname = xstrdup(optarg);
128                                 break;
129
130                         case 1:                                 /* show help */
131                                 show_help = true;
132                                 break;
133
134                         case 2:                                 /* show version */
135                                 show_version = true;
136                                 break;
137
138                         case 5:                                 /* open control socket here */
139                                 controlcookiename = xstrdup(optarg);
140                                 break;
141
142                         case '?':
143                                 usage(true);
144                                 return false;
145
146                         default:
147                                 break;
148                 }
149         }
150
151         return true;
152 }
153
154 FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
155         FILE *r;
156         char *directory;
157         char buf[PATH_MAX];
158         char buf2[PATH_MAX];
159         size_t len;
160
161         /* Check stdin and stdout */
162         if(isatty(0) && isatty(1)) {
163                 /* Ask for a file and/or directory name. */
164                 fprintf(stdout, "Please enter a file to save %s to [%s]: ",
165                                 what, filename);
166                 fflush(stdout);
167
168                 if(fgets(buf, sizeof buf, stdin) == NULL) {
169                         fprintf(stderr, "Error while reading stdin: %s\n",
170                                         strerror(errno));
171                         return NULL;
172                 }
173
174                 len = strlen(buf);
175                 if(len)
176                         buf[--len] = 0;
177
178                 if(len)
179                         filename = buf;
180         }
181
182 #ifdef HAVE_MINGW
183         if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
184 #else
185         if(filename[0] != '/') {
186 #endif
187                 /* The directory is a relative path or a filename. */
188                 directory = get_current_dir_name();
189                 snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
190                 filename = buf2;
191         }
192
193         umask(0077);                            /* Disallow everything for group and other */
194
195         /* Open it first to keep the inode busy */
196
197         r = fopen(filename, mode);
198
199         if(!r) {
200                 fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
201                 return NULL;
202         }
203
204         return r;
205 }
206
207 /*
208   Generate a public/private RSA keypair, and ask for a file to store
209   them in.
210 */
211 static bool keygen(int bits) {
212         rsa_t key;
213         FILE *f;
214         char *filename;
215
216         fprintf(stderr, "Generating %d bits keys:\n", bits);
217
218         if(!rsa_generate(&key, bits, 0x10001)) {
219                 fprintf(stderr, "Error during key generation!\n");
220                 return false;
221         } else
222                 fprintf(stderr, "Done.\n");
223
224         xasprintf(&filename, "%s/rsa_key.priv", confbase);
225         f = ask_and_open(filename, "private RSA key", "a");
226
227         if(!f)
228                 return false;
229   
230 #ifdef HAVE_FCHMOD
231         /* Make it unreadable for others. */
232         fchmod(fileno(f), 0600);
233 #endif
234                 
235         if(ftell(f))
236                 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
237
238         rsa_write_pem_private_key(&key, f);
239
240         fclose(f);
241         free(filename);
242
243         if(name)
244                 xasprintf(&filename, "%s/hosts/%s", confbase, name);
245         else
246                 xasprintf(&filename, "%s/rsa_key.pub", confbase);
247
248         f = ask_and_open(filename, "public RSA key", "a");
249
250         if(!f)
251                 return false;
252
253         if(ftell(f))
254                 fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
255
256         rsa_write_pem_public_key(&key, f);
257
258         fclose(f);
259         free(filename);
260
261         return true;
262 }
263
264 /*
265   Set all files and paths according to netname
266 */
267 static void make_names(void) {
268 #ifdef HAVE_MINGW
269         HKEY key;
270         char installdir[1024] = "";
271         long len = sizeof installdir;
272 #endif
273
274         if(netname)
275                 xasprintf(&identname, "tinc.%s", netname);
276         else
277                 identname = xstrdup("tinc");
278
279 #ifdef HAVE_MINGW
280         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
281                 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
282                         if(!confbase) {
283                                 if(netname)
284                                         xasprintf(&confbase, "%s/%s", installdir, netname);
285                                 else
286                                         xasprintf(&confbase, "%s", installdir);
287                         }
288                 }
289                 if(!controlcookiename)
290                         xasprintf(&controlcookiename, "%s/cookie", confbase);
291                 RegCloseKey(key);
292                 if(*installdir)
293                         return;
294         }
295 #endif
296
297         if(!controlcookiename)
298                 xasprintf(&controlcookiename, "%s/run/%s.cookie", LOCALSTATEDIR, identname);
299
300         if(netname) {
301                 if(!confbase)
302                         xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
303                 else
304                         fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
305         } else {
306                 if(!confbase)
307                         xasprintf(&confbase, CONFDIR "/tinc");
308         }
309 }
310
311 static char buffer[4096];
312 static size_t blen = 0;
313
314 bool recvline(int fd, char *line, size_t len) {
315         char *newline = NULL;
316
317         while(!(newline = memchr(buffer, '\n', blen))) {
318                 int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
319                 if(result == -1 && errno == EINTR)
320                         continue;
321                 else if(result <= 0)
322                         return false;
323                 blen += result;
324         }
325
326         if(newline - buffer >= len)
327                 return false;
328
329         len = newline - buffer;
330
331         memcpy(line, buffer, len);
332         line[len] = 0;
333         memmove(buffer, newline + 1, blen - len - 1);
334         blen -= len + 1;
335
336         return true;
337 }
338
339 bool recvdata(int fd, char *data, size_t len) {
340         while(blen < len) {
341                 int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
342                 if(result == -1 && errno == EINTR)
343                         continue;
344                 else if(result <= 0)
345                         return false;
346                 blen += result;
347         }
348
349         memcpy(data, buffer, len);
350         memmove(buffer, buffer + len, blen - len);
351         blen -= len;
352
353         return true;
354 }
355
356 bool sendline(int fd, char *format, ...) {
357         static char buffer[4096];
358         char *p = buffer;
359         size_t blen = 0;
360         va_list ap;
361
362         va_start(ap, format);
363         blen = vsnprintf(buffer, sizeof buffer, format, ap);
364         va_end(ap);
365
366         if(blen < 0 || blen >= sizeof buffer)
367                 return false;
368
369         buffer[blen] = '\n';
370         blen++;
371
372         while(blen) {
373                 int result = send(fd, p, blen, 0);
374                 if(result == -1 && errno == EINTR)
375                         continue;
376                 else if(result <= 0);
377                         return false;
378                 p += result;
379                 blen -= result;
380         }
381
382         return true;    
383 }
384
385 void pcap(int fd, FILE *out) {
386         sendline(fd, "%d %d", CONTROL, REQ_PCAP);
387         char data[9018];
388
389         struct {
390                 uint32_t magic;
391                 uint16_t major;
392                 uint16_t minor;
393                 uint32_t tz_offset;
394                 uint32_t tz_accuracy;
395                 uint32_t snaplen;
396                 uint32_t ll_type;
397         } header = {
398                 0xa1b2c3d4,
399                 2, 4,
400                 0, 0,
401                 sizeof data,
402                 1,
403         };
404
405         struct {
406                 uint32_t tv_sec;
407                 uint32_t tv_usec;
408                 uint32_t len;
409                 uint32_t origlen;
410         } packet;
411
412         struct timeval tv;
413
414         fwrite(&header, sizeof header, 1, out);
415         fflush(out);
416
417         char line[32];
418         while(recvline(fd, line, sizeof line)) {
419                 int code, req, len;
420                 int n = sscanf(line, "%d %d %d", &code, &req, &len);
421                 gettimeofday(&tv, NULL);
422                 if(n != 3 || code != CONTROL || req != REQ_PCAP || len < 0 || len > sizeof data)
423                         break;
424                 if(!recvdata(fd, data, len))
425                         break;
426                 packet.tv_sec = tv.tv_sec;
427                 packet.tv_usec = tv.tv_usec;
428                 packet.len = len;
429                 packet.origlen = len;
430                 fwrite(&packet, sizeof packet, 1, out);
431                 fwrite(data, len, 1, out);
432                 fflush(out);
433         }
434 }
435
436 int main(int argc, char *argv[], char *envp[]) {
437         int fd;
438         int result;
439         char port[128];
440         int pid;
441
442         program_name = argv[0];
443
444         if(!parse_options(argc, argv))
445                 return 1;
446         
447         make_names();
448
449         if(show_version) {
450                 printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
451                            VERSION, __DATE__, __TIME__, PROT_CURRENT);
452                 printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
453                                 "See the AUTHORS file for a complete list.\n\n"
454                                 "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
455                                 "and you are welcome to redistribute it under certain conditions;\n"
456                                 "see the file COPYING for details.\n");
457
458                 return 0;
459         }
460
461         if(show_help) {
462                 usage(false);
463                 return 0;
464         }
465
466         if(optind >= argc) {
467                 fprintf(stderr, "Not enough arguments.\n");
468                 usage(true);
469                 return 1;
470         }
471
472         // First handle commands that don't involve connecting to a running tinc daemon.
473
474         if(!strcasecmp(argv[optind], "generate-keys")) {
475                 return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
476         }
477
478         if(!strcasecmp(argv[optind], "start")) {
479                 argv[optind] = NULL;
480                 execve(SBINDIR "/tincd", argv, envp);
481                 fprintf(stderr, "Could not start tincd: %s", strerror(errno));
482                 return 1;
483         }
484
485         /*
486          * Now handle commands that do involve connecting to a running tinc daemon.
487          * Authenticate the server by ensuring the parent directory can be
488          * traversed only by root. Note this is not totally race-free unless all
489          * ancestors are writable only by trusted users, which we don't verify.
490          */
491
492         FILE *f = fopen(controlcookiename, "r");
493         if(!f) {
494                 fprintf(stderr, "Could not open control socket cookie file %s: %s\n", controlcookiename, strerror(errno));
495                 return 1;
496         }
497         if(fscanf(f, "%1024s %128s %d", controlcookie, port, &pid) != 3) {
498                 fprintf(stderr, "Could not parse control socket cookie file %s\n", controlcookiename);
499                 return 1;
500         }
501
502 #ifdef HAVE_MINGW
503         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
504                 fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
505                 return 1;
506         }
507 #endif
508
509         struct addrinfo hints = {
510                 .ai_family = AF_UNSPEC,
511                 .ai_socktype = SOCK_STREAM,
512                 .ai_protocol = IPPROTO_TCP,
513                 .ai_flags = 0,
514         };
515
516         struct addrinfo *res = NULL;
517
518         if(getaddrinfo(host, port, &hints, &res) || !res) {
519                 fprintf(stderr, "Cannot resolve %s port %s: %s", host ?: "localhost", port, strerror(errno));
520                 return 1;
521         }
522
523         fd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
524         if(fd < 0) {
525                 fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno));
526                 return 1;
527         }
528
529 #ifdef HAVE_MINGW
530         unsigned long arg = 0;
531
532         if(ioctlsocket(fd, FIONBIO, &arg) != 0) {
533                 fprintf(stderr, "ioctlsocket failed: %s", sockstrerror(sockerrno));
534         }
535 #endif
536
537         if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
538                 fprintf(stderr, "Cannot connect to %s port %s: %s\n", host ?: "localhost", port, sockstrerror(sockerrno));
539                 return 1;
540         }
541
542         freeaddrinfo(res);
543
544         char line[4096];
545         char data[4096];
546         int code, version, req;
547
548         if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) {
549                 fprintf(stderr, "Cannot read greeting from control socket: %s\n",
550                                 sockstrerror(sockerrno));
551                 return 1;
552         }
553
554         sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT);
555         
556         if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) {
557                 fprintf(stderr, "Could not fully establish control socket connection\n");
558                 return 1;
559         }
560
561         if(!strcasecmp(argv[optind], "pid")) {
562                 printf("%d\n", pid);
563                 return 0;
564         }
565
566         if(!strcasecmp(argv[optind], "stop")) {
567                 sendline(fd, "%d %d", CONTROL, REQ_STOP);
568                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_STOP || result) {
569                         fprintf(stderr, "Could not stop tinc daemon\n");
570                         return 1;
571                 }
572                 return 0;
573         }
574
575         if(!strcasecmp(argv[optind], "reload")) {
576                 sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
577                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RELOAD || result) {
578                         fprintf(stderr, "Could not reload tinc daemon\n");
579                         return 1;
580                 }
581                 return 0;
582         }
583
584         if(!strcasecmp(argv[optind], "restart")) {
585                 sendline(fd, "%d %d", CONTROL, REQ_RESTART);
586                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RESTART || result) {
587                         fprintf(stderr, "Could not restart tinc daemon\n");
588                         return 1;
589                 }
590                 return 0;
591         }
592
593         if(!strcasecmp(argv[optind], "retry")) {
594                 sendline(fd, "%d %d", CONTROL, REQ_RETRY);
595                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RETRY || result) {
596                         fprintf(stderr, "Could not retry outgoing connections\n");
597                         return 1;
598                 }
599                 return 0;
600         }
601
602         if(!strcasecmp(argv[optind], "dump")) {
603                 if(argc < optind + 2) {
604                         fprintf(stderr, "Not enough arguments.\n");
605                         usage(true);
606                         return 1;
607                 }
608
609                 bool do_graph = false;
610
611                 if(!strcasecmp(argv[optind+1], "nodes"))
612                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
613                 else if(!strcasecmp(argv[optind+1], "edges"))
614                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
615                 else if(!strcasecmp(argv[optind+1], "subnets"))
616                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
617                 else if(!strcasecmp(argv[optind+1], "connections"))
618                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
619                 else if(!strcasecmp(argv[optind+1], "graph")) {
620                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
621                         sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
622                         do_graph = true;
623                         printf("digraph {\n");
624                 } else {
625                         fprintf(stderr, "Unknown dump type '%s'.\n", argv[optind+1]);
626                         usage(true);
627                         return 1;
628                 }
629
630                 while(recvline(fd, line, sizeof line)) {
631                         char node1[4096], node2[4096];
632                         int n = sscanf(line, "%d %d %s to %s", &code, &req, node1, node2);
633                         if(n == 2) {
634                                 if(do_graph && req == REQ_DUMP_NODES)
635                                         continue;
636                                 else {
637                                         if(do_graph)
638                                                 printf("}\n");
639                                         return 0;
640                                 }
641                         }
642                         if(n < 2)
643                                 break;
644
645                         if(!do_graph)
646                                 printf("%s\n", line + 5);
647                         else {
648                                 if(req == REQ_DUMP_NODES)
649                                         printf(" %s [label = \"%s\"];\n", node1, node1);
650                                 else
651                                         printf(" %s -> %s;\n", node1, node2);
652                         }
653                 }
654
655                 fprintf(stderr, "Error receiving dump\n");
656                 return 1;
657         }
658
659         if(!strcasecmp(argv[optind], "purge")) {
660                 sendline(fd, "%d %d", CONTROL, REQ_PURGE);
661                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_PURGE || result) {
662                         fprintf(stderr, "Could not purge tinc daemon\n");
663                         return 1;
664                 }
665                 return 0;
666         }
667
668         if(!strcasecmp(argv[optind], "debug")) {
669                 int debuglevel, origlevel;
670
671                 if(argc != optind + 2) {
672                         fprintf(stderr, "Invalid arguments.\n");
673                         return 1;
674                 }
675                 debuglevel = atoi(argv[optind+1]);
676
677                 sendline(fd, "%d %d %d", CONTROL, REQ_SET_DEBUG, debuglevel);
678                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &origlevel) != 3 || code != CONTROL || req != REQ_SET_DEBUG) {
679                         fprintf(stderr, "Could not purge tinc daemon\n");
680                         return 1;
681                 }
682
683                 fprintf(stderr, "Old level %d, new level %d\n", origlevel, debuglevel);
684                 return 0;
685         }
686
687         if(!strcasecmp(argv[optind], "connect")) {
688                 if(argc != optind + 2) {
689                         fprintf(stderr, "Invalid arguments.\n");
690                         return 1;
691                 }
692                 char *name = argv[optind + 1];
693
694                 sendline(fd, "%d %d %s", CONTROL, REQ_CONNECT, name);
695                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_CONNECT || result) {
696                         fprintf(stderr, "Could not connect to %s\n", name);
697                         return 1;
698                 }
699                 return 0;
700         }
701
702         if(!strcasecmp(argv[optind], "disconnect")) {
703                 if(argc != optind + 2) {
704                         fprintf(stderr, "Invalid arguments.\n");
705                         return 1;
706                 }
707                 char *name = argv[optind + 1];
708
709                 sendline(fd, "%d %d %s", CONTROL, REQ_DISCONNECT, name);
710                 if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_DISCONNECT || result) {
711                         fprintf(stderr, "Could not disconnect %s\n", name);
712                         return 1;
713                 }
714                 return 0;
715         }
716
717 #ifdef HAVE_CURSES
718         if(!strcasecmp(argv[optind], "top")) {
719                 top(fd);
720                 return 0;
721         }
722 #endif
723
724         if(!strcasecmp(argv[optind], "pcap")) {
725                 pcap(fd, stdout);
726                 return 0;
727         }
728
729         fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);
730         usage(true);
731         
732         close(fd);
733
734         return 0;
735 }