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