diff --git a/src/core/backbone_router/backbone_tmf.cpp b/src/core/backbone_router/backbone_tmf.cpp index 9d91cd680..38cb3288c 100644 --- a/src/core/backbone_router/backbone_tmf.cpp +++ b/src/core/backbone_router/backbone_tmf.cpp @@ -74,6 +74,12 @@ bool BackboneTmfAgent::HandleResource(const char *aUriPath, ot::Coap::Msg &aMsg) bool didHandle = true; Uri uri = UriFromPath(aUriPath); + if ((uri != kUriUnknown) && !aMsg.IsPostRequest()) + { + IgnoreError(SendAckResponse(aMsg, ot::Coap::kCodeMethodNotAllowed)); + ExitNow(); + } + #define Case(kUri, Type) \ case kUri: \ Get().HandleTmf(aMsg); \ @@ -93,6 +99,7 @@ bool BackboneTmfAgent::HandleResource(const char *aUriPath, ot::Coap::Msg &aMsg) #undef Case +exit: return didHandle; } diff --git a/src/core/backbone_router/bbr_manager.cpp b/src/core/backbone_router/bbr_manager.cpp index 025a9c58a..5fdcddf9b 100644 --- a/src/core/backbone_router/bbr_manager.cpp +++ b/src/core/backbone_router/bbr_manager.cpp @@ -149,7 +149,7 @@ void Manager::HandleMulticastListenerRegistration(const Coap::Msg &aMsg) bool hasCommissionerSessionIdTlv = false; bool processTimeoutTlv = false; - VerifyOrExit(aMsg.IsConfirmablePostRequest(), error = kErrorParse); + VerifyOrExit(aMsg.IsConfirmable(), error = kErrorParse); #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE // Required by Test Specification 5.10.22 DUA-TC-26, only for certification purpose @@ -366,7 +366,7 @@ void Manager::HandleDuaRegistration(const Coap::Msg &aMsg) #endif VerifyOrExit(aMsg.mMessageInfo.GetPeerAddr().GetIid().IsRoutingLocator(), error = kErrorDrop); - VerifyOrExit(aMsg.IsConfirmablePostRequest(), error = kErrorParse); + VerifyOrExit(aMsg.IsConfirmable(), error = kErrorParse); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, target)); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, meshLocalIid)); @@ -542,7 +542,7 @@ template <> void Manager::HandleTmf(Coap::Msg &aMsg) VerifyOrExit(aMsg.mMessageInfo.IsHostInterface(), error = kErrorDrop); VerifyOrExit(Get().IsPrimary(), error = kErrorInvalidState); - VerifyOrExit(aMsg.IsNonConfirmablePostRequest(), error = kErrorParse); + VerifyOrExit(aMsg.IsNonConfirmable(), error = kErrorParse); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, dua)); @@ -574,7 +574,6 @@ template <> void Manager::HandleTmf(Coap::Msg &aMsg) VerifyOrExit(aMsg.mMessageInfo.IsHostInterface(), error = kErrorDrop); VerifyOrExit(Get().IsPrimary(), error = kErrorInvalidState); - VerifyOrExit(aMsg.IsPostRequest(), error = kErrorParse); proactive = !aMsg.IsConfirmable(); diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index 7dfea78ae..863592b78 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -91,10 +91,6 @@ exit: bool HeaderInfo::IsRequest(void) const { return IsValueInRange(mCode, kCodeGet, kCodeDelete); } -bool HeaderInfo::IsConfirmablePostRequest(void) const { return IsConfirmable() && IsPostRequest(); } - -bool HeaderInfo::IsNonConfirmablePostRequest(void) const { return IsNonConfirmable() && IsPostRequest(); } - //--------------------------------------------------------------------------------------------------------------------- // `Message` diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index bf788160d..114cceeac 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -390,22 +390,6 @@ public: */ bool IsReset(void) const { return (mType == kTypeReset); } - /** - * Indicates whether or not the header is a confirmable Post request (`kTypeConfirmable` with`kCodePost`). - * - * @retval TRUE Message is a confirmable Post request. - * @retval FALSE Message is not a confirmable Post request. - */ - bool IsConfirmablePostRequest(void) const; - - /** - * Indicates whether the message is a non-confirmable Post request (`kTypeNonConfirmable` with `kCodePost`). - * - * @retval TRUE Message is a non-confirmable Post request. - * @retval FALSE Message is not a non-confirmable Post request. - */ - bool IsNonConfirmablePostRequest(void) const; - /** * Checks if the message requires a reset response if an error during low level CoAP processing occurred. * diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 4857dd6a4..3f9664f1d 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -375,7 +375,7 @@ template <> void Manager::HandleTmf(Coap::Msg &aMsg) VerifyOrExit(mIsRunning); - VerifyOrExit(aMsg.IsNonConfirmablePostRequest()); + VerifyOrExit(aMsg.IsNonConfirmable()); LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); @@ -589,6 +589,12 @@ bool Manager::CoapDtlsSession::HandleResource(const char *aUriPath, Coap::Msg &a bool didHandle = true; Uri uri = UriFromPath(aUriPath); + if ((uri != kUriUnknown) && !aMsg.IsPostRequest()) + { + IgnoreError(SendAckResponse(aMsg, ot::Coap::kCodeMethodNotAllowed)); + ExitNow(); + } + switch (uri) { case kUriCommissionerPetition: @@ -623,6 +629,7 @@ bool Manager::CoapDtlsSession::HandleResource(const char *aUriPath, Coap::Msg &a break; } +exit: return didHandle; } @@ -990,7 +997,7 @@ void Manager::CoapDtlsSession::HandleTmfRelayTx(Coap::Msg &aMsg) OwnedPtr message; OffsetRange offsetRange; - VerifyOrExit(aMsg.IsNonConfirmablePostRequest()); + VerifyOrExit(aMsg.IsNonConfirmable()); #if OPENTHREAD_CONFIG_BORDER_AGENT_ADMITTER_ENABLE if (IsEnroller()) diff --git a/src/core/meshcop/border_agent_admitter.cpp b/src/core/meshcop/border_agent_admitter.cpp index c9eec3de0..643b6e471 100644 --- a/src/core/meshcop/border_agent_admitter.cpp +++ b/src/core/meshcop/border_agent_admitter.cpp @@ -220,7 +220,7 @@ void Admitter::ForwardJoinerRelayToEnrollers(const Coap::Msg &aMsg) VerifyOrExit(mCommissionerPetitioner.IsActiveCommissioner()); - VerifyOrExit(aMsg.IsNonConfirmablePostRequest()); + VerifyOrExit(aMsg.IsNonConfirmable()); SuccessOrExit(Tlv::Find(aMsg.mMessage, joinerIid)); LogInfo("Processing %s from joiner %s", UriToString(), joinerIid.ToString().AsCString()); @@ -1122,7 +1122,7 @@ void Manager::CoapDtlsSession::HandleEnrollerTmf(Uri aUri, const Coap::Msg &aMsg Error error = kErrorNone; StateTlv::State responseState; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Receive %s", Admitter::EnrollerUriToString(aUri)); diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 84259b4e2..9832307a4 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -813,7 +813,7 @@ template <> void Commissioner::HandleTmf(Coap::Msg &aMsg) VerifyOrExit(mState == kStateActive, error = kErrorInvalidState); - VerifyOrExit(aMsg.IsNonConfirmablePostRequest()); + VerifyOrExit(aMsg.IsNonConfirmable()); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, joinerPort)); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, joinerIid)); @@ -882,7 +882,7 @@ void Commissioner::HandleJoinerSessionTimer(void) template <> void Commissioner::HandleTmf(Coap::Msg &aMsg) { VerifyOrExit(mState == kStateActive); - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s", UriToString()); @@ -1073,7 +1073,7 @@ template <> void Commissioner::HandleTmf(Coap::Msg &aMsg) uint32_t mask; EnergyListTlv energyListTlv; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s", UriToString()); @@ -1127,7 +1127,7 @@ template <> void Commissioner::HandleTmf(Coap::Msg &aMsg) uint16_t panId; uint32_t mask; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s", UriToString()); diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index afeac6d6e..43bf6b39e 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -421,7 +421,7 @@ template <> void Joiner::HandleTmf(Coap::Msg &aMsg) Error error; Dataset::Info datasetInfo; - VerifyOrExit(mState == kStateEntrust && aMsg.IsConfirmablePostRequest(), error = kErrorDrop); + VerifyOrExit(mState == kStateEntrust && aMsg.IsConfirmable(), error = kErrorDrop); LogInfo("Received %s", UriToString()); LogCert("[THCI] direction=recv | type=JOIN_ENT.ntf"); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 86cc2e205..11d85bb4e 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -159,7 +159,7 @@ template <> void JoinerRouter::HandleTmf(Coap::Msg &aMsg) Message::Settings settings(kNoLinkSecurity, Message::kPriorityNet); Ip6::MessageInfo messageInfo; - VerifyOrExit(aMsg.IsNonConfirmablePostRequest(), error = kErrorDrop); + VerifyOrExit(aMsg.IsNonConfirmable(), error = kErrorDrop); LogInfo("Received %s", UriToString()); diff --git a/src/core/meshcop/tcat_agent.cpp b/src/core/meshcop/tcat_agent.cpp index f16d0fc7f..7d364fe32 100644 --- a/src/core/meshcop/tcat_agent.cpp +++ b/src/core/meshcop/tcat_agent.cpp @@ -1103,7 +1103,7 @@ template <> void TcatAgent::HandleTmf(Coap::Msg &aMsg) uint16_t durationSec = 0; uint32_t durationMs; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); message = Get().AllocateAndInitResponseFor(aMsg.mMessage); diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index 2732eff23..41e9e9f35 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -652,7 +652,7 @@ template <> void AddressResolver::HandleTmf(Coap::Msg &aMsg) CacheEntry *entry; CacheEntry *prev; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); SuccessOrExit(Tlv::Find(aMsg.mMessage, target)); SuccessOrExit(Tlv::Find(aMsg.mMessage, meshLocalIid)); @@ -747,8 +747,6 @@ template <> void AddressResolver::HandleTmf(Coap::Msg &aMsg) Ip6::Address destination; #endif - VerifyOrExit(aMsg.IsPostRequest(), error = kErrorDrop); - LogInfo("Received %s", UriToString()); if (aMsg.IsConfirmable() && !aMsg.mMessageInfo.GetSockAddr().IsMulticast()) @@ -819,7 +817,7 @@ template <> void AddressResolver::HandleTmf(Coap::Msg &aMsg) Ip6::Address target; uint32_t lastTransactionTime; - VerifyOrExit(aMsg.IsNonConfirmablePostRequest()); + VerifyOrExit(aMsg.IsNonConfirmable()); SuccessOrExit(Tlv::Find(aMsg.mMessage, target)); diff --git a/src/core/thread/announce_begin_server.cpp b/src/core/thread/announce_begin_server.cpp index 6535bc1dd..ce4f3a19d 100644 --- a/src/core/thread/announce_begin_server.cpp +++ b/src/core/thread/announce_begin_server.cpp @@ -58,7 +58,6 @@ template <> void AnnounceBeginServer::HandleTmf(Coap::Msg &aM uint8_t count; uint16_t period; - VerifyOrExit(aMsg.IsPostRequest()); SuccessOrExit(MeshCoP::ChannelMaskTlv::FindIn(aMsg.mMessage, mask)); SuccessOrExit(Tlv::Find(aMsg.mMessage, count)); diff --git a/src/core/thread/anycast_locator.cpp b/src/core/thread/anycast_locator.cpp index a2750c02d..27fbac6d5 100644 --- a/src/core/thread/anycast_locator.cpp +++ b/src/core/thread/anycast_locator.cpp @@ -99,7 +99,7 @@ template <> void AnycastLocator::HandleTmf(Coap::Msg &aMsg) { Coap::Message *message = nullptr; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); message = Get().AllocateAndInitResponseFor(aMsg.mMessage); VerifyOrExit(message != nullptr); diff --git a/src/core/thread/dua_manager.cpp b/src/core/thread/dua_manager.cpp index 9013e784d..e5f24b1cf 100644 --- a/src/core/thread/dua_manager.cpp +++ b/src/core/thread/dua_manager.cpp @@ -572,8 +572,6 @@ template <> void DuaManager::HandleTmf(Coap::Msg &aMs { Error error; - VerifyOrExit(aMsg.IsPostRequest(), error = kErrorParse); - if (aMsg.IsConfirmable() && Get().SendAckResponse(aMsg) == kErrorNone) { LogInfo("Sent %s ack", UriToString()); @@ -581,7 +579,6 @@ template <> void DuaManager::HandleTmf(Coap::Msg &aMs error = ProcessDuaResponse(aMsg.mMessage); -exit: OT_UNUSED_VARIABLE(error); LogInfo("Received %s: %s", UriToString(), ErrorToString(error)); } diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index b4439fdc6..7c3702a81 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -58,8 +58,6 @@ template <> void EnergyScanServer::HandleTmf(Coap::Msg &aMsg) uint32_t mask; MeshCoP::Tlv tlv; - VerifyOrExit(aMsg.IsPostRequest()); - SuccessOrExit(Tlv::Find(aMsg.mMessage, count)); count = Clamp(count, kMinCount, kMaxCount); diff --git a/src/core/thread/mle_ftd.cpp b/src/core/thread/mle_ftd.cpp index acb37847e..a64dbe04b 100644 --- a/src/core/thread/mle_ftd.cpp +++ b/src/core/thread/mle_ftd.cpp @@ -3462,7 +3462,7 @@ Error Mle::AddrSolicitInfo::ParseFrom(const Coap::Msg &aMsg) Error error; - VerifyOrExit(aMsg.IsConfirmablePostRequest(), error = kErrorParse); + VerifyOrExit(aMsg.IsConfirmable(), error = kErrorParse); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, mExtAddress)); SuccessOrExit(error = Tlv::Find(aMsg.mMessage, mReason)); @@ -3634,7 +3634,7 @@ template <> void Mle::HandleTmf(Coap::Msg &aMsg) VerifyOrExit(mRole == kRoleLeader); - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); Log(kMessageReceive, kTypeAddressRelease, aMsg.mMessageInfo.GetPeerAddr()); diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 334c701f9..07381f20a 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -555,8 +555,6 @@ exit: template <> void Server::HandleTmf(Coap::Msg &aMsg) { - VerifyOrExit(aMsg.IsPostRequest()); - LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); @@ -571,9 +569,6 @@ template <> void Server::HandleTmf(Coap::Msg &aMsg) #elif OPENTHREAD_FTD PrepareAndSendAnswers(aMsg.mMessageInfo.GetPeerAddr(), aMsg.mMessage); #endif - -exit: - return; } #if OPENTHREAD_MTD @@ -911,7 +906,7 @@ template <> void Server::HandleTmf(Coap::Msg &aMsg) Error error = kErrorNone; Coap::Message *response = nullptr; - VerifyOrExit(aMsg.IsConfirmablePostRequest(), error = kErrorDrop); + VerifyOrExit(aMsg.IsConfirmable(), error = kErrorDrop); LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); @@ -933,7 +928,7 @@ template <> void Server::HandleTmf(Coap::Msg &aMsg) uint8_t type; Tlv tlv; - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); @@ -1079,7 +1074,7 @@ exit: template <> void Client::HandleTmf(Coap::Msg &aMsg) { - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); LogInfo("Received %s from %s", ot::UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); diff --git a/src/core/thread/panid_query_server.cpp b/src/core/thread/panid_query_server.cpp index 10acbf3a5..ed3783c29 100644 --- a/src/core/thread/panid_query_server.cpp +++ b/src/core/thread/panid_query_server.cpp @@ -52,7 +52,6 @@ template <> void PanIdQueryServer::HandleTmf(Coap::Msg &aMsg) uint16_t panId; uint32_t mask; - VerifyOrExit(aMsg.IsPostRequest()); SuccessOrExit(MeshCoP::ChannelMaskTlv::FindIn(aMsg.mMessage, mask)); SuccessOrExit(Tlv::Find(aMsg.mMessage, panId)); diff --git a/src/core/thread/tmf.cpp b/src/core/thread/tmf.cpp index 2e1dd2e49..f24dd2235 100644 --- a/src/core/thread/tmf.cpp +++ b/src/core/thread/tmf.cpp @@ -72,6 +72,12 @@ bool Agent::HandleResource(const char *aUriPath, Msg &aMsg) bool didHandle = true; Uri uri = UriFromPath(aUriPath); + if ((uri != kUriUnknown) && !aMsg.IsPostRequest()) + { + IgnoreError(SendAckResponse(aMsg, ot::Coap::kCodeMethodNotAllowed)); + ExitNow(); + } + #define Case(kUri, Type) \ case kUri: \ Get().HandleTmf(aMsg); \ @@ -159,6 +165,7 @@ bool Agent::HandleResource(const char *aUriPath, Msg &aMsg) #undef Case +exit: return didHandle; } diff --git a/src/core/utils/history_tracker_client.cpp b/src/core/utils/history_tracker_client.cpp index 0e2e70261..1dbf46390 100644 --- a/src/core/utils/history_tracker_client.cpp +++ b/src/core/utils/history_tracker_client.cpp @@ -110,7 +110,7 @@ exit: template <> void Client::HandleTmf(Coap::Msg &aMsg) { - VerifyOrExit(aMsg.IsConfirmablePostRequest()); + VerifyOrExit(aMsg.IsConfirmable()); IgnoreError(Get().SendAckResponse(aMsg)); LogInfo("Received %s from %s", ot::UriToString(), diff --git a/src/core/utils/history_tracker_server.cpp b/src/core/utils/history_tracker_server.cpp index b2bd78674..0c74b1446 100644 --- a/src/core/utils/history_tracker_server.cpp +++ b/src/core/utils/history_tracker_server.cpp @@ -49,8 +49,6 @@ Server::Server(Instance &aInstance) template <> void Server::HandleTmf(Coap::Msg &aMsg) { - VerifyOrExit(aMsg.IsPostRequest()); - LogInfo("Received %s from %s", UriToString(), aMsg.mMessageInfo.GetPeerAddr().ToString().AsCString()); @@ -60,9 +58,6 @@ template <> void Server::HandleTmf(Coap::Msg &aMsg) } PrepareAndSendAnswers(aMsg.mMessageInfo.GetPeerAddr(), aMsg.mMessage); - -exit: - return; } Error Server::AllocateAnswer(Coap::Message *&aAnswer, AnswerInfo &aInfo)