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

📄 net_udp.c

📁 ucos+lwip用于AT91SAM7X256
💻 C
📖 第 1 页 / 共 5 页
字号:
                      (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 =  err_rtn;

    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.
*
*               (4) (a) UDP header Check-Sum field MUST be validated BEFORE (or AFTER) any multi-octet words
*                       are converted from network-order to host-order since "the sum of 16-bit integers can
*                       be computed in either byte order" [RFC #1071, Section 2.(B)].
*
*                       In other words, the UDP Datagram Check-Sum CANNOT be validated AFTER SOME but NOT ALL 
*                       multi-octet words have been converted from network-order to host-order.
*
*                   (b) However, ALL received packets' multi-octet words are converted in local or network 
*                       buffer variables ONLY (see Note #1bA).  Therefore, UDP Datagram Check-Sum may be 
*                       validated at any point.
*
*                   (c) The UDP Datagram Check-Sum MUST be validated AFTER the datagram length field has been
*                       validated so that the total UDP Datagram Length (in octets) will already be calculated
*                       for the UDP Check-Sum calculation.
*
*                       For efficiency, the UDP Datagram Check-Sum is validated AFTER all other UDP header 
*                       fields have been validated.  Thus, the iteration-intensive UDP Datagram Check-Sum is 
*                       calculated only after all other UDP header fields have been quickly validated.
*
*                   (d) (1) Before the UDP Datagram Check-Sum is validated, it is necessary to convert the 
*                           Check-Sum from network-order to host-order to verify whether the received UDP 
*                           datagram's Check-Sum is valid -- i.e. whether the UDP datagram was transmitted 
*                           with or without a computed Check-Sum (see RFC #768, Section 'Fields : Checksum').
*
*                       (2) Since the value that indicates no check-sum was computed for the received UDP
*                           datagram is one's-complement positive zero -- all check-sum bits equal to zero,
*                           a value that is endian-order independent -- it is NOT absolutely necessary to 
*                           convert the UDP Datagram Check-Sum from network-order to host-order.
*
*                           However, network data value macro's inherently convert data words from network
*                           word order to CPU word order.
*
*                           See also 'net_util.h  NETWORK DATA VALUE MACRO'S  Note #1a1'.
*
*                       (3) (A) Any UDP datagram received with NO computed check-sum is flagged so that "an
*                               application MAY optionally ... discard ... datagrams without checksums" (see
*                               RFC #1122, Section 4.1.3.4).
*
*                           (B) UDP buffer flag value to clear was previously initialized in NetBuf_Get() when 
*                               the buffer was allocated.  This buffer flag value does NOT need to be re-cleared
*                               but is shown for completeness.
*
*                   (e) (1) In addition to the UDP datagram header & data, the UDP Check-Sum calculation
*                           includes "a pseudo header of information from the IP header ... conceptually
*                           prefixed to the UDP header [which] contains the source address, the destination
*                           address, the protocol, and the UDP length" (see RFC #768, Section 'Fields :
*                           Checksum').
*
*                       (2) Since network check-sum functions REQUIRE that 16-bit one's-complement check-
*                           sum calculations be performed on headers & data arranged in network-order (see 
*                           'net_util.c  NetUtil_16BitOnesCplChkSumDataVerify()  Note #4'), UDP pseudo-header 
*                           values MUST be set or converted to network-order.
*
*                   (f) RFC #768, Section 'Fields : Checksum' specifies that "the data [is] padded with zero
*                       octets at the end (if necessary) to make a multiple of two octets".
*
*                       See also 'net_util.c  NetUtil_16BitSumDataCalc()  Note #7'.
*
*               (5) (a) Since the minimum network buffer size MUST be configured such that the entire UDP 
*                       header MUST be received in a single packet (see 'net_buf.h  NETWORK BUFFER INDEX &
*                       SIZE DEFINES  Note #1c'), after the UDP header size is decremented from the first 
*                       packet buffer's remaining number of data octets, any remaining octets MUST be user 
*                       &/or application data octets.
*
*                       (1) Note that the 'Data' index is updated regardless of a null-size data length.
*
*                   (b) If additional packet buffers exist, the remaining IP datagram 'Data' MUST be user
*                       &/or application data.  Therefore, the 'Data' length does NOT need to be adjusted 
*                       but the 'Data' index MUST be updated.
*
*                   (c) #### Total UDP Datagram Length & Data Length is duplicated in ALL fragmented packet 
*                       buffers (may NOT be necessary; remove before product release if unnecessary).
*
*               (6) RFC #1122, Sections 3.2.1 & 3.2.2 require that IP & ICMP packets with certain invalid
*                   header fields be "silently discarded".  However, NO RFC specifies how UDP should handle 
*                   received datagrams with invalid header fields.
*
*                   In addition, UDP is a "transaction oriented" protocol that does NOT guarantee "delivery
*                   and duplicate protection" of UDP datagrams (see RFC #768, Section 'Introduction').
*
*                   Therefore, it is assumed that ALL UDP datagrams with ANY invalid header fields SHOULD
*                   be silently discarded.
*********************************************************************************************************
*/
/*$PAGE*/
static  void  NetUDP_RxPktValidate (NET_BUF      *pbuf,
                                    NET_BUF_HDR  *pbuf_hdr,
                                    NET_UDP_HDR  *pudp_hdr,
                                    NET_ERR      *perr)
{
#if ((NET_CTR_CFG_ERR_EN      == DEF_ENABLED)                    && \
     (CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))

⌨️ 快捷键说明

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