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 | /** IP checksum value for computed zero checksum.
|
---|
45 | * Zero is returned as 0xFFFF (not flipped)
|
---|
46 | */
|
---|
47 | #define IP_CHECKSUM_ZERO 0xFFFFu
|
---|
48 |
|
---|
49 | /** Computes CRC32 value.
|
---|
50 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
51 | * @param[in] data Pointer to the beginning of data to process.
|
---|
52 | * @param[in] length Length of the data in bits.
|
---|
53 | * @returns The computed CRC32 of the length bits of the data.
|
---|
54 | */
|
---|
55 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
56 | #define compute_crc32(seed, data, length) compute_crc32_be(seed, (uint8_t *) data, length)
|
---|
57 | #else
|
---|
58 | #define compute_crc32(seed, data, length) compute_crc32_le(seed, (uint8_t *) data, length)
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | /** Computes CRC32 value in the little-endian environment.
|
---|
62 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
63 | * @param[in] data Pointer to the beginning of data to process.
|
---|
64 | * @param[in] length Length of the data in bits.
|
---|
65 | * @returns The computed CRC32 of the length bits of the data.
|
---|
66 | */
|
---|
67 | extern uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length);
|
---|
68 |
|
---|
69 | /** Computes CRC32 value in the big-endian environment.
|
---|
70 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
71 | * @param[in] data Pointer to the beginning of data to process.
|
---|
72 | * @param[in] length Length of the data in bits.
|
---|
73 | * @returns The computed CRC32 of the length bits of the data.
|
---|
74 | */
|
---|
75 | extern uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length);
|
---|
76 |
|
---|
77 | /** Computes sum of the 2 byte fields.
|
---|
78 | * Padds one zero (0) byte if odd.
|
---|
79 | * @param[in] seed Initial value. Often used as 0 or ~0.
|
---|
80 | * @param[in] data Pointer to the beginning of data to process.
|
---|
81 | * @param[in] length Length of the data in bytes.
|
---|
82 | * @returns The computed checksum of the length bytes of the data.
|
---|
83 | */
|
---|
84 | extern uint32_t compute_checksum(uint32_t seed, uint8_t * data, size_t length);
|
---|
85 |
|
---|
86 | /** Compacts the computed checksum to the 16 bit number adding the carries.
|
---|
87 | * @param[in] sum Computed checksum.
|
---|
88 | * @returns Compacted computed checksum to the 16 bits.
|
---|
89 | */
|
---|
90 | extern uint16_t compact_checksum(uint32_t sum);
|
---|
91 |
|
---|
92 | /** Returns or flips the checksum if zero.
|
---|
93 | * @param[in] checksum The computed checksum.
|
---|
94 | * @returns The internet protocol header checksum.
|
---|
95 | * @returns 0xFFFF if the computed checksum is zero.
|
---|
96 | */
|
---|
97 | extern uint16_t flip_checksum(uint16_t checksum);
|
---|
98 |
|
---|
99 | /** Computes the ip header checksum.
|
---|
100 | * To compute the checksum of a new packet, the checksum header field must be zero.
|
---|
101 | * To check the checksum of a received packet, the checksum may be left set.
|
---|
102 | * The zero (0) value will be returned in this case if valid.
|
---|
103 | * @param[in] data The header data.
|
---|
104 | * @param[in] length The header length in bytes.
|
---|
105 | * @returns The internet protocol header checksum.
|
---|
106 | * @returns 0xFFFF if the computed checksum is zero.
|
---|
107 | */
|
---|
108 | extern uint16_t ip_checksum(uint8_t * data, size_t length);
|
---|
109 |
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | /** @}
|
---|
113 | */
|
---|