118e369fb75b3aff6da71e02f9e57c9e36247bf3
[pingcheck] / src / icmp / icmpdata_pingfailreply.cpp
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 #include "icmp/icmpdata_pingfailreply.h"
26 #include "boost_assert_handler.h"
27
28 #include <logfunc.hpp>
29 using I2n::Logger::GlobalLogger;
30
31
32 IcmpData_PingFailReply::IcmpData_PingFailReply(const std::size_t size_arg)
33     : IcmpData( size_arg )
34 {}
35
36 bool IcmpData_PingFailReply::match_echo_reply(
37                                            const uint16_t identifier,
38                                            const uint16_t sequence_number) const
39 { return false; }
40
41 bool IcmpData_PingFailReply::match_ping_request(
42                                            const uint16_t identifier,
43                                            const uint16_t sequence_number) const
44 {
45     return      identifier == get_icmp_identifier() &&
46            sequence_number == get_icmp_sequence_number();
47 }
48
49
50 uint16_t IcmpData_PingFailReply::get_icmp_identifier() const
51 { return get_icmp_request_data(4); }
52
53 uint16_t IcmpData_PingFailReply::get_icmp_sequence_number() const
54 { return get_icmp_request_data(6); }
55
56 /**
57  * @brief access a given byte within request ICMP data
58  *
59  * first tests whether data actually IS a reply for an ICMP request and what
60  *   IP version this is, then access the given byte and byte+1 within request
61  *   ICMP header/data
62  *
63  * @returns 2-byte value based on byte and byte+1 in icmp data
64  */
65 uint16_t IcmpData_PingFailReply::get_icmp_request_data(
66                                                 const int icmp_start_byte) const
67 {
68     // payload should be the original query, which is an IP packet.
69     // first check wheter that IP packet contains an ICMP message at all
70     int offset = get_icmp_data_offset();
71
72     if (offset == -1)
73         return static_cast<uint16_t>(-1);
74     else
75         // if it is an icmp message, then the icmp packet comes right after
76         // the IP header. Inside the icmp packet we need the given byte offset
77         return IcmpData::raw_data.decode16(offset+icmp_start_byte,
78                                            offset+icmp_start_byte+1);
79 }
80
81 /**
82  * copying code from ip header parsing ...
83  * If we need more functions of this sort, could create a data stream from
84  *   raw_data, read a IpHeader from it and get info from that
85  *
86  * Unfortunately, the value in packet is not the original one, but the one
87  *   change en route to destination, so in case of TimeExceeded, it is always 1
88  */
89 uint8_t IcmpData_PingFailReply::get_ip_ttl() const
90 {
91     int offset = 4;   // the 4 uninteresting bytes we need to skip
92     int version = IcmpData::get_ip_version();
93
94     if (version == 4)   // IPv4
95         offset += 8;    // byte 8 within IP header
96     else if (version == 6)   // IPv6
97         offset += 7;
98     else
99     {
100         GlobalLogger.error() << "Request IP header is neither IPv4 nor v6!"
101                              << std::endl;
102         BOOST_ASSERT( !"Source IP header is neither IPv4 nor v6!" );
103     }
104
105     return IcmpData::raw_data[offset];
106 }
107
108 /** @brief get byte index of start of ICMP request data after IP header
109  *
110  * @returns -1 if this data is not a
111  * @throws boost assertion if data does not start with an IP header of version
112  *   4 or 6
113  */
114 int IcmpData_PingFailReply::get_icmp_data_offset() const
115 {
116     bool is_icmp = false;
117     int offset = 4;   // the 4 uninteresting bytes we need to skip
118     uint8_t version_with_ihl = static_cast<uint8_t>(IcmpData::raw_data[offset]);
119     uint8_t version = (version_with_ihl & 0xF0) >> 4;
120
121     if (version == 4)   // IPv4
122     {
123         is_icmp = IcmpData::raw_data[offset+9] == 1;
124         offset += 20;     // 20 byte for IPv4 header
125     }
126     else if (version == 6)   // IPv6
127     {
128         is_icmp = IcmpData::raw_data[offset+6] == 58;
129         offset += 40;     // 40 byte for IPv6 header
130     }
131     else
132     {
133         GlobalLogger.error() << "Request IP header is neither IPv4 nor v6!"
134                              << std::endl;
135         BOOST_ASSERT( !"Source IP header is neither IPv4 nor v6!" );
136     }
137
138     if (is_icmp)
139         return offset;
140     else
141         return -1;
142 }
143
144
145 // (created using vim -- the world's best text editor)
146