VirtualBox

Ignore:
Timestamp:
Jul 30, 2008 1:33:48 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
33857
Message:

iprt: More IPv4 checksum calculation.

File:
1 edited

Legend:

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

    r10966 r10982  
    4343 *                      Assumes the caller already checked the minimum size requirement.
    4444 */
    45 RTDECL(uint16_t) RTNetIPv4Checksum(PCRTNETIPV4 pIpHdr)
     45RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr)
    4646{
    4747    uint16_t const *paw = (uint16_t const *)pIpHdr;
     
    9696 *                      to be mapped following pIpHdr.
    9797 */
    98 RTDECL(bool) RTNetIPv4IsValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
     98RTDECL(bool) RTNetIPv4IsHdrsValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
    9999{
    100100    Assert(cbPktMax >= cbHdrMax);
     
    112112    if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_len) > cbPktMax))
    113113        return false;
    114     uint16_t u16Sum = RTNetIPv4Checksum(pIpHdr);
     114    uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr);
    115115    if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_sum) != u16Sum))
    116116        return false;
     
    120120
    121121/**
    122  * Calculates the checksum of a pseudo header given an IPv4 header.
     122 * Calculates the checksum of a pseudo header given an IPv4 header [inlined].
    123123 *
    124124 * @returns 32-bit intermediary checksum value.
    125125 * @param   pIpHdr      The IP header (network endian (big)).
    126126 */
    127 RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)
     127DECLINLINE(uint32_t) rtNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)
    128128{
    129129    uint16_t cbPayload = RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4;
     
    143143
    144144/**
     145 * Calculates the checksum of a pseudo header given an IPv4 header.
     146 *
     147 * @returns 32-bit intermediary checksum value.
     148 * @param   pIpHdr      The IP header (network endian (big)).
     149 */
     150RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)
     151{
     152    return rtNetIPv4PseudoChecksum(pIpHdr);
     153}
     154
     155
     156/**
    145157 * Calculates the checksum of a pseudo header given the individual components.
    146158 *
     
    168180
    169181/**
    170  * Adds the checksum of the UDP header to the intermediate checksum value.
     182 * Adds the checksum of the UDP header to the intermediate checksum value [inlined].
    171183 *
    172184 * @returns 32-bit intermediary checksum value.
     
    174186 * @param   iSum            The 32-bit intermediate checksum value.
    175187 */
    176 RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t iSum)
     188DECLINLINE(uint32_t) rtNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t iSum)
    177189{
    178190    iSum += pUdpHdr->uh_sport
     
    185197
    186198/**
    187  * Adds the checksum of the TCP header to the intermediate checksum value.
     199 * Adds the checksum of the UDP header to the intermediate checksum value.
     200 *
     201 * @returns 32-bit intermediary checksum value.
     202 * @param   pUdpHdr         Pointer to the UDP header to checksum, network endian (big).
     203 * @param   iSum            The 32-bit intermediate checksum value.
     204 */
     205RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t iSum)
     206{
     207    return rtNetIPv4AddUDPChecksum(pUdpHdr,iSum);
     208}
     209
     210
     211/**
     212 * Adds the checksum of the TCP header to the intermediate checksum value [inlined].
    188213 *
    189214 * @returns 32-bit intermediary checksum value.
     
    193218 * @param   iSum            The 32-bit intermediate checksum value.
    194219 */
    195 RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t iSum)
     220DECLINLINE(uint32_t) rtNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t iSum)
    196221{
    197222    uint16_t const *paw = (uint16_t const *)pTcpHdr;
     
    231256
    232257/**
    233  * Adds the checksum of the specified data segment to the intermediate checksum value.
     258 * Adds the checksum of the TCP header to the intermediate checksum value.
     259 *
     260 * @returns 32-bit intermediary checksum value.
     261 * @param   pUdpHdr         Pointer to the TCP header to checksum, network endian (big).
     262 *                          Assums the caller has already validate it and made sure the
     263 *                          entire header is present.
     264 * @param   iSum            The 32-bit intermediate checksum value.
     265 */
     266RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t iSum)
     267{
     268    return rtNetIPv4AddTCPChecksum(pTcpHdr, iSum);
     269}
     270
     271
     272/**
     273 * Adds the checksum of the specified data segment to the intermediate checksum value [inlined].
    234274 *
    235275 * @returns 32-bit intermediary checksum value.
     
    240280 *                          or UDP header (data never start at an odd offset).
    241281 */
    242 RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t iSum, bool *pfOdd)
     282DECLINLINE(uint32_t) rtNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t iSum, bool *pfOdd)
    243283{
    244284    if (*pfOdd)
     
    263303    {
    264304        iSum += *pw;
    265         pw += 2;
     305        pw++;
    266306        cbData -= 2;
    267307    }
     
    282322}
    283323
    284 
    285 /**
    286  * Finalizes a IPv4 checksum.
     324/**
     325 * Adds the checksum of the specified data segment to the intermediate checksum value.
     326 *
     327 * @returns 32-bit intermediary checksum value.
     328 * @param   pUdpHdr         Pointer to the UDP header to checksum, network endian (big).
     329 * @param   iSum            The 32-bit intermediate checksum value.
     330 * @param   pfOdd           This is used to keep track of odd bits, initialize to false
     331 *                          when starting to checksum the data (aka text) after a TCP
     332 *                          or UDP header (data never start at an odd offset).
     333 */
     334RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t iSum, bool *pfOdd)
     335{
     336    return rtNetIPv4AddDataChecksum(pvData, cbData, iSum, pfOdd);
     337}
     338
     339
     340/**
     341 * Finalizes a IPv4 checksum [inlined].
    287342 *
    288343 * @returns The checksum.
    289344 * @param   iSum            The 32-bit intermediate checksum value.
    290345 */
    291 RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t iSum)
     346DECLINLINE(uint16_t) rtNetIPv4FinalizeChecksum(uint32_t iSum)
    292347{
    293348    /* 16-bit one complement fun */
     
    298353
    299354
    300 
     355/**
     356 * Finalizes a IPv4 checksum.
     357 *
     358 * @returns The checksum.
     359 * @param   iSum            The 32-bit intermediate checksum value.
     360 */
     361RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t iSum)
     362{
     363    return rtNetIPv4FinalizeChecksum(iSum);
     364}
     365
     366
     367
     368/**
     369 * Calculates the checksum for the UDP header given the IP header,
     370 * UDP header and payload.
     371 *
     372 * @returns The checksum.
     373 * @param   pIpHdr          Pointer to the IPv4 header, in network endian (big).
     374 * @param   pUdpHdr         Pointer to the UDP header, in network endian (big).
     375 * @param   pvData          Pointer to the UDP payload. The size is taken from the
     376 *                          UDP header and the caller is supposed to have validated
     377 *                          this before calling.
     378 */
     379RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData)
     380{
     381    uint32_t iSum = RTNetIPv4PseudoChecksum(pIpHdr);
     382    iSum = RTNetIPv4AddUDPChecksum(pUdpHdr, iSum);
     383    bool fOdd = false;
     384    iSum = RTNetIPv4AddDataChecksum(pvData, RT_BE2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr), iSum, &fOdd);
     385    iSum = RTNetIPv4FinalizeChecksum(iSum);
     386    return iSum;
     387}
     388
     389
Note: See TracChangeset for help on using the changeset viewer.

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