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