|
1 // Copyright (c) 2005-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 // Name : COwnerSettings.cpp |
|
15 // Part of : ConnectionMgr |
|
16 // See class definition below. |
|
17 // Version : SIP/4.0 |
|
18 // |
|
19 |
|
20 |
|
21 |
|
22 #include <in_sock.h> |
|
23 |
|
24 #include "COwnerSettings.h" |
|
25 #include "CSocketOpt.h" |
|
26 #include "CommonConsts.h" |
|
27 |
|
28 const TInt KSocketOptsGranularity = 2; |
|
29 const TInt KPortsGranularity = 2; |
|
30 |
|
31 // ----------------------------------------------------------------------------- |
|
32 // COwnerSettings::NewLC |
|
33 // ----------------------------------------------------------------------------- |
|
34 // |
|
35 COwnerSettings* COwnerSettings::NewLC( const TSIPTransportParams& aParams ) |
|
36 { |
|
37 COwnerSettings* self = new ( ELeave ) COwnerSettings( aParams ); |
|
38 CleanupStack::PushL( self ); |
|
39 return self; |
|
40 } |
|
41 |
|
42 // ----------------------------------------------------------------------------- |
|
43 // COwnerSettings::~COwnerSettings |
|
44 // ----------------------------------------------------------------------------- |
|
45 // |
|
46 COwnerSettings::~COwnerSettings() |
|
47 { |
|
48 delete iRemoteAddr; |
|
49 iSocketOpts.ResetAndDestroy(); |
|
50 iOriginalOpts.ResetAndDestroy(); |
|
51 iPorts.Close(); |
|
52 } |
|
53 |
|
54 // ----------------------------------------------------------------------------- |
|
55 // COwnerSettings::COwnerSettings |
|
56 // ----------------------------------------------------------------------------- |
|
57 // |
|
58 COwnerSettings::COwnerSettings( const TSIPTransportParams& aParams ) : |
|
59 iSocketOpts( KSocketOptsGranularity ), |
|
60 iPorts( KPortsGranularity ), |
|
61 iParams( aParams ), |
|
62 iUseTLS( EFalse ) |
|
63 { |
|
64 } |
|
65 |
|
66 // ----------------------------------------------------------------------------- |
|
67 // COwnerSettings::AddOptL |
|
68 // ----------------------------------------------------------------------------- |
|
69 // |
|
70 void COwnerSettings::AddOptL( |
|
71 TUint aOptionName, |
|
72 TUint aOptionLevel, |
|
73 TInt aOption ) |
|
74 { |
|
75 CSocketOpt* sockopt = |
|
76 CSocketOpt::NewLC( aOptionName, aOptionLevel, aOption ); |
|
77 AddOptL( sockopt ); |
|
78 CleanupStack::Pop( sockopt ); |
|
79 } |
|
80 |
|
81 // ----------------------------------------------------------------------------- |
|
82 // COwnerSettings::AddOptL |
|
83 // ----------------------------------------------------------------------------- |
|
84 // |
|
85 void COwnerSettings::AddOptL( |
|
86 TUint aOptionName, |
|
87 TUint aOptionLevel, |
|
88 const TDesC8& aOption ) |
|
89 { |
|
90 CSocketOpt* sockopt = |
|
91 CSocketOpt::NewLC( aOptionName, aOptionLevel, aOption ); |
|
92 AddOptL( sockopt ); |
|
93 CleanupStack::Pop( sockopt ); |
|
94 } |
|
95 |
|
96 // ----------------------------------------------------------------------------- |
|
97 // COwnerSettings::TransportParams |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 const TSIPTransportParams& COwnerSettings::TransportParams() const |
|
101 { |
|
102 return iParams; |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // COwnerSettings::SetOpts |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 void COwnerSettings::SetOpts( RSocket& aSocket ) |
|
110 { |
|
111 // Clear original opts, those will be re-created while setting new options |
|
112 iOriginalOpts.ResetAndDestroy(); |
|
113 |
|
114 TRAP_IGNORE( SetOptsL( aSocket ) ) |
|
115 } |
|
116 |
|
117 // ----------------------------------------------------------------------------- |
|
118 // COwnerSettings::ClearOpts |
|
119 // ----------------------------------------------------------------------------- |
|
120 // |
|
121 void COwnerSettings::ClearOpts( RSocket& aSocket ) |
|
122 { |
|
123 TRAP_IGNORE( ClearOptsL( aSocket ) ) |
|
124 } |
|
125 |
|
126 // ----------------------------------------------------------------------------- |
|
127 // COwnerSettings::RememberPortsL |
|
128 // ----------------------------------------------------------------------------- |
|
129 // |
|
130 void COwnerSettings::SaveTransportInfoL( |
|
131 TUint aLocalSendPort, |
|
132 TUint aLocalReceivePort, |
|
133 const TInetAddr& aRemoteAddr, |
|
134 TUint aRemoteSendPort ) |
|
135 { |
|
136 __ASSERT_ALWAYS( !iLocalSendPort && |
|
137 !iLocalReceivePort && |
|
138 !iRemoteAddr && |
|
139 !iRemoteSendPort, |
|
140 User::Leave( KErrAlreadyExists ) ); |
|
141 |
|
142 iRemoteAddr = new ( ELeave ) TInetAddr( aRemoteAddr ); |
|
143 iLocalSendPort = aLocalSendPort; |
|
144 iLocalReceivePort = aLocalReceivePort; |
|
145 iRemoteSendPort = aRemoteSendPort; |
|
146 } |
|
147 |
|
148 // ----------------------------------------------------------------------------- |
|
149 // COwnerSettings::AddReservedPortL |
|
150 // ----------------------------------------------------------------------------- |
|
151 // |
|
152 void COwnerSettings::AddReservedPortL( TUint aPort ) |
|
153 { |
|
154 // Don't allow duplicates in the array, however in such situation no need |
|
155 // to leave |
|
156 TInt err = iPorts.InsertInOrder( aPort ); |
|
157 if ( err && err != KErrAlreadyExists ) |
|
158 { |
|
159 User::Leave( err ); |
|
160 } |
|
161 } |
|
162 |
|
163 // ----------------------------------------------------------------------------- |
|
164 // COwnerSettings::IsUnreservedPort |
|
165 // ----------------------------------------------------------------------------- |
|
166 // |
|
167 TBool COwnerSettings::IsUnreservedPort( TUint aPort ) const |
|
168 { |
|
169 TInt count = iPorts.Count(); |
|
170 for ( TInt i = 0; i < count; i++ ) |
|
171 { |
|
172 if ( iPorts[ i ] == aPort ) |
|
173 { |
|
174 return EFalse; |
|
175 } |
|
176 } |
|
177 return ETrue; |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------------- |
|
181 // COwnerSettings::IsOpts |
|
182 // ----------------------------------------------------------------------------- |
|
183 // |
|
184 TBool COwnerSettings::IsOpts() const |
|
185 { |
|
186 return ( iSocketOpts.Count() > 0 ); |
|
187 } |
|
188 |
|
189 // ----------------------------------------------------------------------------- |
|
190 // COwnerSettings::LocalSendPort |
|
191 // ----------------------------------------------------------------------------- |
|
192 // |
|
193 TUint COwnerSettings::LocalSendPort() const |
|
194 { |
|
195 return iLocalSendPort; |
|
196 } |
|
197 |
|
198 // ----------------------------------------------------------------------------- |
|
199 // COwnerSettings::LocalReceivePort |
|
200 // ----------------------------------------------------------------------------- |
|
201 // |
|
202 TUint COwnerSettings::LocalReceivePort() const |
|
203 { |
|
204 return iLocalReceivePort; |
|
205 } |
|
206 |
|
207 // ----------------------------------------------------------------------------- |
|
208 // COwnerSettings::RemoteAddr |
|
209 // ----------------------------------------------------------------------------- |
|
210 // |
|
211 const TInetAddr* COwnerSettings::RemoteAddr() const |
|
212 { |
|
213 return iRemoteAddr; |
|
214 } |
|
215 |
|
216 // ----------------------------------------------------------------------------- |
|
217 // COwnerSettings::IsMatchingWithSavedInfo |
|
218 // ----------------------------------------------------------------------------- |
|
219 // |
|
220 TBool COwnerSettings::IsMatchingWithSavedInfo( |
|
221 const TSIPTransportParams& aParams, |
|
222 const TInetAddr& aRemoteAddr, |
|
223 TUint aProtocol, |
|
224 TUint aLocalPort ) const |
|
225 { |
|
226 TUint32 transportId( aParams.TransportId() ); |
|
227 TBool isReservedTransport( transportId > 0 ); |
|
228 |
|
229 TBool addressMatch( EFalse ); |
|
230 |
|
231 if ( aProtocol == KProtocolInetUdp && isReservedTransport ) |
|
232 { |
|
233 // In case of protected UDP, check only remote send and local receive |
|
234 // ports |
|
235 addressMatch = ( IsMatchingWithRemoteSendAddr( aRemoteAddr, ETrue ) && |
|
236 iLocalReceivePort == aLocalPort ); |
|
237 } |
|
238 else |
|
239 { |
|
240 // Otherwise check both, remote and local, send and receive ports |
|
241 addressMatch = ( IsMatchingWithRemoteSendAddr( aRemoteAddr, EFalse ) && |
|
242 ( iLocalReceivePort == aLocalPort || |
|
243 iLocalSendPort == aLocalPort ) ); |
|
244 } |
|
245 return ( addressMatch && |
|
246 ( !isReservedTransport || iParams.TransportId() == transportId ) ); |
|
247 } |
|
248 |
|
249 // ----------------------------------------------------------------------------- |
|
250 // COwnerSettings::IsMatchingWithRemoteSendAddr |
|
251 // ----------------------------------------------------------------------------- |
|
252 // |
|
253 TBool COwnerSettings::IsMatchingWithRemoteSendAddr( |
|
254 const TInetAddr& aRemoteSendAddr, |
|
255 TBool aCheckOnlySendPort ) const |
|
256 { |
|
257 if ( !iRemoteAddr ) |
|
258 { |
|
259 return EFalse; |
|
260 } |
|
261 |
|
262 TBool portMatch = ( iRemoteSendPort == aRemoteSendAddr.Port() ); |
|
263 if ( !aCheckOnlySendPort && !portMatch ) |
|
264 { |
|
265 portMatch = ( iRemoteAddr->CmpPort( aRemoteSendAddr ) ); |
|
266 } |
|
267 |
|
268 return ( iRemoteAddr->Match( aRemoteSendAddr ) && portMatch ); |
|
269 } |
|
270 |
|
271 // ----------------------------------------------------------------------------- |
|
272 // COwnerSettings::SetUseTLS |
|
273 // ----------------------------------------------------------------------------- |
|
274 // |
|
275 void COwnerSettings::SetUseTLS( TBool aUseTLS ) |
|
276 { |
|
277 iUseTLS = aUseTLS; |
|
278 } |
|
279 |
|
280 // ----------------------------------------------------------------------------- |
|
281 // COwnerSettings::UseTLS |
|
282 // ----------------------------------------------------------------------------- |
|
283 // |
|
284 TBool COwnerSettings::UseTLS() const |
|
285 { |
|
286 return iUseTLS; |
|
287 } |
|
288 |
|
289 // ----------------------------------------------------------------------------- |
|
290 // COwnerSettings::SaveTLSRemoteL |
|
291 // ----------------------------------------------------------------------------- |
|
292 // |
|
293 void COwnerSettings::SaveTLSRemoteL( const TInetAddr& aRemoteAddr ) |
|
294 { |
|
295 __ASSERT_ALWAYS( iUseTLS, User::Leave( KErrNotReady ) ); |
|
296 |
|
297 TInetAddr* remoteAddr = new ( ELeave ) TInetAddr( aRemoteAddr ); |
|
298 delete iRemoteAddr; |
|
299 iRemoteAddr = remoteAddr; |
|
300 |
|
301 iRemoteAddr->SetPort( KDefaultSipsPort ); |
|
302 } |
|
303 |
|
304 // ----------------------------------------------------------------------------- |
|
305 // COwnerSettings::AddOptL |
|
306 // ----------------------------------------------------------------------------- |
|
307 // |
|
308 void COwnerSettings::AddOptL( CSocketOpt* aSocketOpt ) |
|
309 { |
|
310 __ASSERT_ALWAYS( aSocketOpt, User::Leave( KErrArgument ) ); |
|
311 // Don't allow multiple socket opts with same option name |
|
312 for ( TInt i = 0; i < iSocketOpts.Count(); i++ ) |
|
313 { |
|
314 if ( iSocketOpts[ i ]->OptionName() == aSocketOpt->OptionName() ) |
|
315 { |
|
316 delete iSocketOpts[ i ]; |
|
317 iSocketOpts.Remove( i ); |
|
318 iSocketOpts.InsertL( aSocketOpt, i ); |
|
319 return; |
|
320 } |
|
321 } |
|
322 iSocketOpts.AppendL( aSocketOpt ); |
|
323 } |
|
324 |
|
325 // ----------------------------------------------------------------------------- |
|
326 // COwnerSettings::SetOptsL |
|
327 // ----------------------------------------------------------------------------- |
|
328 // |
|
329 void COwnerSettings::SetOptsL( RSocket& aSocket ) |
|
330 { |
|
331 TInt count = iSocketOpts.Count(); |
|
332 for ( TInt i = 0; i < count; i++ ) |
|
333 { |
|
334 // Store previous opt as it will be used when ClearOpts is called |
|
335 CSocketOpt* previousOpt = iSocketOpts[ i ]->SetOptL( aSocket ); |
|
336 if ( previousOpt ) |
|
337 { |
|
338 CleanupStack::PushL( previousOpt ); |
|
339 iOriginalOpts.AppendL( previousOpt ); |
|
340 CleanupStack::Pop( previousOpt ); |
|
341 } |
|
342 } |
|
343 } |
|
344 |
|
345 // ----------------------------------------------------------------------------- |
|
346 // COwnerSettings::ClearOptsL |
|
347 // ----------------------------------------------------------------------------- |
|
348 // |
|
349 void COwnerSettings::ClearOptsL( RSocket& aSocket ) |
|
350 { |
|
351 TInt count = iOriginalOpts.Count(); |
|
352 for ( TInt i = 0; i < count; i++ ) |
|
353 { |
|
354 CSocketOpt* previousOpt = iOriginalOpts[ i ]->SetOptL( aSocket ); |
|
355 // Not interested about previous value |
|
356 delete previousOpt; |
|
357 } |
|
358 } |
|
359 |
|
360 // end of file |