move socket options to [gs]etsockopt, add new
[unix-history] / usr / src / lib / libc / sys / getsockopt.2
CommitLineData
efbcfcab
KM
1.\" Copyright (c) 1983 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
d25f1c67 5.\" @(#)getsockopt.2 6.3 (Berkeley) %G%
efbcfcab 6.\"
a66e777a 7.TH GETSOCKOPT 2 ""
efbcfcab
KM
8.UC 5
9.SH NAME
10getsockopt, setsockopt \- get and set options on sockets
11.SH SYNOPSIS
12.nf
13.ft B
14#include <sys/types.h>
15#include <sys/socket.h>
16.PP
17.ft B
18getsockopt(s, level, optname, optval, optlen)
19int s, level, optname;
20char *optval;
21int *optlen;
22.sp
23setsockopt(s, level, optname, optval, optlen)
24int s, level, optname;
25char *optval;
26int optlen;
27.fi
28.SH DESCRIPTION
29.I Getsockopt
30and
31.I setsockopt
32manipulate
33.I options
34associated with a socket. Options may exist at multiple
35protocol levels; they are always present at the uppermost
36``socket'' level.
37.PP
38When manipulating socket options the level at which the
39option resides and the name of the option must be specified.
40To manipulate options at the ``socket'' level,
41.I level
42is specified as SOL_SOCKET. To manipulate options at any
43other level the protocol number of the appropriate protocol
44controlling the option is supplied. For example,
45to indicate an option is to be interpreted by the TCP protocol,
46.I level
47should be set to the protocol number of TCP; see
48.IR getprotoent (3N).
49.PP
50The parameters
51.I optval
52and
53.I optlen
54are used to access option values for
55.IR setsockopt .
56For
57.I getsockopt
58they identify a buffer in which the value for the
59requested option(s) are to be returned. For
60.IR getsockopt ,
61.I optlen
62is a value-result parameter, initially containing the
63size of the buffer pointed to by
64.IR optval ,
65and modified on return to indicate the actual size of
66the value returned. If no option value is
67to be supplied or returned,
68.I optval
69may be supplied as 0.
70.PP
71.I Optname
72and any specified options are passed uninterpreted to the appropriate
73protocol module for interpretation.
74The include file
75.RI < sys/socket.h >
d25f1c67 76contains definitions for ``socket'' level options, described below.
efbcfcab 77Options at other protocol levels vary in format and
d25f1c67
MK
78name; consult the appropriate entries in section (4P).
79.PP
80Most socket-level options take an
81.I int
82parameter for
83.IR optval .
84For
85.IR setsockopt ,
86the parameter should non-zero to enable a boolean option,
87or zero if the option is to be disabled.
88SO_LINGER uses a
89.I struct linger
90parameter, defined in
91.RI < sys/socket.h >,
92which specifies the desired state of the option and the
93linger interval (see below).
94.PP
95The following options are recognized at the socket level.
96Except as noted, each may be examined with
97.I getsockopt
98and set with
99.IR setsockopt .
100.PP
101.RS
102.ta \w'SO_BROADCAST\ \ \ \ 'u
103.nf
104SO_DEBUG toggle recording of debugging information
105SO_REUSEADDR toggle local address reuse
106SO_KEEPALIVE toggle keep connections alive
107SO_DONTROUTE toggle routing bypass for outgoing messages
108SO_LINGER linger on close if data present
109SO_BROADCAST toggle permission to transmit broadcast messages
110SO_OOBINLINE toggle reception of out-of-band data in band
111SO_SNDBUF set buffer size for output
112SO_RCVBUF set buffer size for input
113SO_TYPE get the type of the socket (get only)
114SO_ERROR get and clear error on the socket (get only)
115.fi
116.RE
117.PP
118SO_DEBUG enables debugging in the underlying protocol modules.
119SO_REUSEADDR indicates that the rules used in validating addresses supplied
120in a
121.IR bind (2)
122call should allow reuse of local addresses. SO_KEEPALIVE enables the
123periodic transmission of messages on a connected socket. Should the
124connected party fail to respond to these messages, the connection is
125considered broken and processes using the socket are notified via a
126SIGPIPE signal. SO_DONTROUTE indicates that outgoing messages should
127bypass the standard routing facilities. Instead, messages are directed
128to the appropriate network interface according to the network portion
129of the destination address.
130.PP
131SO_LINGER controls the action taken when unsent messags
132are queued on socket and a
133.IR close (2)
134is performed.
135If the socket promises reliable delivery of data and SO_LINGER is set,
136the system will block the process on the
137.I close
138attempt until it is able to transmit the data or until it decides it
139is unable to deliver the information (a timeout period, termed the
140linger interval, is specified in the
141.IR setsockopt
142call when SO_LINGER is requested).
143If SO_LINGER is disabled and a
144.I close
145is issued, the system will process the close in a manner that allows
146the process to continue as quickly as possible.
147.PP
148The option SO_BROADCAST requests permission to send broadcast datagrams
149on the socket.
150Broadcast was a privileged operation in earlier versions of the system.
151With protocols that support out-of-band data, the SO_OOBINLINE option
152requests that out-of-band data be placed in the normal data input queue
153as received; it will then be accessible with
154.I recv
155or
156.I read
157calls without the MSG_OOB flag.
158SO_SNDBUF and SO_RCVBUF are options to adjust the normal
159buffer sizes allocated for output and input buffers, respectively.
160The buffer size may be increased for high-volume connections,
161or may be decreased to limit the possible backlog of incoming data.
162The system places an absolute limit on these values.
163Finally, SO_TYPE and SO_ERROR are options used only with
164.IR setsockopt .
165SO_TYPE returns the type of the socket, such as SOCK_STREAM;
166it is useful for servers that inherit sockets on startup.
167SO_ERROR returns any pending error on the socket and clears
168the error status.
169It may be used to check for asynchronous errors on connected
170datagram sockets or for other asynchronous errors.
efbcfcab
KM
171.SH "RETURN VALUE"
172A 0 is returned if the call succeeds, \-1 if it fails.
173.SH ERRORS
174The call succeeds unless:
175.TP 20
176[EBADF]
177The argument \fIs\fP is not a valid descriptor.
178.TP 20
179[ENOTSOCK]
180The argument \fIs\fP is a file, not a socket.
181.TP 20
182[ENOPROTOOPT]
d25f1c67 183The option is unknown at the level indicated.
efbcfcab
KM
184.TP 20
185[EFAULT]
fd690c8b
KM
186The address pointed to by
187.I optval
188is not in a valid part of the process address space.
189For
190.IR getsockopt ,
191this error may also be returned if
192.I optlen
193is not in a valid part of the process address space.
efbcfcab 194.SH "SEE ALSO"
d25f1c67
MK
195ioctl(2), socket(2), getprotoent(3N)
196.SH BUGS
197Several of the socket options should be handled at lower levels of the system.