docs: add initial workflow document
[netsniff-ng.git] / bpfc.c
blob53ad5b3f99f70ed420151e6c908b3ac2ef35e6c3
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.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <getopt.h>
11 #include <ctype.h>
12 #include <unistd.h>
13 #include <sys/fsuid.h>
15 #include "xmalloc.h"
16 #include "xutils.h"
17 #include "die.h"
18 #include "bpf.h"
20 static const char *short_options = "vhi:VdbD";
21 static const struct option long_options[] = {
22 {"input", required_argument, NULL, 'i'},
23 {"verbose", no_argument, NULL, 'V'},
24 {"decimal", no_argument, NULL, 'D'},
25 {"bypass", no_argument, NULL, 'b'},
26 {"dump", no_argument, NULL, 'd'},
27 {"version", no_argument, NULL, 'v'},
28 {"help", no_argument, NULL, 'h'},
29 {NULL, 0, NULL, 0}
32 extern int compile_filter(char *file, int verbose, int bypass, int decimal);
34 static void help(void)
36 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
37 puts("http://www.netsniff-ng.org\n\n"
38 "Usage: bpfc [options] || bpfc <program>\n"
39 "Options:\n"
40 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
41 " -D|--decimal Decimal output, e.g. for xt_bpf\n"
42 " -V|--verbose Be more verbose\n"
43 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
44 " -d|--dump Dump supported instruction table\n"
45 " -v|--version Print version\n"
46 " -h|--help Print this help\n\n"
47 "Examples:\n"
48 " bpfc fubar\n"
49 " bpfc -Dbi fubar\n"
50 " bpfc - (read from stdin)\n\n"
51 "Please report bugs to <bugs@netsniff-ng.org>\n"
52 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
53 "Swiss federal institute of technology (ETH Zurich)\n"
54 "License: GNU GPL version 2.0\n"
55 "This is free software: you are free to change and redistribute it.\n"
56 "There is NO WARRANTY, to the extent permitted by law.\n");
57 die();
60 static void version(void)
62 printf("\nbpfc %s, a tiny BPF compiler\n", VERSION_STRING);
63 puts("http://www.netsniff-ng.org\n\n"
64 "Please report bugs to <bugs@netsniff-ng.org>\n"
65 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
66 "Swiss federal institute of technology (ETH Zurich)\n"
67 "License: GNU GPL version 2.0\n"
68 "This is free software: you are free to change and redistribute it.\n"
69 "There is NO WARRANTY, to the extent permitted by law.\n");
70 die();
73 int main(int argc, char **argv)
75 int ret, verbose = 0, c, opt_index, bypass = 0, decimal = 0;
76 char *file = NULL;
78 setfsuid(getuid());
79 setfsgid(getgid());
81 if (argc == 1)
82 help();
84 while ((c = getopt_long(argc, argv, short_options,
85 long_options, &opt_index)) != EOF) {
86 switch (c) {
87 case 'h':
88 help();
89 break;
90 case 'v':
91 version();
92 break;
93 case 'V':
94 verbose = 1;
95 break;
96 case 'D':
97 decimal = 1;
98 break;
99 case 'b':
100 bypass = 1;
101 break;
102 case 'd':
103 bpf_dump_op_table();
104 die();
105 case 'i':
106 file = xstrdup(optarg);
107 break;
108 case '?':
109 switch (optopt) {
110 case 'i':
111 panic("Option -%c requires an argument!\n",
112 optopt);
113 default:
114 if (isprint(optopt))
115 printf("Unknown option character `0x%X\'!\n", optopt);
116 die();
118 default:
119 break;
123 if (argc == 2)
124 file = xstrdup(argv[1]);
125 if (!file)
126 panic("No Berkeley Packet Filter program specified!\n");
128 ret = compile_filter(file, verbose, bypass, decimal);
130 xfree(file);
131 return ret;