docs: update on todo for website
[netsniff-ng.git] / src / bpfc.c
blobec171399573bec832e8af89ba813115f75fa92a4
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
8 * This is a tiny Berkeley Packet Filter compiler that understands the
9 * Syntax / Semantic from the USENIX paper {"The BSD Packet Filter: A New
10 * Architecture for User-level Packet Capture", McCanne, Steven and
11 * Jacobson, Van, Lawrence Berkeley Laboratory}. With this, BPFs can be
12 * written the good old way and understood by the Linux kernel and *BSD
13 * kernels where Berkeley Packet Filters are used.
15 * The one small garden of a free gardener was all his need and due, not
16 * a garden swollen to a realm; his own hands to use, not the hands of
17 * others to command.
19 * -- The Lord of the Rings, Sam, Chapter 'The Tower of Cirith Ungol'.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <ctype.h>
27 #include "xmalloc.h"
28 #include "die.h"
29 #include "bpf.h"
31 static const char *short_options = "vhi:Vdb";
32 static const struct option long_options[] = {
33 {"input", required_argument, NULL, 'i'},
34 {"verbose", no_argument, NULL, 'V'},
35 {"bypass", no_argument, NULL, 'b'},
36 {"dump", no_argument, NULL, 'd'},
37 {"version", no_argument, NULL, 'v'},
38 {"help", no_argument, NULL, 'h'},
39 {NULL, 0, NULL, 0}
42 extern int compile_filter(char *file, int verbose, int bypass);
44 static void help(void)
46 printf("\n%s %s, a tiny BPF compiler\n",
47 PROGNAME_STRING, VERSION_STRING);
48 puts("http://www.netsniff-ng.org\n\n"
49 "Usage: bpfc [options] || bpfc <program>\n"
50 "Options:\n"
51 " -i|--input <program> Berkeley Packet Filter file\n"
52 " -V|--verbose Be more verbose\n"
53 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
54 " -d|--dump Dump supported instruction table\n"
55 " -v|--version Print version\n"
56 " -h|--help Print this help\n\n"
57 "Examples:\n"
58 " bpfc -i fubar.bpf\n\n"
59 "Please report bugs to <bugs@netsniff-ng.org>\n"
60 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
61 "Swiss federal institute of technology (ETH Zurich)\n"
62 "License: GNU GPL version 2.0\n"
63 "This is free software: you are free to change and redistribute it.\n"
64 "There is NO WARRANTY, to the extent permitted by law.\n");
65 die();
68 static void version(void)
70 printf("\n%s %s, a tiny BPF compiler\n",
71 PROGNAME_STRING, VERSION_STRING);
72 puts("http://www.netsniff-ng.org\n\n"
73 "Please report bugs to <bugs@netsniff-ng.org>\n"
74 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
75 "Swiss federal institute of technology (ETH Zurich)\n"
76 "License: GNU GPL version 2.0\n"
77 "This is free software: you are free to change and redistribute it.\n"
78 "There is NO WARRANTY, to the extent permitted by law.\n");
79 die();
82 int main(int argc, char **argv)
84 int ret, verbose = 0, c, opt_index, bypass = 0;
85 char *file = NULL;
87 if (argc == 1)
88 help();
90 while ((c = getopt_long(argc, argv, short_options,
91 long_options, &opt_index)) != EOF) {
92 switch (c) {
93 case 'h':
94 help();
95 break;
96 case 'v':
97 version();
98 break;
99 case 'V':
100 verbose = 1;
101 break;
102 case 'b':
103 bypass = 1;
104 break;
105 case 'd':
106 bpf_dump_op_table();
107 die();
108 case 'i':
109 file = xstrdup(optarg);
110 break;
111 case '?':
112 switch (optopt) {
113 case 'i':
114 panic("Option -%c requires an argument!\n",
115 optopt);
116 default:
117 if (isprint(optopt))
118 whine("Unknown option character "
119 "`0x%X\'!\n", optopt);
120 die();
122 default:
123 break;
126 if (argc == 2)
127 file = xstrdup(argv[1]);
129 if (!file)
130 panic("No Berkeley Packet Filter program specified!\n");
132 ret = compile_filter(file, verbose, bypass);
134 xfree(file);
135 return ret;