udp, UDP - Internet User Datagram Protocol
s = socket(AF_INET, SOCK_DGRAM, 0);
s = socket(AF_INET6, SOCK_DGRAM, 0);
t = t_open("/dev/udp", O_RDWR);
t = t_open("/dev/udp6", O_RDWR);
UDP is a simple datagram protocol which is layered directly above the Internet Protocol (“IP”) or the Internet Protocol Version 6 (“IPv6”). Programs may access UDP using the socket interface, where it supports the SOCK_DGRAM socket type, or using the Transport Level Interface (“TLI”), where it supports the connectionless (T_CLTS) service type.
Within the socket interface, UDP is normally used with the sendto(), sendmsg(), recvfrom(), and recvmsg() calls (see send(3C) and recv(3C)). If the connect(3C) call is used to fix the destination for future packets, then the recv(3C) or read(2) and send(3C) or write(2) calls may be used.
UDP address formats are identical to those used by the Transmission Control Protocol (“TCP”). Like TCP, UDP uses a port number along with an IP or IPv6 address to identify the endpoint of communication. The UDP port number space is separate from the TCP port number space, that is, a UDP port may not be “connected” to a TCP port. The bind(3C) call can be used to set the local address and port number of a UDP socket. The local IP or IPv6 address may be left unspecified in the bind() call by using the special value INADDR_ANY for IP, or the unspecified address (all zeroes) for IPv6. If the bind() call is not done, a local IP or IPv6 address and port number will be assigned to the endpoint when the first packet is sent. Broadcast packets may be sent, assuming the underlying network supports this, by using a reserved “broadcast address.” This address is network interface dependent.
Note that no two UDP sockets can be bound to the same port unless the bound IP addresses are different. IPv4 INADDR_ANY and IPv6 unspecified addresses compare as equal to any IPv4 or IPv6 address. For example, if a socket is bound to INADDR_ANY or unspecified address and port X, no other socket can bind to port X, regardless of the binding address. This special consideration of INADDR_ANY and unspecified address can be changed using the SO_REUSEADDR socket option. If SO_REUSEADDR is set on a socket doing a bind, IPv4 INADDR_ANY and IPv6 unspecified address do not compare as equal to any IP address. This means that as long as the two sockets are not both bound to INADDR_ANY/unspecified address or the same IP address, the two sockets can be bound to the same port.
If an application does not want to allow another socket using the SO_REUSEADDR option to bind to a port its socket is bound to, the application can set the socket level option SO_EXCLBIND on a socket. The option values of 0 and 1 represent enabling and disabling the option, respectively. Once this option is enabled on a socket, no other socket can be bound to the same port.
IPv6 does not support broadcast addresses; their function is supported by IPv6 multicast addresses.
If this boolean option is set, datagrams sent via this socket will have a non-ESP marker inserted between the UDP header and the data. Likewise, inbound packets that match the endpoint's local-port will be demultiplexed between ESP or the endpoint itself if a non-ESP marker is present. This option is only available on IPv4 sockets (AF_INET), and the application must have sufficient privilege to use PF_KEY sockets to also enable this option.
There are a variety of ways that a UDP packet can be lost or corrupted, including a failure of the underlying communication mechanism. UDP implements a checksum over the data portion of the packet. If the checksum of a received packet is in error, the packet will be dropped with no indication given to the user. A queue of received packets is provided for each UDP socket. This queue has a limited capacity. Arriving datagrams which will not fit within its high-water capacity are silently discarded.
UDP processes Internet Control Message Protocol (“ICMP”) and Internet Control Message Protocol Version 6 (“ICMP6”) error messages received in response to UDP packets it has sent. See icmp(4P) and icmp6(4P).
ICMP “source quench” messages are ignored. ICMP “destination unreachable,” “time exceeded” and “parameter problem” messages disconnect the socket from its peer so that subsequent attempts to send packets using that socket will return an error. UDP will not guarantee that packets are delivered in the order they were sent. As well, duplicate packets may be generated in the communication process.
ICMP6 “destination unreachable” packets are ignored unless the enclosed code indicates that the port is not in use on the target host, in which case, the application is notified. ICMP6 “parameter problem” notifications are similarly passed upstream. All other ICMP6 messages are ignored.
Postel, Jon, RFC 768, User Datagram Protocol, Network Information Center, August 1980. https://tools.ietf.org/html/rfc768.
Huttunen, A., Swander, B., Volpe, V., DiBurro, L., Stenberg, M., RFC 3948, UDP Encapsulation of IPsec ESP Packets, The Internet Society, 2005. https://tools.ietf.org/html/rfc3948.
A socket operation may fail if:
A connect() operation was attempted on a socket on which a connect() operation had already been performed, and the socket could not be successfully disconnected before making the new connection.
A sendto() or sendmsg() operation specifying an address to which the message should be sent was attempted on a socket on which a connect() operation had already been performed.
A send() or write() operation, or a sendto() or sendmsg() operation not specifying an address to which the message should be sent, was attempted on a socket on which a connect() operation had not already been performed.
A bind() operation was attempted on a socket with a network address/port pair that has already been bound to another socket.
A bind() operation was attempted on a socket with a network address for which no network interface exists.
A sendmsg() operation with a non-NULL msg_accrights was attempted.
A bind() operation was attempted with a “reserved” port number and the effective user ID of the process was not the privileged user.
The system ran out of memory for internal data structures.