williamr@2: /*
williamr@2: * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2: * All rights reserved.
williamr@2: * This component and the accompanying materials are made available
williamr@4: * under the terms of "Eclipse Public License v1.0"
williamr@2: * which accompanies this distribution, and is available
williamr@4: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@2: *
williamr@2: * Initial Contributors:
williamr@2: * Nokia Corporation - initial contribution.
williamr@2: *
williamr@2: * Contributors:
williamr@2: *
williamr@2: * Description:
williamr@2: * Name          : SdpConnectionField.h
williamr@2: * Part of       : SDP Codec
williamr@2: * Interface     : SDK API, SDP Codec API
williamr@2: * Version       : 1.0
williamr@2: *
williamr@2: */
williamr@2: 
williamr@2: 
williamr@2: 
williamr@2: #ifndef CSDPCONNECTIONFIELD_H
williamr@2: #define CSDPCONNECTIONFIELD_H
williamr@2: 
williamr@2: //  INCLUDES
williamr@2: #include <e32base.h>
williamr@2: #include <in_sock.h>
williamr@2: #include <stringpool.h>
williamr@2: 
williamr@2: // CONSTANTS
williamr@2: const TUint KDefaultNumOfAddress = 1;
williamr@2: 
williamr@2: // FORWARD DECLARATIONS
williamr@2: class RReadStream;
williamr@2: class RWriteStream;
williamr@2: 
williamr@2: // CLASS DECLARATION
williamr@2: /**
williamr@2:  *  @publishedAll
williamr@2:  *  @released
williamr@2:  *
williamr@2:  *	This class encapsulates the connection information field of 
williamr@2:  *  the Session Description Protocol.
williamr@2:  *
williamr@2:  *	The normative reference for correct formatting and values is
williamr@2:  *	draft-ietf-mmusic-sdp-new-14 unless specified otherwise in
williamr@2:  *  member documentation. The implementation supports this normative
williamr@2:  *  reference, but does not enforce it fully. 
williamr@2:  *
williamr@2:  *  @lib sdpcodec.lib
williamr@2:  */
williamr@2: class CSdpConnectionField : public CBase
williamr@2: 	{
williamr@2:     public: // Constructors and destructor
williamr@2: 
williamr@2:         /**
williamr@2:          *	Constructs a new connection field.
williamr@2: 		 * 
williamr@2:          *	@param aText A string containing a correctly 
williamr@2:          *         formatted field value terminated by a CRLF.
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* DecodeL( const TDesC8& aText );
williamr@2: 
williamr@2:         /**
williamr@2:          *	Constructs a new connection field and adds the pointer to the 
williamr@2:          *  cleanup stack.
williamr@2: 		 *	
williamr@2:          *	@param aText A string containing a correctly 
williamr@2:          *         formatted field value terminated by a CRLF.
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* DecodeLC( const TDesC8& aText );
williamr@2: 
williamr@2:         /**
williamr@2:          *	Constructs a new connection field.
williamr@2: 		 *	Also sets the network type to "IN" and address type to "IP4" or
williamr@2: 		 *  "IP6" depending on the address family of aAddress.
williamr@2: 		 *	
williamr@2: 		 *	@param aAddress IP address from either KAfInet 
williamr@2:          *         or KAfInet6 family
williamr@2:          *  @param aTTL Time-To-Live for IP4 multicasts, set it as
williamr@2:          *         KErrNotFound if IPv6 multicast or IPv4 unicast
williamr@2:          *  @param aNumOfAddress Number of addresses in multicast,
williamr@2:          *         if unicast, must be 1
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* NewL(            
williamr@2:             const TInetAddr& aAddress,
williamr@2:             TInt aTTL = KErrNotFound, 
williamr@2:             TUint aNumOfAddress = KDefaultNumOfAddress );
williamr@2: 
williamr@2:         /**
williamr@2:          *	Constructs a new connection field.
williamr@2: 		 *	
williamr@2:          *	@param aNetType A valid network type name from the pre-
williamr@2:          *              defined SDP string table or defined by the user.
williamr@2: 		 *  @paramaAddressType A valid address type name from the 
williamr@2:          *              pre-defined SDP string table or defined by the user.
williamr@2: 		 *	@param  const TDesC8& aAddress: A valid address of the address type.
williamr@2:          *              Can contain TTL & number of addresses parameter as well.
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* NewL(
williamr@2: 			RStringF aNetType, 
williamr@2: 			RStringF aAddressType, 
williamr@2: 			const TDesC8& aAddress );
williamr@2: 
williamr@2: 		/**
williamr@2:          *	Constructs a new connection field and adds the pointer to the 
williamr@2:          *  cleanup stack. Also sets the network type to "IN" and address type
williamr@2: 		 *	to "IP4" or "IP6" depending on the address family of aAddress.
williamr@2: 		 *
williamr@2: 		 *	@param aAddress IP address from either KAfInet 
williamr@2:          *              or KAfInet6 family
williamr@2:          *  @param aTTL Time-To-Live for IP4 multicasts, set it as
williamr@2:          *              KErrNotFound if IPv6 multicast or IPv4 unicast
williamr@2:          *  @param aNumOfAddress Number of addresses in multicast,
williamr@2:          *              if unicast, must be 1
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* NewLC(       
williamr@2:             const TInetAddr& aAddress, 
williamr@2:             TInt aTTL = KErrNotFound,
williamr@2:             TUint aNumOfAddress = KDefaultNumOfAddress );
williamr@2: 
williamr@2:         /**
williamr@2:          *	Construct a new connection field  and adds the pointer to the
williamr@2:          *  cleanup stack.
williamr@2: 		 *	
williamr@2:          *	@param aNetType A valid network type name from the pre-
williamr@2:          *         defined SDP string table or defined by the user
williamr@2: 		 *  @paramaAddressType A valid address type name from the 
williamr@2:          *              pre-defined SDP string table or defined by the user
williamr@2: 		 *	@param  const TDesC8& aAddress: A valid address of the address type.
williamr@2:          *              Can contain TTL & number of addresses parameter as well.
williamr@2:          *	@return a new instance.
williamr@2:          */
williamr@2: 		IMPORT_C static CSdpConnectionField* NewLC(
williamr@2:             RStringF aNetType, RStringF aAddressType, const TDesC8& aAddress );
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Deletes the resources held by the instance.
williamr@2: 		 */
williamr@2: 		IMPORT_C ~CSdpConnectionField();
williamr@2: 
williamr@2:     public: // New functions
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Outputs the field formatted according to SDP syntax and including
williamr@2: 		 *  the terminating CRLF.
williamr@2: 		 * 
williamr@2: 		 *	@param aStream: Stream used for output. On return the
williamr@2:          *         stream includes correctly formatted connection field.
williamr@2: 		 */
williamr@2: 		IMPORT_C void EncodeL( RWriteStream& aStream ) const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Creates a new instance that is equal to the target.
williamr@2: 		 *
williamr@2: 		 *	@return a new instance.
williamr@2: 		 */
williamr@2: 		IMPORT_C CSdpConnectionField* CloneL() const;
williamr@2: 
williamr@2: 		/**	
williamr@2: 		 *	Compares this instance to another for equality.
williamr@2: 		 *
williamr@2: 		 *	@param const CSdpConnectionField& aObj: The instance to compare to.
williamr@2: 		 *	@return ETrue if equal, EFalse if not.
williamr@2: 		 */
williamr@2: 		IMPORT_C TBool operator== ( const CSdpConnectionField& aObj ) const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Gets the network type that is from the pre-defined SDP string table
williamr@2:          *  or given by the user.
williamr@2: 		 *
williamr@2: 		 *	@return The network type.
williamr@2: 		 */
williamr@2: 		IMPORT_C RStringF NetType() const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Gets the address type that is from the pre-defined SDP string table 
williamr@2:          *  or given by the user.
williamr@2: 		 *
williamr@2: 		 *	@return The address type.
williamr@2: 		 */
williamr@2: 		IMPORT_C RStringF AddressType() const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Gets the address.
williamr@2: 		 *
williamr@2: 		 *	@return Address as an IP address or null if it is
williamr@2:          *          not an IP address. This may be e.g. when the address
williamr@2: 		 *          has been specified as a FQDN. In this case, the address
williamr@2:          *          can be accessed using the other getters.
williamr@2: 		 */
williamr@2: 		IMPORT_C const TInetAddr* InetAddress() const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Gets the address.
williamr@2: 		 *
williamr@2: 		 *  @return Address as a string.
williamr@2: 		 */
williamr@2: 		IMPORT_C const TDesC8& Address() const;
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Sets the address, network and address type. Also sets the network 
williamr@2: 		 *	type to "IN" and address type to "IP4" or "IP6" depending on the 
williamr@2: 		 *  address family of aAddress.
williamr@2: 		 *
williamr@2: 		 *	@param aValue The new address.
williamr@2:          *  @param aTTL Time-To-Live for IP4 multicasts, set it as
williamr@2:          *         KErrNotFound if IPv6 multicast or IPv4 unicast
williamr@2:          *  @param aNumOfAddress Number of addresses in multicast,
williamr@2:          *         if unicast, must be 1.
williamr@2:          *  @leave KErrSdpCodecConnectionField ifaddress to be set is wrongly 
williamr@2:          *         formatted.
williamr@2:          */
williamr@2: 		IMPORT_C void SetInetAddressL( const TInetAddr& aValue, 
williamr@2:                                    TInt aTTL = KErrNotFound,
williamr@2:                                    TUint aNumOfAddress = KDefaultNumOfAddress);
williamr@2: 
williamr@2: 		/**
williamr@2: 		 *	Sets the address, network and address type.
williamr@2: 		 *
williamr@2:          *	@param aNetType A valid network type name from the pre-
williamr@2:          *         defined SDP string table or defined by the user
williamr@2: 		 *  @param aAddressType A valid address type name from the 
williamr@2:          *         pre-defined SDP string table or defined by the user
williamr@2: 		 *	@param aAddress A valid address of the address type.
williamr@2: 		 */
williamr@2: 		IMPORT_C void SetAddressL( RStringF aNetType, 
williamr@2: 								   RStringF aAddressType, 
williamr@2: 								   const TDesC8& aAddress );
williamr@2: 
williamr@2:         /**
williamr@2:          *  Gets TTL attribute.
williamr@2:          *
williamr@2:          *  @return TTL or KErrNotFound, if one is not available for the address
williamr@2:          */
williamr@2:         IMPORT_C TInt TTLValue() const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Sets TTL attribute (only valid for IP4 multicasts).
williamr@2:          *  Leaves if trying to set TTL to address that doesn't support it.
williamr@2:          *
williamr@2:          *  @param aTTL Time-To-Live for IP4 multicasts
williamr@2:          *	@leave KErrSdpCodecConnectionField if aTTL is invalid.
williamr@2:          */
williamr@2:         IMPORT_C void SetTTLL( TInt aTTL );
williamr@2: 
williamr@2:         /**
williamr@2:          *  Gets the number of addresses (can be more than 1 for multicasts).
williamr@2:          *  Multicast addresses are contiguously allocated above the base 
williamr@2:          *  address.
williamr@2:          *
williamr@2:          *  @return Number of addresses
williamr@2:          */
williamr@2:         IMPORT_C TInt NumOfAddress() const;
williamr@2:          
williamr@2:         /**
williamr@2:          *  Sets the number of addreses allocated for multicast. 
williamr@2:          *  
williamr@2:          *	@param aNumOfAddress Number of addresses in multicast
williamr@2:          *  @leave KErrSdpCodecConnectionField if the address is unicast.
williamr@2:          */
williamr@2:         IMPORT_C void SetNumOfAddressL( TUint aNumOfAddress );
williamr@2: 
williamr@2:     public:     // Internal to codec
williamr@2: 
williamr@2:         /**
williamr@2:          *  Externalizes the object to stream
williamr@2:          *
williamr@2:          *  @param aStream Stream where the object's state will be stored
williamr@2:          */
williamr@2: 		void ExternalizeL( RWriteStream& aStream ) const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Creates object from the stream data
williamr@2:          *
williamr@2:          *  @param aStream Stream where the object's state will be read
williamr@2:          *  @return Initialized object
williamr@2:          */
williamr@2: 		static CSdpConnectionField* InternalizeL( RReadStream& aStream );
williamr@2: 
williamr@2:     private:    // Internal
williamr@2: 
williamr@2:         /**
williamr@2:          *  Constructor
williamr@2:          */
williamr@2:         CSdpConnectionField();
williamr@2: 
williamr@2:         /**
williamr@2:          *  2nd phase constructor
williamr@2:          *
williamr@2:          *	@param aText A string containing a correctly formatted field value
williamr@2: 		 *         terminated by a CRLF.         
williamr@2:          */         
williamr@2:         void ConstructL( const TDesC8& aText );
williamr@2:         
williamr@2:         /**
williamr@2:          *  2nd phase constructor
williamr@2:          *
williamr@2:          *	@param aAddress IP address from either KAfInet or KAfInet6 family
williamr@2:          *  @param aTTL Time-To-Live for IP4 multicasts
williamr@2:          *  @param aNumOfAddress Nubmer of addresses in multicast
williamr@2:          */
williamr@2:         void ConstructL( const TInetAddr& aAddress,
williamr@2:                          TInt aTTL, TUint aNumOfAddress );
williamr@2: 
williamr@2:         /**
williamr@2:          *  2nd phase constructor
williamr@2:          *
williamr@2:          *	@param aNetType A valid network type name from the pre-defined
williamr@2: 		 *         SDP string table or defined by the user
williamr@2: 		 *  @param aAddressType A valid address type name from the pre-defined
williamr@2: 		 *         SDP string table or defined by the user
williamr@2: 		 *	@param aAddress A valid address of the address type
williamr@2:          */
williamr@2:         void ConstructL( RStringF aNetType, RStringF aAddressType, 
williamr@2: 	                     const TDesC8& aAddress );
williamr@2: 
williamr@2:         /**
williamr@2:          *  Checks if the address is valid against given arguments
williamr@2:          *
williamr@2:          *  @param aAddressTypeIP4 The given type of address (EFalse = IP6)
williamr@2:          *  @param aAddress Address with possibly TTL & number of addresses
williamr@2:          *  @return error code (KErrNone if valid)
williamr@2:          */
williamr@2:         TInt IsValidAddress( TBool aAddressTypeIP4,
williamr@2:                              const TDesC8& aAddress ) const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Checks if the address is valid against given arguments
williamr@2:          *         
williamr@2:          *  @param aAddress Address in TInetAddr format
williamr@2:          *  @param aTTL TTL attribute
williamr@2:          *  @param aNumOfAddress Number off addresses
williamr@2:          *  @return error code (KErrNone if valid)
williamr@2:          */
williamr@2:         TInt IsValidAddress( const TInetAddr& aAddress, 
williamr@2:                              TInt aTTL, TUint aNumOfAddress ) const;
williamr@2:         
williamr@2:         /**
williamr@2:          *  Parses address field
williamr@2:          *
williamr@2:          *  @param aAddressTypeIP4 The given type of address (EFalse = IP6)
williamr@2:          *  @param aAddress Address with possibly TTL & number of addresses
williamr@2:          *  @param aTTL TTL value is stored here (or KErrNotFound)
williamr@2:          *  @param aNumberOfAddresses Range of addreses
williamr@2:          *  @return The address
williamr@2:          */
williamr@2:         HBufC8* ParseAddressFieldL( TBool aAddressTypeIP4,
williamr@2:                                     const TDesC8& aAddress,
williamr@2:                                     TInt& aTTL,
williamr@2:                                     TUint& aNumberOfAddresses ) const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Parses IP4 address
williamr@2:          *
williamr@2:          *  @param aPos Position of the (first) separation mark         
williamr@2:          *  @param aTTL TTL value is stored here (or KErrNotFound)
williamr@2:          *  @param aAddr Addres in TInetAddr format
williamr@2:          *  @param aAddress The whole address field
williamr@2:          *  @param aNumberOfAddresses Range of addreses
williamr@2:          *  @return The address
williamr@2:          */
williamr@2:         HBufC8* ParseIP4AddressL( TInt aPos, 
williamr@2:                                   TInetAddr& aAddr,
williamr@2:                                   const TDesC8& aAddress,
williamr@2:                                   TInt& aTTL,
williamr@2:                                   TUint& aNumberOfAddresses ) const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Parses IP6 address
williamr@2:          *
williamr@2:          *  @param aPos Position of the (first) separation mark         
williamr@2:          *  @param aTTL TTL value is stored here (or KErrNotFound)
williamr@2:          *  @param aAddr Addres in TInetAddr format
williamr@2:          *  @param aAddress The whole address field
williamr@2:          *  @param aNumberOfAddresses Range of addreses
williamr@2:          *  @return The address
williamr@2:          */
williamr@2:         HBufC8* ParseIP6AddressL( TInt aPos, 
williamr@2:                                   TInetAddr& aAddr,
williamr@2:                                   const TDesC8& aAddress,
williamr@2:                                   TInt& aTTL,
williamr@2:                                   TUint& aNumberOfAddresses ) const;
williamr@2: 
williamr@2:         /**
williamr@2:          *  Copies given network type to iNetType and verifies aNetType to
williamr@2:          *  be valid
williamr@2:          *
williamr@2:          *  @param aNetType Given network type                
williamr@2:          */
williamr@2:         void CopyNetTypeL( const TDesC8& aNetType );
williamr@2: 
williamr@2:         /**
williamr@2:          *  Copies given address type to iAddressType and verifies aAddrType
williamr@2:          *  to be valid
williamr@2:          * 
williamr@2:          *  @param aAddrType Given address type     
williamr@2:          */
williamr@2:         void CopyAddressTypeL( const TDesC8& aAddrType );
williamr@2: 
williamr@2:         /**
williamr@2:          *  Copies address to iAddress, and initalizes TTL & number of addresses,
williamr@2:          *  leaves on error
williamr@2:          *  
williamr@2:          *  @param aAddress Address string, which can also contain TTL
williamr@2:          *                  and number of addresses attributes
williamr@2:          */
williamr@2:         void CopyAddressL( const TDesC8& aAddress, RStringPool aPool );
williamr@2: 
williamr@2:     private:    // Data
williamr@2: 
williamr@2:         // <network type>
williamr@2:         RStringF iNetType;
williamr@2:         
williamr@2:         // <address type>        
williamr@2:         RStringF iAddressType;
williamr@2: 
williamr@2:         // mutable TInetAddr for InetAddress()
williamr@2:         mutable TInetAddr iInetAddress;
williamr@2: 
williamr@2:         // Address in text format
williamr@2:         HBufC8* iAddress;
williamr@2: 
williamr@2:         // TTL for IP4 multicast addresses
williamr@2:         TInt iTTL;
williamr@2:         // Number of addresses
williamr@2:         TUint iNumOfAddress;
williamr@2: 
williamr@2:         // String pool
williamr@2:         RStringPool iPool;
williamr@2: 
williamr@4: 		void __DbgTestInvariant() const;
williamr@4: 
williamr@4: 
williamr@2: 	};
williamr@2: 
williamr@2: #endif // CSDPCONNECTIONFIELD_H