"TCP/IP Detailed Volume 2: implementation" Note--udp: User Datagram Protocol

Source: Internet
Author: User
Tags sendmsg

The User Datagram protocol, or UDP, is a simple transport layer protocol for datagrams: Each output of a process produces only one UDP datagram, which sends

An IP datagram.

The process accesses UDP by creating an sock_dgram type of socket within an Internet domain. This type of socket is referred to as a non-connected by default. Each process sends

Data, you must specify the destination IP address and port number. Each time a datagram is received from the socket, the process can receive the source IP address and port number from the datagram.

A UDP socket can also be connected to a special IP address and port, so that all datagrams written to that socket are sent to that destination, and only from that IP

Address and port Number data are passed to the process.


PROTOSW Structure of 1.UDP

Shows the protocol Exchange entry for UDP



The 2.UDP header

The UDP header is defined as a UDPHDR structure, which is the data structure and diagram of the UDP header.



In code, the UDP header is usually referred to as an IP header immediately following the UDP header. This is how Udp_input handles incoming IP datagrams, and

Udp_output How to construct an outgoing IP datagram, this federated IP/UDP header is a UDPIPHDR structure, as shown in:


The 20-byte IP header is defined as a ipovly structure, as shown in. Unfortunately, this structure is not a true IP header, although the size is the same, but

fields are different.



3.udp_init function

The Domaininit function calls the UDP initialization function (UDP_INIT) during system initialization, and the only work that this function does is to put the head PCB forward and

After the pointer points to it itself. This is a doubly linked list.

Other parts of the UDB PCB are initialized to 0, although the only field used in this head PCB is Inp_lport, which is the next UDP temporary

Port number.


4.udp_output function

UDP output occurs when an application calls any of the five write functions. These five functions are: Send,sendto or sendmsg, write, or

Writev. If the socket is already connected, you can call these five functions arbitrarily, although you cannot specify a destination address with sendto or sendmsg. If the socket

is not yet connected, you can only call SendTo and sendmsg, and you must specify a destination address. Summarizes these five functions, which, when terminated,

Call Udp_output, and the function calls Ip_output again.


Five functions terminate the call to Sosend and pass a pointer to the MSGHDR structure as a parameter to the function. The data to be output is split into a mbuf chain

, Sosend puts an optional destination address and optional control information in the MBUF, and publishes the Pru_send request.

The processing flow of the Udp_output function is as follows:

1. Discard the optional control information. UDP output does not use any control information.

2. Temporarily connect an interface that is not connected. If the caller specifies a destination address for the UDP datagram, the socket is temporarily connected to the address by the In_pcbconnect

and is disconnected at the end of the function.

3. Add the IP/UDP header to the front. M_prepend allocates space in front of the data for the IP and UDP headers.


4.1.UDP inspection and calculation and pseudo header

Before we discuss the latter part of Udp_output, we describe how UDP populates some fields of the IP/UDP header, how to calculate UDP validation, and how to pass

IP/UDP header and data to the IP output, these work cleverly used the ipovly structure.

Shows the Udp_output 28-byte IP/UDP header constructed on the first memory of the Mbuf chain pointed to by M. Fields with no shadows are udp_output

Filled, shaded fields are populated by Ip_output.



The following three facts are used in the calculation of UDP inspection and use: 1. The third 32bit word in the pseudo header (e.g.) looks similar to the third 32bit word in the IP header:

2 8bit values and a 16bit value. 2. The order of three 32bit values in the pseudo-header is irrelevant. In fact, internet inspection and computation does not depend on the use of the

The order of the 16bit values. 3. The addition of a full 0 32bit word to the test and calculation has no effect.


Udp_output uses these three facts to populate the fields of the UDPIPHDR structure as shown in.


In the 20-byte IP header, the last three 32bit words are used as the first part of the inspection and calculation, and the top two 32bit words of the IP header are also used for inspection and calculation, but they

is initialized to 0 and does not affect the final Test and.

Summarizes the actions we describe:


1. The top figure is the agreement definition for the first part.

2. The middle diagram is the UDPIPHDR structure used in the source code, which is used to calculate the UDP test and.

3. The following figure is the IP/UDP header that appears on the line. The 7 fields with the arrows above are the 3 fields of the udp_output that were filled before the test and preceded by an asterisk.

Udp_output is filled after the test and calculation, and the other 6 shaded fields are ip_output filled.


The following is the second part of the Udp_output function.

1. Prepare the pseudo-header for inspection and calculation.

2. Calculation of inspection and

3. Populate the UDP length, TTL, and TOS.

4. Send the datagram. Call Ip_output to send datagrams.

5. Disconnect the socket from the temporary connection.


The 5.udp_input function receives an IP datagram on the input queue that the IP specifies as UDP in its Protocol field, and the input of UDP occurs. IP calls udp_input through the Pr_input function in the Protocol exchange table. Because the IP input is at the software interrupt level, Udp_input is also executed at this level. The goal of the udp_input is to place the UDP datagram in the appropriate socket cache, waking up all processes that are blocked by the input on that socket. The discussion of the Udp_input function is divided into three parts: 1.UDP completes the general confirmation of the received datagram. 2. Processing a UDP datagram where the destination is a unicast address: Locate the appropriate PCB and place the datagram in the socket cache. 3. Processing a UDP datagram where the destination is a broadcast or multicast address: The datagram must be submitted to multiple sockets.
5.1. General acknowledgement of the received UDP datagram

1. Discard IP options.

2. Verify the UDP length. The two lengths associated with UDP datagrams are: the Length field (ip_len) of the IP header and the length field of the UDP header (Uh_ulen).

Comparing these two lengths, there may be three possibilities:

1) Ip_len equals Uh_ulen. This is usually the case.

2) Ip_len greater than Uh_ulen. The IP header is too large, as shown in. The code believes in two small-length bits, and removes the extra bytes of data from the last datagram,

Truncates the data from the last Mbuf chain.


3) Ip_len less than Uh_ulen. When the length of the UDP header is given, the IP datagram is smaller than possible, as shown in. This indicates that the datagram has errors and must be discarded,

There's no other option.


3. Save the IP header backup, verify the UDP check and.


5.2. Split unicast datagrams

Assume that the destination address of the datagram is a unicast address.

1. Check the "back one" cache. UDP maintains a pointer to the Internet PCB on which the datagram was last received, and before viewing the PCB, it may be necessary to

Search the PCB on the UDP table to compare the external and local addresses of the last received PCB with the port number and the received datagram. This is referred to as the "one-back" cache.

It is based on the assumption that the received datagram is most likely to be sent to the same port as the previous datagram.

2. Search all of the UDP PCB.

3. Generating ICMP Port unreachable error. If a matching pcb,udp is not found, it usually produces an ICMP Port unreachable error.

4. Return the source station IP address and the source station port.

5. Handle the IP_RECVDSTADDR socket option. This option returns the destination IP address in the received UDP datagram as control information.

6. Add the data to the receive queue of the socket.


5.3. Split multicast and broadcast datagrams

These datagrams are presented to all matching sockets, not just a socket.


6.udp_saveopt function

If the process specifies the IP_RECVDSTADDR socket option, Udp_input calls Udp_saveopt to receive the destination IP address from the received datagram.


7.udp_ctlinput function

When Icmp_input receives an ICMP error (destination host unreachable, parameter problem, redirect, source station suppression, and timeout), the pr_ctlinput of the corresponding protocol is invoked

Function. For UDP, call Udp_cltinput. Let's take a look at the processing of ICMP received:

The 1.icmp_input converts the ICMP type and code to prc_xxxx error code.

2. Pass the PRC_XXXX error code to the control input function of the protocol.

The 3.internet PCB protocol (TCP and UDP) maps a prc_xxx error code to a UNIX errno value, which is returned to the process.


8.udp_usrreq function

Many operations call the Protocol's user request function, and one of the five write functions is invoked on a UDP socket to call UDP on request Pru_send

End of the user request function. The individual requests in this function are discussed below.

1.pru_attach request, from the socket system call, assign a new PCB, add it to the front of the UDP PCB table, the socket structure and PCB connected together.

The 2.close system call publishes the Pru_detach request. Remove the PCB from the UDP table and release the PCB.

The 3.pru_bind request completes the bind operation.

4. If there is a pru_listen request, it is invalid and only used by a connection-oriented protocol.

5. In a UDP application, the client or server can call connect, which sends a connect request to modify the external IP address and port number that the socket sends or receives.

The 6.socketpair system call publishes the PRU_CONNECT2 request.

8. For UDP sockets, there are two scenarios in which Pru_disconnect requests are generated:

A. When a UDP socket on a connection is closed, Pru_disconnect is called before Pru_detach.

10. Call cannot write function, publish Pru_send request, final call Udp_output send the datagram.

12.PRU_SOCKADDR and PRU_PEERADDR request allocations from system calls GetSockName and Getpeername.


9.udp_sysctl function

The SYSCTL function of UDP only supports one option, UDP check and flag bit. System administrators can disable the use of SYSCTL programs to enable or QUIESCE UDP inspection and.

"TCP/IP Detailed Volume 2: implementation" Note--udp: User Datagram Protocol

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.