vpnengine/ikesocket/src/datatransfer.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 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:  Data transfer functionality
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "datatransfer.h"
       
    20 #include "ikemsgheader.h"
       
    21 #include "ikev2const.h"
       
    22 #include "localaddressresolver.h"
       
    23 #include "ikedebug.h"
       
    24 #include "ikesocketassert.h"
       
    25 
       
    26 using namespace IkeSocket;
       
    27 
       
    28 const TInt KReceiveQueueMaxCount( 10 );
       
    29 
       
    30 // ======== MEMBER FUNCTIONS ========
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // Two-phased constructor.
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 CDataTransfer* CDataTransfer::NewL( RSocketServ& aSocketServer,
       
    37                                     RConnection& aConnection,
       
    38                                     CLocalAddressResolver& aLocalAddressResolver,
       
    39                                     MDataTransferCallback& aCallback,
       
    40                                     MIkeDebug& aDebug  )
       
    41     {    
       
    42     CDataTransfer* self = new (ELeave) CDataTransfer( aSocketServer,
       
    43                                                       aConnection,
       
    44                                                       aLocalAddressResolver,
       
    45                                                       aCallback,
       
    46                                                       aDebug );  
       
    47     CleanupStack::PushL(self);
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop(self);    
       
    50     return self;            
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // Destructor.
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CDataTransfer::~CDataTransfer()
       
    58     {
       
    59     DEBUG_LOG( _L("CDataTransfer::~CDataTransfer") );
       
    60     
       
    61     CloseSockets();    
       
    62     iReceiveQueue.Close();
       
    63     
       
    64     delete iSender;
       
    65     delete iReceiver;
       
    66     delete iReceiverNAT;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Constructor.
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CDataTransfer::CDataTransfer( RSocketServ& aSocketServer,
       
    74                               RConnection& aConnection,
       
    75                               CLocalAddressResolver& aLocalAddressResolver,
       
    76                               MDataTransferCallback& aCallback,
       
    77                               MIkeDebug& aDebug )
       
    78  : iSocketServer( aSocketServer ),
       
    79    iConnection( aConnection ),
       
    80    iLocalNokiaNATPort( 0 ),
       
    81    iLocalAddressResolver( aLocalAddressResolver ),
       
    82    iErrorCallback( aCallback ),
       
    83    iDebug( aDebug )
       
    84     {
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Second phase construction.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CDataTransfer::ConstructL()
       
    92     {
       
    93     DEBUG_LOG( _L("CDataTransfer::ConstructL") );
       
    94     
       
    95     iSender = CSender::NewL( iSocket,
       
    96                              iSocketNAT,
       
    97                              iSocketNokiaNAT,
       
    98                              *this,
       
    99                              iDebug );
       
   100     iReceiver = CReceiver::NewL( iSocket,
       
   101                                  *this,
       
   102                                  iDebug );
       
   103     
       
   104     iReceiverNAT = CReceiver::NewL( iSocketNAT,
       
   105                                     *this,
       
   106                                     iDebug );
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // Sets IKE major version to receivers.
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CDataTransfer::SetIkeMajorVersion( const TIkeMajorVersion aIkeMajorVersion )
       
   114     {
       
   115     IKESOCKET_ASSERT( iReceiver );
       
   116     IKESOCKET_ASSERT( iReceiverNAT );
       
   117     
       
   118     iReceiver->SetIkeMajorVersion( aIkeMajorVersion );
       
   119     iReceiverNAT->SetIkeMajorVersion( aIkeMajorVersion );    
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Sets IP version.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CDataTransfer::SetIpVersion( const IkeSocket::TIpVersion aIpVersion )
       
   127     {
       
   128     iIpVersion = aIpVersion;
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Opens sockets and binds sockets for ports 500 and 4500. 
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 TInt CDataTransfer::OpenSockets( const TInetAddr& aLocalIp )
       
   136     {    
       
   137     IKESOCKET_ASSERT( !iSocketsOpen );
       
   138     
       
   139     // Open sockets
       
   140     TInt err = OpenSocket( iSocket );
       
   141     
       
   142     if ( err == KErrNone )
       
   143         {
       
   144         err = OpenSocket( iSocketNAT );
       
   145         
       
   146         if ( err == KErrNone )
       
   147             {
       
   148             err = OpenSocket( iSocketNokiaNAT );        
       
   149             }            
       
   150         }
       
   151     
       
   152     // Bind sockets for ports 500 and 4500
       
   153     if ( err == KErrNone )
       
   154         {
       
   155         err = BindSocket( iSocket, aLocalIp, KIkePort500 );
       
   156         
       
   157         if ( err == KErrNone )
       
   158             {
       
   159             err = BindSocket( iSocketNAT, aLocalIp, KIkePort4500 );
       
   160             }        
       
   161         }       
       
   162     
       
   163     if ( err == KErrNone )
       
   164         {                
       
   165         iSocketsOpen = ETrue;
       
   166         }
       
   167     else
       
   168         {
       
   169         // Close sockets if error
       
   170         iSocket.Close();
       
   171         iSocketNAT.Close();
       
   172         iSocketNokiaNAT.Close();        
       
   173         iSocketsOpen = EFalse;
       
   174         }
       
   175 
       
   176     DEBUG_LOG1( _L("CDataTransfer::OpenSockets, err=%d"), err );
       
   177     return err;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // Closes sockets.
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 void CDataTransfer::CloseSockets()
       
   185     {
       
   186     DEBUG_LOG1( _L("CDataTransfer::CloseSockets, sockets open=%d"),
       
   187             iSocketsOpen );
       
   188     
       
   189     if ( iSocketsOpen )
       
   190         {
       
   191         // Cancel send and receive.
       
   192         DoCancelSend( KErrDisconnected );
       
   193         DoCancelReceive( KErrDisconnected );
       
   194 
       
   195         // Stop receiving.
       
   196         StopReceive();
       
   197         
       
   198         // Close sockets.
       
   199         iSocket.Close();    
       
   200         iSocketNAT.Close();
       
   201         iSocketNokiaNAT.Close();
       
   202         
       
   203         iSocketsOpen = EFalse;
       
   204         }        
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // Sends UDP data.
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CDataTransfer::SendUdpData( const TInt aLocalPort,
       
   212                                  const TInetAddr& aDestAddr,
       
   213                                  const TDesC8& aUdpData,
       
   214                                  const TUint aDscp,
       
   215                                  TRequestStatus& aStatus )
       
   216     {
       
   217     TInt err( KErrNone );
       
   218     
       
   219     IKESOCKET_ASSERT( iClientStatusSend == NULL );
       
   220     IKESOCKET_ASSERT( iSender );
       
   221     
       
   222     iClientStatusSend = &aStatus;
       
   223     *iClientStatusSend = KRequestPending;
       
   224     
       
   225     if ( !iSocketsOpen )
       
   226         {
       
   227         err = KErrDisconnected;
       
   228         }
       
   229     
       
   230     if ( err == KErrNone )
       
   231         {
       
   232         if ( ( aLocalPort != KIkePort500 ) &&
       
   233              ( aLocalPort != KIkePort4500 ) )
       
   234             {
       
   235             // Nokia NAT keepalive packet.
       
   236             if ( aLocalPort == aDestAddr.Port() )
       
   237                 {    
       
   238                 if ( iLocalNokiaNATPort == 0 )
       
   239                     {
       
   240                     // Set Nokia NAT Port if not set.
       
   241                     err = iSocketNokiaNAT.SetLocalPort( aLocalPort );                    
       
   242 
       
   243                     if ( err == KErrNone )
       
   244                         {
       
   245                         iLocalNokiaNATPort = aLocalPort;
       
   246                         }                    
       
   247                     }
       
   248                 else if ( iLocalNokiaNATPort != aLocalPort )
       
   249                     {
       
   250                     // Nokia NAT port cannot be changed
       
   251                     // during connection.
       
   252                     err = KErrArgument;
       
   253                     }
       
   254                 else
       
   255                     {
       
   256                     err = KErrNone;
       
   257                     }
       
   258                 }
       
   259             else
       
   260                 {
       
   261                 // Local port does not match destination port.
       
   262                 err = KErrArgument;
       
   263                 }        
       
   264             }
       
   265         }
       
   266 
       
   267     if ( err == KErrNone )
       
   268         {
       
   269         err = iSender->SendUdpData( aLocalPort,
       
   270                                     aDestAddr,
       
   271                                     aUdpData,
       
   272                                     aDscp );        
       
   273         }
       
   274 
       
   275     if ( err )
       
   276         {
       
   277         CompleteSendToClient( err );
       
   278         }    
       
   279     }
       
   280 
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // Cancels sending.
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CDataTransfer::CancelSend()
       
   287     {
       
   288     DoCancelSend();
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // Receives UDP data.
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CDataTransfer::ReceiveUdpData( HBufC8*& aUdpData,
       
   296                                     TInetAddr& aSrcAddr,
       
   297                                     TInt& aLocalPort,
       
   298                                     TRequestStatus& aStatus  )
       
   299     {
       
   300     IKESOCKET_ASSERT( iClientStatusReceive == NULL );
       
   301     IKESOCKET_ASSERT( iClientMsgReceive == NULL );
       
   302     IKESOCKET_ASSERT( iClientSrcAddrReceive == NULL );
       
   303     IKESOCKET_ASSERT( iClientLocalPort == NULL );
       
   304     
       
   305     // Store client data.
       
   306     iClientStatusReceive = &aStatus;
       
   307     *iClientStatusReceive = KRequestPending;
       
   308     iClientMsgReceive = &aUdpData;
       
   309     iClientSrcAddrReceive = &aSrcAddr;
       
   310     iClientLocalPort = &aLocalPort;    
       
   311     
       
   312     if ( !iSocketsOpen )
       
   313         {
       
   314         CompleteReceiveToClient( KErrDisconnected );
       
   315         return;
       
   316         }
       
   317 
       
   318     iReceivingStopped = EFalse;
       
   319     
       
   320     TInt count = iReceiveQueue.Count();    
       
   321     if ( count < KReceiveQueueMaxCount )
       
   322         {
       
   323         ReceiveData();
       
   324         }
       
   325     
       
   326     if ( count )
       
   327         {
       
   328         // Data is already available.
       
   329         CompleteReceiveToClient( KErrNone );       
       
   330         }    
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // Cancels receive request.
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 void CDataTransfer::CancelReceive()
       
   338     {
       
   339     if ( iClientStatusReceive )
       
   340         {
       
   341         CompleteReceiveToClient( KErrCancel );
       
   342         }
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // Clears available data.
       
   347 // ---------------------------------------------------------------------------
       
   348 //
       
   349 void CDataTransfer::ClearReceivedData()
       
   350     {
       
   351     DEBUG_LOG( _L("CDataTransfer::ClearReceivedData") );
       
   352     
       
   353     CleanupReceiveQueue();
       
   354     ReceiveData();
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 // Stops receiving. Available data is cleared.
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 void CDataTransfer::StopReceive()
       
   362     {
       
   363     DEBUG_LOG( _L("CDataTransfer::StopReceive") );
       
   364     
       
   365     CleanupReceiveQueue();    
       
   366     DoCancelReceive( KErrCancel );    
       
   367     iReceivingStopped = ETrue;
       
   368     }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // Gets local IP address.
       
   372 // ---------------------------------------------------------------------------
       
   373 //
       
   374 TInt CDataTransfer::GetLocalAddress( TInetAddr& aLocalIp )
       
   375     {
       
   376     IKESOCKET_ASSERT( iIpVersion == EIPv4 || iIpVersion == EIPv6 );
       
   377     return iLocalAddressResolver.GetLocalAddress( iIpVersion, aLocalIp );        
       
   378     }
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 // Notification about completed send from sender.
       
   382 // ---------------------------------------------------------------------------
       
   383 //
       
   384 void CDataTransfer::SendCompleted( const TInt aStatus )
       
   385     {
       
   386     CompleteSendToClient( aStatus );
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // Notification that data has been received.
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CDataTransfer::DataReceived( HBufC8* aUdpData,
       
   394                                   const TInetAddr& aSrcAddr,
       
   395                                   const TInt aLocalPort )
       
   396     {
       
   397     // Store message to receive queue.
       
   398     TReceiveQueueItem item( aUdpData, // Ownership transferred.
       
   399                             aSrcAddr,
       
   400                             aLocalPort );      
       
   401     TInt err = iReceiveQueue.Append( item );
       
   402     
       
   403     if ( iReceiveQueue.Count() >= KReceiveQueueMaxCount )
       
   404         {
       
   405         // Queue is full. Cancel receiving.
       
   406         iReceiver->CancelReceive();
       
   407         iReceiverNAT->CancelReceive();
       
   408         }
       
   409     
       
   410     if ( iClientStatusReceive )
       
   411         {
       
   412         CompleteReceiveToClient( KErrNone );
       
   413         }
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // Notification about receive error.
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void CDataTransfer::ReceiveError( const TInt aStatus )
       
   421     {    
       
   422     if ( iClientStatusReceive )
       
   423         {
       
   424         CompleteReceiveToClient( aStatus );
       
   425         }
       
   426     
       
   427     StopReceive();
       
   428     
       
   429     iErrorCallback.DataTransferError( aStatus,
       
   430                                       MDataTransferCallback::EReceiveError );
       
   431     }
       
   432 
       
   433 // ---------------------------------------------------------------------------
       
   434 // Opens socket.
       
   435 // ---------------------------------------------------------------------------
       
   436 //
       
   437 TInt CDataTransfer::OpenSocket( RSocket& aSocket )
       
   438     {
       
   439     TInt err = aSocket.Open( iSocketServer,
       
   440                              KAfInet,
       
   441                              KSockDatagram,
       
   442                              KProtocolInetUdp,
       
   443                              iConnection );
       
   444     if ( err == KErrNone )
       
   445         {
       
   446         // Enable multiple binds to same port
       
   447         err = aSocket.SetOpt( KSoReuseAddr, KSolInetIp, 1 );
       
   448         }
       
   449     
       
   450     return err;
       
   451     }
       
   452 
       
   453 // ---------------------------------------------------------------------------
       
   454 // Binds socket.
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 TInt CDataTransfer::BindSocket( RSocket& aSocket,
       
   458                                 const TInetAddr& aLocalIp,
       
   459                                 const TInt aLocalPort )
       
   460     {
       
   461     TInt err( KErrNone );
       
   462     TInetAddr localAddr( aLocalIp );
       
   463     
       
   464     localAddr.SetPort( aLocalPort );
       
   465     err = aSocket.Bind( localAddr );
       
   466 
       
   467 #ifdef _DEBUG    
       
   468         TBuf<100> txt_addr;
       
   469         aLocalIp.Output( txt_addr );
       
   470         DEBUG_LOG3( _L("Bind socket, address:port=%S:%d, err=%d"),
       
   471                 &txt_addr, aLocalPort, err );
       
   472 #endif
       
   473     
       
   474     return err;    
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------------------------
       
   478 // Cancels sending.
       
   479 // ---------------------------------------------------------------------------
       
   480 //
       
   481 void CDataTransfer::DoCancelSend( const TInt aCompletionStatus )
       
   482     {
       
   483     iSender->Cancel();
       
   484     
       
   485     if ( iClientStatusSend )
       
   486         {
       
   487         CompleteSendToClient( aCompletionStatus );
       
   488         }
       
   489     }
       
   490 
       
   491 // ---------------------------------------------------------------------------
       
   492 // Cancels receiving.
       
   493 // ---------------------------------------------------------------------------
       
   494 //
       
   495 void CDataTransfer::DoCancelReceive( const TInt aCompletionStatus )
       
   496     {
       
   497     iReceiver->CancelReceive();
       
   498     iReceiverNAT->CancelReceive();
       
   499     
       
   500     if ( iClientStatusReceive )
       
   501         {
       
   502         CompleteReceiveToClient( aCompletionStatus );
       
   503         }
       
   504     }
       
   505 
       
   506 // ---------------------------------------------------------------------------
       
   507 // Completes send to client.
       
   508 // ---------------------------------------------------------------------------
       
   509 //
       
   510 void CDataTransfer::CompleteSendToClient( const TInt aStatus )
       
   511     {
       
   512     IKESOCKET_ASSERT( iClientStatusSend  );
       
   513     
       
   514     User::RequestComplete( iClientStatusSend, aStatus );
       
   515     iClientStatusSend = NULL;        
       
   516     }
       
   517 
       
   518 // ---------------------------------------------------------------------------
       
   519 // Completes receive to client.
       
   520 // ---------------------------------------------------------------------------
       
   521 //
       
   522 void CDataTransfer::CompleteReceiveToClient( const TInt aStatus )
       
   523     {
       
   524     IKESOCKET_ASSERT( iClientStatusReceive );
       
   525     IKESOCKET_ASSERT( iClientMsgReceive );
       
   526     IKESOCKET_ASSERT( iClientSrcAddrReceive );
       
   527     IKESOCKET_ASSERT( iClientLocalPort );
       
   528     
       
   529     TInt count = iReceiveQueue.Count();
       
   530     
       
   531     if ( ( aStatus == KErrNone ) && count )
       
   532         {        
       
   533         // Get oldest item from receive queue.
       
   534         TReceiveQueueItem item = iReceiveQueue[0];
       
   535         iReceiveQueue.Remove( 0 );        
       
   536         *iClientMsgReceive = item.UdpData(); // Transfer ownership.
       
   537         *iClientSrcAddrReceive = item.SrcAddr();
       
   538         *iClientLocalPort = item.LocalPort();
       
   539         
       
   540         // Need to receive more data if queue was full.
       
   541         ReceiveData();
       
   542         }
       
   543     
       
   544     // Complete receive.
       
   545     User::RequestComplete( iClientStatusReceive, aStatus );    
       
   546     iClientStatusReceive = NULL;
       
   547     iClientMsgReceive = NULL;
       
   548     iClientSrcAddrReceive = NULL;
       
   549     iClientLocalPort = NULL;    
       
   550     }
       
   551     
       
   552 // ---------------------------------------------------------------------------
       
   553 // Receives more data if receiving not requested to be stopped.
       
   554 // ---------------------------------------------------------------------------
       
   555 //
       
   556 void CDataTransfer::ReceiveData()
       
   557     {
       
   558     if ( !iReceivingStopped )
       
   559         {
       
   560         iReceiver->Receive();
       
   561         iReceiverNAT->Receive();
       
   562         }    
       
   563     }
       
   564 
       
   565 // ---------------------------------------------------------------------------
       
   566 // Cleans up data from receive queue.
       
   567 // ---------------------------------------------------------------------------
       
   568 //
       
   569 void CDataTransfer::CleanupReceiveQueue()
       
   570     {
       
   571     while ( iReceiveQueue.Count() )
       
   572         {
       
   573         delete iReceiveQueue[0].UdpData();
       
   574         iReceiveQueue.Remove( 0 );
       
   575         }    
       
   576     }
       
   577