2f8a702a433f983650a409dd41106e6805b6437a
[pingcheck] / src / icmp / icmppacket.h
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_PACKET_H
26 #define ICMP_PACKET_H
27
28 #include <stdint.h>
29 #include <iostream>
30
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>
35
36 #include "ip/ipheader.h"
37 #include "icmp/icmpheader.h"
38 #include "icmp/icmpdata.h"
39 #include "icmp/icmptype.h"
40
41 using boost::asio::ip::icmp;
42 using boost::asio::ip::address;
43
44 class IcmpPacket
45 {
46 protected:
47     IpHeaderPtr ip_head_ptr;
48     IcmpHeader icmp_header;
49     IcmpDataPtr icmp_data_ptr;
50
51 public:
52
53     enum ReadReturnCode {
54         ReadReturnCode_OK = 0,
55         ReadReturnCode_UNSPECIFIED,
56         ReadReturnCode_FAIL,
57         ReadReturnCode_NOT_ENOUGH_DATA,
58         ReadReturnCode_BAD_STREAM,
59         ReadReturnCode_INVALID_SIZE,
60         ReadReturnCode_UNKNOWN_PROTOCOL,
61         ReadReturnCode_UNKNOWN_ICMP_TYPE
62     };
63
64     static std::string return_code_to_string( const ReadReturnCode &code );
65
66     IcmpPacket(const icmp::socket::protocol_type &protocol);
67
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);
72
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
79
80     bool check_integrity();   // not implemented yet
81
82     bool match_destination_unreachable(
83                           const uint16_t identifier,
84                           const uint16_t sequence_number,
85                           const address &destination_address) const;
86
87     bool match_echo_reply(const uint16_t identifier,
88                           const uint16_t sequence_number,
89                           const address &destination_address) const;
90
91     bool match_time_exceeded(const uint16_t identifier,
92                              const uint16_t sequence_number,
93                              const address &destination_address) const;
94     /**
95      * @brief print echo reply / destination unreachable message depending on
96      *    ICMP data type
97      *
98      * @param bytes_transferred Number of bytes transferred.
99      * @param time_packet_sent The time when this packet was sent.
100      *
101      * @return void
102      */
103     void print( const size_t &bytes_transferred,
104                 const boost::posix_time::ptime &time_packet_sent ) const;
105
106     ReadReturnCode read(std::istream &is);
107
108     bool write(std::ostream &os) const;
109
110     std::string to_string() const;
111
112     friend std::istream& operator>>(
113             std::istream &is,
114             IcmpPacket &packet
115     );
116     friend std::ostream& operator<<(
117             std::ostream &os,
118             const IcmpPacket &packet
119     );
120
121
122 };
123
124 typedef boost::shared_ptr<IcmpPacket> IcmpPacketItem;
125
126 #endif
127