ip,udp的checksum算法

来源:互联网 发布:seo编辑工资一般多少 编辑:程序博客网 时间:2024/05/30 04:59

http://irw.ncut.edu.tw/peterju/internet.html#udp udp checksum

IPv4

  • ip共32個bit, 分成NetID 與 HostID。
  • Network Mask即所謂的網路遮罩。網路遮罩的最主要用途在於子網路(Subnetwork)的切割,並使電腦在彼此建立通訊管道之前,可先行判斷通訊對象是否可 直接連通(Directly Reachable),再決定是否須轉送服務。
  • ip+ NetMask配合決定網段。
  • 每個網段的最前面ip作為網段辨識用, 最後面的ip作為網段廣播用。
  • 傳統網段的切割方式以8bit為切割, 因此只有A-C網段。
  • 因傳統網段的切割方式會造成ip浪費, 故發展以HostID給NetID以增加NetMask的方法稱為CIDR或SubNet。
  • 區域網路中,超過一個子網路存在時,即需使用通訊閘道(Gateway)加以區隔。
  • Gateway即所謂的通訊閘道(or router , according to OSI Definition)。通訊閘道一般用於區隔不同的子網路,其主要功能有:
    1. 阻擋同一子網路間之封包向外傳送(Filtering)。
    2. 轉送須跨越不同子網路間之網路封包(Redirecting)。
    3. 網路路徑之偵測及選擇(Routing)。
  • 路徑選擇(Routing)可分為靜態路由(Static Routing)及動態路由(Dynamic Routing)兩種,一般單一網路卡之電腦設備,採靜態路由者居多。

ip checksum的計算

首先,讓我們看看IP封包的格式是怎樣的,和其組成部份,以及各部份的長度如何:

Version (4)Internet Header Length (4)Type of Service (8)Total Length (16)Identification (16)Flags (3)Fragment Offset (13)Time To Live (8)Protocol (8)Header checksum (16)Source Address (32)Destination Address (32)Options (Variable)Padding (0-24) 
Data
.... 

有一 ip 封包檔頭資料為

45 // IP version (IPv4) 
00 // Differentiated Services Code Point (i have no idea what this octet is for) 
00 28 // Total Length (of the packet?) 
c5 38 // ID (not really sure what this octet's for) 
40 00 // Fragmentation offset 
67 // Time To Live (in network hops) 
06 // Protocol (TCP) 
a1 a1 // IP Header Checksum (two's compliment)
d5 36 14 83 // Source IP 
3d da 85 62 // Destination IP

將上述資料以2byte為一組(16 bits)予以加總

4500 + 0028 + c538 + 4000 + 6706 + 0000 + d536 + 1483 + 3dda + 8562 
= 35e5b

進位的LSB必須再加回來

3 + 5e5b = 5e5e

對結果求補數,即為checksum值

~5e5e = a1a1

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


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


由抓包软件的结果看,ip的对,udp的还是对不上。

再找个udp实例看看吧

0 0