From 43926c8256a7694adb3538345a4dba52ea0eecac Mon Sep 17 00:00:00 2001 From: Thomas Markwalder Date: Mon, 20 Apr 2015 10:59:08 -0400 Subject: [PATCH] [3743] Added label methods to Pkt and Pkt4 --- src/lib/dhcp/pkt.h | 11 +++++++++ src/lib/dhcp/pkt4.cc | 18 ++++++++++++++ src/lib/dhcp/pkt4.h | 21 ++++++++++++++++ src/lib/dhcp/tests/pkt4_unittest.cc | 38 +++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/src/lib/dhcp/pkt.h b/src/lib/dhcp/pkt.h index 1bacff6cb2..e0f9844711 100755 --- a/src/lib/dhcp/pkt.h +++ b/src/lib/dhcp/pkt.h @@ -141,6 +141,17 @@ public: /// @return true if option was deleted, false if no such option existed bool delOption(uint16_t type); + /// @brief Returns text representation primary packet identifiers + /// + /// This method is intended to be used to provide as a consistent way to + /// identify packets within log statements. Derivations should supply + /// there own implementation. + /// + /// @return string with text representation + virtual std::string getLabel() { + isc_throw(NotImplemented, "Pkt::getLabel()"); + } + /// @brief Returns text representation of the packet. /// /// This function is useful mainly for debugging. diff --git a/src/lib/dhcp/pkt4.cc b/src/lib/dhcp/pkt4.cc index 06fd12db30..a0d4b98c86 100644 --- a/src/lib/dhcp/pkt4.cc +++ b/src/lib/dhcp/pkt4.cc @@ -281,6 +281,24 @@ void Pkt4::setType(uint8_t dhcp_type) { } } +std::string +Pkt4::getLabel() { + return makeLabel(hwaddr_, getOption(DHO_DHCP_CLIENT_IDENTIFIER), transid_); +} + +std::string +Pkt4::makeLabel(HWAddrPtr hwaddr, OptionPtr client_id, uint32_t transid) +{ + stringstream tmp; + + tmp << "hwaddr=[" << (hwaddr ? hwaddr->toText() : "no info") + << "], client-id=[" << (client_id ? client_id->toText() : "no info") + << "], transid=0x" << hex << transid << dec; + + return tmp.str(); +} + + std::string Pkt4::toText() { stringstream tmp; diff --git a/src/lib/dhcp/pkt4.h b/src/lib/dhcp/pkt4.h index d93a56e695..a622e3179f 100755 --- a/src/lib/dhcp/pkt4.h +++ b/src/lib/dhcp/pkt4.h @@ -108,6 +108,27 @@ public: /// Method will throw exception if anomaly is found. void check(); + /// @brief Returns text representation primary packet identifiers + /// + /// This method is intended to be used to provide a consistent way to + /// identify packets within log statements. It is an instance-level + /// wrapper around static makeLabel()(). See this method for string + /// content. + /// + /// @return string with text representation + std::string getLabel(); + + /// @brief Returns text representation of the given packet identifiers + /// + /// @param hwaddr - hardware address to include in the string + /// @param client_id - DHO_DHCP_CLIENT_ID_OPTION containing the client id + /// to include in the string + /// @param transid - numeric transaction id to include in the string + /// + /// @return string with text representation + static std::string makeLabel(HWAddrPtr hwaddr, OptionPtr client_id, + uint32_t transid); + /// @brief Returns text representation of the packet. /// /// This function is useful mainly for debugging. diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc index 39da5e5bdd..7fa3fe5e4c 100644 --- a/src/lib/dhcp/tests/pkt4_unittest.cc +++ b/src/lib/dhcp/tests/pkt4_unittest.cc @@ -878,4 +878,42 @@ TEST_F(Pkt4Test, getMAC) { ASSERT_TRUE(*dummy_hwaddr == *pkt.getMAC(HWAddr::HWADDR_SOURCE_RAW)); } +// Tests that getLabel/makeLabel methods produces the expected strings based on +// packet content. +TEST_F(Pkt4Test, getLabel) { + Pkt4 pkt(DHCPOFFER, 1234); + + // Verify makeLabel() handles empty values + EXPECT_EQ ("hwaddr=[no info], client-id=[no info], transid=0x0", + Pkt4::makeLabel(HWAddrPtr(), OptionPtr(), 0)); + + // Verify an "empty" packet label is as we expect + EXPECT_EQ ("hwaddr=[hwtype=1 ], client-id=[no info], transid=0x4d2", + pkt.getLabel()); + + // Set that packet hardware address, then verify getLabel + const uint8_t hw[] = { 2, 4, 6, 8, 10, 12 }; // MAC + const uint8_t hw_type = 123; // hardware type + HWAddrPtr dummy_hwaddr(new HWAddr(hw, sizeof(hw), hw_type)); + pkt.setHWAddr(dummy_hwaddr); + + EXPECT_EQ ("hwaddr=[hwtype=123 02:04:06:08:0a:0c]," + " client-id=[no info], transid=0x4d2", pkt.getLabel()); + + // Add a client id to the packet then verify getLabel + OptionBuffer clnt_id(4); + for (int i = 0; i < 4; i++) { + clnt_id[i] = 100 + i; + } + + OptionPtr opt(new Option(Option::V4, DHO_DHCP_CLIENT_IDENTIFIER, + clnt_id.begin(), clnt_id.begin() + 4)); + pkt.addOption(opt); + + EXPECT_EQ ("hwaddr=[hwtype=123 02:04:06:08:0a:0c]," + " client-id=[type=61, len=4: 64:65:66:67], transid=0x4d2", + pkt.getLabel()); + +} + } // end of anonymous namespace -- 2.47.3