written 8.4 years ago by |
Checksum :
i. Here the checksum includes three sections: a pseudo header, the UDP header, and the data coming from the application layer.
ii. The pseudo header is the part of the header of the IP packet in which the user datagram is to be encapsulated with some fields filled with 0s (see Figure1).
iii. If the checksum does not include the pseudo header, a user datagram may arrive safe and sound. However, if the IP header is corrupted, it may be delivered to the wrong host.
iv. The protocol field is added to ensure that the packet belongs to UDP, and not to TCP.
v. The value of the protocol field for UDP is 17. If this value is changed during transmission, the checksum calculation at the receiver will detect it and UDP drops the packet. It is not delivered to the wrong protocol.
Checksum Calculation:
UDP Checksum calculation is similar to TCP Checksum computation. It’s also a 16-bit field of one’s complement of one’s complement sum of a pseudo UDP header + UDP datagram.
Sender side:
It treats segment contents as sequence of 16-bit integers.
All segments are added. Let's call it sum.
Checksum: 1's complement of sum.(In 1's complement all 0s are converted into 1s and all 1s are converted into 0s).
Sender puts this checksum value in UDP checksum field.
- Receiver side:
Calculate checksum
All segments are added and then sum is added with sender's checksum.
Check that any 0 bit is presented in checksum. If receiver side checksum contains any 0 then error is detected. So the packet is discarded by receiver.
As an example, suppose that we have the bit stream 0110011001100110 $ \ \ \ $ 0110011001100110 $ \ \ \ \ $ 0000111100001111:
This bit stream is divided into segments of 16-bits integers.
So, it looks like this:
0110011001100110 (16-bit integer segment)
0101010101010101
0000111100001111
The sum of first of these 16-bit words is:
0110011001100110
0101010101010101
$------------------------------$
1011101110111011
Adding the third word to the above sum gives
1011101110111011
0000111100001111
$-----------------------------$
1100101011001010 (sum of all segments)
Now to calculate checksum 1's complement of sum is taken. As I mentioned earlier, 1's complement is achieved by converting all 1s into 0s and all 0s into 1s. So, the checksum at sender side is: 0011010100110101.
Now at the receiver side, again all segments are added .and sum is added with sender's checksum.
If no error than check of receiver would be: 1111111111111111.
If any 0 bit is presented in the header than there is an error in checksum. So, the packet is discarded.
Although UDP provides error checking, it does not do anything to recover from an error. Some implementations of UDP simply discard the damaged segment; others pass the damaged segment to the application with a warning.