Commit | Line | Data |
---|---|---|
0ba8adc0 CH |
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 | |
057e6cc9 CH |
21 | Based on an example in Boost Documentation (by Christopher M. Kohlhoff) |
22 | and adaptation by Guilherme M. Ferreira | |
0ba8adc0 CH |
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, | |
81c26517 CH |
53 | const uint16_t sequence_number, |
54 | const address &destination_address) const | |
0ba8adc0 CH |
55 | { return false; } |
56 | ||
57 | ||
15023b99 CH |
58 | bool IcmpEchoData::match_time_exceeded( |
59 | const uint16_t identifier, | |
60 | const uint16_t sequence_number) const | |
61 | { return false; } | |
62 | ||
63 | ||
0ba8adc0 CH |
64 | bool IcmpEchoData::match_echo_reply(const uint16_t identifier, |
65 | const uint16_t sequence_number) const | |
66 | { | |
67 | return get_identifier() == identifier | |
68 | && get_sequence_number() == sequence_number; | |
69 | } | |
70 | ||
71 | uint16_t IcmpEchoData::get_identifier() const | |
72 | { return IcmpData::raw_data.decode16(0, 1); } | |
73 | ||
74 | uint16_t IcmpEchoData::get_sequence_number() const | |
75 | { return IcmpData::raw_data.decode16(2, 3); } | |
76 | ||
77 | /** | |
78 | * @brief Prints the ICMP echo reply messages. | |
79 | * | |
80 | * @return void | |
81 | */ | |
82 | void IcmpEchoData::print( const size_t &bytes_received, | |
83 | const boost::posix_time::ptime &time_packet_sent, | |
84 | const std::string &remote_address, | |
85 | const uint32_t ttl) const | |
86 | { | |
87 | boost::posix_time::ptime now | |
88 | = boost::posix_time::microsec_clock::universal_time(); | |
89 | time_resolution_traits_adapted64_impl::int_type elapsed_time = | |
90 | (now - time_packet_sent).total_milliseconds(); | |
91 | ||
92 | GlobalLogger.info() << bytes_received << " bytes " | |
93 | << "from " << remote_address | |
94 | << ": icmp_seq=" << get_sequence_number() | |
95 | << " ttl=" << ttl | |
96 | << " time=" << elapsed_time << " ms" << std::endl; | |
97 | } | |
98 | ||
99 | std::string IcmpEchoData::to_string() const | |
100 | { | |
101 | std::stringstream buf; | |
102 | buf << "[EchoData ID=" << std::showbase << std::hex << get_identifier() | |
103 | << ",seq.nr=" << std::noshowbase << std::dec<< get_sequence_number() | |
104 | << ",data of size " << IcmpData::size-4; | |
105 | //for (int idx=4; idx < IcmpData::size; ++idx) | |
106 | // buf << IcmpData::raw_data[idx]; | |
107 | buf << "]"; | |
108 | return buf.str(); | |
109 | } | |
110 |