flowtop: make function nested
[netsniff-ng.git] / src / trafgen_parser.y
blob6dc0a1ce2365811fc50be2f37e616aaebb61c916
1 /*
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.
7 */
9 /* yaac-func-prefix: yy */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <signal.h>
16 #include <stdint.h>
17 #include <errno.h>
19 #include "xmalloc.h"
20 #include "trafgen_parser.tab.h"
21 #include "trafgen_conf.h"
22 #include "built_in.h"
23 #include "die.h"
24 #include "mtrand.h"
26 #define YYERROR_VERBOSE 0
27 #define YYDEBUG 0
28 #define YYENABLE_NLS 1
29 #define YYLTYPE_IS_TRIVIAL 1
30 #define ENABLE_NLS 1
32 extern FILE *yyin;
33 extern int yylex(void);
34 extern void yyerror(const char *);
35 extern int yylineno;
36 extern char *yytext;
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");
56 dfunc_note_flag = 1;
60 static inline void init_new_packet_slot(struct packet *slot)
62 slot->payload = NULL;
63 slot->len = 0;
66 static inline void init_new_counter_slot(struct packet_dynamics *slot)
68 slot->counter = NULL;
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)
80 packets_len++;
81 packets = xrealloc(packets, 1, packets_len * sizeof(*packets));
83 init_new_packet_slot(&packets[packets_last]);
85 packet_dyn_len++;
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)
103 int i;
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)
114 int i;
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)
126 int i;
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;
134 start += stepping;
138 static void set_seqdec(uint8_t start, size_t len, uint8_t stepping)
140 int i;
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;
148 start -= stepping;
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;
156 counter->max = stop;
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)
171 give_note_dynamic();
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)
189 give_note_dynamic();
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);
207 %union {
208 long int number;
211 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
212 %token K_NEWL
214 %token ',' '{' '}' '(' ')' '[' ']'
216 %token number
218 %type <number> number
222 packets
223 : { }
224 | packets packet { }
225 | packets inline_comment { }
226 | packets white { }
229 inline_comment
230 : K_COMMENT { }
233 packet
234 : '{' delimiter payload delimiter '}' { realloc_packet(); }
237 payload
238 : elem { }
239 | payload elem_delimiter { }
242 white
243 : white K_WHITE { }
244 | white K_NEWL { }
245 | K_WHITE { }
246 | K_NEWL { }
249 delimiter
250 : ',' { }
251 | white { }
252 | ',' white { }
255 elem_delimiter
256 : delimiter elem { }
259 fill
260 : K_FILL '(' number delimiter number ')'
261 { set_fill($3, $5); }
265 : K_RND '(' number ')'
266 { set_rnd($3); }
269 seqinc
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); }
276 seqdec
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); }
283 drnd
284 : K_DRND '(' ')'
285 { set_drnd(); }
286 | K_DRND '(' number ')'
288 int i, max = $3;
289 for (i = 0; i < max; ++i)
290 set_drnd();
294 dinc
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); }
301 ddec
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); }
308 elem
309 : number { set_byte((uint8_t) $1); }
310 | fill { }
311 | rnd { }
312 | drnd { }
313 | seqinc { }
314 | seqdec { }
315 | dinc { }
316 | ddec { }
317 | inline_comment { }
322 static void finalize_packet(void)
324 /* XXX hack ... we allocated one packet pointer too much */
325 packets_len--;
326 packet_dyn_len--;
329 static void dump_conf(void)
331 size_t i, j;
333 for (i = 0; i < packets_len; ++i) {
334 printf("[%zu] pkt\n", i);
335 printf(" len %zu cnts %zu rnds %zu\n",
336 packets[i].len,
337 packet_dyns[i].counter_len,
338 packet_dyns[i].randomizer_len);
340 printf(" payload ");
341 for (j = 0; j < packets[i].len; ++j)
342 printf("%02x ", packets[i].payload[j]);
343 printf("\n");
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 ?
352 "inc" : "dec");
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)
362 int i;
364 for (i = 0; i < packets_len; ++i) {
365 if (packets[i].len > 0)
366 xfree(packets[i].payload);
369 if (packets_len > 0)
370 xfree(packets);
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)
381 xfree(packet_dyns);
384 int compile_packets(char *file, int verbose)
386 mt_init_by_seed_time();
388 yyin = fopen(file, "r");
389 if (!yyin)
390 panic("Cannot open file!\n");
392 realloc_packet();
393 yyparse();
394 finalize_packet();
396 if (verbose) {
397 dump_conf();
398 } else {
399 int i;
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);
408 fclose(yyin);
409 return 0;
412 void yyerror(const char *err)
414 panic("Syntax error at line %d: '%s'! %s!\n", yylineno, yytext, err);