man-pages/man7/socket.7.html
2021-03-31 01:06:50 +01:00

1508 lines
37 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SOCKET</TITLE>
</HEAD><BODY>
<H1>SOCKET</H1>
Section: Linux Programmer's Manual (7)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
socket - Linux socket interface
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<P>
<I>sockfd</I><B> = socket(int </B><I>socket_family</I><B>, int </B><I>socket_type</I><B>, int </B><I>protocol</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
This manual page describes the Linux networking socket layer user
interface.
The BSD compatible sockets
are the uniform interface
between the user process and the network protocol stacks in the kernel.
The protocol modules are grouped into
<I>protocol families</I>
such as
<B>AF_INET</B>, <B>AF_IPX</B>, and <B>AF_PACKET</B>,
and
<I>socket types</I>
such as
<B>SOCK_STREAM</B>
or
<B>SOCK_DGRAM</B>.
See
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
for more information on families and types.
<A NAME="lbAE">&nbsp;</A>
<H3>Socket-layer functions</H3>
These functions are used by the user process to send or receive packets
and to do other socket operations.
For more information see their respective manual pages.
<P>
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
creates a socket,
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
connects a socket to a remote socket address,
the
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
function binds a socket to a local socket address,
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2)
tells the socket that new connections shall be accepted, and
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
is used to get a new socket with a new incoming connection.
<B><A HREF="/cgi-bin/man/man2html?2+socketpair">socketpair</A></B>(2)
returns two connected anonymous sockets (implemented only for a few
local families like
<B>AF_UNIX</B>)
<P>
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendto">sendto</A></B>(2),
and
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
send data over a socket, and
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+recvfrom">recvfrom</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2)
receive data from a socket.
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
wait for arriving data or a readiness to send data.
In addition, the standard I/O operations like
<B><A HREF="/cgi-bin/man/man2html?2+write">write</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+writev">writev</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendfile">sendfile</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
and
<B><A HREF="/cgi-bin/man/man2html?2+readv">readv</A></B>(2)
can be used to read and write data.
<P>
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2)
returns the local socket address and
<B><A HREF="/cgi-bin/man/man2html?2+getpeername">getpeername</A></B>(2)
returns the remote socket address.
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
are used to set or get socket layer or protocol options.
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2)
can be used to set or read some other options.
<P>
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)
is used to close a socket.
<B><A HREF="/cgi-bin/man/man2html?2+shutdown">shutdown</A></B>(2)
closes parts of a full-duplex socket connection.
<P>
Seeking, or calling
<B><A HREF="/cgi-bin/man/man2html?2+pread">pread</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+pwrite">pwrite</A></B>(2)
with a nonzero position is not supported on sockets.
<P>
It is possible to do nonblocking I/O on sockets by setting the
<B>O_NONBLOCK</B>
flag on a socket file descriptor using
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
Then all operations that would block will (usually)
return with
<B>EAGAIN</B>
(operation should be retried later);
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
will return
<B>EINPROGRESS</B>
error.
The user can then wait for various events via
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2).
<TABLE BORDER>
<TR VALIGN=top><TD ALIGN=center COLSPAN=3>I/O events<BR></TD></TR>
<TR VALIGN=top><TD>Event</TD><TD>Poll flag</TD><TD>Occurrence<BR></TD></TR>
<TR VALIGN=top><TD>Read</TD><TD>POLLIN</TD><TD>
New data arrived.
<BR></TD></TR>
<TR VALIGN=top><TD>Read</TD><TD>POLLIN</TD><TD>
A connection setup has been completed
(for connection-oriented sockets)
<BR></TD></TR>
<TR VALIGN=top><TD>Read</TD><TD>POLLHUP</TD><TD>
A disconnection request has been initiated by the other end.
<BR></TD></TR>
<TR VALIGN=top><TD>Read</TD><TD>POLLHUP</TD><TD>
A connection is broken (only for connection-oriented protocols).
When the socket is written
<B>SIGPIPE</B>
is also sent.
<BR></TD></TR>
<TR VALIGN=top><TD>Write</TD><TD>POLLOUT</TD><TD>
Socket has enough send buffer space for writing new data.
<BR></TD></TR>
<TR VALIGN=top><TD>Read/Write</TD><TD>
POLLIN |
<BR>
POLLOUT
</TD><TD>
An outgoing
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
finished.
<BR></TD></TR>
<TR VALIGN=top><TD>Read/Write</TD><TD>POLLERR</TD><TD>An asynchronous error occurred.<BR></TD></TR>
<TR VALIGN=top><TD>Read/Write</TD><TD>POLLHUP</TD><TD>The other end has shut down one direction.<BR></TD></TR>
<TR VALIGN=top><TD>Exception</TD><TD>POLLPRI</TD><TD>
Urgent data arrived.
<B>SIGURG</B>
is sent then.
<BR></TD></TR>
</TABLE>
<P>
An alternative to
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
is to let the kernel inform the application about events
via a
<B>SIGIO</B>
signal.
For that the
<B>O_ASYNC</B>
flag must be set on a socket file descriptor via
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
and a valid signal handler for
<B>SIGIO</B>
must be installed via
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2).
See the
<I>Signals</I>
discussion below.
<A NAME="lbAF">&nbsp;</A>
<H3>Socket address structures</H3>
Each socket domain has its own format for socket addresses,
with a domain-specific address structure.
Each of these structures begins with an
integer &quot;family&quot; field (typed as
<I>sa_family_t</I>)
that indicates the type of the address structure.
This allows
the various system calls (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockname">getsockname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getpeername">getpeername</A></B>(2)),
which are generic to all socket domains,
to determine the domain of a particular socket address.
<P>
To allow any type of socket address to be passed to
interfaces in the sockets API,
the type
<I>struct sockaddr</I>
is defined.
The purpose of this type is purely to allow casting of
domain-specific socket address types to a &quot;generic&quot; type,
so as to avoid compiler warnings about type mismatches in
calls to the sockets API.
<P>
In addition, the sockets API provides the data type
<I>struct sockaddr_storage.</I>
This type
is suitable to accommodate all supported domain-specific socket
address structures; it is large enough and is aligned properly.
(In particular, it is large enough to hold
IPv6 socket addresses.)
The structure includes the following field, which can be used to identify
the type of socket address actually stored in the structure:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa_family_t&nbsp;ss_family;
<P>
The
<I>sockaddr_storage</I>
structure is useful in programs that must handle socket addresses
in a generic way
(e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
<A NAME="lbAG">&nbsp;</A>
<H3>Socket options</H3>
The socket options listed below can be set by using
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
and read with
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
with the socket level set to
<B>SOL_SOCKET</B>
for all sockets.
Unless otherwise noted,
<I>optval</I>
is a pointer to an
<I>int</I>.
<DL COMPACT>
<DT id="1"><B>SO_ACCEPTCONN</B>
<DD>
Returns a value indicating whether or not this socket has been marked
to accept connections with
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2).
The value 0 indicates that this is not a listening socket,
the value 1 indicates that this is a listening socket.
This socket option is read-only.
<DT id="2"><B>SO_ATTACH_FILTER</B> (since Linux 2.2), <B>SO_ATTACH_BPF</B> (since Linux 3.19)
<DD>
Attach a classic BPF
(<B>SO_ATTACH_FILTER</B>)
or an extended BPF
(<B>SO_ATTACH_BPF</B>)
program to the socket for use as a filter of incoming packets.
A packet will be dropped if the filter program returns zero.
If the filter program returns a
nonzero value which is less than the packet's data length,
the packet will be truncated to the length returned.
If the value returned by the filter is greater than or equal to the
packet's data length, the packet is allowed to proceed unmodified.
<DT id="3"><DD>
The argument for
<B>SO_ATTACH_FILTER</B>
is a
<I>sock_fprog</I>
structure, defined in
<I>&lt;<A HREF="file:///usr/include/linux/filter.h">linux/filter.h</A>&gt;</I>:
<DT id="4"><DD>
struct sock_fprog {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;short&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sock_filter&nbsp;*filter;
};
<DT id="5"><DD>
The argument for
<B>SO_ATTACH_BPF</B>
is a file descriptor returned by the
<B><A HREF="/cgi-bin/man/man2html?2+bpf">bpf</A></B>(2)
system call and must refer to a program of type
<B>BPF_PROG_TYPE_SOCKET_FILTER</B>.
<DT id="6"><DD>
These options may be set multiple times for a given socket,
each time replacing the previous filter program.
The classic and extended versions may be called on the same socket,
but the previous filter will always be replaced such that a socket
never has more than one filter defined.
<DT id="7"><DD>
Both classic and extended BPF are explained in the kernel source file
<I>Documentation/networking/filter.txt</I>
<DT id="8"><B>SO_ATTACH_REUSEPORT_CBPF</B>, <B>SO_ATTACH_REUSEPORT_EBPF</B>
<DD>
For use with the
<B>SO_REUSEPORT</B>
option, these options allow the user to set a classic BPF
(<B>SO_ATTACH_REUSEPORT_CBPF</B>)
or an extended BPF
(<B>SO_ATTACH_REUSEPORT_EBPF</B>)
program which defines how packets are assigned to
the sockets in the reuseport group (that is, all sockets which have
<B>SO_REUSEPORT</B>
set and are using the same local address to receive packets).
<DT id="9"><DD>
The BPF program must return an index between 0 and N-1 representing
the socket which should receive the packet
(where N is the number of sockets in the group).
If the BPF program returns an invalid index,
socket selection will fall back to the plain
<B>SO_REUSEPORT</B>
mechanism.
<DT id="10"><DD>
Sockets are numbered in the order in which they are added to the group
(that is, the order of
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
calls for UDP sockets or the order of
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2)
calls for TCP sockets).
New sockets added to a reuseport group will inherit the BPF program.
When a socket is removed from a reuseport group (via
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)),
the last socket in the group will be moved into the closed socket's
position.
<DT id="11"><DD>
These options may be set repeatedly at any time on any socket in the group
to replace the current BPF program used by all sockets in the group.
<DT id="12"><DD>
<B>SO_ATTACH_REUSEPORT_CBPF</B>
takes the same argument type as
<B>SO_ATTACH_FILTER</B>
and
<B>SO_ATTACH_REUSEPORT_EBPF</B>
takes the same argument type as
<B>SO_ATTACH_BPF</B>.
<DT id="13"><DD>
UDP support for this feature is available since Linux 4.5;
TCP support is available since Linux 4.6.
<DT id="14"><B>SO_BINDTODEVICE</B>
<DD>
Bind this socket to a particular device like "eth0",
as specified in the passed interface name.
If the
name is an empty string or the option length is zero, the socket device
binding is removed.
The passed option is a variable-length null-terminated
interface name string with the maximum size of
<B>IFNAMSIZ</B>.
If a socket is bound to an interface,
only packets received from that particular interface are processed by the
socket.
Note that this works only for some socket types, particularly
<B>AF_INET</B>
sockets.
It is not supported for packet sockets (use normal
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
there).
<DT id="15"><DD>
Before Linux 3.8,
this socket option could be set, but could not retrieved with
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2).
Since Linux 3.8, it is readable.
The
<I>optlen</I>
argument should contain the buffer size available
to receive the device name and is recommended to be
<B>IFNAMSIZ</B>
bytes.
The real device name length is reported back in the
<I>optlen</I>
argument.
<DT id="16"><B>SO_BROADCAST</B>
<DD>
Set or get the broadcast flag.
When enabled, datagram sockets are allowed to send
packets to a broadcast address.
This option has no effect on stream-oriented sockets.
<DT id="17"><B>SO_BSDCOMPAT</B>
<DD>
Enable BSD bug-to-bug compatibility.
This is used by the UDP protocol module in Linux 2.0 and 2.2.
If enabled, ICMP errors received for a UDP socket will not be passed
to the user program.
In later kernel versions, support for this option has been phased out:
Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
(printk()) if a program uses this option.
Linux 2.0 also enabled BSD bug-to-bug compatibility
options (random header changing, skipping of the broadcast flag) for raw
sockets with this option, but that was removed in Linux 2.2.
<DT id="18"><B>SO_DEBUG</B>
<DD>
Enable socket debugging.
Allowed only for processes with the
<B>CAP_NET_ADMIN</B>
capability or an effective user ID of 0.
<DT id="19"><B>SO_DETACH_FILTER</B> (since Linux 2.2), <B>SO_DETACH_BPF</B> (since Linux 3.19)
<DD>
These two options, which are synonyms,
may be used to remove the classic or extended BPF
program attached to a socket with either
<B>SO_ATTACH_FILTER</B>
or
<B>SO_ATTACH_BPF</B>.
The option value is ignored.
<DT id="20"><B>SO_DOMAIN</B> (since Linux 2.6.32)
<DD>
Retrieves the socket domain as an integer, returning a value such as
<B>AF_INET6</B>.
See
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
for details.
This socket option is read-only.
<DT id="21"><B>SO_ERROR</B>
<DD>
Get and clear the pending socket error.
This socket option is read-only.
Expects an integer.
<DT id="22"><B>SO_DONTROUTE</B>
<DD>
Don't send via a gateway, send only to directly connected hosts.
The same effect can be achieved by setting the
<B>MSG_DONTROUTE</B>
flag on a socket
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2)
operation.
Expects an integer boolean flag.
<DT id="23"><B>SO_INCOMING_CPU</B> (gettable since Linux 3.19, settable since Linux 4.4)
<DD>
Sets or gets the CPU affinity of a socket.
Expects an integer flag.
<DT id="24"><DD>
int cpu = 1;
setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &amp;cpu, sizeof(cpu));
<DT id="25"><DD>
Because all of the packets for a single stream
(i.e., all packets for the same 4-tuple)
arrive on the single RX queue that is associated with a particular CPU,
the typical use case is to employ one listening process per RX queue,
with the incoming flow being handled by a listener
on the same CPU that is handling the RX queue.
This provides optimal NUMA behavior and keeps CPU caches hot.
<DT id="26"><B>SO_KEEPALIVE</B>
<DD>
Enable sending of keep-alive messages on connection-oriented sockets.
Expects an integer boolean flag.
<DT id="27"><B>SO_LINGER</B>
<DD>
Sets or gets the
<B>SO_LINGER</B>
option.
The argument is a
<I>linger</I>
structure.
<DT id="28"><DD>
struct linger {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;l_onoff;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;linger&nbsp;active&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;l_linger;&nbsp;&nbsp;&nbsp;/*&nbsp;how&nbsp;many&nbsp;seconds&nbsp;to&nbsp;linger&nbsp;for&nbsp;*/
};
<DT id="29"><DD>
When enabled, a
<B><A HREF="/cgi-bin/man/man2html?2+close">close</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+shutdown">shutdown</A></B>(2)
will not return until all queued messages for the socket have been
successfully sent or the linger timeout has been reached.
Otherwise,
the call returns immediately and the closing is done in the background.
When the socket is closed as part of
<B><A HREF="/cgi-bin/man/man2html?2+exit">exit</A></B>(2),
it always lingers in the background.
<DT id="30"><B>SO_LOCK_FILTER</B>
<DD>
When set, this option will prevent
changing the filters associated with the socket.
These filters include any set using the socket options
<B>SO_ATTACH_FILTER</B>,
<B>SO_ATTACH_BPF</B>,
<B>SO_ATTACH_REUSEPORT_CBPF</B>,
and
<B>SO_ATTACH_REUSEPORT_EBPF</B>.
<DT id="31"><DD>
The typical use case is for a privileged process to set up a raw socket
(an operation that requires the
<B>CAP_NET_RAW</B>
capability), apply a restrictive filter, set the
<B>SO_LOCK_FILTER</B>
option,
and then either drop its privileges or pass the socket file descriptor
to an unprivileged process via a UNIX domain socket.
<DT id="32"><DD>
Once the
<B>SO_LOCK_FILTER</B>
option has been enabled, attempts to change or remove the filter
attached to a socket, or to disable the
<B>SO_LOCK_FILTER</B>
option will fail with the error
<B>EPERM</B>.
<DT id="33"><B>SO_MARK</B> (since Linux 2.6.25)
<DD>
Set the mark for each packet sent through this socket
(similar to the netfilter MARK target but socket-based).
Changing the mark can be used for mark-based
routing without netfilter or for packet filtering.
Setting this option requires the
<B>CAP_NET_ADMIN</B>
capability.
<DT id="34"><B>SO_OOBINLINE</B>
<DD>
If this option is enabled,
out-of-band data is directly placed into the receive data stream.
Otherwise, out-of-band data is passed only when the
<B>MSG_OOB</B>
flag is set during receiving.
<DT id="35"><B>SO_PASSCRED</B>
<DD>
Enable or disable the receiving of the
<B>SCM_CREDENTIALS</B>
control message.
For more information see
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7).
<DT id="36"><B>SO_PASSSEC</B>
<DD>
Enable or disable the receiving of the
<B>SCM_SECURITY</B>
control message.
For more information see
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7).
<DT id="37"><B>SO_PEEK_OFF</B> (since Linux 3.4)
<DD>
This option, which is currently supported only for
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7)
sockets, sets the value of the &quot;peek offset&quot; for the
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2)
system call when used with
<B>MSG_PEEK</B>
flag.
<DT id="38"><DD>
When this option is set to a negative value
(it is set to -1 for all new sockets),
traditional behavior is provided:
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2)
with the
<B>MSG_PEEK</B>
flag will peek data from the front of the queue.
<DT id="39"><DD>
When the option is set to a value greater than or equal to zero,
then the next peek at data queued in the socket will occur at
the byte offset specified by the option value.
At the same time, the &quot;peek offset&quot; will be
incremented by the number of bytes that were peeked from the queue,
so that a subsequent peek will return the next data in the queue.
<DT id="40"><DD>
If data is removed from the front of the queue via a call to
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2)
(or similar) without the
<B>MSG_PEEK</B>
flag, the &quot;peek offset&quot; will be decreased by the number of bytes removed.
In other words, receiving data without the
<B>MSG_PEEK</B>
flag will cause the &quot;peek offset&quot; to be adjusted to maintain
the correct relative position in the queued data,
so that a subsequent peek will retrieve the data that would have been
retrieved had the data not been removed.
<DT id="41"><DD>
For datagram sockets, if the &quot;peek offset&quot; points to the middle of a packet,
the data returned will be marked with the
<B>MSG_TRUNC</B>
flag.
<DT id="42"><DD>
The following example serves to illustrate the use of
<B>SO_PEEK_OFF</B>.
Suppose a stream socket has the following queued input data:
<DT id="43"><DD>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;aabbccddeeff
<DT id="44"><DD>
The following sequence of
<B><A HREF="/cgi-bin/man/man2html?2+recv">recv</A></B>(2)
calls would have the effect noted in the comments:
<DT id="45"><DD>
int ov = 4; // Set peek offset to 4
setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &amp;ov, sizeof(ov));
<P>
recv(fd, buf, 2, MSG_PEEK); // Peeks &quot;cc&quot;; offset set to 6
recv(fd, buf, 2, MSG_PEEK); // Peeks &quot;dd&quot;; offset set to 8
recv(fd, buf, 2, 0); // Reads &quot;aa&quot;; offset set to 6
recv(fd, buf, 2, MSG_PEEK); // Peeks &quot;ee&quot;; offset set to 8
<DT id="46"><B>SO_PEERCRED</B>
<DD>
Return the credentials of the peer process connected to this socket.
For further details, see
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7).
<DT id="47"><B>SO_PRIORITY</B>
<DD>
Set the protocol-defined priority for all packets to be sent on
this socket.
Linux uses this value to order the networking queues:
packets with a higher priority may be processed first depending
on the selected device queueing discipline.
Setting a priority outside the range 0 to 6 requires the
<B>CAP_NET_ADMIN</B>
capability.
<DT id="48"><B>SO_PROTOCOL</B> (since Linux 2.6.32)
<DD>
Retrieves the socket protocol as an integer, returning a value such as
<B>IPPROTO_SCTP</B>.
See
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2)
for details.
This socket option is read-only.
<DT id="49"><B>SO_RCVBUF</B>
<DD>
Sets or gets the maximum socket receive buffer in bytes.
The kernel doubles this value (to allow space for bookkeeping overhead)
when it is set using
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2),
and this doubled value is returned by
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2).
The default value is set by the
<I>/proc/sys/net/core/rmem_default</I>
file, and the maximum allowed value is set by the
<I>/proc/sys/net/core/rmem_max</I>
file.
The minimum (doubled) value for this option is 256.
<DT id="50"><B>SO_RCVBUFFORCE</B> (since Linux 2.6.14)
<DD>
Using this socket option, a privileged
(<B>CAP_NET_ADMIN</B>)
process can perform the same task as
<B>SO_RCVBUF</B>,
but the
<I>rmem_max</I>
limit can be overridden.
<DT id="51"><B>SO_RCVLOWAT</B> and <B>SO_SNDLOWAT</B>
<DD>
Specify the minimum number of bytes in the buffer until the socket layer
will pass the data to the protocol
(<B>SO_SNDLOWAT</B>)
or the user on receiving
(<B>SO_RCVLOWAT</B>).
These two values are initialized to 1.
<B>SO_SNDLOWAT</B>
is not changeable on Linux
(<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
fails with the error
<B>ENOPROTOOPT</B>).
<B>SO_RCVLOWAT</B>
is changeable
only since Linux 2.4.
<DT id="52"><DD>
Before Linux 2.6.28
<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),
and
<B><A HREF="/cgi-bin/man/man2html?7+epoll">epoll</A></B>(7)
did not respect the
<B>SO_RCVLOWAT</B>
setting on Linux,
and indicated a socket as readable when even a single byte of data
was available.
A subsequent read from the socket would then block until
<B>SO_RCVLOWAT</B>
bytes are available.
<DT id="53"><B>SO_RCVTIMEO</B> and <B>SO_SNDTIMEO</B>
<DD>
Specify the receiving or sending timeouts until reporting an error.
The argument is a
<I>struct timeval</I>.
If an input or output function blocks for this period of time, and
data has been sent or received, the return value of that function
will be the amount of data transferred; if no data has been transferred
and the timeout has been reached, then -1 is returned with
<I>errno</I>
set to
<B>EAGAIN</B>
or
<B>EWOULDBLOCK</B>,
or
<B>EINPROGRESS</B>
(for
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2))
just as if the socket was specified to be nonblocking.
If the timeout is set to zero (the default),
then the operation will never timeout.
Timeouts only have effect for system calls that perform socket I/O (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+send">send</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2));
timeouts have no effect for
<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),
<B><A HREF="/cgi-bin/man/man2html?2+epoll_wait">epoll_wait</A></B>(2),
and so on.
<DT id="54"><B>SO_REUSEADDR</B>
<DD>
Indicates that the rules used in validating addresses supplied in a
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
call should allow reuse of local addresses.
For
<B>AF_INET</B>
sockets this
means that a socket may bind, except when there
is an active listening socket bound to the address.
When the listening socket is bound to
<B>INADDR_ANY</B>
with a specific port then it is not possible
to bind to this port for any local address.
Argument is an integer boolean flag.
<DT id="55"><B>SO_REUSEPORT</B> (since Linux 3.9)
<DD>
Permits multiple
<B>AF_INET</B>
or
<B>AF_INET6</B>
sockets to be bound to an identical socket address.
This option must be set on each socket (including the first socket)
prior to calling
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
on the socket.
To prevent port hijacking,
all of the processes binding to the same address must have the same
effective UID.
This option can be employed with both TCP and UDP sockets.
<DT id="56"><DD>
For TCP sockets, this option allows
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
load distribution in a multi-threaded server to be improved by
using a distinct listener socket for each thread.
This provides improved load distribution as compared
to traditional techniques such using a single
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)ing
thread that distributes connections,
or having multiple threads that compete to
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
from the same socket.
<DT id="57"><DD>
For UDP sockets,
the use of this option can provide better distribution
of incoming datagrams to multiple processes (or threads) as compared
to the traditional technique of having multiple processes
compete to receive datagrams on the same socket.
<DT id="58"><B>SO_RXQ_OVFL</B> (since Linux 2.6.33)
<DD>
Indicates that an unsigned 32-bit value ancillary message (cmsg)
should be attached to received skbs indicating
the number of packets dropped by the socket since its creation.
<DT id="59"><B>SO_SNDBUF</B>
<DD>
Sets or gets the maximum socket send buffer in bytes.
The kernel doubles this value (to allow space for bookkeeping overhead)
when it is set using
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2),
and this doubled value is returned by
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2).
The default value is set by the
<I>/proc/sys/net/core/wmem_default</I>
file and the maximum allowed value is set by the
<I>/proc/sys/net/core/wmem_max</I>
file.
The minimum (doubled) value for this option is 2048.
<DT id="60"><B>SO_SNDBUFFORCE</B> (since Linux 2.6.14)
<DD>
Using this socket option, a privileged
(<B>CAP_NET_ADMIN</B>)
process can perform the same task as
<B>SO_SNDBUF</B>,
but the
<I>wmem_max</I>
limit can be overridden.
<DT id="61"><B>SO_TIMESTAMP</B>
<DD>
Enable or disable the receiving of the
<B>SO_TIMESTAMP</B>
control message.
The timestamp control message is sent with level
<B>SOL_SOCKET</B>
and the
<I>cmsg_data</I>
field is a
<I>struct timeval</I>
indicating the
reception time of the last packet passed to the user in this call.
See
<B><A HREF="/cgi-bin/man/man2html?3+cmsg">cmsg</A></B>(3)
for details on control messages.
<DT id="62"><B>SO_TYPE</B>
<DD>
Gets the socket type as an integer (e.g.,
<B>SOCK_STREAM</B>).
This socket option is read-only.
<DT id="63"><B>SO_BUSY_POLL</B> (since Linux 3.11)
<DD>
Sets the approximate time in microseconds to busy poll on a blocking receive
when there is no data.
Increasing this value requires
<B>CAP_NET_ADMIN</B>.
The default for this option is controlled by the
<I>/proc/sys/net/core/busy_read</I>
file.
<DT id="64"><DD>
The value in the
<I>/proc/sys/net/core/busy_poll</I>
file determines how long
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
will busy poll when they operate on sockets with
<B>SO_BUSY_POLL</B>
set and no events to report are found.
<DT id="65"><DD>
In both cases,
busy polling will only be done when the socket last received data
from a network device that supports this option.
<DT id="66"><DD>
While busy polling may improve latency of some applications,
care must be taken when using it since this will increase
both CPU utilization and power usage.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H3>Signals</H3>
When writing onto a connection-oriented socket that has been shut down
(by the local or the remote end)
<B>SIGPIPE</B>
is sent to the writing process and
<B>EPIPE</B>
is returned.
The signal is not sent when the write call
specified the
<B>MSG_NOSIGNAL</B>
flag.
<P>
When requested with the
<B>FIOSETOWN</B>
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
or
<B>SIOCSPGRP</B>
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2),
<B>SIGIO</B>
is sent when an I/O event occurs.
It is possible to use
<B><A HREF="/cgi-bin/man/man2html?2+poll">poll</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+select">select</A></B>(2)
in the signal handler to find out which socket the event occurred on.
An alternative (in Linux 2.2) is to set a real-time signal using the
<B>F_SETSIG</B>
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2);
the handler of the real time signal will be called with
the file descriptor in the
<I>si_fd</I>
field of its
<I>siginfo_t</I>.
See
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
for more information.
<P>
Under some circumstances (e.g., multiple processes accessing a
single socket), the condition that caused the
<B>SIGIO</B>
may have already disappeared when the process reacts to the signal.
If this happens, the process should wait again because Linux
will resend the signal later.
<A NAME="lbAI">&nbsp;</A>
<H3>/proc interfaces</H3>
The core socket networking parameters can be accessed
via files in the directory
<I>/proc/sys/net/core/</I>.
<DL COMPACT>
<DT id="67"><I>rmem_default</I>
<DD>
contains the default setting in bytes of the socket receive buffer.
<DT id="68"><I>rmem_max</I>
<DD>
contains the maximum socket receive buffer size in bytes which a user may
set by using the
<B>SO_RCVBUF</B>
socket option.
<DT id="69"><I>wmem_default</I>
<DD>
contains the default setting in bytes of the socket send buffer.
<DT id="70"><I>wmem_max</I>
<DD>
contains the maximum socket send buffer size in bytes which a user may
set by using the
<B>SO_SNDBUF</B>
socket option.
<DT id="71"><I>message_cost</I> and <I>message_burst</I>
<DD>
configure the token bucket filter used to load limit warning messages
caused by external network events.
<DT id="72"><I>netdev_max_backlog</I>
<DD>
Maximum number of packets in the global input queue.
<DT id="73"><I>optmem_max</I>
<DD>
Maximum length of ancillary data and user control data like the iovecs
per socket.
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H3>Ioctls</H3>
These operations can be accessed using
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2):
<P>
<I>error</I><B> = ioctl(</B><I>ip_socket</I><B>, </B><I>ioctl_type</I><B>, </B><I>&amp;value_result</I><B>);</B>
<DL COMPACT>
<DT id="74"><B>SIOCGSTAMP</B>
<DD>
Return a
<I>struct timeval</I>
with the receive timestamp of the last packet passed to the user.
This is useful for accurate round trip time measurements.
See
<B><A HREF="/cgi-bin/man/man2html?2+setitimer">setitimer</A></B>(2)
for a description of
<I>struct timeval</I>.
This ioctl should be used only if the socket option
<B>SO_TIMESTAMP</B>
is not set on the socket.
Otherwise, it returns the timestamp of the
last packet that was received while
<B>SO_TIMESTAMP</B>
was not set, or it fails if no such packet has been received,
(i.e.,
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2)
returns -1 with
<I>errno</I>
set to
<B>ENOENT</B>).
<DT id="75"><B>SIOCSPGRP</B>
<DD>
Set the process or process group that is to receive
<B>SIGIO</B>
or
<B>SIGURG</B>
signals when I/O becomes possible or urgent data is available.
The argument is a pointer to a
<I>pid_t</I>.
For further details, see the description of
<B>F_SETOWN</B>
in
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
<DT id="76"><B>FIOASYNC</B>
<DD>
Change the
<B>O_ASYNC</B>
flag to enable or disable asynchronous I/O mode of the socket.
Asynchronous I/O mode means that the
<B>SIGIO</B>
signal or the signal set with
<B>F_SETSIG</B>
is raised when a new I/O event occurs.
<DT id="77"><DD>
Argument is an integer boolean flag.
(This operation is synonymous with the use of
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
to set the
<B>O_ASYNC</B>
flag.)
<DT id="78"><B>SIOCGPGRP</B>
<DD>
Get the current process or process group that receives
<B>SIGIO</B>
or
<B>SIGURG</B>
signals,
or 0
when none is set.
</DL>
<P>
Valid
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
operations:
<DL COMPACT>
<DT id="79"><B>FIOGETOWN</B>
<DD>
The same as the
<B>SIOCGPGRP</B>
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2).
<DT id="80"><B>FIOSETOWN</B>
<DD>
The same as the
<B>SIOCSPGRP</B>
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2).
</DL>
<A NAME="lbAK">&nbsp;</A>
<H2>VERSIONS</H2>
<B>SO_BINDTODEVICE</B>
was introduced in Linux 2.0.30.
<B>SO_PASSCRED</B>
is new in Linux 2.2.
The
<I>/proc</I>
interfaces were introduced in Linux 2.2.
<B>SO_RCVTIMEO</B>
and
<B>SO_SNDTIMEO</B>
are supported since Linux 2.3.41.
Earlier, timeouts were fixed to
a protocol-specific setting, and could not be read or written.
<A NAME="lbAL">&nbsp;</A>
<H2>NOTES</H2>
Linux assumes that half of the send/receive buffer is used for internal
kernel structures; thus the values in the corresponding
<I>/proc</I>
files are twice what can be observed on the wire.
<P>
Linux will allow port reuse only with the
<B>SO_REUSEADDR</B>
option
when this option was set both in the previous program that performed a
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
to the port and in the program that wants to reuse the port.
This differs from some implementations (e.g., FreeBSD)
where only the later program needs to set the
<B>SO_REUSEADDR</B>
option.
Typically this difference is invisible, since, for example, a server
program is designed to always set this option.
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+wireshark">wireshark</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+bpf">bpf</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+pcap">pcap</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+address_families">address_families</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+ddp">ddp</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+packet">packet</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+udp">udp</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+unix">unix</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+tcpdump">tcpdump</A></B>(8)
<A NAME="lbAN">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="81"><A HREF="#lbAB">NAME</A><DD>
<DT id="82"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="83"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="84"><A HREF="#lbAE">Socket-layer functions</A><DD>
<DT id="85"><A HREF="#lbAF">Socket address structures</A><DD>
<DT id="86"><A HREF="#lbAG">Socket options</A><DD>
<DT id="87"><A HREF="#lbAH">Signals</A><DD>
<DT id="88"><A HREF="#lbAI">/proc interfaces</A><DD>
<DT id="89"><A HREF="#lbAJ">Ioctls</A><DD>
</DL>
<DT id="90"><A HREF="#lbAK">VERSIONS</A><DD>
<DT id="91"><A HREF="#lbAL">NOTES</A><DD>
<DT id="92"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="93"><A HREF="#lbAN">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:09 GMT, March 31, 2021
</BODY>
</HTML>