docs: authors: add Doug as minor contr. (thanks)
[netsniff-ng.git] / src / bpfc.c
blob0863e743c37b9b6601a96105e516c07a87b1c756
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'.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <ctype.h>
26 #include <unistd.h>
28 #include "xmalloc.h"
29 #include "xutils.h"
30 #include "die.h"
31 #include "bpf.h"
33 static const char *short_options = "vhi:VdbHLg";
34 static const struct option long_options[] = {
35 {"input", required_argument, NULL, 'i'},
36 {"verbose", no_argument, NULL, 'V'},
37 {"hla", no_argument, NULL, 'H'},
38 {"lla", no_argument, NULL, 'L'},
39 {"hla-debug", no_argument, NULL, 'g'},
40 {"bypass", no_argument, NULL, 'b'},
41 {"dump", no_argument, NULL, 'd'},
42 {"version", no_argument, NULL, 'v'},
43 {"help", no_argument, NULL, 'h'},
44 {NULL, 0, NULL, 0}
47 extern int compile_filter(char *file, int verbose, int bypass);
48 extern int compile_hla_filter(char *file, int verbose, int debug);
50 static void help(void)
52 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
53 puts("http://www.netsniff-ng.org\n\n"
54 "Usage: bpfc [options] || bpfc <program>\n"
55 "Options:\n"
56 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
57 " -H|--hla Compile high-level BPF\n"
58 " -L|--lla Compile low-level BPF\n"
59 " -V|--verbose Be more verbose\n"
60 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
61 " -g|--hla-debug Print BPF expressions to stdout\n"
62 " -d|--dump Dump supported instruction table\n"
63 " -v|--version Print version\n"
64 " -h|--help Print this help\n\n"
65 "Examples:\n"
66 " bpfc -Hi fubar\n"
67 " bpfc -Hgi fubar\n"
68 " bpfc -Li fubar\n"
69 " bpfc -Lbi fubar\n"
70 " bpfc -Li - (read from stdin)\n\n"
71 "Please report bugs to <bugs@netsniff-ng.org>\n"
72 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
73 "Swiss federal institute of technology (ETH Zurich)\n"
74 "License: GNU GPL version 2.0\n"
75 "This is free software: you are free to change and redistribute it.\n"
76 "There is NO WARRANTY, to the extent permitted by law.\n");
77 die();
80 static void version(void)
82 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
83 puts("http://www.netsniff-ng.org\n\n"
84 "Please report bugs to <bugs@netsniff-ng.org>\n"
85 "Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
86 "Swiss federal institute of technology (ETH Zurich)\n"
87 "License: GNU GPL version 2.0\n"
88 "This is free software: you are free to change and redistribute it.\n"
89 "There is NO WARRANTY, to the extent permitted by law.\n");
90 die();
93 int main(int argc, char **argv)
95 int ret, verbose = 0, c, opt_index, bypass = 0, hla = 0, debug = 0;
96 char *file = NULL;
98 if (argc == 1)
99 help();
101 while ((c = getopt_long(argc, argv, short_options,
102 long_options, &opt_index)) != EOF) {
103 switch (c) {
104 case 'h':
105 help();
106 break;
107 case 'v':
108 version();
109 break;
110 case 'H':
111 hla = 1;
112 break;
113 case 'L':
114 hla = 0;
115 break;
116 case 'g':
117 debug = 1;
118 break;
119 case 'V':
120 verbose = 1;
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 panic("Option -%c requires an argument!\n",
135 optopt);
136 default:
137 if (isprint(optopt))
138 whine("Unknown option character "
139 "`0x%X\'!\n", optopt);
140 die();
142 default:
143 break;
146 if (argc == 2)
147 file = xstrdup(argv[1]);
149 if (!file)
150 panic("No Berkeley Packet Filter program specified!\n");
152 if (hla) {
153 ret = compile_hla_filter(file, verbose, debug);
154 if (!ret) {
155 char file_tmp[128];
157 slprintf(file_tmp, sizeof(file_tmp), ".%s", file);
158 ret = compile_filter(file_tmp, verbose, bypass);
159 unlink(file_tmp);
161 } else {
162 ret = compile_filter(file, verbose, bypass);
165 xfree(file);
166 return ret;