码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-12 20:48:46      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:contain   tcp   调用   char   int   turn   inpu   sock   origin   

通过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);
}

 

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

标签:contain   tcp   调用   char   int   turn   inpu   sock   origin   

原文地址:https://www.cnblogs.com/dpf-10/p/8810139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!