-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.c
143 lines (113 loc) · 3.54 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ip.h> // required by "struct iph"
#include <netinet/tcp.h> // required by "struct tcph"
#include <netinet/udp.h> // required by "struct udph"
#define PKT_BUF_SIZE 1500
unsigned short in_cksum(unsigned short* addr, int len) // Interent checksum
{
int nleft = len, sum = 0;
unsigned short *w = addr, answer = 0;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(unsigned char*) &answer = *(u_char*) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += sum >> 16;
return ~sum;
}
unsigned short ip_checksum(unsigned char *iphdr)
{
char buf[20]; // IP header size
struct iphdr *iph;
memcpy(buf, iphdr, sizeof(buf));
iph = (struct iphdr *) buf;
iph->check = 0;
return in_cksum((unsigned short *)buf, sizeof(buf));
}
///////////////////////////////////////////////////////////////
struct pseudo_IP_header /* The pseudo IP header (checksum calc) */
{
unsigned int source, destination;
char zero_byte, protocol;
unsigned short len;
};
unsigned short tcp_checksum(unsigned char *input)
{
char buf[PKT_BUF_SIZE];
struct tcphdr *tcph;
struct pseudo_IP_header *psh;
struct iphdr *iph;
unsigned short ck_bkup;
memset(buf, 0, PKT_BUF_SIZE);
iph = (struct iphdr *) input;
tcph = (struct tcphdr *) (input + iph->ihl*4);
psh = (struct pseudo_IP_header *) buf;
ck_bkup = tcph->check;
tcph->check = 0;
psh->source = iph->saddr;
psh->destination = iph->daddr;
psh->zero_byte = 0;
psh->protocol = 6;
psh->len = htons(ntohs(iph->tot_len) - iph->ihl * 4);
memcpy(buf + 12, tcph, ntohs(iph->tot_len) - iph->ihl * 4);
tcph->check = ck_bkup;
return in_cksum((unsigned short *) buf, ntohs(iph->tot_len) - iph->ihl * 4 + 12);
}
unsigned short udp_checksum(unsigned char *input)
{
char buf[PKT_BUF_SIZE];
struct udphdr *udph;
struct pseudo_IP_header *psh;
struct iphdr *iph;
unsigned short ck_bkup;
memset(buf, 0, PKT_BUF_SIZE);
iph = (struct iphdr *) input;
udph = (struct udphdr *) (input + iph->ihl*4);
psh = (struct pseudo_IP_header *) buf;
ck_bkup = udph->check;
udph->check = 0;
psh->source = iph->saddr;
psh->destination = iph->daddr;
psh->zero_byte = 0;
psh->protocol = 17;
psh->len = htons(ntohs(iph->tot_len) - iph->ihl * 4);
memcpy(buf + 12, udph, ntohs(iph->tot_len) - iph->ihl * 4);
udph->check = ck_bkup;
return in_cksum((unsigned short *) buf, ntohs(iph->tot_len) - iph->ihl * 4 + 12);
}
///////////////////////////////////////////////////////////////
void show_checksum(unsigned char *data, int transport)
{
struct iphdr *iph = (struct iphdr *) data;
printf(" IP checksum = %x\t%x\n", (unsigned short) iph->check, ip_checksum(data));
if(iph->check != ip_checksum(data)) {
fprintf(stderr, "IP checksum error (%x,%x)\n", iph->check, ip_checksum(data));
exit(1);
}
if(transport && iph->protocol==IPPROTO_TCP) {
struct tcphdr *tcph = (struct tcphdr *) (((char *) iph) + iph->ihl*4);
printf("TCP checksum = %x\t%x\n", (unsigned short) tcph->check, tcp_checksum(data));
if( tcph->check != tcp_checksum(data) )
{
fprintf(stderr, "TCP checksum error (%x %x)\n", tcph->check, tcp_checksum(data));
exit(1);
}
}
else if(transport && iph->protocol==IPPROTO_UDP) {
struct udphdr *udph = (struct udphdr *) (((char *) iph) + iph->ihl*4);
printf("UDP checksum = %x\t%x\n", (unsigned short) udph->check, udp_checksum(data));
if( udph->check != udp_checksum(data) )
{
fprintf(stderr, "UDP checksum error (%x %x)\n", udph->check, udp_checksum(data));
exit(1);
}
}
}