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

📄 net_udp.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 5 页
字号:
*               (2) Although UDP data units are typically referred to as 'datagrams' (see RFC #768, Section
*                   'Introduction'), the term 'UDP packet' (see RFC #1983, 'packet') is used for UDP Receive
*                   until the packet is validated as a UDP datagram.
*
*
* Argument(s) : pbuf        Pointer to network buffer that received UDP packet.
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_UDP_ERR_NONE                UDP datagram successfully received & processed.
*                               NET_ERR_INIT_INCOMPLETE         Network initialization NOT complete.
*
*                                                               ----- RETURNED BY NetUDP_RxPktDiscard() : -----
*                               NET_ERR_RX                      Receive error; packet discarded.
*
* Return(s)   : none.
*
* Caller(s)   : NetIP_RxPktDemuxDatagram().
*
*               This function is an INTERNAL network protocol suite function & MUST NOT be called by 
*               application function(s).
*
* Note(s)     : (3) NetUDP_Rx() blocked until network initialization completes; perform NO action.
*
*               (4) Network buffer already freed by higher layer; only increment error counter.
*
*               (5) 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".
*********************************************************************************************************
*/
/*$PAGE*/
void  NetUDP_Rx (NET_BUF  *pbuf,
                 NET_ERR  *perr)
{
#if (((NET_CTR_CFG_STAT_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_BUF_HDR  *pbuf_hdr;
    NET_UDP_HDR  *pudp_hdr;
    NET_ERR       msg_err;


#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
    if (Net_InitDone != DEF_YES) {                              /* If init NOT complete, exit rx (see Note #3).         */
        NetUDP_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) {
        NetUDP_RxPktDiscard(pbuf, perr);
        NET_CTR_ERR_INC(NetUDP_ErrNullPtrCtr);
        return;
    }
#endif


    NET_CTR_STAT_INC(NetUDP_StatRxPktCtr);


                                                                /* ----------------- VALIDATE UDP PKT ----------------- */
    pbuf_hdr = &pbuf->Hdr;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
    NetUDP_RxPktValidateBuf(pbuf_hdr, perr);                    /* Validate rx'd buf.                                   */
    switch (*perr) {
        case NET_UDP_ERR_NONE:
             break;


        case NET_ERR_INVALID_PROTOCOL:
        case NET_BUF_ERR_INVALID_IX:
        default:
             NetUDP_RxPktDiscard(pbuf, perr);
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */
    }
#endif
    pudp_hdr = (NET_UDP_HDR *)&pbuf->Data[pbuf_hdr->TCP_UDP_HdrDataIx];
    NetUDP_RxPktValidate(pbuf, pbuf_hdr, pudp_hdr, perr);       /* Validate rx'd pkt.                                   */


                                                                /* ------------------ DEMUX DATAGRAM ------------------ */
    switch (*perr) {
        case NET_UDP_ERR_NONE:
             NetUDP_RxPktDemuxDatagram(pbuf, perr);
             break;


        case NET_UDP_ERR_INVALID_PORT_NBR:
        case NET_UDP_ERR_INVALID_LEN:
        case NET_UDP_ERR_INVALID_LEN_DATA:
        case NET_UDP_ERR_INVALID_CHK_SUM:
        default:
             NetUDP_RxPktDiscard(pbuf, perr);
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */
    }


/*$PAGE*/
                                                                /* ----------------- UPDATE RX STATS ------------------ */
    switch (*perr) {                                            /* Chk err from NetUDP_RxPktDemuxDatagram().            */
        case NET_APP_ERR_NONE:
        case NET_SOCK_ERR_NONE:
             NET_CTR_STAT_INC(NetUDP_StatRxDatagramProcessedCtr);
            *perr = NET_UDP_ERR_NONE;
             break;


        case NET_ERR_INIT_INCOMPLETE:
        case NET_ERR_RX:
             NET_CTR_ERR_INC(NetUDP_ErrRxPktDiscardedCtr);      /* See Note #4.                                         */
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */


        case NET_ERR_RX_DEST:
             NET_CTR_ERR_INC(NetUDP_ErrRxDestCtr);
                                                                /* Tx ICMP port unreach (see Note #5).                  */
             NetICMP_TxMsgErr(pbuf, NET_ICMP_MSG_TYPE_DEST_UNREACH, NET_ICMP_MSG_CODE_DEST_PORT, NET_ICMP_MSG_PTR_NONE, &msg_err);
             NetUDP_RxPktDiscard(pbuf, perr);
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */


        default:
             NetUDP_RxPktDiscard(pbuf, perr);
             return;                                            /* Prevent 'break NOT reachable' compiler warning.      */
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                         NetUDP_RxAppData()
*
* Description : (1) Deframe application data from received UDP packet buffer(s) :
*
*                   (a) Validate receive packet buffer(s)
*                   (b) Validate receive data buffer                                        See Note #4
*                   (c) Validate receive flags                                              See Note #5
*                   (d) Get any received IP options                                         See Note #6
*                   (e) Deframe application data from UDP packet buffer(s)
*                   (f) Free UDP packet buffer(s)
*
*
* Argument(s) : pbuf                Pointer to network buffer that received UDP datagram.
*
*               pdata_buf           Pointer to application buffer to receive application data.
*
*               data_buf_len        Size    of application receive buffer (in octets) [see Note #4].
*
*               flags               Flags to select receive options (see Note #5); bit-field flags logically OR'd :
*
*                                       NET_UDP_FLAG_RX_DATA_PEEK       Receive UDP application data without consuming
*                                                                           the data; i.e. do NOT free any UDP receive
*                                                                           packet buffer(s).
*
*               pip_opts_buf        Pointer to buffer to receive possible IP options (see Note #6a), if NO errors.
*
*               ip_opts_buf_len     Size of IP options receive buffer (in octets)    [see Note #6b].
*
*               pip_opts_len        Pointer to variable that will receive the return size of any received IP options,
*                                       if NO errors.
*
*               perr                Pointer to variable that will receive the return error code from this function :
*
*                                       NET_UDP_ERR_NONE                UDP application data successfully deframed; check 
*                                                                           return value for number of data octets received.
*                                       NET_UDP_ERR_INVALID_DATA_SIZE   UDP data receive buffer insufficient size; some, 
*                                                                           but not all, UDP application data deframed
*                                                                           into receive buffer (see Note #4b).
*
*                                       NET_ERR_INIT_INCOMPLETE         Network initialization NOT complete.
*                                       NET_UDP_ERR_NULL_PTR            Argument 'pbuf'/'pdata_buf' passed a NULL pointer.
*                                       NET_UDP_ERR_INVALID_FLAG        Invalid UDP flags.
*                                                                       
*                                                                       ------- RETURNED BY NetUDP_RxPktDiscard() : --------
*                                       NET_ERR_RX                      Receive error; packet discarded.
*
* Return(s)   : Total application data octets deframed into receive buffer, if NO errors.
*
*               0,                                                          otherwise.
*
* Caller(s)   : NetSock_RxDataHandlerDatagram(),
*               NetUDP_RxAppDataHandler().
*
*               This function is a network protocol suite application interface (API) function & MAY be 
*               called by application function(s).
*$PAGE*
* Note(s)     : (2) NetUDP_RxAppData() MUST be called with the global network lock already acquired.
*
*                   See also 'NetUDP_RxPktDemuxAppData()  Note #1a1A1b'.
*
*               (3) NetUDP_RxAppData() blocked until network initialization completes; perform NO action.
*
*               (4) (a) Application data receive buffer should be large enough to receive either ...
*
*                       (1) The maximum UDP datagram size (i.e. 65,507 octets)
*                             OR
*                       (2) The application's expected maximum UDP datagram size
*
*                   (b) If the application receive buffer size is NOT large enough for the received UDP datagram,
*                       the remaining application data octets are discarded & NET_UDP_ERR_INVALID_DATA_SIZE error 
*                       is returned.
*
*               (5) If UDP receive flag options that are NOT implemented are requested, NetUDP_RxAppData() aborts
*                   & returns appropriate error codes so that requested flag options are NOT silently ignored.
*
*               (6) (a) If ...
*
*                       (1) NO IP options were received with the UDP datagram
*                             OR
*                       (2) NO IP options receive buffer is provided by the application
*                             OR
*                       (3) IP options receive buffer NOT large enough for the received IP options
*
*                       ... then NO IP options are returned & any received IP options are silently discarded.
*
*                   (b) The IP options receive buffer size SHOULD be large enough to receive the maximum 
*                       IP options size, NET_IP_HDR_OPT_SIZE_MAX.
*
*                   (c) IP options are received from the first packet buffer.  In other words, if multiple
*                       packet buffers are received for a fragmented datagram, IP options are received from
*                       the first fragment of the datagram.
*
*                   (d) #### Received IP options should be provided/decoded via appropriate IP layer API.
*
*               (7) Pointers to variables that return length or size values MUST be initialized to return
*                   zero PRIOR to all other validation or function handling in case of any error(s).
*
*               (8) Since pointer arithmetic is based on the specific pointer data type & inherent pointer
*                   data type size, pointer arithmetic operands :
*
*                   (a) MUST be in terms of the specific pointer data type & data type size; ...
*                   (b) SHOULD NOT & in some cases MUST NOT be cast to other data types or data type sizes.
*
*               (9) (a) On any internal receive     errors, UDP receive packets are     discarded.
*
*                   (b) On any external application errors, UDP receive packets are NOT discarded;
*                       the application MAY continue to attempt to receive the application data
*                       via NetUDP_RxAppData().
*
*              (10) #### IP options arguments may NOT be necessary.
*********************************************************************************************************
*/
/*$PAGE*/
CPU_INT16U  NetUDP_RxAppData (NET_BUF     *pbuf,
                              void        *pdata_buf,
                              CPU_INT16U   data_buf_len,
                              CPU_INT16U   flags,
                              void        *pip_opts_buf,
                              CPU_INT08U   ip_opts_buf_len,
                              CPU_INT08U  *pip_opts_len,
                              NET_ERR     *perr)
{

⌨️ 快捷键说明

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