Subversion Repositories HelenOS

Rev

Rev 4722 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup net
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  General CRC and checksum computation.
  35.  */
  36.  
  37. #ifndef __NET_CHECKSUM_H__
  38. #define __NET_CHECKSUM_H__
  39.  
  40. #include <byteorder.h>
  41.  
  42. #include <sys/types.h>
  43.  
  44. /** Computes CRC32 value.
  45.  *  @param seed Initial value. Often used as 0 or ~0. Input parameter.
  46.  *  @param data Pointer to the beginning of data to process. Input parameter.
  47.  *  @param length Length of the data in bits. Input parameter.
  48.  *  @returns The computed CRC32 of the length bits of the data.
  49.  */
  50. #ifdef ARCH_IS_BIG_ENDIAN
  51.     #define compute_crc32( seed, data, length ) compute_crc32_be( seed, ( uint8_t * ) data, length )
  52. #else
  53.     #define compute_crc32( seed, data, length ) compute_crc32_le( seed, ( uint8_t * ) data, length )
  54. #endif
  55.  
  56. /** Computes CRC32 value in the little-endian environment.
  57.  *  @param seed Initial value. Often used as 0 or ~0. Input parameter.
  58.  *  @param data Pointer to the beginning of data to process. Input parameter.
  59.  *  @param length Length of the data in bits. Input parameter.
  60.  *  @returns The computed CRC32 of the length bits of the data.
  61.  */
  62. uint32_t    compute_crc32_le( uint32_t seed, uint8_t * data, size_t length );
  63.  
  64. /** Computes CRC32 value in the big-endian environment.
  65.  *  @param seed Initial value. Often used as 0 or ~0. Input parameter.
  66.  *  @param data Pointer to the beginning of data to process. Input parameter.
  67.  *  @param length Length of the data in bits. Input parameter.
  68.  *  @returns The computed CRC32 of the length bits of the data.
  69.  */
  70. uint32_t    compute_crc32_be( uint32_t seed, uint8_t * data, size_t length );
  71.  
  72. /** Computes sum of the 2 byte fields.
  73.  *  Padds one zero (0) byte if odd.
  74.  *  @param seed Initial value. Often used as 0 or ~0. Input parameter.
  75.  *  @param data Pointer to the beginning of data to process. Input parameter.
  76.  *  @param length Length of the data in bytes. Input parameter.
  77.  *  @returns The computed checksum of the length bytes of the data.
  78.  */
  79. uint32_t    compute_checksum( uint32_t seed, uint8_t * data, size_t length );
  80.  
  81. /** Compacts the computed checksum to the 16 bit number adding the carries.
  82.  *  @param sum Computed checksum. Input parameter.
  83.  *  @returns Compacted computed checksum to the 16 bits.
  84.  */
  85. uint16_t    compact_checksum( uint32_t sum );
  86.  
  87. /** Returns or flips the checksum if zero.
  88.  *  @param checksum The computed checksum. Input parameter.
  89.  *  @returns The internet protocol header checksum.
  90.  *  @returns 0xFFFF if the computed checksum is zero.
  91.  */
  92. uint16_t    flip_checksum( uint16_t checksum );
  93.  
  94. /** Computes the ip header checksum.
  95.  *  To compute the checksum of a new packet, the checksum header field must be zero.
  96.  *  To check the checksum of a received packet, the checksum may be left set.
  97.  *  The zero (0) value will be returned in this case if valid.
  98.  *  @param data The header data. Input parameter.
  99.  *  @param length The header length in bytes. Input parameter.
  100.  *  @returns The internet protocol header checksum.
  101.  *  @returns 0xFFFF if the computed checksum is zero.
  102.  */
  103. uint16_t ip_checksum( uint8_t * data, size_t length );
  104.  
  105. #endif
  106.  
  107. /** @}
  108.  */
  109.