realtimenetprots/sipfw/SIP/ConnectionMgr/src/CLocalAddrResolver.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : CLocalAddrResolver.cpp
       
    15 // Part of       : ConnectionMgr
       
    16 // Version       : SIP/5.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "CLocalAddrResolver.h"
       
    23 #include "SipLogs.h"
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CLocalAddrResolver::NewL
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 CLocalAddrResolver* CLocalAddrResolver::NewL(
       
    30     RSocketServ& aServer,
       
    31     RConnection& aConnection )
       
    32 	{
       
    33 	CLocalAddrResolver* self = NewLC( aServer, aConnection );
       
    34 	CleanupStack::Pop();
       
    35 	return self;	
       
    36 	}
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CLocalAddrResolver::NewLC
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CLocalAddrResolver* CLocalAddrResolver::NewLC(
       
    43     RSocketServ& aServer,
       
    44     RConnection& aConnection )
       
    45 	{
       
    46 	CLocalAddrResolver* self = 
       
    47 	    new( ELeave ) CLocalAddrResolver( aServer, aConnection );
       
    48 	CleanupStack::PushL( self );
       
    49 	self->ConstructL();
       
    50 	return self;	
       
    51 	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CLocalAddrResolver::CLocalAddrResolver
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CLocalAddrResolver::CLocalAddrResolver(
       
    58     RSocketServ& aServer,
       
    59     RConnection& aConnection )
       
    60 	: iServer( aServer ),
       
    61 	  iConnection( aConnection )
       
    62 	{
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CLocalAddrResolver::~CLocalAddrResolver
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CLocalAddrResolver::~CLocalAddrResolver()
       
    70 	{
       
    71 	}
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CLocalAddrResolver::ConstructL
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CLocalAddrResolver::ConstructL()
       
    78 	{
       
    79 	_LIT( KIap, "IAP\\Id" );
       
    80 	// The following is done to make local IP address lookup work in a case
       
    81 	// where there are several IAPs used which have different IDs but all
       
    82 	// point to the same physical AP. For each CSipConnection instance, their
       
    83 	// iLocalAddrResolver->iIapId will be the correct IAP's ID after the
       
    84 	// following function call is complete.
       
    85 	User::LeaveIfError( iConnection.GetIntSetting( KIap, iIapId ) );
       
    86 	}
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CLocalAddrResolver::Addr
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 const TInetAddr& CLocalAddrResolver::IPv6Addr() const
       
    93     {
       
    94     return iIPv6Addr;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CLocalAddrResolver::HasIPv4Addr
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 TBool CLocalAddrResolver::HasIPv4Addr() const
       
   102     {
       
   103     return ( !iIPv4Addr.IsUnspecified() );
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CLocalAddrResolver::IPv4Addr
       
   108 // -----------------------------------------------------------------------------
       
   109 //	
       
   110 const TInetAddr& CLocalAddrResolver::IPv4Addr() const
       
   111     {
       
   112     return iIPv4Addr;
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CLocalAddrResolver::IsLocalAddr
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 TBool CLocalAddrResolver::IsLocalAddr(const TInetAddr& aAddr) const
       
   120     {
       
   121     return ( aAddr.CmpAddr( iIPv6Addr ) || aAddr.CmpAddr( iIPv4Addr ) );
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CLocalAddrResolver::RefreshLocalAddressesL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CLocalAddrResolver::RefreshLocalAddressesL()
       
   129 	{	
       
   130 	RSocket socket;
       
   131 	User::LeaveIfError( socket.Open(
       
   132 	    iServer, KAfInet, KSockDatagram, KProtocolInetUdp, iConnection ) );
       
   133 
       
   134 	iIPv6Addr.Init( KAFUnspec );
       
   135 	iIPv4Addr.Init( KAFUnspec );
       
   136 	
       
   137     TInt err = socket.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl );
       
   138 	if ( err == KErrNone )
       
   139 		{
       
   140 		TPckgBuf<TSoInetInterfaceInfo> info;
       
   141 		err = socket.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, info );
       
   142 		while ( err == KErrNone )
       
   143 			{
       
   144 			TPckgBuf<TSoInetIfQuery> query;
       
   145 			query().iName = info().iName;
       
   146 			err = socket.GetOpt( KSoInetIfQueryByName, KSolInetIfQuery, query );
       
   147 			if ( err == KErrNone )
       
   148 				{
       
   149 				if ( Match(info(), query() ) )
       
   150 				    {
       
   151                     SetAddress( info().iAddress );
       
   152 				    }
       
   153 				}
       
   154 		    err = socket.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, info );
       
   155 			}
       
   156 		}
       
   157 	socket.Close();
       
   158 	if ( err == KErrNoMemory )
       
   159 	    {
       
   160 	    User::LeaveNoMemory();
       
   161 	    }
       
   162 	}	
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CLocalAddrResolver::Match
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 TBool CLocalAddrResolver::Match(
       
   169     const TSoInetInterfaceInfo& aInfo,
       
   170     const TSoInetIfQuery& aQuery ) const
       
   171     {
       
   172     TBool match = EFalse;
       
   173     if (!aInfo.iAddress.IsUnspecified() && 
       
   174         !aInfo.iAddress.IsLoopback() &&
       
   175         !aInfo.iAddress.IsLinkLocal() &&
       
   176         aQuery.iZone[1] == iIapId )
       
   177         {
       
   178         match = ETrue;
       
   179         }
       
   180     return match;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CLocalAddrResolver::SetAddress
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CLocalAddrResolver::SetAddress( const TInetAddr& aAddr )
       
   188     {    
       
   189     if ( aAddr.Address() != 0 )
       
   190         {
       
   191         iIPv4Addr = aAddr;     
       
   192         iIPv4Addr.ConvertToV4();
       
   193         }
       
   194     else
       
   195         {
       
   196         iIPv6Addr = aAddr;    
       
   197         }         
       
   198     }