diff --git a/src/core/Makefile.am b/src/core/Makefile.am index f29a25414..24818f93e 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -319,6 +319,7 @@ HEADERS_COMMON = \ coap/coap.hpp \ coap/coap_message.hpp \ coap/coap_secure.hpp \ + common/clearable.hpp \ common/code_utils.hpp \ common/crc16.hpp \ common/debug.hpp \ diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index 7b1f1560c..c601a4e81 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -38,6 +38,7 @@ #include +#include "common/clearable.hpp" #include "common/code_utils.hpp" #include "common/encoding.hpp" #include "common/message.hpp" @@ -628,10 +629,8 @@ private: * This structure represents a HelpData used by this CoAP message. * */ - struct HelpData + struct HelpData : public Clearable { - void Clear(void) { memset(this, 0, sizeof(*this)); } - Header mHeader; uint16_t mOptionLast; uint16_t mHeaderOffset; ///< The byte offset for the CoAP Header diff --git a/src/core/common/clearable.hpp b/src/core/common/clearable.hpp new file mode 100644 index 000000000..2cef0918e --- /dev/null +++ b/src/core/common/clearable.hpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes definitions for Clearable class for OpenThread objects. + */ + +#ifndef CLEARABLE_HPP_ +#define CLEARABLE_HPP_ + +#include "openthread-core-config.h" + +#include + +namespace ot { + +/** + * This template class defines a Clearable object which provides `Clear()` method. + * + * The `Clear` implementation simply sets all the bytes of a `Type` instance to zero. + * + * Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit + * from `Clearable`. + * + */ +template class Clearable +{ +public: + void Clear(void) { memset(reinterpret_cast(this), 0, sizeof(Type)); } +}; + +} // namespace ot + +#endif // CLEARABLE_HPP_ diff --git a/src/core/mac/mac_types.hpp b/src/core/mac/mac_types.hpp index f42a65cad..a332c308c 100644 --- a/src/core/mac/mac_types.hpp +++ b/src/core/mac/mac_types.hpp @@ -42,6 +42,7 @@ #include #include +#include "common/clearable.hpp" #include "common/equatable.hpp" #include "common/string.hpp" @@ -87,7 +88,7 @@ PanId GenerateRandomPanId(void); * */ OT_TOOL_PACKED_BEGIN -class ExtAddress : public otExtAddress, public Equatable +class ExtAddress : public otExtAddress, public Equatable, public Clearable { public: enum @@ -111,12 +112,6 @@ public: kReverseByteOrder, // Copy address bytes in reverse byte order. }; - /** - * This method clears the Extended Address (sets all bytes to zero). - * - */ - void Clear(void) { Fill(0); } - /** * This method fills all bytes of address with a given byte value. * @@ -426,7 +421,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class Key : public otMacKey, public Equatable +class Key : public otMacKey, public Equatable, public Clearable { public: enum @@ -434,12 +429,6 @@ public: kSize = OT_MAC_KEY_SIZE, // Key size in bytes. }; - /** - * This method clears the key (set all bytes to zero). - * - */ - void Clear(void) { memset(m8, 0, kSize); } - /** * This method gets a pointer to the buffer containing the key. * @@ -455,7 +444,7 @@ public: * */ OT_TOOL_PACKED_BEGIN -class ExtendedPanId : public otExtendedPanId, public Equatable +class ExtendedPanId : public otExtendedPanId, public Equatable, public Clearable { public: enum @@ -469,12 +458,6 @@ public: */ typedef String InfoString; - /** - * This method clears the Extended PAN Identifier (sets all bytes to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - /** * This method converts an address to a string. * diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index 6ca09928c..13d306810 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -45,11 +45,6 @@ using ot::Encoding::BigEndian::HostSwap32; namespace ot { namespace Ip6 { -void Address::Clear(void) -{ - memset(mFields.m8, 0, sizeof(mFields)); -} - bool Address::IsUnspecified(void) const { return (mFields.m32[0] == 0 && mFields.m32[1] == 0 && mFields.m32[2] == 0 && mFields.m32[3] == 0); diff --git a/src/core/net/ip6_address.hpp b/src/core/net/ip6_address.hpp index 1e3e72520..ca64746f3 100644 --- a/src/core/net/ip6_address.hpp +++ b/src/core/net/ip6_address.hpp @@ -38,6 +38,7 @@ #include +#include "common/clearable.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" #include "common/string.hpp" @@ -61,7 +62,7 @@ namespace Ip6 { * */ OT_TOOL_PACKED_BEGIN -class Address : public otIp6Address, public Equatable
+class Address : public otIp6Address, public Equatable
, public Clearable
{ public: /** @@ -105,12 +106,6 @@ public: */ typedef String InfoString; - /** - * This method clears the IPv6 address by setting it to the Unspecified Address "::". - * - */ - void Clear(void); - /** * This method indicates whether or not the IPv6 address is the Unspecified Address. * diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp index 35710c166..3414396db 100644 --- a/src/core/net/netif.hpp +++ b/src/core/net/netif.hpp @@ -36,6 +36,7 @@ #include "openthread-core-config.h" +#include "common/clearable.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/message.hpp" @@ -64,17 +65,13 @@ class Ip6; * This class implements an IPv6 network interface unicast address. * */ -class NetifUnicastAddress : public otNetifAddress, public LinkedListEntry +class NetifUnicastAddress : public otNetifAddress, + public LinkedListEntry, + public Clearable { friend class Netif; public: - /** - * This method clears the object (setting all fields to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - /** * This method returns the unicast address. * @@ -113,17 +110,13 @@ private: * This class implements an IPv6 network interface multicast address. * */ -class NetifMulticastAddress : public otNetifMulticastAddress, public LinkedListEntry +class NetifMulticastAddress : public otNetifMulticastAddress, + public LinkedListEntry, + public Clearable { friend class Netif; public: - /** - * This method clears the object (setting all fields to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - /** * This method returns the multicast address. * diff --git a/src/core/net/socket.hpp b/src/core/net/socket.hpp index bbe0a82b3..869406a36 100644 --- a/src/core/net/socket.hpp +++ b/src/core/net/socket.hpp @@ -36,6 +36,7 @@ #include "openthread-core-config.h" +#include "common/clearable.hpp" #include "net/ip6_address.hpp" namespace ot { @@ -204,7 +205,7 @@ public: * This class implements a socket address. * */ -class SockAddr : public otSockAddr +class SockAddr : public otSockAddr, public Clearable { public: /** @@ -213,12 +214,6 @@ public: */ SockAddr(void) { Clear(); } - /** - * This method clears the object (sets all fields to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - /** * This method returns a reference to the IPv6 address. * diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index 1dfc53ba5..be6bd1690 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -40,6 +40,7 @@ #include +#include "common/clearable.hpp" #include "common/equatable.hpp" #include "common/locator.hpp" #include "common/random.hpp" @@ -73,15 +74,9 @@ class MasterKey : public otMasterKey, public Equatable * */ OT_TOOL_PACKED_BEGIN -class Pskc : public otPskc, public Equatable +class Pskc : public otPskc, public Equatable, public Clearable { public: - /** - * This method clears the PSKc (sets all bytes to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - #if !OPENTHREAD_RADIO /** * This method generates a cryptographically secure random sequence to populate the Thread PSKc. diff --git a/src/core/thread/mle_types.hpp b/src/core/thread/mle_types.hpp index 6db7dfbeb..1a61a35f7 100644 --- a/src/core/thread/mle_types.hpp +++ b/src/core/thread/mle_types.hpp @@ -42,6 +42,7 @@ #include +#include "common/clearable.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" #include "common/string.hpp" @@ -448,15 +449,9 @@ public: * This class represents the Thread Leader Data. * */ -class LeaderData : public otLeaderData +class LeaderData : public otLeaderData, public Clearable { public: - /** - * This method clears the Leader Data (setting all the fields to zero). - * - */ - void Clear(void) { memset(this, 0, sizeof(*this)); } - /** * This method returns the Partition ID value. *