natfw/natfwstunserver/inc/natfwstunsrvimpl.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:    Implements functionality of NATFW STUN server client session
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #ifndef NATFWSTUNSRVIMPL_H
       
    22 #define NATFWSTUNSRVIMPL_H
       
    23 
       
    24 #include <e32base.h>
       
    25 #include <in_sock.h>
       
    26 #include "natfwconnectivityapidefs.h"
       
    27 
       
    28 #include "mncmincomingconnectionobserver.h"
       
    29 
       
    30 #include "natfwunsaftransactionid.h"
       
    31 
       
    32 class MNATFWStunSrvObserver;
       
    33 class CNATFWCredentials;
       
    34 class CNATFWUNSAFMessage;
       
    35 class MNcmConnectionMultiplexer;
       
    36 
       
    37 /**
       
    38  * Implementation class of NATFW STUN server.
       
    39  *
       
    40  * @lib stunserver.lib
       
    41  * @since S60 v3.2
       
    42  */                                                   
       
    43 NONSHARABLE_CLASS( CNATFWSTUNSrvImpl ) : public CBase, 
       
    44     public MNcmIncomingConnectionObserver
       
    45     {
       
    46     
       
    47     friend class ut_cnatfwstunsrvimpl;
       
    48     
       
    49 public:
       
    50     
       
    51     /**
       
    52      * Two-phased constructor.
       
    53      * @param aObserver    A callback object to receive and handle
       
    54      *                     event notifications from NAT FW STUN server.
       
    55      * @param aObserver    Stun server observer
       
    56      * @param aMultiplexer A multiplexer
       
    57      */
       
    58     static CNATFWSTUNSrvImpl* NewL( MNATFWStunSrvObserver& aObserver,
       
    59         MNcmConnectionMultiplexer& aMultiplexer );
       
    60 
       
    61     /**
       
    62      * Destructor.
       
    63      */
       
    64     virtual ~CNATFWSTUNSrvImpl();
       
    65 
       
    66     /**
       
    67      * Adds authentication params which are used to authenticate
       
    68      * incoming binding requests.
       
    69      * 
       
    70      * Notifies the client of STUN requests that are valid.
       
    71      *  
       
    72      * @since S60 3.2
       
    73      * @param aIdentifications Identification array.
       
    74      * @return void
       
    75      */
       
    76     void AddAuthenticationParamsL( 
       
    77         const RPointerArray<CNATFWCredentials>& aIdentifications );
       
    78      
       
    79     /**
       
    80      * Removes specified authentication params from STUN server.
       
    81      *  
       
    82      * @since S60 3.2
       
    83      * @param aIdentifications Identification array.
       
    84      * @return void
       
    85      */
       
    86     void RemoveAuthenticationParamsL( 
       
    87         const RPointerArray<CNATFWCredentials>& aIdentifications );
       
    88         
       
    89     /**
       
    90      * Sets role for an agent. 
       
    91      *  
       
    92      * @since S60 3.2
       
    93      * @param aRole       Either controlling or controlled.  
       
    94      * @param aTieBreaker Value for determining which agent will change
       
    95      *                    Role if role conflict is present
       
    96      * @return void
       
    97      */             
       
    98     void SetRoleL( TNATFWIceRole aRole, TUint64 aTieBreaker );
       
    99 
       
   100 // from base class MNcmIncomingConnectionObserver
       
   101  
       
   102     void IncomingMessageL( TUint aStreamId, 
       
   103         const TDesC8& aMessage, const TInetAddr& aLocalAddr, 
       
   104         const TInetAddr& aFromAddr, const TInetAddr& aPeerRemoteAddress,
       
   105         TBool& aConsumed );
       
   106 
       
   107 private: // Constructors
       
   108 
       
   109     void ConstructL( MNcmConnectionMultiplexer& aMultiplexer );
       
   110 
       
   111     CNATFWSTUNSrvImpl( MNATFWStunSrvObserver& aObserver );
       
   112  
       
   113 private: // private methods
       
   114 
       
   115     /**
       
   116      * Function that checks if role conflict is present between agents.
       
   117      *
       
   118      * @since S60 3.2   
       
   119      * @param aRequest UNSAF message.    
       
   120      * return ETrue if chosen role conflicts
       
   121      *        EFalse if not 
       
   122      */    
       
   123     TBool IsRoleConflictPresent( const CNATFWUNSAFMessage& aRequest );
       
   124         
       
   125     /**
       
   126      * Setting function for changing peer remote address.
       
   127      * @since S60 3.2
       
   128      * @param aFromAddr          Address the server saw in request.
       
   129      * @param aPeerRemoteAddress Peer remote address.
       
   130      * return void
       
   131      */
       
   132     void SetPeerRemoteAddress( const TInetAddr& aFromAddr,
       
   133         TInetAddr& aPeerRemoteAddress) const;
       
   134               
       
   135     /**
       
   136      * Decodes a byte stream and return the decoded UNSAF message.
       
   137      *  
       
   138      * @since S60 3.2
       
   139      * @param aData Incoming message data.
       
   140      * @return Decoded message, ownership is transferred.
       
   141      *         NULL if the stream didn't contain an UNSAF message.
       
   142      * @return One of the standard system-wide error codes.
       
   143      */    
       
   144     CNATFWUNSAFMessage* DecodeMessageL( const TDesC8& aData ) const;
       
   145     
       
   146     /**
       
   147      * Encodes an UNSAF message.
       
   148      *
       
   149      * @since S60 3.2
       
   150      * @param aRequest   UNSAF message.
       
   151      * @param aPassword  Key for checking the message integrity.
       
   152      * @param aErrorCode Error code.
       
   153      * @return
       
   154      */
       
   155     HBufC8* EncodeMessageL( const CNATFWUNSAFMessage& aRequest, 
       
   156         const TDesC8& aPassword, const TInt aErrorCode ) const;
       
   157 
       
   158     /**
       
   159      * Validates the request. Authenticate and check message integrity.
       
   160      * Sends binding response if everything goes right and error response
       
   161      * if there is missing argument or message integrity check fails.
       
   162      *  
       
   163      * @since S60 3.2
       
   164      * @param aRequest    UNSAF message.
       
   165      * @param aByteStream Same UNSAF message as aRequest, but in undecoded
       
   166      *                    format.
       
   167      * @return ETrue if needed attributes are present in binding request.
       
   168      *         EFalse if any needed attribute is missing.
       
   169      */
       
   170     TBool IsRequestValidL( const CNATFWUNSAFMessage& aRequest,
       
   171                            const TDesC8& aByteStream );
       
   172     
       
   173     /**
       
   174      * Verifies the integrity of a Binding Request.
       
   175      *  
       
   176      * @since S60 3.2
       
   177      * @param aRequest    UNSAF message.
       
   178      * @param aByteStream Same message as aRequest, but in undecoded format
       
   179      * @param aPassword   Key for checking the message integrity.
       
   180      * @return ETrue If integrity check succeeded.
       
   181      *         EFalse Integrity check fails.
       
   182      */    
       
   183     TBool CheckIntegrityL( const CNATFWUNSAFMessage& aRequest,
       
   184                            const TDesC8& aByteStream,
       
   185                            const TDesC8& aPassword );
       
   186 
       
   187     /**
       
   188      * Finds the credential which match for the given username and
       
   189      * returns the password.
       
   190      *  
       
   191      * @since S60 3.2
       
   192      * @param aRequest The received UNSAF message.
       
   193      * @param aError Returns error code via reference 
       
   194      * @return Password NUll if password not found.
       
   195      */                                     
       
   196     HBufC8* GetPassword( const CNATFWUNSAFMessage& aRequest, TInt& aError );
       
   197     
       
   198     /**
       
   199      * Creates and sends binding response or binding error response.
       
   200      *  
       
   201      * @since S60 3.2
       
   202      * @param aRequest   The received UNSAF message.
       
   203      * @param aErrorCode Error code.
       
   204      * @param aPassword  Password is used to calculate message integrity.
       
   205      * @return void
       
   206      */
       
   207     void SendResponseL( const CNATFWUNSAFMessage& aRequest, TInt aErrorCode, 
       
   208                         const TDesC8& aPassword );
       
   209     
       
   210     TBool IsRetransmittedRequest( const CNATFWUNSAFMessage& aRequest ) const;
       
   211     
       
   212     void SaveTransactionIdL( const TNATFWUNSAFTransactionID& aId );
       
   213     
       
   214     TInt FindTransactionId( const TNATFWUNSAFTransactionID& aId ) const;
       
   215 
       
   216 private:
       
   217     
       
   218     /*
       
   219      * Observer for STUN request related notifies.
       
   220      */
       
   221     MNATFWStunSrvObserver& iObserver;
       
   222     
       
   223     /**
       
   224      * Identification array, password for authentication
       
   225      */
       
   226     RPointerArray<CNATFWCredentials> iIdentificationArray;
       
   227     
       
   228     /**
       
   229      * A Stream Id
       
   230      */
       
   231     TUint iStreamId; 
       
   232     
       
   233     /**
       
   234      * Remote address
       
   235      */
       
   236     TInetAddr iFromAddress;
       
   237     
       
   238     /**
       
   239      * Multiplexer instance
       
   240      * Not own.
       
   241      */
       
   242     MNcmConnectionMultiplexer* iMultiplexer;
       
   243     
       
   244     /**
       
   245      * Peer remote address
       
   246      */
       
   247     TInetAddr iPeerAddr;
       
   248     
       
   249     /**
       
   250      * Local address
       
   251      */
       
   252     TInetAddr iLocalAddress;
       
   253     
       
   254     /**
       
   255      * Username
       
   256      * Own.
       
   257      */
       
   258     HBufC8* iUsername;
       
   259     
       
   260     /**
       
   261      * Boolean indicating if data indication includes STUN binding request
       
   262      */
       
   263     TBool iRequestInsideIndication;
       
   264     
       
   265     /**
       
   266      * Current role
       
   267      */
       
   268     TInt iRole;
       
   269     
       
   270     /**
       
   271      * Chosen role conflicts with another agent if true
       
   272      */
       
   273     TBool iRoleConflict;
       
   274     
       
   275     /**
       
   276      * Value for determining which agent will change it's role if role 
       
   277      * conflict is present
       
   278      */
       
   279     TUint64 iTieBreaker;
       
   280     
       
   281     /**
       
   282      * Array containing latest transaction identifiers.
       
   283      */
       
   284     RArray<TNATFWUNSAFTransactionID> iTransactionIds;
       
   285     };
       
   286 
       
   287 #endif // NATFWSTUNSRVIMPL_H