diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index c2db49d8e..abd7b2afb 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -455,6 +455,10 @@ otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort); /** * This function removes a port from the allowed unsecure port list. * + * @note This function removes @p aPort by overwriting @p aPort with the element after @p aPort in the internal port + * list. Be careful when calling otIp6GetUnsecurePorts() followed by otIp6RemoveUnsecurePort() to remove unsecure + * ports. + * * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aPort The port value. * @@ -464,6 +468,14 @@ otError otIp6AddUnsecurePort(otInstance *aInstance, uint16_t aPort); */ otError otIp6RemoveUnsecurePort(otInstance *aInstance, uint16_t aPort); +/** + * This function removes all ports from the allowed unsecure port list. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + */ +void otIp6RemoveAllUnsecurePorts(otInstance *aInstance); + /** * This function returns a pointer to the unsecure port list. * diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 64d4cd2d0..0277560d1 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -221,6 +221,13 @@ otError otIp6RemoveUnsecurePort(otInstance *aInstance, uint16_t aPort) return instance.GetThreadNetif().GetIp6Filter().RemoveUnsecurePort(aPort); } +void otIp6RemoveAllUnsecurePorts(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + instance.GetThreadNetif().GetIp6Filter().RemoveAllUnsecurePorts(); +} + const uint16_t *otIp6GetUnsecurePorts(otInstance *aInstance, uint8_t *aNumEntries) { Instance &instance = *static_cast(aInstance); diff --git a/src/core/net/ip6_filter.cpp b/src/core/net/ip6_filter.cpp index 6a0b8ea93..d68f2ff16 100644 --- a/src/core/net/ip6_filter.cpp +++ b/src/core/net/ip6_filter.cpp @@ -166,6 +166,11 @@ exit: return error; } +void Filter::RemoveAllUnsecurePorts(void) +{ + memset(mUnsecurePorts, 0, sizeof(mUnsecurePorts)); +} + const uint16_t *Filter::GetUnsecurePorts(uint8_t &aNumEntries) const { // Count the number of unsecure ports. diff --git a/src/core/net/ip6_filter.hpp b/src/core/net/ip6_filter.hpp index 8e91c67d7..165648e1f 100644 --- a/src/core/net/ip6_filter.hpp +++ b/src/core/net/ip6_filter.hpp @@ -97,6 +97,12 @@ public: */ otError RemoveUnsecurePort(uint16_t aPort); + /** + * This method removes all ports from the allowed unsecure port list. + * + */ + void RemoveAllUnsecurePorts(void); + /** * This method returns a pointer to the unsecure port list. * diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index 012875658..9263d609d 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -656,15 +656,10 @@ exit: template <> otError NcpBase::HandlePropertySet(void) { - uint8_t numEntries = 0; - const uint16_t *ports = otIp6GetUnsecurePorts(mInstance, &numEntries); - otError error = OT_ERROR_NONE; + otError error = OT_ERROR_NONE; // First, we need to remove all of the current assisting ports. - for (; numEntries != 0; ports++, numEntries--) - { - SuccessOrExit(error = otIp6RemoveUnsecurePort(mInstance, *ports)); - } + otIp6RemoveAllUnsecurePorts(mInstance); while (mDecoder.GetRemainingLengthInStruct() >= sizeof(uint16_t)) {