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
31 #include <boost/shared_ptr.hpp>
32 #include <boost/asio/ip/address.hpp>
33 #include <boost/asio/ip/icmp.hpp>
34 #include <boost/date_time/posix_time/ptime.hpp>
36 #include "ip/ipheader.h"
37 #include "icmp/icmpheader.h"
38 #include "icmp/icmpdata.h"
39 #include "icmp/icmptype.h"
41 using boost::asio::ip::icmp;
42 using boost::asio::ip::address;
47 IpHeaderPtr ip_head_ptr;
48 IcmpHeader icmp_header;
49 IcmpDataPtr icmp_data_ptr;
54 ReadReturnCode_OK = 0,
55 ReadReturnCode_UNSPECIFIED,
57 ReadReturnCode_NOT_ENOUGH_DATA,
58 ReadReturnCode_BAD_STREAM,
59 ReadReturnCode_INVALID_SIZE,
60 ReadReturnCode_UNKNOWN_PROTOCOL,
61 ReadReturnCode_UNKNOWN_ICMP_TYPE
64 static std::string return_code_to_string( const ReadReturnCode &code );
66 IcmpPacket(const icmp::socket::protocol_type &protocol);
68 // IP header is created with only 0s, is not used in write, anyway
69 IcmpPacket(const icmp::socket::protocol_type &protocol,
70 const IcmpHeader &icmp_header_arg,
71 const IcmpDataPtr &icmp_data_arg);
73 Icmpv4Type get_type_v4() const;
74 Icmpv6Type get_type_v6() const;
75 uint8_t get_icmp_code() const;
76 address get_source_address() const;
77 address get_destination_address() const;
78 std::size_t get_icmp_size() const; // ICMP header + data
80 bool check_integrity(); // not implemented yet
82 bool match_destination_unreachable(
83 const uint16_t identifier,
84 const uint16_t sequence_number,
85 const address &destination_address) const;
87 bool match_echo_reply(const uint16_t identifier,
88 const uint16_t sequence_number,
89 const address &destination_address) const;
91 bool match_time_exceeded(const uint16_t identifier,
92 const uint16_t sequence_number,
93 const address &destination_address) const;
95 * @brief print echo reply / destination unreachable message depending on
98 * @param bytes_transferred Number of bytes transferred.
99 * @param time_packet_sent The time when this packet was sent.
103 void print( const size_t &bytes_transferred,
104 const boost::posix_time::ptime &time_packet_sent ) const;
106 ReadReturnCode read(std::istream &is);
108 bool write(std::ostream &os) const;
110 std::string to_string() const;
112 friend std::istream& operator>>(
116 friend std::ostream& operator<<(
118 const IcmpPacket &packet
124 typedef boost::shared_ptr<IcmpPacket> IcmpPacketItem;
128 // (created using vim -- the world's best text editor)