-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipsum.h
More file actions
31 lines (25 loc) · 709 Bytes
/
ipsum.h
File metadata and controls
31 lines (25 loc) · 709 Bytes
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
#ifndef IPSUM_H
#define IPSUM_H
#include <inttypes.h>
// do an ip checksum on a generic block of memory
// for IP, len should always be the size of the ip header (sizeof (struct ip))
static inline int ip_sum(char* packet, int n) {
uint16_t *p = (uint16_t*)packet;
uint16_t answer;
long sum = 0;
uint16_t odd_byte = 0;
while (n > 1) {
sum += *p++;
n -= 2;
}
/* mop up an odd byte, if necessary */
if (n == 1) {
*(uint8_t*)(&odd_byte) = *(uint8_t*)p;
sum += odd_byte;
}
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* ones-complement, truncate*/
return answer;
}
#endif