proto_icmpv6.h: intermediate state
[netsniff-ng.git] / src / bpfc.c
blob3da140245fa4f994cbb13b920fd3edccddbce60e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
8 * This is a tiny Berkeley Packet Filter compiler that understands the
9 * Syntax / Semantic from the USENIX paper {"The BSD Packet Filter: A New
10 * Architecture for User-level Packet Capture", McCanne, Steven and
11 * Jacobson, Van, Lawrence Berkeley Laboratory}. With this, BPFs can be
12 * written the good old way and understood by the Linux kernel and *BSD
13 * kernels where Berkeley Packet Filters are used.
15 * The one small garden of a free gardener was all his need and due, not
16 * a garden swollen to a realm; his own hands to use, not the hands of
17 * others to command.
19 * -- The Lord of the Rings, Sam, Chapter 'The Tower of Cirith Ungol'.
24 =head1 NAME
26 bpfc - compile a BPF expression into BPF opcode
28 =head1 SYNOPSIS
30 bpfc -i|--input <program> [-V|--verbose][-v|--version][-h|--help]
32 =head1 DESCRIPTION
34 bpfc is a tool to generate BPF opcode from a literal expression.
35 The generated BPF opcode then can be used to filter out the
36 corresponding traffic.
38 =head1 EXAMPLES
40 =over
42 =item bpfc --input example.bpf
44 Transform the literal expression in example.bpf into BPF opcodes
46 =back
48 =head1 OPTIONS
50 =over
52 =item -i|--input <program>
54 Path to Berkeley Packet Filter file.
56 =item -V|--verbose
58 Increase program verbosity
60 =item -v|--version
62 Print version.
64 =item -h|--help
66 Print help text and lists all options.
68 =back
70 =head1 AUTHOR
72 Written by Daniel Borkmann <daniel@netsniff-ng.org>
74 =head1 DOCUMENTATION
76 Documentation by Emmanuel Roullit <emmanuel@netsniff-ng.org>
78 =head1 BUGS
80 Please report bugs to <bugs@netsniff-ng.org>
82 =cut
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <getopt.h>
89 #include <ctype.h>
91 #include "xmalloc.h"
92 #include "die.h"
94 static const char *short_options = "vhi:V";
96 static struct option long_options[] = {
97 {"input", required_argument, 0, 'i'},
98 {"verbose", no_argument, 0, 'V'},
99 {"version", no_argument, 0, 'v'},
100 {"help", no_argument, 0, 'h'},
101 {0, 0, 0, 0}
104 extern int compile_filter(char *file, int verbose);
106 static void help(void)
108 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
109 printf("http://www.netsniff-ng.org\n\n");
110 printf("Usage: bpfc [options] || bpfc <program>\n");
111 printf("Options:\n");
112 printf(" -i|--input <program> Berkeley Packet Filter file\n");
113 printf(" -V|--verbose Be more verbose\n");
114 printf(" -v|--version Print version\n");
115 printf(" -h|--help Print this help\n");
116 printf("\n");
117 printf("Examples:\n");
118 printf(" bpfc -i fubar.bpf\n\n");
119 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
120 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
121 printf("Swiss federal institute of technology (ETH Zurich)\n");
122 printf("License: GNU GPL version 2\n");
123 printf("This is free software: you are free to change and redistribute it.\n");
124 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
125 die();
128 static void version(void)
130 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
131 printf("http://www.netsniff-ng.org\n\n");
132 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
133 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
134 printf("Swiss federal institute of technology (ETH Zurich)\n");
135 printf("License: GNU GPL version 2\n");
136 printf("This is free software: you are free to change and redistribute it.\n");
137 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
138 die();
141 int main(int argc, char **argv)
143 int ret, verbose = 0, c, opt_index;
144 char *file = NULL;
146 if (argc == 1)
147 help();
149 while (argc > 2 && (c = getopt_long(argc, argv, short_options,
150 long_options, &opt_index)) != EOF) {
151 switch (c) {
152 case 'h':
153 help();
154 break;
155 case 'v':
156 version();
157 break;
158 case 'V':
159 verbose = 1;
160 break;
161 case 'i':
162 file = xstrdup(optarg);
163 break;
164 case '?':
165 switch (optopt) {
166 case 'i':
167 panic("Option -%c requires an argument!\n",
168 optopt);
169 default:
170 if (isprint(optopt))
171 whine("Unknown option character "
172 "`0x%X\'!\n", optopt);
173 die();
175 default:
176 break;
179 if (argc == 2)
180 file = xstrdup(argv[1]);
182 if (!file)
183 panic("No Berkeley Packet Filter program specified!\n");
185 ret = compile_filter(file, verbose);
187 xfree(file);
189 return ret;