On Mon, Nov 30, 2015 at 01:46:03AM +0200, Vadim Kochan wrote:
> Add -D option for bpfc & trafgen to make possible pass -Dkey=value
> to C preprocessor. Characters like .?*()[]-^|`!#$&<>;\ are quoted
> to do not trick with shell's special symbols (e.g. process spawning),
> \ is ignored only if it goes before " - to make possible pass escaped
> text for trafgen's conf file.
> 
> Signed-off-by: Vadim Kochan <[email protected]>
> ---
>  bpf_parser.y     |   6 ++--
>  bpfc.8           |   6 ++++
>  bpfc.c           |  18 ++++++++--
>  cpp.c            | 104 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++---
>  cpp.h            |   2 +-
>  trafgen.8        |   7 ++++
>  trafgen.c        |  25 +++++++++----
>  trafgen_conf.h   |   3 +-
>  trafgen_parser.y |   5 +--
>  9 files changed, 155 insertions(+), 21 deletions(-)
> 
> diff --git a/bpf_parser.y b/bpf_parser.y
> index 003c031..28e8d11 100644
> diff --git a/bpfc.c b/bpfc.c
> index d360cf5..1d4388f 100644
> --- a/bpfc.c
> +++ b/bpfc.c
> @@ -18,11 +18,12 @@
>  #include "bpf.h"
>  #include "config.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'},
> +     {"define",      required_argument,      NULL, 'D'},
>       {"verbose",     no_argument,            NULL, 'V'},
>       {"bypass",      no_argument,            NULL, 'b'},
>       {"dump",        no_argument,            NULL, 'd'},
> @@ -39,7 +40,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_defs);
>  
>  static void __noreturn help(void)
>  {
> @@ -49,6 +50,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|--define             Add macro/define 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 +83,8 @@ int main(int argc, char **argv)
>  {
>       int ret, verbose = 0, c, opt_index, bypass = 0, format = 0;
>       bool invoke_cpp = false;
> +     char **cpp_defs = NULL;
> +     int defs_count = 0;
>       char *file = NULL;
>  
>       setfsuid(getuid());
> @@ -89,6 +93,8 @@ int main(int argc, char **argv)
>       if (argc == 1)
>               help();
>  
> +     cpp_defs = (char **)xmalloc(sizeof(char *) * argc);

I will re-work this line to allocate char * pointer on demand

> diff --git a/trafgen.c b/trafgen.c
> index d01f160..86cef60 100644
> --- a/trafgen.c
> +++ b/trafgen.c
> @@ -65,6 +65,8 @@ struct ctx {
>       struct timespec gap;
>       struct sockaddr_in dest;
>       char *packet_str;
> +     bool invoke_cpp;
> +     char **cpp_defs;
>  };
>  
>  struct cpu_stats {
> @@ -83,7 +85,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'},
> @@ -105,6 +107,7 @@ static const struct option long_options[] = {
>       {"jumbo-support",       no_argument,            NULL, 'J'},
>       {"no-cpu-stats",        no_argument,            NULL, 'C'},
>       {"cpp",                 no_argument,            NULL, 'p'},
> +     {"define",              required_argument,      NULL, 'D'},
>       {"rfraw",               no_argument,            NULL, 'R'},
>       {"rand",                no_argument,            NULL, 'r'},
>       {"verbose",             no_argument,            NULL, 'V'},
> @@ -163,6 +166,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|--define                    Add macro/define 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 +827,12 @@ 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, 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, ctx->invoke_cpp, 
> ctx->cpp_defs);
>  
>       preprocess_packets();
>  
> @@ -886,7 +890,7 @@ static void on_panic_del_rfmon(void *arg)
>  
>  int main(int argc, char **argv)
>  {
> -     bool slow = false, invoke_cpp = false, reseed = true, cpustats = true;
> +     bool slow = false, reseed = true, cpustats = true;
>       bool prio_high = false, set_irq_aff = true, set_sock_mem = true;
>       int c, opt_index, vals[4] = {0}, irq;
>       uint64_t gap = 0;
> @@ -896,12 +900,15 @@ int main(int argc, char **argv)
>       unsigned long long tx_packets, tx_bytes;
>       struct ctx ctx;
>       int min_opts = 5;
> +     int defs_count = 0;
>  
>       fmemset(&ctx, 0, sizeof(ctx));
>       ctx.cpus = get_number_cpus_online();
>       ctx.uid = getuid();
>       ctx.gid = getgid();
>       ctx.qdisc_path = false;
> +     ctx.cpp_defs = (char **)xmalloc(sizeof(char *) * argc);

I will re-work this line to allocate char * pointer on demand

-- 
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.

Reply via email to