]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getsockopt.2
iconv.1, ldd.1, locale.1, localedef.1, memusage.1, memusagestat.1, mtrace.1, pldd...
[thirdparty/man-pages.git] / man2 / getsockopt.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\" $Id: getsockopt.2,v 1.1 1999/05/24 14:57:04 freitag Exp $
35 .\"
36 .\" Modified Sat Jul 24 16:19:32 1993 by Rik Faith (faith@cs.unc.edu)
37 .\" Modified Mon Apr 22 02:29:06 1996 by Martin Schulze (joey@infodrom.north.de)
38 .\" Modified Tue Aug 27 10:52:51 1996 by Andries Brouwer (aeb@cwi.nl)
39 .\" Modified Thu Jan 23 13:29:34 1997 by Andries Brouwer (aeb@cwi.nl)
40 .\" Modified Sun Mar 28 21:26:46 1999 by Andries Brouwer (aeb@cwi.nl)
41 .\" Modified 1999 by Andi Kleen <ak@muc.de>.
42 .\" Removed most stuff because it is in socket.7 now.
43 .\"
44 .TH GETSOCKOPT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
45 .SH NAME
46 getsockopt, setsockopt \- get and set options on sockets
47 .SH SYNOPSIS
48 .nf
49 .BR "#include <sys/types.h>" " /* See NOTES */"
50 .B #include <sys/socket.h>
51 .PP
52 .BI "int getsockopt(int " sockfd ", int " level ", int " optname ,
53 .BI " void *" optval ", socklen_t *" optlen );
54 .BI "int setsockopt(int " sockfd ", int " level ", int " optname ,
55 .BI " const void *" optval ", socklen_t " optlen );
56 .fi
57 .SH DESCRIPTION
58 .BR getsockopt ()
59 and
60 .BR setsockopt ()
61 manipulate options for the socket referred to by the file descriptor
62 .IR sockfd .
63 Options may exist at multiple
64 protocol levels; they are always present at the uppermost
65 socket level.
66 .PP
67 When manipulating socket options, the level at which the
68 option resides and the name of the option must be specified.
69 To manipulate options at the sockets API level,
70 .I level
71 is specified as
72 .BR SOL_SOCKET .
73 To manipulate options at any
74 other level the protocol number of the appropriate protocol
75 controlling the option is supplied.
76 For example,
77 to indicate that an option is to be interpreted by the
78 .B TCP
79 protocol,
80 .I level
81 should be set to the protocol number of
82 .BR TCP ;
83 see
84 .BR getprotoent (3).
85 .PP
86 The arguments
87 .I optval
88 and
89 .I optlen
90 are used to access option values for
91 .BR setsockopt ().
92 For
93 .BR getsockopt ()
94 they identify a buffer in which the value for the
95 requested option(s) are to be returned.
96 For
97 .BR getsockopt (),
98 .I optlen
99 is a value-result argument, initially containing the
100 size of the buffer pointed to by
101 .IR optval ,
102 and modified on return to indicate the actual size of
103 the value returned.
104 If no option value is to be supplied or returned,
105 .I optval
106 may be NULL.
107 .PP
108 .I Optname
109 and any specified options are passed uninterpreted to the appropriate
110 protocol module for interpretation.
111 The include file
112 .I <sys/socket.h>
113 contains definitions for socket level options, described below.
114 Options at
115 other protocol levels vary in format and name; consult the appropriate
116 entries in section 4 of the manual.
117 .PP
118 Most socket-level options utilize an
119 .I int
120 argument for
121 .IR optval .
122 For
123 .BR setsockopt (),
124 the argument should be nonzero to enable a boolean option, or zero if the
125 option is to be disabled.
126 .PP
127 For a description of the available socket options see
128 .BR socket (7)
129 and the appropriate protocol man pages.
130 .SH RETURN VALUE
131 On success, zero is returned for the standard options.
132 On error, \-1 is returned, and
133 .I errno
134 is set appropriately.
135 .PP
136 Netfilter allows the programmer
137 to define custom socket options with associated handlers; for such
138 options, the return value on success is the value returned by the handler.
139 .SH ERRORS
140 .TP 10
141 .B EBADF
142 The argument
143 .I sockfd
144 is not a valid file descriptor.
145 .TP
146 .B EFAULT
147 The address pointed to by
148 .I optval
149 is not in a valid part of the process address space.
150 For
151 .BR getsockopt (),
152 this error may also be returned if
153 .I optlen
154 is not in a valid part of the process address space.
155 .TP
156 .B EINVAL
157 .I optlen
158 invalid in
159 .BR setsockopt ().
160 In some cases this error can also occur for an invalid value in
161 .IR optval
162 (e.g., for the
163 .B IP_ADD_MEMBERSHIP
164 option described in
165 .BR ip (7)).
166 .TP
167 .B ENOPROTOOPT
168 The option is unknown at the level indicated.
169 .TP
170 .B ENOTSOCK
171 The file descriptor
172 .I sockfd
173 does not refer to a socket.
174 .SH CONFORMING TO
175 POSIX.1-2001, POSIX.1-2008,
176 SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).
177 .\" SVr4 documents additional ENOMEM and ENOSR error codes, but does
178 .\" not document the
179 .\" .BR SO_SNDLOWAT ", " SO_RCVLOWAT ", " SO_SNDTIMEO ", " SO_RCVTIMEO
180 .\" options
181 .SH NOTES
182 POSIX.1 does not require the inclusion of
183 .IR <sys/types.h> ,
184 and this header file is not required on Linux.
185 However, some historical (BSD) implementations required this header
186 file, and portable applications are probably wise to include it.
187 .PP
188 For background on the
189 .I socklen_t
190 type, see
191 .BR accept (2).
192 .SH BUGS
193 Several of the socket options should be handled at lower levels of the
194 system.
195 .SH SEE ALSO
196 .BR ioctl (2),
197 .BR socket (2),
198 .BR getprotoent (3),
199 .BR protocols (5),
200 .BR ip (7),
201 .BR packet (7),
202 .BR socket (7),
203 .BR tcp (7),
204 .BR udp (7),
205 .BR unix (7)