2010年12月21日 星期二

udp checksum的計算

UDP

udp checksum的計算

提示:
1.UDP的Pseudo Header會用到 IP 封包中的 Sorurce Address, Destination Address, 與 Protocol。
2.UDP Length會在Pseudo Header與UDP Header中各出現1次,也就是說會被計算2次。
3.若Data的長度若非Word(2 Bytes)的倍數,則必須在最後面補一個 byte的padding「0」。
假設有一封包資料如下
// ------- BEGIN Ethernet HEADER ----------------------------
00 09 5b 4f 64 72 // Destination HW address
00 07 95 e7 79 2d // Src HW address
08 00 // Type (not really sure what this octet's for)
// ------- BEGIN IP HEADER ----------------------------
45 // IP version (IPv4)
00 // Differentiated Services Code Point (i have no idea what this octet is for)
00 38 // Total Length (of the packet?)
5d 02 // ID (not really sure what this octet's for)
00 00 // Fragmentation offset
80 // Time To Live (in network hops)
11 // Protocol (UDP)
33 d6 // IP Header Checksum
c0 a8 00 02 // Source IP
c0 f6 28 3c // Destination IP
// ------- BEGIN UDP HEADER ---------------------------
6d 38 // Source Port
6d 2e // Destination Port
00 24 // Length (of the UDP Packet?)
29 b5 // UDP Checksum
ff ff ff ff // Marker (part of the packet data, no idea what it's for)
67 65 74 73 65 72 76 65 72 73 20 38 32 20 66 75 6c 6c 20 65 6d 70 74 79 // Packet Data
將上述資料以2byte為一組(16 bits)予以加總(Pseudo Header, UDP Header, UDP Data)
c0a8 + 0002 + c0f6 + 283c + // Source IP, Dest IP
0011 + // Protocol
0024 + // UDP length
6d38 + 6d2e + // Source Port, Dest Port
0024 + // UDP length
0000 + // empty checksum
ffff + ffff + 6765 + 7473 + 6572 + 7665 + 7273 + 2038 + 3220 + 6675 + 6c6c + 2065 + 6d70 + 7479 // Data
= 8d642
進位的LSB必須再加回來
8 + d642 = d64a
對結果求補數,即為checksum值
~d64a = 29b5

沒有留言:

張貼留言