-
Notifications
You must be signed in to change notification settings - Fork 1
/
csum.c
60 lines (46 loc) · 1.28 KB
/
csum.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
/* csum.c
* Computes the standard internet checksum of a set of data (from RFC1071)
* ChangeLog since sendip 2.0:
* 02/12/2001: Moved ipv6_csum into icmp.c as that is where it is used
* 22/01/2002: Include types.h to make sure u_int*_t defined on Solaris
*/
#define __USE_BSD /* GLIBC */
#define _BSD_SOURCE /* LIBC5 */
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include "types.h"
u_int16_t csum (u_int16_t *packet, int packlen);
/* Checksum a block of data */
u_int16_t csum (u_int16_t *packet, int packlen) {
register unsigned long sum = 0;
while (packlen > 1) {
sum+= *(packet++);
packlen-=2;
}
if (packlen > 0)
sum += *(unsigned char *)packet;
/* TODO: this depends on byte order */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return (u_int16_t) ~sum;
}
/* Checksum a vector of blocks of data */
u_int16_t csumv (u_int16_t *packet[], int packlen[]) {
register unsigned long sum = 0;
int i;
for (i=0; packlen[i]; ++i) {
while (packlen[i] > 1) {
sum+= *(packet[i]++);
packlen[i]-=2;
}
if (packlen[i] > 0)
sum += *(unsigned char *)packet[i];
}
/* TODO: this depends on byte order */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return (u_int16_t) ~sum;
}