From 67222ff900a62ff72a915d7f59c2551664425da8 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Mon, 14 Jan 2013 11:44:41 +0100 Subject: [PATCH] trafgen: inline constant expression calculator Signed-off-by: Daniel Borkmann --- src/trafgen.c | 2 +- src/trafgen_lexer.l | 16 ++++++++++++- src/trafgen_parser.y | 68 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/trafgen.c b/src/trafgen.c index 6d9097fa..672ccc77 100644 --- a/src/trafgen.c +++ b/src/trafgen.c @@ -264,7 +264,7 @@ static void example(void) " # TCP Ackn. Number\n" " 0, 0, 0, 0,\n" " # TCP Header length + TCP SYN Flag\n" - " 0x80, 0b00000010,\n" + " 0x80, c8((1<<1)|(1<<6)),\n" " # Window Size\n" " 0x00, 0x10,\n" " # TCP Checksum (offset IP, offset TCP)\n" diff --git a/src/trafgen_lexer.l b/src/trafgen_lexer.l index 2844f8d9..af2c5890 100644 --- a/src/trafgen_lexer.l +++ b/src/trafgen_lexer.l @@ -51,7 +51,21 @@ number_ascii ([a-zA-Z]) "ddec" { return K_DDEC; } "seqinc" { return K_SEQINC; } "seqdec" { return K_SEQDEC; } - +"const8"|"c8" { return K_CONST8; } +"const16"|"c16" { return K_CONST16; } +"const32"|"c32" { return K_CONST32; } +"const64"|"c64" { return K_CONST64; } + +"-" { return '-'; } +"+" { return '+'; } +"*" { return '*'; } +"/" { return '/'; } +"%" { return '%'; } +"&" { return '&'; } +"|" { return '|'; } +"<" { return '<'; } +">" { return '>'; } +"^" { return '^'; } "{" { return '{'; } "}" { return '}'; } "(" { return '('; } diff --git a/src/trafgen_parser.y b/src/trafgen_parser.y index 21ae5911..c67b44ba 100644 --- a/src/trafgen_parser.y +++ b/src/trafgen_parser.y @@ -15,6 +15,7 @@ #include #include #include +#include #include "xmalloc.h" #include "trafgen_parser.tab.h" @@ -304,17 +305,19 @@ static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping, %} %union { - long int number; + long long int number; } %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE -%token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP +%token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CONST8 K_CONST16 K_CONST32 K_CONST64 -%token ',' '{' '}' '(' ')' '[' ']' ':' +%token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^' %token number -%type number +%type number expression + +%left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^' %% @@ -378,14 +381,71 @@ elem | dinc { } | ddec { } | csum { } + | const { } | inline_comment { } ; +expression + : number + { $$ = $1; } + | expression '+' expression + { $$ = $1 + $3; } + | expression '-' expression + { $$ = $1 - $3; } + | expression '*' expression + { $$ = $1 * $3; } + | expression '/' expression + { $$ = $1 / $3; } + | expression '%' expression + { $$ = $1 % $3; } + | expression '&' expression + { $$ = $1 & $3; } + | expression '|' expression + { $$ = $1 | $3; } + | expression '^' expression + { $$ = $1 ^ $3; } + | expression '<' '<' expression + { $$ = $1 << $4; } + | expression '>' '>' expression + { $$ = $1 >> $4; } + | '(' expression ')' + { $$ = $2;} + ; + fill : K_FILL '(' number delimiter number ')' { set_fill($3, $5); } ; +const + : K_CONST8 '(' expression ')' + { set_byte((uint8_t) $3); } + | K_CONST16 '(' expression ')' { + int i; + uint16_t __c = cpu_to_be16((uint16_t) $3); + uint8_t *ptr = (uint8_t *) &__c; + + for (i = 0; i < sizeof(__c); ++i) + set_byte(ptr[i]); + } + | K_CONST32 '(' expression ')' { + int i; + uint32_t __c = cpu_to_be32((uint32_t) $3); + uint8_t *ptr = (uint8_t *) &__c; + + for (i = 0; i < sizeof(__c); ++i) + set_byte(ptr[i]); + } + | K_CONST64 '(' expression ')' { + int i; + uint64_t __c = cpu_to_be64((uint64_t) $3); + uint8_t *ptr = (uint8_t *) &__c; + + for (i = 0; i < sizeof(__c); ++i) + set_byte(ptr[i]); + } + ; + rnd : K_RND '(' number ')' { set_rnd($3); } -- 2.11.4.GIT