|
1 /* |
|
2 * Copyright (c) 2007 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 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "cnattraversalsession.h" |
|
20 #include "cnattraversalconfig.h" |
|
21 #include "cnatbinding.h" |
|
22 #include "cnatbindingstunrefresher.h" |
|
23 #include "cnatbindingcrlfrefresher.h" |
|
24 #include "cnatbindingstunandcrlfrefresher.h" |
|
25 #include "cudpcrlfsender.h" |
|
26 #include "ctcpcrlfsender.h" |
|
27 #include "ctlscrlfsender.h" |
|
28 #include "sipnatbindingobserver.h" |
|
29 #include "nattraversallog.h" |
|
30 |
|
31 _LIT8(KZeroIPAddress, "0.0.0.0"); |
|
32 |
|
33 // The following addresses are private (RFC 1918): |
|
34 // 10.0.0.0 - 10.255.255.255 |
|
35 const TUint32 KPrivateRange1Low = INET_ADDR(10,0,0,0); |
|
36 const TUint32 KPrivateRange1High = INET_ADDR(10,255,255,255); |
|
37 |
|
38 // 172.16.0.0 - 172.31.255.255 |
|
39 const TUint32 KPrivateRange2Low = INET_ADDR(172,16,0,0); |
|
40 const TUint32 KPrivateRange2High = INET_ADDR(172,31,255,255); |
|
41 |
|
42 // 192.168.0.0 - 192.168.255.255 |
|
43 const TUint32 KPrivateRange3Low = INET_ADDR(192,168,0,0); |
|
44 const TUint32 KPrivateRange3High = INET_ADDR(192,168,255,255); |
|
45 |
|
46 |
|
47 // ----------------------------------------------------------------------------- |
|
48 // CNATTraversalSession::NewL |
|
49 // ----------------------------------------------------------------------------- |
|
50 // |
|
51 CNATTraversalSession* CNATTraversalSession::NewL( |
|
52 CDeltaTimer& aDeltaTimer, |
|
53 RSocketServ& aSocketServ, |
|
54 RConnection& aConnection, |
|
55 const TInetAddr& aLocalAddr, |
|
56 const TInetAddr& aNextHopAddr, |
|
57 CNATTraversalConfig* aConfig, |
|
58 MSTUNClientObserver& aObserver, |
|
59 MNATTraversalSocketManager& aSocketManager) |
|
60 { |
|
61 CNATTraversalSession* self = |
|
62 CNATTraversalSession::NewLC(aDeltaTimer,aSocketServ,aConnection, |
|
63 aLocalAddr,aNextHopAddr,aConfig, |
|
64 aObserver,aSocketManager); |
|
65 CleanupStack::Pop(self); |
|
66 return self; |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // CNATTraversalSession::NewL |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 CNATTraversalSession* CNATTraversalSession::NewLC( |
|
74 CDeltaTimer& aDeltaTimer, |
|
75 RSocketServ& aSocketServ, |
|
76 RConnection& aConnection, |
|
77 const TInetAddr& aLocalAddr, |
|
78 const TInetAddr& aNextHopAddr, |
|
79 CNATTraversalConfig* aConfig, |
|
80 MSTUNClientObserver& aObserver, |
|
81 MNATTraversalSocketManager& aSocketManager) |
|
82 { |
|
83 CNATTraversalSession* self = |
|
84 new(ELeave)CNATTraversalSession( |
|
85 aLocalAddr,aNextHopAddr,aDeltaTimer,aSocketManager); |
|
86 CleanupStack::PushL(self); |
|
87 self->ConstructL(aSocketServ,aConnection,aConfig,aObserver); |
|
88 return self; |
|
89 } |
|
90 |
|
91 // ----------------------------------------------------------------------------- |
|
92 // CNATTraversalSession::CNATTraversalSession |
|
93 // ----------------------------------------------------------------------------- |
|
94 // |
|
95 CNATTraversalSession::CNATTraversalSession( |
|
96 const TInetAddr& aLocalAddr, |
|
97 const TInetAddr& aNextHopAddr, |
|
98 CDeltaTimer& aDeltaTimer, |
|
99 MNATTraversalSocketManager& aSocketManager) |
|
100 : iLocalAddr(aLocalAddr), |
|
101 iNextHopAddr(aNextHopAddr), |
|
102 iDeltaTimer(aDeltaTimer), |
|
103 iSocketManager(aSocketManager) |
|
104 { |
|
105 } |
|
106 |
|
107 // ----------------------------------------------------------------------------- |
|
108 // CNATTraversalSession::ConstructL |
|
109 // ----------------------------------------------------------------------------- |
|
110 // |
|
111 void CNATTraversalSession::ConstructL( |
|
112 RSocketServ& aSocketServ, |
|
113 RConnection& aConnection, |
|
114 CNATTraversalConfig* aConfig, |
|
115 MSTUNClientObserver& aObserver) |
|
116 { |
|
117 __ASSERT_ALWAYS(aConfig, User::Leave(KErrArgument)); |
|
118 |
|
119 if (aConfig->STUNServer().Compare(KZeroIPAddress) == 0) |
|
120 { |
|
121 // With 0.0.0.0 STUN server address, STUN is not used. |
|
122 // With this configuration CRLF bursts are sent |
|
123 // to keep the possible firewall hole open. |
|
124 __ASSERT_ALWAYS(aConfig->CRLFRefreshEnabledToProxyValue() == |
|
125 CNATTraversalConfig::EEnabled, |
|
126 User::Leave(KErrArgument)); |
|
127 } |
|
128 else |
|
129 { |
|
130 TBool stunServerConfigured = ETrue; |
|
131 TPtrC8 stunServerPtr(aConfig->STUNServer()); |
|
132 if (stunServerPtr.Length() == 0) |
|
133 { |
|
134 stunServerPtr.Set(aConfig->Domain()); |
|
135 stunServerConfigured = EFalse; |
|
136 } |
|
137 |
|
138 iSTUNClient = CSTUNClient::NewL(aConfig->STUNRetransmitTimer(), |
|
139 stunServerPtr, |
|
140 aConfig->STUNServerPort(), |
|
141 KStun, |
|
142 aSocketServ, |
|
143 aConnection, |
|
144 iDeltaTimer, |
|
145 aObserver, |
|
146 ETrue, |
|
147 !stunServerConfigured); |
|
148 |
|
149 if (aConfig->Username().Length() && aConfig->Password().Length()) |
|
150 { |
|
151 iSTUNClient->SetCredentialsL(aConfig->Username(), |
|
152 aConfig->Password()); |
|
153 } |
|
154 } |
|
155 |
|
156 // transfer the ownership only after calling all the leaving functions |
|
157 iConfig = aConfig; |
|
158 } |
|
159 |
|
160 // ----------------------------------------------------------------------------- |
|
161 // CNATTraversalSession::~CNATTraversalSession |
|
162 // ----------------------------------------------------------------------------- |
|
163 // |
|
164 CNATTraversalSession::~CNATTraversalSession() |
|
165 { |
|
166 delete iSTUNClient; |
|
167 delete iConfig; |
|
168 iBindings.ResetAndDestroy(); |
|
169 } |
|
170 |
|
171 // ----------------------------------------------------------------------------- |
|
172 // CNATTraversalSession::HasSTUNClient |
|
173 // ----------------------------------------------------------------------------- |
|
174 // |
|
175 TBool CNATTraversalSession::HasSTUNClient(const CSTUNClient& aClient) const |
|
176 { |
|
177 return (iSTUNClient == &aClient); |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------------- |
|
181 // CNATTraversalSession::Domain |
|
182 // ----------------------------------------------------------------------------- |
|
183 // |
|
184 const TDesC8& CNATTraversalSession::Domain() const |
|
185 { |
|
186 return iConfig->Domain(); |
|
187 } |
|
188 |
|
189 // ----------------------------------------------------------------------------- |
|
190 // CNATTraversalSession::IapId |
|
191 // ----------------------------------------------------------------------------- |
|
192 // |
|
193 TUint32 CNATTraversalSession::IapId() const |
|
194 { |
|
195 return iConfig->IapId(); |
|
196 } |
|
197 |
|
198 // ----------------------------------------------------------------------------- |
|
199 // CNATTraversalSession::UpdateNextHop |
|
200 // ----------------------------------------------------------------------------- |
|
201 // |
|
202 void CNATTraversalSession::UpdateNextHop(const TInetAddr& aNextHopAddr) |
|
203 { |
|
204 iNextHopAddr = aNextHopAddr; |
|
205 } |
|
206 |
|
207 // ----------------------------------------------------------------------------- |
|
208 // CNATTraversalSession::HasBindings |
|
209 // ----------------------------------------------------------------------------- |
|
210 // |
|
211 TBool CNATTraversalSession::HasBindings() const |
|
212 { |
|
213 return (iBindings.Count() > 0); |
|
214 } |
|
215 |
|
216 // ----------------------------------------------------------------------------- |
|
217 // CNATTraversalSession::HasBinding |
|
218 // ----------------------------------------------------------------------------- |
|
219 // |
|
220 TBool CNATTraversalSession::HasBinding( |
|
221 const MSIPNATBindingObserver* aObserver) const |
|
222 { |
|
223 return (FindBindingIndex(aObserver) >= 0); |
|
224 } |
|
225 |
|
226 // ----------------------------------------------------------------------------- |
|
227 // CNATTraversalSession::InitCompletedL |
|
228 // ----------------------------------------------------------------------------- |
|
229 // |
|
230 void CNATTraversalSession::InitCompletedL() |
|
231 { |
|
232 NATTRAVERSAL_LOG("CNATTraversalSession::InitCompletedL") |
|
233 __ASSERT_ALWAYS(iSTUNClient, User::Leave(KErrCorrupt)); |
|
234 iConfig->SetSharedSecretNotSupportedL(!iSTUNClient->SharedSecretObtained()); |
|
235 for (TInt i=0; i<iBindings.Count(); i++) |
|
236 { |
|
237 CNATBinding* bindingHolder = iBindings[i]; |
|
238 CSTUNBinding* stunBinding = |
|
239 CSTUNBinding::NewL(*iSTUNClient,bindingHolder->STUNSocket()); |
|
240 bindingHolder->SetBinding(stunBinding); |
|
241 bindingHolder->SendSTUNRequestL(); |
|
242 } |
|
243 } |
|
244 |
|
245 // ----------------------------------------------------------------------------- |
|
246 // CNATTraversalSession::InitFailed |
|
247 // ----------------------------------------------------------------------------- |
|
248 // |
|
249 void CNATTraversalSession::InitFailed(TInt aError) |
|
250 { |
|
251 NATTRAVERSAL_LOG("CNATTraversalSession::InitFailedL") |
|
252 TRAP_IGNORE(iConfig->SetBindingRequestFailedL(ETrue)) |
|
253 for (TInt i=iBindings.Count()-1; i>=0; i--) |
|
254 { |
|
255 iBindings[i]->SetBindingFailed(aError); |
|
256 } |
|
257 } |
|
258 |
|
259 // ----------------------------------------------------------------------------- |
|
260 // CNATTraversalSession::AddressResolvedL |
|
261 // ----------------------------------------------------------------------------- |
|
262 // |
|
263 TBool CNATTraversalSession::AddressResolvedL(const CBinding& aBinding) |
|
264 { |
|
265 NATTRAVERSAL_ADDRLOG("CNATTraversalSession::AddressResolvedL", |
|
266 aBinding.PublicAddr()) |
|
267 TBool found = EFalse; |
|
268 for (TInt i=iBindings.Count()-1; i>=0 && !found; i--) |
|
269 { |
|
270 CNATBinding* bindingHolder = iBindings[i]; |
|
271 if (bindingHolder->Binding() == &aBinding) |
|
272 { |
|
273 found = ETrue; |
|
274 TBool publicAddrChanged = |
|
275 bindingHolder->SetPublicAddr(aBinding.PublicAddr()); |
|
276 if (publicAddrChanged) |
|
277 { |
|
278 // Existing public address has changed -> flow failure |
|
279 iBindings.Remove(i); |
|
280 bindingHolder->NATBindingObserver().FlowFailure(KErrGeneral); |
|
281 delete bindingHolder; |
|
282 } |
|
283 else |
|
284 { |
|
285 bindingHolder->ContinueRefreshing(); |
|
286 } |
|
287 } |
|
288 } |
|
289 return found; |
|
290 } |
|
291 |
|
292 // ----------------------------------------------------------------------------- |
|
293 // CNATTraversalSession::BindingFailed |
|
294 // ----------------------------------------------------------------------------- |
|
295 // |
|
296 TBool CNATTraversalSession::BindingFailed( |
|
297 const CBinding& aBinding, |
|
298 TInt aError) |
|
299 { |
|
300 TBool found = EFalse; |
|
301 for (TInt i=iBindings.Count()-1; i>=0 && !found; i--) |
|
302 { |
|
303 CNATBinding* bindingHolder = iBindings[i]; |
|
304 if (bindingHolder->Binding() == &aBinding) |
|
305 { |
|
306 found = ETrue; |
|
307 TRAP_IGNORE(iConfig->SetBindingRequestFailedL(ETrue)) |
|
308 bindingHolder->SetBindingFailed(aError); |
|
309 } |
|
310 } |
|
311 return found; |
|
312 } |
|
313 |
|
314 // ----------------------------------------------------------------------------- |
|
315 // CNATTraversalSession::CreateNATBindingL |
|
316 // ----------------------------------------------------------------------------- |
|
317 // |
|
318 CNATBinding& CNATTraversalSession::CreateNATBindingL( |
|
319 TUint32 aRequestId, |
|
320 RSocket& aSocket, |
|
321 MSIPNATBindingObserver& aBindingObserver, |
|
322 MSIPNATTraversalRequestObserver& aRequestObserver, |
|
323 TBool& aAsyncCallInitiated) |
|
324 { |
|
325 aAsyncCallInitiated = EFalse; |
|
326 // Initialize binding's public address with the local address |
|
327 CNATBinding* bindingHolder = |
|
328 CNATBinding::NewLC(aRequestId,aSocket,iLocalAddr, |
|
329 aRequestObserver,aBindingObserver, |
|
330 iSocketManager); |
|
331 |
|
332 if (iSTUNClient) |
|
333 { |
|
334 // STUN used, not only CRLF-refreshes. |
|
335 if (iSTUNClient->IsInitialized()) |
|
336 { |
|
337 if (!iConfig->BindingRequestFailed()) |
|
338 { |
|
339 CSTUNBinding* stunBinding = |
|
340 CSTUNBinding::NewL( |
|
341 *iSTUNClient,bindingHolder->STUNSocket()); |
|
342 bindingHolder->SetBinding(stunBinding); |
|
343 bindingHolder->SendSTUNRequestL(); |
|
344 aAsyncCallInitiated = ETrue; |
|
345 } |
|
346 } |
|
347 else |
|
348 { |
|
349 // STUN Client is not initialized. |
|
350 aAsyncCallInitiated = (!iConfig->BindingRequestFailed()); |
|
351 } |
|
352 } |
|
353 |
|
354 iBindings.AppendL(bindingHolder); |
|
355 CleanupStack::Pop(bindingHolder); |
|
356 return *bindingHolder; |
|
357 } |
|
358 |
|
359 // ----------------------------------------------------------------------------- |
|
360 // CNATTraversalSession::RefreshNATBindingL |
|
361 // ----------------------------------------------------------------------------- |
|
362 // |
|
363 void CNATTraversalSession::RefreshNATBindingL( |
|
364 RSocket& aSocket, |
|
365 const MSIPNATBindingObserver* aBindingObserver, |
|
366 TBool& aMaintainPersistentConnection, |
|
367 TBool& aHandled) |
|
368 { |
|
369 aMaintainPersistentConnection = EFalse; |
|
370 aHandled = EFalse; |
|
371 TInt index = FindBindingIndex(aBindingObserver); |
|
372 if (index >= 0) |
|
373 { |
|
374 CNATBinding* binding = iBindings[index]; |
|
375 if (!binding->Refresher() && |
|
376 (IsBehindNAT(*binding) || |
|
377 iConfig->CRLFRefreshEnabledToProxy(Protocol(aSocket)))) |
|
378 { |
|
379 CreateRefresherL(*binding,aSocket,aMaintainPersistentConnection); |
|
380 } |
|
381 aMaintainPersistentConnection = binding->PersistentConnectionRequired(); |
|
382 aHandled = ETrue; |
|
383 } |
|
384 } |
|
385 |
|
386 // ----------------------------------------------------------------------------- |
|
387 // CNATTraversalSession::RefreshNATBindingL |
|
388 // ----------------------------------------------------------------------------- |
|
389 // |
|
390 void CNATTraversalSession::RefreshNATBindingL( |
|
391 CSecureSocket& aSecureSocket, |
|
392 const MSIPNATBindingObserver* aBindingObserver, |
|
393 TBool& aMaintainPersistentConnection, |
|
394 TBool& aHandled) |
|
395 { |
|
396 aMaintainPersistentConnection = EFalse; |
|
397 aHandled = EFalse; |
|
398 TInt index = FindBindingIndex(aBindingObserver); |
|
399 if (index >= 0) |
|
400 { |
|
401 CNATBinding* binding = iBindings[index]; |
|
402 if (!binding->Refresher() && |
|
403 (IsBehindNAT(*binding) || |
|
404 iConfig->CRLFRefreshEnabledToProxy(KProtocolInetTcp))) |
|
405 { |
|
406 CTlsCRLFSender* sender = |
|
407 new(ELeave)CTlsCRLFSender(aSecureSocket,*binding); |
|
408 CleanupStack::PushL(sender); |
|
409 CNATBindingRefresher* refresher = |
|
410 new(ELeave)CNATBindingCRLFRefresher( |
|
411 iDeltaTimer, |
|
412 iConfig->TCPRefreshInterval(), |
|
413 *binding, |
|
414 sender); |
|
415 CleanupStack::Pop(sender); |
|
416 binding->SetRefresher(refresher); |
|
417 } |
|
418 aMaintainPersistentConnection = binding->PersistentConnectionRequired(); |
|
419 aHandled = ETrue; |
|
420 } |
|
421 } |
|
422 |
|
423 // ----------------------------------------------------------------------------- |
|
424 // CNATTraversalSession::SocketIdle |
|
425 // ----------------------------------------------------------------------------- |
|
426 // |
|
427 void CNATTraversalSession::SocketIdle( |
|
428 TBool aIdle, |
|
429 RSocket& aSocket) |
|
430 { |
|
431 SocketIdleImpl(aIdle,aSocket); |
|
432 } |
|
433 |
|
434 // ----------------------------------------------------------------------------- |
|
435 // CNATTraversalSession::SocketIdle |
|
436 // ----------------------------------------------------------------------------- |
|
437 // |
|
438 void CNATTraversalSession::SocketIdle( |
|
439 TBool aIdle, |
|
440 CSecureSocket& aSocket) |
|
441 { |
|
442 SocketIdleImpl(aIdle,aSocket); |
|
443 } |
|
444 |
|
445 // ----------------------------------------------------------------------------- |
|
446 // CNATTraversalSession::DataReceivedL |
|
447 // ----------------------------------------------------------------------------- |
|
448 // |
|
449 void CNATTraversalSession::DataReceivedL( |
|
450 const TDesC8& aData, |
|
451 const RSocket& aSocket, |
|
452 TBool& aHandled) |
|
453 { |
|
454 for (TInt i=0; i<iBindings.Count() && !aHandled; i++) |
|
455 { |
|
456 iBindings[i]->HandleDataL(aData,aSocket,aHandled); |
|
457 } |
|
458 } |
|
459 |
|
460 // ----------------------------------------------------------------------------- |
|
461 // CNATTraversalSession::RemoveBinding |
|
462 // ----------------------------------------------------------------------------- |
|
463 // |
|
464 TBool CNATTraversalSession::RemoveBinding(TUint32 aRequestId) |
|
465 { |
|
466 TBool found = EFalse; |
|
467 for (TInt i=0; i<iBindings.Count() && !found; i++) |
|
468 { |
|
469 CNATBinding* binding = iBindings[i]; |
|
470 if (binding->InitialRequestId() == aRequestId) |
|
471 { |
|
472 iBindings.Remove(i); |
|
473 delete binding; |
|
474 found = ETrue; |
|
475 } |
|
476 } |
|
477 return found; |
|
478 } |
|
479 |
|
480 // ----------------------------------------------------------------------------- |
|
481 // CNATTraversalSession::RemoveBinding |
|
482 // ----------------------------------------------------------------------------- |
|
483 // |
|
484 TBool CNATTraversalSession::RemoveBinding( |
|
485 const MSIPNATBindingObserver& aObserver) |
|
486 { |
|
487 TBool found = EFalse; |
|
488 TInt index = FindBindingIndex(&aObserver); |
|
489 if (index >= 0) |
|
490 { |
|
491 CNATBinding* binding = iBindings[index]; |
|
492 iBindings.Remove(index); |
|
493 delete binding; |
|
494 found = ETrue; |
|
495 } |
|
496 return found; |
|
497 } |
|
498 |
|
499 // ----------------------------------------------------------------------------- |
|
500 // CNATTraversalSession::DetachConfig |
|
501 // ----------------------------------------------------------------------------- |
|
502 // |
|
503 void CNATTraversalSession::DetachConfig() |
|
504 { |
|
505 iConfig = NULL; |
|
506 } |
|
507 |
|
508 // ----------------------------------------------------------------------------- |
|
509 // CNATTraversalSession::Protocol |
|
510 // ----------------------------------------------------------------------------- |
|
511 // |
|
512 TUint CNATTraversalSession::Protocol(RSocket& aSocket) const |
|
513 { |
|
514 TProtocolDesc proto; |
|
515 aSocket.Info(proto); |
|
516 return proto.iProtocol; |
|
517 } |
|
518 |
|
519 // ----------------------------------------------------------------------------- |
|
520 // CNATTraversalSession::RefreshInterval |
|
521 // ----------------------------------------------------------------------------- |
|
522 // |
|
523 TInt CNATTraversalSession::RefreshInterval(TUint aProtocol) const |
|
524 { |
|
525 TInt interval = 0; |
|
526 if (aProtocol == KProtocolInetTcp) |
|
527 { |
|
528 interval = iConfig->TCPRefreshInterval(); |
|
529 } |
|
530 if (aProtocol == KProtocolInetUdp) |
|
531 { |
|
532 interval = iConfig->UDPRefreshInterval(); |
|
533 } |
|
534 return interval; |
|
535 } |
|
536 |
|
537 // ----------------------------------------------------------------------------- |
|
538 // CNATTraversalSession::FindBindingIndex |
|
539 // ----------------------------------------------------------------------------- |
|
540 // |
|
541 TInt CNATTraversalSession::FindBindingIndex( |
|
542 const MSIPNATBindingObserver* aBindingObserver) const |
|
543 { |
|
544 TInt index = KErrNotFound; |
|
545 for (TInt i=0; i < iBindings.Count() && index == KErrNotFound; i++) |
|
546 { |
|
547 CNATBinding* binding = iBindings[i]; |
|
548 if (&(binding->NATBindingObserver()) == aBindingObserver) |
|
549 { |
|
550 index = i; |
|
551 } |
|
552 } |
|
553 return index; |
|
554 } |
|
555 |
|
556 // ----------------------------------------------------------------------------- |
|
557 // CNATTraversalSession::CreateRefresherL |
|
558 // ----------------------------------------------------------------------------- |
|
559 // |
|
560 void CNATTraversalSession::CreateRefresherL( |
|
561 CNATBinding& aBinding, |
|
562 RSocket& aSocket, |
|
563 TBool& aMaintainPersistentConnection) |
|
564 { |
|
565 aMaintainPersistentConnection = EFalse; |
|
566 CNATBindingRefresher* refresher = NULL; |
|
567 TUint protocol = Protocol(aSocket); |
|
568 TInt refreshInterval = RefreshInterval(protocol); |
|
569 if (protocol == KProtocolInetTcp) |
|
570 { |
|
571 CTcpCRLFSender* sender = |
|
572 new(ELeave)CTcpCRLFSender(aSocket,aBinding); |
|
573 CleanupStack::PushL(sender); |
|
574 refresher = new(ELeave)CNATBindingCRLFRefresher(iDeltaTimer, |
|
575 refreshInterval, |
|
576 aBinding, |
|
577 sender); |
|
578 CleanupStack::Pop(sender); |
|
579 aMaintainPersistentConnection = ETrue; |
|
580 } |
|
581 else if (protocol == KProtocolInetUdp) |
|
582 { |
|
583 if (IsBehindNAT(aBinding) && aBinding.AddressResolved()) |
|
584 { |
|
585 // STUN has been succesfully used to detect a NAT |
|
586 if (iSTUNClient && |
|
587 iNextHopAddr.Match(iSTUNClient->STUNServerAddrL())) |
|
588 { |
|
589 // Next hop has a STUN server -> refresh using STUN |
|
590 refresher = |
|
591 new(ELeave)CNATBindingSTUNRefresher(iDeltaTimer, |
|
592 refreshInterval, |
|
593 aBinding, |
|
594 aBinding); |
|
595 } |
|
596 else |
|
597 { |
|
598 // Refresh using STUN towards STUN server and |
|
599 // using CRLF towards the SIP proxy |
|
600 refresher = CNATBindingSTUNAndCRLFRefresher::NewL( |
|
601 iDeltaTimer,refreshInterval,aBinding, |
|
602 aBinding,aSocket,iNextHopAddr,iSocketManager); |
|
603 } |
|
604 } |
|
605 else |
|
606 { |
|
607 if (iConfig->CRLFRefreshEnabledToProxy(protocol)) |
|
608 { |
|
609 CUdpCRLFSender* sender = |
|
610 new(ELeave)CUdpCRLFSender( |
|
611 aSocket,iNextHopAddr,aBinding,iSocketManager); |
|
612 CleanupStack::PushL(sender); |
|
613 refresher = |
|
614 new(ELeave)CNATBindingCRLFRefresher( |
|
615 iDeltaTimer,refreshInterval,aBinding,sender); |
|
616 CleanupStack::Pop(sender); |
|
617 } |
|
618 } |
|
619 } |
|
620 else |
|
621 { |
|
622 User::Leave(KErrNotSupported); |
|
623 } |
|
624 aBinding.SetRefresher(refresher); |
|
625 } |
|
626 |
|
627 // ----------------------------------------------------------------------------- |
|
628 // CNATTraversalSession::SocketIdleImpl |
|
629 // ----------------------------------------------------------------------------- |
|
630 // |
|
631 template<class T> void CNATTraversalSession::SocketIdleImpl( |
|
632 TBool aIdle, |
|
633 T& aSocket) |
|
634 { |
|
635 for (TInt i=0; i<iBindings.Count(); i++) |
|
636 { |
|
637 CNATBindingRefresher* bindingRefresher = iBindings[i]->Refresher(); |
|
638 if (bindingRefresher && bindingRefresher->HasSocket(aSocket)) |
|
639 { |
|
640 bindingRefresher->SetRefresh(aIdle); |
|
641 } |
|
642 } |
|
643 } |
|
644 |
|
645 // ----------------------------------------------------------------------------- |
|
646 // CNATTraversalSession::IsBehindNAT |
|
647 // ----------------------------------------------------------------------------- |
|
648 // |
|
649 TBool CNATTraversalSession::IsBehindNAT(const CNATBinding& aBinding) const |
|
650 { |
|
651 TBool natDetected = EFalse; |
|
652 if (aBinding.AddressResolved()) |
|
653 { |
|
654 if (!aBinding.PublicAddr().Match(iLocalAddr)) |
|
655 { |
|
656 natDetected = ETrue; |
|
657 } |
|
658 } |
|
659 else |
|
660 { |
|
661 if (IsPrivateAddress(iLocalAddr)) |
|
662 { |
|
663 natDetected = ETrue; |
|
664 } |
|
665 } |
|
666 return natDetected; |
|
667 } |
|
668 |
|
669 // ----------------------------------------------------------------------------- |
|
670 // CNATTraversalSession::IsPrivateAddress |
|
671 // ----------------------------------------------------------------------------- |
|
672 // |
|
673 TBool CNATTraversalSession::IsPrivateAddress(const TInetAddr& aAddr) const |
|
674 { |
|
675 TBool isPrivate = EFalse; |
|
676 if (aAddr.Family() == KAfInet) |
|
677 { |
|
678 // aAddr is IPv4 address, IPv6 addresses are always public |
|
679 TUint32 addr = aAddr.Address(); |
|
680 if ((addr >= KPrivateRange1Low && addr <= KPrivateRange1High) || |
|
681 (addr >= KPrivateRange2Low && addr <= KPrivateRange2High) || |
|
682 (addr >= KPrivateRange3Low && addr <= KPrivateRange3High)) |
|
683 { |
|
684 isPrivate = ETrue; |
|
685 } |
|
686 } |
|
687 return isPrivate; |
|
688 } |
|
689 |