Kea 2.5.8
token.h
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef TOKEN_H
8#define TOKEN_H
9
11#include <dhcp/pkt.h>
12#include <stack>
13
14namespace isc {
15namespace dhcp {
16
17class Token;
18
20typedef boost::shared_ptr<Token> TokenPtr;
21
28typedef std::vector<TokenPtr> Expression;
29
30typedef boost::shared_ptr<Expression> ExpressionPtr;
31
33typedef std::stack<std::string> ValueStack;
34
37class EvalBadStack : public Exception {
38public:
39 EvalBadStack(const char* file, size_t line, const char* what) :
40 isc::Exception(file, line, what) { };
41};
42
45class EvalTypeError : public Exception {
46public:
47 EvalTypeError(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) { };
49};
50
62class Token {
63public:
64
77 virtual void evaluate(Pkt& pkt, ValueStack& values) = 0;
78
80 virtual ~Token() {}
81
89 static inline bool toBool(std::string value) {
90 if (value == "true") {
91 return (true);
92 } else if (value == "false") {
93 return (false);
94 } else {
95 isc_throw(EvalTypeError, "Incorrect boolean. Expected exactly "
96 "\"false\" or \"true\", got \"" << value << "\"");
97 }
98 }
99};
100
108
113class TokenString : public Token {
114public:
118 TokenString(const std::string& str) : value_(str) {}
119
124 void evaluate(Pkt& pkt, ValueStack& values);
125
126protected:
127 std::string value_;
128};
129
134class TokenHexString : public Token {
135public:
141 TokenHexString(const std::string& str);
142
149 void evaluate(Pkt& pkt, ValueStack& values);
150
151protected:
152 std::string value_;
153};
154
159class TokenLowerCase : public Token {
160public:
163
169 void evaluate(Pkt& pkt, ValueStack& values);
170};
171
176class TokenUpperCase : public Token {
177public:
180
186 void evaluate(Pkt& pkt, ValueStack& values);
187};
188
195class TokenInteger : public TokenString {
196public:
203 TokenInteger(const uint32_t value);
204
210 uint32_t getInteger() const {
211 return (int_value_);
212 }
213
214protected:
215 uint32_t int_value_;
216};
217
222class TokenIpAddress : public Token {
223public:
227 TokenIpAddress(const std::string& addr);
228
234 void evaluate(Pkt& pkt, ValueStack& values);
235
236protected:
238 std::string value_;
239};
240
246public:
249
255 void evaluate(Pkt& pkt, ValueStack& values);
256};
257
262class TokenInt8ToText : public Token {
263public:
266
273 void evaluate(Pkt& pkt, ValueStack& values);
274};
275
280class TokenInt16ToText : public Token {
281public:
284
291 void evaluate(Pkt& pkt, ValueStack& values);
292};
293
298class TokenInt32ToText : public Token {
299public:
302
309 void evaluate(Pkt& pkt, ValueStack& values);
310};
311
316class TokenUInt8ToText : public Token {
317public:
320
327 void evaluate(Pkt& pkt, ValueStack& values);
328};
329
334class TokenUInt16ToText : public Token {
335public:
338
345 void evaluate(Pkt& pkt, ValueStack& values);
346};
347
352class TokenUInt32ToText : public Token {
353public:
356
363 void evaluate(Pkt& pkt, ValueStack& values);
364};
365
375class TokenOption : public Token {
376public:
377
388 EXISTS
389 };
390
398 TokenOption(const uint16_t option_code, const RepresentationType& rep_type)
399 : option_code_(option_code), representation_type_(rep_type) {}
400
409 void evaluate(Pkt& pkt, ValueStack& values);
410
417 uint16_t getCode() const {
418 return (option_code_);
419 }
420
428 return (representation_type_);
429 }
430
431protected:
441 virtual OptionPtr getOption(Pkt& pkt);
442
449 virtual std::string pushFailure(ValueStack& values);
450
451 uint16_t option_code_;
453};
454
467public:
468
473 TokenRelay4Option(const uint16_t option_code,
474 const RepresentationType& rep_type);
475
476protected:
480 virtual OptionPtr getOption(Pkt& pkt);
481};
482
499public:
506 TokenRelay6Option(const int8_t nest_level, const uint16_t option_code,
507 const RepresentationType& rep_type)
508 : TokenOption(option_code, rep_type), nest_level_(nest_level) {}
509
517 int8_t getNest() const {
518 return (nest_level_);
519 }
520
521protected:
525 virtual OptionPtr getOption(Pkt& pkt);
526
527 int8_t nest_level_;
528};
529
540class TokenPkt : public Token {
541public:
542
548 LEN
549 };
550
552 TokenPkt(const MetadataType type) : type_(type) {}
553
561 void evaluate(Pkt& pkt, ValueStack& values);
562
568 return (type_);
569 }
570
571private:
573 MetadataType type_;
574};
575
589class TokenPkt4 : public Token {
590public:
591
603 };
604
607 : type_(type) {}
608
618 void evaluate(Pkt& pkt, ValueStack& values);
619
625 return (type_);
626 }
627
628private:
630 FieldType type_;
631};
632
643class TokenPkt6 : public Token {
644public:
648 TRANSID
649 };
650
653 : type_(type) {}
654
664 void evaluate(Pkt& pkt, ValueStack& values);
665
671 return (type_);
672 }
673
674private:
676 FieldType type_;
677};
678
694class TokenRelay6Field : public Token {
695public:
696
700 LINKADDR
701 };
702
708 TokenRelay6Field(const int8_t nest_level, const FieldType type)
709 : nest_level_(nest_level), type_(type) {}
710
718 void evaluate(Pkt& pkt, ValueStack& values);
719
727 int8_t getNest() const {
728 return (nest_level_);
729 }
730
738 return (type_);
739 }
740
741protected:
743 int8_t nest_level_;
745};
746
751class TokenEqual : public Token {
752public:
755
768 void evaluate(Pkt& pkt, ValueStack& values);
769};
770
776class TokenSubstring : public Token {
777public:
780
825 void evaluate(Pkt& pkt, ValueStack& values);
826};
827
828class TokenSplit : public Token {
829public:
832
869 void evaluate(Pkt& pkt, ValueStack& values);
870};
871
877class TokenConcat : public Token {
878public:
881
893 void evaluate(Pkt& pkt, ValueStack& values);
894};
895
905class TokenIfElse : public Token {
906public:
909
924 void evaluate(Pkt& pkt, ValueStack& values);
925};
926
935class TokenToHexString : public Token {
936public:
939
963 void evaluate(Pkt& pkt, ValueStack& values);
964};
965
970class TokenNot : public Token {
971public:
974
988 void evaluate(Pkt& pkt, ValueStack& values);
989};
990
994class TokenAnd : public Token {
995public:
998
1013 void evaluate(Pkt& pkt, ValueStack& values);
1014};
1015
1019class TokenOr : public Token {
1020public:
1023
1038 void evaluate(Pkt& pkt, ValueStack& values);
1039};
1040
1044class TokenMember : public Token {
1045public:
1049 TokenMember(const std::string& client_class)
1050 : client_class_(client_class) {
1051 }
1052
1058 void evaluate(Pkt& pkt, ValueStack& values);
1059
1067 return (client_class_);
1068 }
1069
1070protected:
1073};
1074
1089class TokenVendor : public TokenOption {
1090public:
1091
1097 DATA
1099
1105 TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field);
1106
1118 TokenVendor(Option::Universe u, uint32_t vendor_id, RepresentationType repr,
1119 uint16_t option_code = 0);
1120
1126 uint32_t getVendorId() const;
1127
1133 FieldType getField() const;
1134
1157 virtual void evaluate(Pkt& pkt, ValueStack& values);
1158
1159protected:
1168 virtual OptionPtr getOption(Pkt& pkt);
1169
1175
1180 uint32_t vendor_id_;
1181
1184};
1185
1204public:
1205
1211 TokenVendorClass(Option::Universe u, uint32_t vendor_id, RepresentationType repr);
1212
1219 TokenVendorClass(Option::Universe u, uint32_t vendor_id, FieldType field,
1220 uint16_t index = 0);
1221
1226 uint16_t getDataIndex() const;
1227
1228protected:
1229
1253 void evaluate(Pkt& pkt, ValueStack& values);
1254
1256 uint16_t index_;
1257};
1258
1275class TokenSubOption : public virtual TokenOption {
1276public:
1277
1280
1288 TokenSubOption(const uint16_t option_code,
1289 const uint16_t sub_option_code,
1290 const RepresentationType& rep_type)
1291 : TokenOption(option_code, rep_type), sub_option_code_(sub_option_code) {}
1292
1304 virtual void evaluate(Pkt& pkt, ValueStack& values);
1305
1312 uint16_t getSubCode() const {
1313 return (sub_option_code_);
1314 }
1315
1316protected:
1321 virtual OptionPtr getSubOption(const OptionPtr& parent);
1322
1324};
1325
1326} // end of isc::dhcp namespace
1327} // end of isc namespace
1328
1329#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
EvalBadStack is thrown when more or less parameters are on the stack than expected.
Definition: token.h:37
EvalBadStack(const char *file, size_t line, const char *what)
Definition: token.h:39
EvalTypeError is thrown when a value on the stack has a content with an unexpected type.
Definition: token.h:45
EvalTypeError(const char *file, size_t line, const char *what)
Definition: token.h:47
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
Base class for classes representing DHCP messages.
Definition: pkt.h:161
Token that represents logical and operator.
Definition: token.h:994
void evaluate(Pkt &pkt, ValueStack &values)
Logical and.
Definition: token.cc:1039
TokenAnd()
Constructor (does nothing)
Definition: token.h:997
Token that represents concat operator (concatenates two other tokens)
Definition: token.h:877
void evaluate(Pkt &pkt, ValueStack &values)
Concatenate two values.
Definition: token.cc:922
TokenConcat()
Constructor (does nothing)
Definition: token.h:880
Token that represents equality operator (compares two other tokens)
Definition: token.h:751
void evaluate(Pkt &pkt, ValueStack &values)
Compare two values.
Definition: token.cc:713
TokenEqual()
Constructor (does nothing)
Definition: token.h:754
Token representing a constant string in hexadecimal format.
Definition: token.h:134
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding or an empty string if...
Definition: token.cc:79
std::string value_
Constant value.
Definition: token.h:152
Token that represents an alternative.
Definition: token.h:905
TokenIfElse()
Constructor (does nothing)
Definition: token.h:908
void evaluate(Pkt &pkt, ValueStack &values)
Alternative.
Definition: token.cc:945
Token representing a 16 bit integer as a string.
Definition: token.h:280
TokenInt16ToText()
Constructor (does nothing)
Definition: token.h:283
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:219
Token representing a 32 bit integer as a string.
Definition: token.h:298
TokenInt32ToText()
Constructor (does nothing)
Definition: token.h:301
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:250
Token representing an 8 bit integer as a string.
Definition: token.h:262
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:189
TokenInt8ToText()
Constructor (does nothing)
Definition: token.h:265
Token representing an unsigned 32 bit integer.
Definition: token.h:195
uint32_t getInteger() const
Returns integer value.
Definition: token.h:210
uint32_t int_value_
value as integer (stored for testing only)
Definition: token.h:215
Token representing an IP address as a string.
Definition: token.h:245
TokenIpAddressToText()
Constructor (does nothing)
Definition: token.h:248
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:154
Token representing an IP address as a constant string.
Definition: token.h:222
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding)
Definition: token.cc:143
std::string value_
< Constant value (empty string if the IP address cannot be converted)
Definition: token.h:238
Token representing a constant lower case string.
Definition: token.h:159
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the evaluated string expression converted to lower case on the stack)
Definition: token.cc:90
TokenLowerCase()
Constructor (does nothing)
Definition: token.h:162
Token that represents client class membership.
Definition: token.h:1044
const ClientClass & getClientClass() const
Returns client class name.
Definition: token.h:1066
ClientClass client_class_
The client class name.
Definition: token.h:1072
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (check if client_class_ was added to packet client classes)
Definition: token.cc:1095
TokenMember(const std::string &client_class)
Constructor.
Definition: token.h:1049
Token that represents logical negation operator.
Definition: token.h:970
TokenNot()
Constructor (does nothing)
Definition: token.h:973
void evaluate(Pkt &pkt, ValueStack &values)
Logical negation.
Definition: token.cc:1016
Token that represents a value of an option.
Definition: token.h:375
uint16_t getCode() const
Returns option-code.
Definition: token.h:417
RepresentationType getRepresentation() const
Returns representation-type.
Definition: token.h:427
virtual OptionPtr getOption(Pkt &pkt)
Attempts to retrieve an option.
Definition: token.cc:373
void evaluate(Pkt &pkt, ValueStack &values)
Evaluates the values of the option.
Definition: token.cc:378
RepresentationType representation_type_
Representation type.
Definition: token.h:452
uint16_t option_code_
Code of the option to be extracted.
Definition: token.h:451
RepresentationType
Token representation type.
Definition: token.h:385
TokenOption(const uint16_t option_code, const RepresentationType &rep_type)
Constructor that takes an option code as a parameter.
Definition: token.h:398
virtual std::string pushFailure(ValueStack &values)
Auxiliary method that puts string representing a failure.
Definition: token.cc:418
Token that represents logical or operator.
Definition: token.h:1019
void evaluate(Pkt &pkt, ValueStack &values)
Logical or.
Definition: token.cc:1067
TokenOr()
Constructor (does nothing)
Definition: token.h:1022
Token that represents fields of a DHCPv4 packet.
Definition: token.h:589
FieldType getType()
Returns field type.
Definition: token.h:624
FieldType
enum value that determines the field.
Definition: token.h:593
@ CIADDR
ciaddr (IPv4 address)
Definition: token.h:596
@ HLEN
hlen (hardware address length)
Definition: token.h:599
@ HTYPE
htype (hardware address type)
Definition: token.h:600
@ GIADDR
giaddr (IPv4 address)
Definition: token.h:595
@ CHADDR
chaddr field (up to 16 bytes link-layer address)
Definition: token.h:594
@ YIADDR
yiaddr (IPv4 address)
Definition: token.h:597
@ SIADDR
siaddr (IPv4 address)
Definition: token.h:598
@ TRANSID
transaction-id (xid)
Definition: token.h:602
@ MSGTYPE
message type (not really a field, content of option 53)
Definition: token.h:601
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
Definition: token.cc:530
TokenPkt4(const FieldType type)
Constructor (does nothing)
Definition: token.h:606
Token that represents fields of DHCPv6 packet.
Definition: token.h:643
TokenPkt6(const FieldType type)
Constructor (does nothing)
Definition: token.h:652
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value of the specified packet.
Definition: token.cc:609
FieldType getType()
Returns field type.
Definition: token.h:670
FieldType
enum value that determines the field.
Definition: token.h:646
@ TRANSID
transaction id (integer but manipulated as a string)
Definition: token.h:648
@ MSGTYPE
msg type
Definition: token.h:647
Token that represents meta data of a DHCP packet.
Definition: token.h:540
MetadataType
enum value that determines the field.
Definition: token.h:544
@ LEN
length (4 octets)
Definition: token.h:548
@ DST
destination (IP address)
Definition: token.h:547
@ IFACE
interface name (string)
Definition: token.h:545
@ SRC
source (IP address)
Definition: token.h:546
MetadataType getType()
Returns metadata type.
Definition: token.h:567
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
Definition: token.cc:479
TokenPkt(const MetadataType type)
Constructor (does nothing)
Definition: token.h:552
Represents a sub-option inserted by the DHCPv4 relay.
Definition: token.h:466
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified sub-option of option 82 from the packet.
Definition: token.cc:432
Token that represents a value of a field within a DHCPv6 relay encapsulation.
Definition: token.h:694
FieldType type_
field to get
Definition: token.h:744
void evaluate(Pkt &pkt, ValueStack &values)
Extracts the specified field from the requested relay.
Definition: token.cc:649
FieldType
enum value that determines the field.
Definition: token.h:698
@ LINKADDR
Link address field (IPv6 address)
Definition: token.h:700
@ PEERADDR
Peer address field (IPv6 address)
Definition: token.h:699
int8_t nest_level_
Specifies field of the DHCPv6 relay option to get.
Definition: token.h:743
FieldType getType()
Returns field type.
Definition: token.h:737
TokenRelay6Field(const int8_t nest_level, const FieldType type)
Constructor that takes a nesting level and field type as parameters.
Definition: token.h:708
int8_t getNest() const
Returns nest-level.
Definition: token.h:727
Token that represents a value of an option within a DHCPv6 relay encapsulation.
Definition: token.h:498
int8_t nest_level_
nesting level of the relay block to use
Definition: token.h:527
int8_t getNest() const
Returns nest-level.
Definition: token.h:517
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified option from the specified relay block.
Definition: token.cc:443
TokenRelay6Option(const int8_t nest_level, const uint16_t option_code, const RepresentationType &rep_type)
Constructor that takes a nesting level and an option code as parameters.
Definition: token.h:506
TokenSplit()
Constructor (does nothing)
Definition: token.h:831
void evaluate(Pkt &pkt, ValueStack &values)
Extract a field from a delimited string.
Definition: token.cc:836
The order where Token subtypes are declared should be:
Definition: token.h:113
std::string value_
Constant value.
Definition: token.h:127
TokenString(const std::string &str)
Value is set during token construction.
Definition: token.h:118
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack)
Definition: token.cc:40
Token that represents sub-options in DHCPv4 and DHCPv6.
Definition: token.h:1275
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Definition: token.cc:1351
uint16_t sub_option_code_
Code of the sub-option to be extracted.
Definition: token.h:1323
uint16_t getSubCode() const
Returns sub-option-code.
Definition: token.h:1312
TokenSubOption(const uint16_t option_code, const uint16_t sub_option_code, const RepresentationType &rep_type)
Constructor that takes an option and sub-option codes as parameter.
Definition: token.h:1288
virtual OptionPtr getSubOption(const OptionPtr &parent)
Attempts to retrieve a sub-option.
Definition: token.cc:1343
Token that represents the substring operator (returns a portion of the supplied string)
Definition: token.h:776
void evaluate(Pkt &pkt, ValueStack &values)
Extract a substring from a string.
Definition: token.cc:738
TokenSubstring()
Constructor (does nothing)
Definition: token.h:779
Token that converts to hexadecimal string.
Definition: token.h:935
TokenToHexString()
Constructor (does nothing)
Definition: token.h:938
void evaluate(Pkt &pkt, ValueStack &values)
Convert a binary value to its hexadecimal string representation.
Definition: token.cc:982
Token representing a 16 bit unsigned integer as a string.
Definition: token.h:334
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:311
TokenUInt16ToText()
Constructor (does nothing)
Definition: token.h:337
Token representing a 32 bit unsigned integer as a string.
Definition: token.h:352
TokenUInt32ToText()
Constructor (does nothing)
Definition: token.h:355
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:342
Token representing an 8 bit unsigned integer as a string.
Definition: token.h:316
TokenUInt8ToText()
Constructor (does nothing)
Definition: token.h:319
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the string on the stack after decoding)
Definition: token.cc:281
Token representing a constant upper case string.
Definition: token.h:176
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the evaluated string expression converted to upper case on the stack)
Definition: token.cc:109
TokenUpperCase()
Constructor (does nothing)
Definition: token.h:179
Token that represents vendor class options in DHCPv4 and DHCPv6.
Definition: token.h:1203
uint16_t getDataIndex() const
Returns data index.
Definition: token.cc:1236
uint16_t index_
Data chunk index.
Definition: token.h:1256
void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Definition: token.cc:1240
Token that represents vendor options in DHCPv4 and DHCPv6.
Definition: token.h:1089
FieldType
Specifies a field of the vendor option.
Definition: token.h:1093
@ DATA
data chunk, used in derived vendor-class only
Definition: token.h:1097
@ EXISTS
vendor[123].exists
Definition: token.h:1096
@ ENTERPRISE_ID
enterprise-id field (vendor-info, vendor-class)
Definition: token.h:1095
@ SUBOPTION
If this token fetches a suboption, not a field.
Definition: token.h:1094
Option::Universe universe_
Universe (V4 or V6)
Definition: token.h:1174
uint32_t vendor_id_
Enterprise-id value.
Definition: token.h:1180
FieldType field_
Specifies which field should be accessed.
Definition: token.h:1183
uint32_t getVendorId() const
Returns enterprise-id.
Definition: token.cc:1123
virtual OptionPtr getOption(Pkt &pkt)
Attempts to get a suboption.
Definition: token.cc:1203
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Definition: token.cc:1131
FieldType getField() const
Returns field.
Definition: token.cc:1127
Base class for all tokens.
Definition: token.h:62
virtual void evaluate(Pkt &pkt, ValueStack &values)=0
This is a generic method for evaluating a packet.
virtual ~Token()
Virtual destructor.
Definition: token.h:80
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
Definition: token.h:89
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
boost::shared_ptr< Token > TokenPtr
Pointer to a single Token.
Definition: token.h:20
boost::shared_ptr< Expression > ExpressionPtr
Definition: token.h:30
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition: token.h:28
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
Definition: token.h:33
Defines the logger used by the top-level component of kea-lfc.