From: Marcin Siodelski Date: Wed, 21 Oct 2015 09:20:15 +0000 (+0200) Subject: [3977] Added variants of PktX::makeLabel excluding transaction id. X-Git-Tag: fdfb_base~1^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=472f3887092b58b25eae48e2726843e2b94d4e3f;p=thirdparty%2Fkea.git [3977] Added variants of PktX::makeLabel excluding transaction id. --- diff --git a/src/lib/dhcp/pkt4.cc b/src/lib/dhcp/pkt4.cc index 44a96ca53c..cae19d0754 100644 --- a/src/lib/dhcp/pkt4.cc +++ b/src/lib/dhcp/pkt4.cc @@ -357,15 +357,26 @@ Pkt4::getLabel() const { std::string Pkt4::makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id, const uint32_t transid) { + // Create label with HW address and client identifier. + stringstream label; + label << makeLabel(hwaddr, client_id); + + // Append transaction id. + label << ", tid=0x" << hex << transid << dec; + + return label.str(); +} + +std::string +Pkt4::makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id) { stringstream label; label << "[" << (hwaddr ? hwaddr->toText() : "no hwaddr info") << "], cid=[" << (client_id ? client_id->toText() : "no info") - << "], tid=0x" << hex << transid << dec; + << "]"; return label.str(); } - std::string Pkt4::toText() const { stringstream output; diff --git a/src/lib/dhcp/pkt4.h b/src/lib/dhcp/pkt4.h index 549be78da8..0c7d009e16 100644 --- a/src/lib/dhcp/pkt4.h +++ b/src/lib/dhcp/pkt4.h @@ -119,6 +119,15 @@ public: const ClientIdPtr& client_id, const uint32_t transid); + /// @brief Returns text representation of the given packet identifers. + /// + /// This variant of the method does not include transaction id. + /// + /// @param hwaddr hardware address to include in the string, it may be + /// NULL. + /// @param client_id client id to include in the string, it may be NULL. + static std::string makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id); + /// @brief Returns text representation of the packet. /// /// This function is useful mainly for debugging. diff --git a/src/lib/dhcp/pkt6.cc b/src/lib/dhcp/pkt6.cc index 7881672476..91d1e18049 100644 --- a/src/lib/dhcp/pkt6.cc +++ b/src/lib/dhcp/pkt6.cc @@ -498,22 +498,31 @@ Pkt6::getMACFromDUID() { std::string Pkt6::makeLabel(const DuidPtr duid, const uint32_t transid, const HWAddrPtr& hwaddr) { + // Create label with DUID and HW address. + std::stringstream label; + label << makeLabel(duid, hwaddr); + + // Append transaction id. + label << ", tid=0x" << std::hex << transid << std::dec; + + return (label.str()); +} + +std::string +Pkt6::makeLabel(const DuidPtr duid, const HWAddrPtr& hwaddr) { std::stringstream label; // DUID should be present at all times, so explicitly inform when // it is no present (no info). label << "duid=[" << (duid ? duid->toText() : "no info") - << "],"; + << "]"; // HW address is typically not carried in the DHCPv6 mmessages // and can be extracted using various, but not fully reliable, // techniques. If it is not present, don't print anything. if (hwaddr) { - label << " [" << hwaddr->toText() << "],"; + label << ", [" << hwaddr->toText() << "]"; } - // Transaction id is always there. - label << " tid=0x" << std::hex << transid << std::dec; - return (label.str()); } diff --git a/src/lib/dhcp/pkt6.h b/src/lib/dhcp/pkt6.h index febb92d35f..32d960a0c6 100644 --- a/src/lib/dhcp/pkt6.h +++ b/src/lib/dhcp/pkt6.h @@ -173,6 +173,17 @@ public: static std::string makeLabel(const DuidPtr duid, const uint32_t transid, const HWAddrPtr& hwaddr); + /// @brief Returns text representation of the given packet identifiers. + /// + /// This variant of the method does not include transaction id. + /// + /// @param duid Pointer to the client identifier or NULL. + /// @param transid Numeric transaction id to include in the string. + /// @param hwaddr Hardware address to include in the string or NULL. + /// + /// @return String with text representation of the packet identifiers. + static std::string makeLabel(const DuidPtr duid, const HWAddrPtr& hwaddr); + /// @brief Returns text representation of the primary packet identifiers /// /// This method is intended to be used to provide a consistent way to diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc index 908ab6f2ed..6f0ce0ed3f 100644 --- a/src/lib/dhcp/tests/pkt4_unittest.cc +++ b/src/lib/dhcp/tests/pkt4_unittest.cc @@ -1024,6 +1024,23 @@ TEST_F(Pkt4Test, getLabel) { } +// Tests that the variant of makeLabel which doesn't include transaction +// id produces expected output. +TEST_F(Pkt4Test, makeLabelWithoutTransactionId) { + EXPECT_EQ("[no hwaddr info], cid=[no info]", + Pkt4::makeLabel(HWAddrPtr(), ClientIdPtr())); + + // Test non-null hardware address. + HWAddrPtr hwaddr(new HWAddr(HWAddr::fromText("01:02:03:04:05:06", 123))); + EXPECT_EQ("[hwtype=123 01:02:03:04:05:06], cid=[no info]", + Pkt4::makeLabel(hwaddr, ClientIdPtr())); + + // Test non-null client identifier. + ClientIdPtr cid = ClientId::fromText("01:02:03:04"); + EXPECT_EQ("[hwtype=123 01:02:03:04:05:06], cid=[01:02:03:04]", + Pkt4::makeLabel(hwaddr, cid)); +} + // Tests that the correct DHCPv4 message name is returned for various // message types. TEST_F(Pkt4Test, getName) { diff --git a/src/lib/dhcp/tests/pkt6_unittest.cc b/src/lib/dhcp/tests/pkt6_unittest.cc index 12e80e10bd..b917241cdb 100644 --- a/src/lib/dhcp/tests/pkt6_unittest.cc +++ b/src/lib/dhcp/tests/pkt6_unittest.cc @@ -1474,6 +1474,30 @@ TEST_F(Pkt6Test, makeLabel) { Pkt6::makeLabel(DuidPtr(), 0x0, HWAddrPtr())); } +// Tests that the variant of makeLabel which doesn't include transaction +// id produces expected output. +TEST_F(Pkt6Test, makeLabelWithoutTransactionId) { + DuidPtr duid(new DUID(DUID::fromText("0102020202030303030303"))); + HWAddrPtr hwaddr(new HWAddr(HWAddr::fromText("01:02:03:04:05:06", + HTYPE_ETHER))); + + // Specify DUID and no HW Address. + EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03]", + Pkt6::makeLabel(duid, HWAddrPtr())); + + // Specify HW Address and no DUID. + EXPECT_EQ("duid=[no info], [hwtype=1 01:02:03:04:05:06]", + Pkt6::makeLabel(DuidPtr(), hwaddr)); + + // Specify both DUID and HW Address. + EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03], " + "[hwtype=1 01:02:03:04:05:06]", + Pkt6::makeLabel(duid, hwaddr)); + + // Specify neither DUID nor HW Address. + EXPECT_EQ("duid=[no info]", Pkt6::makeLabel(DuidPtr(), HWAddrPtr())); +} + // This test verifies that it is possible to obtain the packet // identifiers in the textual format from the packet instance. TEST_F(Pkt6Test, getLabel) {