|
1 /* |
|
2 * Copyright (c) 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: IPProxy TCP protocol expression for a regular TCP message |
|
15 * connection. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 #include "CExprTCPMsg.h" |
|
23 #include "CommRouterDefinitions.h" |
|
24 #include "MProtocolObserverTCP.h" |
|
25 #include "MExpressionObserver.h" |
|
26 |
|
27 #define DEBUG_FILENAME "IPProxyEngine.log" |
|
28 #include "DebugPrint.h" |
|
29 |
|
30 |
|
31 // ============================ MEMBER FUNCTIONS =============================== |
|
32 |
|
33 // ----------------------------------------------------------------------------- |
|
34 // CExprTCPMsg::CExprTCPMsg |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 CExprTCPMsg::CExprTCPMsg( MExpressionObserver* aObserver ) |
|
38 : iObserver( aObserver ) |
|
39 { |
|
40 __ASSERT_DEBUG( iObserver, User::Invariant() ); |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // CExprTCPMsg::NewL |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 CExprTCPMsg* CExprTCPMsg::NewL( MExpressionObserver* aObserver ) |
|
48 { |
|
49 CExprTCPMsg* self = CExprTCPMsg::NewLC( aObserver ); |
|
50 CleanupStack::Pop(); |
|
51 |
|
52 return self; |
|
53 } |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // CExprTCPMsg::NewLC |
|
57 // ----------------------------------------------------------------------------- |
|
58 // |
|
59 CExprTCPMsg* CExprTCPMsg::NewLC( MExpressionObserver* aObserver ) |
|
60 { |
|
61 CExprTCPMsg* self = new( ELeave ) CExprTCPMsg( aObserver ); |
|
62 CleanupStack::PushL( self ); |
|
63 |
|
64 return self; |
|
65 } |
|
66 |
|
67 |
|
68 // Destructor |
|
69 CExprTCPMsg::~CExprTCPMsg() |
|
70 { |
|
71 } |
|
72 |
|
73 |
|
74 // ----------------------------------------------------------------------------- |
|
75 // CExprTCPMsg::HandleReceivedDataL() |
|
76 // ----------------------------------------------------------------------------- |
|
77 // |
|
78 TBool CExprTCPMsg::HandleRecievedMsgL( TDes8& aData, TInt& aStartPos, TInt& aLength ) |
|
79 { |
|
80 // Check if the prefix matches |
|
81 aStartPos = aData.Find( KTCPPrefix ); |
|
82 |
|
83 if ( aStartPos != KErrNotFound ) |
|
84 { |
|
85 // Found a matching prefix |
|
86 // Let the observer know |
|
87 iObserver->FrameStarted(); |
|
88 |
|
89 TPtr8 dataToParse( aData.MidTPtr( aStartPos ) ); |
|
90 |
|
91 TInt err = TryParsingL( dataToParse, aLength ); |
|
92 |
|
93 if ( err != KErrNone ) |
|
94 { |
|
95 // corrupted data in the frame |
|
96 iObserver->ProtocolErrorL( err, aData ); |
|
97 // delete the corrupted data |
|
98 aData.SetLength( 0 ); |
|
99 } |
|
100 |
|
101 return ETrue; |
|
102 } |
|
103 else |
|
104 { |
|
105 return EFalse; |
|
106 } |
|
107 |
|
108 } |
|
109 |
|
110 // ----------------------------------------------------------------------------- |
|
111 // CExprTCPMsg::TryParsingL |
|
112 // ----------------------------------------------------------------------------- |
|
113 // |
|
114 TInt CExprTCPMsg::TryParsingL( TDes8& aData, TInt& aLength ) |
|
115 { |
|
116 __ASSERT_ALWAYS( aData.Left( KTCPPrefix().Length() ) == KTCPPrefix, |
|
117 User::Panic( _L("Protocol"), 1 ) ); |
|
118 |
|
119 // TCP:0123,000e,[Some test data] |
|
120 TInt frameOverhead = |
|
121 KTCPPrefix().Length() + |
|
122 KHexDecimalLength + |
|
123 KPortSuffix().Length() + |
|
124 KHexDecimalLength + |
|
125 KLengthSuffix().Length() + |
|
126 KDataSuffix().Length() + |
|
127 KMessageSuffix().Length(); |
|
128 |
|
129 if ( aData.Length() >= frameOverhead ) |
|
130 { |
|
131 TPtrC8 portPtr( |
|
132 aData.Mid( KTCPPrefix().Length(), KHexDecimalLength ) ); |
|
133 |
|
134 TLex8 portLexer( portPtr ); |
|
135 TUint port; |
|
136 if ( portLexer.Val( port, EHex ) != KErrNone ) |
|
137 { |
|
138 return KErrCorrupt; |
|
139 } |
|
140 DEBUG_PRINT( DEBUG_STRING( "CExprTCPMsg::TryParsingL, port = %d" ), port ); |
|
141 |
|
142 //Check port suffix |
|
143 if ( aData.Mid( KTCPPrefix().Length() + |
|
144 KHexDecimalLength, KPortSuffix().Length() ) != KPortSuffix ) |
|
145 { |
|
146 return KErrCorrupt; |
|
147 } |
|
148 |
|
149 TPtrC8 lengthPtr( aData.Mid( KTCPPrefix().Length() + |
|
150 KHexDecimalLength + KPortSuffix().Length(), KHexDecimalLength ) ); |
|
151 TLex8 lengthLexer( lengthPtr ); |
|
152 TUint length; |
|
153 if ( lengthLexer.Val( length, EHex ) != KErrNone ) |
|
154 { |
|
155 return KErrCorrupt; |
|
156 } |
|
157 DEBUG_PRINT( DEBUG_STRING( "CExprTCPMsg::TryParsingL, length = %d" ), length ); |
|
158 |
|
159 //Check length suffix |
|
160 if ( aData.Mid( |
|
161 KTCPPrefix().Length() + |
|
162 KHexDecimalLength + |
|
163 KPortSuffix().Length() + |
|
164 KHexDecimalLength, KLengthSuffix().Length() ) != KLengthSuffix ) |
|
165 { |
|
166 return KErrCorrupt; |
|
167 } |
|
168 |
|
169 if ( aData.Length() >= TInt( frameOverhead + length ) ) |
|
170 { |
|
171 TInt messagePos = KTCPPrefix().Length() + |
|
172 KHexDecimalLength + |
|
173 KPortSuffix().Length() + |
|
174 KHexDecimalLength + |
|
175 KLengthSuffix().Length(); |
|
176 |
|
177 TPtrC8 message( aData.Mid( messagePos, length ) ); |
|
178 if ( aData.Mid( messagePos + length, |
|
179 KDataSuffix().Length() ) != KDataSuffix ) |
|
180 { |
|
181 return KErrCorrupt; |
|
182 } |
|
183 DEBUG_PRINT( DEBUG_STRING( "CExprTCPMsg::TryParsingL, message OK" ) ); |
|
184 |
|
185 if ( aData.Mid( messagePos + length + KDataSuffix().Length(), |
|
186 KMessageSuffix().Length() ) != KMessageSuffix ) |
|
187 { |
|
188 return KErrCorrupt; |
|
189 } |
|
190 |
|
191 // send parsed results |
|
192 iObserver->FrameParsedL( port, message ); |
|
193 // set the length of the handled message |
|
194 aLength = frameOverhead + length; |
|
195 |
|
196 return KErrNone; |
|
197 } |
|
198 |
|
199 } |
|
200 return KErrNone; |
|
201 } |
|
202 |
|
203 // End of File |