forked from renatoaloi/EtherEncLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.c
103 lines (92 loc) · 3.66 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
/*
EtherEncLib - Ethernet ENC28J60 Library for Arduino
Copyright (C) 2015 Renato Aloi
[email protected] -- http://www.seriallink.com.br
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "checksum.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 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){
//--- made by SKA --- sum += (0xFF & *buf)<<8;
sum += 0xFFFF & (*buf<<8|0x00);
}
// 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;
}