ip tcp check sum

190 篇文章 1 订阅
168 篇文章 0 订阅
unsigned short ip_fast_csum(unsigned char * iph,
  unsigned int ihl)
{
unsigned int sum;

__asm__ __volatile__(
    “movl (%1), %0 ;\n”
    “subl $4, %2 ;\n”
    “jbe 2f ;\n”
    “addl 4(%1), %0 ;\n”
    “adcl 8(%1), %0 ;\n”
    “adcl 12(%1), %0 ;\n”
“1:     adcl 16(%1), %0 ;\n”
    “lea 4(%1), %1 ;\n”
    “decl %2 ;\n”
    “jne 1b ;\n”
    “adcl $0, %0 ;\n”
    “movl %0, %2 ;\n”
    “shrl $16, %0 ;\n”
    “addw %w2, %w0 ;\n”
    “adcl $0, %0 ;\n”
    “notl %0 ;\n”
“2: ;\n”
/* Since the input registers which are loaded with iph and ihl
  are modified, we must also specify them as outputs, or gcc
  will assume they contain their original values. */
: “=r” (sum), “=r” (iph), “=r” (ihl)
: “1″ (iph), “2″ (ihl)
: “memory”);
return(sum);
}
unsigned short checksum(unsigned short *buf,int nword)
{
    unsigned long sum;
   
    for(sum=0;nword>0;nword–)
        sum += *buf++;
    sum = (sum>>16) + (sum&0xffff);
    sum += (sum>>16);
   
    return ~sum;
}


TCP CHECKSUM CALCULATION
(See also short Description of Internet Checksum).

To calculate TCP checksum a "pseudo header" is added to the TCP header. This includes:

IP Source Address		4 bytes
IP Destination Address		4 bytes
TCP Protocol			2 bytes
TCP Length  			2 bytes

The checksum is calculated over all the octets of the pseudo header, TCP header and data. 
If the data contains an odd number of octets a pad, zero octet is added to the end of data. The pseudo 
header and the pad are not transmitted with the packet. 

In the example code, 
u16 buff[] is an array containing all the octets in the TCP header and data.
u16 len_tcp is the length (number of octets) of the TCP header and data.
BOOL padding is 1 if data has an even number of octets and 0 for an odd number. 
u16 src_addr[4] and u16 dest_addr[4] are the IP source and destination address octets.

/*
**************************************************************************
Function: tcp_sum_calc()
**************************************************************************
Description: 
	Calculate TCP checksum
***************************************************************************
*/

typedef unsigned short u16;
typedef unsigned long u32;

u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], BOOL padding, u16 buff[])
{
u16 prot_tcp=6;
u16 padd=0;
u16 word16;
u32 sum;	
	
	// Find out if the length of data is even or odd number. If odd,
	// add a padding byte = 0 at the end of packet
	if (padding&1==1){
		padd=1;
		buff[len_tcp]=0;
	}
	
	//initialize sum to zero
	sum=0;
	
	// make 16 bit words out of every two adjacent 8 bit words and 
	// calculate the sum of all 16 vit words
	for (i=0;i<len_tcp+padd;i=i+2){
		word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
		sum = sum + (unsigned long)word16;
	}	
	// add the TCP pseudo header which contains:
	// the IP source and destinationn addresses,
	for (i=0;i<4;i=i+2){
		word16 =((src_addr[i]<<8)&0xFF00)+(src_addr[i+1]&0xFF);
		sum=sum+word16;	
	}
	for (i=0;i<4;i=i+2){
		word16 =((dest_addr[i]<<8)&0xFF00)+(dest_addr[i+1]&0xFF);
		sum=sum+word16; 	
	}
	// the protocol number and the length of the TCP packet
	sum = sum + prot_tcp + len_tcp;

	// keep only the last 16 bits of the 32 bit calculated sum and add the carries
    	while (sum>>16)
		sum = (sum & 0xFFFF)+(sum >> 16);
		
	// Take the one's complement of sum
	sum = ~sum;

return ((unsigned short) sum);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值