📄 socklib.c
字号:
* sender's socket is copied to it. The value-result parameter <pFromLen>* should be initialized to the size of the <from> buffer. On return,* <pFromLen> contains the actual size of the address stored in <from>.** The maximum length of <buf> is subject to the limits on UDP buffer* size; see the discussion of SO_RCVBUF in the setsockopt() manual* entry.** You may OR the following values into the <flags> parameter with this* operation:** .IP "MSG_OOB (0x1)" 26* Out-of-band data.** .IP "MSG_PEEK (0x2)"* Return data without removing it from socket.* .LP** RETURNS* The number of number of bytes received, or ERROR if the call fails.** SEE ALSO* setsockopt()*/int recvfrom ( FAST int s, /* socket to receive from */ FAST char *buf, /* pointer to data buffer */ FAST int bufLen, /* length of buffer */ FAST int flags, /* flags to underlying protocols */ FAST struct sockaddr *from, /* where to copy sender's addr */ FAST int *pFromLen /* value/result length of <from> */ ) { SOCK_FUNC * pSockFunc = NULL; if (buf == NULL || from == NULL || pFromLen == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->recvfromRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->recvfromRtn) (s, buf, bufLen, flags, from, pFromLen)); }/********************************************************************************* recv - receive data from a socket** This routine receives data from a connection-based (stream) socket.** The maximum length of <buf> is subject to the limits on TCP buffer* size; see the discussion of SO_RCVBUF in the setsockopt() manual* entry.** You may OR the following values into the <flags> parameter with this* operation:** .IP "MSG_OOB (0x1)" 26* Out-of-band data.** .IP "MSG_PEEK (0x2)"* Return data without removing it from socket.* .LP** RETURNS* The number of bytes received, or ERROR if the call fails.** SEE ALSO* setsockopt()*/int recv ( FAST int s, /* socket to receive data from */ FAST char *buf, /* buffer to write data to */ FAST int bufLen, /* length of buffer */ FAST int flags /* flags to underlying protocols */ ) { SOCK_FUNC * pSockFunc = NULL; if (buf == NULL || iosFdValue (s) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->recvRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->recvRtn) (s, buf, bufLen, flags)); }/********************************************************************************* recvmsg - receive a message from a socket** This routine receives a message from a datagram socket. It may be used in* place of recvfrom() to decrease the overhead of breaking down the* message-header structure `msghdr' for each message.** For BSD 4.4 sockets a copy of the <mp>->msg_iov array will be made. This* requires a cluster from the network stack system pool of size * <mp>->msg_iovlen * sizeof (struct iovec) or 8 bytes. ** RETURNS* The number of bytes received, or ERROR if the call fails.*/int recvmsg ( int sd, /* socket to receive from */ struct msghdr *mp, /* scatter-gather message header */ int flags /* flags to underlying protocols */ ) { SOCK_FUNC * pSockFunc = NULL; if (mp == NULL || iosFdValue (sd) == ERROR) { netErrnoSet (EINVAL); return (ERROR); } pSockFunc = pSockFdMap[sd]; if ((pSockFunc == NULL) || (pSockFunc->recvmsgRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->recvmsgRtn) (sd, mp, flags)); }/********************************************************************************* setsockopt - set socket options** This routine sets the options 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.** OPTIONS FOR STREAM SOCKETS* The following sections discuss the socket options available for* stream (TCP) sockets.** .SS "SO_KEEPALIVE -- Detecting a Dead Connection"* Specify the SO_KEEPALIVE option to make the transport protocol (TCP)* initiate a timer to detect a dead connection:* .CS* setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof (optval));* .CE* This prevents an application from hanging on an invalid connection.* The value at <optval> for this option is an integer (type `int'),* either 1 (on) or 0 (off).** The integrity of a connection is verified by transmitting* zero-length TCP segments triggered by a timer, to force a response* from a peer node. If the peer does not respond after repeated* transmissions of the KEEPALIVE segments, the connection is dropped,* all protocol data structures are reclaimed, and processes sleeping* on the connection are awakened with an ETIMEDOUT error.** The ETIMEDOUT timeout can happen in two ways. If the connection is* not yet established, the KEEPALIVE timer expires after idling* for TCPTV_KEEP_INIT. If the connection is established, the* KEEPALIVE timer starts up when there is no traffic for* TCPTV_KEEP_IDLE. If no response is received from the peer after* sending the KEEPALIVE segment TCPTV_KEEPCNT times with interval* TCPTV_KEEPINTVL, TCP assumes that the connection is invalid.* The TCPTV_KEEP_INIT, TCPTV_KEEP_IDLE, TCPTV_KEEPCNT, and TCPTV_KEEPINTVL * parameters are defined in the file target/h/netinet/tcp_timer.h.** .SS "SO_LINGER -- Closing a Connection"* Specify the SO_LINGER option to determine whether TCP should perform a* "graceful" close:* .CS* setsockopt (sock, SOL_SOCKET, SO_LINGER, &optval, sizeof (optval));* .CE* To achieve a "graceful" close in response to the shutdown of a connection,* TCP puts itself through an elaborate set of state transitions. The goal is * to assure that all the unacknowledged data in the transmission channel are * acknowledged, and that the peer is shut down properly.** The value at <optval> indicates the amount of time to linger if* there is unacknowledged data, using `struct linger' in* target/h/sys/socket.h. The `linger' structure has two members:* `l_onoff' and `l_linger'. `l_onoff' can be set to 1 to turn on the* SO_LINGER option, or set to 0 to turn off the SO_LINGER option.* `l_linger' indicates the amount of time to linger. If `l_onoff' is* turned on and `l_linger' is set to 0, a default value TCP_LINGERTIME* (specified in netinet/tcp_timer.h) is used for incoming* connections accepted on the socket.** When SO_LINGER is turned on and the `l_linger' field is set to 0,* TCP simply drops the connection by sending out an RST (if a* connection is already established). This frees up the space for the TCP* protocol control block, and wakes up all tasks sleeping on the* socket.** For the client side socket, the value of `l_linger' is not changed* 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 "TCP_MAXSEG -- Changing TCP MSS for the connection"* Specify the TCP_MAXSEG option to decrease the maximum allowable size of an* outgoing TCP segment. This option cannot be used to increase the MSS.* .CS* setsockopt (sock, IPPROTO_TCP, TCP_MAXSEG, &optval, sizeof (optval));* .CE* The value at <optval> is an integer set to the desired MSS (eg. 1024).** When a TCP socket is created, the MSS is initialized to the default MSS* value which is determined by the configuration parameter `TCP_MSS_DFLT' (512* by default). When a connection request is received from the other end with* an MSS option, the MSS is modified depending on the value of the received* MSS and on the results of Path MTU Discovery (which is enabled by default). * The MSS may be set as high as the outgoing interface MTU (1460 for an * Ethernet). Therefore, after a call to `socket' but before a connection is * established, an application can only decrease the MSS from its default of 512. * After a connection is established, the application can decrease the MSS from * whatever value was selected.** .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_DEBUG, &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 DATAGRAM, STREAM AND RAW SOCKETS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -