UDP protocol analysis --1

1 Overview

User Datagram Protocol (UDP, User Datagram Protocol) provides a method for IP datagrams can be sent without establishing a connection for the application package. UDP is a simple datagram-oriented protocol that reservation message boundaries. UDP does not provide error correction, queue management, deduplication, flow control and congestion control, but to provide error detection (real comprising a first end (end-to-end) we have encountered in the transport layer checksum and ). This protocol itself provides minimal functionality, so use it to do a lot of applications and processes on how to send datagrams control. Want to ensure that data is reliable delivery or correct sequencing, these applications must implement their own protection. Generally, each UDP output operation requested application generates only a UDP datagram, so as to send an IP datagram. For the transport layer protocol (e.g. TCP) stream-oriented data, all data written to the real application in a single transmission of data packets in the IP or the receiving party may not contact the contents.

2. UDP main features

1). UDP is connectionless , i.e. without establishing a connection before sending data, thereby reducing the overhead and time delay before data transmission.
2). UDP using best effort , that does not guarantee reliable delivery, so the host does not need to maintain complex connection status table.
3). UDP is a message-oriented is. The sender of the UDP application to pay down the message, add a header in after delivery down the IP layer. UDP application layer packets down the post, neither combination nor split, but retain those packets boundary . Therefore, the application must select the appropriate size of the packet.
4). UDP no congestion control , congestion of the network and therefore does not invalidate the transmission rate of the source host is reduced. Many real-time applications (such as IP telephony, real-time video conferencing, etc.) to go to the source host sends data at a constant rate, and allows lose some data when network congestion occurs, but the data do not allow too many delays. UDP fits this requirement.
5). UDP support one to one, one to many, and many-to-many interactive communication .
6). UDP is a small header overhead , only 8 bytes, shorter than the first 20 bytes of TCP.

Although some real-time applications do not require the use of UDP congestion control, but at the same time a lot of the source host sends live video stream to the high rate of network, network congestion is likely to occur, we are unable to receive normal results. Therefore, do not use UDP congestion control function may cause serious network congestion problems.
There are some real-time applications using UDP, the need for unreliable UDP transport make appropriate improvements to reduce loss of data. In this case, the application process itself can be under the premise does not affect the real-time applications, add some measures to improve reliability, such as the use of forward error correction or pass the message has been lost weight .

3. UDP header format

UDP has two fields: a data field and a header field. Header field is simple, only 8 bytes, the four fields, each field is a length of two bytes. The fields have the following meanings:

  1. Source Port : Source port number. Choice in times of need reply. You do not need to be used when all zeros.
  2. Destination Port : Destination port number. This time must be used to deliver the message at the end.
  3. Length : length of the UDP user datagram, the minimum value is 8 (header only), data transmission with a 0-byte UDP datagram is permitted. Notably, UDP length field is redundant; the IPV4 header contains the total length of the datagram, while IPV6 header contains a payload length. Accordingly, the length of a UDP / IPV4 equal to the total length of the datagram by subtracting the length of the datagram IPV4 IPV4 head. The length of a UDP / IPV6 datagram equals IPV6 header contained in the payload length value (payload length) of all minus extended header field (unless the data packets long) length. In both cases, the length of the UDP length field should be calculated with the information provided from the IP layer is the same.
  4. Checksum : UDP User Datagram detecting for errors in transmission. Wrong discarded.

When the transport layer receives a UDP datagram from the IP layer, according to the destination port, through the application process to the corresponding port.

If the recipient in the packet is UDP destination port number is incorrect (i.e., corresponding to the port number of the application process does not exist), the packet is discarded by the Internet Control Message Protocol ICMP sends a "port unreachable" error packet. Wen to the sender.
Note that, although the communication between the UDP port number to its use, but since the communication of UDP is connectionless, it is not necessary (the communication between the TCP socket must be used between the two sockets establish connection).

3. UDP checksum

UDP checksum is an end of the transport layer checksum is UDP pseudo header IP header contains the source (Source) and the destination IP address (Destination Address) field is calculated. It is obtained from the calculated initial sender, verification by the ultimate destination. It will not be modified (unless it through a NAT) in the transmission. IPV4 header checksum covers only the entire head (i.e., it does not overwrite any data in the IP packets), which should be recalculated (IPV4 will Because the TTL value in the field of forwarding IP datagrams in each hop reduced router). Transport protocols (e.g., TCP, UDP) uses a checksum to cover their heads and data. For UDP, the checksum is optional, and others are mandatory. When used in a UDP IPV6, and calculates a checksum is mandatory, because not the IP layer header checksum. In order to provide error-free data to the application layer protocol like the UDP, the data delivered to the application before receiving side must be calculated using a checksum or other error-monitoring mechanism.

3.1 pseudo-header

In the pseudo UDP header includes source IP address 32, destination IP address 32-bit, 8-bit protocol fill 0,8, 16-bit UDP length. Pseudo-header is not TCP & UDP datagram actual active ingredients. Is a virtual dummy header data structure, wherein the information is extracted from the IP packet header data packet resides in the packet header , neither transmission nor submitted upward downward, but only to calculate the checksum.
Purpose is to let the head of the dummy UDP layer verify that the data has arrived at the correct destination (i.e., the IP address is not received erroneous data packets, did not give the present UDP datagram other transport protocols), calculated UDP checksum and when the coverage field, comprising pseudo header and UDP header and load.

3.2 UDP checksum calculation method

UDP checksum calculation method and the method of calculating the IP datagram header checksum similar. But the difference is: IP datagram header checksum and only test IP datagrams, but the UDP checksum is the test with both header and data portion .
In the sender, first of all zeros into the checksum field. Then the pseudo-header and a UDP user datagram to see pick up by a number of 16-bit string. If the UDP datagram user data portion is not an even number of bytes, will have to fill a zero byte (the last byte of an odd number of bits should be 16 fills the low byte and high byte 0, this byte is not sent ). Then calculated by the binary one and the 16-bit word. After the checksum field and writes this binary one, send UDP user datagram.
On the receiving side, the received UDP User Datagram together, these requirements according to the binary one and a 16-bit word along with the dummy header (and possibly all zero bytes padding). As a result, when no error is 1 to Be . Otherwise, it indicates an error occurs, the receiving side discards the UDP user datagram (can also be handed over to the application layer, but an error has occurred attach warning). If the checksum field value of 0x0000 indicates the sender and calculate parity.

As shown above, the pseudo-header 3 field is all zeros; fourth column is the value of the protocol field in the IP header. For UDP, this field value is 17 this protocol; a fifth field is the length of the UDP user datagram. Therefore, such a test and both checked the source and destination ports, and UDP User Datagram UDP User Datagram data section, but also to check the source IP address and destination address of the IP datagram. Note that the length of the UDP datagram occurs twice in the checksum calculation.

The principle is to calculate the binary one summation, specifically that:

0 + 0 = 0
1 + 0 = 0 + 1 = 1
1 + 1 = 10

If there is a carry highest, put into a position to take down to the lowest bit binary adder to do it again
example:

FIG 3-2-1 binary summing the inverted reference code:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAX_NUM = 65536; // 2^16 = 1 0000 0000 0000 0000
int a[15] = {153*256+19,8*256+104,171*256+3,14*256+11,17,15,1087,13,15,21573,21332,18766,18176};
// 256 = 2^8 = 1 0000 0000
int main()
{
    int sum = 0;
    for(int i=0;i<13;i++){
        sum += a[i];
        if(sum > MAX_NUM){
            sum = sum % MAX_NUM + 1;
        }
    }
    printf("%d\n",sum);
    
    return 0;
}

Note: UDP protocol (transport layer) Direct operating IP (network layer) bits, can lead to so-called "violation of stratification" (layering violation) rules. But only for protocol little impact because, in general, when data is transmitted to (or from) the UDP, the IP layer information is already in the ready. In contrast, we should be concerned about NAT, especially if the UDP datagram is fragmented.

Although the UDP datagram in the original UDP checksum is optional in the specification, they are also now required to be used by default in the host [RFC1122]. In the 1980s, some computer vendors turned off by default UDP checksum function to accelerate the achievement of its Sun Network File System (NFS), the network file system uses the UDP. Because of the protective layer 2 CRC (checksum it stronger than the Internet), and in many cases this may not cause problems, but turned off by default checksum function is considered a bad way (also against RFC-compliant). Early Internet experience shows that when the data reported by the router, the router will always exist with software and hardware vulnerabilities will modify the bit forwarding datagrams. If the end of the UDP checksums are turned off, the UDP datagram error can not be detected. While noting that some of the older data link protocol (e.g., serial line IP or SLIP) without any form of data link checksum, there is not the IP packet is modified to detect the possibility, unless further introduced kind of checksum.
In consideration of such a pseudo-head configuration, you can clearly see that when a UDP / IPv4 datagram through one of the NAT, only the IP layer header checksum is to be modified, and the head of the dummy UDP checksum and it must also be properly modified, because the address of the IP layer and / or the end of the day number UDP layers may change. Therefore, NAT is usually due to simultaneous multi-layer protocol packets Mei repair the breach of hierarchical rules. Of course, given the pseudo-layered head itself is a violation of the rules, NAT is not selected. UDP traffic are specific rules for NAT processing given by [RFC4784].

[references]

  1. "TCP / IP Detailed Volume 1: Second Edition"
  2. "Computer Network" Xie Xiren.
  3. Baidu Encyclopedia pseudo-head
  4. IP, UDP, TCP checksum What is the difference http://www.lianhekj.com/question/328086705.html
  5. Binary one summation https://www.cnblogs.com/jcchan/p/10400504.html
  6. UDP checksums and binary code counter-operation https://blog.csdn.net/weixin_43790264/article/details/92847622

Guess you like

Origin www.cnblogs.com/sxiszero/p/11565108.html