natfw/natfwstunplugin/inc/natfwstunconnectionhandler.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 STUN Client. It is responsible
       
    16 *                to handle data received from different observers it
       
    17 *                implements. Connection handler implements 2 Connection
       
    18 *                Multiplexer's observers and STUNClientObserver.
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #ifndef C_NATFWSTUNCONNECTIONHANDLER_H
       
    26 #define C_NATFWSTUNCONNECTIONHANDLER_H
       
    27 
       
    28 
       
    29 #include <e32base.h>
       
    30 #include <e32std.h>
       
    31 #include <in_sock.h>
       
    32 
       
    33 #include "mncmconnectionobserver.h"
       
    34 #include "mncmincomingconnectionobserver.h"
       
    35 #include "natfwstunclientobserver.h"
       
    36 #include "natfwconnectivityapidefs.h"
       
    37 
       
    38 #include "natfwrefreshobserver.h"
       
    39 
       
    40 // forward declarations
       
    41 class MNATFWPluginObserver;
       
    42 class MNcmConnectionMultiplexer;
       
    43 class CNATFWCandidate;
       
    44 class CNATFWStunRefreshTimer;
       
    45 class CSTUNBinding;
       
    46 class CSTUNClient;
       
    47 class CBinding;
       
    48 class TStreamData;
       
    49 class TConnectionData;
       
    50 class CNATFWNatSettingsApi;
       
    51 class MNATFWStunSettings;
       
    52 class CStunAsyncCallback;
       
    53 class CNATFWPluginApi;
       
    54 class CStunServerSettings;
       
    55 
       
    56 /**
       
    57  *  Connection handler for STUN Plug-in
       
    58  *
       
    59  *  Connection handler is responsible for creating connections to Connection
       
    60  *  Multiplexer and STUN Client. It creates STUN Binding and it is responsible
       
    61  *  to handle data received from different observers it implements. Connection
       
    62  *  handler implements 2 Connection Multiplexer's observers and
       
    63  *  STUNClientObserver.
       
    64  *  
       
    65  *  @lib stunplugin.lib
       
    66  *  @since S60 v3.2
       
    67  */
       
    68  
       
    69 class CNATFWStunConnectionHandler : public CBase,
       
    70                                     public MSTUNClientObserver,
       
    71                                     public MNcmIncomingConnectionObserver,
       
    72                                     public MNcmConnectionObserver,
       
    73                                     public MNATFWRefreshObserver
       
    74     {
       
    75 
       
    76     friend class UT_CNATFWStunConnectionHandler;
       
    77 
       
    78 public:
       
    79 
       
    80     /**
       
    81      * Two-phased constructor.
       
    82      * @param aStunPlugin The STUN plugin instance
       
    83      * @param aPluginObserver Observer for callbacks to STUN plug-in's client
       
    84      */
       
    85     static CNATFWStunConnectionHandler* NewL(
       
    86         const CNATFWPluginApi& aStunPlugin,
       
    87         MNATFWPluginObserver& aPluginObserver );
       
    88 
       
    89     /**
       
    90      * Two-phased constructor.
       
    91      * @param aStunPlugin The STUN plugin instance
       
    92      * @param aPluginObserver Observer for callbacks to STUN plug-in's client
       
    93      */
       
    94     static CNATFWStunConnectionHandler* NewLC(
       
    95         const CNATFWPluginApi& aStunPlugin,
       
    96         MNATFWPluginObserver& aPluginObserver );
       
    97 
       
    98     /**
       
    99      * Destructor.
       
   100      */
       
   101     ~CNATFWStunConnectionHandler();
       
   102 
       
   103     /**
       
   104      * Initializes STUN Connection Handler.
       
   105      *
       
   106      * @since S60 v3.2
       
   107      * @param aIapId        IAP ID used in retrieving IAP settings from
       
   108      *                      NAT FW NAT Settings
       
   109      * @param aDomain       Domain name used in creating instance of NATFW
       
   110      *                      NAT Settings to get STUN server address and port.
       
   111      * @param aMultiplexer  Reference to the Connection Multiplexer
       
   112      */
       
   113     void PluginInitializeL( TUint32 aIapId,
       
   114                             const TDesC8& aDomain,
       
   115                             MNcmConnectionMultiplexer& aMultiplexer );
       
   116 
       
   117     /**
       
   118      * Finds out a STUN Server and establishes a connection to it.
       
   119      *
       
   120      * MNATFWPluginObserver::Notify is called with event code EServerConnected
       
   121      * when connecting has completed.
       
   122      *
       
   123      * FetchCandidateL can be called after successful connecting.
       
   124      *
       
   125      * This is done asynchronously.
       
   126      * 
       
   127      * @since   S60 v3.2
       
   128      * @param   aSocketServ         The handle to socket server session
       
   129      * @param   aConnectionName     The unique name of the RConnection
       
   130      */
       
   131     void ConnectServerL( const RSocketServ& aSocketServ,
       
   132         const TName& aConnectionName );
       
   133 
       
   134     /**
       
   135      * Fetches available candidates for a specified stream using given
       
   136      * retransmission timeout for STUN transactions.
       
   137      * 
       
   138      * RTO value 0 indicates default retransmission timeout as specified
       
   139      * in STUN specification.
       
   140      * 
       
   141      * @since   S60 v3.2
       
   142      * @param   aStreamId       Stream identifier
       
   143      * @param   aRtoValue       Retransmission timeout in milliseconds
       
   144      * @param   aAddrFamily     KAFUnspec / KAfInet / KAfInet6
       
   145      * @param   aBaseAddr       Address from which to create connection
       
   146      */
       
   147     void FetchCandidateL( TUint aStreamId, TUint aRtoValue, TUint aAddrFamily,
       
   148         const TInetAddr& aBaseAddr );
       
   149 
       
   150     /**
       
   151      * Gets identifier for a connection that is associated with the
       
   152      * given local end point.
       
   153      *
       
   154      * @since   S60 v3.2
       
   155      * @param   aLocalCandidate     The local end point for a media
       
   156      * @param   aStreamId           The stream identifier
       
   157      * @param   aConnectionId       Reference to found ID
       
   158      */
       
   159     void GetConnectionIdL( const CNATFWCandidate& aLocalCandidate,
       
   160         TUint aStreamId, TUint& aConnectionId );
       
   161 
       
   162     /**
       
   163      * Starts STUN Refresh timer. Sends periodically keepalive messages to
       
   164      * STUN client, which will keep the bindings to STUN Server active.
       
   165      *
       
   166      * @since S60 v3.2
       
   167      */
       
   168     void StartStunRefresh();
       
   169 
       
   170     /**
       
   171      * Creates STUN Binding and calls SendRequestL().
       
   172      *
       
   173      * @since S60 v3.2
       
   174      * @param aStreamId     Stream ID of current STUN Binding
       
   175      * @param aConnectionId Connection ID of current STUN Binding
       
   176      */
       
   177     void CreateSTUNBindingL( TUint aStreamId, TUint aConnectionId );
       
   178 
       
   179     /**
       
   180      * Enables/disables media receiving for the specified local candidate.
       
   181      * 
       
   182      * MNATFWPluginObserver::Notify is called with event code
       
   183      * EReceivingActivated/EReceivingDeactivated when status change has 
       
   184      * completed. While activated incoming data is delivered to the media
       
   185      * engine.
       
   186      * 
       
   187      * @since   S60 v3.2
       
   188      * @pre     FetchCandidate(/s) and possible ICE processing have completed
       
   189      * @param   aLocalCandidate The local end point for a media
       
   190      * @param   aState          The receiving state
       
   191      */
       
   192     void SetReceivingStateL( const CNATFWCandidate& aLocalCandidate,
       
   193         TNATFWStreamingState aState );
       
   194 
       
   195     /**
       
   196      * Enables/disables media sending from the specified local candidate.
       
   197      * 
       
   198      * MNATFWPluginObserver::Notify is called with event code
       
   199      * ESendingActivated/ESendingDeactivated when status change has
       
   200      * completed. While activated outgoing data from media engine is delivered
       
   201      * to the specified remote address.
       
   202      * 
       
   203      * @since   S60 v3.2
       
   204      * @pre     FetchCandidate(/s) and possible ICE processing have completed
       
   205      * @param   aLocalCandidate The local end point for a media
       
   206      * @param   aState          The sending state
       
   207      * @param   aDestAddr       The remote end point for a media
       
   208      */
       
   209     void SetSendingStateL( const CNATFWCandidate& aLocalCandidate,
       
   210         TNATFWStreamingState aState, const TInetAddr& aDestAddr );
       
   211 
       
   212 // From base class MStunClientObserver
       
   213 
       
   214     /**
       
   215      * Indicates that asynchronous initialization of the related
       
   216      * CSTUNClient has been completed and it can be used for
       
   217      * creating CSTUNBinding(s).
       
   218      *
       
   219      * @since S60 v3.2
       
   220      * @param aClient         Handle to STUN client
       
   221      * @param aCompletionCode Completion code
       
   222      */
       
   223     void STUNClientInitCompleted( const CSTUNClient& aClient,
       
   224                                   TInt aCompletionCode );
       
   225 
       
   226     /**
       
   227      * A STUN binding related event occurred. After this callback
       
   228      * the aBinding can be used again by calling its SendRequestL
       
   229      * function.
       
   230      *
       
   231      * @since S60 v3.2
       
   232      * @param aEvent   Binding event occurred
       
   233      * @param aBinding Handle to CBinding
       
   234      */
       
   235     void STUNBindingEventOccurredL( TSTUNBindingEvent aEvent, 
       
   236                                     const CBinding& aBinding );
       
   237 
       
   238     /**
       
   239      * Error occurred during IP address resolution. This callback
       
   240      * will also be called if STUNBindingEventOccurredL leaves.
       
   241      *
       
   242      * @since S60 v3.2
       
   243      * @param aBinding Handle to CBinding
       
   244      * @param aError   Error code
       
   245      */
       
   246     void STUNBindingErrorOccurred( const CBinding& aBinding,TInt aError );
       
   247 
       
   248 // from base class MNcmIncomingConnectionObserver
       
   249 
       
   250     /**
       
   251      * Called by Connection Multiplexer when incoming message is found.
       
   252      *
       
   253      * @since   S60 v3.2
       
   254      * @param   aStreamId           The ID identifying stream
       
   255      * @param   aMessage            Message is a reference to constant message
       
   256      * @param   aLocalAddr          A stream local address.
       
   257      * @param   aFromAddr           A remote source address. ( From socket )
       
   258      * @param   aPeerRemoteAddr     A peer remote address.
       
   259      * @param   aConsumed           Flag to indicate is this message consumed
       
   260      */
       
   261     void IncomingMessageL( TUint aStreamId,
       
   262                            const TDesC8& aMessage,
       
   263                            const TInetAddr& aLocalAddr,
       
   264                            const TInetAddr& aFromAddr,
       
   265                            const TInetAddr& aPeerRemoteAddr,
       
   266                            TBool& aConsumed );
       
   267 
       
   268  // from base class MNcmConnectionObserver
       
   269 
       
   270     /**
       
   271      * Called by Connection Multiplexer when connecting connection completes.
       
   272      *
       
   273      * @since S60 v3.2
       
   274      * @param aStreamId     Stream of target of notify
       
   275      * @param aConnectionId Target of notify
       
   276      * @param aType         Type of notify
       
   277      * @param aError        Error code
       
   278      */
       
   279     void ConnectionNotify( TUint aStreamId,
       
   280                            TUint aConnectionId,
       
   281                            TConnectionNotifyType aType,
       
   282                            TInt aError );
       
   283 
       
   284  // from base class MNATFWRefreshObserver
       
   285 
       
   286     /**
       
   287      * Sends binding request to STUN Binding.
       
   288      *
       
   289      * @since S60 v3.2
       
   290      */
       
   291     void BindingRefreshL();
       
   292 
       
   293 private:
       
   294 
       
   295     CNATFWStunConnectionHandler( const CNATFWPluginApi& aStunPlugin );
       
   296 
       
   297     void ConstructL( MNATFWPluginObserver& aPluginObserver );
       
   298 
       
   299     void DeleteStream( TUint aStreamInd, TBool aRemoveMuxConn );
       
   300 
       
   301     TInt IndexByStreamId( TUint aStreamId );
       
   302 
       
   303     void GenerateServerListL();
       
   304 
       
   305     void TryNextServerL();
       
   306 
       
   307     TConnectionData* ConnectionById( TUint aStreamInd, TUint aConnectionId );
       
   308 
       
   309     TConnectionData* ConnectionByIndex( TUint aStreamInd,
       
   310         TUint aConnectionInd );
       
   311 
       
   312     TBool MatchAddresses( const TInetAddr& aAddr1,
       
   313         const TInetAddr& aAddr2 ) const;
       
   314 
       
   315 private:
       
   316 
       
   317     /**
       
   318      * Flag defining whether STUN refresh has been started.
       
   319      */
       
   320     TBool iStunRefreshStarted;
       
   321 
       
   322     /**
       
   323      * Currently selected index in list of servers ( iServerList )
       
   324      */    
       
   325     TInt iServerIndex;
       
   326     
       
   327     /**
       
   328      * Handle to the socket server.
       
   329      */
       
   330     RSocketServ iSocketServ;
       
   331 
       
   332     /**
       
   333      * Handle to the connection.
       
   334      * Own.
       
   335      */
       
   336     RConnection iConnection;
       
   337 
       
   338     /**
       
   339      * Array for storing data for each stream.
       
   340      */
       
   341     RArray<TStreamData> iStreamArray;
       
   342 
       
   343     /**
       
   344      * Pointer to domain name data.
       
   345      * Own.
       
   346      */
       
   347     HBufC8* iDomain;
       
   348 
       
   349 // Instances from other C classes
       
   350 
       
   351     /**
       
   352      * STUN and TURN client handle.
       
   353      * Own.
       
   354      */
       
   355     CSTUNClient* iStunClient;
       
   356 
       
   357     /**
       
   358      * Set up stream for client.
       
   359      * Not own.
       
   360      */
       
   361     MNcmConnectionMultiplexer* iConnMux;
       
   362 
       
   363     /**
       
   364      * Sends keepalive messages periodically.
       
   365      * Own.
       
   366      */
       
   367     CNATFWStunRefreshTimer* iStunRefreshTimer;
       
   368 
       
   369     /**
       
   370      * Pointer to timer for STUN Client
       
   371      * Own.
       
   372      */
       
   373     CDeltaTimer* iTimerServ;
       
   374 
       
   375     /**
       
   376      * Pointer to NAT Settings.
       
   377      * Own.
       
   378      */
       
   379     CNATFWNatSettingsApi* iNatSettings;
       
   380 
       
   381     /**
       
   382      * Pointer to STUN related NAT Settings.
       
   383      * Not own.
       
   384      */
       
   385     MNATFWStunSettings* iStunSettings;
       
   386 
       
   387     /**
       
   388      * STUN UDP Refresh interval.
       
   389      */
       
   390     TUint iStunRefreshInterval;
       
   391 
       
   392     /**
       
   393      * Async callback handler
       
   394      * own.
       
   395      */
       
   396     CStunAsyncCallback* iAsyncCallback;
       
   397 
       
   398     /**
       
   399      * STUN plugin
       
   400      * Not own.
       
   401      */
       
   402     const CNATFWPluginApi& iStunPlugin;
       
   403 
       
   404     /**
       
   405      * Array of STUN server related settings
       
   406      * Own.
       
   407      */
       
   408     RPointerArray<CStunServerSettings> iServerList;
       
   409 
       
   410     };
       
   411 
       
   412 #endif // C_NATFWSTUNCONNECTIONHANDLER_H