natfw/natfwunsaf_protocols/unsaf_codec/src/natfwunsaftcprelaypacket.cpp
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 /*
       
    22 TCP Relay packet header:
       
    23 
       
    24     0                   1                   2                   3
       
    25     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       
    26    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    27    | Relay type    | Reserved=0x00 |           Data length         |
       
    28    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
    29 
       
    30 */
       
    31 
       
    32 #include <es_sock.h>
       
    33 #include "natfwunsaftcprelaypacket.h"
       
    34 #include "natfwunsafutils.h"
       
    35 
       
    36 // CONSTANTS
       
    37 const TInt KByteSize = 8;
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CNATFWUNSAFTcpRelayPacket::NewL
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 EXPORT_C CNATFWUNSAFTcpRelayPacket* CNATFWUNSAFTcpRelayPacket::NewL(
       
    44     const TDesC8& aData,
       
    45     const TRelayType aType)
       
    46     {
       
    47     CNATFWUNSAFTcpRelayPacket* self =
       
    48         CNATFWUNSAFTcpRelayPacket::NewLC(aData, aType);
       
    49     CleanupStack::Pop(self);
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CNATFWUNSAFTcpRelayPacket::NewLC
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CNATFWUNSAFTcpRelayPacket* CNATFWUNSAFTcpRelayPacket::NewLC(
       
    58     const TDesC8& aData,
       
    59     const TRelayType aType)
       
    60     {
       
    61     CNATFWUNSAFTcpRelayPacket* self =
       
    62         new (ELeave) CNATFWUNSAFTcpRelayPacket();
       
    63     CleanupStack::PushL(self);
       
    64     self->ConstructL(aData, aType);
       
    65     return self;
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CNATFWUNSAFTcpRelayPacket::CreateL
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CNATFWUNSAFTcpRelayPacket* CNATFWUNSAFTcpRelayPacket::CreateL(
       
    73     const TDesC8& aData,
       
    74     const TRelayType aType )
       
    75     {
       
    76     return NewL(aData, aType);
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CNATFWUNSAFTcpRelayPacket::CNATFWUNSAFTcpRelayPacket
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CNATFWUNSAFTcpRelayPacket::CNATFWUNSAFTcpRelayPacket()
       
    84     {
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CNATFWUNSAFTcpRelayPacket::~CNATFWUNSAFTcpRelayPacket
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 CNATFWUNSAFTcpRelayPacket::~CNATFWUNSAFTcpRelayPacket()
       
    92     {
       
    93     delete iData;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CNATFWUNSAFTcpRelayPacket::ConstructL
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CNATFWUNSAFTcpRelayPacket::ConstructL(
       
   101     const TDesC8& aData,
       
   102     const TRelayType aType )
       
   103     {
       
   104     CheckLengthL(aData.Length());
       
   105     iData = aData.AllocL();
       
   106     iType = aType;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CNATFWUNSAFTcpRelayPacket::EncodeL
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C CBufBase* CNATFWUNSAFTcpRelayPacket::EncodeL() const
       
   114     {
       
   115     CBufBase* relay = EncodeRelayHeaderLC();
       
   116     relay->InsertL(relay->Size(), *iData);
       
   117     CleanupStack::Pop(relay);
       
   118     SetRelayLength(*relay);
       
   119     return relay;
       
   120     }
       
   121     
       
   122 // -----------------------------------------------------------------------------
       
   123 // CNATFWUNSAFTcpRelayPacket::EncodeRelayHeaderLC
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 CBufBase* CNATFWUNSAFTcpRelayPacket::EncodeRelayHeaderLC() const
       
   127     {
       
   128     CBufBase* header = CBufFlat::NewL(EFrameSize);
       
   129     CleanupStack::PushL(header);
       
   130     TUint16 relayType = Type();
       
   131      // Last eight bits of frame type are reserved as zeros
       
   132     relayType <<= KByteSize;
       
   133     TUint16 bigEndianType(0);
       
   134     BigEndian::Put16(reinterpret_cast<TUint8*>(&bigEndianType), relayType);
       
   135     header->InsertL(EFrameTypeOffset, &bigEndianType, sizeof(bigEndianType));
       
   136     //Value is zero, so it is same also in the big endian representation
       
   137     TUint16 frameLength(0);
       
   138     header->InsertL(EFrameLengthOffset, &frameLength, sizeof(frameLength));
       
   139     return header;
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CNATFWUNSAFTcpRelayPacket::RelayLength
       
   144 // TInt is guaranteed to be at least 32 bits.
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 TInt CNATFWUNSAFTcpRelayPacket::RelayLength(const TDesC8& aRelay)
       
   148     {
       
   149     if (aRelay.Length() < EFrameSize)
       
   150         {
       
   151         return KErrNotFound;
       
   152         }
       
   153 
       
   154     return BigEndian::Get16(aRelay.Mid(EFrameLengthOffset).Ptr());
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CNATFWUNSAFTcpRelayPacket::SetRelayLength
       
   159 // UNSAF Relay length does not include the NATFWUNSAF header
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CNATFWUNSAFTcpRelayPacket::SetRelayLength(CBufBase& aRelay)
       
   163     {
       
   164     SetRelayLength(aRelay, aRelay.Size() - EFrameSize);
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CNATFWUNSAFTcpRelayPacket::SetRelayLength
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CNATFWUNSAFTcpRelayPacket::SetRelayLength(CBufBase& aRelay, TInt aLength)
       
   172     {
       
   173     __ASSERT_ALWAYS(aLength >= 0, User::Panic(KNullDesC, KErrUnderflow));
       
   174 
       
   175     TUint16 bigEndianLength(0);
       
   176     BigEndian::Put16(reinterpret_cast<TUint8*>(&bigEndianLength),
       
   177                  static_cast<TUint16>(aLength));
       
   178 
       
   179     aRelay.Write(EFrameLengthOffset, &bigEndianLength,
       
   180         sizeof(bigEndianLength));
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CNATFWUNSAFTcpRelayPacket::CheckLengthL
       
   185 // Check the granularity of attribute's length.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CNATFWUNSAFTcpRelayPacket::CheckLengthL(TInt aLength) const
       
   189     {
       
   190     __ASSERT_ALWAYS((aLength % EGranularity) == 0, User::Leave(KErrCorrupt));
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CNATFWUNSAFTcpRelayPacket::Data
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 EXPORT_C const TDesC8& CNATFWUNSAFTcpRelayPacket::Data() const
       
   198     {
       
   199     return *iData;
       
   200     }
       
   201     
       
   202 // -----------------------------------------------------------------------------
       
   203 // CNATFWUNSAFTcpRelayPacket::Type
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C CNATFWUNSAFTcpRelayPacket::TRelayType
       
   207     CNATFWUNSAFTcpRelayPacket::Type() const
       
   208     {
       
   209     return iType;
       
   210     }