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 ;\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"   //保存sum的值到%2
    "shrl $16, %0 ;\n"  //右移16位(读取高16位)到%0
    "addw %w2, %w0 ;\n" //%0的16位加%2的16位
    "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);
}

 

posted @ 2018-04-12 20:10  你且浅笑相安  阅读(432)  评论(0编辑  收藏  举报