1 | // |
---|
2 | // ICMPPacketImpl.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/ICMPPacketImpl.h#1 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: ICMP |
---|
8 | // Module: ICMPPacketImpl |
---|
9 | // |
---|
10 | // Definition of the ICMPPacketImpl 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_ICMPPacketImpl_INCLUDED |
---|
40 | #define Net_ICMPPacketImpl_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Net/Socket.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace Net { |
---|
49 | |
---|
50 | |
---|
51 | class Net_API ICMPPacketImpl |
---|
52 | /// This is the abstract class for ICMP packet implementations. |
---|
53 | { |
---|
54 | public: |
---|
55 | ICMPPacketImpl(int dataSize = 48); |
---|
56 | /// Constructor. Creates an ICMPPacketImpl. |
---|
57 | |
---|
58 | virtual ~ICMPPacketImpl(); |
---|
59 | /// Destructor. |
---|
60 | |
---|
61 | const Poco::UInt8* packet(bool init = true); |
---|
62 | /// Returns raw ICMP packet. |
---|
63 | /// ICMP header and data are included in the packet. |
---|
64 | /// If init is true, initPacket() is called. |
---|
65 | |
---|
66 | virtual int packetSize() const = 0; |
---|
67 | /// Returns the total size of packet (ICMP header + data) in number of octets. |
---|
68 | /// Must be overriden. |
---|
69 | |
---|
70 | virtual int maxPacketSize() const; |
---|
71 | /// Returns the maximum permitted size of packet in number of octets. |
---|
72 | |
---|
73 | Poco::UInt16 sequence() const; |
---|
74 | /// Returns the most recent sequence number generated. |
---|
75 | |
---|
76 | void setDataSize(int dataSize); |
---|
77 | /// Sets data size. |
---|
78 | |
---|
79 | int getDataSize() const; |
---|
80 | /// Returns data size. |
---|
81 | |
---|
82 | virtual struct timeval time(Poco::UInt8* buffer = 0, int length = 0) const = 0; |
---|
83 | /// Returns current epoch time if either argument is equal to zero. |
---|
84 | /// Otherwise, it extracts the time value from the supplied buffer. |
---|
85 | /// |
---|
86 | /// Supplied buffer includes IP header, ICMP header and data. |
---|
87 | /// Must be overriden. |
---|
88 | |
---|
89 | virtual bool validReplyID(unsigned char* buffer, int length) const = 0; |
---|
90 | /// Returns true if the extracted id is recognized |
---|
91 | /// (i.e. equals the process id). |
---|
92 | /// |
---|
93 | /// Supplied buffer includes IP header, ICMP header and data. |
---|
94 | /// Must be overriden. |
---|
95 | |
---|
96 | virtual std::string errorDescription(Poco::UInt8* buffer, int length) = 0; |
---|
97 | /// Returns error description string. |
---|
98 | /// If supplied buffer contains an ICMP echo reply packet, an |
---|
99 | /// empty string is returned indicating the absence of error. |
---|
100 | /// |
---|
101 | /// Supplied buffer includes IP header, ICMP header and data. |
---|
102 | /// Must be overriden. |
---|
103 | |
---|
104 | virtual std::string typeDescription(int typeId) = 0; |
---|
105 | /// Returns the description of the packet type. |
---|
106 | /// Must be overriden. |
---|
107 | |
---|
108 | static const Poco::UInt16 MAX_PACKET_SIZE; |
---|
109 | static const Poco::UInt16 MAX_SEQ_VALUE; |
---|
110 | |
---|
111 | protected: |
---|
112 | Poco::UInt16 nextSequence(); |
---|
113 | /// Increments sequence number and returns the new value. |
---|
114 | |
---|
115 | void resetSequence(); |
---|
116 | /// Resets the sequence to zero. |
---|
117 | |
---|
118 | virtual void initPacket() = 0; |
---|
119 | /// (Re)assembles the packet. |
---|
120 | /// Must be overriden. |
---|
121 | |
---|
122 | Poco::UInt16 checksum(Poco::UInt16 *addr, Poco::Int32 len); |
---|
123 | /// Calculates the checksum for supplied buffer. |
---|
124 | |
---|
125 | private: |
---|
126 | Poco::UInt16 _seq; |
---|
127 | Poco::UInt8* _pPacket; |
---|
128 | int _dataSize; |
---|
129 | |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | // |
---|
134 | // inlines |
---|
135 | // |
---|
136 | inline Poco::UInt16 ICMPPacketImpl::sequence() const |
---|
137 | { |
---|
138 | return _seq; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | inline Poco::UInt16 ICMPPacketImpl::nextSequence() |
---|
143 | { |
---|
144 | return ++_seq; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | inline void ICMPPacketImpl::resetSequence() |
---|
149 | { |
---|
150 | _seq = 0; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | inline int ICMPPacketImpl::maxPacketSize() const |
---|
155 | { |
---|
156 | return MAX_PACKET_SIZE; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | } } // namespace Poco::Net |
---|
161 | |
---|
162 | |
---|
163 | #endif // Net_ICMPPacketImpl_INCLUDED |
---|