From: Shawn Routhier Date: Fri, 8 Apr 2016 06:05:50 +0000 (-0700) Subject: [trac4269] Update to put 4 byte strings on the value stack for integers X-Git-Tag: trac4106_update_base~43^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75063d9b417eb97d2c973975bd1eb0da5a2fe232;p=thirdparty%2Fkea.git [trac4269] Update to put 4 byte strings on the value stack for integers --- diff --git a/doc/guide/classify.xml b/doc/guide/classify.xml index 2aca1e4e31..bb9f7a06ae 100644 --- a/doc/guide/classify.xml +++ b/doc/guide/classify.xml @@ -172,14 +172,14 @@ sub-option with code "code" from the DHCPv4 Relay Agent Information option Message Type in DHCPv6 packet pkt6.msgtype - The value of the message type field in the DHCPv6 packet. Transaction ID in DHCPv6 packet pkt6.transid - The value of the transaction id in the DHCPv6 packet. @@ -226,9 +226,9 @@ sub-option with code "code" from the DHCPv4 Relay Agent Information option "pkt6" refers to information from the client request. To access any information from an intermediate relay use "relay6". "pkt6.msgtype" - outputs the string representation of the integer value for the message - type, for example SOLICIT will be '1'. "pkt6.transid" outputs the string - representation of the value of the transaction ID. + and "pkt6.transid" output a 4 byte binary string for the message type + or transaction id. For example the message type SOLICIT will be + "0x00000001" or simply 1 as in "pkt6.msgtype == 1". diff --git a/src/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc index bd63f1a68f..c47d9e8096 100644 --- a/src/lib/eval/tests/token_unittest.cc +++ b/src/lib/eval/tests/token_unittest.cc @@ -977,14 +977,16 @@ TEST_F(TokenTest, pkt6Fields) { ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::MSGTYPE))); EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); ASSERT_EQ(1, values_.size()); - EXPECT_EQ("1", values_.top()); + uint32_t expected = htonl(1); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); // Check the transaction id field clearStack(); ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID))); EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); ASSERT_EQ(1, values_.size()); - EXPECT_EQ("12345", values_.top()); + expected = htonl(12345); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); // Check that working with a v4 packet generates an error clearStack(); diff --git a/src/lib/eval/token.cc b/src/lib/eval/token.cc index 05b784ec7b..9610f4af66 100644 --- a/src/lib/eval/token.cc +++ b/src/lib/eval/token.cc @@ -278,6 +278,7 @@ TokenOr::evaluate(const Pkt& /*pkt*/, ValueStack& values) { void TokenPkt6::evaluate(const Pkt& pkt, ValueStack& values) { + vector binary; try { // Check if it's a Pkt6. If it's not the dynamic_cast will throw // std::bad_cast (failed dynamic_cast returns NULL for pointers and @@ -286,24 +287,32 @@ TokenPkt6::evaluate(const Pkt& pkt, ValueStack& values) { switch (type_) { case MSGTYPE: { - // msg type is an uint8_t integer. We need to represent it as a string. - stringstream tmp; - tmp << static_cast(pkt6.getType()); - values.push(tmp.str()); - return; + // msg type is an uint8_t integer. We want a 4 byte string so 0 pad. + binary.push_back(0); + binary.push_back(0); + binary.push_back(0); + binary.push_back(pkt6.getType()); + break; } case TRANSID: { - // transaction id is an uint32_t integer. We need to represent it as a string. - stringstream tmp; - tmp << static_cast(pkt6.getTransid()); - values.push(tmp.str()); - return; + // transaction id is an uint32_t integer. We want a 4 byte string so copy + uint32_t transid = pkt6.getTransid(); + binary.push_back(transid >> 24); + binary.push_back((transid >> 16) & 0xFF); + binary.push_back((transid >> 8) & 0xFF); + binary.push_back(transid & 0xFF); + break; } default: - isc_throw(EvalTypeError, "Bad filed specified: " + isc_throw(EvalTypeError, "Bad field specified: " << static_cast(type_) ); } } catch (const std::bad_cast&) { isc_throw(EvalTypeError, "Specified packet is not Pkt6"); } + + string value; + value.resize(binary.size()); + memmove(&value[0], &binary[0], binary.size()); + values.push(value); }; diff --git a/src/lib/eval/token.h b/src/lib/eval/token.h index 598b968ad9..e3a1854e56 100644 --- a/src/lib/eval/token.h +++ b/src/lib/eval/token.h @@ -446,8 +446,10 @@ public: /// @brief Token that represents fields of DHCPv6 packet. /// -/// For example in the expression pkt6.msgtype == "1" -/// this token represents the message type of the DHCPv6 packet +/// For example in the expression pkt6.msgtype == 1 +/// this token represents the message type of the DHCPv6 packet. +/// The integer values are placed on the value stack as 4 byte +/// strings. /// /// Currently supported fields are: /// - msgtype