]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3743] Added label methods to Pkt and Pkt4
authorThomas Markwalder <tmark@isc.org>
Mon, 20 Apr 2015 14:59:08 +0000 (10:59 -0400)
committerThomas Markwalder <tmark@isc.org>
Mon, 20 Apr 2015 14:59:08 +0000 (10:59 -0400)
src/lib/dhcp/pkt.h
src/lib/dhcp/pkt4.cc
src/lib/dhcp/pkt4.h
src/lib/dhcp/tests/pkt4_unittest.cc

index 1bacff6cb2db3dda2af1dad751271f62043e1014..e0f98447113ccc4680507abda135a39199078604 100755 (executable)
@@ -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.
index 06fd12db30b95ecb3fbf771db9e24e6bba9802c6..a0d4b98c8635608e0cd468299b9540a3454fd0fa 100644 (file)
@@ -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;
index d93a56e695e63db08fae55cd1060e18ece25960c..a622e3179fe5e40ee013e2b3be85e28735507a03 100755 (executable)
@@ -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.
index 39da5e5bddfe07830978614f7e3c041ca95764c9..7fa3fe5e4cd7a19095006e6ffba1dcb8cf2d1b3f 100644 (file)
@@ -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