vpnengine/ikev2lib/src/ikev2receiver.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:  Receiver of UDP datagrams
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <in_sock.h>
       
    20 
       
    21 #include "ikedatainterface.h"
       
    22 #include "ikemsgheader.h"
       
    23 
       
    24 // CLASS HEADER
       
    25 #include "ikev2receiver.h"
       
    26 
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Two-phased constructor.
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CIkev2Receiver* CIkev2Receiver::NewL( MIkeDataInterface& aDataInterface,
       
    35                                       MIkev2ReceiverCallback& aCallback )
       
    36     {
       
    37     CIkev2Receiver* self = new (ELeave) CIkev2Receiver( aDataInterface,
       
    38                                                         aCallback );
       
    39     CleanupStack::PushL( self );
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Destructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CIkev2Receiver::~CIkev2Receiver()
       
    50     {
       
    51     StopReceive();
       
    52     
       
    53     delete iUdpData;    
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Constructor.
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CIkev2Receiver::CIkev2Receiver( MIkeDataInterface& aDataInterface,
       
    61                                 MIkev2ReceiverCallback& aCallback )
       
    62  : CActive( EPriorityStandard ),
       
    63    iUdpData( NULL ),
       
    64    iDataInterface( aDataInterface ),
       
    65    iCallback( aCallback )
       
    66     {
       
    67     CActiveScheduler::Add( this );
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // Second phase construction.
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 void CIkev2Receiver::ConstructL()
       
    75     {
       
    76     StartReceive();
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Starts receive.
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void CIkev2Receiver::StartReceive()
       
    84     {
       
    85     iReceivingData = ETrue;
       
    86     DoReceive();
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Stops receive.
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 void CIkev2Receiver::StopReceive()
       
    94     {
       
    95     iReceivingData = EFalse;
       
    96     Cancel();
       
    97     iDataInterface.StopReceive();
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // From class CActive
       
   102 // Handles completion of receive. 
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CIkev2Receiver::RunL()
       
   106     {
       
   107     if ( iStatus.Int() == KErrNone )
       
   108         {
       
   109         __ASSERT_DEBUG( iUdpData != NULL,
       
   110                         User::Invariant() );
       
   111         
       
   112         const ThdrISAKMP* ikeHdr = ThdrISAKMP::Ptr( iUdpData->Des() );
       
   113         TInt msgLth = iUdpData->Length();
       
   114         
       
   115         // Ignore possible <non-ESP marker> in the beginning of IKE message.
       
   116         TUint32 ikeMsgHdrOctets = GET32( ikeHdr );
       
   117         if ( ikeMsgHdrOctets == NON_ESP_MARKER )
       
   118             {
       
   119             ikeHdr  = ikeHdr->GotoOffset( NON_ESP_MARKER_SIZE );
       
   120             msgLth -= NON_ESP_MARKER_SIZE;
       
   121             }
       
   122         
       
   123         iCallback.IkeMsgReceived( *ikeHdr, iSrcAddr, iLocalPort );                
       
   124         }
       
   125     
       
   126     delete iUdpData;
       
   127     iUdpData = NULL;
       
   128     
       
   129     if ( iStatus.Int() == KErrNone )
       
   130         {
       
   131         if ( iReceivingData )
       
   132             {
       
   133             // Continue receiving.
       
   134             DoReceive();
       
   135             }
       
   136         }
       
   137     else
       
   138         {
       
   139         iCallback.ReceiveError( iStatus.Int() );
       
   140         }
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // From class CActive
       
   145 // Handles cancellation of receive. 
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CIkev2Receiver::DoCancel()
       
   149     {
       
   150     iDataInterface.CancelReceive();
       
   151     
       
   152     delete iUdpData;
       
   153     iUdpData = NULL;
       
   154     }
       
   155     
       
   156 // ---------------------------------------------------------------------------
       
   157 // Receives UDP data. 
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 void CIkev2Receiver::DoReceive()
       
   161     {
       
   162     iDataInterface.ReceiveUdpData( iUdpData, iSrcAddr, iLocalPort, iStatus );
       
   163     SetActive();
       
   164     }
       
   165