Use Python to compute IP, TCP, UDP checksum

Source: Internet
Author: User

Tags: result end-to-end summation for analysis source address own span append

1.1 Calculation of IP checksum

IP check is for the IP header, that is, only the IP header, and for the IP data part of the verification, the corresponding four times the agreement to ensure that the IP head of the test and the field is 16bit.

The calculation principle is as follows:

1. Set the checksum field to 0

2. Calculate the sum of all 16bit words in the IP header

3. Reverse the obtained and bitwise in 2 and get the checksum.

Calculation principle of 1.2 TCP checksum

For the IP layer protocol, its checksum as long as the IP header can be computed, the relative, for the four layer protocol, its checksum needs to calculate four layers of head and four layer of data.

The TCP checksum needs to divide the IP pseudo header, TCP header, TCP data into 16-bit words, and then accumulate (if the total length is an odd number of bytes, then add a bit to 0 bytes at the end), and finally the cumulative and bitwise reverse.

The IP pseudo header includes the source IP address (4 bytes), the destination IP address (4 bytes), the protocol number (two bytes), the TCP Kanenaga (2 bytes), and a total of 12 bytes.

Calculation principle of 1.3 UDP checksum

The UDP checksum is basically consistent with the TCP checksum.

UDP checksum needs to divide the IP pseudo header, UDP header, UDP data into 16-bit words, and then accumulate (if the total length is an odd number of bytes, then add a bit to 0 bytes at the end), and finally the cumulative and bitwise reverse.

The IP pseudo header includes the source IP address (4 bytes), the destination IP address (4 bytes), the protocol number (two bytes), the TCP Kanenaga (2 bytes), and a total of 12 bytes.

The above is the IP, TCP, UDP checksum calculation principle, if we write the corresponding checksum function, the problem is not too large, as long as the basis of its calculation principle can be.

UDP checksum needs to calculate the UDP header plus data load, but also need to add a UDP pseudo-header.
This pseudo-header refers to the source address, destination address, UDP data length, protocol type (0X11), the protocol type is one byte, but needs to complement one byte of 0x0, constituting 12 bytes.
The pseudo-header +UDP header + data calculates the checksum together.

The UDP test and calculation methods are:
1. A 32-digit number according to the sum of each 16 bits;
2. If this 32-bit number, the high 16-bit is not 0, then the high 16-bit plus-low 16-bit is then given a 32-bit number;
3. Repeat the 2nd step until the high 16 bits are 0, the lower 16 bits are reversed, and the checksum is obtained.

The

upd test and calculation is similar to the IP header check and, the UDP test and is an end-to-end test and is computed by the sender and then validated by the receiving end. The UDP datagram is discarded if the receiving end detects a test and has errors.  
Under the example of a UDP packet received by this computer, I analyze the UDP test and the calculation.  
0000:00  E0 4c A9 e0 0f 7d 1e ba (00 );
-xx-XX B-C0 a8 
0020:12  5c 1f 0f A0 34 + 2a 0f  2e 
0030:eb 3f  C4 B6 CB A3 B4 one A8 d3 
0040:93 D2 CF AC 1a B7 98 D4 AE, ed 24 
0050:BF C8 6f db 9a fa D8 C6 2a ca a2 BD 8b 
0060:96 03 
E0 4c A9-XX e0 0f 7d 1e BA----Ethernet frame Header (14 bytes)  
(20 bytes)  
1f 5c 0f A0 00 40 71, 6c EA DB, C0 A8,---- 2a----UDP Header (8 bytes), where 1f 40 represents the 16-bit source port number 8000;0f A0 represents the 16-bit destination port number 4000;00 40 indicates that the upd length is 64 bytes, and that 2a represents the UDP test and (this is the sender It will be used later in the calculation).  
0f ............ -----The remainder of the section is the UDP data section.  
Let's take a look at the tests together.  

The UDP datagram and TCP segment are set to a 12-byte long pseudo header in order to calculate the test. Because the length of a UDP datagram can be an odd number of sections, the test and algorithm add a number of three bits, so you can fill in the Padding byte field with 0, and the padding bytes that may increase are not transmitted.

Therefore, a pseudo-header of 12 bytes must be constructed: DB C0 A8 5c 00 11 00 40
After the construction is completed, the UDP test and the partial 0, and then the UDP per each of the binary inverse code summation, db85+2825+c0a8+125c+0011+0040 + 1f40+0fa0+0040+0000 +......+258b+9603=e8ec7
(0xe8ec7 >> +) + (0xe8ec7 & 0xFFFF) =0x8ed5
Finally, 0X8ED5+0X712A=0XFFFF
Thus, it can be judged that the above UDP datagram has no error in the transmission process.

To convert a hexadecimal string to a decimal number:
Python code
>>> print int (' FF ', 16)
255

1 #The original IP packet is split into 10 binary list by byte2 Ip_content = [69, 0, 1, 35, 127, 30, 0, 0, 64, 17, 213, 133, 10, 8, 136, 23, 10, 8, 136, 255, 214, 131, 214, 131, 1, 15, 95, 98, 0, 115, 104, 121, 121, 121, 102, 45, 103, 117, 116, 105, 110, 103, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 32, 95, 39, 2, 0, 0, 0, 0, 48, 180, 159, 6, 0, 0, 0, 0, 51, 39, 0, 0, 0, 0, 0, 0, 16, 95, 39, 2, 0, 0, 0, 0, 192,  4, 110, 5, 0, 0, 0, 0, 124, 106, 122, 112, 0, 0, 0, 0, 152, 163, 218, 111, 0, 0, 0, 0, 89, 184, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 151, 218, 4, 0, 0, 0, 0, 164, 180, 159, 6, 0, 0, 0, 0, 192, 180, 159, 6, 0, 0, 0, 0, 168, 217, 122 , 123, 97, 99, 54, 53, 100, 102, 100, 98, 45, 54, 50, 55, 52, 45, 52, 101, 101, 52, 45, 98, 99, 100, 100, 45, 52, 53, 98, 54, 97, 98, 99, 99, 55, 49, 54, 57, 125, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 160, 180, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 165, 138, 243 ]3 4 #The original UDP message part is made up of 10 binary list5 [214, 131, 214, 131, 1, 15, 95, 98, 0, 115, 104, 121, 121, 121, 102, 45, 103, 117, 116, 105, 110, 103, 116, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 95, 39, 2, 0, 0, 0, 0, 48, 180, 159, 6, 0, 0, 0, 0, 51, 39, 0, 0, 0, 0, 0, 0, 16 , 95, 39, 2, 0, 0, 0, 0, 192, 4, 110, 5, 0, 0, 0, 0, 124, 106, 122, 112, 0, 0, 0, 0, 152, 163, 218, 111, 0, 0, 0, 0, 89, 1  84, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 151, 218, 4, 0, 0, 0, 0, 164, 180, 159, 6, 0, 0, 0, 0, 192, 180, 159, 6, 0, 0, 0, 0, 168, 217, 122, 123, 97, 99, 54, 53, 100, 102, 100, 98, 45, 54, 50, 55, 52, 45, 52, 101, 101, 52, 45, 98, 9 9, 100, 100, 45, 52, 53, 98, 54, 97, 98, 99, 99, 55, 49, 54, 57, 125, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 160 0, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 165, 138]6 7 #need to assemble the compute UDP checksum section8Udp_content = []9     Ten #IP Source Address 13, 14, 15, 16 bytes of IP packets OneUdp_content.append (ip_content[12]) AUdp_content.append (ip_content[13]) -Udp_content.append (ip_content[14]) -Udp_content.append (ip_content[15]) the  - #IP Destination Address is 17, 18, 19, 20 bytes of IP packets -Udp_content.append (ip_content[16]) -Udp_content.append (ip_content[17]) +Udp_content.append (ip_content[18]) -Udp_content.append (ip_content[19]) +  A #UDP data length is the 5th, 6 bytes in a UDP message atUdp_content.append (IP_CONTENT[IHL + 4]) -Udp_content.append (IP_CONTENT[IHL + 5]) -  - #the protocol type is the 10th byte of the IP message -Udp_content.append (0x00) -Udp_content.append (ip_content[9]) in  -  forIinchRange (Udp_len): toUdp_content.append (IP_CONTENT[IHL +i]) +  -UDP_CONTENT[18] = 0#set the original checksum to 0 theUDP_CONTENT[19] =0 *  $ ifUdp_len% 2 = = 1:#the entire message length is odd and needs to be added 0Panax NotoginsengUdp_content.append (0x00) -  the Print('the UDP checksum content to be computed is:'+str (udp_content)) +Manual_check =calc_checksum (udp_content) A Print(Manual_check) the  + #after assembling the result is, according to the 10 binary display - Udp_content = [10, 8, 136, 23, 10, 8, 136, 255, 1, 15, 0, 17, 214,131, 214, 131, 1, 15, 0, 0, 0, 115, 104, 121, 121, 121, 102, 45, 103, 117, 116,105, 110, 103, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 95, 39, 2, 0, 0, 0, 0, 48, 180, 159, 6, 0, 0, 0, 0, 51, 39, 0, 0, 0, 0, 0, 0, 16, 95, 39, 2, 0, 0, 0, 0, 192, 4, 110, 5, 0, 0, 0, 0 2, 112, 0, 0, 0, 0,152, 163, 218, 111, 0, 0, 0, 0, 89, 184, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 151, 218, 4, 0, 0, 0, 0, 164, 180, 159, 6, 0, 0, 0, 0, 192, 180, 159, 6, 0, 0, 0, 0, 168, 217, 122, 123, 97, 99, 54, 53, 100, 102, 100, 98, 45, 54, 50, 55, 52, 45, 52, 101, 101, 52, 45, 98, 99, 100, 100, 45, 52, 53, 98, 54, 97, 98, 99, 99, 55, 49, 54, 57, 12  5, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 160, 180, 159, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 165, 138, 243 , 0] $  $ defcalc_checksum (sum_data): -Join_sum_data = [] -      forIinchRange (0, Len (sum_data), 2):#first, you need to combine the front and back two numbers into a 16-bit length 16 binary number theFirst_part = str (hex (sum_data[i])) [2:]#10 binary converted to 16 binary, 8 bits in length -         ifLen (First_part) < 2:#If you convert to 16, only 1 bits require a high-fill 0 operationWuyiFirst_part ='0'+First_part the          -Second_part = str (Hex (sum_data[i + 1])) [2:]#10 binary converted to 16 binary, 8 bits in length Wu         ifLen (Second_part) < 2:#If you convert to 16, only 1 bits require a high-fill 0 operation -Second_part ='0'+Second_part About          $Total_part = First_part + Second_part#combined into 16-bit lengths -          -Join_sum_data.append (int (Total_part, 16))#re-convert 16 binary to 10 binary -         #join_sum_data.append (Total_part) A      +     #print (join_sum_data) the      -Sum_result =0 $      forSingle_valueinchJoin_sum_data: theSum_result = Sum_result + single_value#calculate the count of and the          theHex_sum_result = str (hex (Sum_result)) [2:]#Convert to 4-byte, 32-bit hexadecimal format the      -Len_hex_sum = Len (hex_sum_result)#number of bytes obtained in      the     ifLen_hex_sum > 4:#The result of summing is greater than 2 bytes 16 bits, divided into two 2 bytes 16 digits theFirst_part = Int (hex_sum_result[:len_hex_sum-4], 16)#splits the hexadecimal number of the first to second byte into 10 binary About          theSecond_part = Int (hex_sum_result[len_hex_sum-4:], 16)#splits the hexadecimal number of the third to fourth byte into 10 binary the          theLast_check_sum = str (Hex (0xFFFF-(First_part + Second_part))) [2:]#the sum of hexadecimal numbers of two bytes is reversed +         returnLast_check_sum -     Else: theLast_check_sum = str (hex (65535-sum_result)) [2:]#only the hexadecimal number of two bytes is reversed.Bayi         returnLast_check_sum

Use Python to compute IP, TCP, UDP checksum

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: