540e2e2a3feaffaba4c61b8ce3bd625afe2249b2
[pingcheck] / src / icmp / icmpheader.h
1 /*
2  The software in this package is distributed under the GNU General
3  Public License version 2 (with a special exception described below).
4
5  A copy of GNU General Public License (GPL) is included in this distribution,
6  in the file COPYING.GPL.
7
8  As a special exception, if other files instantiate templates or use macros
9  or inline functions from this file, or you compile this file and link it
10  with other works to produce a work based on this file, this file
11  does not by itself cause the resulting work to be covered
12  by the GNU General Public License.
13
14  However the source code for this file must still be made available
15  in accordance with section (3) of the GNU General Public License.
16
17  This exception does not invalidate any other reasons why a work based
18  on this file might be covered by the GNU General Public License.
19
20  Christian Herdtweck, Intra2net AG 2015
21  Based on an example in Boost Documentation (by Christopher M. Kohlhoff)
22  and adaptation by Guilherme M. Ferreira
23  */
24
25 #ifndef ICMP_HEADER_H
26 #define ICMP_HEADER_H
27
28 #include <stdint.h>
29 #include <iostream>
30
31 /** @brief first 4 byte of ICMP header (v4 and v6)
32  *
33  * In ICMP v4, the first 4 bytes are standardized, the other 4 depend on
34  *   message type. In Icmp v6, the header IS only the first 4 bytes.
35  * So everything after those first 4 bytes is contained in IcmpData subclasses
36  */
37 class IcmpHeader
38 {
39 public:
40     IcmpHeader();
41
42     IcmpHeader(const uint8_t type_arg, const uint8_t code_arg);
43
44     std::istream& read(std::istream &is);
45     std::ostream& write(std::ostream &os) const;
46
47     uint8_t get_type() const;
48     uint8_t get_code() const;
49     uint16_t get_checksum() const;
50
51     void calc_checksum( const uint32_t body_checksum_part );
52
53     // returns the amount of data represented by this class;
54     // for Icmp v4, the header is actually 8 bytes, see comments above
55     uint16_t get_header_length() const;
56
57     std::string to_string() const;
58
59     friend std::istream& operator>>(
60             std::istream &is,
61             IcmpHeader &header
62     );
63     friend std::ostream& operator<<(
64             std::ostream &os,
65             const IcmpHeader &header
66     );
67
68 protected:
69     uint8_t type;
70     uint8_t code;
71     uint16_t checksum;
72 };
73
74 #endif
75
76 // (created using vim -- the world's best text editor)
77