1 | // |
---|
2 | // ICMPv4PacketImpl.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/ICMPv4PacketImpl.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: ICMP |
---|
8 | // Module: ICMPv4PacketImpl |
---|
9 | // |
---|
10 | // Definition of the ICMPv4PacketImpl class. |
---|
11 | // |
---|
12 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_ICMPv4PacketImpl_INCLUDED |
---|
40 | #define Net_ICMPv4PacketImpl_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Net/Socket.h" |
---|
45 | #include "Poco/Net/ICMPPacketImpl.h" |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | namespace Net { |
---|
50 | |
---|
51 | |
---|
52 | class Net_API ICMPv4PacketImpl : public ICMPPacketImpl |
---|
53 | /// This class implements the ICMPv4 packet. |
---|
54 | /// Parts are based on original ICMP code by |
---|
55 | /// Mike Muuss |
---|
56 | /// U. S. Army Ballistic Research Laboratory |
---|
57 | /// December, 1983 |
---|
58 | { |
---|
59 | public: |
---|
60 | // ICMPv4 header |
---|
61 | struct Header |
---|
62 | { |
---|
63 | Poco::UInt8 type; // ICMP packet type |
---|
64 | Poco::UInt8 code; // Type sub code |
---|
65 | Poco::UInt16 checksum; |
---|
66 | Poco::UInt16 id; |
---|
67 | Poco::UInt16 seq; |
---|
68 | }; |
---|
69 | |
---|
70 | enum MessageType |
---|
71 | { |
---|
72 | ECHO_REPLY, |
---|
73 | ICMP_1, |
---|
74 | ICMP_2, |
---|
75 | DESTINATION_UNREACHABLE, |
---|
76 | SOURCE_QUENCH, |
---|
77 | REDIRECT, |
---|
78 | ICMP_6, |
---|
79 | ICMP_7, |
---|
80 | ECHO_REQUEST, |
---|
81 | ICMP_9, |
---|
82 | ICMP_10, |
---|
83 | TIME_EXCEEDED, |
---|
84 | PARAMETER_PROBLEM, |
---|
85 | TIMESTAMP_REQUEST, |
---|
86 | TIMESTAMP_REPLY, |
---|
87 | INFORMATION_REQUEST, |
---|
88 | INFORMATION_REPLY, |
---|
89 | MESSAGE_TYPE_UNKNOWN, // non-standard default, must remain last but one |
---|
90 | MESSAGE_TYPE_LENGTH // length indicator, must remain last |
---|
91 | }; |
---|
92 | |
---|
93 | enum DestinationUnreachableCode |
---|
94 | { |
---|
95 | NET_UNREACHABLE, |
---|
96 | HOST_UNREACHABLE, |
---|
97 | PROTOCOL_UNREACHABLE, |
---|
98 | PORT_UNREACHABLE, |
---|
99 | FRAGMENTATION_NEEDED_AND_DF_SET, |
---|
100 | SOURCE_ROUTE_FAILED, |
---|
101 | DESTINATION_UNREACHABLE_UNKNOWN, // non-standard default, must remain last but one |
---|
102 | DESTINATION_UNREACHABLE_LENGTH // length indicator, must remain last |
---|
103 | }; |
---|
104 | |
---|
105 | enum RedirectMessageCode |
---|
106 | { |
---|
107 | REDIRECT_NETWORK, |
---|
108 | REDIRECT_HOST, |
---|
109 | REDIRECT_SERVICE_NETWORK, |
---|
110 | REDIRECT_SERVICE_HOST, |
---|
111 | REDIRECT_MESSAGE_UNKNOWN, // non-standard default, must remain last but one |
---|
112 | REDIRECT_MESSAGE_LENGTH // length indicator, must remain last |
---|
113 | }; |
---|
114 | |
---|
115 | enum TimeExceededCode |
---|
116 | { |
---|
117 | TIME_TO_LIVE, |
---|
118 | FRAGMENT_REASSEMBLY, |
---|
119 | TIME_EXCEEDED_UNKNOWN, // non-standard default, must remain last but one |
---|
120 | TIME_EXCEEDED_LENGTH // length indicator, must remain last |
---|
121 | }; |
---|
122 | |
---|
123 | enum ParameterProblemCode |
---|
124 | { |
---|
125 | POINTER_INDICATES_THE_ERROR, |
---|
126 | PARAMETER_PROBLEM_UNKNOWN, // non-standard default, must remain last but one |
---|
127 | PARAMETER_PROBLEM_LENGTH // length indicator, must remain last |
---|
128 | }; |
---|
129 | |
---|
130 | ICMPv4PacketImpl(int dataSize = 48); |
---|
131 | /// Constructor. Creates an ICMPv4PacketImpl. |
---|
132 | |
---|
133 | ~ICMPv4PacketImpl(); |
---|
134 | /// Destructor. |
---|
135 | |
---|
136 | int packetSize() const; |
---|
137 | /// Returns the total length of packet (header + data); |
---|
138 | |
---|
139 | struct timeval time(Poco::UInt8* buffer = 0, int length = 0) const; |
---|
140 | /// Returns current epoch time if either buffer or length are equal to zero. |
---|
141 | /// Otherwise, it extracts the time value from the supplied buffer. |
---|
142 | /// |
---|
143 | /// Buffer includes IP header, ICMP header and data. |
---|
144 | |
---|
145 | bool validReplyID(Poco::UInt8* buffer, int length) const; |
---|
146 | /// Returns true if the extracted id is recognized |
---|
147 | /// (i.e. equals the process id). |
---|
148 | /// |
---|
149 | /// Buffer includes IP header, ICMP header and data. |
---|
150 | |
---|
151 | virtual std::string errorDescription(Poco::UInt8* buffer, int length); |
---|
152 | /// Returns error description string. |
---|
153 | /// If supplied buffer contains ICMPv4 echo reply packet, an |
---|
154 | /// empty string is returned indicating the absence of error. |
---|
155 | /// |
---|
156 | /// Buffer includes IP header, ICMP header and data. |
---|
157 | |
---|
158 | virtual std::string typeDescription(int typeId); |
---|
159 | /// Returns the description of the packet type. |
---|
160 | |
---|
161 | static const Poco::UInt16 MAX_PACKET_SIZE; |
---|
162 | static const std::string MESSAGE_TYPE[MESSAGE_TYPE_LENGTH]; |
---|
163 | static const Poco::UInt8 DESTINATION_UNREACHABLE_TYPE; // 3 |
---|
164 | static const Poco::UInt8 SOURCE_QUENCH_TYPE; // 4 |
---|
165 | static const Poco::UInt8 REDIRECT_MESSAGE_TYPE; // 5 |
---|
166 | static const Poco::UInt8 TIME_EXCEEDED_TYPE; // 11 |
---|
167 | static const Poco::UInt8 PARAMETER_PROBLEM_TYPE; // 12 |
---|
168 | |
---|
169 | private: |
---|
170 | void initPacket(); |
---|
171 | Header* header(Poco::UInt8* buffer, int length) const; |
---|
172 | Poco::UInt8* data(Poco::UInt8* buffer, int length) const; |
---|
173 | |
---|
174 | static const std::string DESTINATION_UNREACHABLE_CODE[DESTINATION_UNREACHABLE_LENGTH]; |
---|
175 | static const std::string REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_LENGTH]; |
---|
176 | static const std::string TIME_EXCEEDED_CODE[TIME_EXCEEDED_LENGTH]; |
---|
177 | static const std::string PARAMETER_PROBLEM_CODE[PARAMETER_PROBLEM_LENGTH]; |
---|
178 | |
---|
179 | Poco::UInt16 _seq; |
---|
180 | }; |
---|
181 | |
---|
182 | |
---|
183 | } } // namespace Poco::Net |
---|
184 | |
---|
185 | |
---|
186 | #endif // Net_ICMPv4PacketImpl_INCLUDED |
---|