File: csum.c

package info (click to toggle)
netselect 0.3.ds1-28
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 336 kB
  • ctags: 243
  • sloc: ansic: 1,082; sh: 625; makefile: 43
file content (33 lines) | stat: -rw-r--r-- 599 bytes parent folder | download | duplicates (3)
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
/*
    Copyright (c)  2006, 2007		Dmitry Butskoy
					<buc@citadel.stu.neva.ru>
    License:  GPL v2 or any later

    See COPYING for the status of this software.
*/

#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>


u_int16_t in_csum (const void *ptr, size_t len) {
	const u_int16_t *p = (const u_int16_t *) ptr;
	size_t nw = len / 2;
	unsigned int sum = 0;
	u_int16_t res;

	while (nw--)  sum += *p++;

	if (len & 0x1)
	    sum += htons (*((unsigned char *) p) << 8);

	sum = (sum >> 16) + (sum & 0xffff);
	sum += (sum >> 16);

	res = ~sum;
	if (!res)  res = ~0;

	return res;
}