📄 net_sock.c
字号:
* (a) Demultiplex data to connection
* (b) Update receive statistics
*
*
* Argument(s) : pbuf Pointer to network buffer that received socket data.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_SOCK_ERR_NONE Socket data successfully received & processed.
* NET_ERR_INIT_INCOMPLETE Network initialization NOT complete.
*
* ----- RETURNED BY NetSock_RxPktDemux() : -----
* NET_ERR_RX_DEST Invalid destination; no socket connection
* available for received packet.
*
* ---- RETURNED BY NetSock_RxPktDiscard() : ----
* NET_ERR_RX Receive error; packet discarded.
*
* Return(s) : none.
*
* Caller(s) : NetUDP_RxPktDemuxDatagram().
*
* This function is an INTERNAL network protocol suite function & MUST NOT be called by
* application function(s).
*
* Note(s) : (2) NetSock_Rx() blocked until network initialization completes; perform NO action.
*
* (3) Since RFC #792, Section 'Destination Unreachable Message : Description' states
* that "if, in the destination host, the IP module cannot deliver the datagram
* because the indicated ... process port is not active, the destination host may
* send a destination unreachable message to the source host"; the network buffer
* MUST NOT be freed by the socket layer but must be returned to the transport or
* internet layer(s) to send an appropriate ICMP error message.
*
* See also 'net_udp.c NetUDP_Rx() Note #5'.
*
* (4) Network buffer freed by lower layer (see Note #3); only increment error counter.
*********************************************************************************************************
*/
/*$PAGE*/
void NetSock_Rx (NET_BUF *pbuf,
NET_ERR *perr)
{
#if (((NET_CTR_CFG_ERR_EN == DEF_ENABLED) || \
(NET_CTR_CFG_STAT_EN == DEF_ENABLED)) && \
(CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))
CPU_SR cpu_sr;
#endif
NET_BUF_HDR *pbuf_hdr;
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (Net_InitDone != DEF_YES) { /* If init NOT complete, exit rx (see Note #2). */
NetSock_RxPktDiscard(pbuf, perr);
*perr = NET_ERR_INIT_INCOMPLETE;
return;
}
#endif
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
/* ------------------- VALIDATE PTR ------------------- */
if (pbuf == (NET_BUF *)0) {
NetSock_RxPktDiscard(pbuf, perr);
NET_CTR_ERR_INC(NetSock_ErrNullPtrCtr);
return;
}
#endif
NET_CTR_STAT_INC(NetSock_StatRxPktCtr);
/* ---------------- VALIDATE SOCK PKT ----------------- */
pbuf_hdr = &pbuf->Hdr;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
NetSock_RxPktValidateBuf(pbuf_hdr, perr); /* Validate rx'd buf. */
switch (*perr) {
case NET_SOCK_ERR_NONE:
break;
case NET_ERR_INVALID_PROTOCOL:
case NET_BUF_ERR_INVALID_IX:
default:
NetSock_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
#endif
/* --------- DEMUX SOCK PKT / UPDATE RX STATS --------- */
NetSock_RxPktDemux(pbuf, pbuf_hdr, perr);
switch (*perr) {
case NET_SOCK_ERR_NONE:
NET_CTR_STAT_INC(NetSock_StatRxPktProcessedCtr);
break;
case NET_ERR_RX_DEST: /* See Note #3. */
NET_CTR_ERR_INC(NetSock_ErrRxPktDiscardedCtr); /* See Note #4. */
return; /* Prevent 'break NOT reachable' compiler warning. */
case NET_SOCK_ERR_NOT_USED:
case NET_SOCK_ERR_CLOSED:
case NET_SOCK_ERR_INVALID_SOCK:
case NET_SOCK_ERR_INVALID_FAMILY:
case NET_SOCK_ERR_INVALID_PROTOCOL:
case NET_SOCK_ERR_INVALID_OP:
case NET_SOCK_ERR_RX_Q_FULL:
case NET_SOCK_ERR_RX_Q_SIGNAL:
case NET_CONN_ERR_INVALID_CONN:
case NET_CONN_ERR_NOT_USED:
case NET_CONN_ERR_CONN_NONE:
default:
NetSock_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetSock_Open()
*
* Description : (1) Open a socket :
*
* (a) Acquire network lock
* (b) Validate socket arguments :
* (1) Socket protocol family See 'net_sock.c Note #1a'
* (2) Socket protocol See 'net_sock.c Note #1b'
* (3) Socket type See 'net_sock.c Note #1c'
*
* (c) Get socket from socket pool
* (d) Initialize socket
* (e) Release network lock
* (f) Return socket descriptor/handle identifier
* OR
* NET_SOCK_BSD_ERR_OPEN & error code, on failure
*
*
* Argument(s) : protocol_family Socket protocol family (see Note #1b1).
*
* sock_type Socket type (see Note #1b2).
*
* protocol Socket protocol (see Note #1b3).
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_SOCK_ERR_NONE Socket successfully opened.
* NET_ERR_INIT_INCOMPLETE Network initialization NOT complete.
* NET_SOCK_ERR_INVALID_FAMILY Invalid socket protocol family.
* NET_SOCK_ERR_INVALID_PROTOCOL Invalid socket protocol.
* NET_SOCK_ERR_INVALID_TYPE Invalid socket type.
*
* --- RETURNED BY NetSock_Get() : ----
* NET_SOCK_ERR_NONE_AVAIL NO available sockets to allocate.
*
* ---- RETURNED BY NetOS_Lock() : ----
* NET_OS_ERR_LOCK Network access NOT acquired.
*
* Return(s) : Socket descriptor/handle identifier, if NO errors.
*
* NET_SOCK_BSD_ERR_OPEN, otherwise.
*
* Caller(s) : socket().
*
* This function is a network protocol suite application interface (API) function & MAY be
* called by application function(s).
*
* Note(s) : (2) NetSock_Open() blocked until network initialization completes; perform NO action.
*********************************************************************************************************
*/
NET_SOCK_ID NetSock_Open (CPU_INT16S protocol_family,
CPU_INT16S sock_type,
CPU_INT16S protocol,
NET_ERR *perr)
{
#if ((NET_CTR_CFG_ERR_EN == DEF_ENABLED) && \
(CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))
CPU_SR cpu_sr;
#endif
NET_SOCK *psock;
NET_SOCK_ID sock_id;
/* ----------------- ACQUIRE NET LOCK ----------------- */
NetOS_Lock(perr);
if (*perr != NET_OS_ERR_NONE) {
return (NET_SOCK_BSD_ERR_OPEN);
}
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (Net_InitDone != DEF_YES) { /* If init NOT complete, exit (see Note #2). */
NetOS_Unlock();
*perr = NET_ERR_INIT_INCOMPLETE;
return (NET_SOCK_BSD_ERR_OPEN);
}
#endif
/*$PAGE*/
/* ---------------- VALIDATE SOCK ARGS ---------------- */
switch (protocol_family) {
#if (NET_SOCK_CFG_FAMILY == NET_SOCK_FAMILY_IP_V4)
case NET_SOCK_FAMILY_IP_V4:
switch (sock_type) {
case NET_SOCK_TYPE_DATAGRAM:
switch (protocol) {
case NET_SOCK_PROTOCOL_UDP:
break;
case NET_SOCK_PROTOCOL_DFLT:
protocol = NET_SOCK_PROTOCOL_UDP;
break;
default:
NET_CTR_ERR_INC(NetSock_ErrInvalidProtocolCtr);
NetOS_Unlock();
*perr = NET_SOCK_ERR_INVALID_PROTOCOL;
return (NET_SOCK_BSD_ERR_OPEN); /* Prevent 'break NOT reachable' compiler warning. */
}
break;
#if (NET_SOCK_CFG_TYPE_STREAM_EN == DEF_ENABLED)
case NET_SOCK_TYPE_STREAM:
switch (protocol) {
#ifdef NET_TCP_MODULE_PRESENT
case NET_SOCK_PROTOCOL_TCP:
break;
case NET_SOCK_PROTOCOL_DFLT:
protocol = NET_SOCK_PROTOCOL_TCP;
break;
#endif
default:
NET_CTR_ERR_INC(NetSock_ErrInvalidProtocolCtr);
NetOS_Unlock();
*perr = NET_SOCK_ERR_INVALID_PROTOCOL;
return (NET_SOCK_BSD_ERR_OPEN); /* Prevent 'break NOT reachable' compiler warning. */
}
break;
#endif
case NET_SOCK_TYPE_NONE:
default:
NET_CTR_ERR_INC(NetSock_ErrInvalidSockTypeCtr);
NetOS_Unlock();
*perr = NET_SOCK_ERR_INVALID_TYPE;
return (NET_SOCK_BSD_ERR_OPEN); /* Prevent 'break NOT reachable' compiler warning. */
}
break;
#endif
case NET_SOCK_FAMILY_NONE:
default:
NET_CTR_ERR_INC(NetSock_ErrInvalidFamilyCtr);
NetOS_Unlock();
*perr = NET_SOCK_ERR_INVALID_FAMILY;
return (NET_SOCK_BSD_ERR_OPEN); /* Prevent 'break NOT reachable' compiler warning. */
}
/* --------------------- GET SOCK --------------------- */
psock = NetSock_Get(perr);
if (psock == (NET_SOCK *)0) {
NetOS_Unlock();
return (NET_SOCK_BSD_ERR_OPEN); /* Rtn err from NetSock_Get(). */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -