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