1UDP(7) Linux Programmer's Manual UDP(7)
2
3
4
6 udp - User Datagram Protocol for IPv4
7
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/udp.h>
12
13 udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
14
16 This is an implementation of the User Datagram Protocol described in
17 RFC 768. It implements a connectionless, unreliable datagram packet
18 service. Packets may be reordered or duplicated before they arrive.
19 UDP generates and checks checksums to catch transmission errors.
20
21 When a UDP socket is created, its local and remote addresses are un‐
22 specified. Datagrams can be sent immediately using sendto(2) or
23 sendmsg(2) with a valid destination address as an argument. When con‐
24 nect(2) is called on the socket, the default destination address is set
25 and datagrams can now be sent using send(2) or write(2) without speci‐
26 fying a destination address. It is still possible to send to other
27 destinations by passing an address to sendto(2) or sendmsg(2). In or‐
28 der to receive packets, the socket can be bound to a local address
29 first by using bind(2). Otherwise, the socket layer will automatically
30 assign a free local port out of the range defined by
31 /proc/sys/net/ipv4/ip_local_port_range and bind the socket to IN‐
32 ADDR_ANY.
33
34 All receive operations return only one packet. When the packet is
35 smaller than the passed buffer, only that much data is returned; when
36 it is bigger, the packet is truncated and the MSG_TRUNC flag is set.
37 MSG_WAITALL is not supported.
38
39 IP options may be sent or received using the socket options described
40 in ip(7). They are processed by the kernel only when the appropriate
41 /proc parameter is enabled (but still passed to the user even when it
42 is turned off). See ip(7).
43
44 When the MSG_DONTROUTE flag is set on sending, the destination address
45 must refer to a local interface address and the packet is sent only to
46 that interface.
47
48 By default, Linux UDP does path MTU (Maximum Transmission Unit) discov‐
49 ery. This means the kernel will keep track of the MTU to a specific
50 target IP address and return EMSGSIZE when a UDP packet write exceeds
51 it. When this happens, the application should decrease the packet
52 size. Path MTU discovery can be also turned off using the IP_MTU_DIS‐
53 COVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see
54 ip(7) for details. When turned off, UDP will fragment outgoing UDP
55 packets that exceed the interface MTU. However, disabling it is not
56 recommended for performance and reliability reasons.
57
58 Address format
59 UDP uses the IPv4 sockaddr_in address format described in ip(7).
60
61 Error handling
62 All fatal errors will be passed to the user as an error return even
63 when the socket is not connected. This includes asynchronous errors
64 received from the network. You may get an error for an earlier packet
65 that was sent on the same socket. This behavior differs from many
66 other BSD socket implementations which don't pass any errors unless the
67 socket is connected. Linux's behavior is mandated by RFC 1122.
68
69 For compatibility with legacy code, in Linux 2.0 and 2.2 it was possi‐
70 ble to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors
71 only when the socket has been connected (except for EPROTO and EMSG‐
72 SIZE). Locally generated errors are always passed. Support for this
73 socket option was removed in later kernels; see socket(7) for further
74 information.
75
76 When the IP_RECVERR option is enabled, all errors are stored in the
77 socket error queue, and can be received by recvmsg(2) with the MSG_ER‐
78 RQUEUE flag set.
79
80 /proc interfaces
81 System-wide UDP parameter settings can be accessed by files in the di‐
82 rectory /proc/sys/net/ipv4/.
83
84 udp_mem (since Linux 2.6.25)
85 This is a vector of three integers governing the number of pages
86 allowed for queueing by all UDP sockets.
87
88 min Below this number of pages, UDP is not bothered about its
89 memory appetite. When the amount of memory allocated by
90 UDP exceeds this number, UDP starts to moderate memory
91 usage.
92
93 pressure
94 This value was introduced to follow the format of tcp_mem
95 (see tcp(7)).
96
97 max Number of pages allowed for queueing by all UDP sockets.
98
99 Defaults values for these three items are calculated at boot
100 time from the amount of available memory.
101
102 udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
103 Minimal size, in bytes, of receive buffers used by UDP sockets
104 in moderation. Each UDP socket is able to use the size for re‐
105 ceiving data, even if total pages of UDP sockets exceed udp_mem
106 pressure.
107
108 udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
109 Minimal size, in bytes, of send buffer used by UDP sockets in
110 moderation. Each UDP socket is able to use the size for sending
111 data, even if total pages of UDP sockets exceed udp_mem pres‐
112 sure.
113
114 Socket options
115 To set or get a UDP socket option, call getsockopt(2) to read or set‐
116 sockopt(2) to write the option with the option level argument set to
117 IPPROTO_UDP. Unless otherwise noted, optval is a pointer to an int.
118
119 Following is a list of UDP-specific socket options. For details of
120 some other socket options that are also applicable for UDP sockets, see
121 socket(7).
122
123 UDP_CORK (since Linux 2.5.44)
124 If this option is enabled, then all data output on this socket
125 is accumulated into a single datagram that is transmitted when
126 the option is disabled. This option should not be used in code
127 intended to be portable.
128
129 Ioctls
130 These ioctls can be accessed using ioctl(2). The correct syntax is:
131
132 int value;
133 error = ioctl(udp_socket, ioctl_type, &value);
134
135 FIONREAD (SIOCINQ)
136 Gets a pointer to an integer as argument. Returns the size of
137 the next pending datagram in the integer in bytes, or 0 when no
138 datagram is pending. Warning: Using FIONREAD, it is impossible
139 to distinguish the case where no datagram is pending from the
140 case where the next pending datagram contains zero bytes of
141 data. It is safer to use select(2), poll(2), or epoll(7) to
142 distinguish these cases.
143
144 TIOCOUTQ (SIOCOUTQ)
145 Returns the number of data bytes in the local send queue. Sup‐
146 ported only with Linux 2.4 and above.
147
148 In addition, all ioctls documented in ip(7) and socket(7) are sup‐
149 ported.
150
152 All errors documented for socket(7) or ip(7) may be returned by a send
153 or receive on a UDP socket.
154
155 ECONNREFUSED
156 No receiver was associated with the destination address. This
157 might be caused by a previous packet sent over the socket.
158
160 IP_RECVERR is a new feature in Linux 2.2.
161
163 ip(7), raw(7), socket(7), udplite(7)
164
165 The kernel source file Documentation/networking/ip-sysctl.txt.
166
167 RFC 768 for the User Datagram Protocol.
168 RFC 1122 for the host requirements.
169 RFC 1191 for a description of path MTU discovery.
170
172 This page is part of release 5.13 of the Linux man-pages project. A
173 description of the project, information about reporting bugs, and the
174 latest version of this page, can be found at
175 https://www.kernel.org/doc/man-pages/.
176
177
178
179Linux 2021-03-22 UDP(7)