upnp/upnpstack/upnpconnmanager/src/upnpconninterfacewatcher.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2007-2008 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 *
       
    16 */
       
    17 
       
    18 #include "upnpipaddressutils.h"
       
    19 #include "upnpipeventnotifier.h"
       
    20 #include "upnpconninterfacewatcher.h"
       
    21 #include "upnpsettings.h"
       
    22 #define KLogFile _L("UPnPConnManager.txt")
       
    23 #include "upnpcustomlog.h"
       
    24 
       
    25 using namespace UpnpIpAddressUtil;
       
    26 // ---------------------------------------------------------------------------
       
    27 // CUpnpConnInterfaceWatcher::NewL
       
    28 //
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CUpnpConnInterfaceWatcher* CUpnpConnInterfaceWatcher::NewL(
       
    32     MUpnpConnInterfaceWatcherObserver& aIfObserver, 
       
    33     RSocketServ& aSockServ,
       
    34     RConnection &aConnection )
       
    35     {
       
    36     CUpnpConnInterfaceWatcher* self = new (ELeave) CUpnpConnInterfaceWatcher( 
       
    37                                             aIfObserver, aSockServ, aConnection );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CUpnpConnInterfaceWatcher::CUpnpConnInterfaceWatcher
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CUpnpConnInterfaceWatcher::CUpnpConnInterfaceWatcher(
       
    51     MUpnpConnInterfaceWatcherObserver& aIfObserver, 
       
    52     RSocketServ& aSockServ,
       
    53     RConnection &aConnection ) 
       
    54     : CActive( CActive::EPriorityStandard ),
       
    55       iIfObserver( aIfObserver ),
       
    56       iSockServ( aSockServ ),
       
    57       iConnection( aConnection )
       
    58     {
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CUpnpConnInterfaceWatcher::~CUpnpConnInterfaceWatcher
       
    63 // Destructor.
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CUpnpConnInterfaceWatcher::~CUpnpConnInterfaceWatcher()
       
    67     {
       
    68     Cancel();
       
    69     delete iIPEvent;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CUpnpConnInterfaceWatcher::ConstructL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CUpnpConnInterfaceWatcher::ConstructL()
       
    77     {
       
    78     LOG_FUNC_NAME;
       
    79     CActiveScheduler::Add( this );
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CUpnpConnInterfaceWatcher::SubscribeForConnectionServiceChangeNotifications
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CUpnpConnInterfaceWatcher::SubscribeForConnectionServiceChangeNotifications()
       
    87     {
       
    88     if ( !IsActive() )
       
    89         {
       
    90         iConnection.ServiceChangeNotification( iNewISPId, iNewServiceType, iStatus );
       
    91         SetActive();
       
    92         }
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CUpnpConnInterfaceWatcher::CancelSubscriptionForConnectionServiceChangeNotifications
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 void CUpnpConnInterfaceWatcher::CancelSubscriptionForConnectionServiceChangeNotifications()
       
   100     {
       
   101     Cancel();
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CUpnpConnInterfaceWatcher::ListenAddressChangeL
       
   106 // Creates and subscribes to CUpnpIPEventNotifier instance
       
   107 // -----------------------------------------------------------------------------
       
   108 //  
       
   109 void CUpnpConnInterfaceWatcher::ListenAddressChangeL( TUint aIapId )
       
   110     {
       
   111     LOG_FUNC_NAME;
       
   112     iIapId = aIapId;
       
   113     LOGS1("ListenAddressChangeL: Given IAP: %d",iIapId);
       
   114     // get local IP                
       
   115     TInt interfaceIndex; // interface index used for subscribing for IP event
       
   116 
       
   117     SetLocalAddress( LocalIpAddress( interfaceIndex ) );
       
   118     
       
   119     delete iIPEvent;
       
   120     iIPEvent = NULL;
       
   121     
       
   122 #ifdef _DEBUG
       
   123     LOGS1( "ListenAddressChangeL: interfaceIndex: %d", interfaceIndex );
       
   124     TBuf<50> buf;
       
   125     iLocalIpAddress.Output( buf );
       
   126     LOGS1( "ListenAddressChangeL: IP Addr: %S", &buf );
       
   127 #endif //_DEBUG
       
   128     
       
   129     iIPEvent = CUpnpIPEventNotifier::NewL( interfaceIndex );
       
   130     iIPEvent->Subscribe( this );
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CUpnpConnInterfaceWatcher::LocalAddress
       
   135 // -----------------------------------------------------------------------------
       
   136 //  
       
   137 const TInetAddr& CUpnpConnInterfaceWatcher::LocalAddress()
       
   138     {
       
   139     return iLocalIpAddress;
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CUpnpConnInterfaceWatcher::SetLocalAddress
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 void CUpnpConnInterfaceWatcher::SetLocalAddress( const TInetAddr& aAddress )
       
   147     {
       
   148     iLocalIpAddress.SetAddress( aAddress.Address() );
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CUpnpConnInterfaceWatcher::IPEventOccursL
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CUpnpConnInterfaceWatcher::IPEventOccursL()
       
   156     {            
       
   157     TInt index;
       
   158 
       
   159     TInetAddr ip = LocalIpAddress( index );
       
   160 #ifdef _DEBUG
       
   161     TBuf<KMaxName> address;
       
   162     ip.Output(address);       
       
   163     LOGS1H( iHandle, "MH IPEvent: %S", &address );
       
   164     RDebug::Print(_L("CUpnpConnInterfaceWatcher *** IPEventOccursL *** IP Address: %S"), &address );
       
   165 #endif //_DEBUG
       
   166            
       
   167     if ( ip.Address() == 0 )
       
   168         {
       
   169         return;
       
   170         }
       
   171            
       
   172     if ( ip.Address() != LocalAddress().Address() )
       
   173         {       
       
   174         SetLocalAddress( ip );
       
   175         iIfObserver.InterfaceAddressChange( iLocalIpAddress );
       
   176         }    
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CUpnpConnInterfaceWatcher::RunL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CUpnpConnInterfaceWatcher::RunL()
       
   184     {
       
   185     if ( iStatus.Int() == KErrCancel )
       
   186         {
       
   187         iIfObserver.WlanNetworkLost();
       
   188         }
       
   189     else if ( iStatus.Int() >= KErrNone )
       
   190         {
       
   191         SubscribeForConnectionServiceChangeNotifications();
       
   192         }
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CUpnpConnInterfaceWatcher::DoCancel
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CUpnpConnInterfaceWatcher::DoCancel()
       
   200     {
       
   201     iConnection.CancelServiceChangeNotification();
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CUpnpConnInterfaceWatcher::RunError
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 TInt CUpnpConnInterfaceWatcher::RunError( TInt /*aError*/ )
       
   209     {
       
   210     LOG_FUNC_NAME;
       
   211     return KErrNone;
       
   212     }
       
   213 
       
   214 TInetAddr CUpnpConnInterfaceWatcher::LocalIpAddress(TInt& aInterfaceIndex)
       
   215     {
       
   216     LOG_FUNC_NAME;
       
   217     
       
   218     RSocket socket;
       
   219     TInt err;
       
   220     TInt index; 
       
   221     TInt autoIfIndex = 0;
       
   222     TBool autoIpFound = EFalse;
       
   223     TBool ipFound = EFalse;
       
   224     TInetAddr localIpAddress;        
       
   225     TInetAddr autoIpAddress;
       
   226     
       
   227     TInt ret= socket.Open( iSockServ, KAfInet, KSockDatagram, KProtocolInetUdp);
       
   228     if ( KErrNone == ret )
       
   229           {
       
   230           err = socket.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl );
       
   231            
       
   232           if ( err == KErrNone )
       
   233                 {
       
   234                 TPckgBuf<TSoInetInterfaceInfo> info;
       
   235                 err = socket.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, info );
       
   236                 while ( err == KErrNone )
       
   237                     {
       
   238                     TPckgBuf<TSoInetIfQuery> query;
       
   239                     query().iName = info().iName;
       
   240                     err = socket.GetOpt( KSoInetIfQueryByName, KSolInetIfQuery, query );
       
   241                     if ( err == KErrNone )
       
   242                         {
       
   243                         index = query().iIndex;
       
   244                         
       
   245                           //Address should be IPv4, not 0.0.0.0 & not loopback.
       
   246                           if ((info().iAddress.Address() != 0) && !info().iAddress.IsUnspecified() && !info().iAddress.IsLoopback() && query().iZone[1] == iIapId)
       
   247                             {
       
   248                             
       
   249                         #ifdef _DEBUG
       
   250                              LOGS1( "LocalIpAddress: IAP is matched with %d",query().iZone[1] );
       
   251                              TBuf<50> buf;
       
   252                              info().iAddress.Output( buf );
       
   253                              LOGS1( "LocalIpAddress: info().iAddress: %S", &buf );
       
   254                         #endif
       
   255         
       
   256                              if ( info().iAddress.IsLinkLocal() )
       
   257                                  {
       
   258                                  autoIpFound = ETrue;
       
   259                                  autoIpAddress = info().iAddress;
       
   260                                  autoIpAddress.ConvertToV4();
       
   261                                  autoIfIndex = index;
       
   262                                  LOGS("LocalIpAddress: IP is Matched with AutoIP");
       
   263                             #ifdef _DEBUG
       
   264                                  TBuf<50> buf;
       
   265                                  autoIpAddress.Output( buf );
       
   266                                  LOGS1( "LocalIpAddress: Auto IP Addr: %S", &buf );
       
   267                             #endif
       
   268                                  }
       
   269                              // Other than auto IP
       
   270                              else
       
   271                                  {
       
   272                                  ipFound = ETrue;
       
   273                                  localIpAddress = info().iAddress;
       
   274                                  // we're using ipv4 addresses
       
   275                                  localIpAddress.ConvertToV4();
       
   276                                  aInterfaceIndex = index;
       
   277                                  LOGS("LocalIpAddress: IP is Matched with DHCP IP");
       
   278                             #ifdef _DEBUG
       
   279                                  TBuf<50> buf;
       
   280                                  localIpAddress.Output( buf );
       
   281                                  LOGS1( "LocalIpAddress: DHCP IP Addr: %S", &buf );
       
   282                             #endif
       
   283                                  }
       
   284                 
       
   285                             }
       
   286                         }
       
   287                     err = socket.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, info );
       
   288                     }
       
   289                 }
       
   290           }
       
   291     socket.Close();
       
   292     
       
   293     if ( !ipFound && autoIpFound )
       
   294         {
       
   295         localIpAddress = autoIpAddress;
       
   296         aInterfaceIndex = autoIfIndex;
       
   297         }
       
   298     
       
   299 #ifdef _DEBUG
       
   300     TBuf<50> buf;
       
   301     localIpAddress.Output( buf );
       
   302     LOGS1( "LocalIpAddress: IP Addr: %S", &buf );
       
   303 #endif
       
   304     
       
   305     return localIpAddress;  
       
   306     
       
   307     }
       
   308 
       
   309 
       
   310 
       
   311 
       
   312 //End of file