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