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