Use same function to calculate csum for packet_update
and for packet_finish events.

Allow update csum if one of the ICMPv6 fields was changed.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_l4.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/trafgen_l4.c b/trafgen_l4.c
index 19ebb37..908641c 100644
--- a/trafgen_l4.c
+++ b/trafgen_l4.c
@@ -202,39 +202,43 @@ static void icmpv6_header_init(struct proto_hdr *hdr)
        proto_header_fields_add(hdr, icmpv6_fields, array_size(icmpv6_fields));
 }
 
-static void icmpv6_packet_finish(struct proto_hdr *hdr)
+static void icmpv6_csum_update(struct proto_hdr *hdr)
 {
        struct proto_hdr *lower = proto_lower_header(hdr);
-       struct packet *pkt = current_packet();
+       struct packet *pkt = packet_get(hdr->pkt_id);
        uint16_t total_len;
        uint16_t csum;
 
        if (proto_field_is_set(hdr, ICMPV6_CSUM))
                return;
-
        if (!lower)
                return;
 
        total_len = pkt->len - hdr->pkt_offset;
 
-       switch (lower->id) {
-       case PROTO_IP6:
+       proto_field_set_be16(hdr, ICMPV6_CSUM, 0);
+
+       if (lower->id == PROTO_IP6) {
                csum = p6_csum((void *) proto_header_ptr(lower), 
proto_header_ptr(hdr),
                                total_len, IPPROTO_ICMPV6);
-               break;
-       default:
-               csum = 0;
-               break;
+
+               proto_field_set_be16(hdr, ICMPV6_CSUM, bswap_16(csum));
+               hdr->is_csum_valid = true;
        }
+}
 
-       proto_field_set_be16(hdr, ICMPV6_CSUM, bswap_16(csum));
+static void icmpv6_field_changed(struct proto_hdr *hdr, struct proto_field 
*field)
+{
+       hdr->is_csum_valid = false;
 }
 
 static struct proto_hdr icmpv6_hdr = {
        .id             = PROTO_ICMP6,
        .layer          = PROTO_L4,
        .header_init    = icmpv6_header_init,
-       .packet_finish  = icmpv6_packet_finish,
+       .packet_finish  = icmpv6_csum_update,
+       .packet_update  = icmpv6_csum_update,
+       .field_changed  = icmpv6_field_changed,
 };
 
 void protos_l4_init(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