Commit | Line | Data |
---|---|---|
c120ad42 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 | |
c120ad42 CH |
23 | */ |
24 | ||
8ddcec43 GMF |
25 | #ifndef ICMP_PACKET_H |
26 | #define ICMP_PACKET_H | |
27 | ||
d21f681c | 28 | #include <stdint.h> |
34a2dd33 | 29 | #include <iostream> |
8ddcec43 | 30 | |
34a2dd33 | 31 | #include <boost/shared_ptr.hpp> |
c120ad42 CH |
32 | #include <boost/asio/ip/address.hpp> |
33 | #include <boost/asio/ip/icmp.hpp> | |
34 | #include <boost/date_time/posix_time/ptime.hpp> | |
8ddcec43 | 35 | |
c120ad42 | 36 | #include "ip/ipheader.h" |
c120ad42 CH |
37 | #include "icmp/icmpheader.h" |
38 | #include "icmp/icmpdata.h" | |
39 | #include "icmp/icmptype.h" | |
8ddcec43 | 40 | |
c120ad42 CH |
41 | using boost::asio::ip::icmp; |
42 | using boost::asio::ip::address; | |
43 | ||
44 | class IcmpPacket | |
8ddcec43 | 45 | { |
c120ad42 CH |
46 | protected: |
47 | IpHeaderPtr ip_head_ptr; | |
48 | IcmpHeader icmp_header; | |
49 | IcmpDataPtr icmp_data_ptr; | |
50 | ||
8ddcec43 | 51 | public: |
8ddcec43 | 52 | |
059138a5 CH |
53 | enum ReadReturnCode { |
54 | ReadReturnCode_OK = 0, | |
d9bbc1d7 | 55 | ReadReturnCode_UNSPECIFIED, |
059138a5 CH |
56 | ReadReturnCode_FAIL, |
57 | ReadReturnCode_NOT_ENOUGH_DATA, | |
58 | ReadReturnCode_BAD_STREAM, | |
d9bbc1d7 | 59 | ReadReturnCode_INVALID_SIZE, |
c120ad42 CH |
60 | ReadReturnCode_UNKNOWN_PROTOCOL, |
61 | ReadReturnCode_UNKNOWN_ICMP_TYPE | |
059138a5 CH |
62 | }; |
63 | ||
d9bbc1d7 CH |
64 | static std::string return_code_to_string( const ReadReturnCode &code ); |
65 | ||
0ba8adc0 | 66 | IcmpPacket(const icmp::socket::protocol_type &protocol); |
34a2dd33 | 67 | |
c120ad42 CH |
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, | |
0ba8adc0 CH |
71 | const IcmpDataPtr &icmp_data_arg); |
72 | ||
aadc7032 CH |
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 | |
0ba8adc0 CH |
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; | |
8ddcec43 | 86 | |
c120ad42 CH |
87 | bool match_echo_reply(const uint16_t identifier, |
88 | const uint16_t sequence_number, | |
0ba8adc0 | 89 | const address &destination_address) const; |
c120ad42 | 90 | |
15023b99 CH |
91 | bool match_time_exceeded(const uint16_t identifier, |
92 | const uint16_t sequence_number, | |
93 | const address &destination_address) const; | |
c120ad42 CH |
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, | |
15023b99 | 104 | const boost::posix_time::ptime &time_packet_sent ) const; |
c120ad42 CH |
105 | |
106 | ReadReturnCode read(std::istream &is); | |
107 | ||
108 | bool write(std::ostream &os) const; | |
747c13ca CH |
109 | |
110 | std::string to_string() const; | |
39d326f2 CH |
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 | ||
c120ad42 | 122 | }; |
34a2dd33 GMF |
123 | |
124 | typedef boost::shared_ptr<IcmpPacket> IcmpPacketItem; | |
125 | ||
c120ad42 CH |
126 | #endif |
127 |