2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
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
25 #include "icmp/icmpdata_pingfailreply.h"
26 #include "boost_assert_handler.h"
28 #include <logfunc.hpp>
29 using I2n::Logger::GlobalLogger;
32 IcmpData_PingFailReply::IcmpData_PingFailReply(const std::size_t size_arg)
33 : IcmpData( size_arg )
36 bool IcmpData_PingFailReply::match_echo_reply(
37 const uint16_t identifier,
38 const uint16_t sequence_number) const
41 bool IcmpData_PingFailReply::match_ping_request(
42 const uint16_t identifier,
43 const uint16_t sequence_number) const
45 return identifier == get_icmp_identifier() &&
46 sequence_number == get_icmp_sequence_number();
50 uint16_t IcmpData_PingFailReply::get_icmp_identifier() const
51 { return get_icmp_request_data(4); }
53 uint16_t IcmpData_PingFailReply::get_icmp_sequence_number() const
54 { return get_icmp_request_data(6); }
57 * @brief access a given byte within request ICMP data
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
63 * @returns 2-byte value based on byte and byte+1 in icmp data
65 uint16_t IcmpData_PingFailReply::get_icmp_request_data(
66 const int icmp_start_byte) const
68 // payload should be the original query, which is an IP packet.
69 // first check whether that IP packet contains an ICMP message at all
70 int offset = get_icmp_data_offset();
73 return static_cast<uint16_t>(-1);
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);
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
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
89 * @throws boost assertion if data does not seem to start with an IP v4/6 header
91 uint8_t IcmpData_PingFailReply::get_ip_ttl() const
93 int offset = 4; // the 4 uninteresting bytes we need to skip
94 int version = IcmpData::get_ip_version();
96 if (version == 4) // IPv4
97 offset += 8; // byte 8 within IP header
98 else if (version == 6) // IPv6
102 GlobalLogger.error() << "Request IP header is neither IPv4 nor v6!"
104 BOOST_ASSERT( !"Source IP header is neither IPv4 nor v6!" );
107 return IcmpData::raw_data[offset];
110 /** @brief get byte index of start of ICMP request data after IP header
112 * @returns -1 if this data does not contain a ICMP request packet
113 * @throws boost assertion if data does not seem to start with an IP v4/6 header
115 int IcmpData_PingFailReply::get_icmp_data_offset() const
117 bool is_icmp = false;
118 int offset = 4; // the 4 uninteresting bytes we need to skip
119 uint8_t version_with_ihl = static_cast<uint8_t>(IcmpData::raw_data[offset]);
120 uint8_t version = (version_with_ihl & 0xF0) >> 4;
122 if (version == 4) // IPv4
124 is_icmp = IcmpData::raw_data[offset+9] == 1;
125 offset += 20; // 20 byte for IPv4 header
127 else if (version == 6) // IPv6
129 is_icmp = IcmpData::raw_data[offset+6] == 58;
130 offset += 40; // 40 byte for IPv6 header
135 << "Icmp data does not seem to start with IPv4/6 header!"
137 BOOST_ASSERT( !"Icmp data does not seem to start with IPv4/6 header!" );