On 2015-11-25 at 00:51:50 +0100, Vadim Kochan <[email protected]> wrote:
> 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 <[email protected]>
> ---
> 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
I would go for --define here. You can still use --def on the command
line as it is the only unique prefix.
> +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"\\\\\\"\\".
You might want to do some wordsmithing here, e.g. have a look at the gcc
manpage where -D is documented. I also don't think that this is the
right example usage here. Please stick to something more general like
-DNAME=definition here and if need be add an extra paragraph documenting
the escaping rules. If you add the escaping example, you should also
show how it will show up in the preprocessor. IMO instead of taking the
work of explaining this all by ourselves, we should rather add a
sentence like:
If you are invoking the preprocessor from a shell or shell-like program
you may need to use the shell's quoting syntax to protect characters
such as spaces that have a meaning in the shell syntax.
(taken from the cpp manpage)
> +.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);
As mentioned in the comments for 2/5, please fill all optargs into an
char** to pass them to cpp_exec instead of creating a string and
introducing potentially dangerous commands.
--
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.