[netsniff-ng] [PATCH 1/5] trafgen: Make sure yyin is set before close it

2015-11-24 Thread Vadim Kochan
In case if cpp failed then it is possible that trafgen
may hang on closing uninitialized yyin.

Signed-off-by: Vadim Kochan 
---
 trafgen_parser.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/trafgen_parser.y b/trafgen_parser.y
index afcb3e2..44751ab 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -625,7 +625,7 @@ void compile_packets(char *file, bool verbose, unsigned int 
cpu, bool invoke_cpp
 
ret = 0;
 err:
-   if (yyin != stdin)
+   if (yyin && yyin != stdin)
fclose(yyin);
 
if (invoke_cpp)
-- 
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 netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] [PATCH 2/5] cpp: Allow to pass cpp arguments

2015-11-24 Thread Vadim Kochan
Extend cpp_exec func to pass cpp arguments

Signed-off-by: Vadim Kochan 
---
 bpf_parser.y | 2 +-
 cpp.c| 6 +++---
 cpp.h| 2 +-
 trafgen_parser.y | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/bpf_parser.y b/bpf_parser.y
index 8aed9dc..db6a007 100644
--- a/bpf_parser.y
+++ b/bpf_parser.y
@@ -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));
+   ret = cpp_exec(file, tmp_file, sizeof(tmp_file), NULL);
if (ret) {
fprintf(stderr, "Failed to invoke C preprocessor!\n");
goto exit;
diff --git a/cpp.c b/cpp.c
index 6734eac..99c4c33 100644
--- a/cpp.c
+++ b/cpp.c
@@ -4,7 +4,7 @@
 #include "str.h"
 #include "xmalloc.h"
 
-int cpp_exec(char *in_file, char *out_file, size_t out_len)
+int cpp_exec(char *in_file, char *out_file, size_t out_len, char *args)
 {
char *tmp = xstrdup(in_file);
char cmd[256], *base;
@@ -13,8 +13,8 @@ int cpp_exec(char *in_file, char *out_file, size_t out_len)
base = basename(tmp);
 
slprintf(out_file, out_len, "/tmp/.tmp-%u-%s", rand(), base);
-   slprintf(cmd, sizeof(cmd), "cpp -I" ETCDIRE_STRING " %s > %s",
-in_file, out_file);
+   slprintf(cmd, sizeof(cmd), "cpp -I" ETCDIRE_STRING " %s %s > %s",
+args ? args : "", in_file, out_file);
 
if (system(cmd) != 0)
ret = -1;
diff --git a/cpp.h b/cpp.h
index a3e295d..c4ef020 100644
--- a/cpp.h
+++ b/cpp.h
@@ -1,6 +1,6 @@
 #ifndef CPP_H
 #define CPP_H
 
-extern int cpp_exec(char *in_file, char *out_file, size_t out_len);
+extern int cpp_exec(char *in_file, char *out_file, size_t out_len, char *args);
 
 #endif
diff --git a/trafgen_parser.y b/trafgen_parser.y
index 44751ab..8a0f3b7 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -599,7 +599,7 @@ void compile_packets(char *file, bool verbose, unsigned int 
cpu, bool invoke_cpp
our_cpu = cpu;
 
if (invoke_cpp) {
-   if (cpp_exec(file, tmp_file, sizeof(tmp_file))) {
+   if (cpp_exec(file, tmp_file, sizeof(tmp_file), NULL)) {
fprintf(stderr, "Failed to invoke C preprocessor!\n");
goto err;
}
-- 
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 netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] [PATCH 5/5] bpfc: Add command-line option to add cpp macro

2015-11-24 Thread Vadim Kochan
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 
---
 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 
 Specify a different output format than the default that is netsniff-ng
 compatible. The  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 
  Berkeley Packet Filter file/stdin\n"
 "  -p|--cppRun bpf program through C 
preprocessor\n"
+"  -D|--defSpecify a macro for C preprocessor\n"
 "  -f|--format Output format: 
C|netsniff-ng|xt_bpf|tcpdump\n"
 "  -b|--bypass Bypass filter validation (e.g. for bug 
testing)\n"
 "  -V|--verboseBe 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 netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] [PATCH 4/5] trafgen: Add command-line option to add cpp macro

2015-11-24 Thread Vadim Kochan
Add -D option to allow pass -Dkey0=val0...-DkeyN=valN
defines to use them in conf file via C preprocessor.

Signed-off-by: Vadim Kochan 
---
 trafgen.8|  5 +
 trafgen.c| 18 ++
 trafgen_conf.h   |  3 ++-
 trafgen_parser.y |  5 +++--
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/trafgen.8 b/trafgen.8
index 18b2b61..50d6d53 100644
--- a/trafgen.8
+++ b/trafgen.8
@@ -79,6 +79,11 @@ Pass the packet configuration to the C preprocessor before 
reading it into
 trafgen. This allows #define and #include directives (e.g. to include
 definitions from system headers) to be used in the trafgen configuration file.
 .PP
+.SS -D, --def
+Pass a macro definition for C preprocessor. This allows to specify runtime
+parameters for the #ifdef directives. To pass a string it is needed to escape
+it in the command line like -DTEXT="\\"\\"Hello TCP"\\"\\".
+.PP
 .SS -J, --jumbo-support
 By default trafgen's ring buffer frames are of a fixed size of 2048 bytes.
 This means that if you're expecting jumbo frames or even super jumbo frames to
diff --git a/trafgen.c b/trafgen.c
index d01f160..fb755f8 100644
--- a/trafgen.c
+++ b/trafgen.c
@@ -83,7 +83,7 @@ size_t plen = 0;
 struct packet_dyn *packet_dyn = NULL;
 size_t dlen = 0;
 
-static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQq";
+static const char *short_options = "d:c:n:t:vJhS:rk:i:o:VRs:P:eE:pu:g:CHQqD:";
 static const struct option long_options[] = {
{"dev", required_argument,  NULL, 'd'},
{"out", required_argument,  NULL, 'o'},
@@ -110,6 +110,7 @@ static const struct option long_options[] = {
{"verbose", no_argument,NULL, 'V'},
{"version", no_argument,NULL, 'v'},
{"example", no_argument,NULL, 'e'},
+   {"def", required_argument,  NULL, 'D'},
{"help",no_argument,NULL, 'h'},
{NULL, 0, NULL, 0}
 };
@@ -163,6 +164,7 @@ static void __noreturn help(void)
 "  -i|-c|--in|--conf   Packet configuration 
file/stdin\n"
 "  -o|-d|--out|--dev  Networking device i.e., eth0\n"
 "  -p|--cpp   Run packet config through C 
preprocessor\n"
+"  -D|--def   Specify a macro for C 
preprocessor\n"
 "  -J|--jumbo-support Support 64KB super jumbo frames 
(def: 2048B)\n"
 "  -R|--rfraw Inject raw 802.11 frames\n"
 "  -s|--smoke-test  Probe if machine survived 
fuzz-tested packet\n"
@@ -823,12 +825,13 @@ static void xmit_packet_precheck(struct ctx *ctx, 
unsigned int cpu)
 }
 
 static void main_loop(struct ctx *ctx, char *confname, bool slow,
- unsigned int cpu, bool invoke_cpp, unsigned long orig_num)
+ unsigned int cpu, bool invoke_cpp, char *cpp_args,
+ unsigned long orig_num)
 {
if (ctx->packet_str)
compile_packets_str(ctx->packet_str, ctx->verbose, cpu);
else
-   compile_packets(confname, ctx->verbose, cpu, invoke_cpp);
+   compile_packets(confname, ctx->verbose, cpu, invoke_cpp, 
cpp_args);
 
preprocess_packets();
 
@@ -896,6 +899,7 @@ int main(int argc, char **argv)
unsigned long long tx_packets, tx_bytes;
struct ctx ctx;
int min_opts = 5;
+   char *cpp_args = NULL;
 
fmemset(&ctx, 0, sizeof(ctx));
ctx.cpus = get_number_cpus_online();
@@ -1045,6 +1049,10 @@ int main(int argc, char **argv)
 
ctx.reserve_size *= strtoul(optarg, NULL, 0);
break;
+   case 'D':
+   cpp_args = str_cat(cpp_args, " -D");
+   cpp_args = str_cat(cpp_args, optarg);
+   break;
case '?':
switch (optopt) {
case 'd':
@@ -1133,7 +1141,8 @@ int main(int argc, char **argv)
srand(seed);
 
cpu_affinity(i);
-   main_loop(&ctx, confname, slow, i, invoke_cpp, 
orig_num);
+   main_loop(&ctx, confname, slow, i, invoke_cpp,
+ cpp_args, orig_num);
 
goto thread_out;
case -1:
@@ -1184,6 +1193,7 @@ thread_out:
free(ctx.rhost);
free(confname);
free(ctx.packet_str);
+   free(cpp_args);
 
return 0;
 }
diff --git a/trafgen_conf.h b/trafgen_conf.h
index deadb7c..a37728f 100644
--- a/trafgen_conf.h
+++ b/trafgen_conf.h
@@ -56,7 +56,8 @@ static inline bool packet_dyn_has_only_csums(struct 
packet_dyn *p)
 }
 
 extern void compile_packets_str(char *str, bool verbose, unsigned int cpu);
-extern void compile_packets(char *file, bool verbose, unsi

[netsniff-ng] [PATCH 3/5] str: Add string concatenation func

2015-11-24 Thread Vadim Kochan
Add str_cat func for concat 2 strings.

Signed-off-by: Vadim Kochan 
---
 str.c | 17 +
 str.h |  1 +
 2 files changed, 18 insertions(+)

diff --git a/str.c b/str.c
index f4cb099..e838466 100644
--- a/str.c
+++ b/str.c
@@ -109,3 +109,20 @@ char *argv2str(int startind, int argc, char **argv)
 
return str;
 }
+
+char *str_cat(char *to, char *from)
+{
+   size_t from_len;
+   size_t to_len;
+
+   if (!from)
+   return to;
+
+   to_len = to ? strlen(to) : 0;
+   from_len = strlen(from);
+
+   to = xrealloc(to, to_len + from_len + 1);
+   slprintf(to + to_len, from_len + 1, "%s", from);
+
+   return to;
+}
diff --git a/str.h b/str.h
index 7d078da..4b4ed22 100644
--- a/str.h
+++ b/str.h
@@ -9,5 +9,6 @@ extern int slprintf_nocheck(char *dst, size_t size, const char 
*fmt, ...);
 extern char *strtrim_right(char *p, char c);
 extern noinline void *xmemset(void *s, int c, size_t n);
 extern char *argv2str(int startind, int argc, char **argv);
+extern char *str_cat(char *to, char *from);
 
 #endif /* STR_H */
-- 
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 netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[netsniff-ng] Re: [PATCH v2] flowtop: Add runtime commands to filter flows by proto

2015-11-24 Thread Tobias Klauser
On 2015-11-22 at 10:39:06 +0100, Vadim Kochan  wrote:
> Add U/T/I/D/S runtime commands (same like for command line)
> to filter flows by UDP/TCP/ICMP/DCCP/SCTP proto.
> 
> Signed-off-by: Vadim Kochan 

Applied now, thanks.

-- 
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 netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.