natplugins/natpnatfwsdpprovider/src/nsputil.cpp
changeset 0 1bce908db942
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:  Utility class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sdpcodecstringpool.h>
       
    19 #include <sdpcodecstringconstants.h>
       
    20 #include <sdpdocument.h>
       
    21 #include <sdpconnectionfield.h>
       
    22 #include <sdporiginfield.h>
       
    23 #include <sdpmediafield.h>
       
    24 #include "natfwcandidate.h"
       
    25 #include "nsputil.h"
       
    26 #include "nspdefs.h"
       
    27 #include "nspmediastream.h"
       
    28 
       
    29 const TUint KMaxLengthOfFQDN = 255;
       
    30 const TUint KRejectPort = 0;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 // ---------------------------------------------------------------------------
       
    34 // NSPUtil::FindMediaStream
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CNSPMediaStream* NSPUtil::FindMediaStream( const CSdpMediaField& aMediaField,
       
    38         RPointerArray<CNSPMediaStream>& aMediaStreams )
       
    39     {
       
    40     const TInt mediastreamscount( aMediaStreams.Count() );
       
    41     for ( TInt index = 0; index < mediastreamscount; index++ )
       
    42         {
       
    43         if ( aMediaStreams[index]->MediaField().Media() == aMediaField.Media() )
       
    44             {
       
    45             return aMediaStreams[index];
       
    46             }
       
    47         }
       
    48     
       
    49     return NULL;
       
    50     }
       
    51 
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // NSPUtil::FindMediaStream
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CNSPMediaStream* NSPUtil::FindMediaStream(
       
    58         const CNSPMediaStream& aMediaStream,
       
    59         RPointerArray<CNSPMediaStream>& aMediaStreams )
       
    60     {
       
    61     const TInt mediastreamscount( aMediaStreams.Count() );
       
    62     for ( TInt index = 0; index < mediastreamscount; index++ )
       
    63         {
       
    64         if ( aMediaStreams[index]->MediaField().Media() ==
       
    65              aMediaStream.MediaField().Media() )
       
    66             {
       
    67             return aMediaStreams[index];
       
    68             }
       
    69         }
       
    70     
       
    71     return NULL;
       
    72     }
       
    73 
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // NSPUtil::FindMediaField
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CSdpMediaField* NSPUtil::FindMediaField( const CNSPMediaStream& aMediaStream,
       
    80         RPointerArray<CSdpMediaField>& aMediaFields )
       
    81     {
       
    82     const TInt mediafieldscount( aMediaFields.Count() );
       
    83     for ( TInt index = 0; index < mediafieldscount; index++ )
       
    84         {
       
    85         if ( aMediaFields[index]->Media() == aMediaStream.MediaField().Media() )
       
    86             {
       
    87             return aMediaFields[index];
       
    88             }
       
    89         }
       
    90     
       
    91     return NULL;
       
    92     }
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // NSPUtil::FindMediaField
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 CSdpMediaField* NSPUtil::FindMediaField( const CSdpMediaField& aMediaField,
       
   100         RPointerArray<CSdpMediaField>& aMediaFields )
       
   101     {
       
   102     const TInt mediafieldscount( aMediaFields.Count() );
       
   103     for ( TInt index = 0; index < mediafieldscount; index++ )
       
   104         {
       
   105         if ( aMediaFields[index]->Media() == aMediaField.Media() )
       
   106             {
       
   107             return aMediaFields[index];
       
   108             }
       
   109         }
       
   110     
       
   111     return NULL;
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // NSPUtil::SortCandidatesL
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void NSPUtil::SortCandidatesL( RPointerArray<CNATFWCandidate>& aLocalCands )
       
   120     {
       
   121     __ASSERT_ALWAYS( &aLocalCands, User::Leave( KErrArgument ) );
       
   122     __ASSERT_ALWAYS( aLocalCands.Count(), User::Leave( KErrArgument ) );
       
   123     
       
   124     TLinearOrder<CNATFWCandidate> order( CNATFWCandidate::PriorityOrder );
       
   125     aLocalCands.Sort( order );
       
   126     }
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // NSPUtil::UpdateConnectionFieldL
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void NSPUtil::UpdateConnectionFieldL(
       
   134         CSdpConnectionField& aField, const TDesC8& aAddress )
       
   135     {
       
   136     __ASSERT_ALWAYS( &aField, User::Leave( KErrArgument ) );
       
   137     __ASSERT_ALWAYS( &aAddress != NULL, User::Leave( KErrArgument ) );
       
   138     
       
   139     TBool unspecf( EFalse );
       
   140     RStringF netType = aField.NetType();
       
   141     RStringF addressType = aField.AddressType();
       
   142     const TDesC8& address = aField.Address();
       
   143     
       
   144     TInetAddr addr;
       
   145     HBufC* addrBuf = HBufC::NewLC( KMaxLengthOfFQDN );
       
   146     TPtr addrPtr( addrBuf->Des() );
       
   147     addrPtr.Copy( address );
       
   148     
       
   149     if ( !addr.Input( addrBuf->Des() ) ) // valid IP
       
   150         {
       
   151         unspecf = addr.IsUnspecified();
       
   152         addrPtr.Copy( aAddress );
       
   153         
       
   154         if ( !unspecf && !addr.Input( addrBuf->Des() ) )
       
   155             {
       
   156             CSdpConnectionField* field = CSdpConnectionField::NewL( addr );
       
   157             addressType = field->AddressType();
       
   158             delete field;
       
   159             }
       
   160         }
       
   161     
       
   162     if ( !unspecf )
       
   163         {
       
   164         aField.SetAddressL( netType, addressType, aAddress );
       
   165         }
       
   166     
       
   167     CleanupStack::PopAndDestroy( addrBuf );
       
   168     }
       
   169 
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // NSPUtil::UpdateOriginFieldL
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void NSPUtil::UpdateOriginFieldL(
       
   176         CSdpOriginField& aField, const TDesC8& aAddress )
       
   177     {
       
   178     __ASSERT_ALWAYS( &aField, User::Leave( KErrArgument ) );
       
   179     __ASSERT_ALWAYS( &aAddress != NULL, User::Leave( KErrArgument ) );
       
   180     
       
   181     TBool unspecf( EFalse );
       
   182     RStringF netType = aField.NetType();
       
   183     RStringF addressType = aField.AddressType();
       
   184     const TDesC8& address = aField.Address();
       
   185     
       
   186     TInetAddr addr;
       
   187     HBufC* addrBuf = HBufC::NewLC( KMaxLengthOfFQDN );
       
   188     TPtr addrPtr( addrBuf->Des() );
       
   189     addrPtr.Copy( address );
       
   190     
       
   191     if ( !addr.Input( addrBuf->Des() ) ) // valid IP
       
   192         {
       
   193         unspecf = addr.IsUnspecified();
       
   194         addrPtr.Copy( aAddress );
       
   195         
       
   196         if ( !unspecf && !addr.Input( addrBuf->Des() ) )
       
   197             {
       
   198             CSdpConnectionField* field = CSdpConnectionField::NewL( addr );
       
   199             addressType = field->AddressType();
       
   200             delete field;
       
   201             }
       
   202         }
       
   203     
       
   204     if ( !unspecf )
       
   205         {
       
   206         aField.SetAddressL( aAddress, netType, addressType );
       
   207         }
       
   208     
       
   209     CleanupStack::PopAndDestroy( addrBuf );    
       
   210     }
       
   211 
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // NSPUtil::RejectL
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void NSPUtil::RejectL( CSdpMediaField& aField )
       
   218     {
       
   219     __ASSERT_ALWAYS( &aField, User::Leave( KErrArgument ) );
       
   220     aField.SetPortL( KRejectPort );
       
   221     }
       
   222 
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // NSPUtil::IsReject
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 TBool NSPUtil::IsReject( const CSdpMediaField& aField )
       
   229     {
       
   230     if ( &aField )
       
   231         {
       
   232         return ( KRejectPort == aField.Port() );
       
   233         }
       
   234     else
       
   235         {
       
   236         return EFalse;
       
   237         }
       
   238     }
       
   239 
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // NSPUtil::IsDuplicate
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 TBool NSPUtil::IsDuplicate( const CNATFWCandidate& aCand,
       
   246         const RPointerArray<CNATFWCandidate>& aCandidates )
       
   247     {
       
   248     const TUint candidatecount( aCandidates.Count() );
       
   249     for ( TUint index = 0; index < candidatecount; index++ )
       
   250         {
       
   251         if ( aCand == *aCandidates[index] )
       
   252             {
       
   253             return ETrue;
       
   254             }
       
   255         }
       
   256     
       
   257     return EFalse;
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // NSPUtil::IsUnspecifiedL
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TBool NSPUtil::IsUnspecifiedL( const TDesC8& aAddress, TUint aPort )
       
   266 	{
       
   267 	TInetAddr addr;
       
   268 	TRAPD( error, addr = ConvertTDesC8ToTInetAddrL( aAddress, aPort ) );
       
   269 	User::LeaveIfError( KErrNoMemory == error ? KErrNoMemory : KErrNone );
       
   270 	return ( KErrNone == error ? addr.IsUnspecified() : EFalse );
       
   271 	}
       
   272 
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // NSPUtil::CleanupArrayItem
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void NSPUtil::CleanupArrayItem( TAny* anArray )
       
   279     {
       
   280     if ( anArray )
       
   281         {
       
   282         reinterpret_cast< RPointerArray<CNATFWCandidate>* >( anArray )->
       
   283             ResetAndDestroy();
       
   284         reinterpret_cast< RPointerArray<CNATFWCandidate>* >( anArray )->
       
   285             Close();
       
   286         }
       
   287     }
       
   288 
       
   289 
       
   290 // end of file