tcpiputils/networkaddressandporttranslation/src/naptutil.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2008-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 // Implements client hardware address fetching.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 #include "naptutil.h"
       
    23 
       
    24 /** 
       
    25   * Fucntion to fetch the IP from CommsDb, and Traps any leave.
       
    26   * @internalTechnology
       
    27   * @param aIapId - IAP identifier
       
    28   * @param aClientIp - Returns the fetched IP address 
       
    29   */
       
    30   
       
    31 TInt TNaptUtil::GetClientIp(TInt aIapId, TUint32& aClientIp)
       
    32 {
       
    33 	TRAPD(err, GetClientIpFromCommsL(aIapId, aClientIp));
       
    34 	return err;
       
    35 }
       
    36   
       
    37 
       
    38 /** 
       
    39   * Open the IAP record
       
    40   * @internalTechnology
       
    41   * @param aDbSession - Commsdb session
       
    42   * @param aIapRecord - IAP record
       
    43   * @param aIapId - IAP identifier
       
    44   */
       
    45 void TNaptUtil::OpenIAPViewLC(CMDBSession*& aSession, CCDIAPRecord*& aIapRecord, TInt aIapId)
       
    46 	{
       
    47 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
    48 	aSession = CMDBSession::NewLC(KCDVersion1_2);
       
    49 #else
       
    50 	aSession = CMDBSession::NewLC(KCDVersion1_1);
       
    51 #endif
       
    52 
       
    53 	// Reveal hidden or private IAP records if a licensee has chosen to protect a record
       
    54 	// using one of these flags - the API to do this is public so internal components
       
    55 	// have to support the use of such records.
       
    56 	aSession->SetAttributeMask(ECDHidden | ECDPrivate);
       
    57 	
       
    58 	aIapRecord = static_cast<CCDIAPRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdIAPRecord));
       
    59 	CleanupStack::PushL(aIapRecord);
       
    60 
       
    61     aIapRecord->SetRecordId(aIapId);
       
    62         	
       
    63 	aIapRecord->LoadL(*aSession);
       
    64 	}
       
    65 /** 
       
    66   * Initialize the service link
       
    67   * @internalTechnology
       
    68   * @param aDbSession - Commsdb session
       
    69   * @param aIapRecord - IAP record
       
    70   */
       
    71 void TNaptUtil::InitialServiceLinkL(CMDBSession* aDbSession, CCDIAPRecord* aIapRecord)
       
    72 	{
       
    73 	if (aIapRecord->iService.iLinkedRecord == 0)
       
    74 		{
       
    75 		const TDesC& servType = aIapRecord->iServiceType;
       
    76 
       
    77 		if (servType.CompareF(TPtrC(KCDTypeNameLANService))==0)
       
    78 			{
       
    79 			aIapRecord->iService.iLinkedRecord = static_cast<CCDLANServiceRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdLANServiceRecord));
       
    80 			}
       
    81 		else
       
    82 			{
       
    83 			User::Leave(KErrBadName);	
       
    84 			}
       
    85 		aIapRecord->iService.iLinkedRecord->SetRecordId(aIapRecord->iService);
       
    86 		}
       
    87 	aIapRecord->iService.iLinkedRecord->LoadL(*aDbSession);
       
    88 	}
       
    89 /** 
       
    90   * Fetch the server IP address from comms and generate client ID
       
    91   * @internalTechnology
       
    92   * @param aIapId - IAP identifier
       
    93   * @param aClientIp - Returns the fetched IP address 
       
    94   */
       
    95 void TNaptUtil::GetClientIpFromCommsL(TInt aIapId, TUint32& aClientIp)
       
    96 {
       
    97 	CMDBSession* session;
       
    98 	CCDIAPRecord* iap;
       
    99 
       
   100 	// next call leaves session and iap object on cleanupstack
       
   101 	OpenIAPViewLC(session, iap, aIapId);
       
   102 	CCDServiceRecordBase* service = NULL;
       
   103 	InitialServiceLinkL(session, iap);
       
   104 	service = static_cast<CCDServiceRecordBase *>(iap->iService.iLinkedRecord);
       
   105 	TInt ignoreThis;
       
   106 
       
   107 	CMDBField<TDesC>* cdbLoadBool = static_cast<CMDBField<TDesC>*>(service->GetFieldByNameL(KCDTypeNameIpAddr, ignoreThis));
       
   108 	TInetAddr inetAddr;
       
   109 	inetAddr.Input(*cdbLoadBool);
       
   110 	TUint32 serverAddr = inetAddr.Address();
       
   111 	TUint32 hostId = (serverAddr & ~KInetAddrNetMaskC) + 1;
       
   112 	if (hostId >= 255)
       
   113 	 {
       
   114 	 hostId = 1;
       
   115 	 }
       
   116 	aClientIp = (serverAddr & KInetAddrNetMaskC) | hostId;
       
   117 	service = NULL;
       
   118 	CleanupStack::PopAndDestroy(iap);
       
   119 	CleanupStack::PopAndDestroy(session);
       
   120 }