screen: Add helpers to easy use color by name
[netsniff-ng-new.git] / trafgen_parser.y
blob0b1c0fbc294c97f6b49b5201bc733191bb5fbb90
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>
18 #include <stdbool.h>
19 #include <libgen.h>
20 #include <net/if_arp.h>
21 #include <netinet/in.h>
22 #include <linux/if_ether.h>
24 #include "xmalloc.h"
25 #include "trafgen_parser.tab.h"
26 #include "trafgen_conf.h"
27 #include "trafgen_proto.h"
28 #include "trafgen_l2.h"
29 #include "trafgen_l3.h"
30 #include "trafgen_l4.h"
31 #include "built_in.h"
32 #include "die.h"
33 #include "str.h"
34 #include "csum.h"
35 #include "cpp.h"
37 #define YYERROR_VERBOSE 0
38 #define YYDEBUG 0
39 #define YYENABLE_NLS 1
40 #define YYLTYPE_IS_TRIVIAL 1
41 #define ENABLE_NLS 1
43 extern FILE *yyin;
44 extern int yylex(void);
45 extern void yy_scan_string(char *);
46 extern void yylex_destroy();
47 extern void yyerror(const char *);
48 extern int yylineno;
49 extern char *yytext;
51 extern struct packet *packets;
52 extern size_t plen;
54 #define packet_last (plen - 1)
56 #define payload_last (packets[packet_last].len - 1)
58 extern struct packet_dyn *packet_dyn;
59 extern size_t dlen;
61 #define packetd_last (dlen - 1)
63 #define packetdc_last (packet_dyn[packetd_last].clen - 1)
64 #define packetdr_last (packet_dyn[packetd_last].rlen - 1)
65 #define packetds_last (packet_dyn[packetd_last].slen - 1)
67 static int our_cpu, min_cpu = -1, max_cpu = -1;
69 static struct proto_hdr *hdr;
71 static inline int test_ignore(void)
73 if (min_cpu < 0 && max_cpu < 0)
74 return 0;
75 else if (max_cpu >= our_cpu && min_cpu <= our_cpu)
76 return 0;
77 else
78 return 1;
81 static inline void __init_new_packet_slot(struct packet *slot)
83 slot->payload = NULL;
84 slot->len = 0;
87 static inline void __init_new_counter_slot(struct packet_dyn *slot)
89 slot->cnt = NULL;
90 slot->clen = 0;
93 static inline void __init_new_randomizer_slot(struct packet_dyn *slot)
95 slot->rnd = NULL;
96 slot->rlen = 0;
99 static inline void __init_new_csum_slot(struct packet_dyn *slot)
101 slot->csum = NULL;
102 slot->slen = 0;
105 static inline void __setup_new_counter(struct counter *c, uint8_t start,
106 uint8_t stop, uint8_t stepping,
107 int type)
109 c->min = start;
110 c->max = stop;
111 c->inc = stepping;
112 c->val = (type == TYPE_INC) ? start : stop;
113 c->off = payload_last;
114 c->type = type;
117 static inline void __setup_new_randomizer(struct randomizer *r)
119 r->off = payload_last;
122 static inline void __setup_new_csum16(struct csum16 *s, off_t from, off_t to,
123 enum csum which)
125 s->off = payload_last - 1;
126 s->from = from;
127 s->to = to;
128 s->which = which;
131 static void realloc_packet(void)
133 if (test_ignore())
134 return;
136 plen++;
137 packets = xrealloc(packets, plen * sizeof(*packets));
139 __init_new_packet_slot(&packets[packet_last]);
141 dlen++;
142 packet_dyn = xrealloc(packet_dyn, dlen * sizeof(*packet_dyn));
144 __init_new_counter_slot(&packet_dyn[packetd_last]);
145 __init_new_randomizer_slot(&packet_dyn[packetd_last]);
146 __init_new_csum_slot(&packet_dyn[packetd_last]);
149 struct packet *current_packet(void)
151 return &packets[packet_last];
154 static void set_byte(uint8_t val)
156 struct packet *pkt = &packets[packet_last];
158 if (test_ignore())
159 return;
161 pkt->len++;
162 pkt->payload = xrealloc(pkt->payload, pkt->len);
163 pkt->payload[payload_last] = val;
166 static void set_multi_byte(uint8_t *s, size_t len)
168 size_t i;
170 for (i = 0; i < len; ++i)
171 set_byte(s[i]);
174 void set_fill(uint8_t val, size_t len)
176 size_t i;
177 struct packet *pkt = &packets[packet_last];
179 if (test_ignore())
180 return;
182 pkt->len += len;
183 pkt->payload = xrealloc(pkt->payload, pkt->len);
184 for (i = 0; i < len; ++i)
185 pkt->payload[payload_last - i] = val;
188 static void __set_csum16_dynamic(size_t from, size_t to, enum csum which)
190 struct packet *pkt = &packets[packet_last];
191 struct packet_dyn *pktd = &packet_dyn[packetd_last];
193 pkt->len += 2;
194 pkt->payload = xrealloc(pkt->payload, pkt->len);
196 pktd->slen++;
197 pktd->csum = xrealloc(pktd->csum, pktd->slen * sizeof(struct csum16));
199 __setup_new_csum16(&pktd->csum[packetds_last], from, to, which);
202 static void __set_csum16_static(size_t from, size_t to, enum csum which __maybe_unused)
204 struct packet *pkt = &packets[packet_last];
205 uint16_t sum;
206 uint8_t *psum;
208 sum = htons(calc_csum(pkt->payload + from, to - from));
209 psum = (uint8_t *) &sum;
211 set_byte(psum[0]);
212 set_byte(psum[1]);
215 static inline bool is_dynamic_csum(enum csum which)
217 switch (which) {
218 case CSUM_UDP:
219 case CSUM_TCP:
220 case CSUM_UDP6:
221 case CSUM_TCP6:
222 return true;
223 default:
224 return false;
228 static void set_csum16(size_t from, size_t to, enum csum which)
230 struct packet *pkt = &packets[packet_last];
231 struct packet_dyn *pktd = &packet_dyn[packetd_last];
233 if (test_ignore())
234 return;
236 if (to < from) {
237 size_t tmp = to;
239 to = from;
240 from = tmp;
243 bug_on(!(from < to));
245 if (packet_dyn_has_elems(pktd) || to >= pkt->len || is_dynamic_csum(which))
246 __set_csum16_dynamic(from, to, which);
247 else
248 __set_csum16_static(from, to, which);
251 static void set_rnd(size_t len)
253 size_t i;
254 struct packet *pkt = &packets[packet_last];
256 if (test_ignore())
257 return;
259 pkt->len += len;
260 pkt->payload = xrealloc(pkt->payload, pkt->len);
261 for (i = 0; i < len; ++i)
262 pkt->payload[payload_last - i] = (uint8_t) rand();
265 static void set_sequential_inc(uint8_t start, size_t len, uint8_t stepping)
267 size_t i;
268 struct packet *pkt = &packets[packet_last];
270 if (test_ignore())
271 return;
273 pkt->len += len;
274 pkt->payload = xrealloc(pkt->payload, pkt->len);
275 for (i = 0; i < len; ++i) {
276 off_t off = len - 1 - i;
278 pkt->payload[payload_last - off] = start;
279 start += stepping;
283 static void set_sequential_dec(uint8_t start, size_t len, uint8_t stepping)
285 size_t i;
286 struct packet *pkt = &packets[packet_last];
288 if (test_ignore())
289 return;
291 pkt->len += len;
292 pkt->payload = xrealloc(pkt->payload, pkt->len);
293 for (i = 0; i < len; ++i) {
294 int off = len - 1 - i;
296 pkt->payload[payload_last - off] = start;
297 start -= stepping;
301 static void set_dynamic_rnd(void)
303 struct packet *pkt = &packets[packet_last];
304 struct packet_dyn *pktd = &packet_dyn[packetd_last];
306 if (test_ignore())
307 return;
309 pkt->len++;
310 pkt->payload = xrealloc(pkt->payload, pkt->len);
312 pktd->rlen++;
313 pktd->rnd = xrealloc(pktd->rnd, pktd->rlen * sizeof(struct randomizer));
315 __setup_new_randomizer(&pktd->rnd[packetdr_last]);
318 static void set_dynamic_incdec(uint8_t start, uint8_t stop, uint8_t stepping,
319 int type)
321 struct packet *pkt = &packets[packet_last];
322 struct packet_dyn *pktd = &packet_dyn[packetd_last];
324 if (test_ignore())
325 return;
327 pkt->len++;
328 pkt->payload = xrealloc(pkt->payload, pkt->len);
330 pktd->clen++;
331 pktd->cnt = xrealloc(pktd->cnt, pktd->clen * sizeof(struct counter));
333 __setup_new_counter(&pktd->cnt[packetdc_last], start, stop, stepping, type);
336 static void proto_add(enum proto_id pid)
338 hdr = proto_header_init(pid);
343 %union {
344 struct in_addr ip4_addr;
345 long long int number;
346 uint8_t bytes[256];
347 char *str;
350 %token K_COMMENT K_FILL K_RND K_SEQINC K_SEQDEC K_DRND K_DINC K_DDEC K_WHITE
351 %token K_CPU K_CSUMIP K_CSUMUDP K_CSUMTCP K_CSUMUDP6 K_CSUMTCP6 K_CONST8 K_CONST16 K_CONST32 K_CONST64
353 %token K_DADDR K_SADDR K_ETYPE
354 %token K_OPER K_SHA K_SPA K_THA K_TPA K_REQUEST K_REPLY K_PTYPE K_HTYPE
355 %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
356 %token K_SPORT K_DPORT
357 %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
358 %token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
359 %token K_LABEL K_TC K_LAST K_EXP
361 %token K_ETH
362 %token K_VLAN K_MPLS
363 %token K_ARP
364 %token K_IP4
365 %token K_UDP K_TCP
367 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
369 %token number string mac ip4_addr
371 %type <number> number expression
372 %type <str> string
373 %type <bytes> mac
374 %type <ip4_addr> ip4_addr
376 %left '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
380 packets
381 : { }
382 | packets packet { }
383 | packets inline_comment { }
384 | packets K_WHITE { }
387 inline_comment
388 : K_COMMENT { }
391 cpu_delim
392 : ':' { }
393 | '-' { }
396 delimiter_nowhite
397 : ',' { }
398 | ',' K_WHITE { }
401 noenforce_white
402 : { }
403 | K_WHITE { }
404 | delimiter_nowhite { }
407 skip_white
408 : { }
409 | K_WHITE { }
411 packet
412 : '{' noenforce_white payload noenforce_white '}' {
413 min_cpu = max_cpu = -1;
415 proto_packet_finish();
417 realloc_packet();
419 | K_CPU '(' number cpu_delim number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
420 min_cpu = $3;
421 max_cpu = $5;
423 if (min_cpu > max_cpu) {
424 int tmp = min_cpu;
426 min_cpu = max_cpu;
427 max_cpu = tmp;
430 proto_packet_finish();
432 realloc_packet();
434 | K_CPU '(' number ')' ':' noenforce_white '{' noenforce_white payload noenforce_white '}' {
435 min_cpu = max_cpu = $3;
437 proto_packet_finish();
439 realloc_packet();
443 payload
444 : elem { }
445 | payload elem_delimiter { }
448 delimiter
449 : delimiter_nowhite { }
450 | K_WHITE { }
453 elem_delimiter
454 : delimiter elem { }
457 elem
458 : number { set_byte((uint8_t) $1); }
459 | string { set_multi_byte((uint8_t *) $1 + 1, strlen($1) - 2); }
460 | fill { }
461 | rnd { }
462 | drnd { }
463 | seqinc { }
464 | seqdec { }
465 | dinc { }
466 | ddec { }
467 | csum { }
468 | const { }
469 | proto { proto_header_finish(hdr); }
470 | inline_comment { }
473 expression
474 : number
475 { $$ = $1; }
476 | expression '+' expression
477 { $$ = $1 + $3; }
478 | expression '-' expression
479 { $$ = $1 - $3; }
480 | expression '*' expression
481 { $$ = $1 * $3; }
482 | expression '/' expression
483 { $$ = $1 / $3; }
484 | expression '%' expression
485 { $$ = $1 % $3; }
486 | expression '&' expression
487 { $$ = $1 & $3; }
488 | expression '|' expression
489 { $$ = $1 | $3; }
490 | expression '^' expression
491 { $$ = $1 ^ $3; }
492 | expression '<' '<' expression
493 { $$ = $1 << $4; }
494 | expression '>' '>' expression
495 { $$ = $1 >> $4; }
496 | '-' expression
497 { $$ = -1 * $2; }
498 | '(' expression ')'
499 { $$ = $2;}
502 fill
503 : K_FILL '(' number delimiter number ')'
504 { set_fill($3, $5); }
507 const
508 : K_CONST8 '(' expression ')'
509 { set_byte((uint8_t) $3); }
510 | K_CONST16 '(' expression ')' {
511 uint16_t __c = cpu_to_be16((uint16_t) $3);
513 set_multi_byte((uint8_t *) &__c, sizeof(__c));
515 | K_CONST32 '(' expression ')' {
516 uint32_t __c = cpu_to_be32((uint32_t) $3);
518 set_multi_byte((uint8_t *) &__c, sizeof(__c));
520 | K_CONST64 '(' expression ')' {
521 uint64_t __c = cpu_to_be64((uint64_t) $3);
523 set_multi_byte((uint8_t *) &__c, sizeof(__c));
528 : K_RND '(' number ')'
529 { set_rnd($3); }
532 csum
533 : K_CSUMIP '(' number delimiter number ')'
534 { set_csum16($3, $5, CSUM_IP); }
535 | K_CSUMTCP '(' number delimiter number ')'
536 { set_csum16($3, $5, CSUM_TCP); }
537 | K_CSUMUDP '(' number delimiter number ')'
538 { set_csum16($3, $5, CSUM_UDP); }
539 | K_CSUMTCP6 '(' number delimiter number ')'
540 { set_csum16($3, $5, CSUM_TCP6); }
541 | K_CSUMUDP6 '(' number delimiter number ')'
542 { set_csum16($3, $5, CSUM_UDP6); }
545 seqinc
546 : K_SEQINC '(' number delimiter number ')'
547 { set_sequential_inc($3, $5, 1); }
548 | K_SEQINC '(' number delimiter number delimiter number ')'
549 { set_sequential_inc($3, $5, $7); }
552 seqdec
553 : K_SEQDEC '(' number delimiter number ')'
554 { set_sequential_dec($3, $5, 1); }
555 | K_SEQDEC '(' number delimiter number delimiter number ')'
556 { set_sequential_dec($3, $5, $7); }
559 drnd
560 : K_DRND '(' ')'
561 { set_dynamic_rnd(); }
562 | K_DRND '(' number ')'
564 int i, max = $3;
565 for (i = 0; i < max; ++i)
566 set_dynamic_rnd();
570 dinc
571 : K_DINC '(' number delimiter number ')'
572 { set_dynamic_incdec($3, $5, 1, TYPE_INC); }
573 | K_DINC '(' number delimiter number delimiter number ')'
574 { set_dynamic_incdec($3, $5, $7, TYPE_INC); }
577 ddec
578 : K_DDEC '(' number delimiter number ')'
579 { set_dynamic_incdec($3, $5, 1, TYPE_DEC); }
580 | K_DDEC '(' number delimiter number delimiter number ')'
581 { set_dynamic_incdec($3, $5, $7, TYPE_DEC); }
584 proto
585 : eth_proto { }
586 | vlan_proto { }
587 | mpls_proto { }
588 | arp_proto { }
589 | ip4_proto { }
590 | udp_proto { }
591 | tcp_proto { }
594 eth_proto
595 : eth '(' eth_param_list ')' { }
599 : K_ETH { proto_add(PROTO_ETH); }
602 eth_param_list
603 : { }
604 | eth_field { }
605 | eth_field delimiter eth_param_list { }
608 eth_type
609 : K_ETYPE { }
610 | K_PROT { }
613 eth_field
614 : K_DADDR skip_white '=' skip_white mac
615 { proto_field_set_bytes(hdr, ETH_DST_ADDR, $5); }
616 | K_SADDR skip_white '=' skip_white mac
617 { proto_field_set_bytes(hdr, ETH_SRC_ADDR, $5); }
618 | eth_type skip_white '=' skip_white number
619 { proto_field_set_be16(hdr, ETH_TYPE, $5); }
622 vlan_proto
623 : vlan '(' vlan_param_list ')' { }
626 vlan
627 : K_VLAN { proto_add(PROTO_VLAN); }
630 vlan_param_list
631 : { }
632 | vlan_field { }
633 | vlan_field delimiter vlan_param_list { }
636 vlan_type
637 : K_TPID { }
638 | K_PROT
641 vlan_field
642 : vlan_type skip_white '=' skip_white number
643 { proto_field_set_be16(hdr, VLAN_TPID, $5); }
644 | K_1Q
645 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
646 | K_1AD
647 { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
648 | K_TCI skip_white '=' skip_white number
649 { proto_field_set_be16(hdr, VLAN_TCI, $5); }
650 | K_PCP skip_white '=' skip_white number
651 { proto_field_set_be16(hdr, VLAN_PCP, $5); }
652 | K_DEI skip_white '=' skip_white number
653 { proto_field_set_be16(hdr, VLAN_DEI, $5); }
654 | K_ID skip_white '=' skip_white number
655 { proto_field_set_be16(hdr, VLAN_VID, $5); }
658 mpls_proto
659 : mpls '(' mpls_param_list ')' { }
662 mpls
663 : K_MPLS { proto_add(PROTO_MPLS); }
666 mpls_param_list
667 : { }
668 | mpls_field { }
669 | mpls_field delimiter mpls_param_list { }
672 mpls_tc
673 : K_TC { }
674 | K_EXP { }
677 mpls_field
678 : K_LABEL skip_white '=' skip_white number
679 { proto_field_set_be32(hdr, MPLS_LABEL, $5); }
680 | mpls_tc skip_white '=' skip_white number
681 { proto_field_set_be32(hdr, MPLS_TC, $5); }
682 | K_LAST skip_white '=' skip_white number
683 { proto_field_set_be32(hdr, MPLS_LAST, $5); }
684 | K_TTL skip_white '=' skip_white number
685 { proto_field_set_be32(hdr, MPLS_TTL, $5); }
688 arp_proto
689 : arp '(' arp_param_list ')' { }
692 arp_param_list
693 : { }
694 | arp_field { }
695 | arp_field delimiter arp_param_list { }
698 arp_field
699 : K_OPER skip_white '=' skip_white K_REQUEST
700 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
701 | K_OPER skip_white '=' skip_white K_REPLY
702 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
703 | K_OPER skip_white '=' skip_white number
704 { proto_field_set_be16(hdr, ARP_OPER, $5); }
705 | K_REQUEST
706 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REQUEST); }
707 | K_REPLY
708 { proto_field_set_be16(hdr, ARP_OPER, ARPOP_REPLY); }
709 | K_HTYPE skip_white '=' skip_white number
710 { proto_field_set_be16(hdr, ARP_HTYPE, $5); }
711 | K_PTYPE skip_white '=' skip_white number
712 { proto_field_set_be16(hdr, ARP_PTYPE, $5); }
713 | K_SHA skip_white '=' skip_white mac
714 { proto_field_set_bytes(hdr, ARP_SHA, $5); }
715 | K_THA skip_white '=' skip_white mac
716 { proto_field_set_bytes(hdr, ARP_THA, $5); }
717 | K_SPA skip_white '=' skip_white ip4_addr
718 { proto_field_set_u32(hdr, ARP_SPA, $5.s_addr); }
719 | K_TPA skip_white '=' skip_white ip4_addr
720 { proto_field_set_u32(hdr, ARP_TPA, $5.s_addr); }
723 : K_ARP { proto_add(PROTO_ARP); }
726 ip4_proto
727 : ip4 '(' ip4_param_list ')' { }
730 ip4_param_list
731 : { }
732 | ip4_field { }
733 | ip4_field delimiter ip4_param_list { }
736 ip4_field
737 : K_VER skip_white '=' skip_white number
738 { proto_field_set_u8(hdr, IP4_VER, $5); }
739 | K_IHL skip_white '=' skip_white number
740 { proto_field_set_u8(hdr, IP4_IHL, $5); }
741 | K_DADDR skip_white '=' skip_white ip4_addr
742 { proto_field_set_u32(hdr, IP4_DADDR, $5.s_addr); }
743 | K_SADDR skip_white '=' skip_white ip4_addr
744 { proto_field_set_u32(hdr, IP4_SADDR, $5.s_addr); }
745 | K_PROT skip_white '=' skip_white number
746 { proto_field_set_u8(hdr, IP4_PROTO, $5); }
747 | K_TTL skip_white '=' skip_white number
748 { proto_field_set_u8(hdr, IP4_TTL, $5); }
749 | K_DSCP skip_white '=' skip_white number
750 { proto_field_set_u8(hdr, IP4_DSCP, $5); }
751 | K_ECN skip_white '=' skip_white number
752 { proto_field_set_u8(hdr, IP4_ECN, $5); }
753 | K_TOS skip_white '=' skip_white number
754 { proto_field_set_u8(hdr, IP4_TOS, $5); }
755 | K_LEN skip_white '=' skip_white number
756 { proto_field_set_be16(hdr, IP4_LEN, $5); }
757 | K_ID skip_white '=' skip_white number
758 { proto_field_set_be16(hdr, IP4_ID, $5); }
759 | K_FLAGS skip_white '=' skip_white number
760 { proto_field_set_be16(hdr, IP4_FLAGS, $5); }
761 | K_DF { proto_field_set_be16(hdr, IP4_DF, 1); }
762 | K_MF { proto_field_set_be16(hdr, IP4_MF, 1); }
763 | K_FRAG skip_white '=' skip_white number
764 { proto_field_set_be16(hdr, IP4_FRAG_OFFS, $5); }
765 | K_CSUM skip_white '=' skip_white number
766 { proto_field_set_be16(hdr, IP4_CSUM, $5); }
770 : K_IP4 { proto_add(PROTO_IP4); }
773 udp_proto
774 : udp '(' udp_param_list ')' { }
777 udp_param_list
778 : { }
779 | udp_field { }
780 | udp_field delimiter udp_param_list { }
783 udp_field
784 : K_SPORT skip_white '=' skip_white number
785 { proto_field_set_be16(hdr, UDP_SPORT, $5); }
786 | K_DPORT skip_white '=' skip_white number
787 { proto_field_set_be16(hdr, UDP_DPORT, $5); }
788 | K_LEN skip_white '=' skip_white number
789 { proto_field_set_be16(hdr, UDP_LEN, $5); }
790 | K_CSUM skip_white '=' skip_white number
791 { proto_field_set_be16(hdr, UDP_CSUM, $5); }
795 : K_UDP { proto_add(PROTO_UDP); }
798 tcp_proto
799 : tcp '(' tcp_param_list ')' { }
802 tcp_param_list
803 : { }
804 | tcp_field { }
805 | tcp_field delimiter tcp_param_list { }
808 tcp_field
809 : K_SPORT skip_white '=' skip_white number
810 { proto_field_set_be16(hdr, TCP_SPORT, $5); }
811 | K_DPORT skip_white '=' skip_white number
812 { proto_field_set_be16(hdr, TCP_DPORT, $5); }
813 | K_SEQ skip_white '=' skip_white number
814 { proto_field_set_be32(hdr, TCP_SEQ, $5); }
815 | K_ACK_SEQ skip_white '=' skip_white number
816 { proto_field_set_be32(hdr, TCP_ACK_SEQ, $5); }
817 | K_DOFF skip_white '=' skip_white number
818 { proto_field_set_be16(hdr, TCP_DOFF, $5); }
819 | K_CWR { proto_field_set_be16(hdr, TCP_CWR, 1); }
820 | K_ECE { proto_field_set_be16(hdr, TCP_ECE, 1); }
821 | K_URG { proto_field_set_be16(hdr, TCP_URG, 1); }
822 | K_ACK { proto_field_set_be16(hdr, TCP_ACK, 1); }
823 | K_PSH { proto_field_set_be16(hdr, TCP_PSH, 1); }
824 | K_RST { proto_field_set_be16(hdr, TCP_RST, 1); }
825 | K_SYN { proto_field_set_be16(hdr, TCP_SYN, 1); }
826 | K_FIN { proto_field_set_be16(hdr, TCP_FIN, 1); }
827 | K_WINDOW skip_white '=' skip_white number
828 { proto_field_set_be16(hdr, TCP_WINDOW, $5); }
829 | K_CSUM skip_white '=' skip_white number
830 { proto_field_set_be16(hdr, TCP_CSUM, $5); }
831 | K_URG_PTR skip_white '=' skip_white number
832 { proto_field_set_be16(hdr, TCP_URG_PTR, $5); }
836 : K_TCP { proto_add(PROTO_TCP); }
841 static void finalize_packet(void)
843 /* XXX hack ... we allocated one packet pointer too much */
844 plen--;
845 dlen--;
848 static void dump_conf(void)
850 size_t i, j;
852 for (i = 0; i < plen; ++i) {
853 printf("[%zu] pkt\n", i);
854 printf(" len %zu cnts %zu rnds %zu\n",
855 packets[i].len,
856 packet_dyn[i].clen,
857 packet_dyn[i].rlen);
859 printf(" payload ");
860 for (j = 0; j < packets[i].len; ++j)
861 printf("%02x ", packets[i].payload[j]);
862 printf("\n");
864 for (j = 0; j < packet_dyn[i].clen; ++j)
865 printf(" cnt%zu [%u,%u], inc %u, off %jd type %s\n", j,
866 packet_dyn[i].cnt[j].min,
867 packet_dyn[i].cnt[j].max,
868 packet_dyn[i].cnt[j].inc,
869 (intmax_t)packet_dyn[i].cnt[j].off,
870 packet_dyn[i].cnt[j].type == TYPE_INC ?
871 "inc" : "dec");
873 for (j = 0; j < packet_dyn[i].rlen; ++j)
874 printf(" rnd%zu off %jd\n", j,
875 (intmax_t)packet_dyn[i].rnd[j].off);
879 void cleanup_packets(void)
881 size_t i;
883 for (i = 0; i < plen; ++i) {
884 if (packets[i].len > 0)
885 xfree(packets[i].payload);
888 free(packets);
890 for (i = 0; i < dlen; ++i) {
891 free(packet_dyn[i].cnt);
892 free(packet_dyn[i].rnd);
895 free(packet_dyn);
898 void compile_packets(char *file, bool verbose, unsigned int cpu,
899 bool invoke_cpp, char *const cpp_argv[])
901 char tmp_file[128];
902 int ret = -1;
904 memset(tmp_file, 0, sizeof(tmp_file));
905 our_cpu = cpu;
907 if (invoke_cpp) {
908 if (cpp_exec(file, tmp_file, sizeof(tmp_file), cpp_argv)) {
909 fprintf(stderr, "Failed to invoke C preprocessor!\n");
910 goto err;
912 file = tmp_file;
915 if (!strncmp("-", file, strlen("-")))
916 yyin = stdin;
917 else
918 yyin = fopen(file, "r");
919 if (!yyin) {
920 fprintf(stderr, "Cannot open %s: %s!\n", file, strerror(errno));
921 goto err;
924 realloc_packet();
925 if (yyparse() != 0)
926 goto err;
927 finalize_packet();
929 if (our_cpu == 0 && verbose)
930 dump_conf();
932 ret = 0;
933 err:
934 if (yyin && yyin != stdin)
935 fclose(yyin);
937 if (invoke_cpp)
938 unlink(tmp_file);
939 if (ret)
940 die();
943 void compile_packets_str(char *str, bool verbose, unsigned int cpu)
945 int ret = 1;
947 our_cpu = cpu;
948 realloc_packet();
950 yy_scan_string(str);
951 if (yyparse() != 0)
952 goto err;
954 finalize_packet();
955 if (our_cpu == 0 && verbose)
956 dump_conf();
958 ret = 0;
959 err:
960 yylex_destroy();
962 if (ret)
963 die();
966 void yyerror(const char *err)
968 fprintf(stderr, "Syntax error at line %d, char '%s': %s\n", yylineno, yytext, err);