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 wheter 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 uint8_t IcmpData_PingFailReply::get_ip_ttl() const
91 int offset = 4; // the 4 uninteresting bytes we need to skip
92 int version = IcmpData::get_ip_version();
94 if (version == 4) // IPv4
95 offset += 8; // byte 8 within IP header
96 else if (version == 6) // IPv6
100 GlobalLogger.error() << "Request IP header is neither IPv4 nor v6!"
102 BOOST_ASSERT( !"Source IP header is neither IPv4 nor v6!" );
105 return IcmpData::raw_data[offset];
108 /** @brief get byte index of start of ICMP request data after IP header
110 * @returns -1 if this data is not a
111 * @throws boost assertion if data does not start with an IP header of version
114 int IcmpData_PingFailReply::get_icmp_data_offset() const
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;
121 if (version == 4) // IPv4
123 is_icmp = IcmpData::raw_data[offset+9] == 1;
124 offset += 20; // 20 byte for IPv4 header
126 else if (version == 6) // IPv6
128 is_icmp = IcmpData::raw_data[offset+6] == 58;
129 offset += 40; // 40 byte for IPv6 header
133 GlobalLogger.error() << "Request IP header is neither IPv4 nor v6!"
135 BOOST_ASSERT( !"Source IP header is neither IPv4 nor v6!" );
145 // (created using vim -- the world's best text editor)