Kea  2.1.7-git
token.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2022 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 
10 #include <exceptions/exceptions.h>
11 #include <dhcp/pkt.h>
12 #include <stack>
13 
14 namespace isc {
15 namespace dhcp {
16 
17 class Token;
18 
20 typedef boost::shared_ptr<Token> TokenPtr;
21 
28 typedef std::vector<TokenPtr> Expression;
29 
30 typedef boost::shared_ptr<Expression> ExpressionPtr;
31 
33 typedef std::stack<std::string> ValueStack;
34 
37 class EvalBadStack : public Exception {
38 public:
39  EvalBadStack(const char* file, size_t line, const char* what) :
40  isc::Exception(file, line, what) { };
41 };
42 
45 class EvalTypeError : public Exception {
46 public:
47  EvalTypeError(const char* file, size_t line, const char* what) :
48  isc::Exception(file, line, what) { };
49 };
50 
51 
63 class Token {
64 public:
65 
78  virtual void evaluate(Pkt& pkt, ValueStack& values) = 0;
79 
81  virtual ~Token() {}
82 
90  static inline bool toBool(std::string value) {
91  if (value == "true") {
92  return (true);
93  } else if (value == "false") {
94  return (false);
95  } else {
96  isc_throw(EvalTypeError, "Incorrect boolean. Expected exactly "
97  "\"false\" or \"true\", got \"" << value << "\"");
98  }
99  }
100 };
101 
109 
114 class TokenString : public Token {
115 public:
119  TokenString(const std::string& str)
120  :value_(str){
121  }
122 
127  void evaluate(Pkt& pkt, ValueStack& values);
128 
129 protected:
130  std::string value_;
131 };
132 
137 class TokenHexString : public Token {
138 public:
144  TokenHexString(const std::string& str);
145 
152  void evaluate(Pkt& pkt, ValueStack& values);
153 
154 protected:
155  std::string value_;
156 };
157 
164 class TokenInteger : public TokenString {
165 public:
172  TokenInteger(const uint32_t value);
173 
179  uint32_t getInteger() const {
180  return (int_value_);
181  }
182 
183 protected:
184  uint32_t int_value_;
185 };
186 
191 class TokenIpAddress : public Token {
192 public:
196  TokenIpAddress(const std::string& addr);
197 
203  void evaluate(Pkt& pkt, ValueStack& values);
204 
205 protected:
207  std::string value_;
208 };
209 
214 class TokenIpAddressToText : public Token {
215 public:
218 
224  void evaluate(Pkt& pkt, ValueStack& values);
225 };
226 
231 class TokenInt8ToText : public Token {
232 public:
235 
242  void evaluate(Pkt& pkt, ValueStack& values);
243 };
244 
249 class TokenInt16ToText : public Token {
250 public:
253 
260  void evaluate(Pkt& pkt, ValueStack& values);
261 };
262 
267 class TokenInt32ToText : public Token {
268 public:
271 
278  void evaluate(Pkt& pkt, ValueStack& values);
279 };
280 
285 class TokenUInt8ToText : public Token {
286 public:
289 
296  void evaluate(Pkt& pkt, ValueStack& values);
297 };
298 
303 class TokenUInt16ToText : public Token {
304 public:
307 
314  void evaluate(Pkt& pkt, ValueStack& values);
315 };
316 
321 class TokenUInt32ToText : public Token {
322 public:
325 
332  void evaluate(Pkt& pkt, ValueStack& values);
333 };
334 
344 class TokenOption : public Token {
345 public:
346 
357  EXISTS
358  };
359 
367  TokenOption(const uint16_t option_code, const RepresentationType& rep_type)
368  : option_code_(option_code), representation_type_(rep_type) {}
369 
378  void evaluate(Pkt& pkt, ValueStack& values);
379 
386  uint16_t getCode() const {
387  return (option_code_);
388  }
389 
397  return (representation_type_);
398  }
399 
400 protected:
410  virtual OptionPtr getOption(Pkt& pkt);
411 
418  virtual std::string pushFailure(ValueStack& values);
419 
420  uint16_t option_code_;
422 };
423 
436 public:
437 
442  TokenRelay4Option(const uint16_t option_code,
443  const RepresentationType& rep_type);
444 
445 protected:
449  virtual OptionPtr getOption(Pkt& pkt);
450 };
451 
468 public:
475  TokenRelay6Option(const int8_t nest_level, const uint16_t option_code,
476  const RepresentationType& rep_type)
477  :TokenOption(option_code, rep_type), nest_level_(nest_level) {}
478 
486  int8_t getNest() const {
487  return (nest_level_);
488  }
489 
490 protected:
494  virtual OptionPtr getOption(Pkt& pkt);
495 
496  int8_t nest_level_;
497 };
498 
509 class TokenPkt : public Token {
510 public:
511 
515  SRC,
516  DST,
517  LEN
518  };
519 
521  TokenPkt(const MetadataType type)
522  : type_(type) {}
523 
531  void evaluate(Pkt& pkt, ValueStack& values);
532 
538  return (type_);
539  }
540 
541 private:
543  MetadataType type_;
544 };
545 
559 class TokenPkt4 : public Token {
560 public:
561 
563  enum FieldType {
573  };
574 
576  TokenPkt4(const FieldType type)
577  : type_(type) {}
578 
588  void evaluate(Pkt& pkt, ValueStack& values);
589 
595  return (type_);
596  }
597 
598 private:
600  FieldType type_;
601 };
602 
613 class TokenPkt6 : public Token {
614 public:
616  enum FieldType {
618  TRANSID
619  };
620 
622  TokenPkt6(const FieldType type)
623  : type_(type) {}
624 
634  void evaluate(Pkt& pkt, ValueStack& values);
635 
641  return(type_);
642  }
643 
644 private:
646  FieldType type_;
647 };
648 
664 class TokenRelay6Field : public Token {
665 public:
666 
668  enum FieldType {
670  LINKADDR
671  };
672 
678  TokenRelay6Field(const int8_t nest_level, const FieldType type)
679  : nest_level_(nest_level), type_(type) {}
680 
688  void evaluate(Pkt& pkt, ValueStack& values);
689 
697  int8_t getNest() const {
698  return (nest_level_);
699  }
700 
708  return (type_);
709  }
710 
711 protected:
713  int8_t nest_level_;
715 };
716 
721 class TokenEqual : public Token {
722 public:
725 
738  void evaluate(Pkt& pkt, ValueStack& values);
739 };
740 
746 class TokenSubstring : public Token {
747 public:
750 
795  void evaluate(Pkt& pkt, ValueStack& values);
796 };
797 
798 class TokenSplit : public Token {
799 public:
802 
839  void evaluate(Pkt& pkt, ValueStack& values);
840 };
841 
847 class TokenConcat : public Token {
848 public:
851 
863  void evaluate(Pkt& pkt, ValueStack& values);
864 };
865 
875 class TokenIfElse : public Token {
876 public:
879 
894  void evaluate(Pkt& pkt, ValueStack& values);
895 };
896 
905 class TokenToHexString : public Token {
906 public:
909 
933  void evaluate(Pkt& pkt, ValueStack& values);
934 };
935 
940 class TokenNot : public Token {
941 public:
943  TokenNot() {}
944 
958  void evaluate(Pkt& pkt, ValueStack& values);
959 };
960 
964 class TokenAnd : public Token {
965 public:
967  TokenAnd() {}
968 
983  void evaluate(Pkt& pkt, ValueStack& values);
984 };
985 
989 class TokenOr : public Token {
990 public:
992  TokenOr() {}
993 
1008  void evaluate(Pkt& pkt, ValueStack& values);
1009 };
1010 
1014 class TokenMember : public Token {
1015 public:
1019  TokenMember(const std::string& client_class)
1020  :client_class_(client_class){
1021  }
1022 
1028  void evaluate(Pkt& pkt, ValueStack& values);
1029 
1036  const ClientClass& getClientClass() const {
1037  return (client_class_);
1038  }
1039 
1040 protected:
1043 };
1044 
1059 class TokenVendor : public TokenOption {
1060 public:
1061 
1063  enum FieldType {
1067  DATA
1068  };
1069 
1075  TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field);
1076 
1077 
1089  TokenVendor(Option::Universe u, uint32_t vendor_id, RepresentationType repr,
1090  uint16_t option_code = 0);
1091 
1097  uint32_t getVendorId() const;
1098 
1104  FieldType getField() const;
1105 
1128  virtual void evaluate(Pkt& pkt, ValueStack& values);
1129 
1130 protected:
1139  virtual OptionPtr getOption(Pkt& pkt);
1140 
1146 
1151  uint32_t vendor_id_;
1152 
1155 };
1156 
1175 public:
1176 
1182  TokenVendorClass(Option::Universe u, uint32_t vendor_id, RepresentationType repr);
1183 
1190  TokenVendorClass(Option::Universe u, uint32_t vendor_id, FieldType field,
1191  uint16_t index = 0);
1192 
1197  uint16_t getDataIndex() const;
1198 
1199 protected:
1200 
1224  void evaluate(Pkt& pkt, ValueStack& values);
1225 
1227  uint16_t index_;
1228 };
1229 
1246 class TokenSubOption : public virtual TokenOption {
1247 public:
1248 
1251 
1259  TokenSubOption(const uint16_t option_code,
1260  const uint16_t sub_option_code,
1261  const RepresentationType& rep_type)
1262  : TokenOption(option_code, rep_type), sub_option_code_(sub_option_code) {}
1263 
1275  virtual void evaluate(Pkt& pkt, ValueStack& values);
1276 
1283  uint16_t getSubCode() const {
1284  return (sub_option_code_);
1285  }
1286 
1287 protected:
1292  virtual OptionPtr getSubOption(const OptionPtr& parent);
1293 
1294  uint16_t sub_option_code_;
1295 };
1296 
1297 } // end of isc::dhcp namespace
1298 } // end of isc namespace
1299 
1300 #endif
TokenConcat()
Constructor (does nothing)
Definition: token.h:850
uint32_t vendor_id_
Enterprise-id value.
Definition: token.h:1151
Token representing a 16 bit unsigned integer as a string.
Definition: token.h:303
interface name (string)
Definition: token.h:514
Token that represents fields of DHCPv6 packet.
Definition: token.h:613
TokenMember(const std::string &client_class)
Constructor.
Definition: token.h:1019
giaddr (IPv4 address)
Definition: token.h:565
uint16_t sub_option_code_
Code of the sub-option to be extracted.
Definition: token.h:1294
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:475
int8_t getNest() const
Returns nest-level.
Definition: token.h:697
The order where Token subtypes are declared should be:
Definition: token.h:114
Token representing a 32 bit integer as a string.
Definition: token.h:267
MetadataType getType()
Returns metadata type.
Definition: token.h:537
Token that represents meta data of a DHCP packet.
Definition: token.h:509
std::string value_
< Constant value (empty string if the IP address cannot be converted)
Definition: token.h:207
TokenEqual()
Constructor (does nothing)
Definition: token.h:724
Token that represents concat operator (concatenates two other tokens)
Definition: token.h:847
std::string value_
Constant value.
Definition: token.h:130
uint16_t option_code_
Code of the option to be extracted.
Definition: token.h:420
If this token fetches a suboption, not a field.
Definition: token.h:1064
FieldType type_
field to get
Definition: token.h:714
FieldType
enum value that determines the field.
Definition: token.h:563
destination (IP address)
Definition: token.h:516
Base class for classes representing DHCP messages.
Definition: pkt.h:90
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
TokenIpAddressToText()
Constructor (does nothing)
Definition: token.h:217
Token that converts to hexadecimal string.
Definition: token.h:905
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
FieldType
Specifies a field of the vendor option.
Definition: token.h:1063
FieldType
enum value that determines the field.
Definition: token.h:668
Token that represents logical negation operator.
Definition: token.h:940
FieldType getType()
Returns field type.
Definition: token.h:594
ClientClass client_class_
The client class name.
Definition: token.h:1042
TokenInt16ToText()
Constructor (does nothing)
Definition: token.h:252
Token representing an 8 bit unsigned integer as a string.
Definition: token.h:285
Token that represents an alternative.
Definition: token.h:875
transaction-id (xid)
Definition: token.h:572
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
TokenPkt(const MetadataType type)
Constructor (does nothing)
Definition: token.h:521
Token representing a constant string in hexadecimal format.
Definition: token.h:137
TokenPkt4(const FieldType type)
Constructor (does nothing)
Definition: token.h:576
MetadataType
enum value that determines the field.
Definition: token.h:513
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:1259
Token that represents logical or operator.
Definition: token.h:989
Token that represents a value of a field within a DHCPv6 relay encapsulation.
Definition: token.h:664
Token that represents sub-options in DHCPv4 and DHCPv6.
Definition: token.h:1246
uint16_t getCode() const
Returns option-code.
Definition: token.h:386
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
TokenUInt16ToText()
Constructor (does nothing)
Definition: token.h:306
Base class for all tokens.
Definition: token.h:63
boost::shared_ptr< Token > TokenPtr
Pointer to a single Token.
Definition: token.h:17
ciaddr (IPv4 address)
Definition: token.h:566
vendor[123].exists
Definition: token.h:1066
Option::Universe universe_
Universe (V4 or V6)
Definition: token.h:1145
Token that represents logical and operator.
Definition: token.h:964
TokenUInt8ToText()
Constructor (does nothing)
Definition: token.h:288
TokenOption(const uint16_t option_code, const RepresentationType &rep_type)
Constructor that takes an option code as a parameter.
Definition: token.h:367
TokenUInt32ToText()
Constructor (does nothing)
Definition: token.h:324
uint32_t int_value_
value as integer (stored for testing only)
Definition: token.h:184
enterprise-id field (vendor-info, vendor-class)
Definition: token.h:1065
TokenOr()
Constructor (does nothing)
Definition: token.h:992
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
Definition: token.h:33
std::string value_
Constant value.
Definition: token.h:155
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
Definition: token.h:90
RepresentationType getRepresentation() const
Returns representation-type.
Definition: token.h:396
EvalBadStack(const char *file, size_t line, const char *what)
Definition: token.h:39
chaddr field (up to 16 bytes link-layer address)
Definition: token.h:564
Represents a sub-option inserted by the DHCPv4 relay.
Definition: token.h:435
Token that represents a value of an option.
Definition: token.h:344
Token representing an IP address as a constant string.
Definition: token.h:191
Token representing an IP address as a string.
Definition: token.h:214
htype (hardware address type)
Definition: token.h:570
virtual ~Token()
Virtual destructor.
Definition: token.h:81
uint16_t getSubCode() const
Returns sub-option-code.
Definition: token.h:1283
TokenNot()
Constructor (does nothing)
Definition: token.h:943
TokenSubstring()
Constructor (does nothing)
Definition: token.h:749
message type (not really a field, content of option 53)
Definition: token.h:571
TokenSplit()
Constructor (does nothing)
Definition: token.h:801
Token that represents vendor class options in DHCPv4 and DHCPv6.
Definition: token.h:1174
TokenInt32ToText()
Constructor (does nothing)
Definition: token.h:270
uint32_t getInteger() const
Returns integer value.
Definition: token.h:179
Token that represents fields of a DHCPv4 packet.
Definition: token.h:559
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition: token.h:28
TokenInt8ToText()
Constructor (does nothing)
Definition: token.h:234
TokenIfElse()
Constructor (does nothing)
Definition: token.h:878
EvalTypeError(const char *file, size_t line, const char *what)
Definition: token.h:47
Token that represents equality operator (compares two other tokens)
Definition: token.h:721
RepresentationType
Token representation type.
Definition: token.h:354
TokenAnd()
Constructor (does nothing)
Definition: token.h:967
EvalBadStack is thrown when more or less parameters are on the stack than expected.
Definition: token.h:37
TokenToHexString()
Constructor (does nothing)
Definition: token.h:908
yiaddr (IPv4 address)
Definition: token.h:567
TokenPkt6(const FieldType type)
Constructor (does nothing)
Definition: token.h:622
TokenString(const std::string &str)
Value is set during token construction.
Definition: token.h:119
Token that represents the substring operator (returns a portion of the supplied string) ...
Definition: token.h:746
Peer address field (IPv6 address)
Definition: token.h:669
const ClientClass & getClientClass() const
Returns client class name.
Definition: token.h:1036
RepresentationType representation_type_
Representation type.
Definition: token.h:421
Token representing a 32 bit unsigned integer as a string.
Definition: token.h:321
FieldType
enum value that determines the field.
Definition: token.h:616
siaddr (IPv4 address)
Definition: token.h:568
FieldType getType()
Returns field type.
Definition: token.h:640
EvalTypeError is thrown when a value on the stack has a content with an unexpected type...
Definition: token.h:45
uint16_t index_
Data chunk index.
Definition: token.h:1227
FieldType getType()
Returns field type.
Definition: token.h:707
std::string ClientClass
Defines a single class name.
Definition: classify.h:40
source (IP address)
Definition: token.h:515
int8_t nest_level_
nesting level of the relay block to use
Definition: token.h:496
TokenRelay6Field(const int8_t nest_level, const FieldType type)
Constructor that takes a nesting level and field type as parameters.
Definition: token.h:678
Token representing a 16 bit integer as a string.
Definition: token.h:249
int8_t nest_level_
Specifies field of the DHCPv6 relay option to get.
Definition: token.h:713
hlen (hardware address length)
Definition: token.h:569
int8_t getNest() const
Returns nest-level.
Definition: token.h:486
boost::shared_ptr< Expression > ExpressionPtr
Definition: token.h:30
Token representing an 8 bit integer as a string.
Definition: token.h:231
Token that represents a value of an option within a DHCPv6 relay encapsulation.
Definition: token.h:467
Token that represents client class membership.
Definition: token.h:1014
Token that represents vendor options in DHCPv4 and DHCPv6.
Definition: token.h:1059
Token representing an unsigned 32 bit integer.
Definition: token.h:164
FieldType field_
Specifies which field should be accessed.
Definition: token.h:1154