1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" $Id: udp.7,v 1.7 2000/01/22 01:55:05 freitag Exp $
12 .TH UDP 7 2010-06-13 "Linux" "Linux Programmer's Manual"
14 udp \- User Datagram Protocol for IPv4
16 .B #include <sys/socket.h>
18 .B #include <netinet/in.h>
20 .B udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
22 This is an implementation of the User Datagram Protocol
23 described in RFC\ 768.
24 It implements a connectionless, unreliable datagram packet service.
25 Packets may be reordered or duplicated before they arrive.
26 UDP generates and checks checksums to catch transmission errors.
28 When a UDP socket is created,
29 its local and remote addresses are unspecified.
30 Datagrams can be sent immediately using
34 with a valid destination address as an argument.
37 is called on the socket, the default destination address is set and
38 datagrams can now be sent using
42 without specifying a destination address.
43 It is still possible to send to other destinations by passing an
48 In order to receive packets, the socket can be bound to a local
49 address first by using
51 Otherwise the socket layer will automatically assign
52 a free local port out of the range defined by
53 .I /proc/sys/net/ipv4/ip_local_port_range
54 and bind the socket to
57 All receive operations return only one packet.
58 When the packet is smaller than the passed buffer, only that much
59 data is returned; when it is bigger, the packet is truncated and the
65 IP options may be sent or received using the socket options described in
67 They are processed by the kernel only when the appropriate
70 is enabled (but still passed to the user even when it is turned off).
76 flag is set on sending, the destination address must refer to a local
77 interface address and the packet is sent only to that interface.
79 By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery.
81 will keep track of the MTU to a specific target IP address and return
83 when a UDP packet write exceeds it.
84 When this happens, the application should decrease the packet size.
85 Path MTU discovery can be also turned off using the
88 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
92 When turned off, UDP will fragment outgoing UDP packets
93 that exceed the interface MTU.
94 However, disabling it is not recommended
95 for performance and reliability reasons.
99 address format described in
102 All fatal errors will be passed to the user as an error return even
103 when the socket is not connected.
104 This includes asynchronous errors
105 received from the network.
106 You may get an error for an earlier packet
107 that was sent on the same socket.
108 This behavior differs from many other BSD socket implementations
109 which don't pass any errors unless the socket is connected.
110 Linux's behavior is mandated by
113 For compatibility with legacy code, in Linux 2.0 and 2.2
114 it was possible to set the
117 option to receive remote errors only when the socket has been
118 connected (except for
122 Locally generated errors are always passed.
123 Support for this socket option was removed in later kernels; see
125 for further information.
129 option is enabled, all errors are stored in the socket error queue,
130 and can be received by
136 System-wide UDP parameter settings can be accessed by files in the directory
137 .IR /proc/sys/net/ipv4/ .
139 .IR udp_mem " (since Linux 2.6.25)"
140 This is a vector of three integers governing the number
141 of pages allowed for queueing by all UDP sockets.
145 Below this number of pages, UDP is not bothered about its
147 When the amount of memory allocated by UDP exceeds
148 this number, UDP starts to moderate memory usage.
151 This value was introduced to follow the format of
157 Number of pages allowed for queueing by all UDP sockets.
160 Defaults values for these three items are
161 calculated at boot time from the amount of available memory.
163 .IR udp_rmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
164 Minimal size, in bytes, of receive buffers used by UDP sockets in moderation.
165 Each UDP socket is able to use the size for receiving data,
166 even if total pages of UDP sockets exceed
170 .IR udp_wmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
171 Minimal size, in bytes, of send buffer used by UDP sockets in moderation.
172 Each UDP socket is able to use the size for sending data,
173 even if total pages of UDP sockets exceed
177 To set or get a UDP socket option, call
181 to write the option with the option level argument set to
184 .BR UDP_CORK " (since Linux 2.5.44)"
185 If this option is enabled, then all data output on this socket
186 is accumulated into a single datagram that is transmitted when
187 the option is disabled.
188 This option should not be used in code intended to be
190 .\" FIXME document UDP_ENCAP (new in kernel 2.5.67)
191 .\" From include/linux/udp.h:
192 .\" /* UDP encapsulation types */
193 .\" #define UDP_ENCAP_ESPINUDP_NON_IKE 1 /* draft-ietf-ipsec-nat-t-ike-00/01 */
194 .\" #define UDP_ENCAP_ESPINUDP 2 /* draft-ietf-ipsec-udp-encaps-06 */
195 .\" #define UDP_ENCAP_L2TPINUDP 3 /* rfc2661 */
197 These ioctls can be accessed using
199 The correct syntax is:
204 .IB error " = ioctl(" udp_socket ", " ioctl_type ", &" value ");"
208 .BR FIONREAD " (" SIOCINQ )
209 Gets a pointer to an integer as argument.
210 Returns the size of the next pending datagram in the integer in bytes,
211 or 0 when no datagram is pending.
215 it is impossible to distinguish the case where no datagram is pending
216 from the case where the next pending datagram contains zero bytes of data.
222 to distinguish these cases.
223 .\" See http://www.securiteam.com/unixfocus/5KP0I15IKO.html
224 .\" "GNUnet DoS (UDP Socket Unreachable)", 14 May 2006
226 .BR TIOCOUTQ " (" SIOCOUTQ )
227 Returns the number of data bytes in the local send queue.
228 Only supported with Linux 2.4 and above.
230 In addition all ioctls documented in
236 All errors documented for
240 may be returned by a send or receive on a UDP socket.
243 No receiver was associated with the destination address.
244 This might be caused by a previous packet sent over the socket.
247 is a new feature in Linux 2.2.
249 .\" This man page was written by Andi Kleen.
256 RFC\ 768 for the User Datagram Protocol.
258 RFC\ 1122 for the host requirements.
260 RFC\ 1191 for a description of path MTU discovery.