📄 net_bsd.c
字号:
*
* paddr_remote Pointer to socket address structure (see Note #1).
*
* addr_len Length of socket address structure (in octets).
*
* Return(s) : 0, if NO errors.
*
* -1, otherwise.
*
* 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) 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'.
*********************************************************************************************************
*/
#ifdef NET_SOCK_MODULE_PRESENT
int connect ( int sock_id,
struct sockaddr *paddr_remote,
int addr_len)
{
int rtn_code;
NET_ERR err;
rtn_code = (int)NetSock_Conn((NET_SOCK_ID ) sock_id,
(NET_SOCK_ADDR *) paddr_remote,
(NET_SOCK_ADDR_LEN) addr_len,
(NET_ERR *)&err);
return (rtn_code);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* listen()
*
* Description : Set socket to listen for connection requests.
*
* Argument(s) : sock_id Socket descriptor/handle identifier of socket to listen.
*
* sock_q_size Number of connection requests to queue on listen socket.
*
* Return(s) : 0, if NO errors.
*
* -1, otherwise.
*
* Caller(s) : Application.
*
* This function is a network protocol suite application interface (API) function & MAY be
* called by application function(s).
*
* Note(s) : none.
*********************************************************************************************************
*/
#ifdef NET_SOCK_MODULE_PRESENT
#if (NET_SOCK_CFG_TYPE_STREAM_EN == DEF_ENABLED)
int listen (int sock_id,
int sock_q_size)
{
NET_ERR err;
int rtn_code;
rtn_code = (int)NetSock_Listen((NET_SOCK_ID ) sock_id,
(NET_SOCK_Q_SIZE) sock_q_size,
(NET_ERR *)&err);
return (rtn_code);
}
#endif
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* accept()
*
* Description : Get a new socket accepted from a socket set to listen for connection requests.
*
* Argument(s) : sock_id Socket descriptor/handle identifier of listen socket.
*
* paddr_remote Pointer to an address buffer that will receive the socket address structure
* of the accepted socket's remote address (see Note #1), if NO errors.
*
* paddr_len Pointer to a variable to ... :
*
* (a) Pass the size of the address buffer pointed to by 'paddr_remote'.
* (b) (1) Return the actual size of socket address structure with the
* accepted socket's remote address, if NO errors;
* (2) Return 0, otherwise.
*
* Return(s) : Socket descriptor/handle identifier of new accepted socket, if NO errors.
*
* -1, otherwise.
*
* 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) Socket address structure 'Family' member returned in host-order & SHOULD NOT be
* converted to network-order.
*
* (b) Socket address structure addresses MUST be converted from host-order to network-
* order.
*
* See also 'net_sock.h NETWORK SOCKET ADDRESS DATA TYPES Note #2'.
*********************************************************************************************************
*/
#ifdef NET_SOCK_MODULE_PRESENT
#if (NET_SOCK_CFG_TYPE_STREAM_EN == DEF_ENABLED)
int accept ( int sock_id,
struct sockaddr *paddr_remote,
int *paddr_len)
{
int rtn_code;
NET_SOCK_ADDR_LEN addr_len;
NET_ERR err;
addr_len = (NET_SOCK_ADDR_LEN)*paddr_len;
rtn_code = (int)NetSock_Accept((NET_SOCK_ID ) sock_id,
(NET_SOCK_ADDR *) paddr_remote,
(NET_SOCK_ADDR_LEN *)&addr_len,
(NET_ERR *)&err);
*paddr_len = (int)addr_len;
return (rtn_code);
}
#endif
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* recvfrom()
*
* 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.
*
* paddr_remote Pointer to an address buffer that will receive the socket address structure
* with the received data's remote address (see Note #3), if NO errors.
*
* paddr_len Pointer to a variable to ... :
*
* (a) Pass the size of the address buffer pointed to by 'paddr_remote'.
* (b) (1) Return the actual size of socket address structure with the
* received data's remote address, if NO errors;
* (2) Return 0, otherwise.
*
* Return(s) : Number of positive data octets received, if NO errors (see Note #4a).
*
* 0, if socket connection closed (see Note #4b).
*
* -1, otherwise (see Note #4c1).
*
* 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 'recvfrom() : 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) (a) Socket address structure 'Family' member returned in host-order & SHOULD NOT be
* converted to network-order.
*
* (b) Socket address structure addresses MUST be converted from host-order to network-
* order.
*
* See also 'net_sock.h NETWORK SOCKET ADDRESS DATA TYPES Note #2'.
*
* (4) IEEE Std 1003.1, 2004 Edition, Section 'recvfrom() : RETURN VALUE' states that :
*
* (a) "Upon successful completion, recvfrom() 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, recvfrom() 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 recvfrom ( int sock_id,
void *pdata_buf,
int data_buf_len,
int flags,
struct sockaddr *paddr_remote,
int *paddr_len)
{
int rtn_code;
NET_SOCK_ADDR_LEN addr_len;
NET_ERR err;
addr_len = (NET_SOCK_ADDR_LEN)*paddr_len;
rtn_code = (int)NetSock_RxDataFrom((NET_SOCK_ID ) sock_id,
(void *) pdata_buf,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -