Move setting lower protocol id field value from UDP & TCP
protocols to IPv4 only, so lower layer will know exactly value
to set in protocol id field.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_l3.c | 30 ++++++++++++++++++++++++------
 trafgen_l4.c | 16 ++--------------
 2 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/trafgen_l3.c b/trafgen_l3.c
index 58eaa01..0e923e0 100644
--- a/trafgen_l3.c
+++ b/trafgen_l3.c
@@ -5,6 +5,7 @@
 
 #include <linux/if_ether.h>
 
+#include "die.h"
 #include "csum.h"
 #include "built_in.h"
 #include "trafgen_l2.h"
@@ -33,12 +34,7 @@ static struct proto_field ipv4_fields[] = {
 
 static void ipv4_header_init(struct proto_hdr *hdr)
 {
-       struct proto_hdr *lower;
-
-       lower = proto_lower_default_add(hdr, PROTO_ETH);
-
-       if (lower->id == PROTO_IP4)
-               proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_IPIP);
+       proto_lower_default_add(hdr, PROTO_ETH);
 
        proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_fields));
 
@@ -65,11 +61,33 @@ static void ipv4_packet_finish(struct proto_hdr *hdr)
        }
 }
 
+static void ipv4_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
+{
+       uint8_t ip_proto;
+
+       switch(pid) {
+       case PROTO_IP4:
+               ip_proto = IPPROTO_IPIP;
+               break;
+       case PROTO_UDP:
+               ip_proto = IPPROTO_UDP;
+               break;
+       case PROTO_TCP:
+               ip_proto = IPPROTO_TCP;
+               break;
+       default:
+               panic("ipv4: Not supported protocol id %u\n", pid);
+       }
+
+       proto_field_set_default_u8(hdr, IP4_PROTO, ip_proto);
+}
+
 static struct proto_hdr ipv4_hdr = {
        .id             = PROTO_IP4,
        .layer          = PROTO_L3,
        .header_init    = ipv4_header_init,
        .packet_finish  = ipv4_packet_finish,
+       .set_next_proto = ipv4_set_next_proto,
 };
 
 void protos_l3_init(void)
diff --git a/trafgen_l4.c b/trafgen_l4.c
index 1505b43..64aada4 100644
--- a/trafgen_l4.c
+++ b/trafgen_l4.c
@@ -43,12 +43,7 @@ static struct proto_field tcp_fields[] = {
 
 static void udp_header_init(struct proto_hdr *hdr)
 {
-       struct proto_hdr *lower;
-
-       lower = proto_lower_default_add(hdr, PROTO_IP4);
-
-       if (lower->id == PROTO_IP4)
-               proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_UDP);
+       proto_lower_default_add(hdr, PROTO_IP4);
 
        proto_header_fields_add(hdr, udp_fields, array_size(udp_fields));
 }
@@ -85,14 +80,7 @@ static struct proto_hdr udp_hdr = {
 
 static void tcp_header_init(struct proto_hdr *hdr)
 {
-       struct proto_hdr *lower;
-
-       proto_lower_default_add(PROTO_IP4);
-
-       lower = proto_current_header();
-
-       if (lower->id == PROTO_IP4)
-               proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_TCP);
+       proto_lower_default_add(hdr, PROTO_IP4);
 
        proto_header_fields_add(hdr, tcp_fields, array_size(tcp_fields));
 
-- 
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