📄 net_arp.c
字号:
*/
/*$PAGE*/
NET_ARP_ADDR_LEN NetARP_CacheGetAddrHW (CPU_INT08U *paddr_hw,
NET_ARP_ADDR_LEN addr_hw_len_buf,
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_ADDR_LEN addr_hw_len;
NET_ARP_CACHE *pcache;
/* --------------- VALIDATE HW ADDR BUF --------------- */
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (paddr_hw == (CPU_INT08U *)0) {
NET_CTR_ERR_INC(NetARP_ErrNullPtrCtr);
*perr = NET_ARP_ERR_NULL_PTR;
return (0);
}
#endif
/* Clr hw addr (see Note #2a3). */
Mem_Clr((void *)paddr_hw,
(CPU_SIZE_T) addr_hw_len_buf);
addr_hw_len = NET_ARP_CFG_HW_ADDR_LEN; /* Cfg hw addr len (see Note #2a2). */
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (addr_hw_len_buf < addr_hw_len) { /* Chk hw addr buf len (see Note #2a1). */
NET_CTR_ERR_INC(NetARP_ErrInvalidAddrLenHW_Ctr);
*perr = NET_ARP_ERR_INVALID_HW_LEN;
return (0);
}
#endif
#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 (0);
}
if (addr_protocol_len != NET_ARP_CFG_PROTOCOL_ADDR_LEN) { /* Chk protocol addr len (see Note #2b). */
NET_CTR_ERR_INC(NetARP_ErrInvalidAddrLenProtocolCtr);
*perr = NET_ARP_ERR_INVALID_PROTOCOL_LEN;
return (0);
}
#else
(void)&addr_protocol_len; /* Prevent compiler warning. */
#endif
/* ------------------ SRCH ARP CACHE ------------------ */
pcache = NetARP_CacheSrch(paddr_protocol);
if (pcache == (NET_ARP_CACHE *)0) {
*perr = NET_ARP_ERR_CACHE_NOT_FOUND;
return (0);
}
switch (pcache->State) {
case NET_ARP_CACHE_STATE_PEND: /* If ARP cache pend, hw addr NOT yet avail ... */
*perr = NET_ARP_ERR_CACHE_PEND; /* ... (see Note #4). */
return (0); /* Prevent 'break NOT reachable' compiler warning. */
case NET_ARP_CACHE_STATE_RESOLVED: /* If ARP cache resolved, hw addr avail. */
break;
case NET_ARP_CACHE_STATE_NONE:
case NET_ARP_CACHE_STATE_FREE:
default:
NetARP_CacheRemove(pcache, DEF_YES);
*perr = NET_ARP_ERR_CACHE_NOT_FOUND;
return (0); /* Prevent 'break NOT reachable' compiler warning. */
}
/* ------------------- GET HW ADDR -------------------- */
Mem_Copy((void *) paddr_hw,
(void *)&pcache->HW_Addr[0],
(CPU_SIZE_T) addr_hw_len);
*perr = NET_ARP_ERR_NONE;
return (addr_hw_len);
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetARP_CachePoolStatGet()
*
* Description : Get ARP statistics pool.
*
* Argument(s) : none.
*
* Return(s) : ARP statistics pool, if NO errors.
*
* NULL statistics pool, 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) NetARP_CachePoolStatGet() blocked until network initialization completes; return NULL
* statistics pool.
*********************************************************************************************************
*/
NET_STAT_POOL NetARP_CachePoolStatGet (void)
{
#if (CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL)
CPU_SR cpu_sr;
#endif
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
NET_ERR err;
#endif
NET_STAT_POOL stat_pool;
#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
if (Net_InitDone != DEF_YES) { /* If init NOT complete, ... */
NetStat_PoolClr(&stat_pool, &err);
return (stat_pool); /* ... rtn NULL stat pool (see Note #1). */
}
#endif
CPU_CRITICAL_ENTER();
stat_pool = NetARP_CachePoolStat;
CPU_CRITICAL_EXIT();
return (stat_pool);
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetARP_CachePoolStatResetMaxUsed()
*
* Description : Reset ARP statistics pool's maximum number of entries used.
*
* Argument(s) : none.
*
* 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) : none.
*********************************************************************************************************
*/
void NetARP_CachePoolStatResetMaxUsed (void)
{
NET_ERR err;
NetStat_PoolResetUsedMax(&NetARP_CachePoolStat, &err);
}
/*$PAGE*/
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* NetARP_RxPktValidateBuf()
*
* Description : Validate received buffer header as ARP protocol.
*
* Argument(s) : pbuf_hdr Pointer to network buffer header that received ARP packet.
* -------- Argument validated in NetARP_Rx().
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_ARP_ERR_NONE Received buffer's ARP header validated.
* NET_ERR_INVALID_PROTOCOL Buffer's protocol type is NOT ARP.
* NET_BUF_ERR_INVALID_IX Invalid buffer index.
*
* Return(s) : none.
*
* Caller(s) : NetARP_Rx().
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
static void NetARP_RxPktValidateBuf (NET_BUF_HDR *pbuf_hdr,
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
/* --------------- VALIDATE ARP BUF HDR --------------- */
if (pbuf_hdr->ProtocolHdrType != NET_PROTOCOL_TYPE_ARP) {
NET_CTR_ERR_INC(Net_ErrInvalidProtocolCtr);
*perr = NET_ERR_INVALID_PROTOCOL;
return;
}
if (pbuf_hdr->ARP_MsgIx == NET_BUF_IX_NONE) {
NET_CTR_ERR_INC(NetARP_ErrRxInvalidBufIxCtr);
*perr = NET_BUF_ERR_INVALID_IX;
return;
}
*perr = NET_ARP_ERR_NONE;
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* NetARP_RxPktValidate()
*
* Description : (1) Validate received ARP packet :
*
* (a) Convert the following ARP header fields from network-order to host-order :
*
* (1) Hardware Type
* (2) Protocol Type
* (3) Operation Code See Note #1aB
*
* (A) These fields are NOT converted directly in the received packet buffer's
* data area but are converted in local or network buffer variables ONLY.
*
* (B) To avoid storing the ARP operation code in a network buffer variable &
* passing an additional pointer to the network buffer header that received
* ARP packet, ARP operation code is converted in the following functions :
*
* (1) NetARP_RxPktValidate()
* (2) NetARP_RxPktCacheUpdate()
* (3) NetARP_RxPktReply()
* (4) NetARP_RxPktIsTargetThisHost()
*
* (C) Hardware & Protocol Addresses are NOT converted from network-order to
* host-order & MUST be accessed as multi-octet arrays.
*
* (b) Validate the received packet's following ARP header fields :
*
* (1) Hardware Type
* (2) Hardware Address Length
* (3) Hardware Address : Sender's
* (4) Protocol Type
* (5) Protocol Address Length
* (6) Protocol Address : Sender's
* (7) Operation Code
*
* (c) Update network buffer's length controls.
*
* (d) Validate ARP message length :
*
* (1) The ARP message length is compared to the remaining packet length which should
* be identical.
*
* (2) However, some network interface frame types append 'pad' octets (octets with a
* dummy value to be ignored) if the data length for the frame does NOT meet the
* frame's required minimum size. Thus, if the ARP message length is NOT greater
* than the frame's minimum size, then pad octets were appended & the ARP message
* length CANNOT be compared.
*
*
* Argument(s) : pbuf_hdr Pointer to network buffer header that received ARP packet.
* -------- Argument validated in NetARP_Rx().
*
* parp_hdr Pointer to received packet's ARP header.
* -------- Argument validated in NetARP_Rx()/NetARP_RxPktValidateBuf().
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_ARP_ERR_NONE Received packet validated.
* NET_ARP_ERR_INVALID_HW_TYPE Invalid ARP hardware type.
* NET_ARP_ERR_INVALID_HW_LEN Invalid ARP hardware address length.
* NET_ARP_ERR_INVALID_HW_ADDR Invalid ARP hardware
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -