我想問一下根據ICMPv6協議計算16位校驗和的方案是否正確。我試圖按照Wikipedia,但我不確定主要是關於兩件事情。計算ICMPv6頭的16位校驗和
首先是什麼the packet length
的意思是 - 它是沒有校驗和的整個ICMPv6分組的分組長度,還是隻有有效載荷?它是否與IPv6一樣在八位組中?這個ICMPv6迴應請求的長度是多少?
6000 # beginning of IPv6 packet
0000 0000 3a00 FE80 0000 0000 0000 0202
B3FF FE1E 8329 FE80 0000 0000 0000 0202
B3FF FE1E 8330
8000 xxxx # this is beginning of the ICMP packet - type and checksum
a088 0000 0001 # from here including this line I compute the length
0203 0405 0607 0809 0a0b 0c0d 0e0f 1011
1213 1415 1617 1819 1a1b 1c1d 1e1f 2021
2223 2425 2627 2829 2a2b 2c2d 2e2f 3031
3233
這是否意味着上面的長度是56個八位字節,因爲我在下面的代碼中聲明?
然後我有問題了解這一點(再次從維基)。
在此僞報頭之後,校驗和將繼續,其中校驗和初始設置爲零的ICMPv6消息爲 。該 校驗和計算是根據互聯網協議 標準使用16位的補求和,然後 補充校驗本身和它插入校驗 場
這是否意味着我要補充的整體執行ICMPv6幀與校驗和字段上的0000是否也是校驗和?
我試圖在Python編寫一個簡單的程序進行這個:
# START OF Pseudo header
# we are doing 16 bit checksum hence quadruplets
## source IP
sip = ['FE80', '0000', '0000', '0000', '0202', 'B3FF', 'FE1E', '8329']
## destination IP
dip = ['FE80', '0000', '0000', '0000', '0202', 'B3FF', 'FE1E', '8330']
## next header - 32 bits, permanently set to (58)_dec ~ (88)_hex
nh = ['0000', '0088']
## packet length -> see my question above: (56)_dec ~ (38)_hex
lng = ['0038']
png = "8000 0000 a088 0000 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f 3031 3233".split(" ")
# END OF PSEUDO HEADER
tot = sip + dip + lng + nh + png # from what the sum is going to be counted
stot = sum([int(x, 16) for x in tot]) % 65535 # we are in 16 bits world
rstot = 65535 - stot # wrap around
res = hex(rstot) # convert to hex
print(stot, rstot)
print(res)
check = bin(rstot + stot)
print(check) # all ones
即以下的ICMPv6 Ping請求(具有IPv6報頭):
d392 30fb 0001 d393 30fb 0001 86dd 6000
0000 0000 3a00 FE80 0000 0000 0000 0202
B3FF FE1E 8329 FE80 0000 0000 0000 0202
B3FF FE1E 8330 8000 xxxx a088 0000 0001
0203 0405 0607 0809 0a0b 0c0d 0e0f 1011
1213 1415 1617 1819 1a1b 1c1d 1e1f 2021
2223 2425 2627 2829 2a2b 2c2d 2e2f 3031
3233
和它給輸出:
27741 37794
0xe672 # correct?
0b1111111111111111
所以我應該用e672
替換xxxx
。這是對的嗎?當我嘗試用wireshark計算這個時,我得到了一個不同的答案。
夫婦的注意事項:你的回答包的校驗和一個32位的字段(「> I」),但它應該是一個16位字段([來源](https://開頭的連接。 wikipedia.org/wiki/Internet_Control_Message_Protocol_version_6#Packet_format))導致長度爲34個字節的ICMP數據包,而不是您用於校驗和計算的32個數據包。它似乎也假定ICMP數據包的類型是135/NDP,但OP特別提到了128/Echo Request。如果我錯了,請糾正我,我正在嘗試將您的解決方案用於我自己的基於python的ping程序。 – ocket8888