Fix manpage warnings
[netsniff-ng-new.git] / bpfc.c
blob562c463d1c44d8f64809f49f269c9a67601c7fad
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 <stdbool.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <sys/fsuid.h>
16 #include "xmalloc.h"
17 #include "die.h"
18 #include "bpf.h"
19 #include "config.h"
20 #include "str.h"
22 static const char *short_options = "vhi:Vdbf:pD:";
23 static const struct option long_options[] = {
24 {"input", required_argument, NULL, 'i'},
25 {"format", required_argument, NULL, 'f'},
26 {"cpp", no_argument, NULL, 'p'},
27 {"define", required_argument, NULL, 'D'},
28 {"verbose", no_argument, NULL, 'V'},
29 {"bypass", no_argument, NULL, 'b'},
30 {"dump", no_argument, NULL, 'd'},
31 {"version", no_argument, NULL, 'v'},
32 {"help", no_argument, NULL, 'h'},
33 {NULL, 0, NULL, 0}
36 static const char *copyright = "Please report bugs to <netsniff-ng@googlegroups.com>\n"
37 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
38 "Swiss federal institute of technology (ETH Zurich)\n"
39 "License: GNU GPL version 2.0\n"
40 "This is free software: you are free to change and redistribute it.\n"
41 "There is NO WARRANTY, to the extent permitted by law.";
43 extern int compile_filter(char *file, bool verbose, int bypass, int format,
44 bool invoke_cpp, char **cpp_argv);
46 static void __noreturn help(void)
48 printf("bpfc %s, a tiny BPF compiler\n", VERSION_STRING);
49 puts("http://www.netsniff-ng.org\n\n"
50 "Usage: bpfc [options] || bpfc <program>\n"
51 "Options:\n"
52 " -i|--input <program/-> Berkeley Packet Filter file/stdin\n"
53 " -p|--cpp Run bpf program through C preprocessor\n"
54 " -D|--define Add macro/define for C preprocessor\n"
55 " -f|--format <format> Output format: C|netsniff-ng|xt_bpf|tcpdump\n"
56 " -b|--bypass Bypass filter validation (e.g. for bug testing)\n"
57 " -V|--verbose Be more verbose\n"
58 " -d|--dump Dump supported instruction table\n"
59 " -v|--version Print version and exit\n"
60 " -h|--help Print this help and exit\n\n"
61 "Examples:\n"
62 " bpfc fubar\n"
63 " bpfc fubar > foo (bpfc -f C -i fubar > foo) --> netsniff-ng -f foo ...\n"
64 " bpfc -f tcpdump -i fubar > foo --> tcpdump -ddd like ...\n"
65 " bpfc -f xt_bpf -b -p -i fubar\n"
66 " iptables -A INPUT -m bpf --bytecode \"`./bpfc -f xt_bpf -i fubar`\" -j LOG\n"
67 " bpfc - (read from stdin)\n"
68 "Note:\n"
69 " Generation of seccomp-BPF filters are fully supported as well.\n");
70 puts(copyright);
71 die();
74 static void __noreturn version(void)
76 printf("bpfc %s, Git id: %s\n", VERSION_LONG, GITVERSION);
77 puts("a tiny BPF compiler\n"
78 "http://www.netsniff-ng.org\n");
79 puts(copyright);
80 die();
83 int main(int argc, char **argv)
85 int ret, c, bypass = 0, format = 0;
86 bool verbose = false, invoke_cpp = false;
87 char **cpp_argv = NULL;
88 size_t cpp_argc = 0;
89 char *file = NULL;
91 setfsuid(getuid());
92 setfsgid(getgid());
94 if (argc == 1)
95 help();
97 while ((c = getopt_long(argc, argv, short_options, long_options,
98 NULL)) != EOF) {
99 switch (c) {
100 case 'h':
101 help();
102 break;
103 case 'v':
104 version();
105 break;
106 case 'V':
107 verbose = true;
108 break;
109 case 'p':
110 invoke_cpp = true;
111 break;
112 case 'D':
113 cpp_argv = argv_insert(cpp_argv, &cpp_argc, "-D");
114 cpp_argv = argv_insert(cpp_argv, &cpp_argc, optarg);
115 break;
116 case 'f':
117 if (!strncmp(optarg, "C", 1) ||
118 !strncmp(optarg, "netsniff-ng", 11))
119 format = 0;
120 else if (!strncmp(optarg, "tcpdump", 7))
121 format = 2;
122 else if (!strncmp(optarg, "xt_bpf", 6) ||
123 !strncmp(optarg, "tc", 2))
124 format = 1;
125 else
126 help();
127 break;
128 case 'b':
129 bypass = 1;
130 break;
131 case 'd':
132 bpf_dump_op_table();
133 die();
134 case 'i':
135 file = xstrdup(optarg);
136 break;
137 case '?':
138 switch (optopt) {
139 case 'i':
140 case 'f':
141 panic("Option -%c requires an argument!\n",
142 optopt);
143 default:
144 if (isprint(optopt))
145 printf("Unknown option character `0x%X\'!\n", optopt);
146 die();
148 default:
149 break;
153 if (argc == 2)
154 file = xstrdup(argv[1]);
155 if (!file)
156 panic("No Berkeley Packet Filter program specified!\n");
158 ret = compile_filter(file, verbose, bypass, format, invoke_cpp, cpp_argv);
160 argv_free(cpp_argv);
161 xfree(file);
162 return ret;