1 Sockets Library Functions getsockopt(3SOCKET) 2 3 4 5 NAME 6 getsockopt, setsockopt - get and set options on sockets 7 8 SYNOPSIS 9 cc [ flag ... ] file ... -lsocket -lnsl [ library ... ] 10 #include <sys/types.h> 11 #include <sys/socket.h> 12 13 int getsockopt(int s, int level, int optname, void *optval, 14 int *optlen); 15 16 17 int setsockopt(int s, int level, int optname, const void *optval, 18 int optlen); 19 20 21 DESCRIPTION 22 The getsockopt() and setsockopt() functions manipulate 23 options associated with a socket. Options may exist at mul- 24 tiple protocol levels; they are always present at the upper- 25 most "socket" level. 26 27 28 The *level* argument specifies the protocol level at which 29 the option resides. To manipulate options at the socket 30 level, specify the *level* argument as *SOL_SOCKET*. To 31 manipulate options at the protocol level, supply the 32 appropriate protocol number for the protocol controlling the 33 option. For example, to indicate that an option will be 34 interpreted by TCP, set *level* to the protocol number of 35 TCP, as defined in the *<netinet/in.h>* header, or as 36 determined by using *getprotobyname(3SOCKET)*. Some socket 37 protocol families may also define additional levels, such as 38 *SOL_ROUTE*. Only socket-level options are described here. 39 40 41 The parameters optval and optlen are used to access option 42 values for setsockopt(). For getsockopt(), they identify a 43 buffer in which the value(s) for the requested option(s) are 44 to be returned. For getsockopt(), optlen is a value-result 45 parameter, initially containing the size of the buffer 46 pointed to by optval, and modified on return to indicate the 47 actual size of the value returned. Use a 0 optval if no 48 option value is to be supplied or returned. 49 50 51 The optname and any specified options are passed uninter- 52 preted to the appropriate protocol module for interpreta- 53 tion. The include file <sys/socket.h> contains definitions 54 for the socket-level options described below. Options at 55 other protocol levels vary in format and name. 56 57 58 Most socket-level options take an int for optval. For set- 59 sockopt(), the optval parameter should be non-zero to enable 60 61 62 63 SunOS 5.11 Last change: 7 Aug 2008 1 64 65 66 67 68 69 70 Sockets Library Functions getsockopt(3SOCKET) 71 72 73 74 a boolean option, or zero if the option is to be disabled. 75 SO_LINGER uses a struct linger parameter that specifies the 76 desired state of the option and the linger interval. struct 77 linger is defined in <sys/socket.h>. struct linger contains 78 the following members: 79 80 l_onoff on = 1/off = 0 81 82 83 l_linger linger time, in seconds 84 85 86 87 The following options are recognized at the socket level. 88 Except as noted, each may be examined with getsockopt() and 89 set with setsockopt(). 90 91 SO_DEBUG enable/disable recording of debugging 92 information 93 94 95 SO_REUSEADDR enable/disable local address reuse 96 97 98 SO_KEEPALIVE enable/disable keep connections alive 99 100 101 SO_DONTROUTE enable/disable routing bypass for outgo- 102 ing messages 103 104 105 SO_LINGER linger on close if data is present 106 107 108 SO_BROADCAST enable/disable permission to transmit 109 broadcast messages 110 111 112 SO_OOBINLINE enable/disable reception of out-of-band 113 data in band 114 115 116 SO_SNDBUF set buffer size for output 117 118 119 SO_RCVBUF set buffer size for input 120 121 122 SO_DGRAM_ERRIND application wants delayed error 123 124 125 126 127 128 129 SunOS 5.11 Last change: 7 Aug 2008 2 130 131 132 133 134 135 136 Sockets Library Functions getsockopt(3SOCKET) 137 138 139 140 SO_TIMESTAMP enable/disable reception of timestamp 141 with datagrams 142 143 144 SO_EXCLBIND enable/disable exclusive binding of the 145 socket 146 147 148 SO_TYPE get the type of the socket (get only) 149 150 151 SO_ERROR get and clear error on the socket (get 152 only) 153 154 155 SO_MAC_EXEMPT get or set mandatory access control on 156 the socket. This option is available only 157 when the system is configured with 158 Trusted Extensions. 159 160 161 SO_ALLZONES bypass zone boundaries (privileged). 162 163 164 SO_DOMAIN get the domain used in the socket (get 165 only) 166 167 168 SO_PROTOTYPE for socket in domains PF_INET and 169 PF_INET6, get the underlying protocol 170 number used in the socket. For socket in 171 domain PF_ROUTE, get the address family 172 used in the socket. 173 174 175 176 The SO_DEBUG option enables debugging in the underlying pro- 177 tocol modules. The SO_REUSEADDR option indicates that the 178 rules used in validating addresses supplied in a 179 bind(3SOCKET) call should allow reuse of local addresses. 180 The SO_KEEPALIVE option enables the periodic transmission of 181 messages on a connected socket. If the connected party fails 182 to respond to these messages, the connection is considered 183 broken and threads using the socket are notified using a 184 SIGPIPE signal. The SO_DONTROUTE option indicates that out- 185 going messages should bypass the standard routing facili- 186 ties. Instead, messages are directed to the appropriate net- 187 work interface according to the network portion of the des- 188 tination address. 189 190 191 192 193 194 195 SunOS 5.11 Last change: 7 Aug 2008 3 196 197 198 199 200 201 202 Sockets Library Functions getsockopt(3SOCKET) 203 204 205 206 The SO_LINGER option controls the action taken when unsent 207 messages are queued on a socket and a close(2) is performed. 208 If the socket promises reliable delivery of data and 209 SO_LINGER is set, the system will block the thread on the 210 close() attempt until it is able to transmit the data or 211 until it decides it is unable to deliver the information (a 212 timeout period, termed the linger interval, is specified in 213 the setsockopt() call when SO_LINGER is requested). If 214 SO_LINGER is disabled and a close() is issued, the system 215 will process the close() in a manner that allows the thread 216 to continue as quickly as possible. 217 218 219 The option SO_BROADCAST requests permission to send broad- 220 cast datagrams on the socket. With protocols that support 221 out-of-band data, the SO_OOBINLINE option requests that 222 out-of-band data be placed in the normal data input queue as 223 received; it will then be accessible with recv() or read() 224 calls without the MSG_OOB flag. 225 226 227 The SO_SNDBUF and SO_RCVBUF options adjust the normal buffer 228 sizes allocated for output and input buffers, respectively. 229 The buffer size may be increased for high-volume connections 230 or may be decreased to limit the possible backlog of incom- 231 ing data. The maximum buffer size for UDP is determined by 232 the value of the ndd variable udp_max_buf. The maximum 233 buffer size for TCP is determined the value of the ndd vari- 234 able tcp_max_buf. Use the ndd(1M) utility to determine the 235 current default values. See the Solaris Tunable Parameters 236 Reference Manual for information on setting the values of 237 udp_max_buf and tcp_max_buf. At present, lowering SO_RCVBUF 238 on a TCP connection after it has been established has no 239 effect. 240 241 242 By default, delayed errors (such as ICMP port unreachable 243 packets) are returned only for connected datagram sockets. 244 The SO_DGRAM_ERRIND option makes it possible to receive 245 errors for datagram sockets that are not connected. When 246 this option is set, certain delayed errors received after 247 completion of a sendto() or sendmsg() operation will cause a 248 subsequent sendto() or sendmsg() operation using the same 249 destination address (to parameter) to fail with the 250 appropriate error. See send(3SOCKET). 251 252 253 If the SO_TIMESTAMP option is enabled on a SO_DGRAM or a 254 SO_RAW socket, the recvmsg(3XNET) call will return a times- 255 tamp in the native data format, corresponding to when the 256 datagram was received. 257 258 259 260 261 SunOS 5.11 Last change: 7 Aug 2008 4 262 263 264 265 266 267 268 Sockets Library Functions getsockopt(3SOCKET) 269 270 271 272 The SO_EXCLBIND option is used to enable or disable the 273 exclusive binding of a socket. It overrides the use of the 274 SO_REUSEADDR option to reuse an address on bind(3SOCKET). 275 The actual semantics of the SO_EXCLBIND option depend on the 276 underlying protocol. See tcp(7P) or udp(7P) for more infor- 277 mation. 278 279 280 The SO_TYPE and SO_ERROR options are used only with get- 281 sockopt(). The SO_TYPE option returns the type of the 282 socket, for example, SOCK_STREAM. It is useful for servers 283 that inherit sockets on startup. The SO_ERROR option 284 returns any pending error on the socket and clears the error 285 status. It may be used to check for asynchronous errors on 286 connected datagram sockets or for other asynchronous errors. 287 288 289 The SO_MAC_EXEMPT option is used to toggle socket behavior 290 with unlabeled peers. A socket that has this option enabled 291 can communicate with an unlabeled peer if it is in the glo- 292 bal zone or has a label that dominates the default label of 293 the peer. Otherwise, the socket must have a label that is 294 equal to the default label of the unlabeled peer. Calling 295 setsockopt() with this option returns an EACCES error if the 296 process lacks the NET_MAC_AWARE privilege or if the socket 297 is bound. The SO_MAC_EXEMPT option is available only when 298 the system is configured with Trusted Extensions. 299 300 301 The SO_ALLZONES option can be used to bypass zone boundaries 302 between shared-IP zones. Normally, the system prevents a 303 socket from being bound to an address that is not assigned 304 to the current zone. It also prevents a socket that is bound 305 to a wildcard address from receiving traffic for other 306 zones. However, some daemons which run in the global zone 307 might need to send and receive traffic using addresses that 308 belong to other shared-IP zones. If set before a socket is 309 bound, SO_ALLZONES causes the socket to ignore zone boun- 310 daries between shared-IP zones and permits the socket to be 311 bound to any address assigned to the shared-IP zones. If the 312 socket is bound to a wildcard address, it receives traffic 313 intended for all shared-IP zones and behaves as if an 314 equivalent socket were bound in each active shared-IP zone. 315 Applications that use the SO_ALLZONES option to initiate 316 connections or send datagram traffic should specify the 317 source address for outbound traffic by binding to a specific 318 address. There is no effect from setting this option in an 319 exclusive-IP zone. Setting this option requires the 320 sys_net_config privilege. See zones(5). 321 322 RETURN VALUES 323 324 325 326 327 SunOS 5.11 Last change: 7 Aug 2008 5 328 329 330 331 332 333 334 Sockets Library Functions getsockopt(3SOCKET) 335 336 337 338 If successful, getsockopt() and setsockopt() return 0. Oth- 339 erwise, the functions return -1 and set errno to indicate 340 the error. 341 342 ERRORS 343 The getsockopt() and setsockopt() calls succeed unless: 344 345 EBADF The argument s is not a valid file descrip- 346 tor. 347 348 349 ENOMEM There was insufficient memory available for 350 the operation to complete. 351 352 353 ENOPROTOOPT The option is unknown at the level indi- 354 cated. 355 356 357 ENOSR There were insufficient STREAMS resources 358 available for the operation to complete. 359 360 361 ENOTSOCK The argument s is not a socket. 362 363 364 ENOBUFS SO_SNDBUF or SO_RCVBUF exceeds a system 365 limit. 366 367 368 EINVAL Invalid length for IP_OPTIONS. 369 370 371 EHOSTUNREACH Invalid address for IP_MULTICAST_IF. 372 373 374 EINVAL Not a multicast address for 375 IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP. 376 377 378 EADDRNOTAVAIL Bad interface address for IP_ADD_MEMBERSHIP 379 and IP_DROP_MEMBERSHIP. 380 381 382 EADDRINUSE Address already joined for 383 IP_ADD_MEMBERSHIP. 384 385 386 ENOENT Address not joined for IP_DROP_MEMBERSHIP. 387 388 389 390 391 392 393 SunOS 5.11 Last change: 7 Aug 2008 6 394 395 396 397 398 399 400 Sockets Library Functions getsockopt(3SOCKET) 401 402 403 404 EPERM No permissions. 405 406 407 EACCES Permission denied. 408 409 410 EINVAL The specified option is invalid at the 411 specified socket level, or the socket has 412 been shut down. 413 414 415 ATTRIBUTES 416 See attributes(5) for descriptions of the following attri- 417 butes: 418 419 420 421 ____________________________________________________________ 422 | ATTRIBUTE TYPE | ATTRIBUTE VALUE | 423 |_____________________________|_____________________________| 424 | MT-Level | Safe | 425 |_____________________________|_____________________________| 426 427 428 SEE ALSO 429 ndd(1M), close(2), ioctl(2), read(2), bind(3SOCKET), 430 getprotobyname(3SOCKET), recv(3SOCKET), recvmsg(3XNET), 431 send(3SOCKET), socket(3SOCKET), socket.h(3HEAD), attri- 432 butes(5), zones(5), tcp(7P), udp(7P) 433 434 435 Solaris Tunable Parameters Reference Manual 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 SunOS 5.11 Last change: 7 Aug 2008 7 460 461 462