Blame man2/getsockopt.2

Packit 7cfc04
.\" Copyright (c) 1983, 1991 The Regents of the University of California.
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"     $Id: getsockopt.2,v 1.1 1999/05/24 14:57:04 freitag Exp $
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Sat Jul 24 16:19:32 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Modified Mon Apr 22 02:29:06 1996 by Martin Schulze (joey@infodrom.north.de)
Packit 7cfc04
.\" Modified Tue Aug 27 10:52:51 1996 by Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" Modified Thu Jan 23 13:29:34 1997 by Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" Modified Sun Mar 28 21:26:46 1999 by Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" Modified 1999 by Andi Kleen <ak@muc.de>.
Packit 7cfc04
.\"     Removed most stuff because it is in socket.7 now.
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETSOCKOPT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getsockopt, setsockopt \- get and set options on sockets
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.BR "#include <sys/types.h>" "          /* See NOTES */"
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int getsockopt(int " sockfd ", int " level ", int " optname ,
Packit 7cfc04
.BI "               void *" optval ", socklen_t *" optlen );
Packit 7cfc04
.BI "int setsockopt(int " sockfd ", int " level ", int " optname ,
Packit 7cfc04
.BI "               const void *" optval ", socklen_t " optlen );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR getsockopt ()
Packit 7cfc04
and
Packit 7cfc04
.BR setsockopt ()
Packit 7cfc04
manipulate options for the socket referred to by the file descriptor
Packit 7cfc04
.IR sockfd .
Packit 7cfc04
Options may exist at multiple
Packit 7cfc04
protocol levels; they are always present at the uppermost
Packit 7cfc04
socket level.
Packit 7cfc04
.PP
Packit 7cfc04
When manipulating socket options, the level at which the
Packit 7cfc04
option resides and the name of the option must be specified.
Packit 7cfc04
To manipulate options at the sockets API level,
Packit 7cfc04
.I level
Packit 7cfc04
is specified as
Packit 7cfc04
.BR SOL_SOCKET .
Packit 7cfc04
To manipulate options at any
Packit 7cfc04
other level the protocol number of the appropriate protocol
Packit 7cfc04
controlling the option is supplied.
Packit 7cfc04
For example,
Packit 7cfc04
to indicate that an option is to be interpreted by the
Packit 7cfc04
.B TCP
Packit 7cfc04
protocol,
Packit 7cfc04
.I level
Packit 7cfc04
should be set to the protocol number of
Packit 7cfc04
.BR TCP ;
Packit 7cfc04
see
Packit 7cfc04
.BR getprotoent (3).
Packit 7cfc04
.PP
Packit 7cfc04
The arguments
Packit 7cfc04
.I optval
Packit 7cfc04
and
Packit 7cfc04
.I optlen
Packit 7cfc04
are used to access option values for
Packit 7cfc04
.BR setsockopt ().
Packit 7cfc04
For
Packit 7cfc04
.BR getsockopt ()
Packit 7cfc04
they identify a buffer in which the value for the
Packit 7cfc04
requested option(s) are to be returned.
Packit 7cfc04
For
Packit 7cfc04
.BR getsockopt (),
Packit 7cfc04
.I optlen
Packit 7cfc04
is a value-result argument, initially containing the
Packit 7cfc04
size of the buffer pointed to by
Packit 7cfc04
.IR optval ,
Packit 7cfc04
and modified on return to indicate the actual size of
Packit 7cfc04
the value returned.
Packit 7cfc04
If no option value is to be supplied or returned,
Packit 7cfc04
.I optval
Packit 7cfc04
may be NULL.
Packit 7cfc04
.PP
Packit 7cfc04
.I Optname
Packit 7cfc04
and any specified options are passed uninterpreted to the appropriate
Packit 7cfc04
protocol module for interpretation.
Packit 7cfc04
The include file
Packit 7cfc04
.I <sys/socket.h>
Packit 7cfc04
contains definitions for socket level options, described below.
Packit 7cfc04
Options at
Packit 7cfc04
other protocol levels vary in format and name; consult the appropriate
Packit 7cfc04
entries in section 4 of the manual.
Packit 7cfc04
.PP
Packit 7cfc04
Most socket-level options utilize an
Packit 7cfc04
.I int
Packit 7cfc04
argument for
Packit 7cfc04
.IR optval .
Packit 7cfc04
For
Packit 7cfc04
.BR setsockopt (),
Packit 7cfc04
the argument should be nonzero to enable a boolean option, or zero if the
Packit 7cfc04
option is to be disabled.
Packit 7cfc04
.PP
Packit 7cfc04
For a description of the available socket options see
Packit 7cfc04
.BR socket (7)
Packit 7cfc04
and the appropriate protocol man pages.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, zero is returned for the standard options.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.PP
Packit 7cfc04
Netfilter allows the programmer
Packit 7cfc04
to define custom socket options with associated handlers; for such
Packit 7cfc04
options, the return value on success is the value returned by the handler.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP 10
Packit 7cfc04
.B EBADF
Packit 7cfc04
The argument
Packit 7cfc04
.I sockfd
Packit 7cfc04
is not a valid file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
The address pointed to by
Packit 7cfc04
.I optval
Packit 7cfc04
is not in a valid part of the process address space.
Packit 7cfc04
For
Packit 7cfc04
.BR getsockopt (),
Packit 7cfc04
this error may also be returned if
Packit 7cfc04
.I optlen
Packit 7cfc04
is not in a valid part of the process address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I optlen
Packit 7cfc04
invalid in
Packit 7cfc04
.BR setsockopt ().
Packit 7cfc04
In some cases this error can also occur for an invalid value in
Packit 7cfc04
.IR optval
Packit 7cfc04
(e.g., for the
Packit 7cfc04
.B IP_ADD_MEMBERSHIP
Packit 7cfc04
option described in
Packit 7cfc04
.BR ip (7)).
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOPROTOOPT
Packit 7cfc04
The option is unknown at the level indicated.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTSOCK
Packit 7cfc04
The file descriptor
Packit 7cfc04
.I sockfd
Packit 7cfc04
does not refer to a socket.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008,
Packit 7cfc04
SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).
Packit 7cfc04
.\" SVr4 documents additional ENOMEM and ENOSR error codes, but does
Packit 7cfc04
.\" not document the
Packit 7cfc04
.\" .BR SO_SNDLOWAT ", " SO_RCVLOWAT ", " SO_SNDTIMEO ", " SO_RCVTIMEO
Packit 7cfc04
.\" options
Packit 7cfc04
.SH NOTES
Packit 7cfc04
POSIX.1 does not require the inclusion of
Packit 7cfc04
.IR <sys/types.h> ,
Packit 7cfc04
and this header file is not required on Linux.
Packit 7cfc04
However, some historical (BSD) implementations required this header
Packit 7cfc04
file, and portable applications are probably wise to include it.
Packit 7cfc04
.PP
Packit 7cfc04
For background on the
Packit 7cfc04
.I socklen_t
Packit 7cfc04
type, see
Packit 7cfc04
.BR accept (2).
Packit 7cfc04
.SH BUGS
Packit 7cfc04
Several of the socket options should be handled at lower levels of the
Packit 7cfc04
system.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR ioctl (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR getprotoent (3),
Packit 7cfc04
.BR protocols (5),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR packet (7),
Packit 7cfc04
.BR socket (7),
Packit 7cfc04
.BR tcp (7),
Packit 7cfc04
.BR udp (7),
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.