natfw/natfwunsaf_protocols/unsaf_codec/inc/natfwunsafmessage.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #ifndef CNATFWUNSAFMESSAGE_H
       
    22 #define CNATFWUNSAFMESSAGE_H
       
    23 
       
    24 // INCLUDES
       
    25 #include <e32base.h>
       
    26 #include "natfwinternaldefs.h"    //UNIT_TEST
       
    27 #include "natfwunsaftransactionid.h"
       
    28 #include "natfwunsafattribute.h"
       
    29 
       
    30 // FORWARD DECLARATIONS
       
    31 
       
    32 // CLASS DECLARATION
       
    33 /**
       
    34 * An abstract base class for NATFWUNSAF requests, indications and responses.
       
    35 *
       
    36 *  @lib natfwunsafprotocols.lib
       
    37 */
       
    38 class CNATFWUNSAFMessage : public CBase
       
    39     {
       
    40     public: // Enumerations
       
    41 
       
    42         enum TMethod // Values are from RFC 3489 and TURN draft
       
    43             {
       
    44             EBinding = 0x0001,
       
    45             ESharedSecret = 0x0002,
       
    46             EAllocate = 0x0003,
       
    47             ESetActiveDestination = 0x0004,
       
    48             EConnect = 0x0005,
       
    49             //NOTE: ESend changed from 0x0006 to conform with old TURN draft-03
       
    50             //to get things to work in our test infra for now
       
    51             ESend = 0x0001,
       
    52             //NOTE: EData changed from 0x0007 to conform with old TURN draft-03
       
    53             //to get things to work in our test infra for now
       
    54             EData = 0x0002,
       
    55             EConnectStatus = 0x0008
       
    56             };
       
    57 
       
    58         enum TClass // Values are from RFC 3489 bis 08
       
    59             {
       
    60             ERequestMask = 0x0000,
       
    61             EIndicationMask = 0x0010,
       
    62             EResponseMask = 0x0100,
       
    63             EErrorResponseMask = 0x0110 
       
    64             };
       
    65 
       
    66         enum TType // Currently supported message types
       
    67             {
       
    68             EBindingRequest =
       
    69                 EBinding | ERequestMask,
       
    70             EBindingIndication =
       
    71                 EBinding | EIndicationMask,
       
    72             EBindingResponse =
       
    73                 EBinding | EResponseMask,
       
    74             EBindingErrorResponse =
       
    75                 EBinding | EErrorResponseMask,
       
    76             ESharedSecretRequest =
       
    77                 ESharedSecret | ERequestMask,
       
    78             ESharedSecretResponse =
       
    79                 ESharedSecret | EResponseMask,
       
    80             ESharedSecretErrorResponse =
       
    81                 ESharedSecret | EErrorResponseMask,
       
    82             EAllocateRequest =
       
    83                 EAllocate | ERequestMask,
       
    84             EAllocateResponse =
       
    85                 EAllocate | EResponseMask,
       
    86             EAllocateErrorResponse =
       
    87                 EAllocate | EErrorResponseMask,
       
    88             ESendIndication =
       
    89                 ESend | EIndicationMask,
       
    90             EDataIndication =
       
    91                 EData | EIndicationMask,
       
    92             ESetActiveDestinationRequest =
       
    93                 ESetActiveDestination | ERequestMask,
       
    94             ESetActiveDestinationResponse =
       
    95                 ESetActiveDestination | EResponseMask,
       
    96             ESetActiveDestinationErrorResponse =
       
    97                 ESetActiveDestination | EErrorResponseMask,
       
    98             EConnectRequest =
       
    99                 EConnect | ERequestMask,
       
   100             EConnectResponse =
       
   101                 EConnect | EResponseMask,
       
   102             EConnectErrorResponse =
       
   103                 EConnect | EErrorResponseMask,
       
   104             EConnectStatusIndication =
       
   105                 EConnectStatus | EIndicationMask
       
   106             };
       
   107 
       
   108         enum THeader // RFC 3489 bis 08
       
   109             {
       
   110             EMessageTypeOffset     = 0,
       
   111             EMessageLengthOffset   = 2,
       
   112             EMagicCookieOffset     = 4,
       
   113             ETransactionIDOffset   = 8,
       
   114 
       
   115             EMessageTypePrefixMask = 0xc000,
       
   116             EMessageTypeMask       = 0x3fff,
       
   117             EMessageMethodMask     = 0x0001,
       
   118             EMessageClassMask      = 0x0110,
       
   119             EMagicCookie           = 0x2112A442,
       
   120             EHeaderSize            = 20
       
   121             };
       
   122 
       
   123     public: // Constructors and destructor
       
   124 
       
   125         /**
       
   126         * Destructor, deletes the resources of CNATFWUNSAFMessage.
       
   127         */
       
   128         virtual ~CNATFWUNSAFMessage();
       
   129 
       
   130 
       
   131     public: // New pure virtual functions
       
   132 
       
   133         /**
       
   134         * The type of the UNSAF message
       
   135         * @return UNSAF message type
       
   136         */
       
   137         virtual TType Type() const = 0;
       
   138 
       
   139         /**
       
   140         * Validates the UNSAF message. In other words checks that
       
   141         * all the mandatory attributes are present.
       
   142         * @return ETrue if the message is valid. Otherwise EFalse.
       
   143         */
       
   144         virtual TBool Validate() const = 0;
       
   145 
       
   146         /**
       
   147         * Determine if the specified attribute is allowed to be present in the
       
   148         * UNSAF message in question.
       
   149         * @return ETrue  Attribute is allowed
       
   150         *          EFalse Otherwise
       
   151         */
       
   152         virtual TBool IsAllowed(TUint16 aAttributeType) const = 0;
       
   153 
       
   154     public: // New functions
       
   155 
       
   156         /**
       
   157         * Obtains the transaction ID.
       
   158         * @return NATFWUNSAF transaction ID
       
   159         */
       
   160         IMPORT_C TNATFWUNSAFTransactionID TransactionID() const;
       
   161 
       
   162         /**
       
   163         * Checks whether a UNSAF attribute is present in this UNSAF message.
       
   164         * @param aType the attribute type
       
   165         * @return ETrue if the attribute is present, otherwise EFalse.
       
   166         */
       
   167         IMPORT_C TBool HasAttribute(CNATFWUNSAFAttribute::TType aType) const;
       
   168 
       
   169         /**
       
   170         * Gets a UNSAF attribute from the UNSAF message.
       
   171         * @param aType the attribute type
       
   172         * @return NULL if the attribute is not present.
       
   173         *         Otherwise a pointer to the attribute.
       
   174         *         The ownership is not transferred.
       
   175         */
       
   176         IMPORT_C CNATFWUNSAFAttribute* Attribute(
       
   177             CNATFWUNSAFAttribute::TType aType) const;
       
   178 
       
   179         /**
       
   180         * Adds a UNSAF attribute to the UNSAF message.
       
   181         * @param aAttribute the attribute to be added.
       
   182         *        The ownership is transferred.
       
   183         */
       
   184         IMPORT_C void AddAttributeL(CNATFWUNSAFAttribute* aAttribute);
       
   185 
       
   186         /**
       
   187         * Detaches a UNSAF attribute from the UNSAF message.
       
   188         * @param aAttribute the attribute to be detached.
       
   189         *        The caller gets the ownership of the attribute.
       
   190         */
       
   191         IMPORT_C void DetachAttribute(const CNATFWUNSAFAttribute* aAttribute);
       
   192 
       
   193         /**
       
   194         * Removes and deletes all attribute of the specified type from UNSAF
       
   195         * message.
       
   196         * @param aType Attribute type to delete
       
   197         */
       
   198         IMPORT_C void DeleteAttribute(TUint16 aType);
       
   199 
       
   200         /**
       
   201         * Encodes a UNSAF message without MESSAGE-INTEGRITY attribute.
       
   202         * @return The encoded UNSAF message. The ownership is transferred.
       
   203         */
       
   204         IMPORT_C CBufBase* EncodeL() const;
       
   205 
       
   206          /**
       
   207         * Encodes a UNSAF message and adds MESSAGE-INTEGRITY attribute.
       
   208         * @param aSharedSecret shared secret used to calculate the
       
   209         *        value of the MESSAGE-INTEGRITY attribute.
       
   210         * @param  aUseFingerprint flag indicating whether to add a FINGERPRINT
       
   211         *         attribute.
       
   212         * @return The encoded UNSAF message. The ownership is transferred.
       
   213         */
       
   214         IMPORT_C CBufBase* EncodeL(const TDesC8& aSharedSecret,
       
   215             TBool aUseFingerprint=EFalse) const;
       
   216 
       
   217         /**
       
   218         * When decoding the UNSAF message, an unrecognized mandatory attribute
       
   219         * has been encountered.
       
   220         * @post iUnknownMandatoryAttributes == ETrue
       
   221         */
       
   222         void UnknownMandatoryAttributeFound();
       
   223 
       
   224         /**
       
   225         * Tells if there were unrecognized mandatory attributes when the
       
   226         * message was decoded. These attributes have not been decoded.
       
   227         * @return ETrue Unrecognized mandatory attributes were encountered
       
   228         *         EFalse Otherwise
       
   229         */
       
   230         IMPORT_C TBool HasUnknownMandatoryAttributes() const;
       
   231 
       
   232         /**
       
   233         * Retrieves the UNSAF message length from a UNSAF message header.
       
   234         * @param aNATFWUNSAFMessage a UNSAF message
       
   235         * @return KErrNotFound if length field cannot be parsed.
       
   236         *         Otherwise the value of the length field.
       
   237         */
       
   238         static TInt MessageLength(const TDesC8& aNATFWUNSAFMessage);
       
   239 
       
   240         /**
       
   241         * Writes the length field into NATFWUNSAF header.
       
   242         * @param aNATFWUNSAFMessage Encoded UNSAF message whose length is to be
       
   243         *    calculated and written.
       
   244         */
       
   245         static void SetMessageLength(CBufBase& aNATFWUNSAFMessage);
       
   246 
       
   247         /**
       
   248         * Writes the length field into NATFWUNSAF header.
       
   249         * @param aNATFWUNSAFMessage Encoded UNSAF message whose
       
   250         *        length is to be set.
       
   251         * @param aLength Value to write into the length field.
       
   252         */
       
   253         static void SetMessageLength(CBufBase& aNATFWUNSAFMessage,
       
   254             TInt aLength);
       
   255 
       
   256     protected: // Constructors
       
   257 
       
   258         CNATFWUNSAFMessage(const TNATFWUNSAFTransactionID& aTransactionID);
       
   259 
       
   260     private:
       
   261 
       
   262         /**
       
   263         * Encodes the UNSAF message header.
       
   264         * @return The encoded UNSAF message header. The ownership is
       
   265         * transferred.
       
   266         */
       
   267         CBufBase* EncodeMessageHeaderLC() const;
       
   268 
       
   269     private: // Data
       
   270 
       
   271         TNATFWUNSAFTransactionID iTransactionID;
       
   272 
       
   273         //Owned
       
   274         RPointerArray<CNATFWUNSAFAttribute> iAttributes;
       
   275 
       
   276         //ETrue if an unrecognized mandatory attribute was encountered when
       
   277         //decoding this UNSAF message.
       
   278         TBool iUnknownMandatoryAttributes;
       
   279 
       
   280     private: // For testing purposes
       
   281 
       
   282         UNIT_TEST(UT_CNATFWUNSAFMessage)
       
   283         UNIT_TEST(UT_CNATFWUNSAFMessageFactory)
       
   284         UNIT_TEST(UT_CNATFWUNSAFBindingRequest)
       
   285         UNIT_TEST(UT_CNATFWUNSAFAllocateRequest)
       
   286         UNIT_TEST(UT_CNATFWUNSAFConnectRequest)
       
   287         UNIT_TEST(UT_CNATFWUNSAFSendIndication)
       
   288         UNIT_TEST(UT_CNATFWUNSAFSetActiveDestinationRequest)
       
   289         UNIT_TEST(UT_CNATFWUNSAFTcpRelayPacketFactory)
       
   290         UNIT_TEST(UT_CNATFWUNSAFBindingResponse)
       
   291     };
       
   292 
       
   293 #endif // CNATFWUNSAFMESSAGE_H
       
   294 
       
   295