1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="TCP/IP Library Reference"
23 HREF="tcpip-library-reference.html"><LINK
26 HREF="net-common-tcpip-manpages-getsockname.html"><LINK
29 HREF="net-common-tcpip-manpages-ioctl.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="net-common-tcpip-manpages-getsockname.html"
65 >Chapter 38. TCP/IP Library Reference</TD
71 HREF="net-common-tcpip-manpages-ioctl.html"
85 NAME="NET-COMMON-TCPIP-MANPAGES-GETSOCKOPT">getsockopt</H1
94 >GETSOCKOPT(2) System Calls Manual GETSOCKOPT(2)
97 getsockopt, setsockopt - get and set options on sockets
100 #include <sys/types.h>
101 #include <sys/socket.h>
104 getsockopt(int s, int level, int optname, void *optval,
108 setsockopt(int s, int level, int optname, const void *optval,
112 getsockopt() and setsockopt() manipulate the options associated with a
113 socket. Options may exist at multiple protocol levels; they are always
114 present at the uppermost ``socket'' level.
116 When manipulating socket options the level at which the option resides
117 and the name of the option must be specified. To manipulate options at
118 the socket level, level is specified as SOL_SOCKET. To manipulate
119 options at any other level the protocol number of the appropriate proto-
120 col controlling the option is supplied. For example, to indicate that an
121 option is to be interpreted by the TCP protocol, level should be set to
122 the protocol number of TCP; see getprotoent(3).
124 The parameters optval and optlen are used to access option values for
125 setsockopt(). For getsockopt() they identify a buffer in which the value
126 for the requested option(s) are to be returned. For getsockopt(), optlen
127 is a value-result parameter, initially containing the size of the buffer
128 pointed to by optval, and modified on return to indicate the actual size
129 of the value returned. If no option value is to be supplied or returned,
132 optname and any specified options are passed uninterpreted to the appro-
133 priate protocol module for interpretation. The include file
134 <sys/socket.h> contains definitions for socket level options, described
135 below. Options at other protocol levels vary in format and name; consult
136 the appropriate entries in section 4 of the manual.
138 Most socket-level options utilize an int parameter for optval. For
139 setsockopt(), the parameter should be non-zero to enable a boolean
140 option, or zero if the option is to be disabled. SO_LINGER uses a struct
141 linger parameter, defined in <sys/socket.h>, which specifies the desired
142 state of the option and the linger interval (see below). SO_SNDTIMEO and
143 SO_RCVTIMEO use a struct timeval parameter, defined in <sys/time.h>.
145 The following options are recognized at the socket level. Except as
146 noted, each may be examined with getsockopt() and set with setsockopt().
148 SO_DEBUG enables recording of debugging information
149 SO_REUSEADDR enables local address reuse
150 SO_REUSEPORT enables duplicate address and port bindings
151 SO_KEEPALIVE enables keep connections alive
152 SO_DONTROUTE enables routing bypass for outgoing messages
153 SO_LINGER linger on close if data present
154 SO_BROADCAST enables permission to transmit broadcast messages
155 SO_OOBINLINE enables reception of out-of-band data in band
156 SO_SNDBUF set buffer size for output
157 SO_RCVBUF set buffer size for input
158 SO_SNDLOWAT set minimum count for output
159 SO_RCVLOWAT set minimum count for input
160 SO_SNDTIMEO set timeout value for output
161 SO_RCVTIMEO set timeout value for input
162 SO_TYPE get the type of the socket (get only)
163 SO_ERROR get and clear error on the socket (get only)
165 SO_DEBUG enables debugging in the underlying protocol modules.
166 SO_REUSEADDR indicates that the rules used in validating addresses sup-
167 plied in a bind(2) call should allow reuse of local addresses.
168 SO_REUSEPORT allows completely duplicate bindings by multiple processes
169 if they all set SO_REUSEPORT before binding the port. This option per-
170 mits multiple instances of a program to each receive UDP/IP multicast or
171 broadcast datagrams destined for the bound port. SO_KEEPALIVE enables
172 the periodic transmission of messages on a connected socket. Should the
173 connected party fail to respond to these messages, the connection is con-
174 sidered broken and processes using the socket are notified via a SIGPIPE
175 signal when attempting to send data. SO_DONTROUTE indicates that outgo-
176 ing messages should bypass the standard routing facilities. Instead,
177 messages are directed to the appropriate network interface according to
178 the network portion of the destination address.
180 SO_LINGER controls the action taken when unsent messages are queued on
181 socket and a close(2) is performed. If the socket promises reliable
182 delivery of data and SO_LINGER is set, the system will block the process
183 on the close(2) attempt until it is able to transmit the data or until it
184 decides it is unable to deliver the information (a timeout period mea-
185 sured in seconds, termed the linger interval, is specified in the
186 setsockopt() call when SO_LINGER is requested). If SO_LINGER is disabled
187 and a close(2) is issued, the system will process the close in a manner
188 that allows the process to continue as quickly as possible.
190 The option SO_BROADCAST requests permission to send broadcast datagrams
191 on the socket. Broadcast was a privileged operation in earlier versions
192 of the system. With protocols that support out-of-band data, the
193 SO_OOBINLINE option requests that out-of-band data be placed in the nor-
194 mal data input queue as received; it will then be accessible with recv(2)
195 or read(2) calls without the MSG_OOB flag. Some protocols always behave
196 as if this option is set. SO_SNDBUF and SO_RCVBUF are options to adjust
197 the normal buffer sizes allocated for output and input buffers, respec-
198 tively. The buffer size may be increased for high-volume connections, or
199 may be decreased to limit the possible backlog of incoming data. The
200 system places an absolute limit on these values.
202 SO_SNDLOWAT is an option to set the minimum count for output operations.
203 Most output operations process all of the data supplied by the call,
204 delivering data to the protocol for transmission and blocking as neces-
205 sary for flow control. Nonblocking output operations will process as
206 much data as permitted subject to flow control without blocking, but will
207 process no data if flow control does not allow the smaller of the low
208 water mark value or the entire request to be processed. A select(2) or
209 poll(2) operation testing the ability to write to a socket will return
210 true only if the low water mark amount could be processed. The default
211 value for SO_SNDLOWAT is set to a convenient size for network efficiency,
212 often 1024. SO_RCVLOWAT is an option to set the minimum count for input
213 operations. In general, receive calls will block until any (non-zero)
214 amount of data is received, then return with the smaller of the amount
215 available or the amount requested. The default value for SO_RCVLOWAT is
216 1. If SO_RCVLOWAT is set to a larger value, blocking receive calls nor-
217 mally wait until they have received the smaller of the low water mark
218 value or the requested amount. Receive calls may still return less than
219 the low water mark if an error occurs, a signal is caught, or the type of
220 data next in the receive queue is different than that returned.
222 SO_SNDTIMEO is an option to set a timeout value for output operations.
223 It accepts a struct timeval parameter with the number of seconds and
224 microseconds used to limit waits for output operations to complete. If a
225 send operation has blocked for this much time, it returns with a partial
226 count or with the error EWOULDBLOCK if no data was sent. In the current
227 implementation, this timer is restarted each time additional data are
228 delivered to the protocol, implying that the limit applies to output por-
229 tions ranging in size from the low water mark to the high water mark for
230 output. SO_RCVTIMEO is an option to set a timeout value for input opera-
231 tions. It accepts a struct timeval parameter with the number of seconds
232 and microseconds used to limit waits for input operations to complete.
233 In the current implementation, this timer is restarted each time addi-
234 tional data are received by the protocol, and thus the limit is in effect
235 an inactivity timer. If a receive operation has been blocked for this
236 much time without receiving additional data, it returns with a short
237 count or with the error EWOULDBLOCK if no data were received.
239 Finally, SO_TYPE and SO_ERROR are options used only with getsockopt().
240 SO_TYPE returns the type of the socket, such as SOCK_STREAM; it is useful
241 for servers that inherit sockets on startup. SO_ERROR returns any pend-
242 ing error on the socket and clears the error status. It may be used to
243 check for asynchronous errors on connected datagram sockets or for other
247 A 0 is returned if the call succeeds, -1 if it fails.
250 The call succeeds unless:
252 [EBADF] The argument s is not a valid descriptor.
254 [ENOTSOCK] The argument s is a file, not a socket.
256 [ENOPROTOOPT] The option is unknown at the level indicated.
258 [EFAULT] The address pointed to by optval is not in a valid
259 part of the process address space. For getsockopt(),
260 this error may also be returned if optlen is not in a
261 valid part of the process address space.
264 connect(2), ioctl(2), poll(2), select(2), poll(2), socket(2),
265 getprotoent(3), protocols(5)
268 Several of the socket options should be handled at lower levels of the
272 The getsockopt() system call appeared in 4.2BSD.
274 BSD February 15, 1999 BSD
285 SUMMARY="Footer navigation table"
296 HREF="net-common-tcpip-manpages-getsockname.html"
314 HREF="net-common-tcpip-manpages-ioctl.html"
330 HREF="tcpip-library-reference.html"