Merge pull request #59 from electronjoe/graceful-SIGTERM-handling
[netsniff-ng-old.git] / bpfc.c
blob547e5c1f878abc2048e6b7665dfe7c177e4c04bb
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
4 * Swiss federal institute of technology (ETH Zurich)
5 * Subject to the GPL, version 2.
7 * The one small garden of a free gardener was all his need and due, not
8 * a garden swollen to a realm; his own hands to use, not the hands of
9 * others to command.
11 * -- The Lord of the Rings, Sam, Chapter 'The Tower of Cirith Ungol'.
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <getopt.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <sys/fsuid.h>
21 #include "xmalloc.h"
22 #include "xutils.h"
23 #include "die.h"
24 #include "bpf.h"
26 static const char *short_options = "vhi:VdbD";
27 static const struct option long_options[] = {
28 {"input", required_argument, NULL, 'i'},
29 {"verbose", no_argument, NULL, 'V'},
30 {"decimal", no_argument, NULL, 'D'},
31 {"bypass", no_argument, NULL, 'b'},
32 {"dump", no_argument, NULL, 'd'},
33 {"version", no_argument, NULL, 'v'},
34 {"help", no_argument, NULL, 'h'},
35 {NULL, 0, NULL, 0}
38 extern int compile_filter(char *file, int verbose, int bypass, int decimal);
40 static void help(void)
42 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
43 puts("http://www.netsniff-ng.org\n\n"
44 "Usage: bpfc [options] || bpfc <program>\n"
45 "Options:\n"
46 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
47 " -D|--decimal Decimal output, e.g. for xt_bpf\n"
48 " -V|--verbose Be more verbose\n"
49 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
50 " -d|--dump Dump supported instruction table\n"
51 " -v|--version Print version\n"
52 " -h|--help Print this help\n\n"
53 "Examples:\n"
54 " bpfc fubar\n"
55 " bpfc -Dbi fubar\n"
56 " bpfc - (read from stdin)\n\n"
57 "Please report bugs to <bugs@netsniff-ng.org>\n"
58 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
59 "Swiss federal institute of technology (ETH Zurich)\n"
60 "License: GNU GPL version 2.0\n"
61 "This is free software: you are free to change and redistribute it.\n"
62 "There is NO WARRANTY, to the extent permitted by law.\n");
63 die();
66 static void version(void)
68 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
69 puts("http://www.netsniff-ng.org\n\n"
70 "Please report bugs to <bugs@netsniff-ng.org>\n"
71 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
72 "Swiss federal institute of technology (ETH Zurich)\n"
73 "License: GNU GPL version 2.0\n"
74 "This is free software: you are free to change and redistribute it.\n"
75 "There is NO WARRANTY, to the extent permitted by law.\n");
76 die();
79 int main(int argc, char **argv)
81 int ret, verbose = 0, c, opt_index, bypass = 0, decimal = 0;
82 char *file = NULL;
84 setfsuid(getuid());
85 setfsgid(getgid());
87 if (argc == 1)
88 help();
90 while ((c = getopt_long(argc, argv, short_options,
91 long_options, &opt_index)) != EOF) {
92 switch (c) {
93 case 'h':
94 help();
95 break;
96 case 'v':
97 version();
98 break;
99 case 'V':
100 verbose = 1;
101 break;
102 case 'D':
103 decimal = 1;
104 break;
105 case 'b':
106 bypass = 1;
107 break;
108 case 'd':
109 bpf_dump_op_table();
110 die();
111 case 'i':
112 file = xstrdup(optarg);
113 break;
114 case '?':
115 switch (optopt) {
116 case 'i':
117 panic("Option -%c requires an argument!\n",
118 optopt);
119 default:
120 if (isprint(optopt))
121 printf("Unknown option character `0x%X\'!\n", optopt);
122 die();
124 default:
125 break;
129 if (argc == 2)
130 file = xstrdup(argv[1]);
131 if (!file)
132 panic("No Berkeley Packet Filter program specified!\n");
134 ret = compile_filter(file, verbose, bypass, decimal);
136 xfree(file);
137 return ret;