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