1 |
27 |
unneback |
.\" $OpenBSD: getsockopt.2,v 1.16 2000/10/18 05:12:10 aaron Exp $
|
2 |
|
|
.\" $NetBSD: getsockopt.2,v 1.7 1995/02/27 12:33:29 cgd Exp $
|
3 |
|
|
.\"
|
4 |
|
|
.\" Copyright (c) 1983, 1991, 1993
|
5 |
|
|
.\" The Regents of the University of California. All rights reserved.
|
6 |
|
|
.\"
|
7 |
|
|
.\" Redistribution and use in source and binary forms, with or without
|
8 |
|
|
.\" modification, are permitted provided that the following conditions
|
9 |
|
|
.\" are met:
|
10 |
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
11 |
|
|
.\" notice, this list of conditions and the following disclaimer.
|
12 |
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
13 |
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
14 |
|
|
.\" documentation and/or other materials provided with the distribution.
|
15 |
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
16 |
|
|
.\" must display the following acknowledgement:
|
17 |
|
|
.\" This product includes software developed by the University of
|
18 |
|
|
.\" California, Berkeley and its contributors.
|
19 |
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
20 |
|
|
.\" may be used to endorse or promote products derived from this software
|
21 |
|
|
.\" without specific prior written permission.
|
22 |
|
|
.\"
|
23 |
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
24 |
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
25 |
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26 |
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
27 |
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28 |
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
29 |
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30 |
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
31 |
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
32 |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
33 |
|
|
.\" SUCH DAMAGE.
|
34 |
|
|
.\"
|
35 |
|
|
.\" @(#)getsockopt.2 8.3 (Berkeley) 4/19/94
|
36 |
|
|
.\"
|
37 |
|
|
.Dd February 15, 1999
|
38 |
|
|
.Dt GETSOCKOPT 2
|
39 |
|
|
.Os
|
40 |
|
|
.Sh NAME
|
41 |
|
|
.Nm getsockopt ,
|
42 |
|
|
.Nm setsockopt
|
43 |
|
|
.Nd get and set options on sockets
|
44 |
|
|
.Sh SYNOPSIS
|
45 |
|
|
.Fd #include
|
46 |
|
|
.Fd #include
|
47 |
|
|
.Ft int
|
48 |
|
|
.Fn getsockopt "int s" "int level" "int optname" "void *optval" "socklen_t *optlen"
|
49 |
|
|
.Ft int
|
50 |
|
|
.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
|
51 |
|
|
.Sh DESCRIPTION
|
52 |
|
|
.Fn getsockopt
|
53 |
|
|
and
|
54 |
|
|
.Fn setsockopt
|
55 |
|
|
manipulate the
|
56 |
|
|
.Em options
|
57 |
|
|
associated with a socket.
|
58 |
|
|
Options may exist at multiple protocol levels;
|
59 |
|
|
they are always present at the uppermost
|
60 |
|
|
.Dq socket
|
61 |
|
|
level.
|
62 |
|
|
.Pp
|
63 |
|
|
When manipulating socket options the level at which the
|
64 |
|
|
option resides and the name of the option must be specified.
|
65 |
|
|
To manipulate options at the socket level,
|
66 |
|
|
.Fa level
|
67 |
|
|
is specified as
|
68 |
|
|
.Dv SOL_SOCKET .
|
69 |
|
|
To manipulate options at any other level the protocol number of the
|
70 |
|
|
appropriate protocol controlling the option is supplied.
|
71 |
|
|
For example, to indicate that an option is to be interpreted by the
|
72 |
|
|
.Tn TCP
|
73 |
|
|
protocol,
|
74 |
|
|
.Fa level
|
75 |
|
|
should be set to the protocol number of
|
76 |
|
|
.Tn TCP ;
|
77 |
|
|
see
|
78 |
|
|
.Xr getprotoent 3 .
|
79 |
|
|
.Pp
|
80 |
|
|
The parameters
|
81 |
|
|
.Fa optval
|
82 |
|
|
and
|
83 |
|
|
.Fa optlen
|
84 |
|
|
are used to access option values for
|
85 |
|
|
.Fn setsockopt .
|
86 |
|
|
For
|
87 |
|
|
.Fn getsockopt
|
88 |
|
|
they identify a buffer in which the value for the
|
89 |
|
|
requested option(s) are to be returned.
|
90 |
|
|
For
|
91 |
|
|
.Fn getsockopt ,
|
92 |
|
|
.Fa optlen
|
93 |
|
|
is a value-result parameter, initially containing the
|
94 |
|
|
size of the buffer pointed to by
|
95 |
|
|
.Fa optval ,
|
96 |
|
|
and modified on return to indicate the actual size of the value returned.
|
97 |
|
|
If no option value is to be supplied or returned,
|
98 |
|
|
.Fa optval
|
99 |
|
|
may be
|
100 |
|
|
.Dv NULL .
|
101 |
|
|
.Pp
|
102 |
|
|
.Fa optname
|
103 |
|
|
and any specified options are passed uninterpreted to the appropriate
|
104 |
|
|
protocol module for interpretation.
|
105 |
|
|
The include file
|
106 |
|
|
.Ao Pa sys/socket.h Ac
|
107 |
|
|
contains definitions for socket level options, described below.
|
108 |
|
|
Options at other protocol levels vary in format and name;
|
109 |
|
|
consult the appropriate entries in section 4 of the manual.
|
110 |
|
|
.Pp
|
111 |
|
|
Most socket-level options utilize an
|
112 |
|
|
.Li int
|
113 |
|
|
parameter for
|
114 |
|
|
.Fa optval .
|
115 |
|
|
For
|
116 |
|
|
.Fn setsockopt ,
|
117 |
|
|
the parameter should be non-zero to enable a boolean option,
|
118 |
|
|
or zero if the option is to be disabled.
|
119 |
|
|
.Dv SO_LINGER
|
120 |
|
|
uses a
|
121 |
|
|
.Li struct linger
|
122 |
|
|
parameter, defined in
|
123 |
|
|
.Aq Pa sys/socket.h ,
|
124 |
|
|
which specifies the desired state of the option and the
|
125 |
|
|
linger interval (see below).
|
126 |
|
|
.Dv SO_SNDTIMEO
|
127 |
|
|
and
|
128 |
|
|
.Dv SO_RCVTIMEO
|
129 |
|
|
use a
|
130 |
|
|
.Li struct timeval
|
131 |
|
|
parameter, defined in
|
132 |
|
|
.Aq Pa sys/time.h .
|
133 |
|
|
.Pp
|
134 |
|
|
The following options are recognized at the socket level.
|
135 |
|
|
Except as noted, each may be examined with
|
136 |
|
|
.Fn getsockopt
|
137 |
|
|
and set with
|
138 |
|
|
.Fn setsockopt .
|
139 |
|
|
.Bl -column SO_OOBINLINE -offset indent
|
140 |
|
|
.It Dv SO_DEBUG Ta "enables recording of debugging information"
|
141 |
|
|
.It Dv SO_REUSEADDR Ta "enables local address reuse"
|
142 |
|
|
.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
|
143 |
|
|
.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
|
144 |
|
|
.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
|
145 |
|
|
.It Dv SO_LINGER Ta "linger on close if data present"
|
146 |
|
|
.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
|
147 |
|
|
.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
|
148 |
|
|
.It Dv SO_SNDBUF Ta "set buffer size for output"
|
149 |
|
|
.It Dv SO_RCVBUF Ta "set buffer size for input"
|
150 |
|
|
.It Dv SO_SNDLOWAT Ta "set minimum count for output"
|
151 |
|
|
.It Dv SO_RCVLOWAT Ta "set minimum count for input"
|
152 |
|
|
.It Dv SO_SNDTIMEO Ta "set timeout value for output"
|
153 |
|
|
.It Dv SO_RCVTIMEO Ta "set timeout value for input"
|
154 |
|
|
.It Dv SO_TYPE Ta "get the type of the socket (get only)"
|
155 |
|
|
.It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
|
156 |
|
|
.El
|
157 |
|
|
.Pp
|
158 |
|
|
.Dv SO_DEBUG
|
159 |
|
|
enables debugging in the underlying protocol modules.
|
160 |
|
|
.Dv SO_REUSEADDR
|
161 |
|
|
indicates that the rules used in validating addresses supplied in a
|
162 |
|
|
.Xr bind 2
|
163 |
|
|
call should allow reuse of local addresses.
|
164 |
|
|
.Dv SO_REUSEPORT
|
165 |
|
|
allows completely duplicate bindings by multiple processes if they all set
|
166 |
|
|
.Dv SO_REUSEPORT
|
167 |
|
|
before binding the port.
|
168 |
|
|
This option permits multiple instances of a program to each
|
169 |
|
|
receive UDP/IP multicast or broadcast datagrams destined for the bound port.
|
170 |
|
|
.Dv SO_KEEPALIVE
|
171 |
|
|
enables the periodic transmission of messages on a connected socket.
|
172 |
|
|
Should the connected party fail to respond to these messages, the connection
|
173 |
|
|
is considered broken and processes using the socket are notified via a
|
174 |
|
|
.Dv SIGPIPE
|
175 |
|
|
signal when attempting to send data.
|
176 |
|
|
.Dv SO_DONTROUTE
|
177 |
|
|
indicates that outgoing messages should
|
178 |
|
|
bypass the standard routing facilities.
|
179 |
|
|
Instead, messages are directed to the appropriate network interface
|
180 |
|
|
according to the network portion of the destination address.
|
181 |
|
|
.Pp
|
182 |
|
|
.Dv SO_LINGER
|
183 |
|
|
controls the action taken when unsent messages
|
184 |
|
|
are queued on socket and a
|
185 |
|
|
.Xr close 2
|
186 |
|
|
is performed.
|
187 |
|
|
If the socket promises reliable delivery of data and
|
188 |
|
|
.Dv SO_LINGER
|
189 |
|
|
is set, the system will block the process on the
|
190 |
|
|
.Xr close 2
|
191 |
|
|
attempt until it is able to transmit the data or until it decides it
|
192 |
|
|
is unable to deliver the information (a timeout period measured in seconds,
|
193 |
|
|
termed the linger interval, is specified in the
|
194 |
|
|
.Fn setsockopt
|
195 |
|
|
call when
|
196 |
|
|
.Dv SO_LINGER
|
197 |
|
|
is requested).
|
198 |
|
|
If
|
199 |
|
|
.Dv SO_LINGER
|
200 |
|
|
is disabled and a
|
201 |
|
|
.Xr close 2
|
202 |
|
|
is issued, the system will process the close in a manner that allows
|
203 |
|
|
the process to continue as quickly as possible.
|
204 |
|
|
.Pp
|
205 |
|
|
The option
|
206 |
|
|
.Dv SO_BROADCAST
|
207 |
|
|
requests permission to send broadcast datagrams
|
208 |
|
|
on the socket.
|
209 |
|
|
Broadcast was a privileged operation in earlier versions of the system.
|
210 |
|
|
With protocols that support out-of-band data, the
|
211 |
|
|
.Dv SO_OOBINLINE
|
212 |
|
|
option requests that out-of-band data be placed in the normal data input
|
213 |
|
|
queue as received; it will then be accessible with
|
214 |
|
|
.Xr recv 2
|
215 |
|
|
or
|
216 |
|
|
.Xr read 2
|
217 |
|
|
calls without the
|
218 |
|
|
.Dv MSG_OOB
|
219 |
|
|
flag.
|
220 |
|
|
Some protocols always behave as if this option is set.
|
221 |
|
|
.Dv SO_SNDBUF
|
222 |
|
|
and
|
223 |
|
|
.Dv SO_RCVBUF
|
224 |
|
|
are options to adjust the normal
|
225 |
|
|
buffer sizes allocated for output and input buffers, respectively.
|
226 |
|
|
The buffer size may be increased for high-volume connections,
|
227 |
|
|
or may be decreased to limit the possible backlog of incoming data.
|
228 |
|
|
The system places an absolute limit on these values.
|
229 |
|
|
.Pp
|
230 |
|
|
.Dv SO_SNDLOWAT
|
231 |
|
|
is an option to set the minimum count for output operations.
|
232 |
|
|
Most output operations process all of the data supplied
|
233 |
|
|
by the call, delivering data to the protocol for transmission
|
234 |
|
|
and blocking as necessary for flow control.
|
235 |
|
|
Nonblocking output operations will process as much data as permitted
|
236 |
|
|
subject to flow control without blocking, but will process no data
|
237 |
|
|
if flow control does not allow the smaller of the low water mark value
|
238 |
|
|
or the entire request to be processed.
|
239 |
|
|
A
|
240 |
|
|
.Xr select 2
|
241 |
|
|
or
|
242 |
|
|
.Xr poll 2
|
243 |
|
|
operation testing the ability to write to a socket will return true
|
244 |
|
|
only if the low water mark amount could be processed.
|
245 |
|
|
The default value for
|
246 |
|
|
.Dv SO_SNDLOWAT
|
247 |
|
|
is set to a convenient size for network efficiency, often 1024.
|
248 |
|
|
.Dv SO_RCVLOWAT
|
249 |
|
|
is an option to set the minimum count for input operations.
|
250 |
|
|
In general, receive calls will block until any (non-zero) amount of data
|
251 |
|
|
is received, then return with the smaller of the amount available or the amount
|
252 |
|
|
requested.
|
253 |
|
|
The default value for
|
254 |
|
|
.Dv SO_RCVLOWAT
|
255 |
|
|
is 1.
|
256 |
|
|
If
|
257 |
|
|
.Dv SO_RCVLOWAT
|
258 |
|
|
is set to a larger value, blocking receive calls normally
|
259 |
|
|
wait until they have received the smaller of the low water mark value
|
260 |
|
|
or the requested amount.
|
261 |
|
|
Receive calls may still return less than the low water mark if an error
|
262 |
|
|
occurs, a signal is caught, or the type of data next in the receive queue
|
263 |
|
|
is different than that returned.
|
264 |
|
|
.Pp
|
265 |
|
|
.Dv SO_SNDTIMEO
|
266 |
|
|
is an option to set a timeout value for output operations.
|
267 |
|
|
It accepts a
|
268 |
|
|
.Li struct timeval
|
269 |
|
|
parameter with the number of seconds and microseconds
|
270 |
|
|
used to limit waits for output operations to complete.
|
271 |
|
|
If a send operation has blocked for this much time,
|
272 |
|
|
it returns with a partial count or with the error
|
273 |
|
|
.Er EWOULDBLOCK
|
274 |
|
|
if no data was sent.
|
275 |
|
|
In the current implementation, this timer is restarted each time additional
|
276 |
|
|
data are delivered to the protocol,
|
277 |
|
|
implying that the limit applies to output portions ranging in size
|
278 |
|
|
from the low water mark to the high water mark for output.
|
279 |
|
|
.Dv SO_RCVTIMEO
|
280 |
|
|
is an option to set a timeout value for input operations.
|
281 |
|
|
It accepts a
|
282 |
|
|
.Li struct timeval
|
283 |
|
|
parameter with the number of seconds and microseconds
|
284 |
|
|
used to limit waits for input operations to complete.
|
285 |
|
|
In the current implementation, this timer is restarted each time additional
|
286 |
|
|
data are received by the protocol,
|
287 |
|
|
and thus the limit is in effect an inactivity timer.
|
288 |
|
|
If a receive operation has been blocked for this much time without
|
289 |
|
|
receiving additional data, it returns with a short count
|
290 |
|
|
or with the error
|
291 |
|
|
.Er EWOULDBLOCK
|
292 |
|
|
if no data were received.
|
293 |
|
|
.Pp
|
294 |
|
|
Finally,
|
295 |
|
|
.Dv SO_TYPE
|
296 |
|
|
and
|
297 |
|
|
.Dv SO_ERROR
|
298 |
|
|
are options used only with
|
299 |
|
|
.Fn getsockopt .
|
300 |
|
|
.Dv SO_TYPE
|
301 |
|
|
returns the type of the socket, such as
|
302 |
|
|
.Dv SOCK_STREAM ;
|
303 |
|
|
it is useful for servers that inherit sockets on startup.
|
304 |
|
|
.Dv SO_ERROR
|
305 |
|
|
returns any pending error on the socket and clears the error status.
|
306 |
|
|
It may be used to check for asynchronous errors on connected
|
307 |
|
|
datagram sockets or for other asynchronous errors.
|
308 |
|
|
.Sh RETURN VALUES
|
309 |
|
|
A 0 is returned if the call succeeds, \-1 if it fails.
|
310 |
|
|
.Sh ERRORS
|
311 |
|
|
The call succeeds unless:
|
312 |
|
|
.Bl -tag -width Er
|
313 |
|
|
.It Bq Er EBADF
|
314 |
|
|
The argument
|
315 |
|
|
.Fa s
|
316 |
|
|
is not a valid descriptor.
|
317 |
|
|
.It Bq Er ENOTSOCK
|
318 |
|
|
The argument
|
319 |
|
|
.Fa s
|
320 |
|
|
is a file, not a socket.
|
321 |
|
|
.It Bq Er ENOPROTOOPT
|
322 |
|
|
The option is unknown at the level indicated.
|
323 |
|
|
.It Bq Er EFAULT
|
324 |
|
|
The address pointed to by
|
325 |
|
|
.Fa optval
|
326 |
|
|
is not in a valid part of the process address space.
|
327 |
|
|
For
|
328 |
|
|
.Fn getsockopt ,
|
329 |
|
|
this error may also be returned if
|
330 |
|
|
.Fa optlen
|
331 |
|
|
is not in a valid part of the process address space.
|
332 |
|
|
.El
|
333 |
|
|
.Sh SEE ALSO
|
334 |
|
|
.Xr connect 2 ,
|
335 |
|
|
.Xr ioctl 2 ,
|
336 |
|
|
.Xr poll 2 ,
|
337 |
|
|
.Xr select 2 ,
|
338 |
|
|
.Xr poll 2 ,
|
339 |
|
|
.Xr socket 2 ,
|
340 |
|
|
.Xr getprotoent 3 ,
|
341 |
|
|
.Xr protocols 5
|
342 |
|
|
.Sh BUGS
|
343 |
|
|
Several of the socket options should be handled at lower levels of the system.
|
344 |
|
|
.Sh HISTORY
|
345 |
|
|
The
|
346 |
|
|
.Fn getsockopt
|
347 |
|
|
system call appeared in
|
348 |
|
|
.Bx 4.2 .
|