proto_none: minor: make hex/chr conform with the rest
[netsniff-ng.git] / bpfc.c
blob6c0063dde6c1a129f5bebb56e4e4a5db95f77fc4
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>
27 #include <sys/fsuid.h>
29 #include "xmalloc.h"
30 #include "xutils.h"
31 #include "die.h"
32 #include "bpf.h"
34 static const char *short_options = "vhi:Vdb";
35 static const struct option long_options[] = {
36 {"input", required_argument, NULL, 'i'},
37 {"verbose", no_argument, NULL, 'V'},
38 {"bypass", no_argument, NULL, 'b'},
39 {"dump", no_argument, NULL, 'd'},
40 {"version", no_argument, NULL, 'v'},
41 {"help", no_argument, NULL, 'h'},
42 {NULL, 0, NULL, 0}
45 extern int compile_filter(char *file, int verbose, int bypass);
47 static void help(void)
49 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
50 puts("http://www.netsniff-ng.org\n\n"
51 "Usage: bpfc [options] || bpfc <program>\n"
52 "Options:\n"
53 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
54 " -V|--verbose Be more verbose\n"
55 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
56 " -d|--dump Dump supported instruction table\n"
57 " -v|--version Print version\n"
58 " -h|--help Print this help\n\n"
59 "Examples:\n"
60 " bpfc fubar\n"
61 " bpfc -bi fubar\n"
62 " bpfc - (read from stdin)\n\n"
63 "Please report bugs to <bugs@netsniff-ng.org>\n"
64 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
65 "Swiss federal institute of technology (ETH Zurich)\n"
66 "License: GNU GPL version 2.0\n"
67 "This is free software: you are free to change and redistribute it.\n"
68 "There is NO WARRANTY, to the extent permitted by law.\n");
69 die();
72 static void version(void)
74 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
75 puts("http://www.netsniff-ng.org\n\n"
76 "Please report bugs to <bugs@netsniff-ng.org>\n"
77 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
78 "Swiss federal institute of technology (ETH Zurich)\n"
79 "License: GNU GPL version 2.0\n"
80 "This is free software: you are free to change and redistribute it.\n"
81 "There is NO WARRANTY, to the extent permitted by law.\n");
82 die();
85 int main(int argc, char **argv)
87 int ret, verbose = 0, c, opt_index, bypass = 0;
88 char *file = NULL;
90 setfsuid(getuid());
91 setfsgid(getgid());
93 if (argc == 1)
94 help();
96 while ((c = getopt_long(argc, argv, short_options,
97 long_options, &opt_index)) != EOF) {
98 switch (c) {
99 case 'h':
100 help();
101 break;
102 case 'v':
103 version();
104 break;
105 case 'V':
106 verbose = 1;
107 break;
108 case 'b':
109 bypass = 1;
110 break;
111 case 'd':
112 bpf_dump_op_table();
113 die();
114 case 'i':
115 file = xstrdup(optarg);
116 break;
117 case '?':
118 switch (optopt) {
119 case 'i':
120 panic("Option -%c requires an argument!\n",
121 optopt);
122 default:
123 if (isprint(optopt))
124 printf("Unknown option character `0x%X\'!\n", optopt);
125 die();
127 default:
128 break;
132 if (argc == 2)
133 file = xstrdup(argv[1]);
134 if (!file)
135 panic("No Berkeley Packet Filter program specified!\n");
137 ret = compile_filter(file, verbose, bypass);
139 xfree(file);
140 return ret;