📄 net_bsd.c
字号:
(CPU_INT16S ) data_buf_len,
(CPU_INT16S ) flags,
(NET_SOCK_ADDR *) paddr_remote,
(NET_SOCK_ADDR_LEN *)&addr_len,
(void *) 0,
(CPU_INT08U ) 0,
(CPU_INT08U *) 0,
(NET_ERR *)&err);
*paddr_len = (int)addr_len;
return (rtn_code);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* recv()
*
* Description : Receive data from a socket.
*
* Argument(s) : sock_id Socket descriptor/handle identifier of socket to receive data.
*
* pdata_buf Pointer to an application data buffer that will receive the socket's received
* data.
*
* data_buf_len Size of the application data buffer (in octets) [see Note #1].
*
* flags Flags to select receive options (see Note #2); bit-field flags logically OR'd :
*
* 0 No socket flags selected.
* MSG_PEEK Receive socket data without consuming
* the socket data.
* MSG_DONTWAIT Receive socket data without blocking.
*
* Return(s) : Number of positive data octets received, if NO errors (see Note #3a).
*
* 0, if socket connection closed (see Note #3b).
*
* -1, otherwise (see Note #3c1).
*
* Caller(s) : Application.
*
* This function is a network protocol suite application interface (API) function & MAY be
* called by application function(s).
*
* Note(s) : (1) (a) (1) (A) Datagram-type sockets send & receive all data atomically -- i.e. every single,
* complete datagram transmitted MUST be received as a single, complete datagram.
*
* (B) IEEE Std 1003.1, 2004 Edition, Section 'recv() : DESCRIPTION' summarizes that
* "for message-based sockets, such as SOCK_DGRAM ... the entire message shall be
* read in a single operation. If a message is too long to fit in the supplied
* buffer, and MSG_PEEK is not set in the flags argument, the excess bytes shall
* be discarded".
*
* (2) Thus, if the socket's type is datagram & the receive data buffer size is NOT
* large enough for the received data, the receive data buffer is maximally filled
* with receive data but the remaining data octets are silently discarded & NO
* error is returned.
*
* (A) IEEE Std 1003.1, 2004 Edition, Section 'send() : ERRORS' states to return an
* 'EMSGSIZE' error when "the message is too large to be sent all at once".
*
* ???? Similarly, a socket receive whose receive data buffer size is NOT large
* enough for the received data could return an 'EMSGSIZE' error.
*
* (b) (1) (A) (1) Stream-type sockets send & receive all data octets in one or more non-
* distinct packets. In other words, the application data is NOT bounded
* by any specific packet(s); rather, it is contiguous & sequenced from
* one packet to the next.
*
* (2) IEEE Std 1003.1, 2004 Edition, Section 'recv() : DESCRIPTION' summarizes
* that "for stream-based sockets, such as SOCK_STREAM, message boundaries
* shall be ignored. In this case, data shall be returned to the user as
* soon as it becomes available, and no data shall be discarded".
*
* (B) Thus, if the socket's type is stream & the receive data buffer size is NOT
* large enough for the received data, the receive data buffer is maximally
* filled with receive data & the remaining data octets remain queued for
* later application-socket receives.
*
* (2) Thus, it is typical -- but NOT absolutely required -- that a single application
* task ONLY receive or request to receive data from a stream-type socket.
*
* See also 'net_sock.c NetSock_RxDataHandler() Note #3'.
*$PAGE*
* (2) Only some socket receive flag options are implemented. If other flag options are requested,
* socket receive handler function(s) abort & return appropriate error codes so that requested
* flag options are NOT silently ignored.
*
* (3) IEEE Std 1003.1, 2004 Edition, Section 'recv() : RETURN VALUE' states that :
*
* (a) "Upon successful completion, recv() shall return the length of the message in bytes."
*
* (b) "If no messages are available to be received and the peer has performed an orderly
* shutdown, recv() shall return 0."
*
* (c) (1) "Otherwise, -1 shall be returned" ...
* (2) "and 'errno' set to indicate the error."
*
* See also 'net_sock.c NetSock_RxDataHandler() Note #8'.
*********************************************************************************************************
*/
#ifdef NET_SOCK_MODULE_PRESENT
int recv (int sock_id,
void *pdata_buf,
int data_buf_len,
int flags)
{
int rtn_code;
NET_ERR err;
rtn_code = (int)NetSock_RxData((NET_SOCK_ID ) sock_id,
(void *) pdata_buf,
(CPU_INT16S ) data_buf_len,
(CPU_INT16S ) flags,
(NET_ERR *)&err);
return (rtn_code);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* sendto()
*
* Description : Send data through a socket.
*
* Argument(s) : sock_id Socket descriptor/handle identifier of socket to send data.
*
* p_data Pointer to application data to send.
*
* data_len Length of application data to send (in octets) [see Note #1].
*
* flags Flags to select send options (see Note #2); bit-field flags logically OR'd :
*
* 0 No socket flags selected.
* MSG_DONTWAIT Send socket data without blocking.
*
* paddr_remote Pointer to destination address buffer (see Note #3).
*
* addr_len Length of destination address buffer (in octets).
*
* Return(s) : Number of positive data octets sent, if NO errors (see Note #4a1).
*
* 0, if socket connection closed (see Note #4b).
*
* -1, otherwise (see Note #4a2A).
*
* Caller(s) : Application.
*
* This function is a network protocol suite application interface (API) function & MAY be
* called by application function(s).
*
* Note(s) : (1) (a) (1) (A) Datagram-type sockets send & receive all data atomically -- i.e. every single,
* complete datagram sent MUST be received as a single, complete datagram.
* Thus, each call to send data MUST be transmitted in a single, complete datagram.
*
* (B) (1) IEEE Std 1003.1, 2004 Edition, Section 'send() : DESCRIPTION' states that
* "if the message is too long to pass through the underlying protocol, send()
* shall fail and no data shall be transmitted".
*
* (2) Since IP transmit fragmentation is NOT currently supported (see 'net_ip.h
* Note #1e'), if the socket's type is datagram & the requested send data
* length is greater than the socket/transport layer MTU, then NO data is
* sent & NET_SOCK_ERR_INVALID_DATA_SIZE error is returned.
*
* (2) (A) (1) Stream-type sockets send & receive all data octets in one or more non-
* distinct packets. In other words, the application data is NOT bounded
* by any specific packet(s); rather, it is contiguous & sequenced from
* one packet to the next.
*
* (2) Thus, if the socket's type is stream & the socket's send data queue(s) are
* NOT large enough for the send data, the send data queue(s) are maximally
* filled with send data & the remaining data octets are discarded but may be
* re-sent by later application-socket sends.
*
* (3) Therefore, NO stream-type socket send data length should be "too long to
* pass through the underlying protocol" & cause the socket send to "fail ...
* [with] no data ... transmitted" (see Note #1a1B1).
*
* (B) Thus, it is typical -- but NOT absolutely required -- that a single application
* task ONLY send or request to send data to a stream-type socket.
*
* (b) 'data_len' of 0 octets NOT allowed.
*
* See also 'net_sock.c NetSock_TxDataHandler() Note #3'.
*$PAGE*
* (2) Only some socket send flag options are implemented. If other flag options are requested,
* socket send handler function(s) abort & return appropriate error codes so that requested
* flag options are NOT silently ignored.
*
* (3) (a) Socket address structure 'Family' member MUST be configured in host-order & MUST
* NOT be converted to/from network-order.
*
* (b) Socket address structure addresses MUST be configured/converted from host-order
* to network-order.
*
* See also 'net_sock.h NETWORK SOCKET ADDRESS DATA TYPES Note #2'.
*
* (4) (a) IEEE Std 1003.1, 2004 Edition, Section 'sendto() : RETURN VALUE' states that :
*
* (1) "Upon successful completion, sendto() shall return the number of bytes sent."
*
* (A) Section 'sendto() : DESCRIPTION' elaborates that "successful completion
* of a call to sendto() does not guarantee delivery of the message".
*
* (B) Thus, applications SHOULD verify the actual returned number of data
* octets transmitted &/or prepared for transmission.
*
* (2) (A) "Otherwise, -1 shall be returned" ...
* (1) Section 'sendto() : DESCRIPTION' elaborates that "a return value of
* -1 indicates only locally-detected errors".
*
* (B) "and 'errno' set to indicate the error."
*
* (b) ???? Although NO socket send() specification states to return '0' when the socket's
* connection is closed, it seems reasonable to return '0' since it is possible for the
* socket connection to be close()'d or shutdown() by the remote host.
*
* See also 'net_sock.c NetSock_TxDataHandler() Note #6'.
*********************************************************************************************************
*/
#ifdef NET_SOCK_MODULE_PRESENT
int sendto ( int sock_id,
void *p_data,
int data_len,
int flags,
struct sockaddr *paddr_remote,
int addr_len)
{
int rtn_code;
NET_ERR err;
rtn_code = (int)NetSock_TxDataTo((NET_SOCK_ID ) sock_id,
(void *) p_data,
(CPU_INT16S ) data_len,
(CPU_INT16S ) flags,
(NET_SOCK_ADDR *) paddr_remote,
(NET_SOCK_ADDR_LEN) addr_len,
(NET_ERR *)&err);
return (rtn_code);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* send()
*
* Description : Send data through a socket.
*
* Argument(s) : sock_id Socket descriptor/handle identifier of socket to send data.
*
* p_data Pointer to application data to send.
*
* data_len Length of application data to send (in octets) [see Note #1].
*
* flags Flags to select send options (see Note #2); bit-field flags logically OR'd :
*
* 0 No socket flags selected.
* MSG_DONTWAIT Send socket data without blocking.
*
* Return(s) : Number of positive data octets sent, if NO errors (see Note #3a1).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -