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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值