Network Packet Efficiency and ChemSum

zhaozj2021-02-16  51

Packets transmitted in the network In order to ensure that the transfer is correct, IP, IP, ARP, TCP, etc.

Both data segments have their own papers. The calculation of the test is not complicated. See all the data of the corresponding data package into one byte array {A, B, C, D, E}, divide them into 16bit group {[AB], [CD], [E0]} to calculate it: Ab] [CD] [E0] -------- [XY] Calculates the low-speed carry, the highest position, if the calculated [xy] all bit is 1 (ie 1111 1111 The papers are passed.

If [CD] is the value, the other bytes have been filled out, how to calculate [CD]

(checksum)? We first use 0 to fill [CD], then calculate [xy], the correct [CD] should be a plan

Calculated [XY] bitmap. This will ensure that the entire data package segment passes the test.

In the actual program, because the current machines are 32-bit, the algorithm is used: byte-by-byte "normal" swapped Order ORDER

Byte 0/1: 00 01 0001 0100 Byte 2/3: F2 03 F203 03F2 BYTE 4/5: F4 F5 F4F5 F5F4 BYTE 6/7: F6 F7 F6F7 F7F6 --- --- ----- --- - SUM1: 2DC 1F0 2DDF0 1F2DC

DC F0 DDF0 F2DC Carrys: 1 2 2 1 - - ---- ---- Sum2: DD F2 DDF2 F2DD

Final SWAP: DD F2 DDF2 DDF2 ------------------------------ Byte 0/1/2/3: 0001f203 010003f2 03f20100 Byte 4/5/6/7: F4f5f6f7 f5f4f7f6 f7f6f5f4 -------- -------- -------- Sum1: 0f4f7e8fa 0f6f4fbe8 0fbe8f6f4

Carries: 0 0 0

TOP HALF: F4F7 F6F4 FBE8 BOTTOM HALF: E8FA FBE8 F6F4 ----- --------- Sum2: 1DDF1 1F2DC 1F2DCDDF1 F2DC F2DC Carrys: 1 1 1 ---- ---- --- - Sum3: DDF2 F2DD F2DD

Final SWAP: DDF2 DDF2 DDF2

You can see the 122-bit Checksum, then add high 8 digits to the low eight digits, and finally

Checksum is the same. Algorithm for a C implementation: IN 6 {/ * compute internet checksum for "count" bytes * beginning at location "addr". * / Register long sum = 0;

While (count> 1) {/ * this is the inner loop * / sum = * (unsigned short) addr ; count - = 2;

/ * Add left-overte, if any * / if (count> 0) SUM = * (unsigned char *) addr;

/ * FOLD 32-bit sum to 16 bits * / while (sum >> 16) SUM = (SUM & 0xFFFF) (SUM >> 16);

Checksum = ~ SUM;}

Reference: RFC1071 Website: http://www.faqs.org/rfcs/rfc1071.html

转载请注明原文地址:https://www.9cbs.com/read-15523.html

New Post(0)