📄 socklib.c
字号:
* The following section discusses options for RAW, DGRAM or STREAM* sockets.** .SS "IP_OPTIONS -- set options to be included in outgoing datagrams"* Sets the IP options sent from this socket with every packet.* .CS* setsockopt (sock, IPPROTO_IP, IP_OPTIONS, optbuf, optbuflen);* .CE* Here <optbuf> is a buffer containing the options.** .SS "IP_TOS-- set options to be included in outgoing datagrams"* Sets the Type-Of-Service field for each packet sent from this socket.* .CS* setsockopt (sock, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));* .CE* Here <optval> is an integer (type <int>). This integer can be set to* IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, or IPTOS_MINCOST,* to indicate how the packets sent on this socket should be prioritized.** .SS "IP_TTL-- set the time-to-live field in outgoing datagrams"* Sets the Time-To-Live field for each packet sent from this socket.* .CS* setsockopt (sock, IPPROTO_IP, IP_TTL, &optval, sizeof(optval));* .CE* Here <optval> is an integer (type <int>), indicating the number of hops* a packet can take before it is discarded.** .SS "IP_RECVRETOPTS -- [un-]set queueing of reversed source route"* Sets whether or not reversed source route queueing will be enabled for* incoming datagrams. (Not implemented)* .CS* setsockopt (sock, IPPROTO_IP, IP_RECVRETOPTS, &optval, sizeof(optval));* .CE* Here <optval> is a boolean (type <int>). However, this option is* currently not implemented, so setting it will not change the behavior* of the system.** .SS "IP_RECVDSTADDR -- [un-]set queueing of IP destination address"* Sets whether or not the socket will receive the IP address of the* destination of an incoming datagram in control data.* .CS* setsockopt (sock, IPPROTO_IP, IP_RECVDSTADDR, &optval, sizeof(optval));* .CE* Here <optval> is a boolean (type <int>).** OPTIONS FOR BOTH STREAM AND DATAGRAM SOCKETS* The following sections describe options that can be used with either* stream or datagram sockets.** .SS "SO_REUSEADDR -- Reusing a Socket Address"* Specify the SO_REUSEADDR option to bind a stream socket to a local* port that may be still bound to another stream socket:* .CS* setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type <int>), either 1 (on) or 0* (off).** When the SO_REUSEADDR option is turned on, applications may bind a stream * socket to a local port. This is possible even if the port is still bound * to another stream socket. It is even possible if that other socket is * associated with a "zombie" protocol control block context that has not yet * freed from previous sessions. The uniqueness of port number combinations * for each connection is still preserved through sanity checks performed at * actual connection setup time. If this option is not turned on and an * application attempts to bind to a port that is being used by a zombie * protocol control block, the bind() call fails.** .SS "SO_REUSEPORT -- Reusing a Socket address and port"* This option is similar to the SO_REUSEADDR option but it allows binding to* the same local address and port combination. * .CS* setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type <int>), either 1 (on) or 0 (off).** The SO_REUSEPORT option is mainly required by multicast applications where* a number of applications need to bind to the same multicast address and port* to receive multicast data. Unlike SO_REUSEADDR where only the later* applications need to set this option, with SO_REUSEPORT all applications* including the first to bind to the port are required to set this option. For* multicast addresses SO_REUSEADDR and SO_REUSEPORT show the same behavior so* SO_REUSEADDR can be used instead. ** .SS "SO_SNDBUF -- Specifying the Size of the Send Buffer"* Specify the SO_SNDBUF option to adjust the maximum size of the* socket-level send buffer:* .CS* setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type `int') that specifies the* size of the socket-level send buffer to be allocated.** When stream or datagram sockets are created, each transport protocol* reserves a set amount of space at the socket level for use when the* sockets are attached to a protocol. For TCP, the default size of* the send buffer is 8192 bytes. For UDP, the default size of the* send buffer is 9216 bytes. Socket-level buffers are allocated* dynamically from the mbuf pool.** The effect of setting the maximum size of buffers (for both SO_SNDBUF and * SO_RCVBUF, described below) is not actually to allocate the mbufs from * the mbuf pool. Instead, the effect is to set the high-water mark in the * protocol data structure, which is used later to limit the amount of mbuf * allocation. Thus, the maximum size specified for the socket level send * and receive buffers can affect the performance of bulk data transfers. * For example, the size of the TCP receive windows is limited by the * remaining socket-level buffer space. These parameters must be adjusted * to produce the optimal result for a given application.** .SS "SO_RCVBUF -- Specifying the Size of the Receive Buffer"* Specify the SO_RCVBUF option to adjust the maximum size of the* socket-level receive buffer:* .CS* setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type `int') that specifies the* size of the socket-level receive buffer to be allocated.** When stream or datagram sockets are created, each transport protocol* reserves a set amount of space at the socket level for use when the* sockets are attached to a protocol. For TCP, the default size is* 8192 bytes. UDP reserves 41600 bytes, enough space for up to forty* incoming datagrams (1 Kbyte each).** See the SO_SNDBUF discussion above for a discussion of the impact of* buffer size on application performance.** .SS "SO_OOBINLINE -- Placing Urgent Data in the Normal Data Stream"* Specify the SO_OOBINLINE option to place urgent data within the* normal receive data stream:* .CS* setsockopt (sock, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof (optval));* .CE* TCP provides an expedited data service that does not conform to the* normal constraints of sequencing and flow control of data* streams. The expedited service delivers "out-of-band" (urgent) data* ahead of other "normal" data to provide interrupt-like services (for* example, when you hit a CTRL-C during telnet or rlogin session while* data is being displayed on the screen.)** TCP does not actually maintain a separate stream to support the* urgent data. Instead, urgent data delivery is implemented as a* pointer (in the TCP header) which points to the sequence number of* the octet following the urgent data. If more than one transmission* of urgent data is received from the peer, they are all put into the* normal stream. This is intended for applications that cannot afford* to miss out on any urgent data but are usually too slow to respond* to them promptly.** RETURNS:* OK, or ERROR if there is an invalid socket, an unknown option, an option* length greater than MLEN, insufficient mbufs, or the call is unable to set* the specified option.*/STATUS setsockopt ( int s, /* target socket */ int level, /* protocol level of option */ int optname, /* option name */ char *optval, /* pointer to option value */ int optlen /* option length */ ) { SOCK_FUNC * pSockFunc = NULL; if (optval == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->setsockoptRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->setsockoptRtn) (s, level, optname, optval, optlen)); }/********************************************************************************* getsockopt - get socket options** This routine returns relevant option values associated with a socket.* To manipulate options at the "socket" level, <level> should be SOL_SOCKET.* Any other levels should use the appropriate protocol number.* The <optlen> parameter should be initialized to indicate the amount of* space referenced by <optval>. On return, the value of the option is copied to* <optval> and the actual size of the option is copied to <optlen>.** Although <optval> is passed as a char *, the actual variable whose address * gets passed in should be an integer or a structure, depending on which * <optname> is being passed. Refer to setsockopt() to determine the correct * type of the actual variable (whose address should then be cast to a char *).** RETURNS:* OK, or ERROR if there is an invalid socket, an unknown option, or the call* is unable to get the specified option.** EXAMPLE* Because SO_REUSEADDR has an integer parameter, the variable to be* passed to getsockopt() should be declared as* .CS* int reuseVal;* .CE* and passed in as * .CS* (char *)&reuseVal.* .CE* Otherwise the user might mistakenly declare `reuseVal' as a character,* in which case getsockopt() will only return the first byte of the* integer representing the state of this option. Then whether the return* value is correct or always 0 depends on the endian-ness of the machine.** SEE ALSO: setsockopt()*/STATUS getsockopt ( int s, /* socket */ int level, /* protocol level for options */ int optname, /* name of option */ char *optval, /* where to put option */ int *optlen /* where to put option length */ ) { SOCK_FUNC * pSockFunc = NULL; if (optval == NULL || optlen == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->getsockoptRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->getsockoptRtn) (s, level, optname, optval, optlen)); }/********************************************************************************* getsockname - get a socket name** This routine gets the current name for the specified socket <s>.* The <namelen> parameter should be initialized to indicate the amount of* space referenced by <name>. On return, the name of the socket is copied to* <name> and the actual size of the socket name is copied to <namelen>.** RETURNS: OK, or ERROR if the socket is invalid.*/STATUS getsockname ( int s, /* socket descriptor */ struct sockaddr *name, /* where to return name */ int *namelen /* space available in name, later */ /* filled in with actual name size */ ) { SOCK_FUNC * pSockFunc = NULL; if (name == NULL || namelen == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->getsocknameRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->getsocknameRtn) (s, name, namelen)); }/********************************************************************************* getpeername - get the name of a connected peer** This routine gets the name of the peer connected to socket <s>.* The <namelen> parameter should be initialized to indicate the amount of* space referenced by <name>. On return, the name of the socket is copied to* <name> and the actual size of the socket name is copied to <namelen>.** RETURNS: OK, or ERROR if the socket is invalid* or not connected.*/STATUS getpeername ( int s, /* socket descriptor */ struct sockaddr *name, /* where to put name */ int *namelen /* space available in name, later */ /* filled in with actual name size */ ) { SOCK_FUNC * pSockFunc = NULL; if (name == NULL || namelen == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->getpeernameRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->getpeernameRtn) (s, name, namelen)); }/******************************************************************************** shutdown - shut down a network connection** This routine shuts down all, or part, of a connection-based socket <s>.* If the value of <how> is 0, receives are disallowed. If <how> is 1,* sends are disallowed. If <how> is 2, both sends and receives are* disallowed.** RETURNS: ERROR if the socket is invalid or has no registered* socket-specific routines; otherwise shutdown() returns the return value* from the socket-specific shutdown routine (typically OK in the case of* a successful shutdown or ERROR otherwise).*/STATUS shutdown ( int s, /* socket to shut down */ int how /* 0 = receives disallowed */ /* 1 = sends disallowed */ /* 2 = sends and receives disallowed */ ) { SOCK_FUNC * pSockFunc = NULL; if (((UINT)how) > 2 || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->shutdownRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->shutdownRtn) (s, how)); }SOCK_FUNC * sockFdtosockFunc ( int s ) { if (iosFdValue (s) == ERROR) return (NULL); return pSockFdMap[s]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -