extended ICMP packet dumping to parts after packet creation
[pingcheck] / src / icmp / icmpechodata.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/icmpechodata.h"
26
27 #include "boost/date_time/posix_time/posix_time_types.hpp"
28 #include "boost/date_time/time_resolution_traits.hpp"
29 #include <logfunc.hpp>
30
31 using boost::date_time::time_resolution_traits_adapted64_impl;
32 using I2n::Logger::GlobalLogger;
33
34
35 IcmpEchoData::IcmpEchoData(const uint16_t identifier,
36                            const uint16_t sequence_number,
37                            const std::string &optional_data)
38     : IcmpData( 4 + optional_data.length() )
39 {
40     IcmpData::raw_data.encode16(0, 1, identifier);
41     IcmpData::raw_data.encode16(2, 3, sequence_number);
42     IcmpData::raw_data.encode_string(4, optional_data);
43     GlobalLogger.debug() << "Done creating echo request" << std::endl;
44 }
45
46 IcmpEchoData::IcmpEchoData(const std::size_t size_arg)
47     : IcmpData( size_arg )
48 {}
49
50
51 bool IcmpEchoData::match_destination_unreachable(
52                                     const uint16_t identifier,
53                                     const uint16_t sequence_number,
54                                     const address &destination_address) const
55 { return false; }
56
57
58 bool IcmpEchoData::match_time_exceeded(
59                                     const uint16_t identifier,
60                                     const uint16_t sequence_number,
61                                     const address &destination_address) const
62 { return false; }
63
64
65 bool IcmpEchoData::match_echo_reply(const uint16_t identifier,
66                                     const uint16_t sequence_number) const
67 {
68     return get_identifier() == identifier
69         && get_sequence_number() == sequence_number;
70 }
71
72 uint16_t IcmpEchoData::get_identifier() const
73 { return IcmpData::raw_data.decode16(0, 1);  }
74
75 uint16_t IcmpEchoData::get_sequence_number() const
76 { return IcmpData::raw_data.decode16(2, 3);  }
77
78 /**
79  * @brief Prints the ICMP echo reply messages.
80  *
81  * @return void
82  */
83 void IcmpEchoData::print( const size_t &bytes_received,
84             const boost::posix_time::ptime &time_packet_sent,
85             const std::string &remote_address,
86             const uint32_t ttl) const
87 {
88     boost::posix_time::ptime now
89                       = boost::posix_time::microsec_clock::universal_time();
90     time_resolution_traits_adapted64_impl::int_type elapsed_time =
91             (now - time_packet_sent).total_milliseconds();
92
93     GlobalLogger.info() << bytes_received << " bytes "
94          << "from " << remote_address
95          << ": icmp_seq=" << get_sequence_number()
96          << " ttl=" << ttl
97          << " time=" << elapsed_time << " ms" << std::endl;
98 }
99
100 std::string IcmpEchoData::to_string() const
101 {
102     std::stringstream buf;
103     buf << "[EchoData ID=" << std::showbase << std::hex << get_identifier()
104         << ",seq.nr=" << std::noshowbase << std::dec<< get_sequence_number()
105         << ",data of size " << IcmpData::size-4;
106     //for (int idx=4; idx < IcmpData::size; ++idx)
107     //    buf << IcmpData::raw_data[idx];
108     buf << "]";
109     return buf.str();
110 }
111