1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #include "checksum.h" |
---|
29 | |
---|
30 | uint32_t add_checksum(void *buffer, uint16_t length) { |
---|
31 | uint32_t sum = 0; |
---|
32 | uint16_t * buff = (uint16_t *) buffer; |
---|
33 | uint16_t count = length; |
---|
34 | uint16_t val; |
---|
35 | |
---|
36 | while(count > 1 ) { |
---|
37 | val = *buff; |
---|
38 | sum += val; |
---|
39 | buff ++; |
---|
40 | count = count -2; |
---|
41 | } |
---|
42 | |
---|
43 | if(count > 0) { |
---|
44 | sum += *(uint8_t *)buff; |
---|
45 | } |
---|
46 | |
---|
47 | return sum; |
---|
48 | } |
---|
49 | |
---|
50 | uint16_t finish_checksum(uint32_t sum) { |
---|
51 | while (sum>>16) { |
---|
52 | sum = (sum & 0xffff) + (sum >> 16); |
---|
53 | } |
---|
54 | return (uint16_t)~sum; |
---|
55 | } |
---|
56 | |
---|
57 | uint16_t checksum_buffer(void *buffer, uint16_t length) { |
---|
58 | |
---|
59 | uint32_t sum = add_checksum(buffer, length); |
---|
60 | return finish_checksum(sum); |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | uint32_t ipv4_pseudo_checksum(libtrace_ip_t *ip) { |
---|
65 | |
---|
66 | uint32_t sum = 0; |
---|
67 | uint16_t temp = 0; |
---|
68 | |
---|
69 | sum += add_checksum(&ip->ip_src.s_addr,sizeof(uint32_t)); |
---|
70 | sum += add_checksum(&ip->ip_dst.s_addr,sizeof(uint32_t)); |
---|
71 | |
---|
72 | temp = htons(ip->ip_p); |
---|
73 | sum += add_checksum(&temp, sizeof(uint16_t)); |
---|
74 | |
---|
75 | temp = htons(ntohs(ip->ip_len) - (ip->ip_hl * 4)); |
---|
76 | sum += add_checksum(&temp, sizeof(uint16_t)); |
---|
77 | |
---|
78 | return sum; |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | uint32_t ipv6_pseudo_checksum(libtrace_ip6_t *ip) { |
---|
83 | |
---|
84 | uint32_t sum = 0; |
---|
85 | uint16_t temp = 0; |
---|
86 | |
---|
87 | sum += add_checksum(&ip->ip_src.s6_addr,sizeof(struct in6_addr)); |
---|
88 | sum += add_checksum(&ip->ip_dst.s6_addr,sizeof(struct in6_addr)); |
---|
89 | |
---|
90 | temp = ip->plen; |
---|
91 | sum += add_checksum(&temp, sizeof(uint16_t)); |
---|
92 | |
---|
93 | |
---|
94 | temp = htons(ip->nxt); |
---|
95 | sum += add_checksum(&temp, sizeof(uint16_t)); |
---|
96 | |
---|
97 | |
---|
98 | return sum; |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | |
---|