|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * CLOGPPPFILTER |
|
16 * Prints out the details of all PPP frames that pass through the MT. |
|
17 * System Includes |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 |
|
23 #include <stdlib.h> |
|
24 #include <assert.h> |
|
25 #include <string.h> |
|
26 #ifdef WIN32 |
|
27 #include <winsock2.h> |
|
28 #else |
|
29 #include <netinet/in.h> |
|
30 #endif |
|
31 |
|
32 |
|
33 /******************************************************************************* |
|
34 * |
|
35 * Local Includes |
|
36 * |
|
37 ******************************************************************************/ |
|
38 #include "CLogPPPFilter.h" |
|
39 |
|
40 |
|
41 /******************************************************************************* |
|
42 * |
|
43 * Definitions |
|
44 * |
|
45 ******************************************************************************/ |
|
46 #define MAX_LCP_MESSAGE_ID 11 |
|
47 |
|
48 |
|
49 /******************************************************************************* |
|
50 * |
|
51 * Macro Functions |
|
52 * |
|
53 ******************************************************************************/ |
|
54 #define EXTRACT_SHORT(ptr) (htons((*((short*)(ptr)))&0x0000FFFF)) |
|
55 #define EXTRACT_CHAR(ptr) ((0x000000FF)&(ptr)) |
|
56 |
|
57 |
|
58 /******************************************************************************* |
|
59 * |
|
60 * Static vars |
|
61 * |
|
62 ******************************************************************************/ |
|
63 static char *lcp_message_desc[] = { "(invalid)", "Configure-Request", "Configure-Ack", "Configure-Nack", |
|
64 "Configure-Reject", "Terminate-Request", "Terminate-Ack", "Code-Reject", |
|
65 "Protocol-Reject", "Echo-Request", "Echo-Reply", "Discard-Request", NULL }; |
|
66 |
|
67 /******************************************************************************* |
|
68 * |
|
69 * PUBLIC METHOD: Constructor |
|
70 * |
|
71 ******************************************************************************/ |
|
72 CLogPppFilter::CLogPppFilter( TPhoneData *aPhoneData, CLog *aLog ) : iOutgoingFrame("PPP-Log::OutgoingFrame",1), iIncomingFrame("PPP-Log::IncomingFrame",1) |
|
73 { |
|
74 // check params |
|
75 assert( aPhoneData != NULL ); |
|
76 assert( aLog != NULL ); |
|
77 |
|
78 //set state |
|
79 iPhoneData = aPhoneData; |
|
80 iLog = aLog; |
|
81 iIncomingFrameOverflowFlag = 0; |
|
82 iOutgoingFrameOverflowFlag = 0; |
|
83 } |
|
84 |
|
85 |
|
86 /******************************************************************************* |
|
87 * |
|
88 * PUBLIC METHOD: Destructor |
|
89 * |
|
90 ******************************************************************************/ |
|
91 CLogPppFilter::~CLogPppFilter() |
|
92 { |
|
93 } |
|
94 |
|
95 |
|
96 /******************************************************************************* |
|
97 * |
|
98 * PUBLIC METHOD: IFilter Interface Methods |
|
99 * |
|
100 ******************************************************************************/ |
|
101 |
|
102 int CLogPppFilter::ProcessIncomingData( char *data, int len ) |
|
103 { |
|
104 int i; |
|
105 for( i = 0; i < len; i++ ) { |
|
106 ProcessByte( &iIncomingFrame, data[i], &iIncomingFrameOverflowFlag ); |
|
107 } |
|
108 return 0; |
|
109 } |
|
110 |
|
111 |
|
112 int CLogPppFilter::ProcessOutgoingData( char *data, int len ) |
|
113 { |
|
114 int i; |
|
115 for( i = 0; i < len; i++ ) { |
|
116 ProcessByte( &iOutgoingFrame, data[i], &iOutgoingFrameOverflowFlag ); |
|
117 } |
|
118 return 0; |
|
119 } |
|
120 |
|
121 |
|
122 void CLogPppFilter::ProcessByte( CPppFrame *aFrame, char c, int *iOverflowFlag ) |
|
123 { |
|
124 char *framedesc; |
|
125 TFrameError ferr; |
|
126 TFrameStatus fstatus; |
|
127 |
|
128 // add the byte to the frame buffer |
|
129 ferr = aFrame->AddByteToFrame( c ); |
|
130 |
|
131 // if this causes an overflow then log the warning and return |
|
132 if( ferr == FE_OVERFLOW ) { |
|
133 if( *iOverflowFlag == 0 ) { |
|
134 iLog->WriteLogEntry( SV_WARNING, aFrame->GetFrameName(), "Frame overflow." ); |
|
135 *iOverflowFlag = 1; |
|
136 } |
|
137 return; |
|
138 } |
|
139 assert( ferr == FE_NONE ); |
|
140 |
|
141 // if the frame isn't completed then return |
|
142 fstatus = aFrame->GetFrameStatus(); |
|
143 if( fstatus != FS_COMPLETE ) { |
|
144 return; |
|
145 } |
|
146 |
|
147 // otherwise print a record about the frame and then clear it |
|
148 framedesc = CreateFrameDescription( aFrame ); |
|
149 iLog->WriteLogEntry( SV_INFO, aFrame->GetFrameName(), framedesc ); |
|
150 aFrame->ClearFrame(); |
|
151 *iOverflowFlag = 0; |
|
152 } |
|
153 |
|
154 |
|
155 /******************************************************************************* |
|
156 * |
|
157 * PRIVATE METHOD: Create FrameDescription |
|
158 * |
|
159 * Protocol [Message Type] (Frame Size) |
|
160 * IPCP (21) |
|
161 * LCP Echo-Request (48) |
|
162 * |
|
163 ******************************************************************************/ |
|
164 char *CLogPppFilter::CreateFrameDescription( CPppFrame *aFrame ) |
|
165 { |
|
166 char *frame_ptr; |
|
167 int frame_size; |
|
168 int protocol_id; |
|
169 char *bptr; |
|
170 |
|
171 // get the frame |
|
172 frame_ptr = aFrame->GetFrameBuffer( &frame_size ); |
|
173 |
|
174 // clear the description buffer |
|
175 iFrameDescription[0] = 0; |
|
176 |
|
177 // now insert the protocol and message if appropriate |
|
178 protocol_id = AddProtocolName( frame_ptr, frame_size ); |
|
179 |
|
180 // add the length |
|
181 bptr = &(iFrameDescription[strlen(iFrameDescription)]); |
|
182 sprintf( bptr, "(%d).", frame_size ); |
|
183 |
|
184 // done |
|
185 return iFrameDescription; |
|
186 } |
|
187 |
|
188 |
|
189 /***************************************************************************************** |
|
190 * |
|
191 * PRIVATE METHOD: AddProtocolName |
|
192 * |
|
193 ****************************************************************************************/ |
|
194 int CLogPppFilter::AddProtocolName( char *aFrameBuffer, int aFrameSize ) |
|
195 { |
|
196 int protocol_id; |
|
197 int datapos = 0; |
|
198 char *ptr; |
|
199 int message_type; |
|
200 |
|
201 // find the position of the protocol field (packet format can change a fair bit) |
|
202 if( aFrameBuffer[0] == 0x7E ) { |
|
203 datapos++; |
|
204 if( aFrameBuffer[1] == ((char)0x000000FF) ) { |
|
205 datapos++; |
|
206 if( aFrameBuffer[2] == 0x03 ) { |
|
207 datapos++; |
|
208 } |
|
209 } |
|
210 } |
|
211 |
|
212 // get the protocol id and the target pointer |
|
213 protocol_id = EXTRACT_SHORT(&aFrameBuffer[datapos]); |
|
214 ptr = &(iFrameDescription[strlen(iFrameDescription)]); |
|
215 message_type = EXTRACT_CHAR(aFrameBuffer[datapos+2]); |
|
216 |
|
217 // now print the protocol name |
|
218 switch( protocol_id ) { |
|
219 case 0xc021: |
|
220 sprintf( ptr, "LCP " ); |
|
221 AddLcpMessageTypeString( message_type ); |
|
222 break; |
|
223 |
|
224 case 0xc023: |
|
225 sprintf( ptr, "PAP " ); |
|
226 break; |
|
227 |
|
228 case 0x8021: |
|
229 sprintf( ptr, "IPCP " ); |
|
230 break; |
|
231 |
|
232 case 0x80FD: |
|
233 sprintf( ptr, "CCP " ); |
|
234 break; |
|
235 |
|
236 default: |
|
237 sprintf( ptr, "0x%X ", protocol_id ); |
|
238 protocol_id = 0; |
|
239 break; |
|
240 } |
|
241 |
|
242 // return the protocol id |
|
243 return protocol_id; |
|
244 } |
|
245 |
|
246 |
|
247 /***************************************************************************************** |
|
248 * |
|
249 * PRIVATE METHOD: AddLcpMessageTypeString |
|
250 * |
|
251 ****************************************************************************************/ |
|
252 void CLogPppFilter::AddLcpMessageTypeString( int aMessageType ) |
|
253 { |
|
254 char *bptr; |
|
255 |
|
256 // get the ptr to the output buffer |
|
257 bptr = &(iFrameDescription[strlen(iFrameDescription)]); |
|
258 |
|
259 // make sure the message id is known |
|
260 if( (aMessageType <= 0) || (aMessageType >= MAX_LCP_MESSAGE_ID) ) { |
|
261 sprintf( bptr, "0x%X ", aMessageType ); |
|
262 return; |
|
263 } |
|
264 |
|
265 // print the name of the |
|
266 sprintf( bptr, lcp_message_desc[aMessageType] ); |
|
267 } |
|
268 |