summaryrefslogtreecommitdiff
path: root/trafgen_parser.y
blob: b5fcbc0e0558e1cb6c932be69cc26498afe8468c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
/*
 * netsniff-ng - the packet sniffing beast
 * By Daniel Borkmann <daniel@netsniff-ng.org>
 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
 * Swiss federal institute of technology (ETH Zurich)
 * Subject to the GPL, version 2.
 */

/* yaac-func-prefix: yy */

%{

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <libgen.h>
#include <signal.h>
#include <unistd.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <linux/icmp.h>
#include <linux/if_ether.h>
#include <linux/icmpv6.h>

#include "xmalloc.h"
#include "trafgen_parser.tab.h"
#include "trafgen_conf.h"
#include "trafgen_proto.h"
#include "trafgen_l2.h"
#include "trafgen_l3.h"
#include "trafgen_l4.h"
#include "built_in.h"
#include "die.h"
#include "str.h"
#include "csum.h"
#include "cpp.h"

#ifndef ETH_P_8021AD
#define ETH_P_8021AD	0x88A8
#endif

#define YYERROR_VERBOSE		0
#define YYDEBUG			0
#define YYENABLE_NLS		1
#define YYLTYPE_IS_TRIVIAL	1
#define ENABLE_NLS		1

extern FILE *yyin;
extern int yylex(void);
extern void yy_scan_string(char *);
extern void yylex_destroy();
extern void yyerror(const char *);
extern int yylineno;
extern char *yytext;

extern struct packet *packets;
extern size_t plen;

#define packet_last		(plen - 1)

#define payload_last		(packets[packet_last].len - 1)

extern struct packet_dyn *packet_dyn;
extern size_t dlen;

#define packetd_last		(dlen - 1)

#define packetdc_last		(packet_dyn[packetd_last].clen - 1)
#define packetdr_last		(packet_dyn[packetd_last].rlen - 1)
#define packetds_last		(packet_dyn[packetd_last].slen - 1)

static int our_cpu, min_cpu = -1, max_cpu = -1;

enum field_expr_type_t {
	FIELD_EXPR_UNKNOWN	= 0,
	FIELD_EXPR_NUMB		= 1 << 0,
	FIELD_EXPR_MAC		= 1 << 1,
	FIELD_EXPR_IP4_ADDR	= 1 << 2,
	FIELD_EXPR_IP6_ADDR	= 1 << 3,
	FIELD_EXPR_INC		= 1 << 4,
	FIELD_EXPR_RND		= 1 << 5,
	FIELD_EXPR_OFFSET	= 1 << 6,
};

struct proto_field_expr {
	enum field_expr_type_t type;
	struct proto_field *field;

	union {
		struct in_addr ip4_addr;
		struct in6_addr ip6_addr;
		long long int number;
		uint8_t bytes[256];
		struct proto_field_func func;
	} val;
};

static struct proto_field_expr field_expr;
static struct proto_hdr *hdr;

static inline int test_ignore(void)
{
	if (min_cpu < 0 && max_cpu < 0)
		return 0;
	else if (max_cpu >= our_cpu && min_cpu <= our_cpu)
		return 0;
	else
		return 1;
}

static inline void __init_new_packet_slot(struct packet *slot)
{
	memset(slot, 0, sizeof(*slot));
}

static inline void __init_new_counter_slot(struct packet_dyn *slot)
{
	slot->cnt = NULL;
	slot->clen = 0;
}

static inline void __init_new_randomizer_slot(struct packet_dyn *slot)
{
	slot->rnd = NULL;
	slot->rlen = 0;
}

static inline void __init_new_csum_slot(struct packet_dyn *slot)
{
	slot->csum = NULL;
	slot->slen = 0;
}

static inline void __init_new_fields_slot(struct packet_dyn *slot)
{
	slot->fields = NULL;
	slot->flen = 0;
}

static inline void __setup_new_counter(struct counter *c, uint8_t start,
				       uint8_t stop, uint8_t stepping,
				       int type)
{
	c->min = start;
	c->max = stop;
	c->inc = stepping;
	c->val = (type == TYPE_INC) ? start : stop;
	c->off = payload_last;
	c->type = type;
}

static inline void __setup_new_randomizer(struct randomizer *r)
{
	r->off = payload_last;
}

static inline void __setup_new_csum16(struct csum16 *s, off_t from, off_t to,
				      enum csum which)
{
	s->off = payload_last - 1;
	s->from = from;
	s->to = to;
	s->which = which;
}

static void realloc_packet(void)
{
	if (test_ignore())
		return;

	plen++;
	packets = xrealloc(packets, plen * sizeof(*packets));

	__init_new_packet_slot(&packets[packet_last]);

	dlen++;
	packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));

	__init_new_counter_slot(&packet_dyn[packetd_last]);
	__init_new_randomizer_slot(&packet_dyn[packetd_last]);
	__init_new_csum_slot(&packet_dyn[packetd_last]);
	__init_new_fields_slot(&packet_dyn[packetd_last]);
}

struct packet *current_packet(void)
{
	return &packets[packet_last];
}

uint32_t current_packet_id(void)
{
	return packet_last;
}

struct packet *packet_get(uint32_t id)
{
	return &packets[id];
}

static void set_byte(uint8_t val)
{
	struct packet *pkt = &packets[packet_last];

	if (test_ignore())
		return;

	pkt->len++;
	pkt->payload = xrealloc(pkt->payload, pkt->len);
	pkt->payload[payload_last] = val;
}

static void set_multi_byte(uint8_t *s, size_t len)
{
	size_t i;

	for (i = 0; i < len; ++i)
		set_byte(s[i]);
}

void set_fill(uint8_t val, size_t len)
{
	size_t i;
	struct packet *pkt = &packets[packet_last];

	if (test_ignore())
		return;

	pkt->len += len;
	pkt->payload = xrealloc(pkt->payload, pkt->len);
	for (i = 0; i < len; ++i)
		pkt->payload[payload_last - i] = val;
}

static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
{
	struct packet *pkt = &packets[packet_last];
	struct packet_dyn *pktd = &packet_dyn[packetd_last];

	pkt->len += 2;
	pkt->payload = xrealloc(pkt->payload, pkt->len);

	pktd->slen++;
	pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));

	__setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
}

static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
{
	struct packet *pkt = &packets[packet_last];
	uint16_t sum;
	uint8_t *psum;

	sum = htons(calc_csum(pkt->payload + from, to - from));
	psum = (uint8_t *) &sum;

	set_byte(psum[0]);
	set_byte(psum[1]);
}

static inline bool is_dynamic_csum(enum csum which)
{
	switch (which) {
	case CSUM_UDP:
	case CSUM_TCP:
	case CSUM_UDP6:
	case CSUM_TCP6:
		return true;
	default:
		return false;
	}
}

static void set_csum16(size_t from, size_t to, enum csum which)
{
	struct packet *pkt = &packets[packet_last];
	struct packet_dyn *pktd = &packet_dyn[packetd_last];

	if (test_ignore())
		return;

	if (to < from) {
		size_t tmp = to;

		to = from;
		from = tmp;
	}

	bug_on(!(from < to));

	if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
		__set_csum16_dynamic(from, to, which);
	else
		__set_csum16_static(from, to, which);
}

static void set_rnd(size_t len)
{
	size_t i;
	struct packet *pkt = &packets[packet_last];

	if (test_ignore())
		return;

	pkt->len += len;
	pkt->payload = xrealloc(pkt->payload, pkt->len);
	for (i = 0; i < len; ++i)
		pkt->payload[payload_last - i] = (uint8_t) rand();
}

static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
{
	size_t i;
	struct packet *pkt = &packets[packet_last];

	if (test_ignore())
		return;

	pkt->len += len;
	pkt->payload = xrealloc(pkt->payload, pkt->len);
	for (i = 0; i < len; ++i) {
		off_t off = len - 1 - i;

		pkt->payload[payload_last - off] = start;
		start += stepping;
	}
}

static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
{
	size_t i;
	struct packet *pkt = &packets[packet_last];

	if (test_ignore())
		return;

	pkt->len += len;
	pkt->payload = xrealloc(pkt->payload, pkt->len);
	for (i = 0; i < len; ++i) {
		int off = len - 1 - i;

		pkt->payload[payload_last - off] = start;
		start -= stepping;
	}
}

static void set_dynamic_rnd(void)
{
	struct packet *pkt = &packets[packet_last];
	struct packet_dyn *pktd = &packet_dyn[packetd_last];

	if (test_ignore())
		return;

	pkt->len++;
	pkt->payload = xrealloc(pkt->payload, pkt->len);

	pktd->rlen++;
	pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));

	__setup_new_randomizer(&pktd->rnd[packetdr_last]);
}

static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
			       int type)
{
	struct packet *pkt = &packets[packet_last];
	struct packet_dyn *pktd = &packet_dyn[packetd_last];

	if (test_ignore())
		return;

	pkt->len++;
	pkt->payload = xrealloc(pkt->payload, pkt->len);

	pktd->clen++;
	pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));

	__setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
}

static void proto_add(enum proto_id pid)
{
	hdr = proto_header_push(pid);
}

static void proto_field_set(uint32_t fid)
{
	memset(&field_expr, 0, sizeof(field_expr));
	field_expr.field = proto_hdr_field_by_id(hdr, fid);
}

static void proto_field_func_setup(struct proto_field *field, struct proto_field_func *func)
{
	struct proto_field *field_copy;
	struct packet_dyn *pkt_dyn;

	field_copy = xmalloc(sizeof(*field));
	memcpy(field_copy, field, sizeof(*field));

	field_copy->pkt_offset += func->offset;
	if (func->len)
		field_copy->len = func->len;

	proto_field_func_add(field_copy, func);

	pkt_dyn = &packet_dyn[packetd_last];
	pkt_dyn->flen++;
	pkt_dyn->fields = xrealloc(pkt_dyn->fields, pkt_dyn->flen *
				   sizeof(struct proto_field *));

	pkt_dyn->fields[pkt_dyn->flen - 1] = field_copy;
}

static void proto_field_expr_eval(void)
{
	struct proto_field *field = field_expr.field;

	if ((field_expr.type & FIELD_EXPR_OFFSET) &&
			!((field_expr.type & FIELD_EXPR_INC) ||
				(field_expr.type & FIELD_EXPR_RND))) {

		panic("Field offset expression is valid only with function expression\n");
	}

	if (field_expr.type & FIELD_EXPR_NUMB) {
		if (field->len == 1)
			proto_hdr_field_set_u8(hdr, field->id, field_expr.val.number);
		else if (field->len == 2)
			proto_hdr_field_set_be16(hdr, field->id, field_expr.val.number);
		else if (field->len == 4)
			proto_hdr_field_set_be32(hdr, field->id, field_expr.val.number);
		else
			proto_hdr_field_set_bytes(hdr, field->id, field_expr.val.bytes);
	} else if (field_expr.type & FIELD_EXPR_MAC) {
		proto_hdr_field_set_bytes(hdr, field->id, field_expr.val.bytes);
	} else if (field_expr.type & FIELD_EXPR_IP4_ADDR) {
		proto_hdr_field_set_u32(hdr, field->id, field_expr.val.ip4_addr.s_addr);
	} else if (field_expr.type & FIELD_EXPR_IP6_ADDR) {
		proto_hdr_field_set_bytes(hdr, field->id,
			(uint8_t *)&field_expr.val.ip6_addr.s6_addr);
	} else if ((field_expr.type & FIELD_EXPR_INC) ||
			(field_expr.type & FIELD_EXPR_RND)) {

		if (field_expr.val.func.min
			&& field_expr.val.func.min >= field_expr.val.func.max)
			panic("dinc(): min(%u) can't be >= max(%u)\n",
				field_expr.val.func.min, field_expr.val.func.max);

		proto_field_func_setup(field, &field_expr.val.func);
	} else if ((field_expr.type & FIELD_EXPR_OFFSET) &&
			!((field_expr.type & FIELD_EXPR_INC) ||
				(field_expr.type & FIELD_EXPR_RND))) {

		panic("Field expression is valid only for function value expression\n");
	} else {
		bug();
	}

	memset(&field_expr, 0, sizeof(field_expr));
}

static void field_index_validate(struct proto_field *field, uint16_t index, size_t len)
{
	if (field_expr.field->len <= index) {
		yyerror("Invalid [index] parameter");
		panic("Index (%u) is bigger than field's length (%zu)\n",
		       index, field->len);
	}
	if (len != 1 && len != 2 && len != 4) {
		yyerror("Invalid [index:len] parameter");
		panic("Invalid index length - 1,2 or 4 is only allowed\n");
	}
}

%}

%union {
	struct in_addr ip4_addr;
	struct in6_addr ip6_addr;
	long long int number;
	uint8_t bytes[256];
	char *str;
}

%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 K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64

%token K_DADDR K_SADDR K_ETYPE K_TYPE
%token K_TIME K_PRIO
%token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
%token K_PROT K_TTL K_DSCP K_ECN K_TOS K_LEN K_ID K_FLAGS K_FRAG K_IHL K_VER K_CSUM K_DF K_MF
%token K_FLOW K_NEXT_HDR K_HOP_LIMIT
%token K_CODE K_ECHO_REQUEST K_ECHO_REPLY
%token K_SPORT K_DPORT
%token K_SEQ K_ACK_SEQ K_DOFF K_CWR K_ECE K_URG K_ACK K_PSH K_RST K_SYN K_FIN K_WINDOW K_URG_PTR
%token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
%token K_LABEL K_TC K_LAST K_EXP

%token K_ADDR K_MTU

%token K_ETH
%token K_PAUSE
%token K_PFC
%token K_VLAN K_MPLS
%token K_ARP
%token K_IP4 K_IP6
%token K_ICMP4 K_ICMP6
%token K_UDP K_TCP

%token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'

%token number string mac ip4_addr ip6_addr

%type <number> number expression
%type <str> string
%type <bytes> mac
%type <ip4_addr> ip4_addr
%type <ip6_addr> ip6_addr

%left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'

%%

packets
	: { }
	| packets packet { }
	| packets inline_comment { }
	| packets K_WHITE { }
	;

inline_comment
	: K_COMMENT { }
	;

cpu_delim
	: ':' { }
	| '-' { }
	;

delimiter_nowhite
	: ',' { }
	| ',' K_WHITE { }
	;

noenforce_white
	: { }
	| K_WHITE { }
	| delimiter_nowhite { }
	;

skip_white
	: { }
	| K_WHITE { }
	;
packet
	: '{' noenforce_white payload noenforce_white '}' {
			min_cpu = max_cpu = -1;

			proto_packet_finish();

			realloc_packet();
		}
	| K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
			min_cpu = $3;
			max_cpu = $5;

			if (min_cpu > max_cpu) {
				int tmp = min_cpu;

				min_cpu = max_cpu;
				max_cpu = tmp;
			}

			proto_packet_finish();

			realloc_packet();
		}
	| K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
			min_cpu = max_cpu = $3;

			proto_packet_finish();

			realloc_packet();
		}
	;

payload
	: elem { }
	| payload elem_delimiter { }
	;

delimiter
	: delimiter_nowhite { }
	| K_WHITE { }
	;

elem_delimiter
	: delimiter elem { }
	;

elem
	: number { set_byte((uint8_t) $1); }
	| string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
	| fill { }
	| rnd { }
	| drnd { }
	| seqinc { }
	| seqdec { }
	| dinc { }
	| ddec { }
	| csum { }
	| const { }
	| proto { proto_header_finish(hdr); }
	| 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
		{ $$ = -1 * $2; }
	| '(' expression ')'
		{ $$ = $2;}
	;

fill
	: K_FILL '(' number delimiter number ')'
		{ set_fill($3, $5); }
	;

const
	: K_CONST8 '(' expression ')'
		{ set_byte((uint8_t) $3); }
	| K_CONST16 '(' expression ')' {
			uint16_t __c = cpu_to_be16((uint16_t) $3);

			set_multi_byte((uint8_t *) &__c, sizeof(__c));
		}
	| K_CONST32 '(' expression ')' {
			uint32_t __c = cpu_to_be32((uint32_t) $3);

			set_multi_byte((uint8_t *) &__c, sizeof(__c));
		}
	| K_CONST64 '(' expression ')' {
			uint64_t __c = cpu_to_be64((uint64_t) $3);

			set_multi_byte((uint8_t *) &__c, sizeof(__c));
		}
	;

rnd
	: K_RND '(' number ')'
		{ set_rnd($3); }
	;

csum
	: K_CSUMIP '(' number delimiter number ')'
		{ set_csum16($3, $5, CSUM_IP); }
	| K_CSUMTCP '(' number delimiter number ')'
		{ set_csum16($3, $5, CSUM_TCP); }
	| K_CSUMUDP '(' number delimiter number ')'
		{ set_csum16($3, $5, CSUM_UDP); }
	| K_CSUMTCP6 '(' number delimiter number ')'
		{ set_csum16($3, $5, CSUM_TCP6); }
	| K_CSUMUDP6 '(' number delimiter number ')'
		{ set_csum16($3, $5, CSUM_UDP6); }
	;

seqinc
	: K_SEQINC '(' number delimiter number ')'
		{ set_sequential_inc($3, $5, 1); }
	| K_SEQINC '(' number delimiter number delimiter number ')'
		{ set_sequential_inc($3, $5, $7); }
	;

seqdec
	: K_SEQDEC '(' number delimiter number ')'
		{ set_sequential_dec($3, $5, 1); }
	| K_SEQDEC '(' number delimiter number delimiter number ')'
		{ set_sequential_dec($3, $5, $7); }
	;

drnd
	: K_DRND '(' ')'
		{ set_dynamic_rnd(); }
	| K_DRND '(' number ')'
		{
			int i, max = $3;
			for (i = 0; i < max; ++i)
				set_dynamic_rnd();
		}
	;

dinc
	: K_DINC '(' number delimiter number ')'
		{ set_dynamic_incdec($3, $5, 1, TYPE_INC); }
	| K_DINC '(' number delimiter number delimiter number ')'
		{ set_dynamic_incdec($3, $5, $7, TYPE_INC); }
	;

ddec
	: K_DDEC '(' number delimiter number ')'
		{ set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
	| K_DDEC '(' number delimiter number delimiter number ')'
		{ set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
	;

proto
	: eth_proto { }
	| pause_proto { }
	| pfc_proto { }
	| vlan_proto { }
	| mpls_proto { }
	| arp_proto { }
	| ip4_proto { }
	| ip6_proto { }
	| icmp4_proto { }
	| icmpv6_proto { }
	| udp_proto { }
	| tcp_proto { }
	;

field_expr
	: '[' skip_white number skip_white ']'
		{ field_index_validate(field_expr.field, $3, 1);
		  field_expr.type |= FIELD_EXPR_OFFSET;
		  field_expr.val.func.offset = $3;
		  field_expr.val.func.len = 1; }
	| '[' skip_white number skip_white ':' skip_white number skip_white ']'
		{ field_index_validate(field_expr.field, $3, $7);
		  field_expr.type |= FIELD_EXPR_OFFSET;
		  field_expr.val.func.offset = $3;
		  field_expr.val.func.len = $7; }
	;

field_value_expr
	: number { field_expr.type |= FIELD_EXPR_NUMB;
		   field_expr.val.number = $1; }
	| mac { field_expr.type |= FIELD_EXPR_MAC;
		memcpy(field_expr.val.bytes, $1, sizeof(field_expr.val.bytes)); }
	| ip4_addr { field_expr.type |= FIELD_EXPR_IP4_ADDR;
		     field_expr.val.ip4_addr = $1; }
	| ip6_addr { field_expr.type |= FIELD_EXPR_IP6_ADDR;
		     field_expr.val.ip6_addr = $1; }
	| K_DINC '(' ')' { field_expr.type |= FIELD_EXPR_INC;
			   field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
			   field_expr.val.func.inc = 1; }
	| K_DINC '(' number ')'
			{ field_expr.type |= FIELD_EXPR_INC;
			  field_expr.val.func.type = PROTO_FIELD_FUNC_INC;
			  field_expr.val.func.inc = $3; }
	| K_DINC '(' number delimiter number ')'
			{ field_expr.type |= FIELD_EXPR_INC;
			  field_expr.val.func.type  = PROTO_FIELD_FUNC_INC;
			  field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
			  field_expr.val.func.min = $3;
			  field_expr.val.func.max = $5;
			  field_expr.val.func.inc = 1; }
	| K_DINC '(' number delimiter number delimiter number ')'
			{ field_expr.type |= FIELD_EXPR_INC;
			  field_expr.val.func.type  = PROTO_FIELD_FUNC_INC;
			  field_expr.val.func.type |= PROTO_FIELD_FUNC_MIN;
			  field_expr.val.func.min = $3;
			  field_expr.val.func.max = $5;
			  field_expr.val.func.inc = $7; }
	| K_DRND '(' ')' { field_expr.type |= FIELD_EXPR_RND;
			  field_expr.val.func.type = PROTO_FIELD_FUNC_RND; }
	| K_DRND '(' number delimiter number ')'
			{ field_expr.type |= FIELD_EXPR_RND;
			  field_expr.val.func.type = PROTO_FIELD_FUNC_RND;
			  field_expr.val.func.min = $3;
			  field_expr.val.func.max = $5; }
	;

eth_proto
	: eth '(' eth_param_list ')' { }
	;

eth
	: K_ETH	{ proto_add(PROTO_ETH); }
	;

eth_param_list
	: { }
	| eth_expr { }
	| eth_expr delimiter eth_param_list { }
	;

eth_type
	: K_ETYPE { }
	| K_TYPE { }
	| K_PROT { }
	;

eth_field
	: K_DADDR { proto_field_set(ETH_DST_ADDR); }
	| K_SADDR { proto_field_set(ETH_SRC_ADDR); }
	| eth_type { proto_field_set(ETH_TYPE); }

eth_expr
	: eth_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| eth_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

pause_proto
	: pause '(' pause_param_list ')' { }
	;

pause
	: K_PAUSE { proto_add(PROTO_PAUSE); }
	;

pause_param_list
	: { }
	| pause_expr { }
	| pause_expr delimiter pause_param_list { }
	;

pause_field
	: K_CODE { proto_field_set(PAUSE_OPCODE); }
	| K_TIME { proto_field_set(PAUSE_TIME); }
	;

pause_expr
	: pause_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| pause_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

pfc_proto
	: pfc '(' pfc_param_list ')' { }
	;

pfc
	: K_PFC { proto_add(PROTO_PFC); }
	;

pfc_param_list
	: { }
	| pfc_expr { }
	| pfc_expr delimiter pfc_param_list { }
	;

pfc_field
	: K_CODE { proto_field_set(PFC_OPCODE); }
	| K_PRIO { proto_field_set(PFC_PRIO); }
	| K_PRIO '(' number ')'
		{ if ($3 > 7) {
		      yyerror("pfc: Invalid prio(index) parameter");
		      panic("pfc: prio(0)..prio(7) is allowed only\n");
		  }
		  proto_field_set(PFC_PRIO_0 + $3); }
	| K_TIME '(' number ')'
		{ if ($3 > 7) {
		      yyerror("pfc: Invalid time(index) parameter");
		      panic("pfc: time(0)..time(7) is allowed only\n");
		  }
		  proto_field_set(PFC_TIME_0 + $3); }
	;

pfc_expr
	: pfc_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| pfc_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

vlan_proto
	: vlan '(' vlan_param_list ')' { }
	;

vlan
	: K_VLAN { proto_add(PROTO_VLAN); }
	;

vlan_param_list
	: { }
	| vlan_expr { }
	| vlan_expr delimiter vlan_param_list { }
	;

vlan_type
	: K_TPID { }
	| K_PROT
	;

vlan_field
	: vlan_type { proto_field_set(VLAN_TPID); }
	| K_TCI { proto_field_set(VLAN_TCI); }
	| K_PCP { proto_field_set(VLAN_PCP); }
	| K_DEI { proto_field_set(VLAN_DEI); }
	| K_ID { proto_field_set(VLAN_VID); }
	;

vlan_expr
	: vlan_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| vlan_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_1Q
		{ proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
	| K_1AD
		{ proto_hdr_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
	;

mpls_proto
	: mpls '(' mpls_param_list ')' { }
	;

mpls
	: K_MPLS { proto_add(PROTO_MPLS); }
	;

mpls_param_list
	: { }
	| mpls_expr { }
	| mpls_expr delimiter mpls_param_list { }
	;

mpls_tc
	: K_TC { }
	| K_EXP { }
	;

mpls_field
	: K_LABEL { proto_field_set(MPLS_LABEL); }
	| mpls_tc { proto_field_set(MPLS_TC); }
	| K_LAST { proto_field_set(MPLS_LAST); }
	| K_TTL { proto_field_set(MPLS_TTL); }
	;

mpls_expr
	: mpls_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| mpls_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

arp_proto
	: arp '(' arp_param_list ')' { }
	;

arp_param_list
	: { }
	| arp_expr { }
	| arp_expr delimiter arp_param_list { }
	;

arp_field
	: K_HTYPE
		{ proto_field_set(ARP_HTYPE); }
	| K_PTYPE
		{ proto_field_set(ARP_PTYPE); }
	| K_SHA
		{ proto_field_set(ARP_SHA); }
	| K_THA
		{ proto_field_set(ARP_THA); }
	| K_SPA
		{ proto_field_set(ARP_SPA); }
	| K_TPA
		{ proto_field_set(ARP_TPA); }
	;

arp_expr
	: arp_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| arp_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_OPER field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_set(ARP_OPER);
		  proto_field_expr_eval(); }
	| K_OPER skip_white '=' skip_white field_value_expr
		{ proto_field_set(ARP_OPER);
		  proto_field_expr_eval(); }
	| K_OPER skip_white '=' skip_white K_REQUEST
		{ proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
	| K_OPER skip_white '=' skip_white K_REPLY
		{ proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
	| K_REQUEST
		{ proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
	| K_REPLY
		{ proto_hdr_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
	;

arp
	: K_ARP	{ proto_add(PROTO_ARP); }
	;

ip4_proto
	: ip4 '(' ip4_param_list ')' { }
	;

ip4_param_list
	: { }
	| ip4_expr { }
	| ip4_expr delimiter ip4_param_list { }
	;

ip4_field
	: K_VER { proto_field_set(IP4_VER); }
	| K_IHL { proto_field_set(IP4_IHL); }
	| K_DADDR { proto_field_set(IP4_DADDR); }
	| K_SADDR { proto_field_set(IP4_SADDR); }
	| K_PROT { proto_field_set(IP4_PROTO); }
	| K_TTL { proto_field_set(IP4_TTL); }
	| K_DSCP { proto_field_set(IP4_DSCP); }
	| K_ECN { proto_field_set(IP4_ECN); }
	| K_TOS { proto_field_set(IP4_TOS); }
	| K_LEN { proto_field_set(IP4_LEN); }
	| K_ID { proto_field_set(IP4_ID); }
	| K_FLAGS { proto_field_set(IP4_FLAGS); }
	| K_FRAG { proto_field_set(IP4_FRAG_OFFS); }
	| K_CSUM { proto_field_set(IP4_CSUM); }
	;

ip4_expr
	: ip4_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| ip4_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_DF  { proto_hdr_field_set_be16(hdr, IP4_DF, 1); }
	| K_MF  { proto_hdr_field_set_be16(hdr, IP4_MF, 1); }
	;

ip4
	: K_IP4	{ proto_add(PROTO_IP4); }
	;

ip6_proto
	: ip6 '(' ip6_param_list ')' { }
	;

ip6_param_list
	: { }
	| ip6_expr { }
	| ip6_expr delimiter ip6_param_list { }
	;

ip6_hop_limit
	: K_HOP_LIMIT { }
	| K_TTL { }
	;

ip6_field
	: K_VER { proto_field_set(IP6_VER); }
	| K_TC { proto_field_set(IP6_CLASS); }
	| K_FLOW { proto_field_set(IP6_FLOW_LBL); }
	| K_LEN { proto_field_set(IP6_LEN); }
	| K_NEXT_HDR { proto_field_set(IP6_NEXT_HDR); }
	| ip6_hop_limit { proto_field_set(IP6_HOP_LIMIT); }
	| K_SADDR { proto_field_set(IP6_SADDR); }
	| K_DADDR { proto_field_set(IP6_DADDR) ; }
	;

ip6_expr
	: ip6_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| ip6_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

ip6
	: K_IP6	{ proto_add(PROTO_IP6); }
	;

icmp4_proto
	: icmp4 '(' icmp4_param_list ')' { }
	;

icmp4_param_list
	: { }
	| icmp4_expr { }
	| icmp4_expr delimiter icmp4_param_list { }
	;

icmp4_field
	: K_TYPE { proto_field_set(ICMPV4_TYPE); }
	| K_CODE { proto_field_set(ICMPV4_CODE); }
	| K_ID { proto_field_set(ICMPV4_ID); }
	| K_SEQ { proto_field_set(ICMPV4_SEQ); }
	| K_MTU { proto_field_set(ICMPV4_MTU); }
	| K_ADDR { proto_field_set(ICMPV4_REDIR_ADDR); }
	;

icmp4_expr
	: icmp4_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| icmp4_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_ECHO_REQUEST
		{ proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHO);
		  proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
	| K_ECHO_REPLY
		{ proto_hdr_field_set_u8(hdr, ICMPV4_TYPE, ICMP_ECHOREPLY);
		  proto_hdr_field_set_u8(hdr, ICMPV4_CODE, 0); }
	;

icmp4
	: K_ICMP4	{ proto_add(PROTO_ICMP4); }
	;

icmpv6_proto
	: icmp6 '(' icmp6_param_list ')' { }
	;

icmp6_param_list
	: { }
	| icmp6_expr { }
	| icmp6_expr delimiter icmp6_param_list { }
	;

icmp6_field
	: K_CODE { proto_field_set(ICMPV6_CODE); }
	| K_CSUM { proto_field_set(ICMPV6_CSUM); }
	;

icmp6_expr
	: icmp6_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| icmp6_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_TYPE field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_set(ICMPV6_TYPE);
		  proto_field_expr_eval(); }
	| K_TYPE skip_white '=' skip_white field_value_expr
		{ proto_field_set(ICMPV6_TYPE);
		  proto_field_expr_eval(); }
	| K_TYPE skip_white '=' K_ECHO_REQUEST
		{ proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
	| K_ECHO_REQUEST
		{ proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REQUEST); }
	| K_TYPE skip_white '=' K_ECHO_REPLY
		{ proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
	| K_ECHO_REPLY
		{ proto_hdr_field_set_u8(hdr, ICMPV6_TYPE, ICMPV6_ECHO_REPLY); }
	;
icmp6
	: K_ICMP6 { proto_add(PROTO_ICMP6); }
	;

udp_proto
	: udp '(' udp_param_list ')' { }
	;

udp_param_list
	: { }
	| udp_expr { }
	| udp_expr delimiter udp_param_list { }
	;

udp_field
	: K_SPORT { proto_field_set(UDP_SPORT); }
	| K_DPORT { proto_field_set(UDP_DPORT); }
	| K_LEN { proto_field_set(UDP_LEN); }
	| K_CSUM { proto_field_set(UDP_CSUM); }
	;

udp_expr
	: udp_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| udp_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	;

udp
	: K_UDP	{ proto_add(PROTO_UDP); }
	;

tcp_proto
	: tcp '(' tcp_param_list ')' { }
	;

tcp_param_list
	: { }
	| tcp_expr { }
	| tcp_expr delimiter tcp_param_list { }
	;

tcp_field
	: K_SPORT { proto_field_set(TCP_SPORT); }
	| K_DPORT { proto_field_set(TCP_DPORT); }
	| K_SEQ { proto_field_set(TCP_SEQ); }
	| K_ACK_SEQ { proto_field_set(TCP_ACK_SEQ); }
	| K_DOFF { proto_field_set(TCP_DOFF); }
	| K_WINDOW { proto_field_set(TCP_WINDOW); }
	| K_CSUM { proto_field_set(TCP_CSUM); }
	| K_URG_PTR { proto_field_set(TCP_URG_PTR); }
	;

tcp_expr
	: tcp_field field_expr skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| tcp_field skip_white '=' skip_white field_value_expr
		{ proto_field_expr_eval(); }
	| K_CWR { proto_hdr_field_set_be16(hdr, TCP_CWR, 1); }
	| K_ECE { proto_hdr_field_set_be16(hdr, TCP_ECE, 1); }
	| K_URG { proto_hdr_field_set_be16(hdr, TCP_URG, 1); }
	| K_ACK { proto_hdr_field_set_be16(hdr, TCP_ACK, 1); }
	| K_PSH { proto_hdr_field_set_be16(hdr, TCP_PSH, 1); }
	| K_RST { proto_hdr_field_set_be16(hdr, TCP_RST, 1); }
	| K_SYN { proto_hdr_field_set_be16(hdr, TCP_SYN, 1); }
	| K_FIN { proto_hdr_field_set_be16(hdr, TCP_FIN, 1); }
	;

tcp
	: K_TCP	{ proto_add(PROTO_TCP); }
	;

%%

static void finalize_packet(void)
{
	/* XXX hack ... we allocated one packet pointer too much */
	plen--;
	dlen--;
}

static void dump_conf(void)
{
	size_t i, j;

	for (i = 0; i < plen; ++i) {
		printf("[%zu] pkt\n", i);
		printf(" len %zu cnts %zu rnds %zu\n",
		       packets[i].len,
		       packet_dyn[i].clen,
		       packet_dyn[i].rlen);

		printf(" payload ");
		for (j = 0; j < packets[i].len; ++j)
			printf("%02x ", packets[i].payload[j]);
		printf("\n");

		for (j = 0; j < packet_dyn[i].clen; ++j)
			printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
			       packet_dyn[i].cnt[j].min,
			       packet_dyn[i].cnt[j].max,
			       packet_dyn[i].cnt[j].inc,
			       (intmax_t)packet_dyn[i].cnt[j].off,
			       packet_dyn[i].cnt[j].type == TYPE_INC ?
			       "inc" : "dec");

		for (j = 0; j < packet_dyn[i].rlen; ++j)
			printf(" rnd%zu off %jd\n", j,
			       (intmax_t)packet_dyn[i].rnd[j].off);
	}
}

void cleanup_packets(void)
{
	size_t i, j;

	for (i = 0; i < plen; ++i) {
		struct packet *pkt = &packets[i];

		if (pkt->len > 0)
			xfree(pkt->payload);

		for (j = 0; j < pkt->headers_count; j++) {
			struct proto_hdr *hdr = pkt->headers[j];

			if (hdr->fields)
				xfree(hdr->fields);
			xfree(hdr);
		}
	}

	free(packets);

	for (i = 0; i < dlen; ++i) {
		free(packet_dyn[i].cnt);
		free(packet_dyn[i].rnd);

		for (j = 0; j < packet_dyn[j].flen; j++)
			xfree(packet_dyn[i].fields[j]);

		free(packet_dyn[i].fields);
	}

	free(packet_dyn);
}

void compile_packets(char *file, bool verbose, unsigned int cpu,
		     bool invoke_cpp, char *const cpp_argv[])
{
	char tmp_file[128];
	int ret = -1;

	if (access(file, R_OK)) {
		fprintf(stderr, "Cannot access %s: %s!\n", file, strerror(errno));
		die();
	}

	memset(tmp_file, 0, sizeof(tmp_file));
	our_cpu = cpu;

	if (invoke_cpp) {
		if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
			fprintf(stderr, "Failed to invoke C preprocessor!\n");
			goto err;
		}
		file = tmp_file;
	}

	if (!strncmp("-", file, strlen("-")))
		yyin = stdin;
	else
		yyin = fopen(file, "r");
	if (!yyin) {
		fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
		goto err;
	}

	realloc_packet();
	if (yyparse() != 0)
		goto err;
	finalize_packet();

	if (our_cpu == 0 && verbose)
		dump_conf();

	ret = 0;
err:
	if (yyin && yyin != stdin)
		fclose(yyin);

	if (invoke_cpp)
		unlink(tmp_file);
	if (ret)
		die();
}

void compile_packets_str(char *str, bool verbose, unsigned int cpu)
{
	int ret = 1;

	our_cpu = cpu;
	realloc_packet();

	yy_scan_string(str);
	if (yyparse() != 0)
		goto err;

	finalize_packet();
	if (our_cpu == 0 && verbose)
		dump_conf();

	ret = 0;
err:
	yylex_destroy();

	if (ret)
		die();
}

void yyerror(const char *err)
{
	fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);
}