Browse Source

[3743] Added label methods to Pkt and Pkt4

Thomas Markwalder 10 years ago
parent
commit
43926c8256
4 changed files with 88 additions and 0 deletions
  1. 11 0
      src/lib/dhcp/pkt.h
  2. 18 0
      src/lib/dhcp/pkt4.cc
  3. 21 0
      src/lib/dhcp/pkt4.h
  4. 38 0
      src/lib/dhcp/tests/pkt4_unittest.cc

+ 11 - 0
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.

+ 18 - 0
src/lib/dhcp/pkt4.cc

@@ -282,6 +282,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;
     tmp << "localAddr=" << local_addr_ << ":" << local_port_

+ 21 - 0
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.

+ 38 - 0
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