UDP checksum calculation

The UDP checksum is calculated to verify the integrity of the UDP header and data. It is optional in IPv4 but mandatory in IPv6. Here is a step-by-step process for calculating the UDP checksum:

  1. Pseudo-header Preparation: Construct a pseudo-header, which is not actually transmitted but used in the checksum calculation. It includes the following fields:

    • Source IP address (4 bytes for IPv4, 16 bytes for IPv6)
    • Destination IP address (4 bytes for IPv4, 16 bytes for IPv6)
    • Zero (1 byte for IPv4, 3 bytes for IPv6)
    • Protocol (1 byte, which is 17 for UDP)
    • UDP length (2 bytes, the length of the UDP header and data)
  2. UDP Header and Data: The actual UDP header and the data are then appended to the pseudo-header for checksum calculation.

  3. Checksum Calculation:

    • Combine the pseudo-header, UDP header, and UDP data.
    • If the length of this combination is odd, pad it with a zero byte at the end to make it even.
    • Calculate the sum of all 16-bit words in this combination, adding any overflow bits back into the lower 16 bits.
    • Take the one’s complement (bitwise NOT) of the result.
  4. Placement: Place the resulting checksum in the UDP header checksum field.

Here’s a more detailed breakdown:

Pseudo-header Construction (IPv4)

+---------------------+---------------------+
|     Source IP       |  Destination IP     |
+---------------------+---------------------+
|  Zero   | Protocol  |      UDP Length     |
+---------------------+---------------------+

Pseudo-header Construction (IPv6)

+---------------------------------------------------------------+
|                       Source IP Address                       |
+---------------------------------------------------------------+
|                    Destination IP Address                     |
+---------------------------------------------------------------+
|           UDP Length            |  Zero    | Protocol         |
+---------------------------------------------------------------+

UDP Header

+---------------------+---------------------+
|   Source Port       |  Destination Port   |
+---------------------+---------------------+
|     UDP Length      |     UDP Checksum    |
+---------------------+---------------------+

Example Calculation (IPv4)

Suppose we have the following:

  • Source IP: 192.168.1.1
  • Destination IP: 192.168.1.2
  • Source Port: 12345
  • Destination Port: 24672
  • Data: “Hello”
  1. Pseudo-header:

    Source IP:        C0 A8 01 01
    Destination IP:   C0 A8 01 02
    Zero:             00
    Protocol:         11 (for UDP)
    UDP Length:       00 13 (19 bytes, including UDP header and data)
    
  2. UDP Header and Data:

    Source Port:      30 39 (12345)
    Destination Port: 60 00 (24672)
    Length:           00 13 (19 bytes)
    Checksum:         00 00 (initially set to 0 for calculation)
    Data:             48 65 6C 6C 6F ("Hello")
    
  3. Combining and Padding:

    Pseudo-header:    C0 A8 01 01 C0 A8 01 02 00 11 00 13
    UDP Header+Data:  30 39 60 00 00 13 00 00 48 65 6C 6C 6F
    Combined:         C0 A8 01 01 C0 A8 01 02 00 11 00 13 30 39 60 00 00 13 00 00 48 65 6C 6C 6F
    
  4. Calculating the Sum:

    C0A8 + 0101 + C0A8 + 0102 + 0011 + 0013 + 3039 + 6000 + 0013 + 0000 + 4865 + 6C6C + 6F00 (padded with zero)
    
  5. Sum: Add all 16-bit words together:

    = 7E00 (sum)
    
  6. One’s Complement:

    Checksum: 81FF
    
  7. Final UDP Header:

    Source Port:      30 39 (12345)
    Destination Port: 60 00 (24672)
    Length:           00 13 (19 bytes)
    Checksum:         81 FF
    

This gives the UDP checksum of 81FF for the given data.

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 02:36:06       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 02:36:06       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 02:36:06       45 阅读
  4. Python语言-面向对象

    2024-07-20 02:36:06       55 阅读

热门阅读

  1. 设计模式总结

    2024-07-20 02:36:06       19 阅读
  2. Android gradle groovy改为kotlin总结

    2024-07-20 02:36:06       16 阅读
  3. 大模型日报 2024-07-18

    2024-07-20 02:36:06       16 阅读
  4. SpringBoot如何限制请求访问次数

    2024-07-20 02:36:06       17 阅读
  5. CSS简介

    2024-07-20 02:36:06       16 阅读
  6. C++ STL is_partitioned 用法和实现

    2024-07-20 02:36:06       16 阅读
  7. Python基础学习Day_06

    2024-07-20 02:36:06       16 阅读
  8. android SpannableStringBuilder span 设置点击事件

    2024-07-20 02:36:06       16 阅读