Add -D option to allow pass -Dkey0=val0...-DkeyN=valN defines to use them in bpf filter via C preprocessor.
Signed-off-by: Vadim Kochan <[email protected]> --- bpf_parser.y | 6 +++--- bpfc.8 | 4 ++++ bpfc.c | 14 +++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/bpf_parser.y b/bpf_parser.y index db6a007..05f2322 100644 --- a/bpf_parser.y +++ b/bpf_parser.y @@ -28,7 +28,7 @@ #include "cpp.h" int compile_filter(char *file, int verbose, int bypass, int format, - bool invoke_cpp); + bool invoke_cpp, char *cpp_args); static int curr_instr = 0; @@ -735,7 +735,7 @@ static void pretty_printer(const struct sock_fprog *prog, int format) } int compile_filter(char *file, int verbose, int bypass, int format, - bool invoke_cpp) + bool invoke_cpp, char *cpp_args) { int i; struct sock_fprog res; @@ -745,7 +745,7 @@ int compile_filter(char *file, int verbose, int bypass, int format, memset(tmp_file, 0, sizeof(tmp_file)); if (invoke_cpp) { - ret = cpp_exec(file, tmp_file, sizeof(tmp_file), NULL); + ret = cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_args); if (ret) { fprintf(stderr, "Failed to invoke C preprocessor!\n"); goto exit; diff --git a/bpfc.8 b/bpfc.8 index 8a99e2e..942c904 100644 --- a/bpfc.8 +++ b/bpfc.8 @@ -65,6 +65,10 @@ Pass the bpf program through the C preprocessor before reading it in bpfc. This allows #define and #include directives (e.g. to include definitions from system headers) to be used in the bpf program. .PP +.SS -D, --def +Pass a macro definition for C preprocessor. This allows to specify runtime +parameters for the #ifdef directives. +.PP .SS -f <format>, --format <format> Specify a different output format than the default that is netsniff-ng compatible. The <format> specifier can be: C, netsniff-ng, xt_bpf, tcpdump. diff --git a/bpfc.c b/bpfc.c index d360cf5..6067712 100644 --- a/bpfc.c +++ b/bpfc.c @@ -17,12 +17,14 @@ #include "die.h" #include "bpf.h" #include "config.h" +#include "str.h" -static const char *short_options = "vhi:Vdbf:p"; +static const char *short_options = "vhi:Vdbf:pD:"; static const struct option long_options[] = { {"input", required_argument, NULL, 'i'}, {"format", required_argument, NULL, 'f'}, {"cpp", no_argument, NULL, 'p'}, + {"def", required_argument, NULL, 'D'}, {"verbose", no_argument, NULL, 'V'}, {"bypass", no_argument, NULL, 'b'}, {"dump", no_argument, NULL, 'd'}, @@ -39,7 +41,7 @@ static const char *copyright = "Please report bugs to <netsniff-ng@googlegroups. "There is NO WARRANTY, to the extent permitted by law."; extern int compile_filter(char *file, int verbose, int bypass, int format, - bool invoke_cpp); + bool invoke_cpp, char *cpp_args); static void __noreturn help(void) { @@ -49,6 +51,7 @@ static void __noreturn help(void) "Options:\n" " -i|--input <program/-> Berkeley Packet Filter file/stdin\n" " -p|--cpp Run bpf program through C preprocessor\n" + " -D|--def Specify a macro for C preprocessor\n" " -f|--format <format> Output format: C|netsniff-ng|xt_bpf|tcpdump\n" " -b|--bypass Bypass filter validation (e.g. for bug testing)\n" " -V|--verbose Be more verbose\n" @@ -81,6 +84,7 @@ int main(int argc, char **argv) { int ret, verbose = 0, c, opt_index, bypass = 0, format = 0; bool invoke_cpp = false; + char *cpp_args = NULL; char *file = NULL; setfsuid(getuid()); @@ -125,6 +129,10 @@ int main(int argc, char **argv) case 'i': file = xstrdup(optarg); break; + case 'D': + cpp_args = str_cat(cpp_args, " -D"); + cpp_args = str_cat(cpp_args, optarg); + break; case '?': switch (optopt) { case 'i': @@ -146,7 +154,7 @@ int main(int argc, char **argv) if (!file) panic("No Berkeley Packet Filter program specified!\n"); - ret = compile_filter(file, verbose, bypass, format, invoke_cpp); + ret = compile_filter(file, verbose, bypass, format, invoke_cpp, cpp_args); xfree(file); return ret; -- 2.6.2 -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
