natfw/natfwconnectionmultiplexer/src/cncmicmpv4receiver.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:    ICMP receiver
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <ip4_hdr.h>
       
    22 #include <icmp6_hdr.h> // This is identical to ICMPv4 header
       
    23 #include <udp_hdr.h>
       
    24 #include <in_sock.h>
       
    25 #include "cncmicmpv4receiver.h"
       
    26 #include "cncmicmpsender.h"
       
    27 #include "mncmicmpobserver.h"
       
    28 #include "ncmconnectionmultiplexerlogs.h"
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CNcmIcmpV4Receiver::NewL
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CNcmIcmpV4Receiver* CNcmIcmpV4Receiver::NewL( RSocketServ& aServer,
       
    36     RConnection& aConnection, MNcmIcmpObserver& aObserver )
       
    37     {
       
    38     CNcmIcmpV4Receiver* self =
       
    39         new ( ELeave ) CNcmIcmpV4Receiver( aServer, aConnection, aObserver );
       
    40             
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46 
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CNcmIcmpV4Receiver::CNcmIcmpV4Receiver
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CNcmIcmpV4Receiver::CNcmIcmpV4Receiver( 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 // CNcmIcmpV4Receiver::ConstructL
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CNcmIcmpV4Receiver::ConstructL()
       
    68     {
       
    69     User::LeaveIfError( iSocket.Open(
       
    70         iServer, KAfInet, KSockDatagram, KProtocolInetIcmp, iConnection ) );
       
    71                                           
       
    72     iIcmpSender = CNcmIcmpSender::NewL( iSocket );
       
    73 
       
    74     Receive();
       
    75     }
       
    76 
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CNcmIcmpV4Receiver::~CNcmIcmpV4Receiver
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CNcmIcmpV4Receiver::~CNcmIcmpV4Receiver()
       
    83     {
       
    84     Cancel();
       
    85     
       
    86     delete iIcmpSender;
       
    87     iSocket.Close();
       
    88     }
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CNcmIcmpV4Receiver::RunL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CNcmIcmpV4Receiver::RunL()
       
    96     {
       
    97     if ( iStatus == KErrNone )
       
    98         {
       
    99         CheckError();
       
   100         Receive();
       
   101         }
       
   102     }
       
   103 
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CNcmIcmpV4Receiver::CheckError
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CNcmIcmpV4Receiver::CheckError()
       
   110     {
       
   111     const TUint KDestinationUnreachable = 3;
       
   112 
       
   113     // Pointer to ICMP header
       
   114     TInet6HeaderICMP* icmpPtr( ( TInet6HeaderICMP* ) iData.Ptr() );
       
   115 
       
   116     // Pointer to IPv4 header
       
   117     const TInt KIPHeaderOffset = 8;
       
   118     TInet6HeaderIP4* ipv4Ptr =
       
   119         ( TInet6HeaderIP4* )( iData.Ptr() + KIPHeaderOffset );
       
   120 
       
   121     // Error message type
       
   122     TUint32 error( icmpPtr->Type() );
       
   123     if ( KDestinationUnreachable == error )
       
   124         {
       
   125         // Check that protocol is UDP
       
   126         TUint32 protocol( ipv4Ptr->Protocol() );
       
   127         if ( KProtocolInetUdp == protocol )
       
   128             {
       
   129             // Pointer to UDP data
       
   130             TInet6HeaderUDP* udpPtr =
       
   131                 ( TInet6HeaderUDP* )( ipv4Ptr->EndPtr() );
       
   132 
       
   133             // Get the destination port number
       
   134             iAddress.SetPort( udpPtr->DstPort() );
       
   135 
       
   136             // Set the destination address
       
   137             iAddress.SetAddress( ipv4Ptr->DstAddr() );
       
   138 
       
   139             __CONNECTIONMULTIPLEXER_INT1(
       
   140                 "CNcmIcmpV4Receiver::CheckError - ICMP error:", error )
       
   141             __CONNECTIONMULTIPLEXER_ADDRLOG(
       
   142                 "CNcmIcmpV4Receiver::CheckError - DstAddr:", iAddress )
       
   143 
       
   144             // Error message to observer
       
   145             TInetAddr localAddress;
       
   146             TInetAddr remoteAddress;
       
   147             iObserver.IcmpError( iAddress, localAddress, remoteAddress );
       
   148             
       
   149             if ( !( remoteAddress.IsUnspecified() ||
       
   150                 localAddress.IsUnspecified() ) )
       
   151                 {
       
   152                 udpPtr->SetDstPort( localAddress.Port() );
       
   153                 udpPtr->SetSrcPort( remoteAddress.Port() );
       
   154                 ipv4Ptr->SetDstAddr( localAddress.Address() );
       
   155                 ipv4Ptr->SetSrcAddr( remoteAddress.Address() );
       
   156                 iIcmpSender->Send( iData, remoteAddress );
       
   157                 }
       
   158             }
       
   159         }
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CNcmIcmpV4Receiver::DoCancel
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CNcmIcmpV4Receiver::DoCancel()
       
   167     {
       
   168     iSocket.CancelRecv();
       
   169     }
       
   170 
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CNcmIcmpV4Receiver::Receive
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CNcmIcmpV4Receiver::Receive()
       
   177     {
       
   178     if ( !IsActive() )
       
   179         {
       
   180         iSocket.Recv( iData, 0, iStatus );
       
   181         SetActive();
       
   182         }
       
   183     }