voipplugins/ipapputils/src/cipapputilsaddressresolver.cpp
branchRCL_3
changeset 22 d38647835c2e
parent 0 a4daefaec16c
equal deleted inserted replaced
21:f742655b05bf 22:d38647835c2e
       
     1 /*
       
     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:  Address resolver utility class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <wlaninternalpskeys.h>
       
    20 #include <cmmanagerext.h>
       
    21 #include <cmpluginwlandef.h>
       
    22 
       
    23 #include "cipapputilsaddressresolver.h"
       
    24 
       
    25 const TInt KWlanMacAddressPos = 2;
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // Constructor
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CIPAppUtilsAddressResolver::CIPAppUtilsAddressResolver()
       
    34     {
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 2nd phase constructor
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CIPAppUtilsAddressResolver::ConstructL()
       
    42     {
       
    43     // Connect to Socket server.
       
    44     User::LeaveIfError( iSocketServer.Connect() );
       
    45     
       
    46     // Connect to Socket.
       
    47     User::LeaveIfError( iSocket.Open( iSocketServer,
       
    48                                       KAfInet,
       
    49                                       KSockStream,
       
    50                                       KProtocolInetTcp ) );
       
    51 
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // Constructor
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CIPAppUtilsAddressResolver* CIPAppUtilsAddressResolver::NewL()
       
    59     {
       
    60     CIPAppUtilsAddressResolver* self = CIPAppUtilsAddressResolver::NewLC();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // Constructor
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CIPAppUtilsAddressResolver* CIPAppUtilsAddressResolver::NewLC()
       
    70     {
       
    71     CIPAppUtilsAddressResolver* self = new( ELeave ) CIPAppUtilsAddressResolver;
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     return self;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // Destructor
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CIPAppUtilsAddressResolver::~CIPAppUtilsAddressResolver()
       
    82     {
       
    83     iSocket.Close();
       
    84     iSocketServer.Close();
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CIPAppUtilsAddressResolver::GetWlanMACAddress
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 EXPORT_C TInt CIPAppUtilsAddressResolver::GetWlanMACAddress (
       
    92     TDes8& aWlanMacAddress,
       
    93     const TDesC8& aByteFormat )
       
    94     {
       
    95     TBuf8<KPSWlanMacAddressLength> address;
       
    96     TInt retval = KErrNone;
       
    97 
       
    98     retval = RProperty::Get( 
       
    99         KPSUidWlan, 
       
   100         KPSWlanMacAddress, 
       
   101         address );
       
   102     
       
   103     if ( retval == KErrNone )
       
   104         {
       
   105         if ( aByteFormat.Length() == 0 )
       
   106             {
       
   107             //client wants raw data
       
   108             aWlanMacAddress.Copy( address );
       
   109             }
       
   110         else
       
   111             {
       
   112             for ( TInt counter = 0; counter < address.Length(); counter++ )
       
   113                 {
       
   114                 TUint16 a = (TUint16)address[ counter ];
       
   115                 aWlanMacAddress.AppendNumFixedWidthUC( a, EHex, KWlanMacAddressPos );
       
   116                 aWlanMacAddress.Append( aByteFormat );
       
   117                 aWlanMacAddress.UpperCase();
       
   118                 }
       
   119 
       
   120             // Delete the last format
       
   121             if ( aWlanMacAddress.Length() )
       
   122                 {
       
   123                 aWlanMacAddress.Delete( aWlanMacAddress.Length() - aByteFormat.Length(), aByteFormat.Length() );
       
   124                 }
       
   125             }
       
   126         
       
   127         if ( aWlanMacAddress.Length() == 0 ) 
       
   128             {
       
   129             retval = KErrNotFound;
       
   130             }
       
   131         }
       
   132    
       
   133     return retval;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CIPAppUtilsAddressResolver::GetLocalIpAddressFromIap
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C TInt CIPAppUtilsAddressResolver::GetLocalIpAddressFromIap( 
       
   141     TInetAddr& aLocalIpAddr,
       
   142     TUint32 aIapId )
       
   143     {
       
   144     TInt retval( KErrNotFound );
       
   145     
       
   146     TInt err = iSocket.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl );
       
   147     
       
   148     if ( KErrNone == err )
       
   149         {
       
   150         TPckgBuf<TSoInetInterfaceInfo> info;
       
   151         
       
   152         err = iSocket.GetOpt( KSoInetNextInterface,
       
   153                 KSolInetIfCtrl,info );
       
   154         
       
   155         while ( KErrNone == err && KErrNone != retval )
       
   156             {
       
   157             TPckgBuf<TSoInetIfQuery> query;
       
   158             query().iName = info().iName;
       
   159             
       
   160             err = iSocket.GetOpt( KSoInetIfQueryByName,
       
   161                     KSolInetIfQuery,query );
       
   162             
       
   163             if ( KErrNone == err )
       
   164                 {
       
   165                 retval = CheckAndSetAddr( 
       
   166                         aLocalIpAddr, (TInetAddr&)info().iAddress,
       
   167                         query().iZone[1], aIapId );
       
   168                 
       
   169                 err = iSocket.GetOpt( KSoInetNextInterface,
       
   170                         KSolInetIfCtrl,info );
       
   171                 }
       
   172             }
       
   173         }
       
   174 
       
   175     if ( KErrNoMemory == err )
       
   176         {
       
   177         retval = err;
       
   178         }
       
   179     
       
   180     return retval;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CLocalAddrResolver::CheckAndSetAddr
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 TInt CIPAppUtilsAddressResolver::CheckAndSetAddr( 
       
   188     TInetAddr& aTarget,
       
   189     TInetAddr& aCandidate,
       
   190     TUint32 aCandidateIap,
       
   191     TUint32 aSpecifiedIap ) const
       
   192     {
       
   193     TInt retval = KErrNotFound;
       
   194     if ( !aCandidate.IsLinkLocal() &&
       
   195          !aCandidate.IsUnspecified() && 
       
   196          !aCandidate.IsLoopback() )
       
   197         {
       
   198         if ( aCandidate.IsV4Mapped())
       
   199             {
       
   200             aCandidate.ConvertToV4();
       
   201             }
       
   202         if ( aCandidateIap == aSpecifiedIap )
       
   203             {
       
   204             aTarget = aCandidate;
       
   205             aTarget.SetScope(0);
       
   206             retval = KErrNone;
       
   207             }
       
   208         }
       
   209     return retval;
       
   210     }                                         
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CIPAppUtilsAddressResolver::WlanSsidL
       
   214 // Returns WLAN SSID based on given IAP id.
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 EXPORT_C void CIPAppUtilsAddressResolver::WlanSsidL(
       
   218     TUint32 aIapId, TDes& aWlanSsid )
       
   219     {
       
   220     RCmManagerExt cmManager;
       
   221     cmManager.OpenL();
       
   222     CleanupClosePushL( cmManager );
       
   223     // Check that bearer is WLAN.
       
   224     if ( KUidWlanBearerType == 
       
   225         cmManager.GetConnectionMethodInfoIntL( 
       
   226             aIapId, CMManager::ECmBearerType ) )
       
   227         {
       
   228         RCmConnectionMethodExt connMethod = 
       
   229             cmManager.ConnectionMethodL( aIapId );
       
   230         CleanupClosePushL( connMethod );
       
   231         
       
   232         // Get SSID for WLAN.
       
   233         HBufC* wlanSsid = connMethod.GetStringAttributeL( 
       
   234             CMManager::EWlanSSID );
       
   235         aWlanSsid.Copy( *wlanSsid );
       
   236         delete wlanSsid;
       
   237         
       
   238         CleanupStack::PopAndDestroy( &connMethod );    
       
   239         }
       
   240     else
       
   241         {
       
   242         User::Leave( KErrNotFound ); // leave if bearer is not WLAN
       
   243         }
       
   244     
       
   245     CleanupStack::PopAndDestroy( &cmManager );
       
   246     }
       
   247