natplugins/natptraversalcontroller/src/cnatbinding.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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 #include "natfwstunbinding.h"
       
    20 #include "cnatbinding.h"
       
    21 #include "sipnattraversalrequestobserver.h"
       
    22 #include "sipnatbindingobserver.h"
       
    23 #include "cnatbindingrefresher.h"
       
    24 #include "nattraversalsocketmanager.h"
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CNATBinding::NewL
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CNATBinding* CNATBinding::NewL(
       
    31     TUint32 aInitialRequestId,
       
    32     RSocket& aSTUNSocket,
       
    33     const TInetAddr& aLocalAddr,
       
    34     MSIPNATTraversalRequestObserver& aInitialRequestObserver,
       
    35     MSIPNATBindingObserver& aBindingObserver,
       
    36     MNATTraversalSocketManager& aSocketManager)
       
    37     {
       
    38     CNATBinding* self =
       
    39         CNATBinding::NewLC(aInitialRequestId,aSTUNSocket,aLocalAddr,
       
    40                            aInitialRequestObserver,aBindingObserver,
       
    41                            aSocketManager);
       
    42     CleanupStack::Pop(self);
       
    43     return self;
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CNATBinding::NewLC
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CNATBinding* CNATBinding::NewLC(
       
    51     TUint32 aInitialRequestId,
       
    52     RSocket& aSTUNSocket,
       
    53     const TInetAddr& aLocalAddr,
       
    54     MSIPNATTraversalRequestObserver& aInitialRequestObserver,
       
    55     MSIPNATBindingObserver& aBindingObserver,
       
    56     MNATTraversalSocketManager& aSocketManager)
       
    57     {
       
    58     CNATBinding* self =
       
    59         new(ELeave)CNATBinding(aInitialRequestId,aSTUNSocket,aLocalAddr,
       
    60                                aInitialRequestObserver,aBindingObserver,
       
    61                                aSocketManager);
       
    62     CleanupStack::PushL(self);
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CNATBinding::CNATBinding
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CNATBinding::CNATBinding(
       
    71     TUint32 aInitialRequestId,
       
    72     RSocket& aSTUNSocket,
       
    73     const TInetAddr& aLocalAddr,
       
    74     MSIPNATTraversalRequestObserver& aInitialRequestObserver,
       
    75     MSIPNATBindingObserver& aBindingObserver,
       
    76     MNATTraversalSocketManager& aSocketManager)
       
    77     : iInitialReqId(aInitialRequestId),
       
    78       iSTUNSocket(aSTUNSocket),
       
    79       iInitialReqObserver(aInitialRequestObserver),
       
    80       iBindingObserver(aBindingObserver),
       
    81       iSocketManager(aSocketManager),
       
    82       iPublicAddr(aLocalAddr)
       
    83     {
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CNATBinding::~CNATBinding
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CNATBinding::~CNATBinding()
       
    91     {
       
    92     iSocketManager.SendingCompleted(*this); // Make sure no requests are pending
       
    93     delete iRefresher;
       
    94     delete iBinding;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CNATBinding::PersistentConnectionRequired
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 TBool CNATBinding::PersistentConnectionRequired() const
       
   102     {
       
   103     TBool persistentConnection = EFalse;
       
   104     if (iRefresher)
       
   105         {
       
   106         persistentConnection = iRefresher->PersistentConnectionRequired();
       
   107         }
       
   108     return persistentConnection;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CNATBinding::STUNSocket
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 RSocket& CNATBinding::STUNSocket()
       
   116     {
       
   117     return iSTUNSocket;
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CNATBinding::SetBinding
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CNATBinding::SetBinding(CSTUNBinding* aBinding)
       
   125     {
       
   126     delete iBinding;
       
   127     iBinding = aBinding;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CNATBinding::Binding
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 CSTUNBinding* CNATBinding::Binding()
       
   135     {
       
   136     return iBinding;
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CNATBinding::SendSTUNRequestL
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CNATBinding::SendSTUNRequestL()
       
   144     {
       
   145     __ASSERT_ALWAYS(iBinding, User::Leave(KErrNotReady));
       
   146     if (!iSocketManager.AddToSendingQueueL(*this))
       
   147         {
       
   148         iBinding->SendRequestL();
       
   149         iIsSending = ETrue;
       
   150         }
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CNATBinding::SetBindingFailed
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CNATBinding::SetBindingFailed(TInt aError)
       
   158     {
       
   159     SendingCompleted();
       
   160     delete iBinding;
       
   161     iBinding = NULL;
       
   162     if (iAddressResolved)
       
   163         {
       
   164         iAddressResolved = EFalse;
       
   165         iBindingObserver.FlowFailure(aError);
       
   166         }
       
   167     else
       
   168         {
       
   169         iInitialReqObserver.RequestComplete(iInitialReqId,KErrNone);
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CNATBinding::SetPublicAddr
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 TBool CNATBinding::SetPublicAddr(const TInetAddr& aPublicAddr)
       
   178     {
       
   179     SendingCompleted();
       
   180     TBool removeBinding = EFalse;
       
   181     if (iAddressResolved)
       
   182         {
       
   183         removeBinding = !aPublicAddr.CmpAddr(iPublicAddr);
       
   184         }
       
   185     else
       
   186         {
       
   187         iAddressResolved = ETrue;
       
   188         TBool localAddrMatchesToPublicAddr = iPublicAddr.Match(aPublicAddr);
       
   189         iPublicAddr = aPublicAddr;
       
   190         if (localAddrMatchesToPublicAddr)
       
   191             {
       
   192             // We are not behind a NAT
       
   193             iInitialReqObserver.RequestComplete(iInitialReqId,KErrNone);
       
   194             }
       
   195         else
       
   196             {
       
   197             iInitialReqObserver.PublicAddrResolved(iInitialReqId,aPublicAddr);
       
   198             }
       
   199         }
       
   200     return removeBinding;
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CNATBinding::SetRefresher
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CNATBinding::SetRefresher(CNATBindingRefresher* aRefresher)
       
   208     {
       
   209     delete iRefresher;
       
   210     iRefresher = aRefresher;
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CNATBinding::Refresher
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 CNATBindingRefresher* CNATBinding::Refresher()
       
   218     {
       
   219     return iRefresher;
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CNATBinding::InitialRequestId
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 TUint32 CNATBinding::InitialRequestId() const
       
   227     {
       
   228     return iInitialReqId;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CNATBinding::NATBindingObserver
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 MSIPNATBindingObserver& CNATBinding::NATBindingObserver()
       
   236     {
       
   237     return iBindingObserver;
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CNATBinding::AddressResolved
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 TBool CNATBinding::AddressResolved() const
       
   245     {
       
   246     return iAddressResolved;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CNATBinding::PublicAddr
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 const TInetAddr& CNATBinding::PublicAddr() const
       
   254     {
       
   255     return iPublicAddr;
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CNATBinding::HandleDataL
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 void CNATBinding::HandleDataL(
       
   263     const TDesC8& aData,
       
   264     const RSocket& aSocket,
       
   265     TBool& aConsumed)
       
   266     {
       
   267     aConsumed = EFalse;
       
   268     if (iBinding &&
       
   269         aSocket.SubSessionHandle() == iBinding->Socket().SubSessionHandle())
       
   270         {
       
   271         // Set a dummy remote address as the data is guaranteed not to be a
       
   272         // TURN indication and on return the address is useless to us.
       
   273         TInetAddr addr(KAFUnspec);
       
   274         iBinding->HandleDataL(aData,aConsumed,addr);
       
   275         }
       
   276     }
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // CNATBinding::ContinueRefreshing
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CNATBinding::ContinueRefreshing()
       
   283     {
       
   284     if (iRefresher)
       
   285         {
       
   286         iRefresher->ContinueRefreshing();
       
   287         }
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CNATBinding::NATBindingRefreshComplete
       
   292 // From MNATBindingRefresherObserver
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CNATBinding::NATBindingRefreshComplete(TInt aCompletionCode)
       
   296     {
       
   297     if (aCompletionCode != KErrNone)
       
   298         {
       
   299         iBindingObserver.FlowFailure(aCompletionCode);
       
   300         }
       
   301     else
       
   302         {
       
   303         ContinueRefreshing();
       
   304         }
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CNATBinding::Socket
       
   309 // From MNATTraversalSocketUser
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 const RSocket& CNATBinding::Socket() const
       
   313     {
       
   314     return iSTUNSocket;
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CNATBinding::SendingAllowed
       
   319 // From MNATTraversalSocketUser
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 void CNATBinding::SendingAllowed()
       
   323     {
       
   324     if (iBinding)
       
   325         {
       
   326         TRAPD(err, iBinding->SendRequestL());
       
   327         if (err)
       
   328             {
       
   329             SetBindingFailed(err);
       
   330             }
       
   331         else
       
   332             {
       
   333             iIsSending = ETrue;
       
   334             }
       
   335         }
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CNATBinding::IsSending
       
   340 // From MNATTraversalSocketUser
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 TBool CNATBinding::IsSending() const
       
   344     {
       
   345     return iIsSending;
       
   346     }
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // CNATBinding::CancelSending
       
   350 // From MNATTraversalSocketUser
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CNATBinding::CancelSending()
       
   354     {
       
   355     if (iBinding)
       
   356         {
       
   357         iBinding->CancelRequest();
       
   358         }
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CNATBinding::SendingCompleted
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 void CNATBinding::SendingCompleted()
       
   366     {
       
   367     iIsSending = EFalse;
       
   368     iSocketManager.SendingCompleted(*this);
       
   369     }