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
30 #include <logfunc.hpp>
32 #include "boost_assert_handler.h"
33 #include "ip/ipv4header.h"
34 #include "ip/ipv6header.h"
36 using I2n::Logger::GlobalLogger;
37 using boost::asio::ip::address;
44 IcmpData::IcmpData(const std::size_t size_arg)
46 , raw_data( size_arg )
49 bool IcmpData::match_echo_reply(const uint16_t identifier,
50 const uint16_t sequence_number) const
53 bool IcmpData::match_destination_unreachable(const uint16_t identifier,
54 const uint16_t sequence_number,
55 const address &destination_address) const
58 bool IcmpData::match_time_exceeded(const uint16_t identifier,
59 const uint16_t sequence_number,
60 const address &destination_address) const
63 std::size_t IcmpData::get_size() const
66 int IcmpData::get_ip_version() const
68 int offset = 4; // the 4 uninteresting bytes we need to skip
69 uint8_t version_with_ihl = static_cast<uint8_t>(IcmpData::raw_data[offset]);
70 int version = static_cast<int>( (version_with_ihl & 0xF0) >> 4 );
74 IpHeaderPtr IcmpData::get_ip_header() const
76 // make stream from relevant part of payload
77 std::stringbuf buffer;
78 std::ostream buf_filler( &buffer );
79 raw_data.write( buf_filler );
80 std::istream stream( &buffer );
83 stream.seekg(4, stream.beg);
85 // interpret as IP header
86 int ip_version = get_ip_version();
90 Ipv4Header *result_val = new Ipv4Header();
91 stream >> *result_val;
92 result.reset( result_val );
94 else if (ip_version == 6)
96 Ipv6Header *result_val = new Ipv6Header();
97 stream >> *result_val;
98 result.reset( result_val );
101 { // throw runtime error through boost_assert_handler
102 GlobalLogger.error() << "Invalid IP version: " << ip_version
103 << " ! Maybe data is no IP packet?";
104 BOOST_ASSERT( !"Invalid IP version or not IP packet!" );
110 uint32_t IcmpData::calc_checksum_part() const
111 { return raw_data.calc_checksum_part(); }
114 void IcmpData::print( const size_t &bytes_received,
115 const boost::posix_time::ptime &time_packet_sent,
116 const std::string &remote_address,
117 const uint32_t ttl) const
120 std::istream& IcmpData::read(std::istream &is) // read raw data
122 return raw_data.read(is);
125 std::ostream& IcmpData::write(std::ostream &os) const
127 return raw_data.write(os);
130 std::string IcmpData::to_string() const
132 std::stringstream buf;
133 buf << "[IcmpData of length " << size << "]";
137 std::istream& operator>>(
141 { return data.read( is ); }
143 std::ostream& operator<<(
147 { return data.write( os ); }