VirtualBox

Changeset 11012 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jul 30, 2008 7:24:57 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
33891
Message:

iprt: More IPv4 validating / checksumming.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/checksum/ipv4.cpp

    r10998 r11012  
    8585
    8686
    87 
    8887/**
    8988 * Verifies the header version, header size, packet size, and header checksum
     
    9796 *                      to be mapped following pIpHdr.
    9897 */
    99 RTDECL(bool) RTNetIPv4IsHdrsValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
    100 {
     98RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
     99{
     100    /*
     101     * The header fields.
     102     */
    101103    Assert(cbPktMax >= cbHdrMax);
    102104    if (RT_UNLIKELY(cbHdrMax < RTNETIPV4_MIN_LEN))
     
    113115    if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_len) > cbPktMax))
    114116        return false;
     117
     118    /*
     119     * The header checksum.
     120     */
    115121    uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr);
    116122    if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_sum) != u16Sum))
     
    380386RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData)
    381387{
    382     uint32_t iSum = RTNetIPv4PseudoChecksum(pIpHdr);
    383     iSum = RTNetIPv4AddUDPChecksum(pUdpHdr, iSum);
     388    uint32_t iSum = rtNetIPv4PseudoChecksum(pIpHdr);
     389    iSum = rtNetIPv4AddUDPChecksum(pUdpHdr, iSum);
    384390    bool fOdd = false;
    385     iSum = RTNetIPv4AddDataChecksum(pvData, RT_BE2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr), iSum, &fOdd);
    386     iSum = RTNetIPv4FinalizeChecksum(iSum);
    387     return iSum;
    388 }
    389 
    390 
     391    iSum = rtNetIPv4AddDataChecksum(pvData, RT_BE2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr), iSum, &fOdd);
     392    iSum = rtNetIPv4FinalizeChecksum(iSum);
     393    return iSum;
     394}
     395
     396
     397/**
     398 * Simple verficiation of an UDP packet size.
     399 *
     400 * @returns true if valid, false if invalid.
     401 * @param   pIpHdr          Pointer to the IPv4 header, in network endian (big).
     402 *                          This is assumed to be valid and the minimum size being mapped.
     403 * @param   pUdpHdr         Pointer to the UDP header, in network endian (big).
     404 * @param   cbPktMax        The max UDP packet size, UDP header and payload (data).
     405 */
     406DECLINLINE(bool) rtNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax)
     407{
     408    /*
     409     * Size validation.
     410     */
     411    if (RT_UNLIKELY(cbPktMax < RTNETUDP_MIN_LEN))
     412        return false;
     413    size_t cb = RT_BE2H_U16(pUdpHdr->uh_ulen);
     414    if (RT_UNLIKELY(cb > cbPktMax))
     415        return false;
     416    if (RT_UNLIKELY(cb > RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4))
     417        return false;
     418    return true;
     419}
     420
     421
     422/**
     423 * Simple verficiation of an UDP packet size.
     424 *
     425 * @returns true if valid, false if invalid.
     426 * @param   pIpHdr          Pointer to the IPv4 header, in network endian (big).
     427 *                          This is assumed to be valid and the minimum size being mapped.
     428 * @param   pUdpHdr         Pointer to the UDP header, in network endian (big).
     429 * @param   cbPktMax        The max UDP packet size, UDP header and payload (data).
     430 */
     431RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax)
     432{
     433    return rtNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbPktMax);
     434}
     435
     436
     437/**
     438 * Simple verficiation of an UDP packet (size + checksum).
     439 *
     440 * @returns true if valid, false if invalid.
     441 * @param   pIpHdr          Pointer to the IPv4 header, in network endian (big).
     442 *                          This is assumed to be valid and the minimum size being mapped.
     443 * @param   pUdpHdr         Pointer to the UDP header, in network endian (big).
     444 * @param   pvData          Pointer to the data, assuming it's one single segment
     445 *                          and that cbPktMax - sizeof(RTNETUDP) is mapped here.
     446 * @param   cbPktMax        The max UDP packet size, UDP header and payload (data).
     447 */
     448RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax)
     449{
     450    if (RT_UNLIKELY(!rtNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbPktMax)))
     451        return false;
     452    uint16_t u16Sum = RTNetIPv4UDPChecksum(pIpHdr, pUdpHdr, pvData);
     453    if (RT_UNLIKELY(RT_BE2H_U16(pUdpHdr->uh_sum) != u16Sum))
     454        return false;
     455    return true;
     456}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette