tcpcksum.c,v

来自「TCP-IP红宝书源代码」· C,V 代码 · 共 98 行

C,V
98
字号
head	1.2;
access;
symbols;
locks
	dls:1.2; strict;
comment	@ * @;


1.2
date	97.09.21.19.29.52;	author dls;	state Dist;
branches;
next	1.1;

1.1
date	94.01.14.03.51.20;	author dls;	state v2e1;
branches;
next	;


desc
@@


1.2
log
@pre-3e code
@
text
@/* tcpcksum.c - tcpcksum */

#include <conf.h>
#include <kernel.h>
#include <network.h>

/*------------------------------------------------------------------------
 *  tcpcksum -  compute a TCP pseudo-header checksum
 *------------------------------------------------------------------------
 */
unsigned short tcpcksum(pep, len)
struct ep	*pep;
int		len;
{
	struct	ip	*pip = (struct ip *)pep->ep_data;
	struct	tcp	*ptcp = (struct tcp *)pip->ip_data;
	unsigned	short	*sptr;
	unsigned	long tcksum;
	int		i;

	tcksum = 0;

	sptr = (unsigned short *) &pip->ip_src;
	/* 2*IP_ALEN octets = IP_ALEN shorts... */
	/* they are in net order.		*/
	for (i=0; i<IP_ALEN; ++i)
		tcksum += *sptr++;
	sptr = (unsigned short *)ptcp;
#ifdef notnow
kprintf("tcpcksum: pep %X order %x len %u (swapped %u)\n", pep, pep->ep_order,
len, net2hs(len));
#endif
	tcksum += hs2net(IPT_TCP + len);
	if (len % 2) {
		((char *)ptcp)[len] = 0;	/* pad */
		len += 1;	/* for the following division */
	}
	len >>= 1;	/* convert to length in shorts */

	for (i=0; i<len; ++i)
		tcksum += *sptr++;
	tcksum = (tcksum >> 16) + (tcksum & 0xffff);
	tcksum += (tcksum >> 16);

	return (short)(~tcksum & 0xffff);
}
@


1.1
log
@Initial revision
@
text
@d11 3
a13 2
unsigned short tcpcksum(pip)
struct	ip	*pip;
d15 1
d17 1
a17 1
	unsigned	short	*sptr, len;
d23 1
a23 1
	sptr = (unsigned short *) pip->ip_src;
d29 4
a32 1
	len = pip->ip_len - IP_HLEN(pip);
@

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?