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