d5b73dafa7d42acd6bc605c42ae9f9decd704ded
[senf.git] / senf / Utils / IpChecksum.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief IpChecksum public header */
25
26 #ifndef HH_SENF_Utils_IpChecksum_
27 #define HH_SENF_Utils_IpChecksum_ 1
28
29 // Custom includes
30 #include <boost/cstdint.hpp>
31
32 //#include "IpChecksum.mpp"
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34
35 namespace senf {
36
37     /** \brief Calculate TCP/IP 16bit checksum value
38
39         This class allows calculation of the 16bit checksum value as defined in the TCP/IP v4
40         RFC's. This checksum is \e not a CRC.
41
42         To calculate the checksum, you create an instance of IpChecksum. You then repeatedly call
43         the feed() members to pass data in. After all data has been processed, the sum() member will
44         return the calculated checksum.
45
46         The mathematical properties of the checksum allow to skip any \e even number of zero bytes
47         without changing the checksum value.
48       */
49     class IpChecksum
50     {
51     public:
52         //-////////////////////////////////////////////////////////////////////////
53         // Types
54
55         //-////////////////////////////////////////////////////////////////////////
56         ///\name Structors and default members
57         //\{
58
59         IpChecksum();
60
61         //\}
62         //-////////////////////////////////////////////////////////////////////////
63
64         void feed(boost::uint8_t byte); ///< Feed single byte
65
66         template <class InputIterator>
67         void feed(InputIterator b, InputIterator e);
68                                         ///< Feed range of bytes
69
70         boost::uint16_t sum() const;    ///< Calculate checksum
71
72     protected:
73
74     private:
75         boost::uint32_t sum_;
76         bool odd_;
77     };
78
79
80 }
81
82 //-/////////////////////////////////////////////////////////////////////////////////////////////////
83 #include "IpChecksum.cci"
84 //#include "IpChecksum.ct"
85 #include "IpChecksum.cti"
86 #endif
87
88 \f
89 // Local Variables:
90 // mode: c++
91 // fill-column: 100
92 // comment-column: 40
93 // c-file-style: "senf"
94 // indent-tabs-mode: nil
95 // ispell-local-dictionary: "american"
96 // compile-command: "scons -u test"
97 // End: