tcp/ip通信中ip头部结构iph->check校验计算

通过raw socket修改通信数据后,可通过该函数重新校验计算iph->check值

在http://www.cnblogs.com/dpf-10/p/7899237.html查看实际调用

static inline unsigned short ip_fast_csum(unsigned char* iph,unsigned int ihl){
    unsigned int sum;

__asm__ __volatile__(
    "movl (%1), %0 ;
"
    "subl $4, %2 ;
"
    "jbe 2f ;
"
    "addl 4(%1), %0 ;
"
    "adcl 8(%1), %0 ;
"
    "adcl 12(%1), %0 ;
"
    "1: adcl 16(%1), %0 ;
"
    "lea 4(%1), %1 ;
"
    "decl %2 ;
"
    "jne 1b ;
"
    "adcl $0, %0 ;
"
    "movl %0, %2 ;
"   //保存sum的值到%2
    "shrl $16, %0 ;
"  //右移16位(读取高16位)到%0
    "addw %w2, %w0 ;
" //%0的16位加%2的16位
    "adcl $0, %0 ;
"   //若进位加上进位
    "notl %0 ;
" //取反
    "2: ;
"
/* 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);
}
原文地址:https://www.cnblogs.com/dpf-10/p/8810139.html