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 <vadi...@gmail.com> --- 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 <cfg/-> Packet configuration file/stdin\n" " -o|-d|--out|--dev <netdev> 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 <ipv4> 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, unsigned int cpu, bool invoke_cpp); +extern void compile_packets(char *file, bool verbose, unsigned int cpu, + bool invoke_cpp, char *cpp_args); extern void cleanup_packets(void); #endif /* TRAFGEN_CONF */ diff --git a/trafgen_parser.y b/trafgen_parser.y index 8a0f3b7..b3e7bb1 100644 --- a/trafgen_parser.y +++ b/trafgen_parser.y @@ -590,7 +590,8 @@ void cleanup_packets(void) free(packet_dyn); } -void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp) +void compile_packets(char *file, bool verbose, unsigned int cpu, bool invoke_cpp, + char *cpp_args) { char tmp_file[128]; int ret = -1; @@ -599,7 +600,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), NULL)) { + if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_args)) { 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.