ring: Move common ring block_r/frame_nr shrinking to generic function
[netsniff-ng-new.git] / bpfc.c
blobd360cf5e3c8d3c0ca2e2b6da5a60e60cca7922f9
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 static const char *copyright = "Please report bugs to <netsniff-ng@googlegroups.com>\n"
35 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
36 "Swiss federal institute of technology (ETH Zurich)\n"
37 "License: GNU GPL version 2.0\n"
38 "This is free software: you are free to change and redistribute it.\n"
39 "There is NO WARRANTY, to the extent permitted by law.";
41 extern int compile_filter(char *file, int verbose, int bypass, int format,
42 bool invoke_cpp);
44 static void __noreturn help(void)
46 printf("bpfc %s, a tiny BPF compiler\n", VERSION_STRING);
47 puts("http://www.netsniff-ng.org\n\n"
48 "Usage: bpfc [options] || bpfc <program>\n"
49 "Options:\n"
50 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
51 " -p|--cpp Run bpf program through C preprocessor\n"
52 " -f|--format <format> Output format: C|netsniff-ng|xt_bpf|tcpdump\n"
53 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
54 " -V|--verbose Be more verbose\n"
55 " -d|--dump Dump supported instruction table\n"
56 " -v|--version Print version and exit\n"
57 " -h|--help Print this help and exit\n\n"
58 "Examples:\n"
59 " bpfc fubar\n"
60 " bpfc fubar > foo (bpfc -f C -i fubar > foo) --> netsniff-ng -f foo ...\n"
61 " bpfc -f tcpdump -i fubar > foo --> tcpdump -ddd like ...\n"
62 " bpfc -f xt_bpf -b -p -i fubar\n"
63 " iptables -A INPUT -m bpf --bytecode \"`./bpfc -f xt_bpf -i fubar`\" -j LOG\n"
64 " bpfc - (read from stdin)\n"
65 "Note:\n"
66 " Generation of seccomp-BPF filters are fully supported as well.\n");
67 puts(copyright);
68 die();
71 static void __noreturn version(void)
73 printf("bpfc %s, Git id: %s\n", VERSION_LONG, GITVERSION);
74 puts("a tiny BPF compiler\n"
75 "http://www.netsniff-ng.org\n");
76 puts(copyright);
77 die();
80 int main(int argc, char **argv)
82 int ret, verbose = 0, c, opt_index, bypass = 0, format = 0;
83 bool invoke_cpp = false;
84 char *file = NULL;
86 setfsuid(getuid());
87 setfsgid(getgid());
89 if (argc == 1)
90 help();
92 while ((c = getopt_long(argc, argv, short_options,
93 long_options, &opt_index)) != EOF) {
94 switch (c) {
95 case 'h':
96 help();
97 break;
98 case 'v':
99 version();
100 break;
101 case 'V':
102 verbose = 1;
103 break;
104 case 'p':
105 invoke_cpp = true;
106 break;
107 case 'f':
108 if (!strncmp(optarg, "C", 1) ||
109 !strncmp(optarg, "netsniff-ng", 11))
110 format = 0;
111 else if (!strncmp(optarg, "tcpdump", 7))
112 format = 2;
113 else if (!strncmp(optarg, "xt_bpf", 6) ||
114 !strncmp(optarg, "tc", 2))
115 format = 1;
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;