zoukankan      html  css  js  c++  java
  • tcp校验和

    伪首部(pseudo header),通常指TCP伪首部和UDP伪首部
    TCP的校验和是必需的,而UDP的校验和是可选的
    TCP校验是需要校验包头和数据的

    //共12字节
    typedef struct
    {
        unsigned long saddr; //源IP地址
        unsigned long daddr; //目的IP地址
        unsigned char mbz; // mbz = must be zero, 用于填充对齐
        unsigned char protocal; //8位协议号
        unsigned short tcpl; //TCP包长度
    }psdheader_t;

    伪首部是一个虚拟的数据结构,仅仅是为计算校验和
    接收方计算检验和错误,IP就丢弃收到的数据报

    tcp

    RFC 793的TCP校验和定义
    The checksum field is the 16 bit one’s complement of the one’s complement sum of all 16-bit words in the header and text.
    If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right
    with zeros to form a 16-bit word for checksum purposes. The pad is not transmitted as part of the segment. While computing
    the checksum, the checksum field itself is replaced with zeros

    如果总长度为奇数个字节,则在最后增添一位都为0的字节
    首先,把TCP报头中的校验和字段置为0
    其次,用反码相加法累加所有的16位字
    最后,对计算结果取反

    详细计算方法见ip校验和:
    http://blog.csdn.net/zhangxuechao_/article/details/50677220

    举例

    本地IP: 0xc0 0xa8 0x9f 0x01
    对方IP: 0xc0 0xa8 0x9f 0x82
    TCP字段: 0x04 0xc6 0x87 0x01 0x4b 0xd7 0x89 0x9f 0x4e 0x3b  0x90 0xae 0x50 0x18 0xff 0xff 0xeb 0x69 0x00 0x00
    
    (0xc0a8 + 0x9f01 + 0xc0a8 + 0x9f82 + 0x0006 + 0x0017) + 0x04c6 + 0x8701 + 0x4bd7 + 0x899f + 0x4e3b + 0x90ae + 0x5018 
    + 0xffff + 0x0000 + 0x0000 + 0x6162 + 0x6300 = 0x7148f
    0x0007 + 0x148f = 0x1496
    ~0x1496 = 0xeb69

    注:tcpl指的是tcp包头和数据的总长度(网络字节序)

  • 相关阅读:
    Asp.net mvc validaterequest无效的问题
    News Master-DC and Marvel they are super heroes mother
    <<杰克.韦尔奇自传>>
    UIUC同学Jia-Bin Huang收集的计算机视觉代码合集
    6 个优秀的开源 OCR 光学字符识别工具
    应用OpenCV进行OCR字符识别
    心胸与格局
    Software: MPEG-7 Feature Extraction Library
    也谈LBP
    3个著名加密算法(MD5、RSA、DES)的解析
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709832.html
Copyright © 2011-2022 走看看