?? getsockopt.2
字號:
.\" Copyright (c) 1983, 1991, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)getsockopt.2 8.3 (Berkeley) 4/19/94.\".Dd April 19, 1994.Dt GETSOCKOPT 2.Os BSD 4.3r.Sh NAME.Nm getsockopt ,.Nm setsockopt.Nd get and set options on sockets.Sh SYNOPSIS.Fd #include <sys/types.h>.Fd #include <sys/socket.h>.Ft int.Fn getsockopt "int s" "int level" "int optname" "void *optval" "int *optlen".Ft int.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "int optlen".Sh DESCRIPTION.Fn Getsockoptand.Fn setsockoptmanipulate the.Em optionsassociated with a socket. Options may exist at multipleprotocol levels; they are always present at the uppermost.Dq socketlevel..PpWhen manipulating socket options the level at which theoption resides and the name of the option must be specified.To manipulate options at the socket level,.Fa levelis specified as.Dv SOL_SOCKET .To manipulate options at anyother level the protocol number of the appropriate protocolcontrolling the option is supplied. For example,to indicate that an option is to be interpreted by the.Tn TCPprotocol,.Fa levelshould be set to the protocol number of.Tn TCP ;see.Xr getprotoent 3 ..PpThe parameters.Fa optvaland.Fa optlenare used to access option values for.Fn setsockopt .For.Fn getsockoptthey identify a buffer in which the value for therequested option(s) are to be returned. For.Fn getsockopt ,.Fa optlenis a value-result parameter, initially containing thesize of the buffer pointed to by.Fa optval ,and modified on return to indicate the actual size ofthe value returned. If no option value isto be supplied or returned,.Fa optvalmay be NULL..Pp.Fa Optnameand any specified options are passed uninterpreted to the appropriateprotocol module for interpretation.The include file.Ao Pa sys/socket.h Accontains definitions forsocket level options, described below.Options at other protocol levels vary in format andname; consult the appropriate entries insection4 of the manual..PpMost socket-level options utilize an.Fa intparameter for.Fa optval .For.Fn setsockopt ,the parameter should be non-zero to enable a boolean option,or zero if the option is to be disabled..Dv SO_LINGERuses a.Fa struct lingerparameter, defined in.Ao Pa sys/socket.h Ac ,which specifies the desired state of the option and thelinger interval (see below)..Dv SO_SNDTIMEOand.Dv SO_RCVTIMEOuse a.Fa struct timevalparameter, defined in.Ao Pa sys/time.h Ac ..PpThe following options are recognized at the socket level.Except as noted, each may be examined with.Fn getsockoptand set with.Fn setsockopt ..Bl -column SO_OOBINLINE -offset indent.It Dv SO_DEBUG Ta "enables recording of debugging information".It Dv SO_REUSEADDR Ta "enables local address reuse".It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings".It Dv SO_KEEPALIVE Ta "enables keep connections alive".It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages".It Dv SO_LINGER Ta "linger on close if data present".It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages".It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band".It Dv SO_SNDBUF Ta "set buffer size for output".It Dv SO_RCVBUF Ta "set buffer size for input".It Dv SO_SNDLOWAT Ta "set minimum count for output".It Dv SO_RCVLOWAT Ta "set minimum count for input".It Dv SO_SNDTIMEO Ta "set timeout value for output".It Dv SO_RCVTIMEO Ta "set timeout value for input".It Dv SO_TYPE Ta "get the type of the socket (get only)".It Dv SO_ERROR Ta "get and clear error on the socket (get only)".El.Pp.Dv SO_DEBUGenables debugging in the underlying protocol modules..Dv SO_REUSEADDRindicates that the rules used in validating addresses suppliedin a.Xr bind 2call should allow reuse of local addresses..Dv SO_REUSEPORTallows completely duplicate bindings by multiple processesif they all set.Dv SO_REUSEPORTbefore binding the port.This option permits multiple instances of a program to eachreceive UDP/IP multicast or broadcast datagrams destined for the bound port..Dv SO_KEEPALIVEenables theperiodic transmission of messages on a connected socket. Should theconnected party fail to respond to these messages, the connection isconsidered broken and processes using the socket are notified via a.Dv SIGPIPEsignal when attempting to send data..Dv SO_DONTROUTEindicates that outgoing messages shouldbypass the standard routing facilities. Instead, messages are directedto the appropriate network interface according to the network portionof the destination address..Pp.Dv SO_LINGERcontrols the action taken when unsent messagesare queued on socket and a .Xr close 2is performed.If the socket promises reliable delivery of data and.Dv SO_LINGER is set,the system will block the process on the .Xr closeattempt until it is able to transmit the data or until it decides itis unable to deliver the information (a timeout period, termed thelinger interval, is specified in the.Fn setsockoptcall when.Dv SO_LINGERis requested). If.Dv SO_LINGERis disabled and a .Xr closeis issued, the system will process the close in a manner that allowsthe process to continue as quickly as possible..PpThe option.Dv SO_BROADCASTrequests permission to send broadcast datagramson the socket.Broadcast was a privileged operation in earlier versions of the system.With protocols that support out-of-band data, the.Dv SO_OOBINLINEoptionrequests that out-of-band data be placed in the normal data input queueas received; it will then be accessible with.Xr recvor.Xr readcalls without the.Dv MSG_OOBflag.Some protocols always behave as if this option is set..Dv SO_SNDBUFand.Dv SO_RCVBUFare options to adjust the normalbuffer sizes allocated for output and input buffers, respectively.The buffer size may be increased for high-volume connections,or may be decreased to limit the possible backlog of incoming data.The system places an absolute limit on these values..Pp.Dv SO_SNDLOWATis an option to set the minimum count for output operations.Most output operations process all of the data suppliedby the call, delivering data to the protocol for transmissionand blocking as necessary for flow control.Nonblocking output operations will process as much data as permittedsubject to flow control without blocking, but will process no dataif flow control does not allow the smaller of the low water mark valueor the entire request to be processed.A.Xr select 2operation testing the ability to write to a socket will return trueonly if the low water mark amount could be processed.The default value for.Dv SO_SNDLOWATis set to a convenient size for network efficiency, often 1024..Dv SO_RCVLOWATis an option to set the minimum count for input operations.In general, receive calls will block until any (non-zero) amount of datais received, then return with the smaller of the amount available or the amountrequested.The default value for.Dv SO_RCVLOWATis 1.If .Dv SO_RCVLOWATis set to a larger value, blocking receive calls normallywait until they have received the smaller of the low water mark valueor the requested amount.Receive calls may still return less than the low water mark if an erroroccurs, a signal is caught, or the type of data next in the receive queueis different than that returned..Pp.Dv SO_SNDTIMEOis an option to set a timeout value for output operations.It accepts a.Fa struct timevalparameter with the number of seconds and microsecondsused to limit waits for output operations to complete.If a send operation has blocked for this much time,it returns with a partial countor with the error.Er EWOULDBLOCKif no data were sent.In the current implementation, this timer is restarted each time additionaldata are delivered to the protocol,implying that the limit applies to output portions ranging in sizefrom the low water mark to the high water mark for output..Dv SO_RCVTIMEOis an option to set a timeout value for input operations.It accepts a.Fa struct timevalparameter with the number of seconds and microsecondsused to limit waits for input operations to complete.In the current implementation, this timer is restarted each time additionaldata are received by the protocol,and thus the limit is in effect an inactivity timer.If a receive operation has been blocked for this much time withoutreceiving additional data, it returns with a short countor with the error.Er EWOULDBLOCKif no data were received..PpFinally,.Dv SO_TYPEand.Dv SO_ERRORare options used only with.Fn getsockopt ..Dv SO_TYPEreturns the type of the socket, such as.Dv SOCK_STREAM ;it is useful for servers that inherit sockets on startup..Dv SO_ERRORreturns any pending error on the socket and clearsthe error status.It may be used to check for asynchronous errors on connecteddatagram sockets or for other asynchronous errors..Sh RETURN VALUESA 0 is returned if the call succeeds, -1 if it fails..Sh ERRORSThe call succeeds unless:.Bl -tag -width ENOPROTOOPTAA.It Bq Er EBADFThe argument.Fa sis not a valid descriptor..It Bq Er ENOTSOCKThe argument.Fa sis a file, not a socket..It Bq Er ENOPROTOOPTThe option is unknown at the level indicated..It Bq Er EFAULTThe address pointed to by .Fa optvalis not in a valid part of the process address space.For.Fn getsockopt ,this error may also be returned if.Fa optlenis not in a valid part of the process address space..El.Sh SEE ALSO.Xr ioctl 2 ,.Xr socket 2 ,.Xr getprotoent 3.Xr protocols 5.Sh BUGSSeveral of the socket options should be handled at lower levels of the system..Sh HISTORYThe.Nmsystem call appeared in.Bx 4.2 .
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -