Add syntax to specify IPv4 header fields:

    { ip(df, mf, frag=100, prot=0x1, ecn=2, dscp=20) }

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_lexer.l  | 15 +++++++++++++++
 trafgen_parser.y | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/trafgen_lexer.l b/trafgen_lexer.l
index 9bbd982..b9bcd10 100644
--- a/trafgen_lexer.l
+++ b/trafgen_lexer.l
@@ -117,8 +117,23 @@ ip_addr            ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
 "resp"|"reply" { return F_RESP; }
 "op"           { return F_OPER; }
 
+"ihl"          { return F_IHL; }
+"ver"          { return F_VER; }
+"ttl"          { return F_TTL; }
+"dscp"         { return F_DSCP; }
+"ecn"          { return F_ECN; }
+"tos"          { return F_TOS; }
+"len"          { return F_LEN; }
+"id"           { return F_ID; }
+"flags"                { return F_FLAGS; }
+"frag"         { return F_FRAG; }
+"csum"         { return F_CSUM; }
+"df"           { return F_DF; }
+"mf"           { return F_MF; }
+
 "eth"          { return P_ETH; }
 "arp"          { return P_ARP; }
+"ip"           { return P_IP4; }
 
 [ ]*"-"[ ]*    { return '-'; }
 [ ]*"+"[ ]*    { return '+'; }
diff --git a/trafgen_parser.y b/trafgen_parser.y
index 185b4a8..e36babf 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -25,6 +25,7 @@
 #include "trafgen_conf.h"
 #include "trafgen_proto.h"
 #include "trafgen_l2.h"
+#include "trafgen_l3.h"
 #include "built_in.h"
 #include "die.h"
 #include "str.h"
@@ -350,9 +351,11 @@ static void proto_init(enum proto_id pid)
 
 %token F_DADDR F_SADDR F_PROT
 %token F_OPER F_SHA F_SPA F_THA F_TPA F_REQ F_RESP
+%token F_TTL F_DSCP F_ECN F_TOS F_LEN F_ID F_FLAGS F_FRAG F_IHL F_VER F_CSUM 
F_DF F_MF
 
 %token P_ETH
 %token P_ARP
+%token P_IP4
 
 %token ',' '{' '}' '(' ')' '[' ']' ':' '-' '+' '*' '/' '%' '&' '|' '<' '>' '^'
 
@@ -574,6 +577,7 @@ ddec
 proto
        : eth_proto { }
        | arp_proto { }
+       | ip4_proto { }
        ;
 
 eth_proto
@@ -627,6 +631,53 @@ arp
        : P_ARP { proto_init(PROTO_ARP); }
        ;
 
+ip4_proto
+       : ip4 '(' ip4_param_list ')' { }
+       ;
+
+ip4_param_list
+       : { }
+       | ip4_field { }
+       | ip4_field delimiter ip4_param_list { }
+       ;
+
+ip4_field
+       : F_VER skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_VER, $5); }
+       | F_IHL skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_IHL, $5); }
+       | F_DADDR  skip_white '=' skip_white ip_addr
+               { proto_field_set_u32(PROTO_IP4, IP4_DADDR, $5.s_addr); }
+       | F_SADDR  skip_white '=' skip_white ip_addr
+               { proto_field_set_u32(PROTO_IP4, IP4_SADDR, $5.s_addr); }
+       | F_PROT skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_PROTO, $5); }
+       | F_TTL skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_TTL, $5); }
+       | F_DSCP skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_DSCP, $5); }
+       | F_ECN skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_ECN, $5); }
+       | F_TOS skip_white '=' skip_white number
+               { proto_field_set_u8(PROTO_IP4, IP4_TOS, $5); }
+       | F_LEN skip_white '=' skip_white number
+               { proto_field_set_be16(PROTO_IP4, IP4_LEN, $5); }
+       | F_ID skip_white '=' skip_white number
+               { proto_field_set_be16(PROTO_IP4, IP4_ID, $5); }
+       | F_FLAGS skip_white '=' skip_white number
+               { proto_field_set_be16(PROTO_IP4, IP4_FLAGS, $5); }
+       | F_DF  { proto_field_set_be16(PROTO_IP4, IP4_DF, 1); }
+       | F_MF  { proto_field_set_be16(PROTO_IP4, IP4_MF, 1); }
+       | F_FRAG skip_white '=' skip_white number
+               { proto_field_set_be16(PROTO_IP4, IP4_FRAG_OFFS, $5); }
+       | F_CSUM skip_white '=' skip_white number
+               { proto_field_set_be16(PROTO_IP4, IP4_CSUM, $5); }
+       ;
+
+ip4
+       : P_IP4 { proto_init(PROTO_IP4); }
+       ;
+
 %%
 
 static void finalize_packet(void)
-- 
2.6.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to