|
1 /* |
|
2 * Copyright (c) 2006-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 |
|
20 |
|
21 |
|
22 #ifndef C_CBINDINGIMPLEMENTATION_H |
|
23 #define C_CBINDINGIMPLEMENTATION_H |
|
24 |
|
25 // INCLUDES |
|
26 #include <e32base.h> |
|
27 #include <es_sock.h> |
|
28 #include <in_sock.h> |
|
29 #include "mstuntransactionobserver.h" |
|
30 #include "cstuntimeruser.h" |
|
31 #include "mstunbindingobserver.h" |
|
32 #include "natfwstunclientdefs.h" |
|
33 |
|
34 // FORWARD DECLARATIONS |
|
35 class CNATFWUNSAFMessage; |
|
36 class CNATFWUNSAFUnknownAttributesAttribute; |
|
37 class CBinding; |
|
38 class CSTUNBindingState; |
|
39 class CSTUNBindingInit; |
|
40 class CSTUNBindingGetSharedSecret; |
|
41 class CSTUNBindingGetAddress; |
|
42 class CSTUNBindingActive; |
|
43 class CSTUNBindingWaitToRetry; |
|
44 class CSTUNClient; |
|
45 class MSTUNBindingObserver; |
|
46 class CSTUNTransaction; |
|
47 class CStunIndicationTransmitter; |
|
48 class MNcmConnectionMultiplexer; |
|
49 |
|
50 |
|
51 |
|
52 // CLASS DECLARATION |
|
53 /** |
|
54 * Hides the implementation of bindings for the user of STUNClient. |
|
55 * |
|
56 * @lib natfwstunclient.lib |
|
57 * @since s60 v3.2 |
|
58 */ |
|
59 class CBindingImplementation : public CSTUNTimerUser, |
|
60 public MSTUNTransactionObserver |
|
61 { |
|
62 |
|
63 public: // default constructor |
|
64 |
|
65 CBindingImplementation( MSTUNBindingObserver& aClient, |
|
66 CBinding& aBinding, |
|
67 RSocket& aSocket ); |
|
68 |
|
69 // overloaded default constructor |
|
70 CBindingImplementation( MSTUNBindingObserver& aClient, |
|
71 CBinding& aBinding, |
|
72 TUint aStreamId, |
|
73 TUint aConnectionId, |
|
74 MNcmConnectionMultiplexer* aMultiplexer ); |
|
75 |
|
76 public: // From CSTUNTimerUser |
|
77 |
|
78 virtual void TimerExpiredL() = 0; |
|
79 |
|
80 virtual void LeaveFromTimerExpired( TInt aError ) = 0; |
|
81 |
|
82 public: // From MSTUNTransactionObserver |
|
83 |
|
84 virtual void PublicAddressObtainedL( const TInetAddr& aAddress ) = 0; |
|
85 |
|
86 // for stun relay functionality |
|
87 virtual void PublicAddressObtainedL( const TInetAddr& aReflexiveAddr, |
|
88 const TInetAddr& aRelayAddr ) = 0; |
|
89 |
|
90 virtual void TransactionError( TInt aError, |
|
91 CNATFWUNSAFUnknownAttributesAttribute* aUnknownAttr ) = 0; |
|
92 |
|
93 virtual void TransactionEventOccurredL( |
|
94 TSTUNCallbackInfo::TFunction aEvent ) = 0; |
|
95 |
|
96 virtual void NewRTTSampleMeasured( TInt64& aRTTSample, |
|
97 TBool aRetransmitted ); |
|
98 |
|
99 |
|
100 public: // New functions |
|
101 |
|
102 /** |
|
103 * Refreshes the binding causing a new Binding Request to be sent |
|
104 * to the STUN server. The public IP address is available when |
|
105 * MSTUNClientObserver gets called. |
|
106 * |
|
107 * @since s60 v3.2 |
|
108 * @return void |
|
109 */ |
|
110 virtual void SendRequestL(); |
|
111 |
|
112 /** |
|
113 * Overloaded version of SendRequestL(). |
|
114 * |
|
115 * @since S60 v3.2 |
|
116 * @param aDestAddr Destination where to send request |
|
117 (KAFUnspec=default address) |
|
118 * @param aAddFingerprint Whether to include fingerprint attribute |
|
119 to request |
|
120 * @param aRtoValue Retransmission timeout |
|
121 (=0 if default to be used) |
|
122 * @return void |
|
123 */ |
|
124 virtual void SendRequestL( const TInetAddr& aDestAddr, |
|
125 TBool aAddFingerprint, |
|
126 TUint aRtoValue ); |
|
127 |
|
128 /** |
|
129 * Refreshes the binding causing a new Binding Request to be sent |
|
130 * to the STUN server. The public IP address is available when |
|
131 * MSTUNClientObserver gets called. |
|
132 * |
|
133 * @since s60 v3.2 |
|
134 * @param aRtoValue Retransmission timeout(=0 if default to be used) |
|
135 * @return void |
|
136 */ |
|
137 virtual void AllocateRequestL( TUint aRtoValue ); |
|
138 |
|
139 /** |
|
140 * Cancels a previously issued call to SendRequestL. |
|
141 * |
|
142 * @since s60 v3.2 |
|
143 * @return void |
|
144 */ |
|
145 virtual void CancelRequest() = 0; |
|
146 |
|
147 /** |
|
148 * Gets the socket used for obtaining the public address. |
|
149 * |
|
150 * @since s60 v3.2 |
|
151 * @return socket |
|
152 */ |
|
153 virtual const RSocket& Socket() const = 0; |
|
154 |
|
155 /** |
|
156 * Checks whether the public address has a value set |
|
157 * |
|
158 * @since s60 v3.2 |
|
159 * @return ETrue if the public address has been obtained, |
|
160 * otherwise EFalse. |
|
161 */ |
|
162 virtual TBool AddressResolved() const = 0; |
|
163 |
|
164 /** |
|
165 * Gets the public address for this binding as seen in public network. |
|
166 * |
|
167 * @since s60 v3.2 |
|
168 * @return public address |
|
169 */ |
|
170 virtual const TInetAddr& PublicAddr() const = 0; |
|
171 |
|
172 /** |
|
173 * Returns alternate server transport address. |
|
174 * |
|
175 * @since s60 v3.2 |
|
176 * @return alternate server address |
|
177 */ |
|
178 virtual const TInetAddr& AlternateServerAddr() const = 0; |
|
179 |
|
180 /** |
|
181 * Returns realm value from error response. |
|
182 * |
|
183 * @since s60 v3.2 |
|
184 * @return realm from error response |
|
185 */ |
|
186 virtual const HBufC8* RealmFromResponse() const = 0; |
|
187 |
|
188 /** |
|
189 * The client should call this function when it has received data |
|
190 * from the socket shared with this binding and |
|
191 * the client cannot itself handle the data. |
|
192 * |
|
193 * @since s60 v3.2 |
|
194 * @param aData data received |
|
195 * @param aConsumed on return ETrue if the data was accepted, |
|
196 * otherwise EFalse. |
|
197 * @param aRemoteAddr remote address from remote address attribute |
|
198 * @return Decoded data if data is indication and not consumed, |
|
199 * otherwise NULL. Ownership is transferred. |
|
200 */ |
|
201 virtual HBufC8* HandleDataL( const TDesC8& aData, |
|
202 TBool& aConsumed, |
|
203 TInetAddr& aRemoteAddr ) = 0; |
|
204 |
|
205 /** |
|
206 * Returns the associated CSTUNClient instance. |
|
207 * |
|
208 * @since s60 v3.2 |
|
209 * @return CSTUNClient, or NULL if the CSTUNClient has been deleted |
|
210 */ |
|
211 virtual const CSTUNClient* STUNClient() const = 0; |
|
212 |
|
213 /** |
|
214 * Get a STUN server address from STUN client |
|
215 * |
|
216 * @since s60 v3.2 |
|
217 * @return void |
|
218 */ |
|
219 virtual void GetServerAddressL() = 0; |
|
220 |
|
221 /** |
|
222 * Get a fresh a shared secret. |
|
223 * |
|
224 * @since s60 v3.2 |
|
225 * @return void |
|
226 */ |
|
227 virtual void GetSharedSecretL() = 0; |
|
228 |
|
229 /** |
|
230 * Clears the pointer to CSTUNClient. |
|
231 * |
|
232 * @since s60 v3.2 |
|
233 * @post iClient == NULL |
|
234 * @return void |
|
235 */ |
|
236 virtual void DetachClient() = 0; |
|
237 |
|
238 /** |
|
239 * Store the obtained public address and inform application. |
|
240 * |
|
241 * @since s60 v3.2 |
|
242 * @param aPublicAddress Public address |
|
243 * @return void |
|
244 */ |
|
245 virtual void StoreAddressL( const TInetAddr& aPublicAddress ) = 0; |
|
246 |
|
247 /** |
|
248 * Store the obtained addresses and inform application. |
|
249 * |
|
250 * @since s60 v3.2 |
|
251 * @param aReflexiveAddr public IP address (server reflexive) |
|
252 * @param aRelayAddr public relay IP address |
|
253 * return void |
|
254 */ |
|
255 virtual void StoreAddressL( const TInetAddr& aReflexiveAddr, |
|
256 const TInetAddr& aRelayAddr ); |
|
257 |
|
258 /** |
|
259 * Change the object's state. |
|
260 * |
|
261 * @since s60 v3.2 |
|
262 * @param aNewState State to enter. |
|
263 * @return void |
|
264 */ |
|
265 virtual void ChangeState( CSTUNBindingState& aNewState ) = 0; |
|
266 |
|
267 /** |
|
268 * Binding encountered an error and enters terminated state. The function |
|
269 * MSTUNClientObserver::BindingErrorL is used to inform application. |
|
270 * |
|
271 * @since s60 v3.2 |
|
272 * @pre aError != KErrNone |
|
273 * @param aError Error code |
|
274 * @return void |
|
275 */ |
|
276 virtual void Terminate( TInt aError ) = 0; |
|
277 |
|
278 /** |
|
279 * Checks if this binding is waiting for shared secret. |
|
280 * |
|
281 * @since s60 v3.2 |
|
282 * @return ETrue BInding is waiting shared secret, EFalse otherwise |
|
283 */ |
|
284 virtual TBool IsWaitingSharedSecret() const = 0; |
|
285 |
|
286 /** |
|
287 * STUN client uses this function to give the username and password, that |
|
288 * the binding requested earlier with function |
|
289 * MSTUNBindingObserver::ObtainSharedSecretL. |
|
290 * |
|
291 * @since s60 v3.2 |
|
292 * @pre aUsername and aPassword must either both have a descriptor that |
|
293 * is not empty, or then both must have an empty descritor. |
|
294 * @param aUsername Username or an empty descriptor if the shared secret |
|
295 * is not used. |
|
296 * @param aPassword Password or an empty descriptor if the shared secret |
|
297 * is not used. |
|
298 * @param void |
|
299 */ |
|
300 virtual void SharedSecretObtainedL( const TDesC8& aUsername, |
|
301 const TDesC8& aPassword ) = 0; |
|
302 |
|
303 /** |
|
304 * Creates or updates a Binding Request message, using the given username |
|
305 * and password. |
|
306 * |
|
307 * @since s60 v3.2 |
|
308 * @param aUsername Username |
|
309 * @param aPassword Password |
|
310 * @return void |
|
311 */ |
|
312 virtual void CreateBindingRequestL( const TDesC8& aUsername, |
|
313 const TDesC8& aPassword ) = 0; |
|
314 |
|
315 /** |
|
316 * Creates a transaction for sending Binding Request message |
|
317 * |
|
318 * @since s60 v3.2 |
|
319 * @pre iRequest != NULL |
|
320 * @return void |
|
321 */ |
|
322 virtual void SendBindingRequestL() = 0; |
|
323 |
|
324 /** |
|
325 * Take actions based on the reason why transaction failed. For certain |
|
326 * errors, it is possible to send the Binding Request again. |
|
327 * |
|
328 * @since s60 v3.2 |
|
329 * @pre aError != KErrNone |
|
330 * @param aError Error reason |
|
331 * @return ETrue Request can be retried |
|
332 * EFalse Binding enters Terminated-state |
|
333 */ |
|
334 virtual TBool HandleTransactionError( TInt aError ) = 0; |
|
335 |
|
336 /** |
|
337 * Forward the ICMP error to transaction for processing. |
|
338 * |
|
339 * @since s60 v3.2 |
|
340 * @param aAddress Address where the ICMP error occurred |
|
341 * @return void |
|
342 */ |
|
343 virtual void IcmpError( const TInetAddr& aAddress ) = 0; |
|
344 |
|
345 /** |
|
346 * Wait before retring to send a Binding Request |
|
347 * |
|
348 * @since s60 v3.2 |
|
349 * @return void |
|
350 */ |
|
351 virtual void WaitBeforeRetrying() = 0; |
|
352 |
|
353 /** |
|
354 * ICE specific attributes can be set for the request. |
|
355 * |
|
356 * @since s60 v3.2 |
|
357 * @param aAttributes Contains ICE specific attributes. |
|
358 * @return void |
|
359 */ |
|
360 virtual void SetICESpecificAttributes( |
|
361 const TICEAttributes& aAttributes ) = 0; |
|
362 |
|
363 /** |
|
364 * Returns the relay address allocated for this binding. |
|
365 * |
|
366 * @since s60 v3.2 |
|
367 * @return STUN Relay public IP address |
|
368 */ |
|
369 virtual const TInetAddr& RelayAddr() const; |
|
370 |
|
371 /** |
|
372 * Once the client wants to primarily receive from one peer, it can send |
|
373 * a SetActiveDestination request. All subsequent data received from |
|
374 * the active peer is forwarded directly to the client and vice versa, |
|
375 * except that it is wrapped or framed according to the protocol used |
|
376 * between the STUN relay client and STUN relay server. |
|
377 * The SetActiveDestination Request does not close other bindings. Data |
|
378 * to and from other peers is still wrapped in Send and Data indications |
|
379 * respectively. |
|
380 * |
|
381 * @since s60 v3.2 |
|
382 * @param aRemoteAddr Client can specify remote address |
|
383 * @since aTimerValue Client gets timer value after response |
|
384 * @return void |
|
385 */ |
|
386 virtual void SetActiveDestinationRequestL( |
|
387 const TInetAddr& aRemoteAddr, TUint32& aTimerValue ); |
|
388 |
|
389 /** |
|
390 * Send indication to remote address. Indications are not |
|
391 * retransmitted and responses are not expected. |
|
392 * |
|
393 * @since s60 v3.2 |
|
394 * @param aRemoteAddr Address to send to (remote agent) |
|
395 * @param aData Data to send to remote agent. Use KNullDesC8 |
|
396 * if there is no data available. |
|
397 * @param aAddFingerprint Add fingerprint attribute if true |
|
398 * @return void |
|
399 */ |
|
400 virtual void SendIndicationL( const TInetAddr& aRemoteAddr, |
|
401 const TDesC8& aData, TBool aAddFingerprint ) = 0; |
|
402 |
|
403 /** |
|
404 * The Connect Request is used by a client when it has obtained an |
|
405 * allocated transport address that is TCP. The client can use the |
|
406 * Connect Request to ask the server to open a TCP connection to a |
|
407 * specified destination address included in the request. |
|
408 * |
|
409 * @since s60 v3.2 |
|
410 * @param aRemoteAddr remote address |
|
411 * @return void |
|
412 */ |
|
413 virtual void ConnectRequestL( const TInetAddr& aRemoteAddr ); |
|
414 |
|
415 /** |
|
416 * client can query used stream id |
|
417 * |
|
418 * @since s60 v3.2 |
|
419 * @return stream Id |
|
420 */ |
|
421 virtual TUint StreamId( ) const = 0; |
|
422 |
|
423 /** |
|
424 * client can query used connection id |
|
425 * |
|
426 * @since s60 v3.2 |
|
427 * @return connection Id |
|
428 */ |
|
429 virtual TUint ConnectionId( ) const = 0; |
|
430 |
|
431 |
|
432 public: // New functions, for internal use |
|
433 |
|
434 /** |
|
435 * Release the memory of the Binding Request related data. This data is |
|
436 * only needed for sending a Binding Request and receiving a response. |
|
437 * |
|
438 * @since s60 v3.2 |
|
439 * @return void |
|
440 */ |
|
441 virtual void FreeRequestData() = 0; |
|
442 |
|
443 /** |
|
444 * Return STUN client handle. |
|
445 * |
|
446 * @since s60 v3.2 |
|
447 * @pre iClient != NULL |
|
448 * @return STUN client |
|
449 * @leave KErrNotFound If the STUN client has been deleted |
|
450 */ |
|
451 virtual MSTUNBindingObserver& ClientL() const = 0; |
|
452 |
|
453 /** |
|
454 * Return the current username, or an empty descriptor if username |
|
455 * does not exist. |
|
456 * |
|
457 * @since s60 v3.2 |
|
458 * @return Username |
|
459 */ |
|
460 virtual const TDesC8& Username() const = 0; |
|
461 |
|
462 /** |
|
463 * Checks if the STUN server has requested client to use a shared secret. |
|
464 * @return ETrue if STUN server requires the use of a shared secret |
|
465 * EFalse otherwise |
|
466 */ |
|
467 virtual TBool IsSharedSecretRequired() const = 0; |
|
468 |
|
469 /** |
|
470 * Decodes a byte stream and return the decoded UNSAF message. |
|
471 * @param aData Byte stream to decode |
|
472 * @return Decoded message, ownership is transferred. |
|
473 * NULL if the stream didn't contain an UNSAF message. |
|
474 */ |
|
475 virtual CNATFWUNSAFMessage* DecodeMessageL( const TDesC8& aData ) const = 0; |
|
476 |
|
477 /** |
|
478 * Used binding |
|
479 * |
|
480 * @since s60 v3.2 |
|
481 * @return void |
|
482 */ |
|
483 virtual const CBinding& Binding() = 0; |
|
484 |
|
485 /** |
|
486 * Cancels retransmission process after transaction expires. |
|
487 * |
|
488 * @since s60 v3.2 |
|
489 * @return void |
|
490 */ |
|
491 virtual void CancelRetransmission(); |
|
492 |
|
493 |
|
494 private: |
|
495 |
|
496 void CalculateNewRTO( TInt64& aRTTSample ); |
|
497 |
|
498 protected: // Data |
|
499 |
|
500 /** |
|
501 * Associated STUN client instance |
|
502 * Not own. |
|
503 */ |
|
504 MSTUNBindingObserver* iClient; |
|
505 |
|
506 /** |
|
507 * Associated binding interface |
|
508 */ |
|
509 CBinding& iBinding; |
|
510 |
|
511 /** |
|
512 * The user of NAT Traversal FW provides the UDP RSocket to be used in |
|
513 * Binding Requests and responses. It is passed within CSTUNBinding::NewL |
|
514 * Each CSTUNBinding instance uses a separate RSocket. |
|
515 * |
|
516 * Not own. Must be NULLed in derived classes destructor. |
|
517 */ |
|
518 RSocket* iSocket; |
|
519 |
|
520 /** |
|
521 * Used natfw stream id |
|
522 */ |
|
523 TUint iStreamId; |
|
524 |
|
525 /** |
|
526 * Used natfw connection id |
|
527 */ |
|
528 TUint iConnectionId; |
|
529 |
|
530 /** |
|
531 * Used for transaction by unsaf protocols |
|
532 * Not own. |
|
533 */ |
|
534 MNcmConnectionMultiplexer* iMux; |
|
535 |
|
536 /** |
|
537 * STUN server's address. If the server does not respond, binding tries |
|
538 * to obtain another address from the STUN client. |
|
539 */ |
|
540 TInetAddr iServerAddress; |
|
541 |
|
542 /** |
|
543 *The public server reflexive address, obtained from STUN server |
|
544 */ |
|
545 TInetAddr iPublicAddr; |
|
546 |
|
547 /** |
|
548 *The public stun server relay address |
|
549 */ |
|
550 TInetAddr iRelayAddr; |
|
551 |
|
552 /** |
|
553 * Controls whether to insert XOR-ONLY into Binding Request |
|
554 */ |
|
555 TBool iAddXorOnly; |
|
556 |
|
557 /** |
|
558 * If binding couldn't be obtained because transaction ended with an |
|
559 * Error, the reason is stored here. |
|
560 */ |
|
561 TInt iTransactionError; |
|
562 |
|
563 /** |
|
564 * Amount of successive error (4xx-6xx) responses. Binding may retry to |
|
565 * send the request upon receiving a certain error response, but only |
|
566 * does so for a limited amount of responses. This counter is used to |
|
567 * prevent infinite loops, when e.g. server always responds with a 5xx. |
|
568 */ |
|
569 TInt iErrorResponseCount; |
|
570 |
|
571 /** |
|
572 * Binding Request |
|
573 * owned. |
|
574 */ |
|
575 CNATFWUNSAFMessage* iRequest; |
|
576 |
|
577 /** |
|
578 * Shared secret, owned. Can't be a reference to the password stored in |
|
579 * CSTUNSharedSecret as CBindingImplementation wouldn't know if it |
|
580 * expired and STUN client has created a different CSTUNSharedSecret. |
|
581 * Can be an empty descriptor. |
|
582 * Own. |
|
583 */ |
|
584 HBufC8* iSharedSecret; |
|
585 |
|
586 /** |
|
587 * UNKNOWN-ATTRIBUTES received from transaction. ' |
|
588 * Owned. |
|
589 */ |
|
590 CNATFWUNSAFUnknownAttributesAttribute* iUnknownAttr; |
|
591 |
|
592 /** |
|
593 * instance of CSTUNTransaction |
|
594 * Owned |
|
595 */ |
|
596 CSTUNTransaction* iTransaction; |
|
597 |
|
598 /** |
|
599 * Current state |
|
600 * Not own. |
|
601 */ |
|
602 CSTUNBindingState* iState; |
|
603 |
|
604 /** |
|
605 * ICE specific attributes |
|
606 */ |
|
607 TICEAttributes iICEAttributes; |
|
608 |
|
609 /** |
|
610 * STUN binding states |
|
611 * Own. |
|
612 */ |
|
613 CSTUNBindingInit* iInit; |
|
614 CSTUNBindingGetSharedSecret* iGetSharedSecret; |
|
615 CSTUNBindingGetAddress* iGetAddress; |
|
616 CSTUNBindingActive* iActive; |
|
617 CSTUNBindingWaitToRetry* iWaitToRetry; |
|
618 |
|
619 /** |
|
620 * Retransmit interval |
|
621 */ |
|
622 TInt iRetransmitInterval; |
|
623 |
|
624 /** |
|
625 * Realm value in error response |
|
626 * Own. |
|
627 */ |
|
628 HBufC8* iRealmFromResponse; |
|
629 |
|
630 /** |
|
631 * Nonce value from error response |
|
632 * Own. |
|
633 */ |
|
634 HBufC8* iNonce; |
|
635 |
|
636 /** |
|
637 * Realm value shuold be used from response |
|
638 */ |
|
639 TBool iUseRealmFromResponse; |
|
640 |
|
641 /** |
|
642 * ETrue if fingerprint attributes will be added to requests. |
|
643 */ |
|
644 TBool iAddFingerprint; |
|
645 |
|
646 /** |
|
647 * Indication transmitter |
|
648 * Own. |
|
649 */ |
|
650 CStunIndicationTransmitter* iIndicationTx; |
|
651 |
|
652 |
|
653 private: |
|
654 |
|
655 /** |
|
656 * Indicates if we have calculated the first RTO value |
|
657 */ |
|
658 TBool iFirstRTOCalculated; |
|
659 |
|
660 /** |
|
661 * Smoothed round-trip time |
|
662 */ |
|
663 TInt64 iSRTT; |
|
664 |
|
665 /** |
|
666 * Round-trip time variation |
|
667 */ |
|
668 TInt64 iRTTVAR; |
|
669 |
|
670 /** |
|
671 * Retransmission timeout |
|
672 */ |
|
673 TInt64 iRTO; |
|
674 |
|
675 /** |
|
676 * Dummy IP address |
|
677 */ |
|
678 TInetAddr iDummyIP; |
|
679 }; |
|
680 |
|
681 #endif // CBINDINGIMPLEMENTATION_H |
|
682 |
|
683 // End of File |