Browse Source

[4272a] Addressed comments

Francis Dupont 8 years ago
parent
commit
717a4723bf

+ 2 - 2
src/lib/eval/lexer.ll

@@ -144,6 +144,8 @@ addr6 [0-9a-fA-F]*\:[0-9a-fA-F]*\:[0-9a-fA-F:.]*
 "option"    return isc::eval::EvalParser::make_OPTION(loc);
 "relay4"    return isc::eval::EvalParser::make_RELAY4(loc);
 "relay6"    return isc::eval::EvalParser::make_RELAY6(loc);
+"peeraddr"  return isc::eval::EvalParser::make_PEERADDR(loc);
+"linkaddr"  return isc::eval::EvalParser::make_LINKADDR(loc);
 "text"      return isc::eval::EvalParser::make_TEXT(loc);
 "hex"       return isc::eval::EvalParser::make_HEX(loc);
 "exists"    return isc::eval::EvalParser::make_EXISTS(loc);
@@ -163,8 +165,6 @@ addr6 [0-9a-fA-F]*\:[0-9a-fA-F]*\:[0-9a-fA-F:.]*
 "pkt6"      return isc::eval::EvalParser::make_PKT6(loc);
 "msgtype"   return isc::eval::EvalParser::make_MSGTYPE(loc);
 "transid"   return isc::eval::EvalParser::make_TRANSID(loc);
-"peeraddr"  return isc::eval::EvalParser::make_PEERADDR(loc);
-"linkaddr"  return isc::eval::EvalParser::make_LINKADDR(loc);
 "substring" return isc::eval::EvalParser::make_SUBSTRING(loc);
 "all"       return isc::eval::EvalParser::make_ALL(loc);
 "concat"    return isc::eval::EvalParser::make_CONCAT(loc);

+ 2 - 2
src/lib/eval/parser.yy

@@ -46,6 +46,8 @@ using namespace isc::eval;
   OPTION "option"
   RELAY4 "relay4"
   RELAY6 "relay6"
+  PEERADDR "peeraddr"
+  LINKADDR "linkaddr"
   LBRACKET "["
   RBRACKET "]"
   DOT "."
@@ -68,8 +70,6 @@ using namespace isc::eval;
   PKT6 "pkt6"
   MSGTYPE "msgtype"
   TRANSID "transid"
-  PEERADDR "peeraddr"
-  LINKADDR "linkaddr"
   SUBSTRING "substring"
   ALL "all"
   COMA ","

+ 3 - 5
src/lib/eval/tests/token_unittest.cc

@@ -911,11 +911,9 @@ TEST_F(TokenTest, pkt4MetaData) {
     ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC)));
     EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
     ASSERT_EQ(1, values_.size());
-    ASSERT_EQ(4, values_.top().size());
-    EXPECT_EQ(10, values_.top()[0]);
-    EXPECT_EQ(0, values_.top()[1]);
-    EXPECT_EQ(0, values_.top()[2]);
-    EXPECT_EQ(2, values_.top()[3]);
+    vector<uint8_t> a2 = IOAddress("10.0.0.2").toBytes();
+    ASSERT_EQ(a2.size(), values_.top().size());
+    EXPECT_EQ(0, memcmp(&a2[0], &values_.top()[0], a2.size()));
 
     // Check destination (expect 10.0.0.1)
     clearStack();

+ 9 - 26
src/lib/eval/token.cc

@@ -7,28 +7,19 @@
 #include <eval/token.h>
 #include <eval/eval_log.h>
 #include <util/encode/hex.h>
+#include <util/io_utilities.h>
 #include <asiolink/io_address.h>
 #include <dhcp/pkt4.h>
-#include <boost/lexical_cast.hpp>
 #include <dhcp/pkt6.h>
+#include <boost/lexical_cast.hpp>
 #include <cstring>
 #include <string>
 
 using namespace isc::dhcp;
+using namespace isc::util;
 using namespace std;
 
-namespace {
-
-/// @brief encode in hexadecimal
-///
-/// @param value the value to encode
-/// @return 0x followed by the value encoded in hexa
-inline string toHex(string value) {
-    vector<uint8_t> bin(value.begin(), value.end());
-    return ("0x" + isc::util::encode::encodeHex(bin));
-}
-
-}; // end of anonymous namespace
+using isc::util::encode::toHex;
 
 void
 TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) {
@@ -217,10 +208,8 @@ TokenPkt::evaluate(const Pkt& pkt, ValueStack& values) {
         // the len() method is not const because of DHCPv6 relays.
         // We assume here it has no bad side effects...
         len = static_cast<uint32_t>(const_cast<Pkt&>(pkt).len());
-        binary.push_back(len >> 24);
-        binary.push_back((len >> 16) & 0xFF);
-        binary.push_back((len >> 8) & 0xFF);
-        binary.push_back(len & 0xFF);
+        binary.resize(sizeof(uint32_t));
+        static_cast<void>(writeUint32(len, &binary[0], binary.size()));
         type_str = "len";
         break;
 
@@ -238,15 +227,9 @@ TokenPkt::evaluate(const Pkt& pkt, ValueStack& values) {
     values.push(value);
 
     // Log what we pushed
-    if (is_binary) {
-        LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT)
-            .arg(type_str)
-            .arg(toHex(value));
-    } else {
-        LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT)
-            .arg(type_str)
-            .arg(value);
-    }
+    LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT)
+        .arg(type_str)
+        .arg(is_binary ? toHex(value) : value);
 }
 
 void

+ 1 - 1
src/lib/eval/token.h

@@ -342,7 +342,7 @@ protected:
 /// Currently supported meta datas are:
 /// - iface (incoming/outgoinginterface name)
 /// - src   (source IP address, 4 or 16 octets)
-/// - dst   (destination IP address, 4 octets)
+/// - dst   (destination IP address, 4 or 16 octets)
 /// - len   (length field in the UDP header, padded to 4 octets)
 class TokenPkt : public Token {
 public:

+ 9 - 0
src/lib/util/encode/hex.h

@@ -46,6 +46,15 @@ std::string encodeHex(const std::vector<uint8_t>& binary);
 /// \param result A vector in which the decoded %data is to be stored.
 void decodeHex(const std::string& input, std::vector<uint8_t>& result);
 
+/// \brief Encode in hexadecimal inline
+///
+/// \param value the value to encode
+/// \return 0x followed by the value encoded in hexa
+inline std::string toHex(std::string value) {
+    std::vector<uint8_t> bin(value.begin(), value.end());
+    return ("0x" + encodeHex(bin));
+}
+
 } // namespace encode
 } // namespace util
 } // namespace isc