From: Tomek Mrugalski Date: Thu, 2 Oct 2014 13:49:16 +0000 (+0200) Subject: [3546] hw_type in HWAddr class is now 16 bits long. X-Git-Tag: trac3489_base~2^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=003c8279cf76515acabb1027587aa2873b7ace38;p=thirdparty%2Fkea.git [3546] hw_type in HWAddr class is now 16 bits long. --- diff --git a/src/lib/dhcp/hwaddr.cc b/src/lib/dhcp/hwaddr.cc index 5f130804b5..82bfd1d3f2 100644 --- a/src/lib/dhcp/hwaddr.cc +++ b/src/lib/dhcp/hwaddr.cc @@ -31,14 +31,14 @@ HWAddr::HWAddr() :htype_(HTYPE_ETHER) { } -HWAddr::HWAddr(const uint8_t* hwaddr, size_t len, uint8_t htype) +HWAddr::HWAddr(const uint8_t* hwaddr, size_t len, uint16_t htype) :hwaddr_(hwaddr, hwaddr + len), htype_(htype) { if (len > MAX_HWADDR_LEN) { isc_throw(InvalidParameter, "hwaddr length exceeds MAX_HWADDR_LEN"); } } -HWAddr::HWAddr(const std::vector& hwaddr, uint8_t htype) +HWAddr::HWAddr(const std::vector& hwaddr, uint16_t htype) :hwaddr_(hwaddr), htype_(htype) { if (hwaddr.size() > MAX_HWADDR_LEN) { isc_throw(InvalidParameter, @@ -49,7 +49,7 @@ HWAddr::HWAddr(const std::vector& hwaddr, uint8_t htype) std::string HWAddr::toText(bool include_htype) const { std::stringstream tmp; if (include_htype) { - tmp << "hwtype=" << static_cast(htype_) << " "; + tmp << "hwtype=" << static_cast(htype_) << " "; } tmp << std::hex; bool delim = false; @@ -65,7 +65,7 @@ std::string HWAddr::toText(bool include_htype) const { } HWAddr -HWAddr::fromText(const std::string& text, const uint8_t htype) { +HWAddr::fromText(const std::string& text, const uint16_t htype) { /// @todo optimize stream operations here. std::vector split_text; boost::split(split_text, text, boost::is_any_of(":"), diff --git a/src/lib/dhcp/hwaddr.h b/src/lib/dhcp/hwaddr.h index 3b4d62687c..a2fa5ec1e6 100644 --- a/src/lib/dhcp/hwaddr.h +++ b/src/lib/dhcp/hwaddr.h @@ -41,18 +41,22 @@ public: /// @param hwaddr pointer to hardware address /// @param len length of the address pointed by hwaddr /// @param htype hardware type - HWAddr(const uint8_t* hwaddr, size_t len, uint8_t htype); + HWAddr(const uint8_t* hwaddr, size_t len, uint16_t htype); /// @brief constructor, based on C++ vector /// @param hwaddr const reference to hardware address /// @param htype hardware type - HWAddr(const std::vector& hwaddr, uint8_t htype); + HWAddr(const std::vector& hwaddr, uint16_t htype); // Vector that keeps the actual hardware address std::vector hwaddr_; - // Hardware type - uint8_t htype_; + /// Hardware type + /// + /// @note It used to be uint8_t as used in DHCPv4. However, since we're + /// expanding MAC addresses support to DHCPv6 that uses hw_type as + /// 16 bits, we need to be able to store that wider format. + uint16_t htype_; /// @brief Returns textual representation of a hardware address /// (e.g. 00:01:02:03:04:05) @@ -83,7 +87,7 @@ public: /// /// @return Instance of the HW address created from text. static HWAddr fromText(const std::string& text, - const uint8_t htype = HTYPE_ETHER); + const uint16_t htype = HTYPE_ETHER); /// @brief Compares two hardware addresses for equality bool operator==(const HWAddr& other) const; diff --git a/src/lib/dhcp/tests/hwaddr_unittest.cc b/src/lib/dhcp/tests/hwaddr_unittest.cc index 7a904d36e5..184965c7cf 100644 --- a/src/lib/dhcp/tests/hwaddr_unittest.cc +++ b/src/lib/dhcp/tests/hwaddr_unittest.cc @@ -163,4 +163,16 @@ TEST(HWAddrTest, fromText) { } +// Checks that 16 bits values can be stored in HWaddr +TEST(HWAddrTest, 16bits) { + + uint8_t data[] = {0, 1, 2, 3, 4, 5}; + uint16_t htype = 257; + HWAddrPtr hw(new HWAddr(data, sizeof(data), htype)); + + EXPECT_EQ("hwtype=257 00:01:02:03:04:05", hw->toText()); + + +} + } // end of anonymous namespace