realtimenetprots/sipfw/SIP/ServerResolver/src/CSIPQuerySrv.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          : CSIPQuerySrv.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 "CSIPQuerySrv.h"
       
    25 #include "MSIPHostResolver.h"
       
    26 
       
    27 _LIT8(KDot8, ".");
       
    28 // ----------------------------------------------------------------------------
       
    29 // CSIPQuerySrv::CSIPQuerySrv
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 CSIPQuerySrv::CSIPQuerySrv(TBool aSipsUri)
       
    33 :CSIPQueryBase(), iSipsUri(aSipsUri)
       
    34 	{
       
    35 	}
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CSIPQuerySrv::ConstructL
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 void CSIPQuerySrv::ConstructL( const TDesC8& aTarget )
       
    42 	{
       
    43  	SetTargetL( aTarget );
       
    44 	}
       
    45 	
       
    46 // ----------------------------------------------------------------------------
       
    47 // CSIPQuerySrv::NewL
       
    48 // ----------------------------------------------------------------------------
       
    49 //
       
    50 CSIPQuerySrv* CSIPQuerySrv::NewL( const TDesC8& aTarget, TBool aSipsUri )
       
    51 	{
       
    52 	CSIPQuerySrv* self = new ( ELeave ) CSIPQuerySrv(aSipsUri);
       
    53 	CleanupStack::PushL( self );
       
    54 	self->ConstructL( aTarget );
       
    55 	CleanupStack::Pop( self );
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 // ----------------------------------------------------------------------------
       
    60 // CSIPQuerySrv::~CSIPQuerySrv
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 CSIPQuerySrv::~CSIPQuerySrv()
       
    64 	{
       
    65 	delete iTarget;
       
    66 	iRecordArray.Reset();
       
    67 	}
       
    68 
       
    69 // ----------------------------------------------------------------------------
       
    70 // CSIPQuerySrv::SetTargetProtocolL
       
    71 // ----------------------------------------------------------------------------
       
    72 //
       
    73 HBufC8* CSIPQuerySrv::SetTargetProtocolL( const TDesC8& aTarget,
       
    74 										  RStringF aProtocol,
       
    75 										  TBool aIsSipsUri )
       
    76 	{
       
    77 	_LIT8(Ksipudp, "_sip._udp.");
       
    78 	_LIT8(Ksiptcp, "_sip._tcp.");
       
    79 	_LIT8(Ksipstcp, "_sips._tcp.");
       
    80 	TUint32 len = aTarget.Length() + Ksipudp().Length();
       
    81 	if (aIsSipsUri)
       
    82 		{
       
    83 		len++;
       
    84 		}
       
    85 	HBufC8* temp =  HBufC8::NewLC( len );
       
    86 	TPtr8 tempPtr = temp->Des();
       
    87 	if (aIsSipsUri)
       
    88 		{
       
    89 		// If SIPS URI, ignore the value of aProtocol. and act always as
       
    90 		// transport is TLS (Ksipstcp)
       
    91 		tempPtr.Append( Ksipstcp );
       
    92 		}
       
    93 
       
    94 	else if ( aProtocol == SIPStrings::StringF( SipStrConsts::ETCP ) )
       
    95 		{
       
    96 		tempPtr.Append( Ksiptcp );
       
    97 		}
       
    98 		else if ( aProtocol == SIPStrings::StringF( SipStrConsts::EUDP ) )
       
    99 			{
       
   100 			tempPtr.Append( Ksipudp );
       
   101 			}
       
   102 			else
       
   103 				{
       
   104 				User::Leave( KErrNotSupported );
       
   105 				}
       
   106 	tempPtr.Append( aTarget );
       
   107 	CleanupStack::Pop( temp );
       
   108 	return temp;
       
   109 	}
       
   110 	
       
   111 // ----------------------------------------------------------------------------
       
   112 // CSIPQuerySrv::SetTargetTLSL
       
   113 // ----------------------------------------------------------------------------
       
   114 //
       
   115 HBufC8* CSIPQuerySrv::SetTargetTLSL( const TDesC8& aTarget )
       
   116 	{
       
   117 	_LIT8(Ksiptls, "_sip._tls.");
       
   118 	TUint32 len = aTarget.Length() + Ksiptls().Length();
       
   119 	HBufC8* temp =  HBufC8::NewLC( len );
       
   120 	TPtr8 tempPtr = temp->Des();
       
   121 	tempPtr.Append( Ksiptls );
       
   122 	tempPtr.Append( aTarget );
       
   123 	CleanupStack::Pop( temp );
       
   124 	return temp;
       
   125 	}
       
   126 	
       
   127 // ----------------------------------------------------------------------------
       
   128 // CSIPQuerySrv::AddL
       
   129 // ----------------------------------------------------------------------------
       
   130 //
       
   131 void CSIPQuerySrv::AddL( TDnsRespSRV aSrv )
       
   132 	{
       
   133 	TLinearOrder<TDnsRespSRV> order( CSIPQuerySrv::Compare );
       
   134 	User::LeaveIfError( iRecordArray.InsertInOrderAllowRepeats( aSrv, order ) );
       
   135 	}
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // CSIPQuerySrv::SetTargetL
       
   139 // ----------------------------------------------------------------------------
       
   140 //
       
   141 void CSIPQuerySrv::SetTargetL( const TDesC8& aTarget )
       
   142 	{
       
   143 	__ASSERT_ALWAYS ( aTarget.Length() > 0, User::Leave( KErrArgument ) );
       
   144 
       
   145 	// KQueryDataMaxLength is same as TDnsQryData's max length in dns_qry.h
       
   146 	const TInt KQueryDataMaxLength = 255; 	
       
   147 	__ASSERT_ALWAYS ( aTarget.Length() < KQueryDataMaxLength, 
       
   148 					User::Leave( KErrArgument ) );
       
   149 
       
   150 	HBufC8* temp = aTarget.AllocL();
       
   151 	delete iTarget;
       
   152 	iTarget = temp;
       
   153 	}
       
   154 
       
   155 // ----------------------------------------------------------------------------
       
   156 // CSIPQuerySrv::RecordArray
       
   157 // ----------------------------------------------------------------------------
       
   158 //
       
   159 RArray<TDnsRespSRV>& CSIPQuerySrv::RecordArray()
       
   160 	{
       
   161 	return iRecordArray;
       
   162 	} 
       
   163 
       
   164 // ----------------------------------------------------------------------------
       
   165 // CSIPQuerySrv::SRVRecordL
       
   166 // ----------------------------------------------------------------------------
       
   167 //
       
   168 TDnsRespSRV& CSIPQuerySrv::SRVRecordL( TInt aIndex )
       
   169 	{
       
   170 	__ASSERT_ALWAYS ( aIndex < iRecordArray.Count(), 
       
   171 					  User::Leave( KErrArgument ) );
       
   172 					  
       
   173 	return iRecordArray[aIndex];
       
   174 	}
       
   175 
       
   176 // ----------------------------------------------------------------------------
       
   177 // CSIPQuerySrv::Compare
       
   178 // ----------------------------------------------------------------------------
       
   179 //
       
   180 TInt CSIPQuerySrv::Compare( const TDnsRespSRV& aFirst,
       
   181 							const TDnsRespSRV& aSecond )
       
   182 	{
       
   183 	if ( aFirst.Priority() < aSecond.Priority() )
       
   184 		{
       
   185 		return -1;
       
   186 		}
       
   187 	if ( aFirst.Priority() > aSecond.Priority() )
       
   188 		{
       
   189 		return 1;
       
   190 		}
       
   191 	if ( aFirst.Priority() == aSecond.Priority() )
       
   192 		{
       
   193 		if ( aSecond.Weight() == 0 )
       
   194 			{
       
   195 			return -1;
       
   196 			}
       
   197 		else
       
   198 			{
       
   199 			return 1;
       
   200 			}
       
   201 		}
       
   202 	return 0;
       
   203 	}
       
   204 
       
   205 // ----------------------------------------------------------------------------
       
   206 // CSIPQuerySrv::QueryBuf
       
   207 // ----------------------------------------------------------------------------
       
   208 //
       
   209 const TDesC8& CSIPQuerySrv::QueryBuf()
       
   210 	{
       
   211 	TDnsQuery query( *iTarget, KDnsRRTypeSRV );
       
   212 	TDnsQueryBuf querybuf( query );
       
   213 	return iQueryBuf = querybuf;
       
   214 	}
       
   215 
       
   216 // ----------------------------------------------------------------------------
       
   217 // CSIPQuerySrv::QueryResultBuf
       
   218 // ----------------------------------------------------------------------------
       
   219 //
       
   220 TDes8& CSIPQuerySrv::QueryResultBuf()
       
   221 	{
       
   222 	return iResult;
       
   223 	}
       
   224 
       
   225 // ----------------------------------------------------------------------------
       
   226 // CSIPQuerySrv::QueryResult
       
   227 // ----------------------------------------------------------------------------
       
   228 //
       
   229 TDnsRespSRV CSIPQuerySrv::QueryResult()
       
   230 	{
       
   231 	return iResult();
       
   232 	}
       
   233 
       
   234 // ----------------------------------------------------------------------------
       
   235 // CSIPQuerySrv::Query
       
   236 // ----------------------------------------------------------------------------
       
   237 //
       
   238 void CSIPQuerySrv::Query( MSIPHostResolver& aResolver )
       
   239 	{
       
   240 	aResolver.Resolver().Query( QueryBuf(), 
       
   241 								QueryResultBuf(), 
       
   242 								aResolver.RequestStatus() );	
       
   243 	}
       
   244 
       
   245 // ----------------------------------------------------------------------------
       
   246 // CSIPQuerySrv::HandleQueryResultL
       
   247 // ----------------------------------------------------------------------------
       
   248 //
       
   249 void CSIPQuerySrv::HandleQueryResultL( MSIPHostResolver& aResolver )
       
   250 	{
       
   251 	if ( QueryResult().Target().CompareF( KDot8 ) != KErrNone )
       
   252 		{
       
   253 		AddL( QueryResult() );
       
   254 		}
       
   255 	
       
   256 	while ( aResolver.Resolver().QueryGetNext( QueryResultBuf() ) 
       
   257 												== KErrNone )
       
   258 		{
       
   259 		if ( QueryResult().Target().CompareF( KDot8 ) != KErrNone )
       
   260 			{
       
   261 			//Records are sorted in right order in inserting phase
       
   262 			AddL( QueryResult() );
       
   263 			}
       
   264 		}
       
   265 	}
       
   266 
       
   267 // ----------------------------------------------------------------------------
       
   268 // CSIPQuerySrv::ResultTargetL
       
   269 // ----------------------------------------------------------------------------
       
   270 //
       
   271 const TDesC8& CSIPQuerySrv::ResultTargetL()
       
   272 	{
       
   273 	return SRVRecordL( 0 ).Target();
       
   274 	}
       
   275 
       
   276 // ----------------------------------------------------------------------------
       
   277 // CSIPQuerySrv::ResultPortL
       
   278 // ----------------------------------------------------------------------------
       
   279 //
       
   280 TUint CSIPQuerySrv::ResultPortL() 
       
   281 	{
       
   282 	return SRVRecordL( 0 ).Port();
       
   283 	}
       
   284 	
       
   285 // ----------------------------------------------------------------------------
       
   286 // CSIPQuerySrv::RemoveElementL
       
   287 // ----------------------------------------------------------------------------
       
   288 //
       
   289 void CSIPQuerySrv::RemoveElementL( TInt aIndex )
       
   290 	{
       
   291 	if ( aIndex < iRecordArray.Count() && aIndex >= 0 )
       
   292 		{
       
   293 		iRecordArray.Remove( aIndex );
       
   294 		}
       
   295 	}
       
   296 
       
   297 // ----------------------------------------------------------------------------
       
   298 // CSIPQuerySrv::ArrayCount
       
   299 // ----------------------------------------------------------------------------
       
   300 //
       
   301 TInt CSIPQuerySrv::ArrayCount() 
       
   302 	{
       
   303 	return iRecordArray.Count();
       
   304 	}
       
   305