📄 socklib.c
字号:
* if it is set to 0. To make sure that the value of `l_linger' is 0* on a newly accepted socket connection, issue another setsockopt()* after the accept() call.** Currently the exact value of `l_linger' time is actually ignored* (other than checking for 0); that is, TCP performs the state* transitions if `l_linger' is not 0, but does not explicitly use its* value.** .SS "TCP_NODELAY -- Delivering Messages Immediately"* Specify the TCP_NODELAY option for real-time protocols, such as the X* Window System Protocol, that require immediate delivery of many small* messages:* .CS* setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type `int') set to either 1* (on) or 0 (off).** By default, the VxWorks TCP implementation employs an algorithm that* attempts to avoid the congestion that can be produced by a large number* of small TCP segments. This typically arises with virtual terminal* applications (such as telnet or rlogin) across networks that have* low bandwidth and long delays. The algorithm attempts to have no* more than one outstanding unacknowledged segment in the transmission* channel while queueing up the rest of the smaller segments for later* transmission. Another segment is sent only if enough new data is* available to make up a maximum sized segment, or if the outstanding* data is acknowledged.** This congestion-avoidance algorithm works well for virtual terminal* protocols and bulk data transfer protocols such as FTP without any* noticeable side effects. However, real-time protocols that require* immediate delivery of many small messages, such as the X Window* System Protocol, need to defeat this facility to guarantee proper* responsiveness in their operation.** TCP_NODELAY is a mechanism to turn off the use of this algorithm.* If this option is turned on and there is data to be sent out, TCP* bypasses the congestion-avoidance algorithm: any available data* segments are sent out if there is enough space in the send window.** .SS "SO_DEBUG -- Debugging the underlying protocol"* Specify the SO_DEBUG option to let the underlying protocol module record* debug information.* .CS* setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof (optval));* .CE* The value at <optval> for this option is an integer (type `int'),* either 1 (on) or 0 (off).** OPTION FOR DATAGRAM SOCKETS* The following section discusses an option for datagram (UDP) sockets.** .SS "SO_BROADCAST -- Sending to Multiple Destinations"* Specify the SO_BROADCAST option when an application needs to send* data to more than one destination:* .CS* setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval));* .CE* The value at <optval> is an integer (type <int>), either 1 (on) or 0* (off).** OPTIONS FOR DATAGRAM AND RAW SOCKETS* The following section discusses options for multicasting on UDP and RAW* sockets.** .SS "IP_ADD_MEMBERSHIP -- Join a Multicast Group"* Specify the IP_ADD_MEMBERSHIP option when a process needs to join* multicast group:* .CS* setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq,* sizeof (ipMreq));* .CE* The value of <ipMreq> is an 'ip_mreq' structure. * `ipMreq.imr_multiaddr.s_addr' is the internet multicast address* `ipMreq.imr_interface.s_addr' is the internet unicast address of the * interface through which the multicast packet needs to pass.** .SS "IP_DROP_MEMBERSHIP -- Leave a Multicast Group"* Specify the IP_DROP_MEMBERSHIP option when a process needs to leave* a previously joined multicast group:* .CS* setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ipMreq,* sizeof (ipMreq));* .CE* The value of <ipMreq> is an 'ip_mreq' structure. * `ipMreq.imr_multiaddr.s_addr' is the internet multicast address.* `ipMreq.imr_interface.s_addr' is the internet unicast address of the * interface to which the multicast address was bound.** .SS "IP_MULTICAST_IF -- Select a Default Interface for Outgoing Multicasts"* Specify the IP_MULTICAST_IF option when an application needs to specify* an outgoing network interface through which all multicast packets* are sent:* .CS* setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&ifAddr,* sizeof (mCastAddr));* .CE* The value of <ifAddr> is an 'in_addr' structure. * `ifAddr.s_addr' is the internet network interface address.** .SS "IP_MULTICAST_TTL -- Select a Default TTL"* Specify the IP_MULTICAST_TTL option when an application needs to select* a default TTL (time to live) for outgoing multicast* packets:** .CS* setsockopt (sock, IPPROTO_IP, IP_MULTICAST_TTL, &optval, sizeof(optval));* .CE* The value at <optval> is an integer (type <int>), time to live value.** .TS* tab(|);* lf3 lf3 lf3* l l l.* optval(TTL) | Application | Scope* _* 0 | | same interface* 1 | | same subnet* 31 | local event video | * 32 | | same site* 63 | local event audio | * 64 | | same region* 95 | IETF channel 2 video | * 127 | IETF channel 1 video | * 128 | | same continent* 159 | IETF channel 2 audio | * 191 | IETF channel 1 audio | * 223 | IETF channel 2 low-rate audio | * 255 | IETF channel 1 low-rate audio |* | unrestricted in scope |* .TE** .SS "IP_MULTICAST_LOOP -- Enable or Disable Loopback"* Enable or disable loopback of outgoing multicasts.* .CS* setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, &optval, sizeof(optval));* .CE* The value at <optval> is an integer (type <int>), either 1(on) or 0* (off).** 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 even if it is still bound to another* stream socket, if that other socket is associated with a "zombie" protocol* control block context 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 which is being used by a zombie protocol* control block, the bind() call fails.** .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, but 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 which 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 = 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 parameter <optlen> 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 = 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 parameter <namelen> 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 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 = 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 parameter <namelen> 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 = 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: OK, or ERROR if the socket is invalid or not connected.*/STATUS shutdown ( int s, /* socket to shut down */ int how /* 0 = receives disallowed */ /* 1 = sends disallowed */ /* 2 = sends and receives disallowed */ ) { SOCK_FUNC * pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->shutdownRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->shutdownRtn) (s, how)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -