wlanutilities/wlansniffer/wlansnifferkeepalive/src/wsfkeepalive.cpp
changeset 38 2dc6da6fb431
child 39 7b3e49e4608a
equal deleted inserted replaced
29:dbe86d96ce5b 38:2dc6da6fb431
       
     1 /*
       
     2 * Copyright (c) 2010 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:
       
    15 * Main implementation of Wlan Sniffer Keepalive application.
       
    16 */
       
    17 
       
    18 // System include files
       
    19 
       
    20 #include <rconnmon.h>
       
    21 
       
    22 // User include files
       
    23 
       
    24 #include "wsfkeepalivetimer.h"
       
    25 #include "wsfkeepaliveconnmon.h"
       
    26 #include "wsfkeepaliveconnmondisc.h"
       
    27 #include "wsfkeepaliveesock.h"
       
    28 #include "wsfkeepalive.h"
       
    29 #include "OstTraceDefinitions.h"
       
    30 #ifdef OST_TRACE_COMPILER_IN_USE
       
    31 #include "wsfkeepaliveTraces.h"
       
    32 #endif
       
    33 
       
    34 // External function prototypes
       
    35 
       
    36 // Local constants
       
    37 
       
    38 // We need to poll for client info every 5 seconds
       
    39 static const int KKeepalivePollInterval = 5000000;
       
    40 
       
    41 // We can close a connection after 5 minutes of inactivity
       
    42 static const int KKeepaliveInactivityInterval = 300;
       
    43 
       
    44 // UID of Wlan sniffer application
       
    45 static const TUid KWlanSnifferUid = { 0x10281CAA };
       
    46 
       
    47 // List of UIDs of clients not considered as "real"
       
    48 static const TUid KDiscardedClientUids[] = 
       
    49     {
       
    50         { 0x2002FF5F }, // Sniffer keepalive process, that is, us
       
    51         KWlanSnifferUid, // Sniffer application (wlansniffer.exe)
       
    52         { 0x101fD9C5 }  // DHCP server (dhcpserv.exe)
       
    53     };
       
    54 
       
    55 // ======== LOCAL FUNCTIONS ========
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 
       
    59 CWsfKeepalive* CWsfKeepalive::NewL()
       
    60     {
       
    61     OstTraceFunctionEntry0( CWSFKEEPALIVE_NEWL_ENTRY );
       
    62     
       
    63     CWsfKeepalive* me = new ( ELeave ) CWsfKeepalive();
       
    64     CleanupStack::PushL( me );
       
    65     me->ConstructL();
       
    66     CleanupStack::Pop( me );
       
    67     
       
    68     OstTraceFunctionExit0( CWSFKEEPALIVE_NEWL_EXIT );
       
    69     return me;
       
    70     }
       
    71 
       
    72 CWsfKeepalive::~CWsfKeepalive()
       
    73     {
       
    74     OstTraceFunctionEntry0( DUP1_CWSFKEEPALIVE_CWSFKEEPALIVE_ENTRY );
       
    75 
       
    76     delete iEsock;
       
    77     delete iConnMonDisc;
       
    78     delete iConnMon;
       
    79     delete iTimer;
       
    80     
       
    81     OstTraceFunctionExit0( DUP1_CWSFKEEPALIVE_CWSFKEEPALIVE_EXIT );
       
    82     }
       
    83 
       
    84 void CWsfKeepalive::TimerExpired( TInt aError )
       
    85     {
       
    86     OstTraceFunctionEntry0( CWSFKEEPALIVE_TIMEREXPIRED_ENTRY );
       
    87 
       
    88     TBool restartTimer = ETrue; // Should we restart timer or not?
       
    89     if ( aError == KErrNone )
       
    90         {
       
    91         // Timer successfully completed, handle it
       
    92         if ( NoRealClients( iConnectionId ) )
       
    93             {
       
    94             TTime now;
       
    95             now.UniversalTime();
       
    96             
       
    97             // Check whether we are moving from EActiveWithClients to
       
    98             // EActiveNoClients
       
    99             if ( iState == EActiveWithClients )
       
   100                 {
       
   101                 OstTrace0(
       
   102                     TRACE_NORMAL,
       
   103                     CWSFKEEPALIVE_TIMEREXPIRED_RESET,
       
   104                     "CWsfKeepalive::TimerExpired Keepalive time reset" );
       
   105         
       
   106                 // Connection had clients, but doesn't anymore. Keepalive
       
   107                 // inactivity time starts now
       
   108                 iKeepaliveStart = now;
       
   109                 }
       
   110             // Check whether keepalive time has been reached
       
   111             else if ( iKeepaliveStart +
       
   112                 TTimeIntervalSeconds( KKeepaliveInactivityInterval ) <= now )
       
   113                 {
       
   114                 OstTrace0(
       
   115                     TRACE_NORMAL,
       
   116                     CWSFKEEPALIVE_TIMEREXPIRED_DONE,
       
   117                     "CWsfKeepalive::TimerExpired Keepalive time expired" );
       
   118                 
       
   119                 // Keepalive time limit expired, connection should be stopped
       
   120                 iEsock->Disconnect();
       
   121                 iConnMonDisc->Disconnect( iConnectionId );
       
   122                 restartTimer = EFalse;
       
   123                 }
       
   124             
       
   125             // There are now no real clients for the connection
       
   126             SetState( EActiveNoClients );
       
   127             }
       
   128         else
       
   129             {
       
   130             // One or more real clients are using the connection
       
   131             SetState( EActiveWithClients );
       
   132             }
       
   133         }
       
   134     else
       
   135         {
       
   136         // Timer not successful, probably because we stopped it
       
   137         restartTimer = EFalse;
       
   138         }
       
   139 
       
   140     if ( restartTimer )
       
   141         {
       
   142         TTimeIntervalMicroSeconds32 interval( KKeepalivePollInterval );
       
   143         iTimer->After( interval );
       
   144         }
       
   145     
       
   146     OstTraceFunctionExit0( CWSFKEEPALIVE_TIMEREXPIRED_EXIT );
       
   147     }
       
   148 
       
   149 void CWsfKeepalive::WlanConnectionOpenedL( TUint aConnectionId, TUint aIapId )
       
   150     {
       
   151     OstTraceFunctionEntry0( CWSFKEEPALIVE_WLANCONNECTIONOPENEDL_ENTRY );
       
   152     
       
   153     OstTraceExt2(
       
   154         TRACE_NORMAL,
       
   155         CWSFKEEPALIVE_WLANCONNECTIONOPENED,
       
   156         "CWsfKeepalive::WlanConnectionOpened;aConnectionId=%u;aIapId=%u",
       
   157         aConnectionId,
       
   158         aIapId );
       
   159 
       
   160     // We are only interested in connections opened by the Wlan Sniffer
       
   161     if ( OpenedByWlanSniffer( aConnectionId ) )
       
   162         {
       
   163         // Start to monitor this connection, and add us as a user to the
       
   164         // connection
       
   165         iConnectionId = aConnectionId;
       
   166         iEsock->ConnectL( aIapId );
       
   167 
       
   168         // Assume there are no real clients yet. Setup timer for polling
       
   169         // when real clients might be added to the connection
       
   170         SetState( EActiveNoClients );
       
   171         iKeepaliveStart.UniversalTime();
       
   172         
       
   173         OstTrace0(
       
   174             TRACE_NORMAL,
       
   175             CWSFKEEPALIVE_WLANCONNECTIONOPENED_RESET,
       
   176             "CWsfKeepalive::WlanConnectionOpened Keepalive time reset" );
       
   177         
       
   178         TTimeIntervalMicroSeconds32 interval( KKeepalivePollInterval );
       
   179         iTimer->After( interval );
       
   180         }
       
   181     
       
   182     OstTraceFunctionExit0( CWSFKEEPALIVE_WLANCONNECTIONOPENEDL_EXIT );
       
   183     }
       
   184 
       
   185 void CWsfKeepalive::WlanConnectionClosed()
       
   186     {
       
   187     OstTraceFunctionEntry0( CWSFKEEPALIVE_WLANCONNECTIONCLOSED_ENTRY );
       
   188 
       
   189     // No need to monitor anything anymore
       
   190     SetState( EInactive );
       
   191     iConnectionId = KInvalidConnectionId;
       
   192     // Stop also the polling timer
       
   193     iTimer->Stop();
       
   194     
       
   195     OstTraceFunctionExit0( CWSFKEEPALIVE_WLANCONNECTIONCLOSED_EXIT );
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // Default constructor
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 CWsfKeepalive::CWsfKeepalive() :
       
   203     iConnectionId( KInvalidConnectionId ),
       
   204     iState( EInactive )
       
   205     {
       
   206     OstTraceFunctionEntry0( CWSFKEEPALIVE_CWSFKEEPALIVE_ENTRY );
       
   207     OstTraceFunctionExit0( CWSFKEEPALIVE_CWSFKEEPALIVE_EXIT );
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // Leaving constructor
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 void CWsfKeepalive::ConstructL()
       
   215     {
       
   216     OstTraceFunctionEntry0( CWSFKEEPALIVE_CONSTRUCTL_ENTRY );
       
   217     
       
   218     iTimer = CWsfKeepaliveTimer::NewL( *this );
       
   219     iConnMon = CWsfKeepaliveConnMon::NewL( *this );
       
   220     iConnMonDisc = CWsfKeepaliveConnMonDisc::NewL();
       
   221     iEsock = CWsfKeepaliveEsock::NewL();
       
   222     
       
   223     OstTraceFunctionExit0( CWSFKEEPALIVE_CONSTRUCTL_EXIT );
       
   224     }
       
   225  
       
   226 // ---------------------------------------------------------------------------
       
   227 // Checks whether the given connection was opened by the Wlan Sniffer
       
   228 // application
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 TBool CWsfKeepalive::OpenedByWlanSniffer( TUint aConnectionId )
       
   232     {
       
   233     OstTraceFunctionEntry0( CWSFKEEPALIVE_OPENEDBYWLANSNIFFER_ENTRY );
       
   234     
       
   235     TBool retVal = EFalse;
       
   236 
       
   237     // Get all clients of this connection
       
   238     TConnMonClientEnumBuf clientInfo;
       
   239     iConnMon->GetClientInfo( clientInfo, aConnectionId );
       
   240 
       
   241     // Check whether Wlan sniffer is one of the clients
       
   242     for ( TInt i( 0 ); i < clientInfo().iCount; ++i )
       
   243         {
       
   244         if ( clientInfo().iUid[i] == KWlanSnifferUid )
       
   245             {
       
   246             // Match found, stop looking
       
   247             retVal = ETrue;
       
   248             break;
       
   249             }
       
   250         }
       
   251     
       
   252     OstTraceExt2(
       
   253         TRACE_NORMAL,
       
   254         CWSFKEEPALIVE_OPENEDBYWLANSNIFFER,
       
   255         "CWsfKeepalive::OpenedByWlanSniffer;aConnectionId=%u;retVal=%u",
       
   256         aConnectionId,
       
   257         retVal );
       
   258     
       
   259     OstTraceFunctionExit0( CWSFKEEPALIVE_OPENEDBYWLANSNIFFER_EXIT );
       
   260     return retVal;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // Checks whether there are any real clients using the given connection
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 TBool CWsfKeepalive::NoRealClients( TUint aConnectionId )
       
   268     {
       
   269     OstTraceFunctionEntry0( CWSFKEEPALIVE_NOREALCLIENTS_ENTRY );
       
   270     
       
   271     // Get all clients of this connection
       
   272     TConnMonClientEnumBuf clientInfo;
       
   273     iConnMon->GetClientInfo( clientInfo, aConnectionId );
       
   274 
       
   275     // Get the client count
       
   276     TInt clientCount = clientInfo().iCount;
       
   277 
       
   278     TInt discardedClientCount = sizeof( KDiscardedClientUids ) / sizeof( TUid );
       
   279     
       
   280     // Decrease count by each non-real client we must discard
       
   281     for ( TInt i( 0 ); i < clientInfo().iCount; ++i )
       
   282         {
       
   283         for ( TInt j( 0 ); j < discardedClientCount; ++j )
       
   284             {
       
   285             if ( clientInfo().iUid[i] == KDiscardedClientUids[j] )
       
   286                 {
       
   287                 OstTrace1(
       
   288                     TRACE_NORMAL,
       
   289                     CWSFKEEPALIVE_NOREALCLIENTS_DISCARD,
       
   290                     "CWsfKeepalive::NoRealClients Client discarded;clientInfo().iUid[i].iUid=%x",
       
   291                     clientInfo().iUid[i].iUid );
       
   292                 --clientCount;
       
   293                 break;
       
   294                 }
       
   295             }
       
   296         }
       
   297 
       
   298     // If we reached zero, there were no real clients
       
   299     TBool retVal = clientCount == 0 ? ETrue : EFalse;
       
   300     OstTraceExt2(
       
   301         TRACE_NORMAL,
       
   302         CWSFKEEPALIVE_NOREALCLIENTS,
       
   303         "CWsfKeepalive::NoRealClients;aConnectionId=%u;retVal=%u",
       
   304         aConnectionId,
       
   305         retVal );
       
   306     
       
   307     OstTraceFunctionExit0( CWSFKEEPALIVE_NOREALCLIENTS_EXIT );
       
   308     return retVal;
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // Sets the given state and traces the transition, if state changed.
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void CWsfKeepalive::SetState( TUint aState )
       
   316     {
       
   317     OstTraceFunctionEntry0( CWSFKEEPALIVE_SETSTATE_ENTRY );
       
   318 
       
   319 #ifdef OST_TRACE_COMPILER_IN_USE
       
   320     if ( aState != iState )
       
   321         {
       
   322         OstTrace1(
       
   323             TRACE_NORMAL,
       
   324             CWSFKEEPALIVE_SETSTATE,
       
   325             "CWsfKeepalive::SetState;aState=%{State}",
       
   326             aState );
       
   327         }
       
   328 #endif
       
   329     iState = aState;
       
   330     
       
   331     OstTraceFunctionExit0( CWSFKEEPALIVE_SETSTATE_EXIT );
       
   332     }