realtimenetprots/sipfw/SIP/ServerResolver/src/CSIPQueryNaptr.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2004-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          : CSIPQueryNaptr.cpp
       
    15 // Part of       : ServerResolver
       
    16 // Version       : SIP/4.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include <stringpool.h>
       
    22 #include "sipstrings.h"
       
    23 #include "sipstrconsts.h"
       
    24 #include "CSIPQueryNaptr.h"
       
    25 #include "MSIPHostResolver.h"
       
    26 
       
    27 _LIT8(Ks, "s");
       
    28 _LIT8(KServiceSIP, "SIP+*");
       
    29 _LIT8(KServiceSIPS, "SIPS+*");
       
    30 _LIT8(KStarPlusD2T, "*+D2T");
       
    31 _LIT8(KStarPlusD2TPlusStar, "*+D2T+*");
       
    32 _LIT8(KStarPlusD2U, "*+D2U");
       
    33 _LIT8(KStarPlusD2UPlusStar, "*+D2U+*");
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // CSIPQueryNaptr::NewL
       
    37 // ----------------------------------------------------------------------------
       
    38 //
       
    39 CSIPQueryNaptr* CSIPQueryNaptr::NewL( const TDesC8& aTarget, TBool aSipsUri )
       
    40 	{
       
    41 	CSIPQueryNaptr* self = new ( ELeave ) CSIPQueryNaptr(aSipsUri);
       
    42 	CleanupStack::PushL( self );
       
    43 	self->ConstructL( aTarget );
       
    44 	CleanupStack::Pop( self );
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // CSIPQueryNaptr::QueryBuf
       
    50 // ----------------------------------------------------------------------------
       
    51 //
       
    52 const TDesC8& CSIPQueryNaptr::QueryBuf()
       
    53 	{
       
    54 	TDnsQuery query( *iTarget, KDnsRRTypeNAPTR );
       
    55 	TDnsQueryBuf querybuf( query );
       
    56 	return iQueryBuf = querybuf;
       
    57 	}
       
    58 
       
    59 // ----------------------------------------------------------------------------
       
    60 // CSIPQueryNaptr::QueryResultBuf
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 TDes8& CSIPQueryNaptr::QueryResultBuf()
       
    64 	{
       
    65 	return iResult; 
       
    66 	}
       
    67 
       
    68 // ----------------------------------------------------------------------------
       
    69 // CSIPQueryNaptr::QueryResult
       
    70 // ----------------------------------------------------------------------------
       
    71 //
       
    72 TDnsRespNAPTR CSIPQueryNaptr::QueryResult()
       
    73 	{
       
    74 	return iResult();
       
    75 	}
       
    76 
       
    77 // ----------------------------------------------------------------------------
       
    78 // CSIPQueryNaptr::~CSIPQueryNaptr
       
    79 // ----------------------------------------------------------------------------
       
    80 //
       
    81 CSIPQueryNaptr::~CSIPQueryNaptr()
       
    82 	{
       
    83 	delete iTarget;
       
    84 	if ( iRecordArray )
       
    85 		{
       
    86 		iRecordArray->ResetAndDestroy();
       
    87 		delete iRecordArray;
       
    88 		}
       
    89 	}
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CSIPQueryNaptr::AddL
       
    93 // ----------------------------------------------------------------------------
       
    94 //
       
    95 void CSIPQueryNaptr::AddL( TDnsRespNAPTR* aNaptr )
       
    96 	{
       
    97 	TLinearOrder<TDnsRespNAPTR> order( CSIPQueryNaptr::Compare );
       
    98 	User::LeaveIfError( iRecordArray->InsertInOrderAllowRepeats( aNaptr,
       
    99 																 order ) );
       
   100 	}
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 // CSIPQueryNaptr::Compare
       
   104 // ----------------------------------------------------------------------------
       
   105 //
       
   106 TInt CSIPQueryNaptr::Compare( const TDnsRespNAPTR& aFirst, 
       
   107 							  const TDnsRespNAPTR& aSecond )
       
   108 	{
       
   109 	if ( aFirst.Order() < aSecond.Order() )
       
   110 		{
       
   111 		return -1;
       
   112 		}
       
   113 	if ( aFirst.Order() > aSecond.Order() )
       
   114 		{
       
   115 		return 1;
       
   116 		}
       
   117 	if ( aFirst.Order() == aSecond.Order() )
       
   118 		{
       
   119 		if ( aFirst.Pref() == aSecond.Pref() )
       
   120 			{
       
   121 			if ( NaptrProtocol( aFirst ) ==
       
   122 				 SIPStrings::StringF( SipStrConsts::EUDP ) )
       
   123 				{
       
   124 				return -1;
       
   125 				}
       
   126 			else if ( NaptrProtocol( aSecond ) ==
       
   127 					  SIPStrings::StringF( SipStrConsts::EUDP ) )
       
   128 				{
       
   129 				return 1;
       
   130 				}
       
   131 				else
       
   132 					{
       
   133 					return 0;
       
   134 					}
       
   135 			}
       
   136 		if ( aFirst.Pref() < aSecond.Pref() )
       
   137 			{
       
   138 			return -1;
       
   139 			}
       
   140 		if ( aFirst.Pref() > aSecond.Pref() )
       
   141 			{
       
   142 			return 1;
       
   143 			}
       
   144 		}
       
   145 	return 0;
       
   146 	}
       
   147 
       
   148 // ----------------------------------------------------------------------------
       
   149 // CSIPQueryNaptr::NAPTRRecordL
       
   150 // ----------------------------------------------------------------------------
       
   151 //
       
   152 TDnsRespNAPTR& CSIPQueryNaptr::NAPTRRecordL( TInt aIndex )
       
   153 	{
       
   154 	__ASSERT_ALWAYS ( aIndex < iRecordArray->Count(), User::Leave( KErrArgument ) );
       
   155 	return ( *( *iRecordArray )[aIndex] );
       
   156 	}
       
   157 
       
   158 // ----------------------------------------------------------------------------
       
   159 // CSIPQueryNaptr::RemoveElementL
       
   160 // ----------------------------------------------------------------------------
       
   161 //
       
   162 void CSIPQueryNaptr::RemoveElementL( TInt aIndex )
       
   163 	{
       
   164 	if ( aIndex < iRecordArray->Count() && aIndex >= 0 )
       
   165 		{
       
   166 		TDnsRespNAPTR* naptr = ( *iRecordArray )[aIndex];
       
   167 		delete naptr;
       
   168 		naptr = NULL;
       
   169 		iRecordArray->Remove( aIndex );
       
   170 		}
       
   171 	}
       
   172 
       
   173 // ----------------------------------------------------------------------------
       
   174 // CSIPQueryNaptr::MatchToRulesL
       
   175 // ----------------------------------------------------------------------------
       
   176 //
       
   177 TBool CSIPQueryNaptr::MatchToRulesL( const TDnsRespNAPTR& aNaptr )
       
   178 	{	
       
   179 	if ( aNaptr.Flags().CompareF( Ks ) !=0 )
       
   180 		{
       
   181 		return EFalse;
       
   182 		}
       
   183 		
       
   184 	if ( aNaptr.Regexp().Length() != 0 )
       
   185 		{
       
   186 		return EFalse;
       
   187 		}
       
   188 		
       
   189 	HBufC8* temp = aNaptr.Replacement().AllocL();
       
   190 	TPtr8 ptr = temp->Des();
       
   191 	ptr.Trim();
       
   192 	if ( temp->Length() == 0 )
       
   193 		{
       
   194 		delete temp;
       
   195 		return EFalse;
       
   196 		}
       
   197 	delete temp;
       
   198 	
       
   199 	if ( aNaptr.Service().Match( KServiceSIPS ) != KErrNotFound )
       
   200 		{
       
   201 		if ( NaptrProtocol( aNaptr ) ==
       
   202 			 SIPStrings::StringF( SipStrConsts::ETLS ) )
       
   203 			{
       
   204 			return ETrue;
       
   205 			}
       
   206 		}
       
   207 	
       
   208 	if (!iSipsUri)
       
   209 		{
       
   210 		if ( aNaptr.Service().Match( KServiceSIP ) != KErrNotFound )
       
   211 			{
       
   212 			if ( NaptrProtocol( aNaptr ) !=
       
   213 				 SIPStrings::StringF( SipStrConsts::EEmpty ) )
       
   214 				{
       
   215 				return ETrue;
       
   216 				}
       
   217 			}
       
   218 		}
       
   219 	
       
   220 	return EFalse;
       
   221 	}
       
   222 
       
   223 // ----------------------------------------------------------------------------
       
   224 // CSIPQueryNaptr::RecordArray
       
   225 // ----------------------------------------------------------------------------
       
   226 //
       
   227 RPointerArray<TDnsRespNAPTR>& CSIPQueryNaptr::RecordArray()
       
   228 	{
       
   229 	return *iRecordArray;
       
   230 	}
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // CSIPQueryNaptr::NaptrProtocol
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 RStringF CSIPQueryNaptr::NaptrProtocol( const TDnsRespNAPTR& aNaptr )
       
   237 	{	
       
   238 	if ( aNaptr.Service().Match( KStarPlusD2T ) != KErrNotFound ||
       
   239 		aNaptr.Service().Match( KStarPlusD2TPlusStar ) != KErrNotFound )
       
   240 		{
       
   241 		if (aNaptr.Service().Match( KServiceSIPS ) != KErrNotFound)
       
   242 			{
       
   243 			return SIPStrings::StringF( SipStrConsts::ETLS );
       
   244 			}
       
   245 		return SIPStrings::StringF( SipStrConsts::ETCP );
       
   246 		}
       
   247 	
       
   248 	if( aNaptr.Service().Match( KStarPlusD2U ) != KErrNotFound ||
       
   249 		aNaptr.Service().Match( KStarPlusD2UPlusStar ) != KErrNotFound )
       
   250 		{
       
   251 		return SIPStrings::StringF( SipStrConsts::EUDP );
       
   252 		}
       
   253 	
       
   254 	return SIPStrings::StringF( SipStrConsts::EEmpty );
       
   255 	}
       
   256 
       
   257 
       
   258 // ----------------------------------------------------------------------------
       
   259 // CSIPQueryNaptr::Query
       
   260 // ----------------------------------------------------------------------------
       
   261 //
       
   262 void CSIPQueryNaptr::Query( MSIPHostResolver& aResolver )
       
   263 	{
       
   264 	aResolver.Resolver().Query( QueryBuf(),
       
   265 							    QueryResultBuf(),
       
   266 								aResolver.RequestStatus() );	
       
   267 	}
       
   268 
       
   269 // ----------------------------------------------------------------------------
       
   270 // CSIPQueryNaptr::ConstructL
       
   271 // ----------------------------------------------------------------------------
       
   272 //
       
   273 void CSIPQueryNaptr::ConstructL( const TDesC8& aTarget )
       
   274 	{
       
   275 	// KQueryDataMaxLength is same as TDnsQryData's max length in dns_qry.h
       
   276 	const TInt KQueryDataMaxLength = 255; 	
       
   277  
       
   278 	__ASSERT_ALWAYS ( aTarget.Length() < KQueryDataMaxLength, 
       
   279 					  User::Leave( KErrArgument ) );
       
   280 					  
       
   281 	__ASSERT_ALWAYS ( aTarget.Length() > 0, User::Leave( KErrArgument ) );
       
   282 
       
   283 
       
   284 	iTarget = aTarget.AllocL();
       
   285 	iRecordArray = new ( ELeave ) RPointerArray<TDnsRespNAPTR>; 
       
   286 	}
       
   287 
       
   288 // ----------------------------------------------------------------------------
       
   289 // CSIPQueryNaptr::CSIPQueryNaptr
       
   290 // ----------------------------------------------------------------------------
       
   291 //
       
   292 CSIPQueryNaptr::CSIPQueryNaptr(TBool aSipsUri)
       
   293 :CSIPQueryBase(), iSipsUri(aSipsUri)
       
   294 	{
       
   295 	}
       
   296 
       
   297 // ----------------------------------------------------------------------------
       
   298 // CSIPQueryNaptr::HandleQueryResultL
       
   299 // ----------------------------------------------------------------------------
       
   300 //
       
   301 void CSIPQueryNaptr::HandleQueryResultL( MSIPHostResolver& aResolver )
       
   302 	{
       
   303 	TDnsRespNAPTR* naptr = NULL;
       
   304 	if ( MatchToRulesL( QueryResult() ) )
       
   305 		{
       
   306 		naptr = new ( ELeave ) TDnsRespNAPTR();
       
   307 		CleanupStack::PushL( naptr );
       
   308 		naptr->SetFlags( iResult().Flags() );
       
   309 		naptr->SetOrder( iResult().Order() );
       
   310 		naptr->SetPref( iResult().Pref() );
       
   311 		naptr->SetRegexp( iResult().Regexp() );
       
   312 		naptr->SetReplacement( iResult().Replacement() );
       
   313 		naptr->SetService( iResult().Service() );
       
   314 		AddL( naptr );
       
   315 		CleanupStack::Pop( naptr );
       
   316 		}
       
   317 	while ( aResolver.Resolver().QueryGetNext( QueryResultBuf() ) 
       
   318 												== KErrNone )
       
   319 		{
       
   320 		if ( MatchToRulesL( QueryResult() ) )
       
   321 			{
       
   322 			naptr = new ( ELeave ) TDnsRespNAPTR();
       
   323 			CleanupStack::PushL( naptr );
       
   324 			naptr->SetFlags( iResult().Flags() );
       
   325 			naptr->SetOrder( iResult().Order() );
       
   326 			naptr->SetPref( iResult().Pref() );
       
   327 			naptr->SetRegexp( iResult().Regexp() );
       
   328 			naptr->SetReplacement( iResult().Replacement() );
       
   329 			naptr->SetService( iResult().Service() );
       
   330 			AddL( naptr );
       
   331 			CleanupStack::Pop( naptr );
       
   332 			}
       
   333 		}
       
   334 	}
       
   335 
       
   336 // ----------------------------------------------------------------------------
       
   337 // CSIPQueryNaptr::ResultTargetL
       
   338 // ----------------------------------------------------------------------------
       
   339 //
       
   340 const TDesC8& CSIPQueryNaptr::ResultTargetL()
       
   341 	{
       
   342 	return NAPTRRecordL( 0 ).Replacement();
       
   343 	}
       
   344 
       
   345 // ----------------------------------------------------------------------------
       
   346 // CSIPQueryNaptr::ResultProtocolL
       
   347 // ----------------------------------------------------------------------------
       
   348 //
       
   349 RStringF CSIPQueryNaptr::ResultProtocolL() 
       
   350 	{
       
   351 	return NaptrProtocol( NAPTRRecordL( 0 ) );
       
   352 	}
       
   353 	
       
   354 // ----------------------------------------------------------------------------
       
   355 // CSIPQueryNaptr::ArrayCount
       
   356 // ----------------------------------------------------------------------------
       
   357 //
       
   358 TInt CSIPQueryNaptr::ArrayCount() 
       
   359 	{
       
   360 	return iRecordArray->Count();
       
   361 	}