natfw/natfwunsaf_protocols/unsaf_codec/src/natfwunsafaddressattribute.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 #include <es_sock.h>
       
    22 #include "natfwunsafaddressattribute.h"
       
    23 #include "natfwunsafutils.h"
       
    24 
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CNATFWUNSAFAddressAttribute::CNATFWUNSAFAddressAttribute
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CNATFWUNSAFAddressAttribute::CNATFWUNSAFAddressAttribute()
       
    31     {
       
    32     }
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CNATFWUNSAFAddressAttribute::CNATFWUNSAFAddressAttribute
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CNATFWUNSAFAddressAttribute::CNATFWUNSAFAddressAttribute(
       
    39     const TInetAddr& aInetAddr) :
       
    40     CNATFWUNSAFAttribute(),
       
    41     iInetAddr(aInetAddr)
       
    42     {
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CNATFWUNSAFAddressAttribute::~CNATFWUNSAFAddressAttribute
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CNATFWUNSAFAddressAttribute::~CNATFWUNSAFAddressAttribute()
       
    50     {
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CNATFWUNSAFAddressAttribute::EncodeValueL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 HBufC8* CNATFWUNSAFAddressAttribute::EncodeValueL() const
       
    58     {
       
    59     TInt size = IsIPv6Address() ? EAttributeValueSizeIPv6 :
       
    60                                   EAttributeValueSizeIPv4;
       
    61     HBufC8* encodedValue = HBufC8::NewLC(size);
       
    62     TPtr8 ptr = encodedValue->Des();
       
    63     ptr.FillZ(size);
       
    64 
       
    65     if (IsIPv6Address())
       
    66         {
       
    67         ptr[EFamilyOffset] = KAddressFamilyIPv6;
       
    68         }
       
    69     else
       
    70         {
       
    71         ptr[EFamilyOffset] = KAddressFamilyIPv4;
       
    72         }
       
    73     EncodeAddressL(ptr);
       
    74 
       
    75     CleanupStack::Pop(encodedValue);
       
    76     return encodedValue;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CNATFWUNSAFAddressAttribute::Address
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C TInetAddr& CNATFWUNSAFAddressAttribute::Address()
       
    84     {
       
    85     return iInetAddr;
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CNATFWUNSAFAddressAttribute::Address
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 EXPORT_C const TInetAddr& CNATFWUNSAFAddressAttribute::Address() const
       
    93     {
       
    94     return iInetAddr;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CNATFWUNSAFAddressAttribute::EncodeAddressL
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CNATFWUNSAFAddressAttribute::EncodeAddressL(TDes8& aResult) const
       
   102     {
       
   103     EncodeAddressL(iInetAddr, aResult);
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CNATFWUNSAFAddressAttribute::EncodeAddressL
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CNATFWUNSAFAddressAttribute::EncodeAddressL(const TInetAddr& aInetAddr,
       
   111                                             TDes8& aResult) const
       
   112     {
       
   113     NATFWUNSAFUtils::WriteNetworkOrder16L(aResult,
       
   114                                         EPortOffset,
       
   115                                      static_cast<TUint16>
       
   116                                          (aInetAddr.Port()));
       
   117     if (IsIPv6Address(aInetAddr))
       
   118         {
       
   119         const TInt KIPv6AddressSize = EAttributeValueSizeIPv6 - EAddressOffset;
       
   120         const TIp6Addr& ipv6Addr = aInetAddr.Ip6Address();
       
   121         for ( TInt i = 0; i < KIPv6AddressSize; ++ i )
       
   122             {
       
   123             aResult[EAddressOffset + i] = ipv6Addr.u.iAddr8[i];
       
   124             }
       
   125         }
       
   126     else
       
   127         {
       
   128         NATFWUNSAFUtils::WriteNetworkOrder32L(aResult,
       
   129                                          EAddressOffset,
       
   130                                          aInetAddr.Address());
       
   131         }
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CNATFWUNSAFAddressAttribute::DecodeValueL
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CNATFWUNSAFAddressAttribute::DecodeValueL(const TDesC8& aValue)
       
   139     {
       
   140     //Byte stream must contain at least enough data for this attribute (at least
       
   141     //enough data to contain an IPv4 address). It may contain more data (other
       
   142     //attributes etc).
       
   143     __ASSERT_ALWAYS(aValue.Length() >= EAttributeValueSizeIPv4,
       
   144         User::Leave(KErrArgument));
       
   145 
       
   146     iInetAddr.SetPort(BigEndian::Get16(aValue.Mid(EPortOffset).Ptr()));
       
   147 
       
   148     TUint8 addressFamily = aValue[EFamilyOffset];
       
   149     __ASSERT_ALWAYS(addressFamily == KAddressFamilyIPv4 ||
       
   150                     addressFamily == KAddressFamilyIPv6,
       
   151                     User::Leave(KErrArgument));
       
   152     if (addressFamily == KAddressFamilyIPv6)
       
   153         {
       
   154         DecodeIPv6AddressL(aValue.Mid(EAddressOffset));
       
   155         }
       
   156     else
       
   157         {
       
   158         //SetAddress also sets address family to KAfInet, meaning IPv4 address
       
   159         iInetAddr.SetAddress(
       
   160             BigEndian::Get32(aValue.Mid(EAddressOffset).Ptr()));
       
   161         }
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CNATFWUNSAFAddressAttribute::DecodeIPv6AddressL
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CNATFWUNSAFAddressAttribute::DecodeIPv6AddressL(const TDesC8& aValue)
       
   169     {
       
   170     const TInt KIPv6AddressSize = EAttributeValueSizeIPv6 - EAddressOffset;
       
   171     __ASSERT_ALWAYS(aValue.Length() >= KIPv6AddressSize,
       
   172         User::Leave(KErrArgument));
       
   173 
       
   174     TIp6Addr addr;
       
   175     for ( TInt i = 0; i < KIPv6AddressSize; ++ i )
       
   176         {
       
   177         addr.u.iAddr8[i] = aValue[i];
       
   178         }
       
   179     iInetAddr.SetAddress(addr);
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CNATFWUNSAFAddressAttribute::CheckLengthL
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CNATFWUNSAFAddressAttribute::CheckLengthL(const TDesC8& aByteStream)
       
   187     {
       
   188     TUint16 length = ParseLengthL(aByteStream);
       
   189     __ASSERT_ALWAYS(length == EAttributeValueSizeIPv4 ||
       
   190                      length == EAttributeValueSizeIPv6,
       
   191                     User::Leave(KErrCorrupt));
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CNATFWUNSAFAddressAttribute::IsIPv6Address
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 TBool CNATFWUNSAFAddressAttribute::IsIPv6Address() const
       
   199     {
       
   200     return IsIPv6Address(iInetAddr);
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CNATFWUNSAFAddressAttribute::IsIPv6Address
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 TBool CNATFWUNSAFAddressAttribute::IsIPv6Address(
       
   208     const TInetAddr& aInetAddr) const
       
   209     {
       
   210     return aInetAddr.Address() == 0 || aInetAddr.IsV4Compat();
       
   211     }