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