OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / sendip / csum.c
1 /* csum.c
2  * Computes the standard internet checksum of a set of data (from RFC1071)
3  *      ChangeLog since sendip 2.0:
4  * 02/12/2001: Moved ipv6_csum into icmp.c as that is where it is used
5  * 22/01/2002: Include types.h to make sure u_int*_t defined on Solaris
6  */
7
8 #define __USE_BSD    /* GLIBC */
9 #define _BSD_SOURCE  /* LIBC5 */
10 #include <sys/types.h>
11 #include <netinet/in_systm.h>
12 #include <netinet/in.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "types.h"
16
17 u_int16_t csum (u_int16_t *packet, int packlen);
18
19 /* Checksum a block of data */
20 u_int16_t csum (u_int16_t *packet, int packlen) {
21         register unsigned long sum = 0;
22
23         while (packlen > 1) {
24                 sum+= *(packet++);
25                 packlen-=2;
26         }
27
28         if (packlen > 0)
29                 sum += *(unsigned char *)packet;
30
31         /* TODO: this depends on byte order */
32
33         while (sum >> 16)
34                 sum = (sum & 0xffff) + (sum >> 16);
35
36         return (u_int16_t) ~sum;
37 }