zoukankan      html  css  js  c++  java
  • TCPIP: UDP/TCP checksum

    UDP/TCP checksum

    1Content Table

    Internet checksum

    1’s complement sum

    UDP/TCP checksum

     

    2Internet checksum

    (1) Adjacent octets to be checksummed are paired to form 16-bit

    integers, and the 1's complement sum of these 16-bit integers is formed.

    (2) To generate a checksum, the checksum field itself is cleared,

    the 16-bit 1's complement sum is computed over the octets concerned, and the 1's complement of this sum is placed in the checksum field.

    (3) To check a checksum, the 1's complement sum is computed over the same set of octets, including the checksum field. If the result is all 1 bits (-0 in 1's complement arithmetic), the check succeeds.



    31’s complement sum

    Checksum of a block of data is the complement of the one's complement of the 16-bit sum of the block. If checksum is included in the block of data, the new block of data will have its checksum zero. This is how checksum is used for error detection in datagram transmissions.

    As an example, consider a block of data:

    0x23fb

    + 0x34c0

    + 0xa090

    + 0xbcaf

    + 0xfc05

    -----------

    0x2b1ff

    To calculate 16-bit 1's complement sum, the excess digit 2 needs to be added back to the least significant 16 bits:

    0xb1ff

    + 0x2

    -----------

    0xb201

    The complement of this is 0x4dfe. This is the checksum of the block of data.

    Note that: checksum(checksum+data)=0

    So, if we transmit the block of data including the checksum field, the receiver should see a checksum of 0 if there are no bit errors.



    4UDP/TCP checksum

     

    Although the basics for calculating the UDP checksum are similar to the general Internet checksum (the one’s complement of the one’s complement sum of 16-bit words), there are two small special details.

    First, the length of the UDP datagram can be an odd number of bytes, whereas the checksum algorithm adds 16-bit words (always an even number of bytes). The procedure for UDP is to append a (virtual) pad byte of 0 to the end of odd-length datagrams, just for the checksum computation and verification.

    The second detail is that UDP (as well as TCP) computes its checksum over a 12-byte pseudo-header derived from fields in the IPv4 header or a 40-byte pseudo-header derived from fields in the IPv6 header. This pseudo-header is also virtual and is used only for purposes of the checksum computation (at both the sender and the receiver). Its purpose is to let the UDP layer verify that the data has arrived at the correct destination (i.e., that IP has not accepted a misaddressed datagram, and that IP has not given UDP a datagram that is for another transport protocol).


    The TCP Checksum field covers the TCP header and data and some fields in the IP header, using a pseudo-header computation similar to the one used with UDP. It is mandatory for this field to be calculated and stored by the sender, and then verified by the receiver.



    5Reference

    1. https://tools.ietf.org/html/rfc1071

    2. http://www.cs.newpaltz.edu/~easwaran/CCN/Week5/Checksum.pdf

    3. Vol 1. TCP/IP Illustrated

  • 相关阅读:
    js把秒数转换为HH:MM:SS及时分秒格式
    java将流量KB转换为GB、MB、KB格式
    java将秒数转换为时分秒格式
    springboot前端向后端请求返回html语句
    java判断手机号三大运营商归属的工具类
    Java读取txt文件、excel文件的方法
    Linux安装JDK、Mysql和Tomcat
    原理篇—文件包含漏洞
    原理篇—上传漏洞
    原理篇—CSRF 跨站脚本请求伪造
  • 原文地址:https://www.cnblogs.com/wordchao/p/11127637.html
Copyright © 2011-2022 走看看