multimediacommsengine/mmcesrv/mmceserver/src/mcelocaladdrresolver.cpp
changeset 0 1bce908db942
child 59 b0e4b01681c5
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007 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 
       
    19 
       
    20 
       
    21 #include "mcelocaladdrresolver.h"
       
    22 #include <es_sock.h>
       
    23 #include <commdbconnpref.h>
       
    24 
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CMceLocalAddrResolver::NewL
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CMceLocalAddrResolver* CMceLocalAddrResolver::NewL(
       
    31     RSocketServ& aSocketServ,
       
    32     TUint32 aIapId )
       
    33 	{
       
    34 	CMceLocalAddrResolver* self = NewLC( aSocketServ, aIapId );
       
    35 	CleanupStack::Pop();
       
    36 	return self;	
       
    37 	}
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CMceLocalAddrResolver::NewLC
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CMceLocalAddrResolver* CMceLocalAddrResolver::NewLC(
       
    44     RSocketServ& aSocketServ,
       
    45     TUint32 aIapId )
       
    46 	{
       
    47 	CMceLocalAddrResolver* self = 
       
    48 	    new( ELeave )CMceLocalAddrResolver( aSocketServ );
       
    49 	CleanupStack::PushL(self);
       
    50 	self->ConstructL( aIapId );
       
    51 	return self;
       
    52 	}
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CMceLocalAddrResolver::CMceLocalAddrResolver
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CMceLocalAddrResolver::CMceLocalAddrResolver( RSocketServ& aSocketServ )
       
    59     : iSocketServ( aSocketServ )
       
    60 	{
       
    61 	}
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CMceLocalAddrResolver::ConstructL
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CMceLocalAddrResolver::ConstructL( TUint32 aIapId )
       
    68     {
       
    69 	// The actual IAP ID is fetched dynamically 
       
    70 	// to make local IP address lookup work in a case
       
    71 	// where there are several IAPs used which have different IDs 
       
    72 	// but all point to the same physical AP.
       
    73     
       
    74     RConnection connection;
       
    75     User::LeaveIfError( connection.Open( iSocketServ ) );
       
    76     CleanupClosePushL( connection );
       
    77     
       
    78     // The connection has to be started to get the actual IAP ID from it
       
    79     // At this point the physical connection should be already up
       
    80     // as the related SIP profile has been registered using it and
       
    81     // the synchronous RConnection::Start should return quickly and
       
    82     // should not hang the whole MCE server process for too long
       
    83     TCommDbConnPref prefs;
       
    84     prefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
    85     prefs.SetDirection( ECommDbConnectionDirectionOutgoing );
       
    86     prefs.SetIapId( aIapId );
       
    87     connection.Start( prefs );    
       
    88     
       
    89     // Fetch the actual IAP ID from the started connection
       
    90 	_LIT( KIapIdSetting, "IAP\\Id" );
       
    91 	User::LeaveIfError( connection.GetIntSetting( KIapIdSetting, iIapId ) );
       
    92 	
       
    93 	CleanupStack::PopAndDestroy(); // connection
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CMceLocalAddrResolver::~CMceLocalAddrResolver
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CMceLocalAddrResolver::~CMceLocalAddrResolver()
       
   101 	{
       
   102 	}
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CMceLocalAddrResolver::Addr
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 const TInetAddr& CMceLocalAddrResolver::Addr() const
       
   109     {
       
   110     return iAddr;
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CMceLocalAddrResolver::HasIPv4Addr
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 TBool CMceLocalAddrResolver::HasIPv4Addr() const
       
   118     {
       
   119     return (!iIPv4Addr.IsUnspecified());
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CMceLocalAddrResolver::IPv4Addr
       
   124 // -----------------------------------------------------------------------------
       
   125 //	
       
   126 const TInetAddr& CMceLocalAddrResolver::IPv4Addr() const
       
   127     {
       
   128     return iIPv4Addr;
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CMceLocalAddrResolver::RefreshLocalAddressesL
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CMceLocalAddrResolver::RefreshLocalAddressesL()
       
   136     {
       
   137     RSocket socket;
       
   138 	User::LeaveIfError( socket.Open( iSocketServ, KAfInet, KSockDatagram, 
       
   139 		                             KProtocolInetUdp ) );
       
   140 
       
   141 	iAddr.Init( KAfInet );
       
   142 	iIPv4Addr.Init( KAfInet );
       
   143 
       
   144 	if ( socket.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl ) == KErrNone )
       
   145 		{
       
   146 		TPckgBuf<TSoInetInterfaceInfo> info;
       
   147 		while ( socket.GetOpt( KSoInetNextInterface,
       
   148 		                       KSolInetIfCtrl, info ) == KErrNone )
       
   149 			{
       
   150 			TPckgBuf<TSoInetIfQuery> query;
       
   151 			query().iName = info().iName;
       
   152 			if ( socket.GetOpt( KSoInetIfQueryByName,
       
   153 			                    KSolInetIfQuery, query ) == KErrNone )
       
   154 				{
       
   155 				if ( Match( info(), query() ) )
       
   156 				    {
       
   157                     SetAddressesIfNotSet( info().iAddress );
       
   158 				    }
       
   159 				}
       
   160 			}
       
   161 		}
       
   162 		
       
   163 	socket.Close();
       
   164 	}	
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CMceLocalAddrResolver::Match
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TBool CMceLocalAddrResolver::Match(
       
   171     const TSoInetInterfaceInfo& aInfo,
       
   172     const TSoInetIfQuery& aQuery) const
       
   173     {
       
   174     TBool match = EFalse;
       
   175     if ( !aInfo.iAddress.IsUnspecified() && 
       
   176          !aInfo.iAddress.IsLoopback() &&
       
   177          !aInfo.iAddress.IsLinkLocal() &&
       
   178          aQuery.iZone[1] == iIapId )
       
   179         {
       
   180         match = ETrue;
       
   181         }
       
   182     return match;
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CMceLocalAddrResolver::SetAddressesIfNotSet
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CMceLocalAddrResolver::SetAddressesIfNotSet( const TInetAddr& aAddr )
       
   190     {
       
   191     if ( aAddr.Family() == KAfInet )
       
   192         {
       
   193         if ( iAddr.IsUnspecified() )
       
   194             {
       
   195             iAddr = aAddr;
       
   196             }
       
   197         iIPv4Addr = aAddr;
       
   198         }
       
   199     else
       
   200         {
       
   201         iAddr = aAddr;
       
   202         }
       
   203     if ( iIPv4Addr.IsUnspecified() && iAddr.IsV4Mapped() )
       
   204         {
       
   205         iIPv4Addr = iAddr;
       
   206         iIPv4Addr.ConvertToV4();
       
   207         }    
       
   208     }
       
   209