xref: /minix3/lib/libc/sys/getsockopt.2 (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1.\"	$NetBSD: getsockopt.2,v 1.35 2012/01/25 00:28:35 christos 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. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)getsockopt.2	8.4 (Berkeley) 5/2/95
31.\"
32.Dd January 23, 2012
33.Dt GETSOCKOPT 2
34.Os
35.Sh NAME
36.Nm getsockopt ,
37.Nm setsockopt
38.Nd get and set options on sockets
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In sys/socket.h
43.Ft int
44.Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen"
45.Ft int
46.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
47.Sh DESCRIPTION
48.Fn getsockopt
49and
50.Fn setsockopt
51manipulate the
52.Em options
53associated with a socket.
54Options may exist at multiple
55protocol levels; they are always present at the uppermost
56.Dq socket
57level.
58.Pp
59When manipulating socket options the level at which the
60option resides and the name of the option must be specified.
61To manipulate options at the socket level,
62.Fa level
63is specified as
64.Dv SOL_SOCKET .
65To manipulate options at any
66other level the protocol number of the appropriate protocol
67controlling the option is supplied.
68For example, to indicate that an option is to be interpreted by the
69.Tn TCP
70protocol,
71.Fa level
72should be set to the protocol number of
73.Tn TCP ;
74see
75.Xr getprotoent 3 .
76.Pp
77The parameters
78.Fa optval
79and
80.Fa optlen
81are used to access option values for
82.Fn setsockopt .
83For
84.Fn getsockopt
85they identify a buffer in which the value for the
86requested option(s) are to be returned.
87For
88.Fn getsockopt ,
89.Fa optlen
90is a value-result parameter, initially containing the
91size of the buffer pointed to by
92.Fa optval ,
93and modified on return to indicate the actual size of
94the value returned.
95If no option value is to be supplied or returned,
96.Fa optval
97may be
98.Dv 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, including:
111.Xr clnp 4 ,
112.Xr faith 4 ,
113.Xr icmp6 4 ,
114.Xr ip 4 ,
115.Xr ip6 4 ,
116.Xr ipsec 4 ,
117.Xr multicast 4 ,
118.Xr pim 4 ,
119.Xr route 4 ,
120.Xr tcp 4 ,
121.Xr tp 4 ,
122and
123.Xr unix 4 .
124.Pp
125Most socket-level options use an
126.Fa int
127parameter for
128.Fa optval .
129For
130.Fn setsockopt ,
131the parameter should be non-zero to enable a boolean option,
132or zero if the option is to be disabled.
133.Dv SO_LINGER
134uses a
135.Fa struct linger
136parameter, defined in
137.Ao Pa sys/socket.h Ac ,
138which specifies the desired state of the option and the
139linger interval (see below).
140.Dv SO_SNDTIMEO
141and
142.Dv SO_RCVTIMEO
143use a
144.Fa struct timeval
145parameter, defined in
146.Ao Pa sys/time.h Ac .
147.Pp
148The following options are recognized at the socket level.
149Except as noted, each may be examined with
150.Fn getsockopt
151and set with
152.Fn setsockopt .
153.Bl -column SO_ACCEPTFILTER data -offset indent
154.It Dv SO_DEBUG Ta "enables recording of debugging information"
155.It Dv SO_REUSEADDR Ta "enables local address reuse"
156.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
157.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
158.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
159.It Dv SO_LINGER  Ta "linger on close if data present"
160.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
161.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
162.It Dv SO_SNDBUF Ta "set buffer size for output"
163.It Dv SO_RCVBUF Ta "set buffer size for input"
164.It Dv SO_SNDLOWAT Ta "set minimum count for output"
165.It Dv SO_RCVLOWAT Ta "set minimum count for input"
166.It Dv SO_SNDTIMEO Ta "set timeout value for output"
167.It Dv SO_RCVTIMEO Ta "set timeout value for input"
168.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams"
169.It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket"
170.It Dv SO_NOSIGPIPE Ta
171controls generation of
172.Dv SIGPIPE
173for the socket
174.It Dv SO_TYPE Ta "get the type of the socket (get only)"
175.It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
176.El
177.Pp
178.Dv SO_DEBUG
179enables debugging in the underlying protocol modules.
180.Dv SO_REUSEADDR
181indicates that the rules used in validating addresses supplied
182in a
183.Xr bind 2
184call should allow reuse of local addresses.
185.Dv SO_REUSEPORT
186allows completely duplicate bindings by multiple processes
187if they all set
188.Dv SO_REUSEPORT
189before binding the port.
190This option permits multiple instances of a program to each
191receive UDP/IP multicast or broadcast datagrams destined for the bound port.
192.Dv SO_KEEPALIVE
193enables the
194periodic transmission of messages on a connected socket.
195Should the connected party fail to respond to these messages, the connection
196is considered broken and processes using the socket are notified via a
197.Dv SIGPIPE
198signal when attempting to send data.
199.Dv SO_DONTROUTE
200indicates that outgoing messages should
201bypass the standard routing facilities.
202Instead, messages are directed to the appropriate network interface
203according to the network portion of the destination address.
204.Pp
205.Dv SO_LINGER
206controls the action taken when unsent messages
207are queued on socket and a
208.Xr close 2
209is performed.
210If the socket promises reliable delivery of data and
211.Dv SO_LINGER
212is set, the system will block the process on the
213.Xr close 2
214attempt until it is able to transmit the data or until it decides it
215is unable to deliver the information (a timeout period, measured in
216seconds, termed the linger interval, is specified in the
217.Fn setsockopt
218call when
219.Dv SO_LINGER
220is requested).
221If
222.Dv SO_LINGER
223is disabled and a
224.Xr close 2
225is issued, the system will process the close in a manner that allows
226the process to continue as quickly as possible.
227.Pp
228The option
229.Dv SO_BROADCAST
230requests permission to send broadcast datagrams
231on the socket.
232Broadcast was a privileged operation in earlier versions of the system.
233With protocols that support out-of-band data, the
234.Dv SO_OOBINLINE
235option
236requests that out-of-band data be placed in the normal data input queue
237as received; it will then be accessible with
238.Xr recv 2
239or
240.Xr read 2
241calls without the
242.Dv MSG_OOB
243flag.
244Some protocols always behave as if this option is set.
245.Dv SO_SNDBUF
246and
247.Dv SO_RCVBUF
248are options to adjust the normal
249buffer sizes allocated for output and input buffers, respectively.
250The buffer size may be increased for high-volume connections,
251or may be decreased to limit the possible backlog of incoming data.
252The system places an absolute limit on these values.
253.Pp
254.Dv SO_SNDLOWAT
255is an option to set the minimum count for output operations.
256Most output operations process all of the data supplied
257by the call, delivering data to the protocol for transmission
258and blocking as necessary for flow control.
259Nonblocking output operations will process as much data as permitted
260subject to flow control without blocking, but will process no data
261if flow control does not allow the smaller of the low water mark value
262or the entire request to be processed.
263A
264.Xr select 2
265or
266.Xr poll 2
267operation testing the ability to write to a socket will return true
268only if the low water mark amount could be processed.
269The default value for
270.Dv SO_SNDLOWAT
271is set to a convenient size for network efficiency, often 1024.
272.Dv SO_RCVLOWAT
273is an option to set the minimum count for input operations.
274In general, receive calls will block until any (non-zero) amount of data
275is received, then return with the smaller of the amount available or the amount
276requested.
277The default value for
278.Dv SO_RCVLOWAT
279is 1.
280If
281.Dv SO_RCVLOWAT
282is set to a larger value, blocking receive calls normally
283wait until they have received the smaller of the low water mark value
284or the requested amount.
285Receive calls may still return less than the low water mark if an error
286occurs, a signal is caught, or the type of data next in the receive queue
287is different than that returned.
288.Pp
289.Dv SO_SNDTIMEO
290is an option to set a timeout value for output operations.
291It accepts a
292.Fa struct timeval
293parameter with the number of seconds and microseconds
294used to limit waits for output operations to complete.
295If a send operation has blocked for this much time,
296it returns with a partial count
297or with the error
298.Er EAGAIN
299if no data were sent.
300In the current implementation, this timer is restarted each time additional
301data are delivered to the protocol,
302implying that the limit applies to output portions ranging in size
303from the low water mark to the high water mark for output.
304.Dv SO_RCVTIMEO
305is an option to set a timeout value for input operations.
306It accepts a
307.Fa struct timeval
308parameter with the number of seconds and microseconds
309used to limit waits for input operations to complete.
310In the current implementation, this timer is restarted each time additional
311data are received by the protocol,
312and thus the limit is in effect an inactivity timer.
313If a receive operation has been blocked for this much time without
314receiving additional data, it returns with a short count
315or with the error
316.Er EAGAIN
317if no data were received.
318.Pp
319If the
320.Dv SO_TIMESTAMP
321option is enabled on a
322.Dv SOCK_DGRAM
323socket, the
324.Xr recvmsg 2
325call will return a timestamp corresponding to when the datagram was
326received.
327The msg_control field in the msghdr structure points to a buffer
328that contains a cmsghdr structure followed by a struct timeval.
329The cmsghdr fields have the following values:
330.Bd -literal
331cmsg_len = sizeof(struct timeval)
332cmsg_level = SOL_SOCKET
333cmsg_type = SCM_TIMESTAMP
334.Ed
335.Pp
336.Dv SO_ACCEPTFILTER
337places an
338.Xr accept_filter 9
339on the socket, which will filter incoming connections on a listening
340socket before being presented for
341.Xr accept 2 .
342The
343.Fn setsockopt
344system call will fail if the socket already has a filter set, and
345.Xr listen 2
346must be called on the socket before trying to install a filter.
347The
348.Fa optval
349argument
350should point to a
351.Fa struct accept_filter_arg
352that will select and configure the
353.Xr accept_filter 9 ,
354defined as follows:
355.Bd -literal
356struct  accept_filter_arg {
357        char    af_name[16];
358        char    af_arg[256-16];
359};
360.Ed
361.Lp
362The
363.Fa af_name
364argument should be filled with the name of the accept filter
365that the application wishes to place on the listening socket.
366The optional argument
367.Fa af_arg
368can be passed to the accept filter specified by
369.Fa af_name
370to provide additional configuration options at attach time.
371Passing in an
372.Fa optval
373of
374.Dv NULL
375will remove the filter.
376.Pp
377Finally,
378.Dv SO_TYPE
379and
380.Dv SO_ERROR
381are options used only with
382.Fn getsockopt .
383.Dv SO_TYPE
384returns the type of the socket, such as
385.Dv SOCK_STREAM ;
386it is useful for servers that inherit sockets on startup.
387.Dv SO_ERROR
388returns any pending error on the socket and clears
389the error status.
390It may be used to check for asynchronous errors on connected
391datagram sockets or for other asynchronous errors.
392.Sh RETURN VALUES
393A 0 is returned if the call succeeds, \-1 if it fails.
394.Sh ERRORS
395The call succeeds unless:
396.Bl -tag -width Er
397.It Bq Er EBADF
398The argument
399.Fa s
400is not a valid descriptor.
401.It Bq Er EFAULT
402The address pointed to by
403.Fa optval
404is not in a valid part of the process address space.
405For
406.Fn getsockopt ,
407this error may also be returned if
408.Fa optlen
409is not in a valid part of the process address space.
410.It Bq Er EINVAL
411The socket
412.Fa s
413was not suitable for installing an
414.Xr accept_filter 9 .
415.It Bq Er ENOPROTOOPT
416The option is unknown at the level indicated.
417.It Bq Er ENOTSOCK
418The argument
419.Fa s
420is a file, not a socket.
421.El
422.Sh SEE ALSO
423.Xr ioctl 2 ,
424.Xr poll 2 ,
425.Xr select 2 ,
426.Xr socket 2 ,
427.Xr getprotoent 3 ,
428.Xr clnp 4 ,
429.Xr faith 4 ,
430.Xr icmp6 4 ,
431.Xr ip 4 ,
432.Xr ip6 4 ,
433.Xr ipsec 4 ,
434.Xr multicast 4 ,
435.Xr pim 4 ,
436.Xr route 4 ,
437.Xr tcp 4 ,
438.Xr tp 4 ,
439.Xr unix 4 ,
440.Xr protocols 5 ,
441.Xr accept_filter 9
442.Sh HISTORY
443The
444.Fn getsockopt
445system call appeared in
446.Bx 4.2 .
447.Sh BUGS
448Several of the socket options should be handled at lower levels of the system.
449