17 #include <boost/lexical_cast.hpp>
41 .arg(
'\'' + value_ +
'\'');
44 TokenHexString::TokenHexString(
const string& str) : value_(
"") {
46 if ((str.size() < 3) ||
48 ((str[1] !=
'x') && (str[1] !=
'X'))) {
51 string digits = str.substr(2);
54 vector<uint8_t> binary;
59 if ((digits.length() % 2) != 0) {
60 digits = digits.insert(0,
"0");
68 value_.resize(binary.size());
69 memmove(&
value_[0], &binary[0], binary.size());
84 vector<uint8_t> binary;
93 value_.resize(binary.size());
94 memmove(&
value_[0], &binary[0], binary.size());
109 if (values.size() == 0) {
113 string op = values.top();
114 size_t size = op.size();
122 if ((size != V4ADDRESS_LEN) && (size != V6ADDRESS_LEN)) {
126 std::vector<uint8_t> binary(op.begin(), op.end());
128 if (size == V4ADDRESS_LEN) {
143 if (values.size() == 0) {
147 string op = values.top();
148 size_t size = op.size();
156 if (size !=
sizeof(int8_t)) {
161 tmp << static_cast<int32_t>(*(
reinterpret_cast<int8_t*
>(
const_cast<char*
>(op.data()))));
172 if (values.size() == 0) {
176 string op = values.top();
177 size_t size = op.size();
185 if (size !=
sizeof(int16_t)) {
190 uint16_t value = *(
reinterpret_cast<uint16_t*
>(
const_cast<char*
>(op.data())));
191 std::string data = EvalContext::fromUint16(value);
192 tmp << *(reinterpret_cast<int16_t*>(
const_cast<char*
>(data.data())));
203 if (values.size() == 0) {
207 string op = values.top();
208 size_t size = op.size();
216 if (size !=
sizeof(int32_t)) {
221 uint32_t value = *(
reinterpret_cast<uint32_t*
>(
const_cast<char*
>(op.data())));
222 std::string data = EvalContext::fromUint32(value);
223 tmp << *(reinterpret_cast<int32_t*>(
const_cast<char*
>(data.data())));
234 if (values.size() == 0) {
238 string op = values.top();
239 size_t size = op.size();
247 if (size !=
sizeof(uint8_t)) {
252 tmp << static_cast<uint32_t>(*(
reinterpret_cast<uint8_t*
>(
const_cast<char*
>(op.data()))));
263 if (values.size() == 0) {
267 string op = values.top();
268 size_t size = op.size();
276 if (size !=
sizeof(uint16_t)) {
281 uint16_t value = *(
reinterpret_cast<uint16_t*
>(
const_cast<char*
>(op.data())));
282 std::string data = EvalContext::fromUint16(value);
283 tmp << *(reinterpret_cast<uint16_t*>(
const_cast<char*
>(data.data())));
294 if (values.size() == 0) {
298 string op = values.top();
299 size_t size = op.size();
307 if (size !=
sizeof(uint32_t)) {
312 uint32_t value = *(
reinterpret_cast<uint32_t*
>(
const_cast<char*
>(op.data())));
313 std::string data = EvalContext::fromUint32(value);
314 tmp << *(reinterpret_cast<uint32_t*>(
const_cast<char*
>(data.data())));
334 opt_str = opt->toString();
336 std::vector<uint8_t> binary = opt->toBinary();
337 opt_str.resize(binary.size());
338 if (!binary.empty()) {
339 memmove(&opt_str[0], &binary[0], binary.size());
350 values.push(opt_str);
358 .arg(
toHex(opt_str));
362 .arg(
'\'' + opt_str +
'\'');
396 Pkt6& pkt6 =
dynamic_cast<Pkt6&
>(pkt);
402 uint8_t nesting_level =
static_cast<uint8_t
>(
nest_level_);
406 if (nesting_level < 0) {
410 static_cast<uint8_t>(nesting_level)));
421 }
catch (
const std::bad_cast&) {
430 vector<uint8_t> binary;
432 bool is_binary =
true;
433 bool print_hex =
true;
454 value = EvalContext::fromUint32(static_cast<uint32_t>(const_cast<Pkt&>(pkt).len()));
461 << static_cast<int>(type_) );
465 value.resize(binary.size());
466 if (!binary.empty()) {
467 memmove(&value[0], &binary[0], binary.size());
475 .arg(print_hex ?
toHex(value) : value);
480 vector<uint8_t> binary;
487 const Pkt4& pkt4 =
dynamic_cast<const Pkt4&
>(pkt);
496 "Packet does not have hardware address");
498 binary = hwaddr->hwaddr_;
520 value = EvalContext::fromUint32(pkt4.
getHlen());
525 value = EvalContext::fromUint32(pkt4.
getHtype());
529 value = EvalContext::fromUint32(pkt4.
getType());
530 type_str =
"msgtype";
533 value = EvalContext::fromUint32(pkt4.
getTransid());
534 type_str =
"transid";
538 << static_cast<int>(type_) );
541 }
catch (
const std::bad_cast&) {
545 if (!binary.empty()) {
546 value.resize(binary.size());
547 memmove(&value[0], &binary[0], binary.size());
565 const Pkt6& pkt6 =
dynamic_cast<const Pkt6&
>(pkt);
570 value = EvalContext::fromUint32(pkt6.
getType());
571 type_str =
"msgtype";
576 value = EvalContext::fromUint32(pkt6.
getTransid());
577 type_str =
"transid";
582 << static_cast<int>(type_) );
585 }
catch (
const std::bad_cast&) {
599 vector<uint8_t> binary;
604 const Pkt6& pkt6 =
dynamic_cast<const Pkt6&
>(pkt);
612 if (nesting_level < 0) {
616 relay_level =
static_cast<uint8_t
>(nesting_level);
622 type_str =
"linkaddr";
626 type_str =
"peeraddr";
641 }
catch (
const std::bad_cast&) {
646 value.resize(binary.size());
647 if (!binary.empty()) {
648 memmove(&value[0], &binary[0], binary.size());
661 if (values.size() < 2) {
663 "2 values for == operator, got " << values.size());
666 string op1 = values.top();
668 string op2 = values.top();
674 values.push(
"false");
680 .arg(
'\'' + values.top() +
'\'');
685 if (values.size() < 3) {
687 "3 values for substring operator, got " << values.size());
690 string len_str = values.top();
692 string start_str = values.top();
694 string string_str = values.top();
698 if (string_str.empty()) {
717 start_pos = boost::lexical_cast<
int>(start_str);
718 }
catch (
const boost::bad_lexical_cast&) {
720 <<
"' for the starting position of the substring "
721 <<
"couldn't be converted to an integer.");
724 if (len_str ==
"all") {
725 length = string_str.length();
727 length = boost::lexical_cast<
int>(len_str);
729 }
catch (
const boost::bad_lexical_cast&) {
731 <<
"' for the length of the substring "
732 <<
"couldn't be converted to an integer.");
735 const int string_length = string_str.length();
738 if ((start_pos < -string_length) || (start_pos >= string_length)) {
745 .arg(
toHex(string_str))
754 start_pos = string_length + start_pos;
759 if (length <= start_pos){
768 values.push(string_str.substr(start_pos, length));
774 .arg(
toHex(string_str))
775 .arg(
toHex(values.top()));
780 if (values.size() < 2) {
782 "2 values for concat, got " << values.size());
785 string op1 = values.top();
787 string op2 = values.top();
791 values.push(op2 + op1);
797 .arg(
toHex(values.top()));
802 if (values.size() < 3) {
804 "3 values for ifelse, got " << values.size());
807 string iffalse = values.top();
809 string iftrue = values.top();
811 string cond = values.top();
818 values.push(iffalse);
824 .arg(
'\'' + cond +
'\'')
829 .arg(
'\'' +cond +
'\'')
831 .arg(
toHex(iffalse));
837 if (values.size() < 2) {
839 "2 values for hexstring, got " << values.size());
842 string separator = values.top();
844 string binary = values.top();
850 for (
size_t i = 0; i < binary.size(); ++i) {
856 tmp << setw(2) << setfill(
'0')
857 << (
static_cast<unsigned>(binary[i]) & 0xff);
859 values.push(tmp.str());
870 if (values.size() == 0) {
874 string op = values.top();
881 values.push(
"false");
886 .arg(
'\'' + op +
'\'')
887 .arg(
'\'' + values.top() +
'\'');
892 if (values.size() < 2) {
894 "2 values for and operator, got " << values.size());
897 string op1 = values.top();
900 string op2 = values.top();
907 values.push(
"false");
912 .arg(
'\'' + op1 +
'\'')
913 .arg(
'\'' + op2 +
'\'')
914 .arg(
'\'' + values.top() +
'\'');
919 if (values.size() < 2) {
921 "2 values for or operator, got " << values.size());
924 string op1 = values.top();
927 string op2 = values.top();
934 values.push(
"false");
939 .arg(
'\'' + op1 +
'\'')
940 .arg(
'\'' + op2 +
'\'')
941 .arg(
'\'' + values.top() +
'\'');
949 values.push(
"false");
955 .arg(
'\'' + values.top() +
'\'');
959 uint16_t option_code)
960 :
TokenOption(option_code, repr), universe_(u), vendor_id_(vendor_id),
961 field_(option_code ? SUBOPTION : EXISTS) {
1009 .arg(vendor->getVendorId())
1018 string txt(
sizeof(uint32_t), 0);
1019 uint32_t value = htonl(vendor->getVendorId());
1020 memcpy(&txt[0], &value,
sizeof(uint32_t));
1023 .arg(vendor->getVendorId())
1037 .arg(vendor->getVendorId())
1039 values.push(
"true");
1114 .arg(vendor->getVendorId())
1123 string txt(
sizeof(uint32_t), 0);
1124 uint32_t value = htonl(vendor->getVendorId());
1125 memcpy(&txt[0], &value,
sizeof(uint32_t));
1128 .arg(vendor->getVendorId())
1141 .arg(vendor->getVendorId())
1143 values.push(
"true");
1147 size_t max = vendor->getTuplesNum();
1153 .arg(vendor->getVendorId())
1162 string txt(buf.begin(), buf.end());
1209 txt = sub->toString();
1211 std::vector<uint8_t> binary = sub->toBinary();
1212 txt.resize(binary.size());
1213 if (!binary.empty()) {
1214 memmove(&txt[0], &binary[0], binary.size());
1238 .arg(
'\'' + txt +
'\'');
data chunk, used in derived vendor-class only
boost::shared_ptr< OptionVendorClass > OptionVendorClassPtr
Defines a pointer to the OptionVendorClass.
void evaluate(Pkt &pkt, ValueStack &values)
Logical negation.
uint32_t vendor_id_
Enterprise-id value.
virtual OptionPtr getOption(Pkt &pkt)
Attempts to get a suboption.
const isc::log::MessageID EVAL_DEBUG_VENDOR_NO_OPTION
void evaluate(Pkt &pkt, ValueStack &values)
Alternative.
uint16_t sub_option_code_
Code of the sub-option to be extracted.
The order where Token subtypes are declared should be:
uint8_t getHlen() const
Returns hlen field.
const isc::log::MessageID EVAL_DEBUG_IPADDRESS
const isc::log::MessageID EVAL_DEBUG_OPTION
const isc::log::MessageID EVAL_DEBUG_IPADDRESSTOTEXT
const isc::log::MessageID EVAL_DEBUG_SUB_OPTION
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_EXISTS
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
virtual OptionPtr getSubOption(const OptionPtr &parent)
Attempts to retrieve a sub-option.
std::string value_
< Constant value (empty string if the IP address cannot be converted)
const isc::asiolink::IOAddress & getRelay6PeerAddress(uint8_t relay_level) const
return the peer address field from a relay option
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_NO_OPTION
const isc::log::MessageID EVAL_DEBUG_PKT6
const isc::log::MessageID EVAL_DEBUG_SUBSTRING_EMPTY
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
const isc::log::MessageID EVAL_DEBUG_SUB_OPTION_NO_OPTION
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
uint16_t option_code_
Code of the option to be extracted.
If this token fetches a suboption, not a field.
FieldType type_
field to get
const isc::log::MessageID EVAL_DEBUG_PKT4
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value of the specified packet.
Base class for classes representing DHCP messages.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (check if client_class_ was added to packet client classes)
boost::shared_ptr< Option > OptionPtr
const isc::log::MessageID EVAL_DEBUG_VENDOR_ENTERPRISE_ID
Universe
defines option universe DHCPv4 or DHCPv6
Represents a DHCPv6 packet.
FieldType
Specifies a field of the vendor option.
OptionPtr getRelayOption(uint16_t option_code, uint8_t nesting_level)
Returns option inserted by relay.
ClientClass client_class_
The client class name.
Link address field (IPv6 address)
void evaluate(Pkt &pkt, ValueStack &values)
Extracts the specified field from the requested relay.
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
isc::log::Logger eval_logger("eval")
Eval Logger.
std::vector< RelayInfo > relay_info_
Relay information.
TokenRelay4Option(const uint16_t option_code, const RepresentationType &rep_type)
Constructor for extracting sub-option from RAI (option 82)
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified option from the specified relay block.
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND
const isc::log::MessageID EVAL_DEBUG_SUBSTRING
const isc::asiolink::IOAddress & getRelay6LinkAddress(uint8_t relay_level) const
return the link address field from a relay option
virtual OptionPtr getOption(Pkt &pkt)
Attempts to retrieve an option.
void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
const isc::log::MessageID EVAL_DEBUG_INT8TOTEXT
const isc::log::MessageID EVAL_DEBUG_VENDOR_EXISTS
Option::Universe universe_
Universe (V4 or V6)
const isc::log::MessageID EVAL_DEBUG_STRING
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
enterprise-id field (vendor-info, vendor-class)
void decodeHex(const string &input, vector< uint8_t > &result)
Decode a text encoded in the base16 ('hex') format into the original data.
uint8_t getType() const
Returns DHCP message type (e.g.
TokenInteger(const uint32_t value)
Integer value set during construction.
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
std::string value_
Constant value.
Evaluation context, an interface to the expression evaluation.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
const isc::log::MessageID EVAL_DEBUG_PKT
const isc::log::MessageID EVAL_DEBUG_RELAY6
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
const isc::log::MessageID EVAL_DEBUG_MEMBER
void evaluate(Pkt &pkt, ValueStack &values)
Extract a substring from a string.
chaddr field (up to 16 bytes link-layer address)
Token that represents a value of an option.
HWAddrPtr getHWAddr() const
returns hardware address information
const isc::log::MessageID EVAL_DEBUG_SUBSTRING_RANGE
htype (hardware address type)
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified sub-option of option 82 from the packet.
const isc::log::MessageID EVAL_DEBUG_EQUAL
virtual std::string pushFailure(ValueStack &values)
Auxiliary method that puts string representing a failure.
message type (not really a field, content of option 53)
void evaluate(Pkt &pkt, ValueStack &values)
Logical and.
std::vector< uint8_t > Buffer
Defines a type of the data buffer used to hold the opaque data.
FieldType getField() const
Returns field.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding or an empty string if...
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Concatenate two values.
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
const isc::log::MessageID EVAL_DEBUG_IFELSE_FALSE
const isc::log::MessageID EVAL_DEBUG_CONCAT
const isc::log::MessageID EVAL_DEBUG_RELAY6_RANGE
TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field)
Constructor used for accessing a field.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding)
Represents DHCPv4 packet.
uint16_t getDataIndex() const
Returns data index.
const isc::log::MessageID EVAL_DEBUG_INT32TOTEXT
const isc::log::MessageID EVAL_DEBUG_TOHEXSTRING
void evaluate(Pkt &pkt, ValueStack &values)
Compare two values.
const isc::log::MessageID EVAL_DEBUG_UINT8TOTEXT
RepresentationType
Token representation type.
const isc::log::MessageID EVAL_DEBUG_UINT16TOTEXT
EvalBadStack is thrown when more or less parameters are on the stack than expected.
const isc::log::MessageID EVAL_DEBUG_OR
uint32_t getTransid() const
Returns value of transaction-id field.
const Buffer & getData() const
Returns a reference to the buffer holding tuple data.
Peer address field (IPv6 address)
RepresentationType representation_type_
Representation type.
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
void evaluate(Pkt &pkt, ValueStack &values)
Convert a binary value to its hexadecimal string representation.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
uint32_t getVendorId() const
Returns enterprise-id.
Represents a single instance of the opaque data preceded by length.
const isc::log::MessageID EVAL_DEBUG_NOT
const isc::log::MessageID EVAL_DEBUG_IFELSE_TRUE
const isc::log::MessageID EVAL_DEBUG_HEXSTRING
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
std::string getIface() const
Returns interface name.
std::string toHex(std::string value)
Encode in hexadecimal inline.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
const isc::log::MessageID EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH
const isc::log::MessageID EVAL_DEBUG_UINT32TOTEXT
uint8_t getHtype() const
Returns htype field.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
EvalTypeError is thrown when a value on the stack has a content with an unexpected type...
uint16_t index_
Data chunk index.
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
void evaluate(Pkt &pkt, ValueStack &values)
Logical or.
TokenIpAddress(const std::string &addr)
Value is set during token construction.
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
int8_t nest_level_
nesting level of the relay block to use
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID
The IOAddress class represents an IP addresses (version agnostic)
boost::shared_ptr< OptionVendor > OptionVendorPtr
Pointer to a vendor option.
int8_t nest_level_
Specifies field of the DHCPv6 relay option to get.
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
void evaluate(Pkt &pkt, ValueStack &values)
Evaluates the values of the option.
const isc::log::MessageID EVAL_DEBUG_AND
hlen (hardware address length)
virtual uint8_t getType() const
Returns message type (e.g.
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH
This class represents vendor-specific information option.
TokenVendorClass(Option::Universe u, uint32_t vendor_id, RepresentationType repr)
This constructor is used to access fields.
transaction id (integer but manipulated as a string)
This class encapsulates DHCPv6 Vendor Class and DHCPv4 V-I Vendor Class options.
Token that represents vendor options in DHCPv4 and DHCPv6.
const isc::log::MessageID EVAL_DEBUG_INT16TOTEXT
FieldType field_
Specifies which field should be accessed.
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_DATA