429 lines
11 KiB
HTML
429 lines
11 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of UDP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>UDP</H1>
|
|
Section: Linux Programmer's Manual (7)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
udp - User Datagram Protocol for IPv4
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/netinet/in.h">netinet/in.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/netinet/udp.h">netinet/udp.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>udp_socket = socket(AF_INET, SOCK_DGRAM, 0);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
This is an implementation of the User Datagram Protocol
|
|
described in RFC 768.
|
|
It implements a connectionless, unreliable datagram packet service.
|
|
Packets may be reordered or duplicated before they arrive.
|
|
UDP generates and checks checksums to catch transmission errors.
|
|
<P>
|
|
|
|
When a UDP socket is created,
|
|
its local and remote addresses are unspecified.
|
|
Datagrams can be sent immediately using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
|
|
|
|
with a valid destination address as an argument.
|
|
When
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
|
|
|
|
is called on the socket, the default destination address is set and
|
|
datagrams can now be sent using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2)
|
|
|
|
without specifying a destination address.
|
|
It is still possible to send to other destinations by passing an
|
|
address to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2).
|
|
|
|
In order to receive packets, the socket can be bound to a local
|
|
address first by using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2).
|
|
|
|
Otherwise, the socket layer will automatically assign
|
|
a free local port out of the range defined by
|
|
<I>/proc/sys/net/ipv4/ip_local_port_range</I>
|
|
|
|
and bind the socket to
|
|
<B>INADDR_ANY</B>.
|
|
|
|
<P>
|
|
|
|
All receive operations return only one packet.
|
|
When the packet is smaller than the passed buffer, only that much
|
|
data is returned; when it is bigger, the packet is truncated and the
|
|
<B>MSG_TRUNC</B>
|
|
|
|
flag is set.
|
|
<B>MSG_WAITALL</B>
|
|
|
|
is not supported.
|
|
<P>
|
|
|
|
IP options may be sent or received using the socket options described in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
|
|
|
|
They are processed by the kernel only when the appropriate
|
|
<I>/proc</I>
|
|
|
|
parameter
|
|
is enabled (but still passed to the user even when it is turned off).
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
|
|
|
|
<P>
|
|
|
|
When the
|
|
<B>MSG_DONTROUTE</B>
|
|
|
|
flag is set on sending, the destination address must refer to a local
|
|
interface address and the packet is sent only to that interface.
|
|
<P>
|
|
|
|
By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery.
|
|
This means the kernel
|
|
will keep track of the MTU to a specific target IP address and return
|
|
<B>EMSGSIZE</B>
|
|
|
|
when a UDP packet write exceeds it.
|
|
When this happens, the application should decrease the packet size.
|
|
Path MTU discovery can be also turned off using the
|
|
<B>IP_MTU_DISCOVER</B>
|
|
|
|
socket option or the
|
|
<I>/proc/sys/net/ipv4/ip_no_pmtu_disc</I>
|
|
|
|
file; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
|
|
|
|
for details.
|
|
When turned off, UDP will fragment outgoing UDP packets
|
|
that exceed the interface MTU.
|
|
However, disabling it is not recommended
|
|
for performance and reliability reasons.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Address format</H3>
|
|
|
|
UDP uses the IPv4
|
|
<I>sockaddr_in</I>
|
|
|
|
address format described in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7).
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Error handling</H3>
|
|
|
|
All fatal errors will be passed to the user as an error return even
|
|
when the socket is not connected.
|
|
This includes asynchronous errors
|
|
received from the network.
|
|
You may get an error for an earlier packet
|
|
that was sent on the same socket.
|
|
This behavior differs from many other BSD socket implementations
|
|
which don't pass any errors unless the socket is connected.
|
|
Linux's behavior is mandated by
|
|
<B>RFC 1122</B>.
|
|
|
|
<P>
|
|
|
|
For compatibility with legacy code, in Linux 2.0 and 2.2
|
|
it was possible to set the
|
|
<B>SO_BSDCOMPAT</B>
|
|
|
|
<B>SOL_SOCKET</B>
|
|
|
|
option to receive remote errors only when the socket has been
|
|
connected (except for
|
|
<B>EPROTO</B>
|
|
|
|
and
|
|
<B>EMSGSIZE</B>).
|
|
|
|
Locally generated errors are always passed.
|
|
Support for this socket option was removed in later kernels; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
|
|
|
|
for further information.
|
|
<P>
|
|
|
|
When the
|
|
<B>IP_RECVERR</B>
|
|
|
|
option is enabled, all errors are stored in the socket error queue,
|
|
and can be received by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2)
|
|
|
|
with the
|
|
<B>MSG_ERRQUEUE</B>
|
|
|
|
flag set.
|
|
<A NAME="lbAG"> </A>
|
|
<H3>/proc interfaces</H3>
|
|
|
|
System-wide UDP parameter settings can be accessed by files in the directory
|
|
<I>/proc/sys/net/ipv4/</I>.
|
|
|
|
<DL COMPACT>
|
|
<DT id="1"><I>udp_mem</I> (since Linux 2.6.25)
|
|
|
|
<DD>
|
|
This is a vector of three integers governing the number
|
|
of pages allowed for queueing by all UDP sockets.
|
|
<DL COMPACT><DT id="2"><DD>
|
|
<DL COMPACT>
|
|
<DT id="3"><I>min</I>
|
|
|
|
<DD>
|
|
Below this number of pages, UDP is not bothered about its
|
|
memory appetite.
|
|
When the amount of memory allocated by UDP exceeds
|
|
this number, UDP starts to moderate memory usage.
|
|
<DT id="4"><I>pressure</I>
|
|
|
|
<DD>
|
|
This value was introduced to follow the format of
|
|
<I>tcp_mem</I>
|
|
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7)).
|
|
|
|
<DT id="5"><I>max</I>
|
|
|
|
<DD>
|
|
Number of pages allowed for queueing by all UDP sockets.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="6"><DD>
|
|
Defaults values for these three items are
|
|
calculated at boot time from the amount of available memory.
|
|
<DT id="7"><I>udp_rmem_min</I> (integer; default value: PAGE_SIZE; since Linux 2.6.25)
|
|
|
|
<DD>
|
|
Minimal size, in bytes, of receive buffers used by UDP sockets in moderation.
|
|
Each UDP socket is able to use the size for receiving data,
|
|
even if total pages of UDP sockets exceed
|
|
<I>udp_mem</I>
|
|
|
|
pressure.
|
|
<DT id="8"><I>udp_wmem_min</I> (integer; default value: PAGE_SIZE; since Linux 2.6.25)
|
|
|
|
<DD>
|
|
Minimal size, in bytes, of send buffer used by UDP sockets in moderation.
|
|
Each UDP socket is able to use the size for sending data,
|
|
even if total pages of UDP sockets exceed
|
|
<I>udp_mem</I>
|
|
|
|
pressure.
|
|
</DL>
|
|
<A NAME="lbAH"> </A>
|
|
<H3>Socket options</H3>
|
|
|
|
To set or get a UDP socket option, call
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
|
|
|
|
to read or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
|
|
|
|
to write the option with the option level argument set to
|
|
<B>IPPROTO_UDP</B>.
|
|
|
|
Unless otherwise noted,
|
|
<I>optval</I>
|
|
|
|
is a pointer to an
|
|
<I>int</I>.
|
|
|
|
<P>
|
|
|
|
Following is a list of UDP-specific socket options.
|
|
For details of some other socket options that are also applicable
|
|
for UDP sockets, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7).
|
|
|
|
<DL COMPACT>
|
|
<DT id="9"><B>UDP_CORK</B> (since Linux 2.5.44)
|
|
|
|
<DD>
|
|
If this option is enabled, then all data output on this socket
|
|
is accumulated into a single datagram that is transmitted when
|
|
the option is disabled.
|
|
This option should not be used in code intended to be
|
|
portable.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H3>Ioctls</H3>
|
|
|
|
These ioctls can be accessed using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2).
|
|
|
|
The correct syntax is:
|
|
<P>
|
|
|
|
<DL COMPACT><DT id="10"><DD>
|
|
<PRE>
|
|
<B>int</B><I> value;</I>
|
|
<I>error</I><B> = ioctl(</B><I>udp_socket</I><B>, </B><I>ioctl_type</I><B>, &</B><I>value</I><B>);</B>
|
|
</PRE>
|
|
|
|
</DL>
|
|
|
|
<DL COMPACT>
|
|
<DT id="11"><B>FIONREAD</B> (<B>SIOCINQ</B>)
|
|
|
|
<DD>
|
|
Gets a pointer to an integer as argument.
|
|
Returns the size of the next pending datagram in the integer in bytes,
|
|
or 0 when no datagram is pending.
|
|
<B>Warning:</B>
|
|
|
|
Using
|
|
<B>FIONREAD</B>,
|
|
|
|
it is impossible to distinguish the case where no datagram is pending
|
|
from the case where the next pending datagram contains zero bytes of data.
|
|
It is safer to use
|
|
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2),
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7)
|
|
|
|
to distinguish these cases.
|
|
|
|
|
|
<DT id="12"><B>TIOCOUTQ</B> (<B>SIOCOUTQ</B>)
|
|
|
|
<DD>
|
|
Returns the number of data bytes in the local send queue.
|
|
Supported only with Linux 2.4 and above.
|
|
</DL>
|
|
<P>
|
|
|
|
In addition, all ioctls documented in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
|
|
|
|
are supported.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
All errors documented for
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)
|
|
|
|
may be returned by a send or receive on a UDP socket.
|
|
<DL COMPACT>
|
|
<DT id="13"><B>ECONNREFUSED</B>
|
|
|
|
<DD>
|
|
No receiver was associated with the destination address.
|
|
This might be caused by a previous packet sent over the socket.
|
|
</DL>
|
|
<A NAME="lbAK"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>IP_RECVERR</B>
|
|
|
|
is a new feature in Linux 2.2.
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+raw">raw</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+udplite">udplite</A></B>(7)
|
|
|
|
<P>
|
|
|
|
RFC 768 for the User Datagram Protocol.
|
|
<BR>
|
|
|
|
RFC 1122 for the host requirements.
|
|
<BR>
|
|
|
|
RFC 1191 for a description of path MTU discovery.
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="15"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="16"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="17"><A HREF="#lbAE">Address format</A><DD>
|
|
<DT id="18"><A HREF="#lbAF">Error handling</A><DD>
|
|
<DT id="19"><A HREF="#lbAG">/proc interfaces</A><DD>
|
|
<DT id="20"><A HREF="#lbAH">Socket options</A><DD>
|
|
<DT id="21"><A HREF="#lbAI">Ioctls</A><DD>
|
|
</DL>
|
|
<DT id="22"><A HREF="#lbAJ">ERRORS</A><DD>
|
|
<DT id="23"><A HREF="#lbAK">VERSIONS</A><DD>
|
|
<DT id="24"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="25"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:06:10 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|