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.
9 /* yaac-func-prefix: yy */
20 #include "trafgen_parser.tab.h"
21 #include "trafgen_conf.h"
26 #define YYERROR_VERBOSE 0
28 #define YYENABLE_NLS 1
29 #define YYLTYPE_IS_TRIVIAL 1
33 extern
int yylex(void);
34 extern
void yyerror(const char *);
38 extern
struct packet
*packets
;
39 extern
unsigned int packets_len
;
40 #define packets_last (packets_len - 1)
41 #define payload_last (packets[packets_last].len - 1)
43 extern
struct packet_dynamics
*packet_dyns
;
44 extern
unsigned int packet_dyn_len
;
45 #define packetds_last (packet_dyn_len - 1)
46 #define packetds_c_last (packet_dyns[packetds_last].counter_len - 1)
47 #define packetds_r_last (packet_dyns[packetds_last].randomizer_len - 1)
49 static int dfunc_note_flag
= 0;
51 static void give_note_dynamic
(void)
53 if
(!dfunc_note_flag
) {
54 printf
("Note: dynamic elements like drnd, dinc, ddec and "
55 "others make trafgen slower!\n");
60 static inline
void init_new_packet_slot
(struct packet
*slot
)
66 static inline
void init_new_counter_slot
(struct packet_dynamics
*slot
)
69 slot
->counter_len
= 0;
72 static inline
void init_new_randomizer_slot
(struct packet_dynamics
*slot
)
74 slot
->randomizer
= NULL
;
75 slot
->randomizer_len
= 0;
78 static void realloc_packet
(void)
81 packets
= xrealloc
(packets
, 1, packets_len
* sizeof
(*packets
));
83 init_new_packet_slot
(&packets
[packets_last
]);
86 packet_dyns
= xrealloc
(packet_dyns
, 1,
87 packet_dyn_len
* sizeof
(*packet_dyns
));
89 init_new_counter_slot
(&packet_dyns
[packetds_last
]);
90 init_new_randomizer_slot
(&packet_dyns
[packetds_last
]);
93 static void set_byte
(uint8_t val
)
95 packets
[packets_last
].len
++;
96 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
97 1, packets
[packets_last
].len
);
98 packets
[packets_last
].payload
[payload_last
] = val
;
101 static void set_fill
(uint8_t val
, size_t len
)
105 packets
[packets_last
].len
+= len
;
106 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
107 1, packets
[packets_last
].len
);
108 for
(i
= 0; i
< len
; ++i
)
109 packets
[packets_last
].payload
[payload_last
- i
] = val
;
112 static void set_rnd
(size_t len
)
116 packets
[packets_last
].len
+= len
;
117 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
118 1, packets
[packets_last
].len
);
119 for
(i
= 0; i
< len
; ++i
)
120 packets
[packets_last
].payload
[payload_last
- i
] =
121 (uint8_t) mt_rand_int32
();
124 static void set_seqinc
(uint8_t start
, size_t len
, uint8_t stepping
)
128 packets
[packets_last
].len
+= len
;
129 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
130 1, packets
[packets_last
].len
);
131 for
(i
= 0; i
< len
; ++i
) {
132 int off
= len
- 1 - i
;
133 packets
[packets_last
].payload
[payload_last
- off
] = start
;
138 static void set_seqdec
(uint8_t start
, size_t len
, uint8_t stepping
)
142 packets
[packets_last
].len
+= len
;
143 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
144 1, packets
[packets_last
].len
);
145 for
(i
= 0; i
< len
; ++i
) {
146 int off
= len
- 1 - i
;
147 packets
[packets_last
].payload
[payload_last
- off
] = start
;
152 static inline
void setup_new_counter
(struct counter
*counter
, uint8_t start
,
153 uint8_t stop
, uint8_t stepping
, int type
)
155 counter
->min
= start
;
157 counter
->inc
= stepping
;
158 counter
->val
= (type
== TYPE_INC
) ? start
: stop
;
159 counter
->off
= payload_last
;
160 counter
->type
= type
;
163 static inline
void setup_new_randomizer
(struct randomizer
*randomizer
)
165 randomizer
->val
= (uint8_t) mt_rand_int32
();
166 randomizer
->off
= payload_last
;
169 static void set_drnd
(void)
173 packets
[packets_last
].len
++;
174 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
175 1, packets
[packets_last
].len
);
177 packet_dyns
[packetds_last
].randomizer_len
++;
178 packet_dyns
[packetds_last
].randomizer
=
179 xrealloc
(packet_dyns
[packetds_last
].randomizer
, 1,
180 packet_dyns
[packetds_last
].randomizer_len
*
181 sizeof
(struct randomizer
));
183 setup_new_randomizer
(&packet_dyns
[packetds_last
].
184 randomizer
[packetds_r_last
]);
187 static void set_dincdec
(uint8_t start
, uint8_t stop
, uint8_t stepping
, int type
)
191 packets
[packets_last
].len
++;
192 packets
[packets_last
].payload
= xrealloc
(packets
[packets_last
].payload
,
193 1, packets
[packets_last
].len
);
195 packet_dyns
[packetds_last
].counter_len
++;
196 packet_dyns
[packetds_last
].counter
=
197 xrealloc
(packet_dyns
[packetds_last
].counter
, 1,
198 packet_dyns
[packetds_last
].counter_len
*
199 sizeof
(struct counter
));
201 setup_new_counter
(&packet_dyns
[packetds_last
].counter
[packetds_c_last
],
202 start
, stop
, stepping
, type
);
211 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
214 %token
',' '{' '}' '(' ')' '[' ']'
218 %type
<number
> number
225 | packets inline_comment
{ }
234 : '{' delimiter payload delimiter
'}' { realloc_packet
(); }
239 | payload elem_delimiter
{ }
260 : K_FILL
'(' number delimiter number
')'
261 { set_fill
($3, $5); }
265 : K_RND
'(' number
')'
270 : K_SEQINC
'(' number delimiter number
')'
271 { set_seqinc
($3, $5, 1); }
272 | K_SEQINC
'(' number delimiter number delimiter number
')'
273 { set_seqinc
($3, $5, $7); }
277 : K_SEQDEC
'(' number delimiter number
')'
278 { set_seqdec
($3, $5, 1); }
279 | K_SEQDEC
'(' number delimiter number delimiter number
')'
280 { set_seqdec
($3, $5, $7); }
286 | K_DRND
'(' number
')'
289 for
(i
= 0; i
< max
; ++i
)
295 : K_DINC
'(' number delimiter number
')'
296 { set_dincdec
($3, $5, 1, TYPE_INC
); }
297 | K_DINC
'(' number delimiter number delimiter number
')'
298 { set_dincdec
($3, $5, $7, TYPE_INC
); }
302 : K_DDEC
'(' number delimiter number
')'
303 { set_dincdec
($3, $5, 1, TYPE_DEC
); }
304 | K_DDEC
'(' number delimiter number delimiter number
')'
305 { set_dincdec
($3, $5, $7, TYPE_DEC
); }
309 : number
{ set_byte
((uint8_t) $1); }
322 static void finalize_packet
(void)
324 /* XXX hack ... we allocated one packet pointer too much */
329 static void dump_conf
(void)
333 for
(i
= 0; i
< packets_len
; ++i
) {
334 printf
("[%zu] pkt\n", i
);
335 printf
(" len %zu cnts %zu rnds %zu\n",
337 packet_dyns
[i
].counter_len
,
338 packet_dyns
[i
].randomizer_len
);
341 for
(j
= 0; j
< packets
[i
].len
; ++j
)
342 printf
("%02x ", packets
[i
].payload
[j
]);
345 for
(j
= 0; j
< packet_dyns
[i
].counter_len
; ++j
)
346 printf
(" cnt%zu [%u,%u], inc %u, off %ld type %s\n", j
,
347 packet_dyns
[i
].counter
[j
].min
,
348 packet_dyns
[i
].counter
[j
].max
,
349 packet_dyns
[i
].counter
[j
].inc
,
350 packet_dyns
[i
].counter
[j
].off
,
351 packet_dyns
[i
].counter
[j
].type
== TYPE_INC ?
354 for
(j
= 0; j
< packet_dyns
[i
].randomizer_len
; ++j
)
355 printf
(" rnd%zu off %ld\n", j
,
356 packet_dyns
[i
].randomizer
[j
].off
);
360 void cleanup_packets
(void)
364 for
(i
= 0; i
< packets_len
; ++i
) {
365 if
(packets
[i
].len
> 0)
366 xfree
(packets
[i
].payload
);
372 for
(i
= 0; i
< packet_dyn_len
; ++i
) {
373 if
(packet_dyns
[i
].counter_len
> 0)
374 xfree
(packet_dyns
[i
].counter
);
376 if
(packet_dyns
[i
].randomizer_len
> 0)
377 xfree
(packet_dyns
[i
].randomizer
);
380 if
(packet_dyn_len
> 0)
384 int compile_packets
(char *file
, int verbose
)
386 mt_init_by_seed_time
();
388 yyin
= fopen
(file
, "r");
390 panic
("Cannot open file!\n");
400 size_t total_len
= 0;
402 printf
("%u packets to schedule\n", packets_len
);
403 for
(i
= 0; i
< packets_len
; ++i
)
404 total_len
+= packets
[i
].len
;
405 printf
("%zu bytes in total\n", total_len
);
412 void yyerror(const char *err
)
414 panic
("Syntax error at line %d: '%s'! %s!\n", yylineno
, yytext
, err
);