xref: /original-bsd/lib/libc/sys/getsockopt.2 (revision ba762ddc)
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)getsockopt.2	6.9 (Berkeley) 05/01/91
7.\"
8.\" Copyright (c) 1983, 1991 The Regents of the University of California.
9.\" All rights reserved.
10.\"
11.\" %sccs.include.redist.man%
12.\"
13.\"     @(#)getsockopt.2	6.9 (Berkeley) 05/01/91
14.\"
15.Dd
16.Dt GETSOCKOPT 2
17.Os BSD 4.3r
18.Sh NAME
19.Nm getsockopt ,
20.Nm setsockopt
21.Nd get and set options on sockets
22.Sh SYNOPSIS
23.Fd #include <sys/types.h>
24.Fd #include <sys/socket.h>
25.Ft int
26.Fn getsockopt "int s" "int level" "int optname" "void *optval" "int *optlen"
27.Ft int
28.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "int optlen"
29.Sh DESCRIPTION
30.Fn Getsockopt
31and
32.Fn setsockopt
33manipulate the
34.Em options
35associated with a socket.  Options may exist at multiple
36protocol levels; they are always present at the uppermost
37.Dq socket
38level.
39.Pp
40When manipulating socket options the level at which the
41option resides and the name of the option must be specified.
42To manipulate options at the socket level,
43.Fa level
44is specified as
45.Dv SOL_SOCKET .
46To manipulate options at any
47other level the protocol number of the appropriate protocol
48controlling the option is supplied.  For example,
49to indicate that an option is to be interpreted by the
50.Tn TCP
51protocol,
52.Fa level
53should be set to the protocol number of
54.Tn TCP ;
55see
56.Xr getprotoent 3 .
57.Pp
58The parameters
59.Fa optval
60and
61.Fa optlen
62are used to access option values for
63.Fn setsockopt .
64For
65.Fn getsockopt
66they identify a buffer in which the value for the
67requested option(s) are to be returned.  For
68.Fn getsockopt ,
69.Fa optlen
70is a value-result parameter, initially containing the
71size of the buffer pointed to by
72.Fa optval ,
73and modified on return to indicate the actual size of
74the value returned.  If no option value is
75to be supplied or returned,
76.Fa optval
77may be NULL.
78.Pp
79.Fa Optname
80and any specified options are passed uninterpreted to the appropriate
81protocol module for interpretation.
82The include file
83.Ao Pa sys/socket.h Ac
84contains definitions for
85socket level options, described below.
86Options at other protocol levels vary in format and
87name; consult the appropriate entries in
88section
894 of the manual.
90.Pp
91Most socket-level options utilize an
92.Fa int
93parameter for
94.Fa optval .
95For
96.Fn setsockopt ,
97the parameter should be non-zero to enable a boolean option,
98or zero if the option is to be disabled.
99.Dv SO_LINGER
100uses a
101.Fa struct linger
102parameter, defined in
103.Ao Pa sys/socket.h Ac ,
104which specifies the desired state of the option and the
105linger interval (see below).
106.Dv SO_SNDTIMEO
107and
108.Dv SO_RCVTIMEO
109use a
110.Fa struct timeval
111parameter, defined in
112.Ao Pa sys/time.h Ac .
113.Pp
114The following options are recognized at the socket level.
115Except as noted, each may be examined with
116.Fn getsockopt
117and set with
118.Fn setsockopt .
119.Bl -column SO_OOBINLINE -offset indent
120.It Dv SO_DEBUG Ta "enables recording of debugging information"
121.It Dv SO_REUSEADDR Ta "enables local address reuse"
122.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
123.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
124.It Dv SO_LINGER  Ta "linger on close if data present"
125.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
126.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
127.It Dv SO_SNDBUF Ta "set buffer size for output"
128.It Dv SO_RCVBUF Ta "set buffer size for input"
129.It Dv SO_SNDLOWAT Ta "set minimum count for output"
130.It Dv SO_RCVLOWAT Ta "set minimum count for input"
131.It Dv SO_SNDTIMEO Ta "set timeout value for output"
132.It Dv SO_RCVTIMEO Ta "set timeout value for input"
133.It Dv SO_TYPE Ta "get the type of the socket (get only)"
134.It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
135.El
136.Pp
137.Dv SO_DEBUG
138enables debugging in the underlying protocol modules.
139.Dv SO_REUSEADDR
140indicates that the rules used in validating addresses supplied
141in a
142.Xr bind 2
143call should allow reuse of local addresses.
144.Dv SO_KEEPALIVE
145enables the
146periodic transmission of messages on a connected socket.  Should the
147connected party fail to respond to these messages, the connection is
148considered broken and processes using the socket are notified via a
149.Dv SIGPIPE
150signal when attempting to send data.
151.Dv SO_DONTROUTE
152indicates that outgoing messages should
153bypass the standard routing facilities.  Instead, messages are directed
154to the appropriate network interface according to the network portion
155of the destination address.
156.Pp
157.Dv SO_LINGER
158controls the action taken when unsent messags
159are queued on socket and a
160.Xr close 2
161is performed.
162If the socket promises reliable delivery of data and
163.Dv SO_LINGER is set,
164the system will block the process on the
165.Xr close
166attempt until it is able to transmit the data or until it decides it
167is unable to deliver the information (a timeout period, termed the
168linger interval, is specified in the
169.Fn setsockopt
170call when
171.Dv SO_LINGER
172is requested).
173If
174.Dv SO_LINGER
175is disabled and a
176.Xr close
177is issued, the system will process the close in a manner that allows
178the process to continue as quickly as possible.
179.Pp
180The option
181.Dv SO_BROADCAST
182requests permission to send broadcast datagrams
183on the socket.
184Broadcast was a privileged operation in earlier versions of the system.
185With protocols that support out-of-band data, the
186.Dv SO_OOBINLINE
187option
188requests that out-of-band data be placed in the normal data input queue
189as received; it will then be accessible with
190.Xr recv
191or
192.Xr read
193calls without the
194.Dv MSG_OOB
195flag.
196Some protocols always behave as if this option is set.
197.Dv SO_SNDBUF
198and
199.Dv SO_RCVBUF
200are options to adjust the normal
201buffer sizes allocated for output and input buffers, respectively.
202The buffer size may be increased for high-volume connections,
203or may be decreased to limit the possible backlog of incoming data.
204The system places an absolute limit on these values.
205.Pp
206.Dv SO_SNDLOWAT
207is an option to set the minimum count for output operations.
208Most output operations process all of the data supplied
209by the call, delivering data to the protocol for transmission
210and blocking as necessary for flow control.
211Nonblocking output operations will process as much data as permitted
212subject to flow control without blocking, but will process no data
213if flow control does not allow the smaller of the low water mark value
214or the entire request to be processed.
215A
216.Xr select 2
217operation testing the ability to write to a socket will return true
218only if the low water mark amount could be processed.
219The default value for
220.Dv SO_SNDLOWAT
221is set to a convenient size for network efficiency, often 1024.
222.Dv SO_RCVLOWAT
223is an option to set the minimum count for input operations.
224In general, receive calls will block until any (non-zero) amount of data
225is received, then return with smaller of the amount available or the amount
226requested.
227The default value for
228.Dv SO_SNDLOWAT
229is 1.
230If
231.Dv SO_SNDLOWAT
232is set to a larger value, blocking receive calls normally
233wait until they have received the smaller of the low water mark value
234or the requested amount.
235Receive calls may still return less than the low water mark if an error
236occurs, a signal is caught, or the type of data next in the receive queue
237is different than that returned.
238.Pp
239.Dv SO_SNDTIMEO
240is an option to set a timeout value for output operations.
241It accepts a
242.Fa struct timeval
243parameter with the number of seconds and microseconds
244used to limit waits for output operations to complete.
245If a send operation has blocked for this much time,
246it returns with a partial count
247or with the error
248.Er EWOULDBLOCK
249if no data were sent.
250In the current implementation, this timer is restarted each time additional
251data are delivered to the protocol,
252implying that the limit applies to output portions ranging in size
253from the low water mark to the high water mark for output.
254.Dv SO_RCVTIMEO
255is an option to set a timeout value for input operations.
256It accepts a
257.Fa struct timeval
258parameter with the number of seconds and microseconds
259used to limit waits for input operations to complete.
260In the current implementation, this timer is restarted each time additional
261data are received by the protocol,
262and thus the limit is in effect an inactivity timer.
263If a receive operation has been blocked for this much time without
264receiving additional data, it returns with a short count
265or with the error
266.Er EWOULDBLOCK
267if no data were received.
268.Pp
269Finally,
270.Dv SO_TYPE
271and
272.Dv SO_ERROR
273are options used only with
274.Fn setsockopt .
275.Dv SO_TYPE
276returns the type of the socket, such as
277.Dv SOCK_STREAM ;
278it is useful for servers that inherit sockets on startup.
279.Dv SO_ERROR
280returns any pending error on the socket and clears
281the error status.
282It may be used to check for asynchronous errors on connected
283datagram sockets or for other asynchronous errors.
284.Sh RETURN VALUES
285A 0 is returned if the call succeeds, -1 if it fails.
286.Sh ERRORS
287The call succeeds unless:
288.Bl -tag -width ENOPROTOOPTAA
289.It Bq Er EBADF
290The argument
291.Fa s
292is not a valid descriptor.
293.It Bq Er ENOTSOCK
294The argument
295.Fa s
296is a file, not a socket.
297.It Bq Er ENOPROTOOPT
298The option is unknown at the level indicated.
299.It Bq Er EFAULT
300The address pointed to by
301.Fa optval
302is not in a valid part of the process address space.
303For
304.Fn getsockopt ,
305this error may also be returned if
306.Fa optlen
307is not in a valid part of the process address space.
308.El
309.Sh SEE ALSO
310.Xr ioctl 2 ,
311.Xr socket 2 ,
312.Xr getprotoent 3
313.Xr protocols 5
314.Sh BUGS
315Several of the socket options should be handled at lower levels of the system.
316.Sh HISTORY
317The
318.Nm
319system call appeared in
320.Bx 4.2 .
321