⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net_icmp.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 5 页
字号:
    pmsg_req_hdr->TotLen          = (NET_BUF_SIZE)pmsg_req_hdr->ICMP_MsgLen;
    pmsg_req_hdr->ProtocolHdrType =  NET_PROTOCOL_TYPE_ICMP;
    msg_ix_data                   =  msg_ix + msg_size_hdr;


    if (msg_size_data > 0) {                                    /* Copy data into ICMP req tx buf.                      */
        NetBuf_DataWr((NET_BUF    *) pmsg_req,
                      (NET_BUF_SIZE) msg_ix_data,
                      (NET_BUF_SIZE) msg_size_data,
                      (CPU_INT08U *) p_data,
                      (NET_ERR    *)&err);
        if (err != NET_BUF_ERR_NONE) {
            NetICMP_TxPktDiscard(pmsg_req, perr);
            return (id_seq);
        }

        pmsg_req_hdr->DataIx  = (CPU_INT16U  )msg_ix_data;
        pmsg_req_hdr->DataLen = (NET_BUF_SIZE)msg_size_data;
#if 0                                                           /* Init'd in NetBuf_Get() [see Note #6].                */
    } else {
        pmsg_req_hdr->DataIx  =  NET_BUF_IX_NONE;
        pmsg_req_hdr->DataLen =  0;
#endif
    }


/*$PAGE*/
                                                                /* --------------- PREPARE ICMP REQ MSG --------------- */
    NET_ICMP_TX_GET_SEQ_NBR(msg_seq_nbr);

    switch (type) {
        case NET_ICMP_MSG_TYPE_ECHO_REQ:
             picmp_hdr_echo       = (NET_ICMP_HDR_ECHO *)&pmsg_req->Data[pmsg_req_hdr->ICMP_MsgIx];
             picmp_hdr_echo->Type =  NET_ICMP_MSG_TYPE_ECHO_REQ;
             picmp_hdr_echo->Code =  NET_ICMP_MSG_CODE_ECHO_REQ;
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_echo->ID,     &id);
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_echo->SeqNbr, &msg_seq_nbr);
                                                                            /* Calc ICMP msg chk sum (see Note #7).     */
             NET_UTIL_VAL_SET_NET_16(&picmp_hdr_echo->ChkSum, 0x0000);      /* Clr           chk sum (see Note #7b).    */
             msg_chk_sum = NetUtil_16BitOnesCplChkSumDataCalc((void     *) pmsg_req,
                                                              (void     *) 0,
                                                              (CPU_INT16U) 0,
                                                              (NET_ERR  *)&err);
             NET_UTIL_VAL_COPY_16(&picmp_hdr_echo->ChkSum, &msg_chk_sum);   /* Copy chk sum in net order (see Note #7c).*/
             break;


        case NET_ICMP_MSG_TYPE_TS_REQ:
             picmp_hdr_ts       = (NET_ICMP_HDR_TS   *)&pmsg_req->Data[pmsg_req_hdr->ICMP_MsgIx];
             picmp_hdr_ts->Type =  NET_ICMP_MSG_TYPE_TS_REQ;
             picmp_hdr_ts->Code =  NET_ICMP_MSG_CODE_TS_REQ;
             ts                 =  NetUtil_TS_Get();                        /* See Note #1cB1.                          */
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_ts->ID,           &id);
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_ts->SeqNbr,       &msg_seq_nbr);
             NET_UTIL_VAL_COPY_SET_NET_32(&picmp_hdr_ts->TS_Originate, &ts);
             NET_UTIL_VAL_SET_NET_32(&picmp_hdr_ts->TS_Rx,  NET_TS_NONE);
             NET_UTIL_VAL_SET_NET_32(&picmp_hdr_ts->TS_Tx,  NET_TS_NONE);
                                                                            /* Calc ICMP msg chk sum (see Note #7).     */
             NET_UTIL_VAL_SET_NET_16(&picmp_hdr_ts->ChkSum, 0x0000);        /* Clr           chk sum (see Note #7b).    */
             msg_chk_sum = NetUtil_16BitOnesCplChkSumHdrCalc((void     *) picmp_hdr_ts,
                                                             (CPU_INT16U) pmsg_req_hdr->ICMP_MsgLen,
                                                             (NET_ERR  *)&err);
             NET_UTIL_VAL_COPY_16(&picmp_hdr_ts->ChkSum, &msg_chk_sum);     /* Copy chk sum in net order (see Note #7c).*/
             break;


        case NET_ICMP_MSG_TYPE_ADDR_MASK_REQ:
             picmp_hdr_addr       = (NET_ICMP_HDR_ADDR_MASK *)&pmsg_req->Data[pmsg_req_hdr->ICMP_MsgIx];
             picmp_hdr_addr->Type =  NET_ICMP_MSG_TYPE_ADDR_MASK_REQ;
             picmp_hdr_addr->Code =  NET_ICMP_MSG_CODE_ADDR_MASK_REQ;
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_addr->ID,     &id);
             NET_UTIL_VAL_COPY_SET_NET_16(&picmp_hdr_addr->SeqNbr, &msg_seq_nbr);
             NET_UTIL_VAL_SET_NET_32(&picmp_hdr_addr->AddrMask, NET_IP_ADDR_NONE);
                                                                            /* Calc ICMP msg chk sum (see Note #7).     */
             NET_UTIL_VAL_SET_NET_16(&picmp_hdr_addr->ChkSum,   0x0000);    /* Clr           chk sum (see Note #7b).    */
             msg_chk_sum = NetUtil_16BitOnesCplChkSumHdrCalc((void     *) picmp_hdr_addr,
                                                             (CPU_INT16U) pmsg_req_hdr->ICMP_MsgLen,
                                                             (NET_ERR  *)&err);
             NET_UTIL_VAL_COPY_16(&picmp_hdr_addr->ChkSum, &msg_chk_sum);   /* Copy chk sum in net order (see Note #7c).*/
             break;


        default:                                                /* See Note #4.                                         */
             NetICMP_TxPktDiscard(pmsg_req, perr);
             return (id_seq);                                   /* Prevent 'break NOT reachable' compiler warning.      */
    }

    if (err != NET_UTIL_ERR_NONE) {                             /* Chk err from NetUtil_16BitOnesCplChkSum()'s.         */
        NetICMP_TxPktDiscard(pmsg_req, perr);
        return (id_seq);
    }



/*$PAGE*/
                                                                /* ----------------- TX ICMP REQ MSG ------------------ */
    NetIP_Tx((NET_BUF   *)pmsg_req,
             (NET_IP_ADDR)NetIP_AddrThisHost,
             (NET_IP_ADDR)addr_dest,
             (NET_IP_TOS )TOS,
             (NET_IP_TTL )TTL,
             (CPU_INT16U )flags,
             (void      *)popts,
             (NET_ERR   *)perr);



                                                                /* ------- FREE ICMP REQ MSG / UPDATE TX STATS -------- */
    switch (*perr) {
        case NET_IP_ERR_NONE:
             NetICMP_TxPktFree(pmsg_req);
             NET_CTR_STAT_INC(NetICMP_StatTxMsgCtr);
             NET_CTR_STAT_INC(NetICMP_StatTxMsgReqCtr);
             break;


        case NET_ERR_INIT_INCOMPLETE:
        case NET_ERR_TX:
             NET_CTR_ERR_INC(NetICMP_ErrTxPktDiscardedCtr);     /* See Note #8.                                         */
             return (id_seq);                                   /* Prevent 'break NOT reachable' compiler warning.      */


        default:
             NetICMP_TxPktDiscard(pmsg_req, perr);
             return (id_seq);                                   /* Prevent 'break NOT reachable' compiler warning.      */
    }



   *perr          = NET_ICMP_ERR_NONE;

                                                                /* ----------- RTN ICMP REQ MSG ID/SEQ NBR ------------ */
    id_seq.ID     = id;
    id_seq.SeqNbr = msg_seq_nbr;

    return (id_seq);                                            
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                        NetICMP_HandlerRx()
*
* Description : Handle & reply to incoming IP packets for ICMP error &/or status conditions.
*
*               (1) (a) When network low-resources status is set, transmit  ICMP Source Quench Error Messages.
*
*                   (b) When network low-resources status clears, clear the ICMP Transmit Source Quench List.
*
*
* Argument(s) : pbuf        Pointer to network buffer that received an IP packet.
*               ----        Argument validated in NetIP_RxPktValidate()
*                                                   by NetIP_Rx().
*
* Return(s)   : none.
*
* Caller(s)   : NetIP_RxPktValidate().
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : none.
*********************************************************************************************************
*/

void  NetICMP_HandlerRx (NET_BUF  *pbuf)
{
#if (NET_ICMP_CFG_TX_SRC_QUENCH_EN == DEF_ENABLED)
    NET_DBG_STATUS  rsrc_lo;


    rsrc_lo = NetDbg_MonTaskStatGetHandlerLo();                 /* Get lo rsrc's status.                                */

    if (rsrc_lo != NET_DBG_STATUS_OK) {                         /* If net rsrc's lo, ...                                */
        NetICMP_HandlerTxSrcQuench(pbuf);                       /* ... tx ICMP src quench err to IP pkt src host.       */

    } else {                                                    /* Else if ICMP tx src quench list avail, ...           */
        if (NetICMP_TxSrcQuenchListHead != (NET_ICMP_TX_SRC_QUENCH *)0) {
            NetICMP_HandlerTxSrcQuenchClrList();                /* ... clr tx src quench list.                          */
        }
    }

#else
   (void)&pbuf;                                                 /* Prevent compiler warning.                            */
#endif
}


/*$PAGE*/
/*
*********************************************************************************************************
*********************************************************************************************************
*                                           LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                     NetICMP_RxPktValidateBuf()
*
* Description : Validate received buffer header as ICMP protocol.
*
* Argument(s) : pbuf_hdr    Pointer to network buffer header that received ICMP packet.
*               --------    Argument validated in NetICMP_Rx().
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_ICMP_ERR_NONE               Received buffer's ICMP header validated.
*                               NET_ERR_INVALID_PROTOCOL        Buffer's protocol type is NOT ICMP.
*                               NET_BUF_ERR_INVALID_IX          Invalid buffer index.
*
* Return(s)   : none.
*
* Caller(s)   : NetICMP_Rx().
*
* Note(s)     : none.
*********************************************************************************************************
*/

#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
static  void  NetICMP_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 ICMP BUF HDR -------------- */
    if (pbuf_hdr->ProtocolHdrType != NET_PROTOCOL_TYPE_ICMP) {
        NET_CTR_ERR_INC(Net_ErrInvalidProtocolCtr);
       *perr = NET_ERR_INVALID_PROTOCOL;
        return;
    }

    if (pbuf_hdr->ICMP_MsgIx == NET_BUF_IX_NONE) {
        NET_CTR_ERR_INC(NetICMP_ErrRxInvalidBufIxCtr);
       *perr = NET_BUF_ERR_INVALID_IX;
        return;
    }

   *perr = NET_ICMP_ERR_NONE;
}
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                       NetICMP_RxPktValidate()
*
* Description : (1) Validate received ICMP packet :
*
*                   (a) Validate the received packet's destination (see Note #3).
*
*                   (b) (1) Validate the received packet's following ICMP message fields :
*
*                           (A) Type
*                           (B) Code
*                           (C) Message Length
*                           (D) Pointer                         See RFC  #792, Section 'Parameter Problem Message'
*                           (E) Check-Sum                       See Note #7a
*
*                       (2) Validation ignores the following ICMP header fields :
*
*                           (A) Unused                          See RFC # 792, Section 'Message Formats'
*                           (B) Data                            See RFC #1122, Sections 3.2.2 & 3.2.2.6
*                           (C) Identification (ID)             See RFC # 792, Sections 'Echo or Echo Reply Message'
*                                                                   & 'Timestamp or Timestamp Reply Message'
*                           (D) Sequence Number                 See RFC # 792, Sections 'Echo or Echo Reply Message'
*                                                                   & 'Timestamp or Timestamp Reply Message'
*
*                   (c) Convert the following ICMP message fields from network-order to host-order :
*
*                       (1) Check-Sum                           See Note #7c
*
*                           (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.
*
*                   (d) Update network buffer's length controls.
*
*                   (e) Demultiplex ICMP message type.
*
*
* Argument(s) : pbu

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -