hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/CProtocolTCP.cpp
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     1 /*
       
     2 * Copyright (c) 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:  HTTPProxy protocol implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CProtocolTCP.h"
       
    22 #include "CExprTCPMsg.h"
       
    23 #include "CExprTCPOpen.h"
       
    24 #include "CExprTCPLstn.h"
       
    25 #include "CExprTCPClose.h"
       
    26 #include "CExprTCPCloseAll.h"
       
    27 #include "MSocketWriter.h"
       
    28 #include "MProtocolObserverTCP.h"
       
    29 #include "CommRouterDefinitions.h"
       
    30 
       
    31 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    32 #include "DebugPrint.h"
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CProtocolTCP::CProtocolTCP
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CProtocolTCP::CProtocolTCP( MProtocolObserverTCP* aObserver )
       
    42     : iObserver( aObserver )
       
    43     {
       
    44     __ASSERT_DEBUG( iObserver, User::Invariant() );
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CProtocolTCP::ConstructL
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CProtocolTCP::ConstructL()
       
    52     {
       
    53     // Add all the expressions for TCP
       
    54     iExpressionArray = new (ELeave) CArrayPtrFlat<MBPExpression> ( 5 );
       
    55 
       
    56     CExprTCPMsg* tcpMsg = CExprTCPMsg::NewL( this );
       
    57     iExpressionArray->AppendL( tcpMsg );
       
    58 
       
    59     CExprTCPOpen* tcpOpen = CExprTCPOpen::NewL( this );
       
    60     iExpressionArray->AppendL( tcpOpen );
       
    61 
       
    62     CExprTCPLstn* tcpLstn = CExprTCPLstn::NewL( this );
       
    63     iExpressionArray->AppendL( tcpLstn );
       
    64 
       
    65     CExprTCPClose* tcpClose = CExprTCPClose::NewL( this );
       
    66     iExpressionArray->AppendL( tcpClose );
       
    67 
       
    68     CExprTCPCloseAll* tcpCloseAll = CExprTCPCloseAll::NewL( this );
       
    69     iExpressionArray->AppendL( tcpCloseAll );
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CProtocolTCP::NewL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 CProtocolTCP* CProtocolTCP::NewL( MProtocolObserverTCP* aObserver )
       
    77     {
       
    78     CProtocolTCP* self = CProtocolTCP::NewLC( aObserver );
       
    79     CleanupStack::Pop();
       
    80 
       
    81     return self;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CProtocolTCP::NewLC
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CProtocolTCP* CProtocolTCP::NewLC( MProtocolObserverTCP* aObserver )
       
    89     {
       
    90     CProtocolTCP* self = new( ELeave ) CProtocolTCP( aObserver );
       
    91     CleanupStack::PushL( self );
       
    92 
       
    93     self->ConstructL();
       
    94     return self;
       
    95     }
       
    96 
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CProtocolTCP::~CProtocolTCP
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CProtocolTCP::~CProtocolTCP()
       
   103     {
       
   104     if ( iExpressionArray )
       
   105         {
       
   106         iExpressionArray->ResetAndDestroy();
       
   107         delete iExpressionArray;
       
   108         }
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CProtocolTCP::FrameStarted
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void CProtocolTCP::FrameStarted()
       
   116     {
       
   117     iObserver->FrameStarted();
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CProtocolTCP::FrameParsedL
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CProtocolTCP::FrameParsedL( TUint aPort, const TDesC8& aData )
       
   125     {
       
   126     iObserver->TCPFrameParsedL( aPort, aData );
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CProtocolTCP::ProtocolErrorL
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CProtocolTCP::ProtocolErrorL(
       
   134     TInt aErrorCode, const TDesC8& aReceivedData )
       
   135     {
       
   136     iObserver->ProtocolErrorL( aErrorCode, aReceivedData );
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CProtocolTCP::OpenLocalTCPConnectionL
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CProtocolTCP::OpenLocalTCPConnectionL( TUint aPort )
       
   144     {
       
   145     iObserver->OpenLocalTCPConnectionL( aPort );
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CProtocolTCP::OpenListeningTCPConnectionL
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CProtocolTCP::OpenListeningTCPConnectionL( TUint aPort )
       
   153     {
       
   154     iObserver->OpenListeningTCPConnectionL( aPort );
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CProtocolTCP::CloseTCPConnection
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CProtocolTCP::CloseTCPConnectionL( TUint aPort )
       
   162     {
       
   163     iObserver->CloseTCPConnectionL( aPort );
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CProtocolTCP::ProtocolErrorL
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CProtocolTCP::CloseAllTCPConnections()
       
   171     {
       
   172     iObserver->CloseAllTCPConnections();
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CProtocolTCP::WriteFrameL
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CProtocolTCP::WriteFrameL( MSocketWriter& aSocketWriter,
       
   180             TUint aPeerPort, TUint aOriginalPort, const TDesC8& aData ) const
       
   181     {
       
   182     DEBUG_PRINT( DEBUG_STRING(
       
   183         "CProtocolTCP::WriteFrameL(), peer port=%d, original port=%d" ),
       
   184         aPeerPort, aOriginalPort );
       
   185 
       
   186     TInt bufSize = 0;
       
   187     bufSize += KTCPPrefix().Length();
       
   188     bufSize += KHexDecimalLength;
       
   189     bufSize += KPortSuffix().Length();
       
   190     bufSize += KHexDecimalLength;
       
   191     bufSize += KPortSuffix().Length();
       
   192     bufSize += KHexDecimalLength;
       
   193     bufSize += KLengthSuffix().Length();
       
   194     bufSize += aData.Length();
       
   195     bufSize += KDataSuffix().Length();
       
   196     bufSize += KMessageSuffix().Length(); // ? is this really needed
       
   197 
       
   198     TBuf8<KHexDecimalLength> hexbuf;
       
   199     HBufC8* buf = HBufC8::NewLC( bufSize );
       
   200     TPtr8 ptr( buf->Des() );
       
   201 
       
   202     // Append TCP prefix
       
   203     ptr.Append( KTCPPrefix );
       
   204 
       
   205     // Append peer port
       
   206     hexbuf.Format( KHexFormat, aPeerPort );
       
   207     ptr.Append( hexbuf );
       
   208     ptr.Append( KPortSuffix );
       
   209 
       
   210     // Append host port
       
   211     hexbuf.Format( KHexFormat, aOriginalPort );
       
   212     ptr.Append( hexbuf );
       
   213     ptr.Append( KPortSuffix );
       
   214 
       
   215     // Append data length
       
   216     hexbuf.Format( KHexFormat, aData.Length() );
       
   217     ptr.Append( hexbuf );
       
   218     ptr.Append( KLengthSuffix );
       
   219 
       
   220     // Append data
       
   221     ptr.Append( aData );
       
   222     ptr.Append( KDataSuffix );
       
   223     ptr.Append( KMessageSuffix );
       
   224 
       
   225     // Write to socket
       
   226     aSocketWriter.WriteL( *buf );
       
   227 
       
   228     CleanupStack::PopAndDestroy( buf );
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CProtocolTCP::HandleReceivedDataL
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TBool CProtocolTCP::HandleReceivedDataL( TDes8& aData, TInt& aStartPos,
       
   236                                          TInt& aLength )
       
   237     {
       
   238     TBool msgHandled = EFalse;
       
   239     TInt protCount = iExpressionArray->Count();
       
   240 
       
   241     for ( TInt i = 0; i < protCount; i++ )
       
   242         {
       
   243         msgHandled = iExpressionArray->At( i )->HandleRecievedMsgL( aData,
       
   244                                                                     aStartPos,
       
   245                                                                     aLength);
       
   246         if ( msgHandled )
       
   247             {
       
   248             break;
       
   249             }
       
   250         }
       
   251 
       
   252     return msgHandled;
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CProtocolTCP::SendCloseTCPConnection
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 void CProtocolTCP::SendCloseTCPConnection( MSocketWriter& aSocketWriter,
       
   260         TUint aPort )
       
   261     {
       
   262     DEBUG_PRINT(
       
   263         DEBUG_STRING( "CProtocolTCP::SendCloseTCPConnection, port %d" ),
       
   264         aPort );
       
   265 
       
   266     aSocketWriter.WriteL( KTCPClosePrefix );
       
   267     TBuf8<KHexDecimalLength> portBuf;
       
   268     portBuf.Format( KHexFormat, aPort );
       
   269     aSocketWriter.WriteL( portBuf );
       
   270     }
       
   271 
       
   272 //  End of File