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

📄 net_udp.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 5 页
字号:
*                               "if the message is too long to pass through the underlying protocol, send() 
*                               shall fail and no data shall be transmitted".
*
*                           (B) Since IP transmit fragmentation is NOT currently supported (see 'net_ip.h 
*                               Note #1e'), if the requested datagram transmit data length is greater than 
*                               the UDP MTU, then NO data is transmitted & NET_UDP_ERR_INVALID_DATA_SIZE
*                               error is returned.
*
*                   (b) 'data_len' of 0 octets NOT allowed.
*
*               (7) On ANY transmit error, any remaining application data transmit is immediately aborted.
*
*               (8) Network buffer already freed by lower layer; only increment error counter.
*********************************************************************************************************
*/

CPU_INT16U  NetUDP_TxAppData (void              *p_data,
                              CPU_INT16U         data_len,
                              NET_IP_ADDR        src_addr,
                              NET_UDP_PORT_NBR   src_port,
                              NET_IP_ADDR        dest_addr,
                              NET_UDP_PORT_NBR   dest_port,
                              NET_IP_TOS         TOS,
                              NET_IP_TTL         TTL,
                              CPU_INT16U         flags_udp,
                              CPU_INT16U         flags_ip,
                              void              *popts_ip,
                              NET_ERR           *perr)
{
#if (((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)                    || \
      (NET_ERR_CFG_ARG_CHK_DBG_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       *pbuf;
    NET_BUF_HDR   *pbuf_hdr;
    NET_BUF_SIZE   buf_size_max;
    NET_BUF_SIZE   buf_size_max_data;
    NET_BUF_SIZE   data_ix_pkt;
    NET_BUF_SIZE   data_len_pkt;
    CPU_INT16U     data_len_tot;
    CPU_INT08U    *p_data_pkt;


#if (NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
    if (Net_InitDone != DEF_YES) {                              /* If init NOT complete, exit tx (see Note #3).         */
       *perr =  NET_ERR_INIT_INCOMPLETE;
        return (0);
    }
#endif


/*$PAGE*/
#if ((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) || \
     (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED))
                                                                /* ---------------- VALIDATE APP DATA ----------------- */
    if (p_data == (void *)0) {
       *perr =  NET_UDP_ERR_NULL_PTR;
        return (0);
    }
    if (data_len < 1) {                                         /* Validate data len (see Note #6b).                    */
        NET_CTR_ERR_INC(NetUDP_ErrTxInvalidSizeCtr);
       *perr =  NET_UDP_ERR_INVALID_DATA_SIZE;
        return (0);
    }
#endif

                                                                /* ------------------- TX APP DATA -------------------- */
                                                                /* Calc buf max data size.                              */
    data_ix_pkt       = NET_BUF_DATA_TX_IX;
    buf_size_max      = NetBuf_GetMaxSize((NET_BUF    *)0,
                                          (NET_BUF_SIZE)data_ix_pkt);
    buf_size_max_data = DEF_MIN(buf_size_max, NET_UDP_MTU);

    if (data_len > buf_size_max_data) {                         /* If data len > max data size, abort tx ...            */
       *perr =  NET_UDP_ERR_INVALID_DATA_SIZE;                  /* ... & rtn size err (see Note #6a2B).                 */
        return (0);

    } else {                                                    /* Else limit pkt data len to data len.                 */
        data_len_pkt = (NET_BUF_SIZE)data_len;
    }

                                                                
    data_len_tot =  0;
    p_data_pkt   = (CPU_INT08U *)p_data;

    pbuf = NetBuf_Get((NET_BUF_SIZE)data_len_pkt,               /* Get app data tx buf.                                 */
                      (NET_BUF_SIZE)data_ix_pkt,
                      (CPU_INT16U  )NET_BUF_FLAG_NONE,
                      (NET_ERR    *)perr);
    if (*perr != NET_BUF_ERR_NONE) {
         NetUDP_TxPktDiscard(pbuf);
         return (data_len_tot);
    }

    NetBuf_DataWr((NET_BUF    *)pbuf,                           /* Wr app data into app data tx buf.                    */
                  (NET_BUF_SIZE)data_ix_pkt,
                  (NET_BUF_SIZE)data_len_pkt,
                  (CPU_INT08U *)p_data_pkt,
                  (NET_ERR    *)perr);
    if (*perr != NET_BUF_ERR_NONE) {
         NetUDP_TxPktDiscard(pbuf);
         return (data_len_tot);
    }

                                                                /* Init app data tx buf ctrls.                          */
    pbuf_hdr                  = &pbuf->Hdr;
    pbuf_hdr->DataIx          = (CPU_INT16U  )data_ix_pkt;
    pbuf_hdr->DataLen         = (NET_BUF_SIZE)data_len_pkt;
    pbuf_hdr->TotLen          = (NET_BUF_SIZE)pbuf_hdr->DataLen;
    pbuf_hdr->ProtocolHdrType =  NET_PROTOCOL_TYPE_APP;

    NetUDP_Tx(pbuf,                                             /* Tx app data buf via UDP tx.                          */
              src_addr,
              src_port,
              dest_addr,
              dest_port,
              TOS,
              TTL,
              flags_udp,
              flags_ip,
              popts_ip,
              perr);
    if (*perr != NET_UDP_ERR_NONE) {
         return (data_len_tot);
    }

    NetUDP_TxPktFree(pbuf);                                     /* Free app data tx buf.                                */


    data_len_tot += data_len_pkt;                               /* Calc tot app data len tx'd.                          */



   *perr =  NET_UDP_ERR_NONE;

    return (data_len_tot);
}


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

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

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

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

   *perr = NET_UDP_ERR_NONE;
}
#endif


/*$PAGE*/
/*
*********************************************************************************************************
*                                       NetUDP_RxPktValidate()
*
* Description : (1) Validate received UDP packet :
*
*                   (a) Validate the received packet's following UDP header fields :
*
*                       (1) Source      Port
*                       (2) Destination Port
*                       (3) Datagram Length                             See Note #3
*                       (4) Check-Sum                                   See Note #4
*
*                   (b) Convert the following UDP header fields from network-order to host-order :
*
*                       (1) Source      Port                            See Notes #1bB1
*                       (2) Destination Port                            See Notes #1bB2
*                       (3) Datagram Length                             See Notes #1bB3
*                       (4) Check-Sum                                   See Note  #4d
*
*                           (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) The following UDP header fields are converted & stored in network buffer
*                               variables :
*
*                               (1) Source      Port
*                               (2) Destination Port
*                               (3) Datagram Length
*
*                   (c) Update network buffer's protocol controls.
*
*
* Argument(s) : pbuf        Pointer to network buffer that received UDP packet.
*               ----        Argument checked   in NetUDP_Rx().
*
*               pbuf_hdr    Pointer to network buffer header.
*               --------    Argument validated in NetUDP_Rx().
*
*               pudp_hdr    Pointer to received packet's UDP header.
*               --------    Argument validated in NetUDP_Rx()/NetUDP_RxPktValidateBuf().
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_UDP_ERR_NONE                Received packet validated.
*                               NET_UDP_ERR_INVALID_PORT_NBR    Invalid UDP port number.
*                               NET_UDP_ERR_INVALID_LEN         Invalid UDP datagram      length.
*                               NET_UDP_ERR_INVALID_LEN_DATA    Invalid UDP datagram data length.
*                               NET_UDP_ERR_INVALID_CHK_SUM     Invalid UDP check-sum.
*
* Return(s)   : none.
*
* Caller(s)   : NetUDP_Rx().
*$PAGE*
* Note(s)     : (2) See 'net_udp.h  UDP HEADER' for UDP header format.
*
*               (3) In addition to validating that the UDP Datagram Length is greater than or equal to the 
*                   minimum UDP header length,     the UDP Datagram Length is compared to the remaining IP 
*                   Datagram Length which should be identical.
*

⌨️ 快捷键说明

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