source: lib/checksum.c @ 68b7f29

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 68b7f29 was c909fad, checked in by Shane Alcock <salcock@…>, 9 years ago
  • 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
  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007-2013 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33#include "checksum.h"
34
35uint32_t add_checksum(void *buffer, uint16_t length) {
36        uint32_t sum = 0;
37        uint16_t * buff = (uint16_t *) buffer;
38        uint16_t count = length;
39        uint16_t val;
40
41        while(count > 1 ) {
42                val = *buff;
43                sum += val;
44                buff ++;
45                count = count -2;
46        }
47
48        if(count > 0) {
49                sum += *(uint8_t *)buff;
50        }
51
52        return sum;
53}
54
55uint16_t finish_checksum(uint32_t sum) {
56        while (sum>>16) {
57                sum = (sum & 0xffff) + (sum >> 16);
58        }
59        return (uint16_t)~sum;
60}
61
62uint16_t checksum_buffer(void *buffer, uint16_t length) {
63
64        uint32_t sum = add_checksum(buffer, length);
65        return finish_checksum(sum);
66
67}
68
69uint32_t ipv4_pseudo_checksum(libtrace_ip_t *ip) {
70
71        uint32_t sum = 0;
72        uint16_t temp = 0;
73
74        sum += add_checksum(&ip->ip_src.s_addr,sizeof(uint32_t));
75        sum += add_checksum(&ip->ip_dst.s_addr,sizeof(uint32_t));
76
77        temp = htons(ip->ip_p);
78        sum += add_checksum(&temp, sizeof(uint16_t));
79
80        temp = htons(ntohs(ip->ip_len) - (ip->ip_hl * 4));
81        sum += add_checksum(&temp, sizeof(uint16_t));
82
83        return sum;     
84
85}
86
87uint32_t ipv6_pseudo_checksum(libtrace_ip6_t *ip) {
88
89        uint32_t sum = 0;
90        uint16_t temp = 0;
91
92        sum += add_checksum(&ip->ip_src.s6_addr,sizeof(struct in6_addr));
93        sum += add_checksum(&ip->ip_dst.s6_addr,sizeof(struct in6_addr));
94       
95        temp = ip->plen;
96        sum += add_checksum(&temp, sizeof(uint16_t));
97
98
99        temp = htons(ip->nxt);
100        sum += add_checksum(&temp, sizeof(uint16_t));
101
102
103        return sum;     
104
105}
106
107
Note: See TracBrowser for help on using the repository browser.