summaryrefslogtreecommitdiff
path: root/bpfc.c
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-03-15 10:41:48 +0100
committerDaniel Borkmann <dborkman@redhat.com>2013-03-15 10:41:48 +0100
commit1a9fbac03c684f29cff9ac44875bd9504a89f54e (patch)
tree1b2e40dbe5dc1899ef5b62c4325c9b94c9c450fc /bpfc.c
all: import netsniff-ng 0.5.8-rc0 source
We decided to get rid of the old Git history and start a new one for several reasons: *) Allow / enforce only high-quality commits (which was not the case for many commits in the history), have a policy that is more close to the one from the Linux kernel. With high quality commits, we mean code that is logically split into commits and commit messages that are signed-off and have a proper subject and message body. We do not allow automatic Github merges anymore, since they are total bullshit. However, we will either cherry-pick your patches or pull them manually. *) The old archive was about ~27MB for no particular good reason. This basically derived from the bad decision that also some PDF files where stored there. From this moment onwards, no binary objects are allowed to be stored in this repository anymore. The old archive is not wiped away from the Internet. You will still be able to find it, e.g. on git.cryptoism.org etc. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'bpfc.c')
-rw-r--r--bpfc.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/bpfc.c b/bpfc.c
new file mode 100644
index 0000000..53ad5b3
--- /dev/null
+++ b/bpfc.c
@@ -0,0 +1,132 @@
+/*
+ * netsniff-ng - the packet sniffing beast
+ * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
+ * Swiss federal institute of technology (ETH Zurich)
+ * Subject to the GPL, version 2.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <sys/fsuid.h>
+
+#include "xmalloc.h"
+#include "xutils.h"
+#include "die.h"
+#include "bpf.h"
+
+static const char *short_options = "vhi:VdbD";
+static const struct option long_options[] = {
+ {"input", required_argument, NULL, 'i'},
+ {"verbose", no_argument, NULL, 'V'},
+ {"decimal", no_argument, NULL, 'D'},
+ {"bypass", no_argument, NULL, 'b'},
+ {"dump", no_argument, NULL, 'd'},
+ {"version", no_argument, NULL, 'v'},
+ {"help", no_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+};
+
+extern int compile_filter(char *file, int verbose, int bypass, int decimal);
+
+static void help(void)
+{
+ printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
+ puts("http://www.netsniff-ng.org\n\n"
+ "Usage: bpfc [options] || bpfc <program>\n"
+ "Options:\n"
+ " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
+ " -D|--decimal Decimal output, e.g. for xt_bpf\n"
+ " -V|--verbose Be more verbose\n"
+ " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
+ " -d|--dump Dump supported instruction table\n"
+ " -v|--version Print version\n"
+ " -h|--help Print this help\n\n"
+ "Examples:\n"
+ " bpfc fubar\n"
+ " bpfc -Dbi fubar\n"
+ " bpfc - (read from stdin)\n\n"
+ "Please report bugs to <bugs@netsniff-ng.org>\n"
+ "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
+ "Swiss federal institute of technology (ETH Zurich)\n"
+ "License: GNU GPL version 2.0\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n");
+ die();
+}
+
+static void version(void)
+{
+ printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
+ puts("http://www.netsniff-ng.org\n\n"
+ "Please report bugs to <bugs@netsniff-ng.org>\n"
+ "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
+ "Swiss federal institute of technology (ETH Zurich)\n"
+ "License: GNU GPL version 2.0\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n");
+ die();
+}
+
+int main(int argc, char **argv)
+{
+ int ret, verbose = 0, c, opt_index, bypass = 0, decimal = 0;
+ char *file = NULL;
+
+ setfsuid(getuid());
+ setfsgid(getgid());
+
+ if (argc == 1)
+ help();
+
+ while ((c = getopt_long(argc, argv, short_options,
+ long_options, &opt_index)) != EOF) {
+ switch (c) {
+ case 'h':
+ help();
+ break;
+ case 'v':
+ version();
+ break;
+ case 'V':
+ verbose = 1;
+ break;
+ case 'D':
+ decimal = 1;
+ break;
+ case 'b':
+ bypass = 1;
+ break;
+ case 'd':
+ bpf_dump_op_table();
+ die();
+ case 'i':
+ file = xstrdup(optarg);
+ break;
+ case '?':
+ switch (optopt) {
+ case 'i':
+ panic("Option -%c requires an argument!\n",
+ optopt);
+ default:
+ if (isprint(optopt))
+ printf("Unknown option character `0x%X\'!\n", optopt);
+ die();
+ }
+ default:
+ break;
+ }
+ }
+
+ if (argc == 2)
+ file = xstrdup(argv[1]);
+ if (!file)
+ panic("No Berkeley Packet Filter program specified!\n");
+
+ ret = compile_filter(file, verbose, bypass, decimal);
+
+ xfree(file);
+ return ret;
+}