Changeset c909fad for lib/protocols_l3.c


Ignore:
Timestamp:
03/06/13 13:28:51 (8 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
68b7f29
Parents:
af27241
Message:
  • Added checksumming functions for both the IP and transport layer. The functions will calculate the correct checksum for that packet and also return a pointer to the checksum field in the appropriate header so that the caller can either evaluate whether the checksum is correct, replace the existing checksum, or do whatever they want.
  • Also managed to fix a bunch of broken Revision svn tags
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/protocols_l3.c

    r5699389 rc909fad  
    3636#include "libtrace.h"
    3737#include "protocols.h"
     38#include "checksum.h"
    3839#include <assert.h>
    3940#include <stdlib.h>
     
    676677}
    677678
     679DLLEXPORT uint16_t *trace_checksum_layer3(libtrace_packet_t *packet,
     680                uint16_t *csum) {
     681
     682        void *l3;
     683        uint16_t ethertype;
     684        uint32_t remaining;
     685        uint16_t *csum_ptr;
     686
     687        uint8_t safety[65536];
     688
     689        if (csum == NULL)
     690                return NULL;
     691       
     692        l3 = trace_get_layer3(packet, &ethertype, &remaining);
     693               
     694        if (l3 == NULL)
     695                return NULL;
     696       
     697        if (ethertype == TRACE_ETHERTYPE_IP) {
     698                libtrace_ip_t *ip = (libtrace_ip_t *)l3;
     699                if (remaining < sizeof(libtrace_ip_t))
     700                        return NULL;
     701
     702                csum_ptr = &ip->ip_sum;
     703
     704                /* I hate memcpys, but this is the only truly safe way to
     705                 * do this without modifying the packet. I'm trying to be
     706                 * careful about not creating any more thread-safety issues
     707                 * than there already are :) */
     708                memcpy(safety, ip, ip->ip_hl * sizeof(uint32_t));
     709               
     710                /* Set the checksum to zero, so we can do the calculation */
     711                ip = (libtrace_ip_t *)safety;
     712                ip->ip_sum = 0;
     713
     714                *csum = checksum_buffer(safety, ip->ip_hl * sizeof(uint32_t));
     715               
     716                /* Remember to byteswap appropriately */
     717                *csum = ntohs(*csum);
     718               
     719                return csum_ptr;
     720        }
     721
     722        return NULL;
     723}
     724
Note: See TracChangeset for help on using the changeset viewer.