]>
git.saurik.com Git - apple/network_cmds.git/blob - alias/alias_util.c
2 Alias_util.h contains general utilities used by other functions
3 in the packet aliasing module. At the moment, there are functions
4 for computing IP header and TCP packet checksums.
6 The checksum routines are based upon example code in a Unix networking
7 text written by Stevens (sorry, I can't remember the title -- but
8 at least this is a good author).
10 Initial Version: August, 1996 (cjm)
12 Version 1.7: January 9, 1997
13 Added differential checksum update function.
17 Note: the checksum routines assume that the actual checksum word has
18 been zeroed out. If the checksum workd is filled with the proper value,
19 then these routines will give a result of zero (useful for testing
23 #include <sys/types.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <netinet/tcp.h>
30 #include "alias_local.h"
33 PacketAliasInternetChecksum(u_short
*ptr
, int nbytes
)
46 *((u_char
*) &oddbyte
) = *(u_char
*) ptr
;
49 sum
= (sum
>> 16) + (sum
& 0xffff);
55 IpChecksum(struct ip
*pip
)
57 return( PacketAliasInternetChecksum((u_short
*) pip
,
63 TcpChecksum(struct ip
*pip
)
67 int nhdr
, ntcp
, nbytes
;
70 nhdr
= pip
->ip_hl
<< 2;
71 ntcp
= ntohs(pip
->ip_len
) - nhdr
;
73 tc
= (struct tcphdr
*) ((char *) pip
+ nhdr
);
76 /* Add up TCP header and data */
87 *((u_char
*) &oddbyte
) = *(u_char
*) ptr
;
91 /* "Pseudo-header" data */
92 ptr
= (u_short
*) &(pip
->ip_dst
);
95 ptr
= (u_short
*) &(pip
->ip_src
);
98 sum
+= htons((u_short
) ntcp
);
99 sum
+= htons((u_short
) pip
->ip_p
);
101 /* Roll over carry bits */
102 sum
= (sum
>> 16) + (sum
& 0xffff);
105 /* Return checksum */
106 return((u_short
) ~sum
);
111 DifferentialChecksum(u_short
*cksum
, u_short
*new, u_short
*old
, int n
)
119 accumulate
-= *new++;
120 accumulate
+= *old
++;
125 accumulate
= -accumulate
;
126 accumulate
= (accumulate
>> 16) + (accumulate
& 0xffff);
127 accumulate
+= accumulate
>> 16;
128 *cksum
= (u_short
) ~accumulate
;
132 accumulate
= (accumulate
>> 16) + (accumulate
& 0xffff);
133 accumulate
+= accumulate
>> 16;
134 *cksum
= (u_short
) accumulate
;