forked from renatoaloi/Enc28CoreLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.c
106 lines (100 loc) · 3.48 KB
/
checksum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "checksum.h"
//#include "net.h"
// The Ip checksum is calculated over the ip header only starting
// with the header length field and a total length of 20 bytes
// unitl ip.dst
// You must set the IP checksum field to zero before you start
// the calculation.
// len for ip is 20.
//
// For UDP/TCP we do not make up the required pseudo header. Instead we
// use the ip.src and ip.dst fields of the real packet:
// The udp checksum calculation starts with the ip.src field
// Ip.src=4bytes,Ip.dst=4 bytes,Udp header=8bytes + data length=16+len
// In other words the len here is 8 + length over which you actually
// want to calculate the checksum.
// You must set the checksum field to zero before you start
// the calculation.
// len for udp is: 8 + 8 + data length
// len for tcp is: 4+4 + 20 + option len + data length
//
// For more information on how this algorithm works see:
// http://www.netfor2.com/checksum.html
// http://www.msc.uky.edu/ken/cs471/notes/chap3.htm
// The RFC has also a C code example: http://www.faqs.org/rfcs/rfc1071.html
uint16_t meu_checksum(uint8_t *header, uint16_t len)
{
uint32_t sum = 0;
// build the sum of 16bit words
while(len >1){
sum += 0xFFFF & (*header<<8|*(header+1));
header+=2;
len-=2;
}
// if there is a byte left then add it (padded with zero)
if (len){
sum += (0xFF & *header)<<8;
}
// now calculate the sum over the bytes in the sum
// until the result is only 16bit long
while (sum>>16){
sum = (sum & 0xFFFF)+(sum >> 16);
}
// build 1's complement:
return( (uint16_t) sum ^ 0xFFFF);
}
uint16_t checksum(uint8_t *buf, uint16_t len, uint8_t type)
{
// type 0=ip
// 1=udp
// 2=tcp
uint32_t sum = 0;
//if(type==0){
// // do not add anything
//}
if(type==1){
sum+=IP_PROTO_UDP_V; // protocol udp
// the length here is the length of udp (data+header len)
// =length given to this function - (IP.scr+IP.dst length)
sum+=len-8; // = real tcp len
}
if(type==2){
sum+=IP_PROTO_TCP_V;
// the length here is the length of tcp (data+header len)
// =length given to this function - (IP.scr+IP.dst length)
sum+=len-8; // = real tcp len
}
// build the sum of 16bit words
while(len >1){
sum += 0xFFFF & (*buf<<8|*(buf+1));
buf+=2;
len-=2;
}
// if there is a byte left then add it (padded with zero)
if (len){
sum += (0xFF & *buf)<<8;
}
// now calculate the sum over the bytes in the sum
// until the result is only 16bit long
while (sum>>16){
sum = (sum & 0xFFFF)+(sum >> 16);
}
// build 1's complement:
return( (uint16_t) sum ^ 0xFFFF);
}
/*
void fillChecksum(uint8_t *buf)
{
uint16_t ck;
// clear the 2 byte checksum
buf[IP_CHECKSUM_P] = 0;
buf[IP_CHECKSUM_P+1] = 0;
buf[IP_FLAGS_P] = 0x40; // don't fragment
buf[IP_FLAGS_P + 1] = 0; // fragement offset
buf[IP_TTL_P] = 64; // ttl
// calculate the checksum:
ck = checksum(&buf[IP_P], IP_HEADER_LEN_V,0);
buf[IP_CHECKSUM_P] = ck >> 8;
buf[IP_CHECKSUM_P+1] = ck & 0xff;
}
*/