README, INSTALL: minor: Remove trailing whitespaces
[netsniff-ng.git] / bpfc.c
blob4ba056349d0c383c11802d770ee2ac6afbecd3df
1 /*
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.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <sys/fsuid.h>
16 #include "xmalloc.h"
17 #include "die.h"
18 #include "bpf.h"
20 static const char *short_options = "vhi:Vdbf:p";
21 static const struct option long_options[] = {
22 {"input", required_argument, NULL, 'i'},
23 {"format", required_argument, NULL, 'f'},
24 {"cpp", no_argument, NULL, 'p'},
25 {"verbose", no_argument, NULL, 'V'},
26 {"bypass", no_argument, NULL, 'b'},
27 {"dump", no_argument, NULL, 'd'},
28 {"version", no_argument, NULL, 'v'},
29 {"help", no_argument, NULL, 'h'},
30 {NULL, 0, NULL, 0}
33 extern int compile_filter(char *file, int verbose, int bypass, int format,
34 bool invoke_cpp);
36 static void __noreturn help(void)
38 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
39 puts("http://www.netsniff-ng.org\n\n"
40 "Usage: bpfc [options] || bpfc <program>\n"
41 "Options:\n"
42 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
43 " -p|--cpp Run bpf program through C preprocessor\n"
44 " -f|--format <format> Output format: C|netsniff-ng|xt_bpf|tcpdump\n"
45 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
46 " -V|--verbose Be more verbose\n"
47 " -d|--dump Dump supported instruction table\n"
48 " -v|--version Print version and exit\n"
49 " -h|--help Print this help and exit\n\n"
50 "Examples:\n"
51 " bpfc fubar\n"
52 " bpfc fubar > foo (bpfc -f C -i fubar > foo) --> netsniff-ng -f foo ...\n"
53 " bpfc -f tcpdump -i fubar > foo --> tcpdump -ddd like ...\n"
54 " bpfc -f xt_bpf -b -p -i fubar\n"
55 " iptables -A INPUT -m bpf --bytecode \"`./bpfc -f xt_bpf -i fubar`\" -j LOG\n"
56 " bpfc - (read from stdin)\n"
57 "Note:\n"
58 " Generation of seccomp-BPF filters are fully supported as well.\n\n"
59 "Please report bugs to <bugs@netsniff-ng.org>\n"
60 "Copyright (C) 2011-2013 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 __noreturn version(void)
70 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_LONG);
71 puts("http://www.netsniff-ng.org\n\n"
72 "Please report bugs to <bugs@netsniff-ng.org>\n"
73 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
74 "Swiss federal institute of technology (ETH Zurich)\n"
75 "License: GNU GPL version 2.0\n"
76 "This is free software: you are free to change and redistribute it.\n"
77 "There is NO WARRANTY, to the extent permitted by law.\n");
78 die();
81 int main(int argc, char **argv)
83 int ret, verbose = 0, c, opt_index, bypass = 0, format = 0;
84 bool invoke_cpp = false;
85 char *file = NULL;
87 setfsuid(getuid());
88 setfsgid(getgid());
90 if (argc == 1)
91 help();
93 while ((c = getopt_long(argc, argv, short_options,
94 long_options, &opt_index)) != EOF) {
95 switch (c) {
96 case 'h':
97 help();
98 break;
99 case 'v':
100 version();
101 break;
102 case 'V':
103 verbose = 1;
104 break;
105 case 'p':
106 invoke_cpp = true;
107 break;
108 case 'f':
109 if (!strncmp(optarg, "C", 1) ||
110 !strncmp(optarg, "netsniff-ng", 11))
111 format = 0;
112 else if (!strncmp(optarg, "xt_bpf", 6))
113 format = 1;
114 else if (!strncmp(optarg, "tcpdump", 7))
115 format = 2;
116 else
117 help();
118 break;
119 case 'b':
120 bypass = 1;
121 break;
122 case 'd':
123 bpf_dump_op_table();
124 die();
125 case 'i':
126 file = xstrdup(optarg);
127 break;
128 case '?':
129 switch (optopt) {
130 case 'i':
131 case 'f':
132 panic("Option -%c requires an argument!\n",
133 optopt);
134 default:
135 if (isprint(optopt))
136 printf("Unknown option character `0x%X\'!\n", optopt);
137 die();
139 default:
140 break;
144 if (argc == 2)
145 file = xstrdup(argv[1]);
146 if (!file)
147 panic("No Berkeley Packet Filter program specified!\n");
149 ret = compile_filter(file, verbose, bypass, format, invoke_cpp);
151 xfree(file);
152 return ret;