/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. Christian Herdtweck, Intra2net AG 2015 Based on an example in Boost Documentation (by Christopher M. Kohlhoff) and adaptation by Guilherme M. Ferreira */ #ifndef ICMP_PACKET_H #define ICMP_PACKET_H #include #include #include #include #include #include #include "ip/ipheader.h" #include "icmp/icmpheader.h" #include "icmp/icmpdata.h" #include "icmp/icmptype.h" using boost::asio::ip::icmp; using boost::asio::ip::address; class IcmpPacket { protected: IpHeaderPtr ip_head_ptr; IcmpHeader icmp_header; IcmpDataPtr icmp_data_ptr; public: enum ReadReturnCode { ReadReturnCode_OK = 0, ReadReturnCode_UNSPECIFIED, ReadReturnCode_FAIL, ReadReturnCode_NOT_ENOUGH_DATA, ReadReturnCode_BAD_STREAM, ReadReturnCode_INVALID_SIZE, ReadReturnCode_UNKNOWN_PROTOCOL, ReadReturnCode_UNKNOWN_ICMP_TYPE }; static std::string return_code_to_string( const ReadReturnCode &code ); IcmpPacket(const icmp::socket::protocol_type &protocol); // IP header is created with only 0s, is not used in write, anyway IcmpPacket(const icmp::socket::protocol_type &protocol, const IcmpHeader &icmp_header_arg, const IcmpDataPtr &icmp_data_arg); Icmpv4Type get_type_v4() const; Icmpv6Type get_type_v6() const; uint8_t get_icmp_code() const; address get_source_address() const; address get_destination_address() const; std::size_t get_icmp_size() const; // ICMP header + data bool check_integrity(); // not implemented yet bool match_destination_unreachable( const uint16_t identifier, const uint16_t sequence_number, const address &destination_address) const; bool match_echo_reply(const uint16_t identifier, const uint16_t sequence_number, const address &destination_address) const; bool match_time_exceeded(const uint16_t identifier, const uint16_t sequence_number, const address &destination_address) const; /** * @brief print echo reply / destination unreachable message depending on * ICMP data type * * @param bytes_transferred Number of bytes transferred. * @param time_packet_sent The time when this packet was sent. * * @return void */ void print( const size_t &bytes_transferred, const boost::posix_time::ptime &time_packet_sent ) const; ReadReturnCode read(std::istream &is); bool write(std::ostream &os) const; bool dump( std::ostream &os) const; std::string to_string() const; friend std::istream& operator>>( std::istream &is, IcmpPacket &packet ); friend std::ostream& operator<<( std::ostream &os, const IcmpPacket &packet ); }; typedef boost::shared_ptr IcmpPacketItem; #endif