|
1 // Copyright (c) 2004-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 // Implements CPacketLogger. |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 */ |
|
21 |
|
22 |
|
23 #include <es_sock.h> |
|
24 #include <hal.h> |
|
25 #include "PacketLogger.h" |
|
26 |
|
27 static const TInt KInternalBufferSize = 4096; |
|
28 |
|
29 /** Constants for tcpdump log*/ |
|
30 static const TUint32 KTcpDumpFileHeaderMagic = 0xa1b2c3d4; |
|
31 static const TUint16 KTcpDumpVersionMajor = 2; |
|
32 static const TUint16 KTcpDumpVersionMinor = 4; |
|
33 |
|
34 /** Constants for pppdump log */ |
|
35 //static const TInt8 KPppDumpRecSentData = 1; ENABLE IF NEEDED |
|
36 //static const TInt8 KPppDumpRecRcvData = 2; ENABLE IF NEEDED |
|
37 //static const TInt8 KPppDumpRecRcvDelim = 4; ENABLE IF NEEDED |
|
38 static const TInt8 KPppDumpRecTimeStepLong = 5; |
|
39 static const TInt8 KPppDumpRecTimeStepShort = 6; |
|
40 static const TInt8 KPppDumpRecResetTime = 7; |
|
41 |
|
42 /** |
|
43 * Factory method for CPacketLogger. |
|
44 * |
|
45 * @param aTag The tag name for the log |
|
46 * @param aFileName The filename to log to. |
|
47 * @param aDumpType The type of dump (one of TDumpType values) |
|
48 * @param aLinkType Used by tcpdump. One of the types defined in |
|
49 * libpcap/bpf/net/bpf.h |
|
50 * In case of ETcpDump this is *TUint32 |
|
51 * which represents the linktype as defined by TcpDump format. |
|
52 * In case of EPppDump it is *TUint8 which represents the direction. |
|
53 * @return Ownership of a new CPacketLogger. |
|
54 */ |
|
55 EXPORT_C CPacketLogger* CPacketLogger::NewL(const TDesC8& aTag, const TDesC8& aFileName, const TDumpType aDumpType, const TInt aLinkType) |
|
56 { |
|
57 CPacketLogger* self = new(ELeave)CPacketLogger; |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(aTag, aFileName, aDumpType, aLinkType); |
|
60 CleanupStack::Pop(self); |
|
61 return self; |
|
62 } |
|
63 |
|
64 /** |
|
65 * 2nd-phase construction. Creates the libpcap file. Adds the |
|
66 * file header based on the dump type. |
|
67 * |
|
68 * @param aTag The tag name for the log |
|
69 * @param aFileName The filename to log to. |
|
70 * @param aDumpType The type of dump (one of TDumpType values) |
|
71 * @param aLinkType Used by tcpdump. (Valid types are specified |
|
72 * in libpcap/bpf/net/bpf.h) |
|
73 */ |
|
74 #ifdef __FLOG_ACTIVE |
|
75 void CPacketLogger::ConstructL(const TDesC8& aTag, const TDesC8& aFileName, const TDumpType aDumpType, const TInt aLinkType) |
|
76 #else |
|
77 void CPacketLogger::ConstructL(const TDesC8&, const TDesC8&, const TDumpType aDumpType, const TInt aLinkType) |
|
78 #endif |
|
79 { |
|
80 TInt tickPeriod; |
|
81 iPacketCounter = 0; |
|
82 iDumpType = aDumpType; |
|
83 //initialize the time related data |
|
84 HAL::Get(HALData::ESystemTickPeriod, tickPeriod); |
|
85 iTickPeriod = tickPeriod; |
|
86 iTimeLastPacket = User::TickCount(); |
|
87 iTimeLastPacket *= iTickPeriod; |
|
88 //initialize intermal buffer |
|
89 iHBuf = HBufC8::NewMaxL(KInternalBufferSize); |
|
90 //write the header |
|
91 if(aDumpType == ETcpDump) |
|
92 { |
|
93 __FLOG_OPEN(aTag, aFileName); |
|
94 WriteTcpDumpHeader(aLinkType); |
|
95 } |
|
96 else if(aDumpType == EPppDump) |
|
97 { |
|
98 __FLOG_OPEN(aTag, aFileName); |
|
99 WritePppDumpHeader(); |
|
100 } |
|
101 } |
|
102 |
|
103 /** |
|
104 * Constructor |
|
105 */ |
|
106 CPacketLogger::CPacketLogger() |
|
107 { |
|
108 } |
|
109 |
|
110 /** |
|
111 * Destructor |
|
112 */ |
|
113 EXPORT_C CPacketLogger::~CPacketLogger() |
|
114 { |
|
115 delete iHBuf; |
|
116 iHBuf = NULL; |
|
117 |
|
118 if(iDumpType == ETcpDump || iDumpType == EPppDump) |
|
119 { |
|
120 __FLOG_CLOSE; |
|
121 } |
|
122 } |
|
123 |
|
124 /** |
|
125 * Adds a raw ip packet to the dump file based on the dump format. |
|
126 * @param aPacket Packet as RMBufChain |
|
127 * @param aDirection Direction of packet. Meaningful only in case of PPP Dump |
|
128 */ |
|
129 EXPORT_C void CPacketLogger::WritePacket(const RMBufChain& aPacket, const TUint8 aDirection) |
|
130 { |
|
131 TInt packetLength = aPacket.Length() - aPacket.First()->Length(); |
|
132 |
|
133 if (packetLength > KInternalBufferSize) |
|
134 { |
|
135 _LIT8(ERROR_STR,"Packet after packet %d was larger than 4K. It is skipped."); |
|
136 __FLOG_1(ERROR_STR, iPacketCounter); |
|
137 //Packet is too big for the internal buffer. Don't dump it. |
|
138 return; |
|
139 } |
|
140 |
|
141 TPtr8 packetPtr = iHBuf->Des(); |
|
142 packetPtr.SetLength(packetLength); |
|
143 aPacket.CopyOut(packetPtr, aPacket.First()->Length()); |
|
144 |
|
145 WritePacket(packetPtr, aDirection); |
|
146 } |
|
147 |
|
148 /** |
|
149 * Adds a text log to the log file. |
|
150 * @param aText text to be added |
|
151 */ |
|
152 EXPORT_C void CPacketLogger::WriteText(const TDesC8& aText) |
|
153 { |
|
154 __FLOG_0(aText); |
|
155 } |
|
156 |
|
157 /** |
|
158 * Adds a raw ip packet to the dump file based on the dump format. |
|
159 * @param aPacket Packet as buffer |
|
160 * @param aDirection Direction of packet. Meaningful only in case of PPP Dump |
|
161 */ |
|
162 EXPORT_C void CPacketLogger::WritePacket(const TDesC8& aPacket, const TUint8 aDirection) |
|
163 { |
|
164 iPacketCounter++; |
|
165 if(iDumpType == ETcpDump) |
|
166 { |
|
167 TcpDumpPacket(aPacket); |
|
168 } |
|
169 else if(iDumpType == EPppDump) |
|
170 { |
|
171 PppDumpPacket(aPacket, aDirection); |
|
172 } |
|
173 } |
|
174 |
|
175 /** |
|
176 * Dumps file header in a format compatible with Libcap. |
|
177 * Libcap compatible files can be manipulated via |
|
178 * utilities such as Mergecap (http://www.ethereal.com/docs/man-pages/mergecap.1.html) |
|
179 * Ethereal (http://www.ethereal.com/docs/man-pages/ethereal.1.html) |
|
180 * etc. More information can be found from www.ethereal.com |
|
181 * tcpdump file header format is as follows |
|
182 * @code |
|
183 * struct TFileHeader |
|
184 * { |
|
185 * TUint32 magic; |
|
186 * TUint16 version_major; |
|
187 * TUint16 version_minor; |
|
188 * TInt32 thiszone; // gmt to local correction |
|
189 * TUint32 sigfigs; // accuracy of timestamps |
|
190 * TUint32 snaplen; // max length saved portion of each pkt |
|
191 * TUint32 linktype; // data link type (LINKTYPE specified in libpcap/bpf/net/bpf.h) |
|
192 * // 12 corresponds to raw IP |
|
193 * }; |
|
194 * @endcode |
|
195 * @param aLinkType Link type for the packet (One of the types specified in |
|
196 * libpcap/bpf/net/bpf.h) |
|
197 */ |
|
198 void CPacketLogger::WriteTcpDumpHeader(const TInt aLinkType) |
|
199 { |
|
200 //construct the file header |
|
201 TBuf8<sizeof(TUint32)*5+sizeof(TUint16)*2> fileHeader; |
|
202 fileHeader.SetLength(fileHeader.MaxLength()); |
|
203 |
|
204 BigEndian::Put32(&fileHeader[0], KTcpDumpFileHeaderMagic); |
|
205 BigEndian::Put16(&fileHeader[4], KTcpDumpVersionMajor); |
|
206 BigEndian::Put16(&fileHeader[6], KTcpDumpVersionMinor); |
|
207 BigEndian::Put32(&fileHeader[8], 0); //gmt to local correction |
|
208 BigEndian::Put32(&fileHeader[12], 0); //accuracy of timestamps |
|
209 BigEndian::Put32(&fileHeader[16], 0xffff); //max length saved portion of each packet |
|
210 BigEndian::Put32(&fileHeader[20], static_cast<TUint32>(aLinkType)); |
|
211 |
|
212 //write the file header |
|
213 __FLOG_BINARY(fileHeader); |
|
214 } |
|
215 |
|
216 /** |
|
217 * Adds a raw ip packet to the dump file based on tcpdump format. |
|
218 * @param aPacket TCP packet to dump |
|
219 * tcpdump packet header format is as follows |
|
220 * @code |
|
221 * struct TTimeval |
|
222 * { |
|
223 * TUint32 tv_sec; // seconds |
|
224 * TUint32 tv_usec; // microseconds |
|
225 * }; |
|
226 * struct TPacketHeader |
|
227 * { |
|
228 * TTimeval ts; // time stamp |
|
229 * TUint32 caplen; // length of portion present |
|
230 * TUint32 len; // length of this packet (off wire) |
|
231 * }; |
|
232 * @endcode |
|
233 */ |
|
234 void CPacketLogger::TcpDumpPacket(const TDesC8& aPacket) |
|
235 { |
|
236 TInt32 nsecs; |
|
237 TInt32 secs; |
|
238 TInt64 curTime = User::TickCount(); |
|
239 curTime *= iTickPeriod; |
|
240 |
|
241 //seconds portion of offset |
|
242 TInt64 timeInSeconds = curTime / 1000000; |
|
243 //microsends portion of offset |
|
244 TInt64 timeInNSeconds; |
|
245 I64DIVMOD(curTime, 1000000, timeInNSeconds); |
|
246 |
|
247 secs = I64INT(timeInSeconds); |
|
248 nsecs = I64INT(timeInNSeconds); |
|
249 iTimeLastPacket = curTime; |
|
250 |
|
251 //construct the packet header |
|
252 TBuf8<sizeof(TUint32)*4> packetHeader; |
|
253 packetHeader.SetLength(packetHeader.MaxLength()); |
|
254 |
|
255 BigEndian::Put32(&packetHeader[0], secs); //seconds for time stamp |
|
256 BigEndian::Put32(&packetHeader[4], nsecs); //microseconds for time stamp |
|
257 BigEndian::Put32(&packetHeader[8], aPacket.Length()); //length of portion present |
|
258 BigEndian::Put32(&packetHeader[12], aPacket.Length()); //length of this packet |
|
259 |
|
260 //dump the packet header |
|
261 __FLOG_BINARY(packetHeader); |
|
262 |
|
263 //dump the packet |
|
264 __FLOG_BINARY(aPacket); |
|
265 } |
|
266 |
|
267 /** |
|
268 * Dumps file header in a format compatible with pppdump. |
|
269 * As a file header just drop a 0x07|t3|t2|t1|t0 record followed by a |
|
270 * 0x05|t3|t2|t1|t0 record, that seems common practice. |
|
271 * Since this "header" is actually a regular record, it can be |
|
272 * appended to an existing file. |
|
273 */ |
|
274 void CPacketLogger::WritePppDumpHeader() |
|
275 { |
|
276 _LIT(Ktime_tOrigin,"19700000:000000.000000"); |
|
277 TTime time_t_Origin(Ktime_tOrigin); |
|
278 TTimeIntervalSeconds secs; |
|
279 TTime timeNow; |
|
280 timeNow.UniversalTime(); |
|
281 timeNow.SecondsFrom(time_t_Origin,secs); |
|
282 |
|
283 TBuf8<10> fileHeader; |
|
284 fileHeader.SetLength(fileHeader.MaxLength()); |
|
285 |
|
286 fileHeader[0] = KPppDumpRecResetTime; |
|
287 BigEndian::Put32(&fileHeader[1], secs.Int()); |
|
288 |
|
289 fileHeader[5] = KPppDumpRecTimeStepLong; |
|
290 BigEndian::Put32(&fileHeader[6], 0); |
|
291 |
|
292 //dump the file header |
|
293 __FLOG_BINARY(fileHeader); |
|
294 } |
|
295 |
|
296 /** |
|
297 * Dumps a packet in a pppdump format (see www.ethereal.com) |
|
298 * @param aPacket Raw IP packet to dump |
|
299 * For each record the format is: |
|
300 * @code |
|
301 * 0x07|t3|t2|t1|t0 Reset time t = time_t (UNIX: secs since 01/01/1970) |
|
302 * 0x06|t0 Time step (short) - ts = time step (tenths) |
|
303 * 0x05|t3|t2|t1|t0 Time step (short) - ts = time step (tenths) |
|
304 * 0x04 Receive deliminator (not seen in practice) |
|
305 * 0x02|n1|n0 Received data - n = number of bytes following |
|
306 * 0x01|n1|n0 Sent data - n = number of bytes following |
|
307 * @endcode |
|
308 * Byte ordering of the header is little endian |
|
309 * Byte ordering of the packet is network byte order (big endian) |
|
310 * |
|
311 * @param aPacket PPP packet as buffer |
|
312 * @param aDirection Direction of packet. |
|
313 */ |
|
314 void CPacketLogger::PppDumpPacket(const TDesC8& aPacket, const TUint8 aDirection) |
|
315 { |
|
316 TInt64 curTime = User::TickCount(); |
|
317 curTime *= iTickPeriod; |
|
318 //offset in microseconds |
|
319 TInt64 timeOffset = curTime - iTimeLastPacket; |
|
320 //seconds portion of offset |
|
321 TInt64 timeInTenthOfSeconds = timeOffset / 100000; |
|
322 TUint32 timeStep = I64INT(timeInTenthOfSeconds); |
|
323 |
|
324 if(timeStep) |
|
325 { |
|
326 iTimeLastPacket = curTime; |
|
327 } |
|
328 |
|
329 TBuf8<2+6> recordHeader; |
|
330 int i=0; |
|
331 if (timeStep > 0xff) |
|
332 { |
|
333 // 32-bit differential time record |
|
334 recordHeader.SetLength(8); |
|
335 recordHeader[i++] = KPppDumpRecTimeStepLong; |
|
336 BigEndian::Put32(&recordHeader[i], timeStep); |
|
337 i += 4; |
|
338 } |
|
339 else if (timeStep > 0) |
|
340 { |
|
341 // 8-bit differential time record |
|
342 recordHeader.SetLength(5); |
|
343 recordHeader[i++] = KPppDumpRecTimeStepShort; |
|
344 recordHeader[i++] = (TUint8) timeStep; |
|
345 } |
|
346 else |
|
347 { |
|
348 recordHeader.SetLength(3); |
|
349 } |
|
350 recordHeader[i++] = aDirection; |
|
351 BigEndian::Put16(&recordHeader[i], (TUint16)aPacket.Length()); |
|
352 |
|
353 //dump the packet header |
|
354 __FLOG_BINARY(recordHeader); |
|
355 |
|
356 //dump the packet |
|
357 __FLOG_BINARY(aPacket); |
|
358 } |