natfw/natfwturnplugin/inc/natfwturnconnectionhandler.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     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:    Connection handler is responsible for creating connections
       
    15 *                to Connection Multiplexer and TURN Client. It is responsible
       
    16 *                to handle data received from different observers it
       
    17 *                implements. Connection handler implements 3 Connection
       
    18 *                Multiplexer's observers and STUNClientObserver.
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #ifndef C_NATFWTURNCONNECTIONHANDLER_H
       
    26 #define C_NATFWTURNCONNECTIONHANDLER_H
       
    27 
       
    28 
       
    29 #include <e32base.h>
       
    30 #include <e32std.h>
       
    31 #include <es_sock.h>
       
    32 #include <in_sock.h>
       
    33 
       
    34 #include "mncmconnectionobserver.h"
       
    35 #include "mncmincomingconnectionobserver.h"
       
    36 #include "mncmoutgoingconnectionobserver.h"
       
    37 #include "mncmmessageobserver.h"
       
    38 #include "natfwstunclientobserver.h"
       
    39 
       
    40 #include "natfwrefreshobserver.h"
       
    41 #include "natfwtimerobserver.h"
       
    42 
       
    43 // forward declarations
       
    44 class MNATFWPluginObserver;
       
    45 class MNcmConnectionMultiplexer;
       
    46 class CNATFWCandidate;
       
    47 class CNATFWTurnRefreshTimer;
       
    48 class CSTUNRelayBinding;
       
    49 class CSTUNClient;
       
    50 class TStreamData;
       
    51 class TConnectionData;
       
    52 class CNATFWNatSettingsApi;
       
    53 class MNATFWTurnSettings;
       
    54 class CNATFWTurnActiveDestinationTimer;
       
    55 class CTurnAsyncCallback;
       
    56 class CNATFWPluginApi;
       
    57 class MNATFWServerSettings;
       
    58 class CTurnServerSettings;
       
    59 
       
    60 /**
       
    61  *  Connection handler for TURN Plug-in.
       
    62  *
       
    63  *  Connection handler is responsible for creating connections to Connection
       
    64  *  Multiplexer and TURN Client. It creates TURN Binding and it is responsible
       
    65  *  to handle data received from different observers it implements. Connection
       
    66  *  handler implements 3 Connection Multiplexer's observers and
       
    67  *  STUNClientObserver.
       
    68  *
       
    69  *  @lib turnplugin.lib
       
    70  *  @since S60 v3.2
       
    71  */
       
    72 class CNATFWTurnConnectionHandler : public CBase,
       
    73                                     public MSTUNClientObserver,
       
    74                                     public MNcmConnectionObserver,
       
    75                                     public MNcmIncomingConnectionObserver,
       
    76                                     public MNcmOutgoingConnectionObserver,
       
    77                                     public MNcmMessageObserver,
       
    78                                     public MNATFWRefreshObserver,
       
    79                                     public MNATFWTimerObserver
       
    80     {
       
    81 
       
    82     friend class UT_CNATFWTurnConnectionHandler;
       
    83 
       
    84 public:
       
    85 
       
    86     /**
       
    87      * Two-phased constructor.
       
    88      * @param aTurnPlugin The TURN plugin instance
       
    89      * @param aPluginObserver Observer for callbacks to STUN plug-in's client
       
    90      */
       
    91     static CNATFWTurnConnectionHandler* NewL(
       
    92         const CNATFWPluginApi& aTurnPlugin,
       
    93         MNATFWPluginObserver& aPluginObserver );
       
    94         
       
    95     /**
       
    96      * Two-phased constructor.
       
    97      * @param aTurnPlugin The TURN plugin instance
       
    98      * @param aPluginObserver Observer for callbacks to STUN plug-in's client
       
    99      */
       
   100     static CNATFWTurnConnectionHandler* NewLC(
       
   101         const CNATFWPluginApi& aTurnPlugin,
       
   102         MNATFWPluginObserver& aPluginObserver );
       
   103 
       
   104 
       
   105     ~CNATFWTurnConnectionHandler();
       
   106 
       
   107     /**
       
   108      * Initializes TURN Connection Handler.
       
   109      *
       
   110      * @since S60 v3.2
       
   111      * @param aIapId        IAP ID used in retrieving IAP settings from
       
   112      *                      NAT FW NAT Settings
       
   113      * @param aDomain       Domain name used in creating instance of NATFW
       
   114      *                      NAT Settings to get STUN server address and port.
       
   115      * @param aMultiplexer  Reference to the Connection Multiplexer
       
   116      */
       
   117     void PluginInitializeL( TUint32 aIapId,
       
   118                             const TDesC8& aDomain,
       
   119                             MNcmConnectionMultiplexer& aMultiplexer );
       
   120 
       
   121     /**
       
   122      * Finds out a STUN Server and establishes a connection to it.
       
   123      * 
       
   124      * MNATFWPluginObserver::Notify is called with event code EServerConnected
       
   125      * when connecting has completed.
       
   126      *
       
   127      * FetchCandidateL can be called after successful connecting.
       
   128      *
       
   129      * This is done asynchronously.
       
   130      * 
       
   131      * @since   S60 v3.2
       
   132      * @param   aSocketServ         The handle to socket server session
       
   133      * @param   aConnectionName     The unique name of the RConnection
       
   134      */
       
   135     void ConnectServerL( const RSocketServ& aSocketServ,
       
   136         const TName& aConnectionName );
       
   137 
       
   138     /**
       
   139      * Fetches available candidates for a specified stream using given
       
   140      * retransmission timeout for STUN transactions.
       
   141      * 
       
   142      * RTO value 0 indicates default retransmission timeout as specified
       
   143      * in STUN specification.
       
   144      * 
       
   145      * @since   S60 v3.2
       
   146      * @param   aStreamId       Stream identifier
       
   147      * @param   aRtoValue       Retransmission timeout in milliseconds
       
   148      * @param   aAddrFamily     KAFUnspec / KAfInet / KAfInet6
       
   149      * @param   aBaseAddr       Address from which to create connection
       
   150      */
       
   151     void FetchCandidateL( TUint aStreamId, TUint aRtoValue, TUint aAddrFamily,
       
   152         const TInetAddr& aBaseAddr );
       
   153 
       
   154     /**
       
   155      * Gets identifier for a connection that is associated with the
       
   156      * given local end point.
       
   157      * 
       
   158      * @since   S60 v3.2
       
   159      * @param   aLocalCandidate     The local end point for a media
       
   160      * @param   aStreamId           The stream identifier
       
   161      * @param   aConnectionId       Reference to found ID
       
   162      */
       
   163     void GetConnectionIdL( const CNATFWCandidate& aLocalCandidate,
       
   164         TUint aStreamId, TUint& aConnectionId );
       
   165 
       
   166     /**
       
   167      * Starts TURN Refresh timer. Sends periodically keepalive messages to
       
   168      * TURN client, which will keep the bindings to TURN Server active.
       
   169      *
       
   170      * @since   S60 v3.2
       
   171      */
       
   172     void StartTurnRefresh();
       
   173 
       
   174     /**
       
   175      * Creates TURN Binding and calls AllocateRequestL().
       
   176      *
       
   177      * @since S60 v3.2
       
   178      * @param aStreamId      Stream ID of current TURN Binding
       
   179      * @param aConnectionId  Connection ID of current TURN Binding
       
   180      */
       
   181     void CreateTURNBindingL( TUint aStreamId, TUint aConnectionId );
       
   182 
       
   183     /**
       
   184      * Enables/disables media receiving for the specified local candidate.
       
   185      * 
       
   186      * MNATFWPluginObserver::Notify is called with event code
       
   187      * EReceivingActivated/EReceivingDeactivated when status change has 
       
   188      * completed. While activated incoming data is delivered to the media
       
   189      * engine.
       
   190      * 
       
   191      * @since   S60 v3.2
       
   192      * @pre     FetchCandidate(/s) and possible ICE processing have completed
       
   193      * @param   aLocalCandidate The local end point for a media
       
   194      * @param   aState          The receiving state
       
   195      */
       
   196     void SetReceivingStateL( const CNATFWCandidate& aLocalCandidate,
       
   197         TNATFWStreamingState aState );
       
   198 
       
   199     /**
       
   200      * Once the client wants to primarily receive from one peer, it can send
       
   201      * a SetActiveDestination request.  All subsequent data received from
       
   202      * the active peer is forwarded directly to the client and vice versa,
       
   203      * except that it is wrapped or framed according to the protocol used
       
   204      * between the STUN relay client and STUN relay server.
       
   205      * The SetActiveDestination Request does not close other bindings. Data
       
   206      * to and from other peers is still wrapped in Send and Data indications
       
   207      * respectively.
       
   208      *
       
   209      * @since   S60 v3.2
       
   210      * @param   aLocalCandidate The local end point for a media
       
   211      * @param   aState          The sending state
       
   212      * @param   aDestAddr       The remote end point for a media
       
   213      */
       
   214     void SetSendingStateL( const CNATFWCandidate& aLocalCandidate,
       
   215         TNATFWStreamingState aState, const TInetAddr& aDestAddr );
       
   216 
       
   217     /**
       
   218      * Validates that is outgoing message a TURN request or Send Indication.
       
   219      *
       
   220      * @since   S60 v3.2
       
   221      * @param   aMessage    Message from which will be validated if
       
   222      *                      it is TURN request or Send Indication
       
   223      * @return ETrue if the message is TURN request or Send Indication
       
   224      */
       
   225     TBool IsRequestOrIndicationL( const TDesC8& aMessage ) const;
       
   226 
       
   227     /**
       
   228      * Validates that is incoming message a TURN response.
       
   229      *
       
   230      * @since   S60 v3.2
       
   231      * @param   aMessage    Message from which will be validated if
       
   232      *                      it is TURN response
       
   233      * @return ETrue if the message is TURN response
       
   234      */
       
   235     TBool IsTurnResponseL( const TDesC8& aMessage ) const;
       
   236 
       
   237 // From base class MStunClientObserver
       
   238 
       
   239     /**
       
   240      * Indicates that asynchronous initialization of the related
       
   241      * CSTUNClient has been completed and it can be used for 
       
   242      * creating CSTUNRelayBinding(s).
       
   243      *
       
   244      * @since S60 v3.2
       
   245      * @param aClient         Handle to STUN client
       
   246      * @param aCompletionCode Completion code
       
   247      */
       
   248     void STUNClientInitCompleted( const CSTUNClient& aClient,
       
   249                                   TInt aCompletionCode );
       
   250 
       
   251     /**
       
   252      * A TURN binding related event occurred. After this callback
       
   253      * the aBinding can be used again by calling its SendRequestL
       
   254      * function.
       
   255      *
       
   256      * @since S60 v3.2
       
   257      * @param aEvent   Binding event occurred
       
   258      * @param aBinding Handle to CBinding
       
   259      */
       
   260     void STUNBindingEventOccurredL( TSTUNBindingEvent aEvent,
       
   261                                     const CBinding& aBinding );
       
   262 
       
   263     /**
       
   264      * Error occurred during IP address resolution. This callback
       
   265      * will also be called if STUNBindingEventOccurredL leaves.
       
   266      *
       
   267      * @since S60 v3.2
       
   268      * @param aBinding Handle to CBinding
       
   269      * @param aError   Error code
       
   270      */
       
   271     void STUNBindingErrorOccurred( const CBinding& aBinding,TInt aError );
       
   272 
       
   273  // from base class MNcmConnectionObserver
       
   274 
       
   275     /**
       
   276      * Called by Connection Multiplexer when connection connecting completes.
       
   277      *
       
   278      * @since S60 v3.2
       
   279      * @param aStreamId     Stream of target of notify
       
   280      * @param aConnectionId Target of notify
       
   281      * @param aType         Type of notify
       
   282      * @param aError        Error code
       
   283      */
       
   284     void ConnectionNotify( TUint aStreamId,
       
   285                            TUint aConnectionId,
       
   286                            TConnectionNotifyType aType,
       
   287                            TInt aError );
       
   288 
       
   289 // from base class MNcmIncomingConnectionObserver
       
   290 
       
   291     /**
       
   292      * Called by Connection Multiplexer when incoming message is found.
       
   293      *
       
   294      * @since   S60 v3.2
       
   295      * @param   aStreamId           The ID identifying stream
       
   296      * @param   aMessage            Message is a reference to constant message
       
   297      * @param   aConsumed           Flag to indicate is this message consumed
       
   298      * @param   aLocalAddr          A stream local address.
       
   299      * @param   aFromAddr           A remote source address. ( From socket )
       
   300      * @param   aPeerRemoteAddr     A peer remote address.
       
   301      * @param   aConsumed           A reference to a boolean variable which MUST
       
   302      *                              be set by observer: ETrue is message was 
       
   303      *                              consumed, EFalse otherwise.
       
   304      */
       
   305     void IncomingMessageL( TUint aStreamId, const TDesC8& aMessage,
       
   306         const TInetAddr& aLocalAddr, const TInetAddr& aFromAddr,
       
   307         const TInetAddr& aPeerRemoteAddr, TBool& aConsumed );
       
   308 
       
   309 // from base class MNcmOutgoingConnectionObserver
       
   310 
       
   311     /**
       
   312      * Called by Connection Multiplexer when outgoing message is found.
       
   313      *
       
   314      * @since S60 v3.2
       
   315      * @param aStreamId             Stream where message was found
       
   316      * @param aConnectionId         Connection where message was found
       
   317      * @param aDestinationAddress   Destination address where the packet
       
   318      *                              is being sent to. If this is set then
       
   319      *                              connection ID is ignored       
       
   320      * @param aMessage              Message is a reference to constant message
       
   321      *                              which was found
       
   322      * @param aConsumed             Is reference to a boolean variable which
       
   323      *                              MUST be set by observer: ETrue is message
       
   324      *                              was consumed, EFalse otherwise.
       
   325      */
       
   326     void OutgoingMessageL( TUint aStreamId, TUint aConnectionId,
       
   327         const TInetAddr& aDestinationAddress, const TDesC8& aMessage,
       
   328         TBool& aConsumed );
       
   329 
       
   330 // from base class MNcmMessageObserver
       
   331 
       
   332     /**
       
   333      * Called by Connection Multiplexer when incoming message is found.
       
   334      * Allows connection observer to observe and make modifications to found
       
   335      * message by offering an own copy of content with modification(s).
       
   336      *
       
   337      * TURN Plug-in must fill the peer address, because only TURN Plug-in can
       
   338      * know currently activated destination.
       
   339      *
       
   340      * @since   S60 v3.2
       
   341      * @param   aStreamId           The ID identifying stream
       
   342      * @param   aMessage            Message is a reference to constant message
       
   343      * @param   aLocalAddr          A stream local address.
       
   344      * @param   aFromAddr           A remote source address. ( From socket )
       
   345      * @param   aPeerRemoteAddr     A peer remote address.
       
   346      * @return  Pointer to a heap descriptor with new content for message,
       
   347      *          otherwise NULL is returned. Ownership of the heap descriptor
       
   348      *          is transferred to connection multiplexer ( from observer ).
       
   349      */
       
   350     HBufC8* IncomingMessageNotify( TUint aStreamId,
       
   351                                    const TDesC8& aMessage,
       
   352                                    const TInetAddr& aLocalAddr,
       
   353                                    const TInetAddr& aFromAddr,
       
   354                                    TInetAddr& aPeerRemoteAddr );
       
   355 
       
   356 // from base class MNcmMessageObserver
       
   357 
       
   358     /**
       
   359      * Called by Connection Multiplexer when outgoing message is found.
       
   360      * Allows connection observer to observe and make modifications to found
       
   361      * message by offering an own copy of content with modification(s).
       
   362      *
       
   363      * @since S60 v3.2
       
   364      * @param aStreamId             The ID identifying stream
       
   365      * @param aConnectionId         The ID identifying connection
       
   366      * @param aDestinationAddress   Destination address where the packet
       
   367      *                              is being sent to.   
       
   368      * @param aMessage              Message is a reference to constant message
       
   369      *                              which was found
       
   370      * @return pointer to a heap descriptor with new content for message,
       
   371      *         otherwise NULL is returned. Ownership of the heap descriptor is
       
   372      *         transferred to multiplexer (from observer).
       
   373      */
       
   374     HBufC8* OutgoingMessageNotify( TUint aStreamId,
       
   375                                    TUint aConnectionId,
       
   376                                    const TInetAddr& aDestinationAddress,
       
   377                                    const TDesC8& aMessage );
       
   378 
       
   379  // from base class MNATFWRefreshObserver
       
   380 
       
   381     /**
       
   382      * Sends binding requests to TURN Bindings.
       
   383      *
       
   384      * @since S60 v3.2
       
   385      */
       
   386     void BindingRefreshL();
       
   387 
       
   388 // from base class MNATFWTimerObserver
       
   389 
       
   390     /**
       
   391      * Notifies when active destination timer has triggered.
       
   392      *
       
   393      * @since S60 v3.2
       
   394      * @param  aStreamId        To identify the binding that the
       
   395      *                          triggering was related to
       
   396      * @param  aConnectionId    To identify the binding that the
       
   397      *                          triggering was related to
       
   398      */
       
   399     void TimerTriggeredL( TUint aStreamId, TUint aConnectionId );
       
   400 
       
   401 private:
       
   402 
       
   403     CNATFWTurnConnectionHandler( const CNATFWPluginApi& aTurnPlugin );
       
   404 
       
   405     void ConstructL( MNATFWPluginObserver& aPluginObserver );
       
   406 
       
   407     void DeleteStream( TUint aStreamInd, TBool aRemoveMuxConn );
       
   408 
       
   409     HBufC8* HandleIncomingMessageL( TUint aStreamId, const TDesC8& aMessage, 
       
   410         const TInetAddr& aLocalAddr, const TInetAddr& aFromAddr, 
       
   411         TInetAddr& aPeerRemoteAddr );
       
   412 
       
   413     TInt IndexByStreamId( TUint aStreamId );
       
   414 
       
   415     TConnectionData* ConnectionById( TUint aStreamInd, TUint aConnectionId );
       
   416 
       
   417     TConnectionData* ConnectionByIndex( TUint aStreamInd,
       
   418         TUint aConnectionInd );
       
   419 
       
   420     void GenerateServerListL();
       
   421 
       
   422     void TryNextServerL();
       
   423 
       
   424     TConnectionData* FindConnection( TUint aStreamId,
       
   425         const TInetAddr& aLocalAddr );
       
   426 
       
   427     TBool MatchAddresses( const TInetAddr& aAddr1, const TInetAddr& aAddr2 );
       
   428 
       
   429 private:
       
   430 
       
   431     /**
       
   432      * Flag defining whether TURN refresh has been started
       
   433      */
       
   434     TBool iTurnRefreshStarted;
       
   435 
       
   436     /**
       
   437      * Currently selected index in list of servers ( iServerList )
       
   438      */    
       
   439     TInt iServerIndex;
       
   440     
       
   441     /**
       
   442      * Handle to the socket server.
       
   443      */
       
   444     RSocketServ iSocketServ;
       
   445 
       
   446     /**
       
   447      * Handle to the connection.
       
   448      * Own.
       
   449      */
       
   450     RConnection iConnection;
       
   451     
       
   452     /**
       
   453      * Pointer to domain name data.
       
   454      * Own.
       
   455      */
       
   456     HBufC8* iDomain;
       
   457 
       
   458     /**
       
   459      * Array for storing data for each stream
       
   460      */
       
   461     RArray<TStreamData> iStreamArray;
       
   462 
       
   463     
       
   464 // Instances from other C -classes
       
   465 
       
   466     /**
       
   467      * STUN and TURN client handle.
       
   468      * Own.
       
   469      */
       
   470     CSTUNClient* iStunClient;
       
   471 
       
   472     /**
       
   473      * Set up stream for client.
       
   474      * Not own.
       
   475      */
       
   476     MNcmConnectionMultiplexer* iConnMux;
       
   477 
       
   478     /**
       
   479      * Sends keepalive messages periodically.
       
   480      * Own.
       
   481      */
       
   482     CNATFWTurnRefreshTimer* iTurnRefreshTimer;
       
   483 
       
   484     /**
       
   485      * Timer for setting Active Destination.
       
   486      * Own.
       
   487      */
       
   488     CNATFWTurnActiveDestinationTimer* iActiveDestinationTimer;
       
   489 
       
   490     /**
       
   491      * Pointer to timer for STUN Client.
       
   492      * Own.
       
   493      */
       
   494     CDeltaTimer* iTimerServ;
       
   495 
       
   496     /**
       
   497      * Pointer to NAT Settings.
       
   498      * Own.
       
   499      */
       
   500     CNATFWNatSettingsApi* iNatSettings;
       
   501 
       
   502     /**
       
   503      * Pointer to TURN related NAT Settings.
       
   504      * Not own.
       
   505      */
       
   506     MNATFWTurnSettings* iTurnSettings;
       
   507 
       
   508     /**
       
   509      * TURN UDP Refresh interval.
       
   510      */
       
   511     TUint iTurnRefreshInterval;
       
   512 
       
   513     /**
       
   514      * TURN Plug-in
       
   515      * Not own.
       
   516      */
       
   517     const CNATFWPluginApi& iTurnPlugin;
       
   518 
       
   519     /**
       
   520      * Async callback handler
       
   521      * own.
       
   522      */
       
   523     CTurnAsyncCallback* iAsyncCallback;
       
   524 
       
   525     /**
       
   526      * Array of server related settings
       
   527      * Own.
       
   528      */    
       
   529     RPointerArray<CTurnServerSettings> iServerList;
       
   530     };
       
   531 
       
   532 #endif // C_NATFWTURNCONNECTIONHANDLER_H