Move setting next protocol id field from higher protocols (ARP, IPv4)
to Ethernet. It makes code little more generic w/o checking each lower
protocol and setting specific field id.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_l2.c | 24 +++++++++++++++++++++++-
 trafgen_l3.c |  4 +---
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/trafgen_l2.c b/trafgen_l2.c
index 529dc36..c0e92a3 100644
--- a/trafgen_l2.c
+++ b/trafgen_l2.c
@@ -6,6 +6,7 @@
 #include <net/if_arp.h>
 #include <linux/if_ether.h>
 
+#include "die.h"
 #include "built_in.h"
 #include "trafgen_l2.h"
 #include "trafgen_proto.h"
@@ -16,6 +17,27 @@ static struct proto_field eth_fields[] = {
        { .id = ETH_TYPE,     .len = 2, .offset = 12 },
 };
 
+static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
+{
+       uint16_t eth_type;
+
+       switch(pid) {
+       case PROTO_ARP:
+               eth_type = ETH_P_ARP;
+               break;
+       case PROTO_IP4:
+               eth_type = ETH_P_IP;
+               break;
+       case PROTO_IP6:
+               eth_type = ETH_P_IPV6;
+               break;
+       default:
+               panic("eth: Not supported protocol id %u\n", pid);
+       }
+
+       proto_field_set_default_be16(hdr, ETH_TYPE, eth_type);
+}
+
 static void eth_header_init(struct proto_hdr *hdr)
 {
        proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
@@ -27,6 +49,7 @@ static struct proto_hdr eth_hdr = {
        .id             = PROTO_ETH,
        .layer          = PROTO_L2,
        .header_init    = eth_header_init,
+       .set_next_proto = eth_set_next_proto,
 };
 
 static struct proto_field arp_fields[] = {
@@ -51,7 +74,6 @@ static void arp_header_init(struct proto_hdr *hdr)
                uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
                proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast);
-               proto_field_set_default_be16(lower, ETH_TYPE, ETH_P_ARP);
        }
 
        proto_header_fields_add(hdr, arp_fields, array_size(arp_fields));
diff --git a/trafgen_l3.c b/trafgen_l3.c
index 9e5126a..58eaa01 100644
--- a/trafgen_l3.c
+++ b/trafgen_l3.c
@@ -37,9 +37,7 @@ static void ipv4_header_init(struct proto_hdr *hdr)
 
        lower = proto_lower_default_add(hdr, PROTO_ETH);
 
-       if (lower->id == PROTO_ETH)
-               proto_field_set_default_be16(lower, ETH_TYPE, ETH_P_IP);
-       else if (lower->id == PROTO_IP4)
+       if (lower->id == PROTO_IP4)
                proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_IPIP);
 
        proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_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