| 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 | #ifndef ICMP_DATA_H |
| 26 | #define ICMP_DATA_H |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <iostream> |
| 30 | #include <boost/shared_ptr.hpp> |
| 31 | #include <boost/date_time/posix_time/ptime.hpp> |
| 32 | #include <boost/asio/ip/address.hpp> |
| 33 | #include "host/messagepayload.h" |
| 34 | #include "ip/ipheader.h" |
| 35 | |
| 36 | using boost::asio::ip::address; |
| 37 | |
| 38 | /** @brief contents of ICMP packets after the first 4 bytes of the ICMP header |
| 39 | * used for ICMPv4 and ICMPv6 |
| 40 | * |
| 41 | * subclasses only have to specify which part of raw_data to access for which |
| 42 | * information |
| 43 | */ |
| 44 | class IcmpData |
| 45 | { |
| 46 | public: |
| 47 | IcmpData(); |
| 48 | |
| 49 | IcmpData(const std::size_t size_arg); |
| 50 | |
| 51 | virtual ~IcmpData() {}; |
| 52 | |
| 53 | virtual bool match_echo_reply(const uint16_t identifier, |
| 54 | const uint16_t sequence_number) const; |
| 55 | |
| 56 | virtual bool match_destination_unreachable(const uint16_t identifier, |
| 57 | const uint16_t sequence_number, |
| 58 | const address &destination_address) const; |
| 59 | |
| 60 | virtual bool match_time_exceeded(const uint16_t identifier, |
| 61 | const uint16_t sequence_number, |
| 62 | const address &destination_address) const; |
| 63 | |
| 64 | // including 4 bytes from ICMPv4 header |
| 65 | std::size_t get_size() const; |
| 66 | |
| 67 | IpHeaderPtr get_ip_header() const; |
| 68 | |
| 69 | uint32_t calc_checksum_part() const; |
| 70 | int get_ip_version() const; |
| 71 | |
| 72 | virtual void print( const size_t &bytes_received, |
| 73 | const boost::posix_time::ptime &time_packet_sent, |
| 74 | const std::string &remote_address, |
| 75 | const uint32_t ttl) const; |
| 76 | |
| 77 | virtual std::string to_string() const; |
| 78 | |
| 79 | virtual std::istream& read( std::istream &is); |
| 80 | virtual std::ostream& write(std::ostream &os) const; |
| 81 | |
| 82 | friend std::istream& operator>>( |
| 83 | std::istream &is, |
| 84 | IcmpData &data |
| 85 | ); |
| 86 | friend std::ostream& operator<<( |
| 87 | std::ostream &os, |
| 88 | const IcmpData &data |
| 89 | ); |
| 90 | |
| 91 | protected: |
| 92 | |
| 93 | std::size_t size; |
| 94 | MessagePayload raw_data; |
| 95 | }; |
| 96 | |
| 97 | typedef boost::shared_ptr<IcmpData> IcmpDataPtr; |
| 98 | |
| 99 | #endif |
| 100 | |