man-pages/man2/getsockopt.2.html
2021-03-31 01:06:50 +01:00

285 lines
6.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETSOCKOPT</TITLE>
</HEAD><BODY>
<H1>GETSOCKOPT</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<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>
getsockopt, setsockopt - get and set options on sockets
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B> /* See NOTES */
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>int getsockopt(int </B><I>sockfd</I><B>, int </B><I>level</I><B>, int </B><I>optname</I><B>,</B>
<B> void *</B><I>optval</I><B>, socklen_t *</B><I>optlen</I><B>);</B>
<B>int setsockopt(int </B><I>sockfd</I><B>, int </B><I>level</I><B>, int </B><I>optname</I><B>,</B>
<B> const void *</B><I>optval</I><B>, socklen_t </B><I>optlen</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>getsockopt</B>()
and
<B>setsockopt</B>()
manipulate options for the socket referred to by the file descriptor
<I>sockfd</I>.
Options may exist at multiple
protocol levels; they are always present at the uppermost
socket level.
<P>
When manipulating socket options, the level at which the
option resides and the name of the option must be specified.
To manipulate options at the sockets API level,
<I>level</I>
is specified as
<B>SOL_SOCKET</B>.
To manipulate options at any
other level the protocol number of the appropriate protocol
controlling the option is supplied.
For example,
to indicate that an option is to be interpreted by the
<B>TCP</B>
protocol,
<I>level</I>
should be set to the protocol number of
<B>TCP</B>;
see
<B><A HREF="/cgi-bin/man/man2html?3+getprotoent">getprotoent</A></B>(3).
<P>
The arguments
<I>optval</I>
and
<I>optlen</I>
are used to access option values for
<B>setsockopt</B>().
For
<B>getsockopt</B>()
they identify a buffer in which the value for the
requested option(s) are to be returned.
For
<B>getsockopt</B>(),
<I>optlen</I>
is a value-result argument, initially containing the
size of the buffer pointed to by
<I>optval</I>,
and modified on return to indicate the actual size of
the value returned.
If no option value is to be supplied or returned,
<I>optval</I>
may be NULL.
<P>
<I>Optname</I>
and any specified options are passed uninterpreted to the appropriate
protocol module for interpretation.
The include file
<I>&lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</I>
contains definitions for socket level options, described below.
Options at
other protocol levels vary in format and name; consult the appropriate
entries in section 4 of the manual.
<P>
Most socket-level options utilize an
<I>int</I>
argument for
<I>optval</I>.
For
<B>setsockopt</B>(),
the argument should be nonzero to enable a boolean option, or zero if the
option is to be disabled.
<P>
For a description of the available socket options see
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
and the appropriate protocol man pages.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned for the standard options.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<P>
Netfilter allows the programmer
to define custom socket options with associated handlers; for such
options, the return value on success is the value returned by the handler.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EBADF</B>
<DD>
The argument
<I>sockfd</I>
is not a valid file descriptor.
<DT id="2"><B>EFAULT</B>
<DD>
The address pointed to by
<I>optval</I>
is not in a valid part of the process address space.
For
<B>getsockopt</B>(),
this error may also be returned if
<I>optlen</I>
is not in a valid part of the process address space.
<DT id="3"><B>EINVAL</B>
<DD>
<I>optlen</I>
invalid in
<B>setsockopt</B>().
In some cases this error can also occur for an invalid value in
<I>optval</I>
(e.g., for the
<B>IP_ADD_MEMBERSHIP</B>
option described in
<B><A HREF="/cgi-bin/man/man2html?7+ip">ip</A></B>(7)).
<DT id="4"><B>ENOPROTOOPT</B>
<DD>
The option is unknown at the level indicated.
<DT id="5"><B>ENOTSOCK</B>
<DD>
The file descriptor
<I>sockfd</I>
does not refer to a socket.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008,
SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1 does not require the inclusion of
<I>&lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</I>,
and this header file is not required on Linux.
However, some historical (BSD) implementations required this header
file, and portable applications are probably wise to include it.
<P>
For background on the
<I>socklen_t</I>
type, see
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2).
<A NAME="lbAI">&nbsp;</A>
<H2>BUGS</H2>
Several of the socket options should be handled at lower levels of the
system.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+ioctl">ioctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+getprotoent">getprotoent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+protocols">protocols</A></B>(5),
<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+socket">socket</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)
<A NAME="lbAK">&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="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAH">NOTES</A><DD>
<DT id="13"><A HREF="#lbAI">BUGS</A><DD>
<DT id="14"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAK">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:05:32 GMT, March 31, 2021
</BODY>
</HTML>