hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/CExprUDPMsg.cpp
changeset 0 a03f92240627
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     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:  IPProxy UDP protocol expression for a regular UDP message
       
    15 *                connection.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CExprUDPMsg.h"
       
    23 #include "CommRouterDefinitions.h"
       
    24 #include "MExpressionObserver.h"
       
    25 
       
    26 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    27 #include "DebugPrint.h"
       
    28 
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CExprUDPMsg::CExprUDPMsg
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CExprUDPMsg::CExprUDPMsg( MExpressionObserver* aObserver )
       
    37     : iObserver( aObserver )
       
    38     {
       
    39     __ASSERT_DEBUG( iObserver, User::Invariant() );
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CExprUDPMsg::NewL
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CExprUDPMsg* CExprUDPMsg::NewL( MExpressionObserver* aObserver )
       
    47     {
       
    48     CExprUDPMsg* self = CExprUDPMsg::NewLC( aObserver );
       
    49     CleanupStack::Pop();
       
    50 
       
    51     return self;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CExprUDPMsg::NewLC
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CExprUDPMsg* CExprUDPMsg::NewLC( MExpressionObserver* aObserver )
       
    59     {
       
    60     CExprUDPMsg* self = new( ELeave ) CExprUDPMsg( aObserver );
       
    61     CleanupStack::PushL( self );
       
    62 
       
    63     return self;
       
    64     }
       
    65 
       
    66 
       
    67 // Destructor
       
    68 CExprUDPMsg::~CExprUDPMsg()
       
    69     {
       
    70     }
       
    71 
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CExprUDPMsg::HandleReceivedDataL()
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 TBool CExprUDPMsg::HandleRecievedMsgL( TDes8& aData, TInt& aStartPos, TInt& aLength )
       
    78     {
       
    79     // Check if the prefix matches
       
    80     aStartPos = aData.Find( KUDPPrefix );
       
    81 
       
    82     if ( aStartPos != KErrNotFound  )
       
    83         {
       
    84         // Found a matching prefix
       
    85         // Let the observer know
       
    86         iObserver->FrameStarted();
       
    87 
       
    88         TPtr8 dataToParse( aData.MidTPtr( aStartPos ) );
       
    89 
       
    90         TInt err = TryParsingL( dataToParse, aLength );
       
    91 
       
    92         if ( err != KErrNone )
       
    93             {
       
    94             // corrupted data in the frame
       
    95             iObserver->ProtocolErrorL( err, aData );
       
    96             // delete the corrupted data
       
    97             aData.SetLength( 0 );
       
    98             }
       
    99 
       
   100         return ETrue;
       
   101         }
       
   102     else
       
   103         {
       
   104         return EFalse;
       
   105         }
       
   106 
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CExprUDPMsg::TryParsingL
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TInt CExprUDPMsg::TryParsingL( TDes8& aData, TInt& aLength )
       
   114     {
       
   115     __ASSERT_ALWAYS( aData.Left( KUDPPrefix().Length() ) == KUDPPrefix,
       
   116         User::Panic( _L("Protocol"), 1 ) );
       
   117 
       
   118     // UDP:0123,000e,[Some test data]
       
   119     TInt frameOverhead =
       
   120         KUDPPrefix().Length() +
       
   121         KHexDecimalLength +
       
   122         KPortSuffix().Length() +
       
   123         KHexDecimalLength +
       
   124         KLengthSuffix().Length() +
       
   125         KDataSuffix().Length() +
       
   126         KMessageSuffix().Length();
       
   127 
       
   128     if ( aData.Length() >= frameOverhead )
       
   129         {
       
   130         TPtrC8 portPtr(
       
   131             aData.Mid( KUDPPrefix().Length(), KHexDecimalLength ) );
       
   132 
       
   133         TLex8 portLexer( portPtr );
       
   134         TUint port;
       
   135         if ( portLexer.Val( port, EHex ) != KErrNone )
       
   136             {
       
   137             return KErrCorrupt;
       
   138             }
       
   139         DEBUG_PRINT( DEBUG_STRING( "CExprUDPMsg::TryParsingL, port = %d" ), port );
       
   140 
       
   141         //Check port suffix
       
   142         if ( aData.Mid( KUDPPrefix().Length() +
       
   143             KHexDecimalLength, KPortSuffix().Length() ) != KPortSuffix )
       
   144             {
       
   145             return KErrCorrupt;
       
   146             }
       
   147 
       
   148         TPtrC8 lengthPtr( aData.Mid( KUDPPrefix().Length() +
       
   149             KHexDecimalLength + KPortSuffix().Length(), KHexDecimalLength ) );
       
   150         TLex8 lengthLexer( lengthPtr );
       
   151         TUint length;
       
   152         if ( lengthLexer.Val( length, EHex ) != KErrNone )
       
   153             {
       
   154             return KErrCorrupt;
       
   155             }
       
   156         DEBUG_PRINT( DEBUG_STRING( "CExprUDPMsg::TryParsingL, length = %d" ), length );
       
   157 
       
   158         //Check length suffix
       
   159         if ( aData.Mid(
       
   160             KUDPPrefix().Length() +
       
   161             KHexDecimalLength +
       
   162             KPortSuffix().Length() +
       
   163             KHexDecimalLength, KLengthSuffix().Length() ) != KLengthSuffix )
       
   164             {
       
   165             return KErrCorrupt;
       
   166             }
       
   167 
       
   168         DEBUG_PRINT( DEBUG_STRING( "CExprUDPMsg::TryParsingL, parsing data" ), length );
       
   169 
       
   170         if ( aData.Length() >= TInt( frameOverhead + length ) )
       
   171             {
       
   172             TInt messagePos = KUDPPrefix().Length() +
       
   173                 KHexDecimalLength +
       
   174                 KPortSuffix().Length() +
       
   175                 KHexDecimalLength +
       
   176                 KLengthSuffix().Length();
       
   177 
       
   178             TPtrC8 message( aData.Mid( messagePos, length ) );
       
   179             if ( aData.Mid( messagePos + length,
       
   180                 KDataSuffix().Length() ) != KDataSuffix )
       
   181                 {
       
   182                 return KErrCorrupt;
       
   183                 }
       
   184             DEBUG_PRINT( DEBUG_STRING( "CExprUDPMsg::TryParsingL, message OK" ) );
       
   185 
       
   186             if ( aData.Mid( messagePos + length + KDataSuffix().Length(),
       
   187                 KMessageSuffix().Length() ) != KMessageSuffix )
       
   188                 {
       
   189                 return KErrCorrupt;
       
   190                 }
       
   191 
       
   192             // send parsed results
       
   193             iObserver->FrameParsedL( port, message );
       
   194             // set the length of the handled message
       
   195             aLength = frameOverhead + length;
       
   196 
       
   197             return KErrNone;
       
   198             }
       
   199 
       
   200         }
       
   201     return KErrNone;
       
   202     }
       
   203 
       
   204 //  End of File