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