natfw/natfwconnectionmultiplexer/src/cncmicmpv6receiver.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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 <ip6_hdr.h>
       
    22 #include <icmp6_hdr.h>
       
    23 #include <udp_hdr.h>
       
    24 #include <ext_hdr.h>
       
    25 #include <in_sock.h>
       
    26 #include "ncmconnectionmultiplexerlogs.h"
       
    27 #include "cncmicmpv6receiver.h"
       
    28 #include "mncmicmpobserver.h"
       
    29 #include "cncmicmpsender.h"
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CNcmIcmpV6Receiver::NewL
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CNcmIcmpV6Receiver* CNcmIcmpV6Receiver::NewL( RSocketServ& aServer,
       
    36     RConnection& aConnection, MNcmIcmpObserver& aObserver )
       
    37     {
       
    38     CNcmIcmpV6Receiver* self =
       
    39         new ( ELeave ) CNcmIcmpV6Receiver( aServer, aConnection, aObserver );
       
    40             
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46 
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CNcmIcmpV6Receiver::CNcmIcmpV6Receiver
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CNcmIcmpV6Receiver::CNcmIcmpV6Receiver( RSocketServ& aServer,
       
    53     RConnection& aConnection, MNcmIcmpObserver& aObserver ) :
       
    54     CActive( CActive::EPriorityStandard ),
       
    55     iServer( aServer ),
       
    56     iConnection( aConnection ),
       
    57     iObserver( aObserver )
       
    58     {
       
    59     CActiveScheduler::Add( this );
       
    60     }
       
    61 
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CNcmIcmpV6Receiver::ConstructL
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CNcmIcmpV6Receiver::ConstructL()
       
    68     {
       
    69     User::LeaveIfError( iSocket.Open(
       
    70         iServer, KAfInet, KSockDatagram, KProtocolInet6Icmp, iConnection ) );
       
    71         
       
    72     iIcmpSender = CNcmIcmpSender::NewL( iSocket );
       
    73 
       
    74     Receive();
       
    75     }
       
    76 
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CNcmIcmpV6Receiver::~CNcmIcmpV6Receiver
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CNcmIcmpV6Receiver::~CNcmIcmpV6Receiver()
       
    83     {
       
    84     Cancel();
       
    85     
       
    86     delete iIcmpSender;
       
    87     iSocket.Close();
       
    88     }
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CNcmIcmpV6Receiver::RunL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CNcmIcmpV6Receiver::RunL()
       
    96     {
       
    97     if ( iStatus == KErrNone )
       
    98         {
       
    99         CheckError();
       
   100         Receive();
       
   101         }
       
   102     }
       
   103 
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CNcmIcmpV6Receiver::CheckError
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CNcmIcmpV6Receiver::CheckError()
       
   110     {
       
   111     const TUint KDestinationUnreachable = 1;
       
   112 
       
   113     // Pointer to ICMP header
       
   114     TInet6HeaderICMP* icmpPtr( ( TInet6HeaderICMP* ) iData.Ptr() );
       
   115 
       
   116     // Pointer to IPv6 header
       
   117     const TInt KIPHeaderOffset = 8;
       
   118     TInet6HeaderIP* ipv6Ptr =
       
   119         ( TInet6HeaderIP* )( iData.Ptr() + KIPHeaderOffset );
       
   120 
       
   121     // Get the whole length of the header in bytes
       
   122     TUint32 payloadLength( ipv6Ptr->PayloadLength() );
       
   123     TUint32 offset( 0 );
       
   124 
       
   125     // Pointer to IPv6 extension header
       
   126     const TInt KHeaderExtensionOffset = 14;
       
   127     TInet6HeaderExtension* extPtr =
       
   128         ( TInet6HeaderExtension* )( iData.Ptr() + KHeaderExtensionOffset );
       
   129     // Get the extension header length in bytes
       
   130     TUint32 extHdrLength( extPtr->HeaderLength() );
       
   131 
       
   132     // Pointer to UDP data
       
   133     TInet6HeaderUDP* udpPtr = ( TInet6HeaderUDP* )( ipv6Ptr->EndPtr() );
       
   134 
       
   135     // Error message type
       
   136     TUint32 error( icmpPtr->Type() );
       
   137     if ( KDestinationUnreachable == error )
       
   138         {
       
   139         TUint32 nextHeader( 0 );
       
   140         do  // Search the correct header, if there are extension headers
       
   141             {
       
   142             nextHeader = extPtr->NextHeader();
       
   143             if( KProtocolInetUdp == nextHeader )
       
   144                 {
       
   145                 // Get the destination port number
       
   146                 iAddress.SetPort( udpPtr->DstPort() );
       
   147 
       
   148                 // Set the destination address
       
   149                 iAddress.SetAddress( ipv6Ptr->DstAddr() );
       
   150 
       
   151                 __CONNECTIONMULTIPLEXER_INT1(
       
   152                     "CNcmIcmpV6Receiver::CheckError - ICMP error:", error )
       
   153                 __CONNECTIONMULTIPLEXER_ADDRLOG(
       
   154                     " CNcmIcmpV6Receiver::CheckError - DstAddr:", iAddress )
       
   155 
       
   156                 TInetAddr localAddress;
       
   157                 TInetAddr remoteAddress;
       
   158                 iObserver.IcmpError( iAddress, localAddress, remoteAddress );
       
   159                             
       
   160                 if ( !( remoteAddress.IsUnspecified() ||
       
   161                     localAddress.IsUnspecified() ) )
       
   162                     {
       
   163                     udpPtr->SetDstPort( localAddress.Port() );
       
   164                     udpPtr->SetSrcPort( remoteAddress.Port() );
       
   165                      
       
   166                     TInetAddr local( localAddress.Address() );
       
   167                     local.ConvertToV4Mapped();
       
   168                     ipv6Ptr->SetDstAddr( local.Ip6Address() );
       
   169                                                         
       
   170                     TInetAddr remote( remoteAddress.Address() );
       
   171                     local.ConvertToV4Mapped();
       
   172                     ipv6Ptr->SetSrcAddr( remote.Ip6Address() );
       
   173                     
       
   174                     iIcmpSender->Send( iData, remoteAddress );
       
   175                     }
       
   176                 }
       
   177             else
       
   178                 {
       
   179                 // Step to next header
       
   180                 if ( offset < payloadLength )
       
   181                     {
       
   182                     extPtr = extPtr + extHdrLength;
       
   183                     offset = offset + extHdrLength;
       
   184                     }
       
   185                 }
       
   186             // Loop until UDP datagram is found. If it is not found,
       
   187             // get out of here and do nothing.
       
   188             } while ( ( KProtocolInetUdp != nextHeader ) &&
       
   189                       ( offset < payloadLength ) );
       
   190         }
       
   191     }
       
   192 
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CNcmIcmpV6Receiver::DoCancel
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CNcmIcmpV6Receiver::DoCancel()
       
   199     {
       
   200     iSocket.CancelRecv();
       
   201     }
       
   202 
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CNcmIcmpV6Receiver::Receive
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 void CNcmIcmpV6Receiver::Receive()
       
   209     {
       
   210     if ( !IsActive() )
       
   211         {
       
   212         iSocket.Recv( iData, 0, iStatus );
       
   213         SetActive();
       
   214         }
       
   215     }