realtimenetprots/sipfw/SIP/Codec/src/CSIPHostPort.cpp
author Petteri Saari <petteri.saari@digia.com>
Thu, 25 Nov 2010 13:59:42 +0200
branchMSRP_FrameWork
changeset 58 cdb720e67852
parent 0 307788aac0a8
permissions -rw-r--r--
This release addresses the following issues: 1. The crash bug fix when receiving file 2. Now the sending is based on MSRP messages, there is no longer file receiving or sending. Client sends data as MSRP was designed. 3. Soma MSRP stack was created so that the client told the correct session-id, Symbian stack generated it by itself. This is not allowed, it was changed so that clients tell the session-id (same as used in SIP INVITE). 4. Unnecessary division of data to chunks removed when there is no need to interrupt sending. The message is sent in as few chunks as possible. 5. Stack can now receive files and chunks with ?unlimited? size. Old stack wrote the incoming data to memory and did not utilize disk space until the end of chunk was reached (large chunks from another client crashed it). 6. Now when writing the incoming data to file, it will take into account the byte-range header values. So, this complies with the RFC4975 requirements that stack must be able to handle chunks that come in any sequence. 7. Some buffering changes to outgoing/incoming data. 8. The outgoing data is now checked that it does not contain the created transaction-id before sending the data. 9. MSRP success reports are now implemented and tested against servers. 10. Progress report system fixed so progress is now visible on client (all the way to 100%). 11. Message Cancel receiving / Cancel sending now corrected and made to work as rfc4975 requires. (termination from sender and error code from receiver when cancelling). 12. Bug correction related to messages received not belonging to any session, old stack implementation did send error response, but after response was written it did give the buffer to client anyway. Now corrected.

// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
// Name          : CSIPHostPort.cpp
// Part of       : SIP Codec
// Version       : SIP/4.0 
//




#include "siphostport.h"
#include "sipcodecerr.h"
#include "SIPSyntaxCheck.h"
#include "_sipcodecdefs.h"

_LIT8(KPortFormat, "%u");


// -----------------------------------------------------------------------------
// CSIPHostPort::DecodeL
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPHostPort* CSIPHostPort::DecodeL (const TDesC8& aValue)
	{
	__ASSERT_ALWAYS (aValue.Length() > 0, User::Leave(KErrSipCodecHostPort));

	CSIPHostPort* hostPort = new(ELeave)CSIPHostPort;
    CleanupStack::PushL (hostPort);

	if (aValue.Locate('[') == 0) // seems to be an IPv6 address
		{
		TInt hostEndPos = aValue.Locate(']');
		if (hostEndPos > 1)
			{
			hostPort->SetHostL (aValue.Mid(1,hostEndPos-1));
			if (hostEndPos < aValue.Length()-1) // seems to have a port
				{
				TLex8 lex(aValue.Mid(hostEndPos+1));
				lex.SkipSpace();
				TPtrC8 portAsText(lex.Remainder());
				if (portAsText.Length() > 0)
					{
					TInt colonPos = portAsText.Locate (':');
					if (colonPos != 0) User::Leave(KErrSipCodecPort);
					hostPort->SetPort (ParsePortL(portAsText.Mid(1)));
					}
				}
			}
		else
			{
			User::Leave(KErrSipCodecHostPort);
			}
	    __ASSERT_ALWAYS (hostPort->HostType() == ESIPIpv6, 
	                     User::Leave(KErrSipCodecHostPort));
		}
	else // seems to be an IPv4 address or a host name 
		{
		TInt colonPos = aValue.Locate(':');
		if (colonPos == 0 || colonPos == aValue.Length()-1)
			{
			User::Leave(KErrSipCodecPort);
			}
		if (colonPos > 0) // has port
			{
			hostPort->SetPort (ParsePortL (aValue.Mid(colonPos+1)));
			hostPort->SetHostL (aValue.Left(colonPos));
			}
		else // has only host part
			{
			hostPort->SetHostL (aValue);
			}
		}
	CleanupStack::Pop(hostPort);
    
	return hostPort;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::NewL
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPHostPort* CSIPHostPort::NewL (const CSIPHostPort& aHostPort)
	{
	CSIPHostPort* self = CSIPHostPort::NewLC (aHostPort);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::NewLC
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPHostPort* CSIPHostPort::NewLC (const CSIPHostPort& aHostPort)
	{
	CSIPHostPort* self = new(ELeave)CSIPHostPort;
	CleanupStack::PushL(self);
	self->ConstructL (aHostPort);
	return self;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::CSIPHostPort
// -----------------------------------------------------------------------------
//
CSIPHostPort::CSIPHostPort()
 : iHostType (ESIPNoHost),
   iHasPort (EFalse)
	{
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::ConstructL
// -----------------------------------------------------------------------------
//
void CSIPHostPort::ConstructL ()
	{
	iHost = HBufC8::NewL(0);
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::ConstructL
// -----------------------------------------------------------------------------
//
void CSIPHostPort::ConstructL (const CSIPHostPort& aHostPort)
	{
	iHost = aHostPort.iHost->AllocL();
	iHostType = aHostPort.iHostType;
	iHasPort = aHostPort.iHasPort;
	iPort = aHostPort.iPort;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::~CSIPHostPort
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPHostPort::~CSIPHostPort()
	{
	delete iHost;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::SetHostL
// -----------------------------------------------------------------------------
//
EXPORT_C void CSIPHostPort::SetHostL (const TDesC8& aHost)
	{
	HBufC8* tmp = aHost.AllocLC();
	tmp->Des().Trim();
	if (tmp->Length() == 0) 
        {
        User::Leave(KErrSipCodecHost);
        }

	TType hostType;
	TInt err = SIPSyntaxCheck::HostType(*tmp,hostType);
	if (err != KErrNone) 
        {
        User::Leave (err);
        }

	CleanupStack::Pop(tmp);

	delete iHost;
	iHost = tmp;
	iHostType = hostType;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::HostType
// -----------------------------------------------------------------------------
//
EXPORT_C CSIPHostPort::TType CSIPHostPort::HostType () const
	{
	return iHostType;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::Host
// -----------------------------------------------------------------------------
//
EXPORT_C const TDesC8& CSIPHostPort::Host () const
	{
	return *iHost;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::HasPort
// -----------------------------------------------------------------------------
//
EXPORT_C TBool CSIPHostPort::HasPort() const
	{
	return iHasPort;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::Port
// -----------------------------------------------------------------------------
//
EXPORT_C TUint CSIPHostPort::Port() const
	{
	if (iHasPort) 
        {
        return iPort;
        }
	return 0;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::SetPort
// -----------------------------------------------------------------------------
//
EXPORT_C void CSIPHostPort::SetPort (TUint aPort)
	{
	iPort = aPort;
	iHasPort = ETrue;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::DeletePort
// -----------------------------------------------------------------------------
//
EXPORT_C TInt CSIPHostPort::DeletePort ()
	{
	if (!iHasPort) 
        {
        return KErrNotFound;
        }
	iPort = 0;
	iHasPort = EFalse;
	return KErrNone;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::operator==
// -----------------------------------------------------------------------------
//
EXPORT_C TBool CSIPHostPort::operator==(const CSIPHostPort& aHostPort) const
	{
	if (iHostType != aHostPort.iHostType) 
        {
        return EFalse;
        }
	if (iHost->CompareF(*(aHostPort.iHost)) != 0) 
        {
        return EFalse;
        }
	if (iHasPort != aHostPort.iHasPort) 
        {
        return EFalse;
        }
	if (iHasPort)
		{
		if (iPort != aHostPort.iPort) 
            {
            return EFalse;
            }
		}
	return ETrue;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::ToTextLC
// -----------------------------------------------------------------------------
//
HBufC8* CSIPHostPort::ToTextLC () const
	{
	TUint encodedLength = iHost->Length();
	if (iHostType == ESIPIpv6)
	    {
	    const TInt KBracketsLength = 2; // []
	    encodedLength += KBracketsLength;
	    }
	
	const TInt KMaxPortAsText = 40;
	TBuf8<KMaxPortAsText> portAsText;
	if (iHasPort)
		{
		portAsText.Format(KPortFormat, iPort);
		encodedLength += 1 + portAsText.Length(); // COLON + port
		}
	
	HBufC8* encodedHostPort = HBufC8::NewLC(encodedLength);
	TPtr8 encodedHostPortPtr = encodedHostPort->Des();
	if (iHostType == ESIPIpv6)
		{
		encodedHostPortPtr.Append('[');
		encodedHostPortPtr.Append(*iHost);
		encodedHostPortPtr.Append(']');
		}
	else
		{
		encodedHostPortPtr.Append(*iHost);
		}

	if (iHasPort)
		{
		encodedHostPortPtr.Append(':');
		encodedHostPortPtr.Append(portAsText);
		}
	return encodedHostPort;
	}

// -----------------------------------------------------------------------------
// CSIPHostPort::ParsePortL
// -----------------------------------------------------------------------------
//
TUint CSIPHostPort::ParsePortL (const TDesC8& aValue)
	{
	TLex8 lex(aValue);
	lex.SkipSpace();
	TPtrC8 remainder(lex.Remainder());
    if (remainder.Length() == 0) 
        {
        User::Leave(KErrSipCodecPort);
        }
	TUint parsedValue=0;
	if (lex.Val(parsedValue) != KErrNone) 
        {
        User::Leave (KErrSipCodecPort);
        }
    lex.SkipSpace();
	if (lex.Remainder().Length() != 0) 
        {
        User::Leave (KErrSipCodecPort);
        }
	return parsedValue;
	}