17 #include <boost/lexical_cast.hpp>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/algorithm/string/classification.hpp>
25 #include <boost/algorithm/string/split.hpp>
46 .arg(
'\'' + value_ +
'\'');
49 TokenHexString::TokenHexString(
const string& str) : value_(
"") {
51 if ((str.size() < 3) ||
53 ((str[1] !=
'x') && (str[1] !=
'X'))) {
56 string digits = str.substr(2);
59 vector<uint8_t> binary;
64 if ((digits.length() % 2) != 0) {
65 digits = digits.insert(0,
"0");
73 value_.resize(binary.size());
74 memmove(&
value_[0], &binary[0], binary.size());
89 if (values.size() == 0) {
93 string op = values.top();
96 string result(boost::algorithm::to_lower_copy(op));
101 .arg(
'\'' + op +
'\'')
102 .arg(
'\'' + result +
'\'');
107 if (values.size() == 0) {
111 string op = values.top();
114 string result(boost::algorithm::to_upper_copy(op));
119 .arg(
'\'' + op +
'\'')
120 .arg(
'\'' + result +
'\'');
125 vector<uint8_t> binary;
134 value_.resize(binary.size());
135 memmove(&
value_[0], &binary[0], binary.size());
150 if (values.size() == 0) {
154 string op = values.top();
155 size_t size = op.size();
163 if ((size != V4ADDRESS_LEN) && (size != V6ADDRESS_LEN)) {
167 std::vector<uint8_t> binary(op.begin(), op.end());
169 if (size == V4ADDRESS_LEN) {
184 if (values.size() == 0) {
188 string op = values.top();
189 size_t size = op.size();
197 if (size !=
sizeof(int8_t)) {
202 tmp << static_cast<int32_t>(*(
reinterpret_cast<const int8_t*
>(op.data())));
213 if (values.size() == 0) {
217 string op = values.top();
218 size_t size = op.size();
226 if (size !=
sizeof(int16_t)) {
231 int16_t value =
static_cast<int16_t
>(
readUint16(
const_cast<const char*
>(op.data()), size));
243 if (values.size() == 0) {
247 string op = values.top();
248 size_t size = op.size();
256 if (size !=
sizeof(int32_t)) {
261 int32_t value =
static_cast<int32_t
>(
readUint32(
const_cast<const char*
>(op.data()), size));
273 if (values.size() == 0) {
277 string op = values.top();
278 size_t size = op.size();
286 if (size !=
sizeof(uint8_t)) {
291 tmp << static_cast<uint32_t>(*(
reinterpret_cast<const uint8_t*
>(op.data())));
302 if (values.size() == 0) {
306 string op = values.top();
307 size_t size = op.size();
315 if (size !=
sizeof(uint16_t)) {
320 uint16_t value =
readUint16(
const_cast<const char*
>(op.data()), size);
332 if (values.size() == 0) {
336 string op = values.top();
337 size_t size = op.size();
345 if (size !=
sizeof(uint32_t)) {
350 uint32_t value =
readUint32(
const_cast<const char*
>(op.data()), size);
371 opt_str = opt->toString();
373 std::vector<uint8_t> binary = opt->toBinary();
374 opt_str.resize(binary.size());
375 if (!binary.empty()) {
376 memmove(&opt_str[0], &binary[0], binary.size());
387 values.push(opt_str);
395 .arg(
toHex(opt_str));
399 .arg(
'\'' + opt_str +
'\'');
433 Pkt6& pkt6 =
dynamic_cast<Pkt6&
>(pkt);
439 uint8_t nesting_level =
static_cast<uint8_t
>(
nest_level_);
443 if (nesting_level < 0) {
447 static_cast<uint8_t
>(nesting_level)));
458 }
catch (
const std::bad_cast&) {
467 vector<uint8_t> binary;
469 bool is_binary =
true;
470 bool print_hex =
true;
491 value = EvalContext::fromUint32(
static_cast<uint32_t
>(
const_cast<Pkt&
>(pkt).len()));
501 value.resize(binary.size());
502 if (!binary.empty()) {
503 memmove(&value[0], &binary[0], binary.size());
511 .arg(print_hex ?
toHex(value) : value);
516 vector<uint8_t> binary;
523 const Pkt4& pkt4 =
dynamic_cast<const Pkt4&
>(pkt);
532 "Packet does not have hardware address");
534 binary = hwaddr->hwaddr_;
556 value = EvalContext::fromUint32(pkt4.
getHlen());
561 value = EvalContext::fromUint32(pkt4.
getHtype());
565 value = EvalContext::fromUint32(pkt4.
getType());
566 type_str =
"msgtype";
569 value = EvalContext::fromUint32(pkt4.
getTransid());
570 type_str =
"transid";
576 }
catch (
const std::bad_cast&) {
580 if (!binary.empty()) {
581 value.resize(binary.size());
582 memmove(&value[0], &binary[0], binary.size());
600 const Pkt6& pkt6 =
dynamic_cast<const Pkt6&
>(pkt);
605 value = EvalContext::fromUint32(pkt6.
getType());
606 type_str =
"msgtype";
611 value = EvalContext::fromUint32(pkt6.
getTransid());
612 type_str =
"transid";
619 }
catch (
const std::bad_cast&) {
633 vector<uint8_t> binary;
638 const Pkt6& pkt6 =
dynamic_cast<const Pkt6&
>(pkt);
646 if (nesting_level < 0) {
650 relay_level =
static_cast<uint8_t
>(nesting_level);
656 type_str =
"linkaddr";
660 type_str =
"peeraddr";
675 }
catch (
const std::bad_cast&) {
680 value.resize(binary.size());
681 if (!binary.empty()) {
682 memmove(&value[0], &binary[0], binary.size());
695 if (values.size() < 2) {
697 "2 values for == operator, got " << values.size());
700 string op1 = values.top();
702 string op2 = values.top();
708 values.push(
"false");
714 .arg(
'\'' + values.top() +
'\'');
719 if (values.size() < 3) {
721 "3 values for substring operator, got " << values.size());
724 string len_str = values.top();
726 string start_str = values.top();
728 string string_str = values.top();
732 if (string_str.empty()) {
751 start_pos = boost::lexical_cast<int>(start_str);
752 }
catch (
const boost::bad_lexical_cast&) {
754 <<
"' for the starting position of the substring "
755 <<
"couldn't be converted to an integer.");
758 if (len_str ==
"all") {
759 length = string_str.length();
761 length = boost::lexical_cast<int>(len_str);
763 }
catch (
const boost::bad_lexical_cast&) {
765 <<
"' for the length of the substring "
766 <<
"couldn't be converted to an integer.");
769 const int string_length = string_str.length();
772 if ((start_pos < -string_length) || (start_pos >= string_length)) {
779 .arg(
toHex(string_str))
788 start_pos = string_length + start_pos;
793 if (length <= start_pos){
802 values.push(string_str.substr(start_pos, length));
808 .arg(
toHex(string_str))
809 .arg(
toHex(values.top()));
814 if (values.size() < 3) {
816 "3 values for split operator, got " << values.size());
820 string field_str = values.top();
822 string delim_str = values.top();
824 string string_str = values.top();
828 if (string_str.empty()) {
844 field = boost::lexical_cast<int>(field_str);
845 }
catch (
const boost::bad_lexical_cast&) {
847 <<
"' for the field field for split "
848 <<
"couldn't be converted to an integer.");
852 if (delim_str.empty()) {
853 values.push(string_str);
860 .arg(
toHex(values.top()));
865 std::vector<std::string> fields;
866 boost::split(fields, string_str, boost::is_any_of(delim_str),
867 boost::algorithm::token_compress_off);
870 if (field < 1 || field > fields.size()) {
884 values.push(fields[field - 1]);
891 .arg(
toHex(values.top()));
896 if (values.size() < 2) {
898 "2 values for concat, got " << values.size());
901 string op1 = values.top();
903 string op2 = values.top();
907 values.push(op2 + op1);
913 .arg(
toHex(values.top()));
918 if (values.size() < 3) {
920 "3 values for ifelse, got " << values.size());
923 string iffalse = values.top();
925 string iftrue = values.top();
927 string cond = values.top();
934 values.push(iffalse);
940 .arg(
'\'' + cond +
'\'')
945 .arg(
'\'' +cond +
'\'')
947 .arg(
toHex(iffalse));
953 if (values.size() < 2) {
955 "2 values for hexstring, got " << values.size());
958 string separator = values.top();
960 string binary = values.top();
966 for (
size_t i = 0; i < binary.size(); ++i) {
972 tmp << setw(2) << setfill(
'0')
973 << (
static_cast<unsigned>(binary[i]) & 0xff);
975 values.push(tmp.str());
986 if (values.size() == 0) {
990 string op = values.top();
997 values.push(
"false");
1002 .arg(
'\'' + op +
'\'')
1003 .arg(
'\'' + values.top() +
'\'');
1008 if (values.size() < 2) {
1010 "2 values for and operator, got " << values.size());
1013 string op1 = values.top();
1016 string op2 = values.top();
1021 values.push(
"true");
1023 values.push(
"false");
1028 .arg(
'\'' + op1 +
'\'')
1029 .arg(
'\'' + op2 +
'\'')
1030 .arg(
'\'' + values.top() +
'\'');
1035 if (values.size() < 2) {
1037 "2 values for or operator, got " << values.size());
1040 string op1 = values.top();
1043 string op2 = values.top();
1048 values.push(
"true");
1050 values.push(
"false");
1055 .arg(
'\'' + op1 +
'\'')
1056 .arg(
'\'' + op2 +
'\'')
1057 .arg(
'\'' + values.top() +
'\'');
1063 values.push(
"true");
1065 values.push(
"false");
1071 .arg(
'\'' + values.top() +
'\'');
1075 uint16_t option_code)
1076 :
TokenOption(option_code, repr), universe_(u), vendor_id_(vendor_id),
1077 field_(option_code ? SUBOPTION : EXISTS) {
1109 OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
1125 .arg(vendor->getVendorId())
1134 string txt(
sizeof(uint32_t), 0);
1135 uint32_t value = htonl(vendor->getVendorId());
1136 memcpy(&txt[0], &value,
sizeof(uint32_t));
1139 .arg(vendor->getVendorId())
1153 .arg(vendor->getVendorId())
1155 values.push(
"true");
1230 .arg(vendor->getVendorId())
1239 string txt(
sizeof(uint32_t), 0);
1240 uint32_t value = htonl(vendor->getVendorId());
1241 memcpy(&txt[0], &value,
sizeof(uint32_t));
1244 .arg(vendor->getVendorId())
1257 .arg(vendor->getVendorId())
1259 values.push(
"true");
1263 size_t max = vendor->getTuplesNum();
1269 .arg(vendor->getVendorId())
1278 string txt(buf.begin(), buf.end());
1325 txt = sub->toString();
1327 std::vector<uint8_t> binary = sub->toBinary();
1328 txt.resize(binary.size());
1329 if (!binary.empty()) {
1330 memmove(&txt[0], &binary[0], binary.size());
1354 .arg(
'\'' + txt +
'\'');
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
EvalBadStack is thrown when more or less parameters are on the stack than expected.
EvalTypeError is thrown when a value on the stack has a content with an unexpected type.
Represents a single instance of the opaque data preceded by length.
const Buffer & getData() const
Returns a reference to the buffer holding tuple data.
std::vector< uint8_t > Buffer
Defines a type of the data buffer used to hold the opaque data.
Universe
defines option universe DHCPv4 or DHCPv6
Represents DHCPv4 packet.
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
HWAddrPtr getHWAddr() const
returns hardware address information
uint8_t getHlen() const
Returns hlen field.
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
uint8_t getType() const
Returns DHCP message type (e.g.
uint8_t getHtype() const
Returns htype field.
Represents a DHCPv6 packet.
OptionPtr getRelayOption(uint16_t option_code, uint8_t nesting_level)
Returns option inserted by relay.
const isc::asiolink::IOAddress & getRelay6PeerAddress(uint8_t relay_level) const
return the peer address field from a relay option
const isc::asiolink::IOAddress & getRelay6LinkAddress(uint8_t relay_level) const
return the link address field from a relay option
std::vector< RelayInfo > relay_info_
Relay information.
virtual uint8_t getType() const
Returns message type (e.g.
Base class for classes representing DHCP messages.
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
uint32_t getTransid() const
Returns value of transaction-id field.
std::string getIface() const
Returns interface name.
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
void evaluate(Pkt &pkt, ValueStack &values)
Logical and.
void evaluate(Pkt &pkt, ValueStack &values)
Concatenate two values.
void evaluate(Pkt &pkt, ValueStack &values)
Compare two values.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding or an empty string if...
std::string value_
Constant value.
void evaluate(Pkt &pkt, ValueStack &values)
Alternative.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
TokenInteger(const uint32_t value)
Integer value set during construction.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding)
std::string value_
< Constant value (empty string if the IP address cannot be converted)
TokenIpAddress(const std::string &addr)
Value is set during token construction.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the evaluated string expression converted to lower case on the stack)
ClientClass client_class_
The client class name.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (check if client_class_ was added to packet client classes)
void evaluate(Pkt &pkt, ValueStack &values)
Logical negation.
Token that represents a value of an option.
virtual OptionPtr getOption(Pkt &pkt)
Attempts to retrieve an option.
void evaluate(Pkt &pkt, ValueStack &values)
Evaluates the values of the option.
RepresentationType representation_type_
Representation type.
uint16_t option_code_
Code of the option to be extracted.
RepresentationType
Token representation type.
virtual std::string pushFailure(ValueStack &values)
Auxiliary method that puts string representing a failure.
void evaluate(Pkt &pkt, ValueStack &values)
Logical or.
@ CIADDR
ciaddr (IPv4 address)
@ HLEN
hlen (hardware address length)
@ HTYPE
htype (hardware address type)
@ GIADDR
giaddr (IPv4 address)
@ CHADDR
chaddr field (up to 16 bytes link-layer address)
@ YIADDR
yiaddr (IPv4 address)
@ SIADDR
siaddr (IPv4 address)
@ TRANSID
transaction-id (xid)
@ MSGTYPE
message type (not really a field, content of option 53)
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value of the specified packet.
@ TRANSID
transaction id (integer but manipulated as a string)
@ DST
destination (IP address)
@ IFACE
interface name (string)
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified sub-option of option 82 from the packet.
TokenRelay4Option(const uint16_t option_code, const RepresentationType &rep_type)
Constructor for extracting sub-option from RAI (option 82)
FieldType type_
field to get
void evaluate(Pkt &pkt, ValueStack &values)
Extracts the specified field from the requested relay.
@ LINKADDR
Link address field (IPv6 address)
@ PEERADDR
Peer address field (IPv6 address)
int8_t nest_level_
Specifies field of the DHCPv6 relay option to get.
int8_t nest_level_
nesting level of the relay block to use
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified option from the specified relay block.
void evaluate(Pkt &pkt, ValueStack &values)
Extract a field from a delimited string.
The order where Token subtypes are declared should be:
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
uint16_t sub_option_code_
Code of the sub-option to be extracted.
virtual OptionPtr getSubOption(const OptionPtr &parent)
Attempts to retrieve a sub-option.
void evaluate(Pkt &pkt, ValueStack &values)
Extract a substring from a string.
void evaluate(Pkt &pkt, ValueStack &values)
Convert a binary value to its hexadecimal string representation.
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the evaluated string expression converted to upper case on the stack)
TokenVendorClass(Option::Universe u, uint32_t vendor_id, RepresentationType repr)
This constructor is used to access fields.
uint16_t getDataIndex() const
Returns data index.
uint16_t index_
Data chunk index.
void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Token that represents vendor options in DHCPv4 and DHCPv6.
FieldType
Specifies a field of the vendor option.
@ DATA
data chunk, used in derived vendor-class only
@ EXISTS
vendor[123].exists
@ ENTERPRISE_ID
enterprise-id field (vendor-info, vendor-class)
@ SUBOPTION
If this token fetches a suboption, not a field.
Option::Universe universe_
Universe (V4 or V6)
uint32_t vendor_id_
Enterprise-id value.
FieldType field_
Specifies which field should be accessed.
uint32_t getVendorId() const
Returns enterprise-id.
TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field)
Constructor used for accessing a field.
virtual OptionPtr getOption(Pkt &pkt)
Attempts to get a suboption.
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
FieldType getField() const
Returns field.
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
Evaluation context, an interface to the expression evaluation.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< OptionVendor > OptionVendorPtr
Pointer to a vendor option.
const isc::log::MessageID EVAL_DEBUG_RELAY6_RANGE
const isc::log::MessageID EVAL_DEBUG_UCASE
const isc::log::MessageID EVAL_DEBUG_UINT32TOTEXT
const isc::log::MessageID EVAL_DEBUG_UINT16TOTEXT
const isc::log::MessageID EVAL_DEBUG_SPLIT_EMPTY
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_EXISTS
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND
const isc::log::MessageID EVAL_DEBUG_PKT
const isc::log::MessageID EVAL_DEBUG_HEXSTRING
const isc::log::MessageID EVAL_DEBUG_TOHEXSTRING
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_DATA
const isc::log::MessageID EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE
const isc::log::MessageID EVAL_DEBUG_INT32TOTEXT
const isc::log::MessageID EVAL_DEBUG_OPTION
const isc::log::MessageID EVAL_DEBUG_SUBSTRING
const isc::log::MessageID EVAL_DEBUG_PKT6
const isc::log::MessageID EVAL_DEBUG_RELAY6
const isc::log::MessageID EVAL_DEBUG_OR
const isc::log::MessageID EVAL_DEBUG_SUB_OPTION_NO_OPTION
const isc::log::MessageID EVAL_DEBUG_INT8TOTEXT
const isc::log::MessageID EVAL_DEBUG_VENDOR_ENTERPRISE_ID
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
const isc::log::MessageID EVAL_DEBUG_STRING
const isc::log::MessageID EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH
const isc::log::MessageID EVAL_DEBUG_SUB_OPTION
const isc::log::MessageID EVAL_DEBUG_SPLIT
const isc::log::MessageID EVAL_DEBUG_PKT4
const isc::log::MessageID EVAL_DEBUG_INT16TOTEXT
isc::log::Logger eval_logger("eval")
Eval Logger.
const isc::log::MessageID EVAL_DEBUG_CONCAT
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH
const isc::log::MessageID EVAL_DEBUG_VENDOR_EXISTS
const isc::log::MessageID EVAL_DEBUG_VENDOR_NO_OPTION
const isc::log::MessageID EVAL_DEBUG_LCASE
boost::shared_ptr< OptionVendorClass > OptionVendorClassPtr
Defines a pointer to the OptionVendorClass.
const isc::log::MessageID EVAL_DEBUG_IPADDRESS
const isc::log::MessageID EVAL_DEBUG_AND
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_NO_OPTION
const isc::log::MessageID EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID
const isc::log::MessageID EVAL_DEBUG_UINT8TOTEXT
const isc::log::MessageID EVAL_DEBUG_EQUAL
const isc::log::MessageID EVAL_DEBUG_IPADDRESSTOTEXT
const isc::log::MessageID EVAL_DEBUG_SPLIT_DELIM_EMPTY
const isc::log::MessageID EVAL_DEBUG_MEMBER
const isc::log::MessageID EVAL_DEBUG_IFELSE_TRUE
const isc::log::MessageID EVAL_DEBUG_NOT
boost::shared_ptr< Option > OptionPtr
const isc::log::MessageID EVAL_DEBUG_SUBSTRING_RANGE
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
const isc::log::MessageID EVAL_DEBUG_IFELSE_FALSE
const isc::log::MessageID EVAL_DEBUG_SUBSTRING_EMPTY
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
void decodeHex(const string &input, vector< uint8_t > &result)
Decode a text encoded in the base16 ('hex') format into the original data.
std::string toHex(std::string value)
Encode in hexadecimal inline.
uint32_t readUint32(const void *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.