0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-08 12:18:42 +00:00

Added packet checksumming code. Watch the comments for an explanation.

This commit is contained in:
Martin Mares 1999-05-10 21:37:39 +00:00
parent a2697f02ac
commit 1a54d44a23
3 changed files with 81 additions and 0 deletions

View File

@ -27,3 +27,5 @@ slists.c
slists.h
event.c
event.h
checksum.c
checksum.h

59
lib/checksum.c Normal file
View File

@ -0,0 +1,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;
}

20
lib/checksum.h Normal file
View File

@ -0,0 +1,20 @@
/*
* 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.
*/
#ifndef _BIRD_CHECKSUM_H_
#define _BIRD_CHECKSUM_H_
/*
* Both checksumming functions accept a vararg list of packet
* fragments finished by NULL pointer.
*/
int ipsum_verify(void *frag, unsigned len, ...);
u16 ipsum_calculate(void *frag, unsigned len, ...);
#endif