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
|
/*
* BIRD Library -- IP One-Complement Checksum
*
* (c) 1999 Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdarg.h>
#include "nest/bird.h"
#include "checksum.h"
static u16
ipsum_calc(void *frag, unsigned len, va_list args)
{
u16 sum = 0;
for(;;)
{
u16 *x = frag;
ASSERT(!(len % 2));
while (len)
{
u16 z = sum + *x++;
sum = z + (z < sum);
len -= 2;
}
frag = va_arg(args, void *);
if (!frag)
break;
len = va_arg(args, unsigned);
}
return sum;
}
int
ipsum_verify(void *frag, unsigned len, ...)
{
va_list args;
u16 sum;
va_start(args, len);
sum = ipsum_calc(frag, len, args);
va_end(args);
return sum == 0xffff;
}
u16
ipsum_calculate(void *frag, unsigned len, ...)
{
va_list args;
u16 sum;
va_start(args, len);
sum = ipsum_calc(frag, len, args);
va_end(args);
return 0xffff - sum;
}
|