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