2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
18 #include "trafgen_parser.tab.h"
19 #include "trafgen_conf.h"
24 #define YYERROR_VERBOSE 0
26 #define YYENABLE_NLS 1
27 #define YYLTYPE_IS_TRIVIAL 1
31 extern
int yylex(void);
32 extern
void yyerror(const char *);
36 static struct pktconf
*conf
= NULL
;
38 #define am(x) ((x)->len - 1)
40 static void dump_conf
(struct pktconf
*cfg
)
44 printf
("n %lu, gap %lu us, pkts %zu\n", cfg
->num
, cfg
->gap
, cfg
->len
);
47 for
(i
= 0; i
< cfg
->len
; ++i
) {
48 printf
("[%zu] pkt\n", i
);
49 printf
(" len %zu cnts %zu rnds %zu\n", cfg
->pkts
[i
].plen
,
50 cfg
->pkts
[i
].clen
, cfg
->pkts
[i
].rlen
);
52 for
(j
= 0; j
< cfg
->pkts
[i
].plen
; ++j
)
53 printf
("%02x ", cfg
->pkts
[i
].payload
[j
]);
55 for
(j
= 0; j
< cfg
->pkts
[i
].clen
; ++j
)
56 printf
(" cnt%zu [%u,%u], inc %u, off %ld\n",
57 j
, cfg
->pkts
[i
].cnt
[j
].min
,
58 cfg
->pkts
[i
].cnt
[j
].max
,
59 cfg
->pkts
[i
].cnt
[j
].inc
,
60 cfg
->pkts
[i
].cnt
[j
].off
);
61 for
(j
= 0; j
< cfg
->pkts
[i
].rlen
; ++j
)
62 printf
(" rnd%zu off %ld\n",
63 j
, cfg
->pkts
[i
].rnd
[j
].off
);
67 static void realloc_packet
(void)
70 conf
->pkts
= xrealloc
(conf
->pkts
, 1, conf
->len
* sizeof
(*conf
->pkts
));
71 fmemset
(&conf
->pkts
[am
(conf
)], 0, sizeof
(conf
->pkts
[am
(conf
)]));
74 static void set_byte
(uint8_t val
)
76 conf
->pkts
[am
(conf
)].plen
++;
77 conf
->pkts
[am
(conf
)].payload
= xrealloc
(conf
->pkts
[am
(conf
)].payload
,
78 1, conf
->pkts
[am
(conf
)].plen
);
80 conf
->pkts
[am
(conf
)].payload
[conf
->pkts
[am
(conf
)].plen
- 1] = val
;
83 static void set_fill
(uint8_t val
, size_t len
)
87 conf
->pkts
[am
(conf
)].plen
+= len
;
88 conf
->pkts
[am
(conf
)].payload
= xrealloc
(conf
->pkts
[am
(conf
)].payload
,
89 1, conf
->pkts
[am
(conf
)].plen
);
91 for
(i
= 0; i
< len
; ++i
) {
93 payload
[conf
->pkts
[am
(conf
)].plen
- 1 - i
] = val
;
97 static void set_rnd
(size_t len
)
101 conf
->pkts
[am
(conf
)].plen
+= len
;
102 conf
->pkts
[am
(conf
)].payload
= xrealloc
(conf
->pkts
[am
(conf
)].payload
,
103 1, conf
->pkts
[am
(conf
)].plen
);
105 for
(i
= 0; i
< len
; ++i
) {
106 conf
->pkts
[am
(conf
)].
107 payload
[conf
->pkts
[am
(conf
)].plen
- 1 - i
] =
108 (uint8_t) mt_rand_int32
();
120 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC
122 %token
' ' ',' '{' '}' '(' ')' '[' ']'
124 %token number_hex number_dec number_ascii number_bin number_oct
126 %type
<number
> number_hex number_dec number_ascii number_bin number_oct number
133 | packets inline_comment
{ }
141 : '{' ' ' payload
' ' '}' { realloc_packet
(); }
146 | payload elem_delimiter
{ }
160 : number_dec
{ $$
= $1; }
161 | number_hex
{ $$
= $1; }
162 | number_ascii
{ $$
= $1; }
163 | number_bin
{ $$
= $1; }
164 | number_oct
{ $$
= $1; }
168 : K_FILL
'(' number delimiter number
')' { set_fill
($3, $5); }
172 : K_RND
'(' number
')' { set_rnd
($3); }
176 : K_DRND
'(' number
')'
177 { printf
("Drnd times %u\n", $3); }
181 : K_SEQINC
'(' number delimiter number
')'
182 { printf
("Seqinc from %u times %u\n", $3, $5); }
183 | K_SEQINC
'(' number delimiter number delimiter number
')'
184 { printf
("Seqinc from %u times %u steps %u\n", $3, $5, $7); }
188 : K_SEQDEC
'(' number delimiter number
')'
189 { printf
("Seqdec from %u times %u\n", $3, $5); }
190 | K_SEQDEC
'(' number delimiter number delimiter number
')'
191 { printf
("Seqdec from %u times %u steps %u\n", $3, $5, $7); }
195 : K_DINC
'(' number delimiter number
')'
196 { printf
("Dinc from %u to %u\n", $3, $5); }
197 | K_DINC
'(' number delimiter number delimiter number
')'
198 { printf
("Seqinc from %u to %u stepping %u\n", $3, $5, $7); }
202 : K_DDEC
'(' number delimiter number
')'
203 { printf
("Ddec from %u to %u\n", $3, $5); }
204 | K_DDEC
'(' number delimiter number delimiter number
')'
205 { printf
("Ddec from %u to %u stepping %u\n", $3, $5, $7); }
209 : number
{ set_byte
((uint8_t) $1); }
222 int compile_packets
(char *file
, struct pktconf
*cfg
, int verbose
)
224 yyin
= fopen
(file
, "r");
226 panic
("Cannot open file!\n");
228 panic
("No config given!\n");
230 mt_init_by_seed_time
();
245 void yyerror(const char *err
)
247 panic
("Syntax error at line %d: '%s'! %s!\n",
248 yylineno
, yytext
, err
);