Skip to content

Commit

Permalink
[border-agent] track ForwardContext and abort TMF txn on disconnect (
Browse files Browse the repository at this point in the history
…openthread#11216)

This commit updates the Border Agent so that the `CoapDtlsSession`
tracks the forwarded CoAP request and the allocated `ForwardContext`
to the leader using `Tmf::Agent` in a list.

If the CoAP session disconnects before the forwarded request finishes,
the pending transaction is explicitly aborted in the session's
`Cleanup()` method using `Tmf::Agent::AbortTransaction()`. This
ensures that the response handler, `HandleCoapResponse()`, is invoked
while the session remains valid.
  • Loading branch information
abtink authored Feb 6, 2025
1 parent 64e0811 commit 521f95f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/core/meshcop/border_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,13 @@ BorderAgent::CoapDtlsSession::CoapDtlsSession(Instance &aInstance, Dtls::Transpo

void BorderAgent::CoapDtlsSession::Cleanup(void)
{
while (!mForwardContexts.IsEmpty())
{
ForwardContext *forwardContext = mForwardContexts.Pop();

IgnoreError(Get<Tmf::Agent>().AbortTransaction(HandleCoapResponse, forwardContext));
}

mTimer.Stop();
IgnoreError(Get<Ip6::Udp>().RemoveReceiver(mUdpReceiver));
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
Expand Down Expand Up @@ -755,7 +762,7 @@ Error BorderAgent::CoapDtlsSession::ForwardToLeader(const Coap::Message &aMes
// will own it. We take back ownership from `HandleCoapResponse()`
// callback.

forwardContext.Release();
mForwardContexts.Push(*forwardContext.Release());

LogInfo("Forwarded request to leader on %s", PathForUri(aUri));

Expand Down Expand Up @@ -790,6 +797,8 @@ void BorderAgent::CoapDtlsSession::HandleCoapResponse(const ForwardContext &aFor
Coap::Message *message = nullptr;
Error error;

IgnoreError(mForwardContexts.Remove(aForwardContext));

SuccessOrExit(error = aResult);
VerifyOrExit((message = NewPriorityMessage()) != nullptr, error = kErrorNoBufs);

Expand Down
7 changes: 6 additions & 1 deletion src/core/meshcop/border_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "common/as_core_type.hpp"
#include "common/heap_allocatable.hpp"
#include "common/linked_list.hpp"
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "common/notifier.hpp"
Expand Down Expand Up @@ -311,14 +312,17 @@ class BorderAgent : public InstanceLocator, private NonCopyable
void Cleanup(void);

private:
class ForwardContext : public Heap::Allocatable<ForwardContext>, private ot::NonCopyable
class ForwardContext : public ot::LinkedListEntry<ForwardContext>,
public Heap::Allocatable<ForwardContext>,
private ot::NonCopyable
{
friend class Heap::Allocatable<ForwardContext>;

public:
Error ToHeader(Coap::Message &aMessage, uint8_t aCode) const;

CoapDtlsSession &mSession;
ForwardContext *mNext;
uint16_t mMessageId;
bool mPetition : 1;
bool mSeparate : 1;
Expand Down Expand Up @@ -358,6 +362,7 @@ class BorderAgent : public InstanceLocator, private NonCopyable
void HandleTimer(void);

bool mIsActiveCommissioner;
LinkedList<ForwardContext> mForwardContexts;
TimerMilliContext mTimer;
Ip6::Udp::Receiver mUdpReceiver;
Ip6::Netif::UnicastAddress mCommissionerAloc;
Expand Down

0 comments on commit 521f95f

Please sign in to comment.