2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
4 * Swiss federal institute of technology (ETH Zurich)
5 * Subject to the GPL, version 2.
14 #include <sys/fsuid.h>
22 static const char *short_options
= "vhi:Vdbf:pD:";
23 static const struct option long_options
[] = {
24 {"input", required_argument
, NULL
, 'i'},
25 {"format", required_argument
, NULL
, 'f'},
26 {"cpp", no_argument
, NULL
, 'p'},
27 {"define", required_argument
, NULL
, 'D'},
28 {"verbose", no_argument
, NULL
, 'V'},
29 {"bypass", no_argument
, NULL
, 'b'},
30 {"dump", no_argument
, NULL
, 'd'},
31 {"version", no_argument
, NULL
, 'v'},
32 {"help", no_argument
, NULL
, 'h'},
36 static const char *copyright
=
37 "Please report bugs at https://github.com/netsniff-ng/netsniff-ng/issues\n"
38 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
39 "Swiss federal institute of technology (ETH Zurich)\n"
40 "License: GNU GPL version 2.0\n"
41 "This is free software: you are free to change and redistribute it.\n"
42 "There is NO WARRANTY, to the extent permitted by law.";
44 extern int compile_filter(char *file
, bool verbose
, int bypass
, int format
,
45 bool invoke_cpp
, char **cpp_argv
);
47 static void __noreturn
help(void)
49 printf("bpfc %s, a tiny BPF compiler\n", VERSION_STRING
);
50 puts("http://www.netsniff-ng.org\n\n"
51 "Usage: bpfc [options] || bpfc <program>\n"
53 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
54 " -p|--cpp Run bpf program through C preprocessor\n"
55 " -D|--define Add macro/define for C preprocessor\n"
56 " -f|--format <format> Output format: C|netsniff-ng|xt_bpf|tcpdump\n"
57 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
58 " -V|--verbose Be more verbose\n"
59 " -d|--dump Dump supported instruction table\n"
60 " -v|--version Print version and exit\n"
61 " -h|--help Print this help and exit\n\n"
64 " bpfc fubar > foo (bpfc -f C -i fubar > foo) --> netsniff-ng -f foo ...\n"
65 " bpfc -f tcpdump -i fubar > foo --> tcpdump -ddd like ...\n"
66 " bpfc -f xt_bpf -b -p -i fubar\n"
67 " iptables -A INPUT -m bpf --bytecode \"`./bpfc -f xt_bpf -i fubar`\" -j LOG\n"
68 " bpfc - (read from stdin)\n"
70 " Generation of seccomp-BPF filters are fully supported as well.\n");
75 static void __noreturn
version(void)
77 printf("bpfc %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
78 puts("a tiny BPF compiler\n"
79 "http://www.netsniff-ng.org\n");
84 int main(int argc
, char **argv
)
86 int ret
, c
, bypass
= 0, format
= 0;
87 bool verbose
= false, invoke_cpp
= false;
88 char **cpp_argv
= NULL
;
98 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
114 cpp_argv
= argv_insert(cpp_argv
, &cpp_argc
, "-D");
115 cpp_argv
= argv_insert(cpp_argv
, &cpp_argc
, optarg
);
118 if (!strncmp(optarg
, "C", 1) ||
119 !strncmp(optarg
, "netsniff-ng", 11))
121 else if (!strncmp(optarg
, "tcpdump", 7))
123 else if (!strncmp(optarg
, "xt_bpf", 6) ||
124 !strncmp(optarg
, "tc", 2))
136 file
= xstrdup(optarg
);
142 panic("Option -%c requires an argument!\n",
146 printf("Unknown option character `0x%X\'!\n", optopt
);
155 file
= xstrdup(argv
[1]);
157 panic("No Berkeley Packet Filter program specified!\n");
159 ret
= compile_filter(file
, verbose
, bypass
, format
, invoke_cpp
, cpp_argv
);