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