vpnengine/ikesocket/inc/datatransfer.h
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 #ifndef C_DATATRANSFER_H
       
    20 #define C_DATATRANSFER_H
       
    21 
       
    22 #include <in_sock.h>
       
    23 #include "ikedatainterface.h"
       
    24 #include "sender.h"
       
    25 #include "receiver.h"
       
    26 #include "receivequeueitem.h"
       
    27 #include "ikesocketdefs.h"
       
    28 
       
    29 // FORWARD DECLARATIONS
       
    30 
       
    31 class CLocalAddressResolver;
       
    32 class MIkeDebug;
       
    33 
       
    34 /**
       
    35  *  Data transfer callback interface.
       
    36  *
       
    37  *  Callback interface for informing fatal error in data transfer.
       
    38  *
       
    39  *  @lib ikesocket.lib
       
    40  */
       
    41 NONSHARABLE_CLASS( MDataTransferCallback )
       
    42     {
       
    43 public:
       
    44     enum TErrorType
       
    45         {
       
    46         ESendError,
       
    47         EReceiveError
       
    48         };
       
    49     
       
    50     /**
       
    51      * Notifies that data transfer error has occured.
       
    52      * 
       
    53      * @param aError Error value
       
    54      * @param aErrorType Error type
       
    55      */        
       
    56     virtual void DataTransferError( const TInt aError,
       
    57                                     const TErrorType aErrorType ) = 0;
       
    58     };
       
    59 
       
    60 /**
       
    61  *  Data transfer class.
       
    62  *
       
    63  *  This class provides functionality for transferring UDP data. This class
       
    64  *  implements data interface (MIkeDataInterface) which can be used
       
    65  *  by the client of data interface to send and receive UDP data. Received data
       
    66  *  is queued until client reads received data.
       
    67  * 
       
    68  *  Before instance of this class can be used for transferring data, owner of
       
    69  *  the instance must set IKE major version and request opening of sockets.
       
    70  *  When data transferring is no more needed, owner can request closing of
       
    71  *  sockets. Fatal data transfer errors are notified to owner through 
       
    72  *  error callback interface (MDataTransferCallback).
       
    73  * 
       
    74  *  This class owns sockets for local ports 500 and 4500 which are used for
       
    75  *  both sending and receiving UDP data. This class owns also third socket
       
    76  *  which is used if the client of data interface sends Nokia NAT keepalive
       
    77  *  packet. 
       
    78  * 
       
    79  *  @lib ikesocket.lib
       
    80  */
       
    81 class CDataTransfer : public CBase, 
       
    82                       public MIkeDataInterface,
       
    83                       public MSenderCallback,
       
    84                       public MReceiverCallback
       
    85     {
       
    86 public:
       
    87     
       
    88     /**
       
    89      * Two-phased constructor.
       
    90      * @param aSocketServer Socket server
       
    91      * @param aConnection Connection
       
    92      * @param aCallback Error callback interface
       
    93      * @param aDebug Debug trace interface
       
    94      */
       
    95     static CDataTransfer* NewL( RSocketServ& aSocketServer,
       
    96                                 RConnection& aConnection,
       
    97                                 CLocalAddressResolver& aLocalAddressResolver,
       
    98                                 MDataTransferCallback& aCallback,
       
    99                                 MIkeDebug& aDebug );
       
   100     
       
   101     /**
       
   102      * Destructor.
       
   103      */
       
   104     ~CDataTransfer();    
       
   105 
       
   106     /**
       
   107      * Sets IKE major version. IKE major version must be set before data
       
   108      * interface is used for data transfer.
       
   109      * 
       
   110      * @param aIkeMajorVersion IKE major version
       
   111      */
       
   112     void SetIkeMajorVersion( const IkeSocket::TIkeMajorVersion aIkeMajorVersion );
       
   113 
       
   114     /**
       
   115      * Sets IP version. IP version must be set before data interface is used
       
   116      * for getting local IP address.
       
   117      * 
       
   118      * @param aIpVersion IP version
       
   119      */
       
   120     void SetIpVersion( const IkeSocket::TIpVersion aIpVersion );
       
   121 
       
   122     /**
       
   123      * Opens sockets. Sockets for ports 500 and 4500 are bound to specified
       
   124      * local IP address.
       
   125      * 
       
   126      * @param aLocalIp Local IP address which is used to bind sockets (for ports
       
   127      *                 500 and 4500). 
       
   128      */
       
   129     TInt OpenSockets( const TInetAddr& aLocalIp );
       
   130     
       
   131     /**
       
   132      * Closes sockets.
       
   133      */
       
   134     void CloseSockets();
       
   135             
       
   136 // from base class MIkeDataInterface
       
   137     
       
   138     /**
       
   139      * Sends UDP data.
       
   140      *
       
   141      * @param aLocalPort Local port
       
   142      * @param aDestAddr Destination IP address/port
       
   143      * @param aUdpData UDP data
       
   144      * @param aDscp DSCP value
       
   145      * @param aStatus Completion status (returned)
       
   146      *                -KErrArgument if local port is neither 500 or 4500, and
       
   147      *                              destination port does not equal local port
       
   148      *                              (equal when Nokia NAT keepalive packet
       
   149      *                              sent)
       
   150      *                -KErrDisconnected if connection is disconnected                                
       
   151      */
       
   152     void SendUdpData( const TInt aLocalPort,
       
   153                       const TInetAddr& aDestAddr,
       
   154                       const TDesC8& aUdpData,
       
   155                       const TUint aDscp,
       
   156                       TRequestStatus& aStatus );
       
   157 
       
   158     /**
       
   159      * Cancels sending.
       
   160      */
       
   161     void CancelSend();
       
   162     
       
   163     /**
       
   164      * Starts to receive UDP data. Completes when data is available or error
       
   165      * has occured. 
       
   166      *
       
   167      * @param aUdpData Received UDP data (returned)
       
   168      * @param aSrcAddr Source IP address/port (returned)
       
   169      * @param aLocalPort Local port (returned)
       
   170      * @param aStatus Completion status (returned)
       
   171      *                  -KErrDisconnected if connection is disconnected
       
   172      */
       
   173     void ReceiveUdpData( HBufC8*& aUdpData,
       
   174                          TInetAddr& aSrcAddr,
       
   175                          TInt& aLocalPort,
       
   176                          TRequestStatus& aStatus  );
       
   177     
       
   178     /**
       
   179      * Cancels receive request.
       
   180      */
       
   181     void CancelReceive();
       
   182 
       
   183     /**
       
   184      * Clears data which has been received.
       
   185      */
       
   186     void ClearReceivedData();
       
   187     
       
   188     /**
       
   189      * Stops receiving. Clears data which has been received.
       
   190      */
       
   191     void StopReceive();
       
   192         
       
   193     /**
       
   194      * Gets local IP address of interface.
       
   195      *
       
   196      * @param aLocalIp Local IP address (returned)
       
   197      * @return Error status. KErrNotFound if address is not found.
       
   198      */
       
   199     TInt GetLocalAddress( TInetAddr& aLocalIp );
       
   200 
       
   201 // from base class MSenderCallback
       
   202 
       
   203     /**
       
   204      * Notification about completed send.
       
   205      */    
       
   206     void SendCompleted( const TInt aStatus );
       
   207     
       
   208 // from base class MReceiverCallback
       
   209 
       
   210     /**
       
   211      * Notification that data has been received. Receiving is continued
       
   212      * automatically by receiver.
       
   213      * 
       
   214      * @param aUdpData Received UDP data. Ownership transferred.
       
   215      * @param aSrcAddr Source address
       
   216      * @param aLocalPort Local port
       
   217      */    
       
   218     void DataReceived( HBufC8* aUdpData,
       
   219                        const TInetAddr& aSrcAddr,
       
   220                        const TInt aLocalPort );
       
   221     
       
   222     /**
       
   223      * Notification about receive error. Receiving has been stopped by receiver.
       
   224      * 
       
   225      * @param aStatus Error status
       
   226      */    
       
   227     void ReceiveError( const TInt aStatus );    
       
   228         
       
   229 private:
       
   230     
       
   231     CDataTransfer( RSocketServ& aSocketServer,
       
   232                    RConnection& aConnection,
       
   233                    CLocalAddressResolver& aLocalAddressResolver,
       
   234                    MDataTransferCallback& aCallback,
       
   235                    MIkeDebug& aDebug );
       
   236     
       
   237     void ConstructL();
       
   238         
       
   239     /**
       
   240      * Opens specified socket.
       
   241      * 
       
   242      * @param aSocket Socket
       
   243      * @return Error value
       
   244      */    
       
   245     TInt OpenSocket( RSocket& aSocket );
       
   246     
       
   247     /**
       
   248      * Binds specified socket to local IP address/port.
       
   249      * @param aSocket Socket
       
   250      * @param aLocalIp Local IP address
       
   251      * @param aLocalPort Local port
       
   252      * @return Error value
       
   253      */    
       
   254     TInt BindSocket( RSocket& aSocket,
       
   255                      const TInetAddr& aLocalIp,
       
   256                      const TInt aLocalPort );
       
   257     
       
   258     /**
       
   259      * Cancels sending.
       
   260      * 
       
   261      * @param aCompletionStatus Client completion status
       
   262      */
       
   263     void DoCancelSend( const TInt aCompletionStatus = KErrCancel );
       
   264 
       
   265     /**
       
   266      * Cancels receiving.
       
   267      * 
       
   268      * @param aCompletionStatus Client completion status
       
   269      */
       
   270     void DoCancelReceive( const TInt aCompletionStatus = KErrCancel );
       
   271 
       
   272     /**
       
   273      * Completes send to client.
       
   274      * 
       
   275      * @param aStatus Send status
       
   276      */
       
   277     void CompleteSendToClient( const TInt aStatus );
       
   278     
       
   279     /**
       
   280      * Completes receive to client.
       
   281      * 
       
   282      * @param aStatus Receive status
       
   283      */
       
   284     void CompleteReceiveToClient( const TInt aStatus );
       
   285     
       
   286     /**
       
   287      * Receives more data if receiving not requested to be stopped.
       
   288      */
       
   289     void ReceiveData();
       
   290 
       
   291     /**
       
   292      * Cleans up data from receive queue.
       
   293      */
       
   294     void CleanupReceiveQueue();    
       
   295     
       
   296 private: // data    
       
   297     
       
   298     /**
       
   299      * Socket server.
       
   300      * Not own.
       
   301      */
       
   302     RSocketServ&                iSocketServer;
       
   303     
       
   304     /**
       
   305      * Connection used for data transfer.
       
   306      * Not own.
       
   307      */
       
   308     RConnection&                iConnection;
       
   309     
       
   310     /**
       
   311      * Pointer to client's request status for sending
       
   312      * Not own.
       
   313      */
       
   314     TRequestStatus*             iClientStatusSend;
       
   315     
       
   316     /**
       
   317      * Pointer to client's request status for receiving.
       
   318      * Not own.
       
   319      */
       
   320     TRequestStatus*             iClientStatusReceive;
       
   321     
       
   322     /**
       
   323      * Pointer to client's heap descriptor pointer (HBufC8*) variable. Variable
       
   324      * will contain received message data, after receive has been completed 
       
   325      * successfully to client.
       
   326      * Not own.
       
   327      */
       
   328     HBufC8**                    iClientMsgReceive;
       
   329     
       
   330     /**
       
   331      * Pointer to client's address variable. Variable will contain source
       
   332      * address from which data has been received, after receive has been
       
   333      * completed successfully to client.
       
   334      * Not own.
       
   335      */
       
   336     TInetAddr*                  iClientSrcAddrReceive;
       
   337 
       
   338     /**
       
   339      * Pointer to client's address variable. Variable will contain local
       
   340      * port from which data has been received, after receive has been
       
   341      * completed successfully to client.
       
   342      * Not own.
       
   343      */
       
   344     TInt*                       iClientLocalPort;
       
   345 
       
   346     /**
       
   347      * Socket for port 500.
       
   348      * Own.
       
   349      */
       
   350     RSocket                     iSocket;
       
   351     
       
   352     /**
       
   353      * Socket for port 4500.
       
   354      * Own.
       
   355      */
       
   356     RSocket                     iSocketNAT;
       
   357     
       
   358     /**
       
   359      * Socket for Nokia NAT port.
       
   360      * Own.
       
   361      */
       
   362     RSocket                     iSocketNokiaNAT;
       
   363     
       
   364     /**
       
   365      * Local Nokia NAT port.
       
   366      * Own.
       
   367      */
       
   368     TInt                        iLocalNokiaNATPort;
       
   369     
       
   370     /**
       
   371      * Informs if sockets are open or not.
       
   372      * Own.
       
   373      */
       
   374     TBool                       iSocketsOpen;
       
   375     
       
   376     /**
       
   377      * Informs if receiving is stopped or not.
       
   378      * Own.
       
   379      */
       
   380     TBool                       iReceivingStopped;
       
   381 
       
   382     /**
       
   383      * Receiver for port 500.
       
   384      * Own.
       
   385      */
       
   386     CReceiver*                  iReceiver;
       
   387     
       
   388     /**
       
   389      * Receiver for port 4500.
       
   390      * Own.
       
   391      */
       
   392     CReceiver*                  iReceiverNAT;
       
   393         
       
   394     /**
       
   395      * Sender.
       
   396      * Own.
       
   397      */
       
   398     CSender*                    iSender;
       
   399     
       
   400     /**
       
   401      * Queue containing received data.
       
   402      * Own.
       
   403      */
       
   404     RArray<TReceiveQueueItem>   iReceiveQueue;
       
   405         
       
   406     /**
       
   407      * Local address resolver.
       
   408      * Not own.
       
   409      */
       
   410     CLocalAddressResolver&      iLocalAddressResolver;
       
   411     
       
   412     /**
       
   413      * IP version.
       
   414      * Own.
       
   415      */
       
   416     IkeSocket::TIpVersion       iIpVersion;
       
   417     
       
   418     /**
       
   419      * Error callback interface.
       
   420      * Not own.
       
   421      */
       
   422     MDataTransferCallback&      iErrorCallback;
       
   423     
       
   424     /**
       
   425      * Debug trace interface.
       
   426      * Not own.
       
   427      */
       
   428     MIkeDebug&                  iDebug;
       
   429     };
       
   430 
       
   431 #endif // C_DATATRANSFER_H