Rev 4743 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4075 | mejdrech | 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 |
||
4505 | mejdrech | 34 | * General CRC and checksum computation. |
4075 | mejdrech | 35 | */ |
36 | |||
4743 | mejdrech | 37 | #ifndef __NET_CHECKSUM_H__ |
38 | #define __NET_CHECKSUM_H__ |
||
4075 | mejdrech | 39 | |
40 | #include <byteorder.h> |
||
41 | |||
42 | #include <sys/types.h> |
||
43 | |||
44 | /** Computes CRC32 value. |
||
4756 | mejdrech | 45 | * @param[in] seed Initial value. Often used as 0 or ~0. |
46 | * @param[in] data Pointer to the beginning of data to process. |
||
47 | * @param[in] length Length of the data in bits. |
||
4075 | mejdrech | 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. |
||
4756 | mejdrech | 57 | * @param[in] seed Initial value. Often used as 0 or ~0. |
58 | * @param[in] data Pointer to the beginning of data to process. |
||
59 | * @param[in] length Length of the data in bits. |
||
4075 | mejdrech | 60 | * @returns The computed CRC32 of the length bits of the data. |
61 | */ |
||
4558 | mejdrech | 62 | uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ); |
4075 | mejdrech | 63 | |
64 | /** Computes CRC32 value in the big-endian environment. |
||
4756 | mejdrech | 65 | * @param[in] seed Initial value. Often used as 0 or ~0. |
66 | * @param[in] data Pointer to the beginning of data to process. |
||
67 | * @param[in] length Length of the data in bits. |
||
4075 | mejdrech | 68 | * @returns The computed CRC32 of the length bits of the data. |
69 | */ |
||
4558 | mejdrech | 70 | uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ); |
4075 | mejdrech | 71 | |
4505 | mejdrech | 72 | /** Computes sum of the 2 byte fields. |
73 | * Padds one zero (0) byte if odd. |
||
4756 | mejdrech | 74 | * @param[in] seed Initial value. Often used as 0 or ~0. |
75 | * @param[in] data Pointer to the beginning of data to process. |
||
76 | * @param[in] length Length of the data in bytes. |
||
4505 | mejdrech | 77 | * @returns The computed checksum of the length bytes of the data. |
78 | */ |
||
4558 | mejdrech | 79 | uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ); |
4505 | mejdrech | 80 | |
81 | /** Compacts the computed checksum to the 16 bit number adding the carries. |
||
4756 | mejdrech | 82 | * @param[in] sum Computed checksum. |
4505 | mejdrech | 83 | * @returns Compacted computed checksum to the 16 bits. |
84 | */ |
||
85 | uint16_t compact_checksum( uint32_t sum ); |
||
86 | |||
4722 | mejdrech | 87 | /** Returns or flips the checksum if zero. |
4756 | mejdrech | 88 | * @param[in] checksum The computed checksum. |
4722 | mejdrech | 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 | |||
4711 | mejdrech | 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. |
||
4722 | mejdrech | 97 | * The zero (0) value will be returned in this case if valid. |
4756 | mejdrech | 98 | * @param[in] data The header data. |
99 | * @param[in] length The header length in bytes. |
||
4711 | mejdrech | 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 | |||
4075 | mejdrech | 105 | #endif |
106 | |||
107 | /** @} |
||
108 | */ |