realtimenetprots/sipfw/SIP/Client/src/CSIPClient.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2007-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          : CSIPClient.cpp
       
    15 // Part of       : SIPClient
       
    16 // Version       : SIP/6.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "sipclient.h"
       
    22 #include "RSIP.h"
       
    23 #include "CSIPClientReceiver.h"
       
    24 #include "sipclientconnection.h"
       
    25 
       
    26 
       
    27 const TInt KSupportedSecurityMechanismsMaxLength = 300;
       
    28 const TInt KNegotiatedSecurityMechanismMaxLength = 100;
       
    29 const TInt KCredentialsBufExpandSize = 100;
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CSIPClient::NewL
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CSIPClient* CSIPClient::NewL (const TUid& aUid, 
       
    36                               MSIPClientObserver& aSIPObserver)
       
    37 	{
       
    38     CSIPClient* self = CSIPClient::NewLC (aUid,aSIPObserver);
       
    39     CleanupStack::Pop (self);
       
    40     return self;
       
    41 	}
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSIPClient::NewLC
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CSIPClient* CSIPClient::NewLC (const TUid& aUid, 
       
    48                                MSIPClientObserver& aSIPObserver)
       
    49 	{
       
    50 	CSIPClient* self = new(ELeave)CSIPClient;
       
    51     CleanupStack::PushL (self);
       
    52     self->ConstructL (aUid, aSIPObserver);
       
    53     return self;
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSIPClient::CSIPClient
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CSIPClient::CSIPClient ()
       
    61 	{
       
    62 	}
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CSIPClient::ConstructL
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CSIPClient::ConstructL (const TUid& aUid, MSIPClientObserver& aSIPObserver)
       
    69 	{
       
    70 	iSip = new(ELeave)RSIP;
       
    71     if (aUid.iUid == 0) 
       
    72         {
       
    73         // Allowing ProfileAgent to use a zero UID
       
    74 	    User::LeaveIfError(iSip->Connect());
       
    75         }
       
    76     else
       
    77         {
       
    78         User::LeaveIfError(iSip->Connect(aUid));
       
    79         }
       
    80 	iReceiver = new(ELeave)CSIPClientReceiver(*iSip,aSIPObserver);
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CSIPClient::~CSIPClient
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CSIPClient::~CSIPClient()
       
    88 	{
       
    89 	delete iReceiver;
       
    90 	if (iSip)
       
    91         {
       
    92         iSip->Close();
       
    93         }
       
    94 	delete iSip;
       
    95 
       
    96 	for (TInt i=0; i < iConnections.Count(); ++i)
       
    97         {
       
    98         iConnections[i]->DetachClient();
       
    99         }    
       
   100     iConnections.Close();
       
   101 	}
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CSIPClient::SupportedSecurityMechanismsL
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CDesC8Array* CSIPClient::SupportedSecurityMechanismsL () const
       
   108     {
       
   109     HBufC8* buf = HBufC8::NewLC(KSupportedSecurityMechanismsMaxLength);
       
   110 	TPtr8 bufPtr = buf->Des();
       
   111     TIpcArgs args(TIpcArgs::ENothing);
       
   112     args.Set (ESipItcArgAuthenticationMechanism,&bufPtr);
       
   113 	
       
   114     TInt err = iSip->Send(ESipItcSupportedSecurityMechanisms,args);
       
   115 	if (err != KErrNone)
       
   116         {
       
   117         User::Leave(err);
       
   118         }
       
   119     CDesC8ArraySeg* mechanisms = new(ELeave)CDesC8ArraySeg(1);
       
   120     CleanupStack::PushL(mechanisms);
       
   121 	if (bufPtr.Length() > 0)
       
   122 		{
       
   123 	    RDesReadStream readStream(bufPtr);
       
   124 	    readStream.PushL();
       
   125         TUint32 mechanismLength = readStream.ReadUint32L();
       
   126         while (mechanismLength > 0)
       
   127             {
       
   128 	        HBufC8* mechanism = HBufC8::NewLC (mechanismLength);
       
   129 		    TPtr8 mechanismPtr(mechanism->Des());
       
   130 		    readStream.ReadL (mechanismPtr,mechanismLength);
       
   131             mechanisms->AppendL(mechanismPtr);
       
   132             CleanupStack::PopAndDestroy(mechanism);
       
   133             mechanismLength = readStream.ReadUint32L();
       
   134             }
       
   135 	    readStream.Pop();
       
   136 	    readStream.Close();
       
   137         }
       
   138     CleanupStack::Pop(mechanisms);
       
   139     CleanupStack::PopAndDestroy(buf);
       
   140 	return mechanisms;
       
   141     }
       
   142     
       
   143 // -----------------------------------------------------------------------------
       
   144 // CSIPClient::NegotiatedSecurityMechanismL
       
   145 // -----------------------------------------------------------------------------
       
   146 //    
       
   147 HBufC8* CSIPClient::NegotiatedSecurityMechanismL(const TDesC8& aHop)
       
   148 	{
       
   149     HBufC8* mechanism = HBufC8::NewLC(KNegotiatedSecurityMechanismMaxLength);
       
   150 	TPtr8 mechanismPtr = mechanism->Des();
       
   151     TIpcArgs args(TIpcArgs::ENothing);
       
   152     args.Set (ESipItcArgAuthenticationMechanism,&mechanismPtr);
       
   153     args.Set (ESipItcArgNextHop,&aHop);    
       
   154     TInt err = iSip->Send(ESipItcNegotiatedSecurityMechanism,args);
       
   155 	if (err != KErrNone)
       
   156         {
       
   157         User::Leave(err);
       
   158         }    
       
   159     CleanupStack::Pop(mechanism);
       
   160     if (mechanism->Length() == 0)
       
   161     	{
       
   162     	delete mechanism;
       
   163     	mechanism = NULL;
       
   164     	}
       
   165     return mechanism;
       
   166 	}
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CSIPClient::IsSigCompSupportedL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TBool CSIPClient::IsSigCompSupportedL () const
       
   173     {
       
   174     TIpcArgs args(TIpcArgs::ENothing);
       
   175     TInt err = iSip->Send(ESipItcIsSigCompSupported, args);
       
   176         
       
   177     if (!(err == KErrNone || err == KErrNotSupported))
       
   178         {
       
   179         User::Leave(err);
       
   180         }
       
   181     return (err == KErrNone);
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CSIPClient::Connection
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 CSIPClientConnection* CSIPClient::Connection (TUint32 aIapId) const
       
   189     {
       
   190     TInt index = FindIndex (aIapId);
       
   191     if (index >= 0)
       
   192         {
       
   193         return iConnections[index];
       
   194         }
       
   195     return 0;
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CSIPClient::SetSecurityHandlingL
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CSIPClient::SetSecurityHandlingL(TBool aEnabled)
       
   203     {
       
   204     TIpcArgs args(TIpcArgs::ENothing);
       
   205     args.Set(ESipItcArgSecurityHandling,aEnabled);        
       
   206     User::LeaveIfError(iSip->Send(ESipItcSetSecurityHandling,args));
       
   207     }
       
   208     
       
   209 // -----------------------------------------------------------------------------
       
   210 // CSIPClient::SetHttpDigestObserver
       
   211 // -----------------------------------------------------------------------------
       
   212 //    
       
   213 TInt CSIPClient::SetHttpDigestObserver(
       
   214     MSIPHttpDigestChallengeObserver& aObserver)
       
   215     {   
       
   216     TInt err = 
       
   217         SendHttpDigestObserverTypeToServer(ESipRealmHttpDigestObserver);
       
   218     
       
   219     if (!err)
       
   220         {
       
   221         iReceiver->SetHttpDigestObserver(aObserver);
       
   222         }
       
   223     
       
   224     return err;
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CSIPClient::SetHttpDigestObserver
       
   229 // -----------------------------------------------------------------------------
       
   230 //    
       
   231 TInt CSIPClient::SetHttpDigestObserver(
       
   232     MSIPHttpDigestChallengeObserver2& aObserver)
       
   233     {   
       
   234     TInt err = 
       
   235         SendHttpDigestObserverTypeToServer(ESipResponseHttpDigestObserver);
       
   236     
       
   237     if (!err)
       
   238         {
       
   239         iReceiver->SetHttpDigestObserver(aObserver);
       
   240         }
       
   241     
       
   242     return err;
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CSIPClient::RemoveHttpDigestObserver
       
   247 // -----------------------------------------------------------------------------
       
   248 //  
       
   249 void CSIPClient::RemoveHttpDigestObserver()
       
   250     {
       
   251     SendHttpDigestObserverTypeToServer(ESipNoHttpDigestObserver);
       
   252     iReceiver->RemoveHttpDigestObserver();
       
   253     }
       
   254 	
       
   255 // -----------------------------------------------------------------------------
       
   256 // CSIPClient::SetCredentialsL
       
   257 // -----------------------------------------------------------------------------
       
   258 //	
       
   259 void CSIPClient::SetCredentialsL(TUint32 aRequestId,
       
   260                                  TUint32 aRefreshId,
       
   261                                  const TDesC8& aRealm,
       
   262                                  const TDesC8& aUsername,
       
   263                                  const TDesC8& aPasswd,
       
   264                                  const TDesC8& aOutboundProxy)
       
   265     {    
       
   266     TIpcArgs args(TIpcArgs::ENothing);  
       
   267     args.Set(ESipItcArgRealm, &aRealm);
       
   268     
       
   269     TSIPIds ids;
       
   270     ids.iRequestId = aRequestId;
       
   271     ids.iRefreshId = aRefreshId;
       
   272     TPckgBuf<TSIPIds> idsPckg(ids);
       
   273     args.Set(ESipItcArgIds, &idsPckg);
       
   274 
       
   275     CBufFlat* credentialsBuf = CBufFlat::NewL(KCredentialsBufExpandSize);
       
   276 	CleanupStack::PushL(credentialsBuf);
       
   277 	RBufWriteStream writeStream(*credentialsBuf,0);
       
   278 	writeStream.PushL();
       
   279     writeStream.WriteUint32L(aUsername.Length());
       
   280     writeStream.WriteL(aUsername);
       
   281     writeStream.WriteUint32L(aPasswd.Length());
       
   282     writeStream.WriteL(aPasswd);
       
   283     TInt outboundProxyLength = aOutboundProxy.Length();
       
   284     writeStream.WriteUint32L(outboundProxyLength);
       
   285     if (outboundProxyLength > 0)
       
   286         {
       
   287         writeStream.WriteL(aOutboundProxy);
       
   288         writeStream.WriteUint32L(0); // end of credentials
       
   289         }
       
   290     writeStream.Pop();
       
   291     writeStream.Close();
       
   292     TPtr8 credentialsPtr(credentialsBuf->Ptr(0));
       
   293     args.Set(ESipItcArgCredentials, &credentialsPtr);
       
   294 
       
   295     User::LeaveIfError(iSip->Send(ESipItcSetCredentials,args));
       
   296 
       
   297     CleanupStack::PopAndDestroy(credentialsBuf);
       
   298     }
       
   299 			            
       
   300 // -----------------------------------------------------------------------------
       
   301 // CSIPClient::RemoveCredentials
       
   302 // -----------------------------------------------------------------------------
       
   303 //	
       
   304 TInt CSIPClient::RemoveCredentials(const TDesC8& aRealm)
       
   305     {
       
   306     TIpcArgs args(TIpcArgs::ENothing);
       
   307     args.Set(ESipItcArgRealm, &aRealm);
       
   308     return iSip->Send(ESipItcRemoveCredentials,args);
       
   309     }
       
   310 		
       
   311 // -----------------------------------------------------------------------------
       
   312 // CSIPClient::IgnoreChallenge
       
   313 // -----------------------------------------------------------------------------
       
   314 //	
       
   315 TInt CSIPClient::IgnoreChallenge(TUint32 aRequestId,
       
   316                                  TUint32 aRefreshId,
       
   317                                  const TDesC8& aRealm)
       
   318     {
       
   319     TIpcArgs args(TIpcArgs::ENothing);
       
   320     args.Set(ESipItcArgRealm, &aRealm);
       
   321     
       
   322     TSIPIds ids;
       
   323     ids.iRequestId = aRequestId;
       
   324     ids.iRefreshId = aRefreshId;
       
   325     TPckgBuf<TSIPIds> idsPckg(ids);
       
   326     args.Set(ESipItcArgIds, &idsPckg);
       
   327     
       
   328     return iSip->Send(ESipItcIgnoreChallenge,args);
       
   329     }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CSIPClient::AddL
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CSIPClient::AddL (CSIPClientConnection* aConnection)
       
   336     {
       
   337     __ASSERT_ALWAYS (aConnection, User::Leave(KErrArgument)); 
       
   338     TInt index = FindIndex (aConnection->IapId());
       
   339     if (index >= 0)
       
   340         {
       
   341         User::Leave (KErrAlreadyExists);
       
   342         }
       
   343     User::LeaveIfError (iConnections.Append(aConnection));
       
   344     }
       
   345     
       
   346 // -----------------------------------------------------------------------------
       
   347 // CSIPClient::Remove
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 TInt CSIPClient::Remove (const CSIPClientConnection* aConnection)
       
   351     {
       
   352     if (aConnection == 0)
       
   353         {
       
   354         return KErrArgument;
       
   355         }
       
   356     TInt index = FindIndex (aConnection->IapId());
       
   357     if (index >= 0)
       
   358         {
       
   359         iConnections.Remove(index);
       
   360         return KErrNone;
       
   361         }
       
   362     return KErrNotFound;
       
   363     }
       
   364 
       
   365 // -----------------------------------------------------------------------------
       
   366 // CSIPClient::FindIndex
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 TInt CSIPClient::FindIndex (TUint32 aIapId) const   
       
   370     {
       
   371     for (TInt i=0; i < iConnections.Count(); i++)
       
   372         {
       
   373         if (iConnections[i]->IapId() == aIapId)
       
   374             {
       
   375             return i;
       
   376             }
       
   377         }
       
   378     return KErrNotFound;
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CSIPClient::SendHttpDigestObserverTypeToServer
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 TInt CSIPClient::SendHttpDigestObserverTypeToServer(
       
   386     const TSipHttpDigestObserverType& aObserverType)
       
   387     {
       
   388     TIpcArgs args(TIpcArgs::ENothing);
       
   389     args.Set(ESipItcArgHttpDigestObserverType,aObserverType);        
       
   390     return iSip->Send(ESipItcSetHttpDigestObserver,args);
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CSIPClient::SIP
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 RSIP& CSIPClient::SIP ()
       
   398     {
       
   399     return *iSip;
       
   400     }