Changeset 10982 in vbox for trunk/src/VBox/Runtime/common/checksum
- Timestamp:
- Jul 30, 2008 1:33:48 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 33857
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/checksum/ipv4.cpp
r10966 r10982 43 43 * Assumes the caller already checked the minimum size requirement. 44 44 */ 45 RTDECL(uint16_t) RTNetIPv4 Checksum(PCRTNETIPV4 pIpHdr)45 RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr) 46 46 { 47 47 uint16_t const *paw = (uint16_t const *)pIpHdr; … … 96 96 * to be mapped following pIpHdr. 97 97 */ 98 RTDECL(bool) RTNetIPv4Is Valid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)98 RTDECL(bool) RTNetIPv4IsHdrsValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax) 99 99 { 100 100 Assert(cbPktMax >= cbHdrMax); … … 112 112 if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_len) > cbPktMax)) 113 113 return false; 114 uint16_t u16Sum = RTNetIPv4 Checksum(pIpHdr);114 uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr); 115 115 if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_sum) != u16Sum)) 116 116 return false; … … 120 120 121 121 /** 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]. 123 123 * 124 124 * @returns 32-bit intermediary checksum value. 125 125 * @param pIpHdr The IP header (network endian (big)). 126 126 */ 127 RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)127 DECLINLINE(uint32_t) rtNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr) 128 128 { 129 129 uint16_t cbPayload = RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4; … … 143 143 144 144 /** 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 */ 150 RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr) 151 { 152 return rtNetIPv4PseudoChecksum(pIpHdr); 153 } 154 155 156 /** 145 157 * Calculates the checksum of a pseudo header given the individual components. 146 158 * … … 168 180 169 181 /** 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]. 171 183 * 172 184 * @returns 32-bit intermediary checksum value. … … 174 186 * @param iSum The 32-bit intermediate checksum value. 175 187 */ 176 RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t iSum)188 DECLINLINE(uint32_t) rtNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t iSum) 177 189 { 178 190 iSum += pUdpHdr->uh_sport … … 185 197 186 198 /** 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 */ 205 RTDECL(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]. 188 213 * 189 214 * @returns 32-bit intermediary checksum value. … … 193 218 * @param iSum The 32-bit intermediate checksum value. 194 219 */ 195 RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t iSum)220 DECLINLINE(uint32_t) rtNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t iSum) 196 221 { 197 222 uint16_t const *paw = (uint16_t const *)pTcpHdr; … … 231 256 232 257 /** 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 */ 266 RTDECL(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]. 234 274 * 235 275 * @returns 32-bit intermediary checksum value. … … 240 280 * or UDP header (data never start at an odd offset). 241 281 */ 242 RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t iSum, bool *pfOdd)282 DECLINLINE(uint32_t) rtNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t iSum, bool *pfOdd) 243 283 { 244 284 if (*pfOdd) … … 263 303 { 264 304 iSum += *pw; 265 pw += 2;305 pw++; 266 306 cbData -= 2; 267 307 } … … 282 322 } 283 323 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 */ 334 RTDECL(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]. 287 342 * 288 343 * @returns The checksum. 289 344 * @param iSum The 32-bit intermediate checksum value. 290 345 */ 291 RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t iSum)346 DECLINLINE(uint16_t) rtNetIPv4FinalizeChecksum(uint32_t iSum) 292 347 { 293 348 /* 16-bit one complement fun */ … … 298 353 299 354 300 355 /** 356 * Finalizes a IPv4 checksum. 357 * 358 * @returns The checksum. 359 * @param iSum The 32-bit intermediate checksum value. 360 */ 361 RTDECL(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 */ 379 RTDECL(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.