|
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // pingmodel.h - icmp echo client engine |
|
15 // This software has been implemented in the 6PACK |
|
16 // project at the Mobile Networks Laboratory (MNW) |
|
17 // http://www.research.nokia.com/projects/6pack/ |
|
18 // |
|
19 |
|
20 #if !defined __PINGMODEL_H |
|
21 #define __PINGMODEL_H |
|
22 |
|
23 #include <es_sock.h> |
|
24 #include <in_sock.h> |
|
25 |
|
26 #include <nifmbuf.h> |
|
27 #include "in_icmp.h" |
|
28 #include <icmp6_hdr.h> |
|
29 |
|
30 #include "const.h" |
|
31 |
|
32 #ifdef IAPSETTING |
|
33 #include <agentclient.h> |
|
34 #endif |
|
35 |
|
36 // constants |
|
37 |
|
38 #define TIMESTAMP_SIZE sizeof(TInt64) //Timestamp Size |
|
39 |
|
40 #define DEFDATALEN 64 - TIMESTAMP_SIZE //default data length |
|
41 |
|
42 |
|
43 #define SECOND 1000000 // One second is 1000000 us. (1 us. per tick) |
|
44 |
|
45 //ICMPv6 codes not defined in .h's |
|
46 #define KInet6ICMP_HopLimitExceeded 0 |
|
47 #define KInet6ICMP_FragReassExceeded 1 |
|
48 |
|
49 #define KInet6ICMP_NoRoute 0 |
|
50 #define KInet6ICMP_AdminProhibition 1 |
|
51 #define KInet6ICMP_NotNeighbour 2 |
|
52 #define KInet6ICMP_AddrUnreach 3 |
|
53 #define KInet6ICMP_PortUnreach 4 |
|
54 |
|
55 #define KInet6ICMP_ErrHdrField 0 |
|
56 #define KInet6ICMP_NextHdrUnknown 1 |
|
57 #define KInet6ICMP_OptionUnkown 2 |
|
58 |
|
59 |
|
60 #define MAX_PATTERN_LENGTH 16 //Must match the value of the pattern in the RSS file!!! |
|
61 #ifdef IAPSETTING |
|
62 #define MAX_IAP_LENGTH 5 //Must match the value of the pattern in the RSS file!!! |
|
63 #endif |
|
64 class CPingContainer; |
|
65 class CPingSender; |
|
66 |
|
67 |
|
68 const TUint KPingQuiet = 0x01; |
|
69 const TUint KPingVerbose = 0x02; |
|
70 const TUint KPingPackLimit = 0x04; |
|
71 const TUint KPingDebug = 0x08; |
|
72 |
|
73 //Contains engine's saved preferences |
|
74 class TPreferences |
|
75 { |
|
76 public: |
|
77 TUint iFlags; |
|
78 //Contains: |
|
79 //Bit 0 TBool iQuiet; //No packet info, just statistics |
|
80 //Bit 1 TBool iVerbose; //Verbose Output. All ICMP packets, not only Echo reply |
|
81 //Bit 2 TBool iPackLimit; //Indicates if unlimited packet number |
|
82 //Bit 3 TBool iDebug; //Set the SO_DEBUG flag in the socket |
|
83 |
|
84 TUint iSecWait; //Time between sent packets (Default 1 second) |
|
85 TUint iPacketDataSize; //Default Data Size (not including ICMP header) |
|
86 TUint iTotalPackets; //Number of ICMP Echo Request packets to send |
|
87 TUint iLastSecWait; //Time to wait for the Last packet. Default 2 |
|
88 TBuf<KHostNameLimit> iHostname; |
|
89 TBuf<MAX_PATTERN_LENGTH> iPattern; //Pattern to fill the packet.Default 0xFF (Stored as text) |
|
90 |
|
91 #ifdef IAPSETTING |
|
92 TUint iIAP; |
|
93 #endif |
|
94 //Not used yet |
|
95 //TUint iHopLimit; //Time-to-live in hops default 255 (0 means not set) |
|
96 //TBool iNumericOutput; //Resolve adresses by default |
|
97 }; |
|
98 |
|
99 class CPingSender; |
|
100 class CPingSingleSender; |
|
101 //Ping Utility class. Contains All the ping model and create active objects to |
|
102 // send and receive from the socket |
|
103 class CPing : public CActive |
|
104 { |
|
105 friend class CPingSender; |
|
106 friend class CPingSingleSender; |
|
107 public: |
|
108 CPing(); |
|
109 ~CPing(); |
|
110 |
|
111 void ConstructL(TPreferences aPref); |
|
112 |
|
113 //return the current preferences |
|
114 void GetPreferences(TPreferences &aPref); |
|
115 static void DefaultPreferences(TPreferences &aPref); |
|
116 void SetConsole(CPingContainer *aConsole); |
|
117 TDes* GetHostName(); |
|
118 void SetHostName(const TDesC& ahostname); |
|
119 |
|
120 void EndPingL(); |
|
121 TBool IsRunning(); |
|
122 void BeginL(); |
|
123 |
|
124 #ifdef IAPSETTING |
|
125 void StartConnection(); |
|
126 #endif |
|
127 |
|
128 protected: |
|
129 |
|
130 TUint16 in_chksum(TUint16 *data, TUint len); |
|
131 void ComposeFirstICMPPacket(); //Compose a whole ICMPPacket |
|
132 void ComposeICMPPacket(); //Only composes timestamp and sequence number |
|
133 void StampPacket(); // Modify the composed packet with fresh timestamp and checksum |
|
134 void NextSeq(); |
|
135 |
|
136 void CloseAll(); //Closes all the resources |
|
137 void PrintICMPData(const TDesC8& adata); |
|
138 |
|
139 void IssueRequest(); |
|
140 void RunL(); //Segment of code to execute |
|
141 void DoCancel(); //Stops Execution of Ping |
|
142 |
|
143 private: |
|
144 void CreateSocketAOL(); |
|
145 void Statistics(); |
|
146 void WriteLineIfNotQuiet(const TDesC& abuf); |
|
147 void ErrorL(const TDesC& string,TInt error); |
|
148 void CLR(TUint16 num); |
|
149 void SET(TUint16 num); |
|
150 TBool TEST(TUint16 num); |
|
151 TPtrC PacketType(ThdrICMP *hdr); |
|
152 //TPtrC PacketTypev6(TInet6HeaderICMP_Echo *aHdr); |
|
153 void PacketTypev6(TDes& buf,ThdrICMP *aHdr); |
|
154 void Stop(); |
|
155 TUint16 RandomNumber(); |
|
156 |
|
157 private: |
|
158 |
|
159 CPingContainer *iConsole; |
|
160 RSocketServ iSockServ; // Socket Server |
|
161 RSocket iSocket; // Socket |
|
162 RHostResolver iHostResolv; //Resolver |
|
163 //TRequestStatus status; // To wait for socket operations completion |
|
164 |
|
165 CPingSender *iPingSender; |
|
166 TUint32 iDup[8]; //Calculates duplicate packets up to 256 packets, after |
|
167 //it begins again using modulus (%, MOD) |
|
168 |
|
169 HBufC8 *iPacketData; |
|
170 TPtr8 iPacket; |
|
171 TUint16 iId; |
|
172 |
|
173 TUint iMaxTime; //Maximum time |
|
174 TUint iMinTime; //Minimum time |
|
175 TUint iTimeSum; //all times sum |
|
176 TBuf<KHostNameLimit> iHostname; // Contains hostname to ping |
|
177 |
|
178 TUint iDupPackets; //Duplicated packets |
|
179 TBool iRunning; |
|
180 TUint iStage; //Stages to use non-blocking resolver in RunL |
|
181 TNameEntry iEntry; //contains address resolution data |
|
182 |
|
183 HBufC8 *iReceivedDataBuffer; |
|
184 TPtr8 iReceivedData; |
|
185 |
|
186 TUint iType; |
|
187 |
|
188 TInt64 iSeed; //Used for random iId generation |
|
189 |
|
190 public: |
|
191 TBool iNumericOutput; //Resolve adresses by default |
|
192 TUint iSecWait; //Time between sent packets (Default 1 second) |
|
193 TUint iPacketDataSize;//Default Data Size (not including ICMP header) |
|
194 TBool iQuiet; //No packet info, just statistics |
|
195 TUint iHopLimit; //Time-to-live in hops default 255 (0 means not set) |
|
196 TBool iVerbose; //Verbose Output. All ICMP packets, not only Echo reply |
|
197 TBool iPackLimit; //Indicates if unlimited packet number |
|
198 TUint iSentPackets; //ICMP Echo Request Packets sent til now |
|
199 TUint iRecvPackets; //ICMP Echo Reply Packets received til now |
|
200 TUint iTotalPackets; //Number of ICMP Echo Request packets to send |
|
201 TBuf<MAX_PATTERN_LENGTH> iPattern; //Pattern to fill the packet.Default 0xFF |
|
202 TBool iDebug; //Set the SO_DEBUG flag in the socket |
|
203 TUint iLastSecWait; //Time to wait for the Last packet. Default 2 |
|
204 TUint iChksumErrors; |
|
205 TUint iSockErrors; |
|
206 TInetAddr iHostAddr; //Address to Ping |
|
207 #ifdef IAPSETTING |
|
208 TUint iIAP; |
|
209 RGenericAgent iGenericAgent; // To establish the connection with the ISP. |
|
210 #endif |
|
211 }; |
|
212 |
|
213 // Used by CPing |
|
214 // Send packets using a CTimer (CActive) object |
|
215 // Not intended to run alone!!! |
|
216 |
|
217 class CPingSender : public CTimer |
|
218 { |
|
219 public: |
|
220 //constructor |
|
221 CPingSender(); |
|
222 |
|
223 //destructor |
|
224 ~CPingSender(); |
|
225 |
|
226 //second phase constructor |
|
227 void ConstructL(CPing *aPingModel); |
|
228 void FirstRunL(); |
|
229 |
|
230 protected: |
|
231 //Issues next RunL execution |
|
232 void IssueRequest(); |
|
233 |
|
234 //Issues last RunL execution |
|
235 void IssueLastRequest(); |
|
236 |
|
237 // will send all the packets |
|
238 void RunL(); |
|
239 |
|
240 //Cancel Packet Sending |
|
241 void DoCancel(); |
|
242 |
|
243 void SendFirstPacketL(); |
|
244 void SendPacket(); |
|
245 |
|
246 private: |
|
247 CPing *iPingModel; |
|
248 CPingSingleSender *iSender; |
|
249 |
|
250 |
|
251 }; |
|
252 |
|
253 |
|
254 // Used by CPingSender |
|
255 // Sends packets. Cannot be done directly by CPingSender because there are conflicts with |
|
256 // diferent TRequestStatus. |
|
257 |
|
258 |
|
259 class CPingSingleSender : public CActive |
|
260 { |
|
261 public: |
|
262 //constructor |
|
263 CPingSingleSender(CPing *aPingModel); |
|
264 |
|
265 //destructor |
|
266 ~CPingSingleSender(); |
|
267 |
|
268 void FirstRunLD(); |
|
269 void NextPacket(); |
|
270 |
|
271 protected: |
|
272 //Issues next RunL execution |
|
273 void IssueRequest(); |
|
274 |
|
275 //Issues last RunL execution |
|
276 void IssueLastRequest(); |
|
277 |
|
278 // will send all the packets |
|
279 void RunL(); |
|
280 |
|
281 //Cancel Packet Sending |
|
282 void DoCancel(); |
|
283 |
|
284 |
|
285 private: |
|
286 CPing *iPingModel; |
|
287 TUint iUnsent; |
|
288 |
|
289 }; |
|
290 |
|
291 #endif |