📄 net_arp.c
字号:
* corresponds to the desired protocol address.
*
* See also 'NetARP_CacheGetAddrHW() Note #1'.
*
*
* Argument(s) : paddr_protocol Pointer to protocol address to probe local network (see Note #3).
*
* addr_protocol_len Length of protocol address (in octets) [see Note #4].
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_ARP_ERR_NONE ARP Request successfully transmitted to
* probe local network for the desired
* protocol address (see Note #2).
* NET_ARP_ERR_NULL_PTR Argument 'paddr_protocol' passed a NULL
* pointer.
* NET_ARP_ERR_INVALID_PROTOCOL_LEN Invalid ARP protocol address length.
*
* --- RETURNED BY NetARP_CacheCfg() : ----
* NET_ARP_ERR_CACHE_NONE_AVAIL NO available ARP caches to allocate.
* NET_ARP_ERR_CACHE_INVALID_TYPE ARP cache is NOT a valid cache type.
* NET_TMR_ERR_NULL_OBJ Argument 'obj' passed a NULL pointer.
* NET_TMR_ERR_NULL_FNCT Argument 'fnct' passed a NULL pointer.
* NET_TMR_ERR_NONE_AVAIL NO available timers to allocate.
* NET_TMR_ERR_INVALID_TYPE Network timer is NOT a valid timer type.
*
* Return(s) : none.
*
* Caller(s) : Application.
*
* This function is a network protocol suite application interface (API) function & MAY be
* called by application function(s).
*
* Note(s) : (3) 'paddr_protocol' MUST point to a valid protocol address that is in network-order.
*
* See also 'NetARP_CacheHandler() Note #2e3'.
*
* (4) The length of the protocol address MUST be equal to NET_ARP_CFG_PROTOCOL_ADDR_LEN
* & is included for correctness & completeness.
*********************************************************************************************************
*/
/*$PAGE*/
void NetARP_ProbeAddrOnNet (CPU_INT08U *paddr_protocol,
NET_ARP_ADDR_LEN addr_protocol_len,
NET_ERR *perr)
{
#if ((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) && \
(NET_CTR_CFG_ERR_EN == DEF_ENABLED) && \
(CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))
CPU_SR cpu_sr;
#endif
NET_ARP_CACHE *pcache;
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------ VALIDATE PROTOCOL ADDR BUF ------------ */
if (paddr_protocol == (CPU_INT08U *)0) {
NET_CTR_ERR_INC(NetARP_ErrNullPtrCtr);
*perr = NET_ARP_ERR_NULL_PTR;
return;
}
if (addr_protocol_len != NET_ARP_CFG_PROTOCOL_ADDR_LEN) { /* Chk protocol addr len (see Note #4). */
NET_CTR_ERR_INC(NetARP_ErrInvalidAddrLenProtocolCtr);
*perr = NET_ARP_ERR_INVALID_PROTOCOL_LEN;
return;
}
#else
(void)&addr_protocol_len; /* Prevent compiler warning. */
#endif
/* --------- REMOVE PROTOCOL ADDR'S ARP CACHE --------- */
pcache = NetARP_CacheSrch(paddr_protocol);
if (pcache != (NET_ARP_CACHE *)0) { /* If protocol addr's ARP cache avail, ... */
NetARP_CacheRemove(pcache, DEF_YES); /* ... remove from ARP Cache List (see Note #1a). */
}
/* ------------------ CFG ARP CACHE ------------------- */
pcache = NetARP_CacheCfg((CPU_INT08U *)0,
(CPU_INT08U *)paddr_protocol,
(CPU_FNCT_PTR)NetARP_CacheReqTimeout,
(NET_TMR_TICK)NetARP_ReqTimeout_tick,
(NET_ERR *)perr);
if (*perr != NET_ARP_ERR_NONE) {
return;
}
pcache->State = NET_ARP_CACHE_STATE_PEND;
/* ------- INSERT ARP CACHE INTO ARP CACHE LIST ------- */
NetARP_CacheInsert(pcache);
/* -------------------- TX ARP REQ -------------------- */
NetARP_TxReq(pcache); /* Tx ARP req to probe local net for protocol addr. */
*perr = NET_ARP_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetARP_Rx()
*
* Description : (1) Process received ARP packets & update ARP Cache List :
*
* (a) Validate ARP packet
* (b) Update ARP cache
* (c) Prepare & transmit an ARP Reply for a received ARP Request
* (d) Free ARP packet
* (e) Update receive statistics
*
*
* Argument(s) : pbuf Pointer to network buffer that received ARP packet.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_ARP_ERR_NONE ARP packet successfully received & processed.
* NET_ERR_INIT_INCOMPLETE Network initialization NOT complete.
*
* ---- RETURNED BY NetARP_RxPktDiscard() : ----
* NET_ERR_RX Receive error; packet discarded.
*
* Return(s) : none.
*
* Caller(s) : NetIF_RxPktFrameDemux().
*
* This function is a network protocol suite to network interface (IF) function & SHOULD be
* called only by appropriate network interface function(s).
*
* Note(s) : (2) NetARP_Rx() blocked until network initialization completes.
*********************************************************************************************************
*/
void NetARP_Rx (NET_BUF *pbuf,
NET_ERR *perr)
{
#if ((((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED) && \
(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
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
NET_CTR *pctr;
#endif
NET_BUF_HDR *pbuf_hdr;
NET_ARP_HDR *parp_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). */
*perr = NET_ERR_INIT_INCOMPLETE;
return;
}
#endif
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED) /* ------------------- VALIDATE PTR ------------------- */
if (pbuf == (NET_BUF *)0) {
NetARP_RxPktDiscard(pbuf, perr);
NET_CTR_ERR_INC(NetARP_ErrNullPtrCtr);
return;
}
#endif
NET_CTR_STAT_INC(NetARP_StatRxPktCtr);
/* ----------------- VALIDATE ARP PKT ----------------- */
pbuf_hdr = &pbuf->Hdr;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
NetARP_RxPktValidateBuf(pbuf_hdr, perr); /* Validate rx'd buf. */
switch (*perr) {
case NET_ARP_ERR_NONE:
break;
case NET_ERR_INVALID_PROTOCOL:
case NET_BUF_ERR_INVALID_IX:
default:
NetARP_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
#endif
parp_hdr = (NET_ARP_HDR *)&pbuf->Data[pbuf_hdr->ARP_MsgIx];
NetARP_RxPktValidate(pbuf_hdr, parp_hdr, perr); /* Validate rx'd pkt. */
/*$PAGE*/
/* ----------------- UPDATE ARP CACHE ----------------- */
switch (*perr) { /* Chk err from NetARP_RxPktValidate(). */
case NET_ARP_ERR_NONE:
NetARP_RxPktCacheUpdate(parp_hdr, perr);
break;
case NET_ARP_ERR_INVALID_HW_TYPE:
case NET_ARP_ERR_INVALID_HW_LEN:
case NET_ARP_ERR_INVALID_HW_ADDR:
case NET_ARP_ERR_INVALID_PROTOCOL_TYPE:
case NET_ARP_ERR_INVALID_PROTOCOL_LEN:
case NET_ARP_ERR_INVALID_PROTOCOL_ADDR:
case NET_ARP_ERR_INVALID_OP_CODE:
case NET_ARP_ERR_INVALID_OP_ADDR:
case NET_ARP_ERR_INVALID_LEN_MSG:
default:
NetARP_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
/* ------------------- TX ARP REPLY ------------------- */
switch (*perr) { /* Chk err from NetARP_RxPktCacheUpdate(). */
case NET_ARP_ERR_CACHE_RESOLVED:
NetARP_RxPktReply(parp_hdr, perr);
break;
case NET_ARP_ERR_CACHE_NONE_AVAIL:
case NET_ARP_ERR_CACHE_INVALID_TYPE:
case NET_ARP_ERR_INVALID_OP_CODE:
case NET_ARP_ERR_RX_TARGET_REPLY:
case NET_ARP_ERR_RX_TARGET_NOT_THIS_HOST:
case NET_TMR_ERR_NULL_OBJ:
case NET_TMR_ERR_NULL_FNCT:
case NET_TMR_ERR_NONE_AVAIL:
case NET_TMR_ERR_INVALID_TYPE:
default:
NetARP_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
/* ---------- FREE ARP PKT / UPDATE RX STATS ---------- */
switch (*perr) { /* Chk err from NetARP_RxPktReply(). */
case NET_ARP_ERR_RX_REQ_TX_REPLY:
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
pctr = &NetARP_StatRxMsgReqProcessedCtr;
#endif
break;
case NET_ARP_ERR_RX_REPLY_TX_PKTS:
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
pctr = &NetARP_StatRxMsgReplyProcessedCtr;
#endif
break;
case NET_ARP_ERR_INVALID_OP_CODE:
case NET_ARP_ERR_RX_TARGET_NOT_THIS_HOST:
default:
NetARP_RxPktDiscard(pbuf, perr);
return; /* Prevent 'break NOT reachable' compiler warning. */
}
NetARP_RxPktFree(pbuf);
NET_CTR_STAT_INC(NetARP_StatRxMsgProcessedCtr);
NET_CTR_STAT_INC(*pctr);
*perr = NET_ARP_ERR_NONE;
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetARP_CacheHandler()
*
* Description : (1) Resolve destination hardware address for transmit data packet :
*
* (a) Search ARP Cache List for ARP cache with corresponding protocol address
* (b) If ARP cache found, handle packet based on ARP cache state :
*
* (1) PENDING -> Enqueue transmit packet buffer to ARP cache
* (2) RESOLVED -> Copy ARP cache's hardware address to data packet;
* Return to Network Interface to transmit data packet
*
* (c) If ARP cache NOT found, allocate new ARP cache in 'PENDING' state (see Note #1b1)
*
* See 'net_arp.h ARP CACHE STATES' for ARP cache state diagram.
*
* (2) This ARP cache handler function assumes the following :
*
* (a) ALL ARP caches in the ARP Cache List are valid [validated by NetARP_CacheGet()]
* (b) ANY ARP cache in the 'PENDING' state MAY have already enqueued at LEAST one
* transmit packet buffer when ARP cache allocated [see NetARP_CacheGet()]
* (c) ALL ARP caches in the 'RESOLVED' state have valid hardware addresses
* (d) ALL transmit buffers enqueued on any ARP cache are valid
* (e) Buffer's ARP address pointers pre-configured by Network Interface to point to :
*
* (1) 'ARP_AddrProtocolPtr' Pointer to the protocol address used to
* resolve the hardware address
* (2) 'ARP_AddrHW_Ptr' Pointer to memory buffer to return the
* resolved hardware address
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -