docs: add tree for fedora/rhel maintenance
[netsniff-ng.git] / src / bpfc.c
blob5013c640142f36d4181ae8638f850b67ef2ce2a9
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>
27 #include "xmalloc.h"
28 #include "die.h"
30 static const char *short_options = "vhi:V";
32 static struct option long_options[] = {
33 {"input", required_argument, 0, 'i'},
34 {"verbose", no_argument, 0, 'V'},
35 {"version", no_argument, 0, 'v'},
36 {"help", no_argument, 0, 'h'},
37 {0, 0, 0, 0}
40 extern int compile_filter(char *file, int verbose);
42 static void help(void)
44 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
45 printf("http://www.netsniff-ng.org\n\n");
46 printf("Usage: bpfc [options] || bpfc <program>\n");
47 printf("Options:\n");
48 printf(" -i|--input <program> Berkeley Packet Filter file\n");
49 printf(" -V|--verbose Be more verbose\n");
50 printf(" -v|--version Print version\n");
51 printf(" -h|--help Print this help\n");
52 printf("\n");
53 printf("Examples:\n");
54 printf(" bpfc -i fubar.bpf\n\n");
55 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
56 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
57 printf("Swiss federal institute of technology (ETH Zurich)\n");
58 printf("License: GNU GPL version 2\n");
59 printf("This is free software: you are free to change and redistribute it.\n");
60 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
61 die();
64 static void version(void)
66 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
67 printf("http://www.netsniff-ng.org\n\n");
68 printf("Please report bugs to <bugs@netsniff-ng.org>\n");
69 printf("Copyright (C) 2011-2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n");
70 printf("Swiss federal institute of technology (ETH Zurich)\n");
71 printf("License: GNU GPL version 2\n");
72 printf("This is free software: you are free to change and redistribute it.\n");
73 printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
74 die();
77 int main(int argc, char **argv)
79 int ret, verbose = 0, c, opt_index;
80 char *file = NULL;
82 if (argc == 1)
83 help();
85 while (argc > 2 && (c = getopt_long(argc, argv, short_options,
86 long_options, &opt_index)) != EOF) {
87 switch (c) {
88 case 'h':
89 help();
90 break;
91 case 'v':
92 version();
93 break;
94 case 'V':
95 verbose = 1;
96 break;
97 case 'i':
98 file = xstrdup(optarg);
99 break;
100 case '?':
101 switch (optopt) {
102 case 'i':
103 panic("Option -%c requires an argument!\n",
104 optopt);
105 default:
106 if (isprint(optopt))
107 whine("Unknown option character "
108 "`0x%X\'!\n", optopt);
109 die();
111 default:
112 break;
115 if (argc == 2)
116 file = xstrdup(argv[1]);
118 if (!file)
119 panic("No Berkeley Packet Filter program specified!\n");
120 ret = compile_filter(file, verbose);
121 xfree(file);
122 return ret;