IP address verification and Algorithm Analysis and Implementation

Source: Internet
Author: User

In order to calculate the IP address check of a datagram, the test field is set to 0. Then
The sum of the line binary anticode (the whole header is regarded as composed of a string of 16 bits), and the result is included in the test and field.
When an IP datagram is received, each 16bit in the same header is used to calculate the inverse code sum. Because the receiver is in the computing process
Contains the verification in the sender's header, because if the header has no errors during transmission
The result calculated by the receiver is full 1. If the result is not 1 (that is, check and error), the number of received IP addresses is discarded.
It is reported.

This is the original article in TCP/IP, which is hard to understand at the beginning. Now let's talk about my understanding:
Theorem 1: If the source code + anticode = value1, then the binary value of value1 is 1
Theorem 2: Get inverse = get inverse and add after addition

Before sending the message, check and set it to 0, and then regard the header as a 16-bit word, and add the words to the end respectively.
If value1 is set to medium. After sending and receiving, the header is regarded as a 16-bit word, and these words are added separately (Packet
Including inspection and)

Order: the value added by Division test header is value2
The value of the test is value1 (the reverse code of value2 is directly sent), so value1 + value2 = value3,
The value3 binary values are all 1.

AlgorithmImplementation:
Sender: Check and value should be returned
Recipient: 0 should be returned (indicating that there is no error in the sending process)
/* Note: The IP header check and domain only have 16 bits */
Ushort checksum (ushort * buffer, int size)
{
Unsigned long cksum = 0;

while (size> 1)
{< br> cksum + = * buffer ++;
size-= sizeof (ushort );
}< br> If (size)
{< br> cksum + = * (uchar *) buffer;
}< br> cksum = (cksum> 16) + (cksum & 0 xFFFF);
cksum + = (cksum> 16);

Return (ushort )(~ Cksum );
}

 

1011 1001 0000
1011 1001 1010 --> 1011 + 1001 + 1010 = 1 1110 --> (add the high and low bits) 1111
(1 0100 + 1010 = 1 1110 --> 1111 <=> 1 0100 --> 0101 + 1010 = 1111)

References: http://blog.csdn.net/kubete/archive/2007/10/30/1856374.aspx

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.