Kea 1.5.0
token.h
Go to the documentation of this file.
1// Copyright (C) 2015-2018 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
51
63class Token {
64public:
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
114class TokenString : public Token {
115public:
119 TokenString(const std::string& str)
120 :value_(str){
121 }
122
127 void evaluate(Pkt& pkt, ValueStack& values);
128
129protected:
130 std::string value_;
131};
132
137class TokenHexString : public Token {
138public:
144 TokenHexString(const std::string& str);
145
152 void evaluate(Pkt& pkt, ValueStack& values);
153
154protected:
155 std::string value_;
156};
157
164class TokenInteger : public TokenString {
165public:
172 TokenInteger(const uint32_t value);
173
179 uint32_t getInteger() const {
180 return (int_value_);
181 }
182
183protected:
184 uint32_t int_value_;
185};
186
191class TokenIpAddress : public Token {
192public:
196 TokenIpAddress(const std::string& addr);
197
203 void evaluate(Pkt& pkt, ValueStack& values);
204
205protected:
207 std::string value_;
208};
209
219class TokenOption : public Token {
220public:
221
232 EXISTS
233 };
234
243 TokenOption(const uint16_t option_code, const RepresentationType& rep_type)
244 : option_code_(option_code), representation_type_(rep_type) {}
245
254 void evaluate(Pkt& pkt, ValueStack& values);
255
262 uint16_t getCode() const {
263 return (option_code_);
264 }
265
273 return (representation_type_);
274 }
275
276protected:
286 virtual OptionPtr getOption(Pkt& pkt);
287
294 virtual std::string pushFailure(ValueStack& values);
295
296 uint16_t option_code_;
298};
299
312public:
313
318 TokenRelay4Option(const uint16_t option_code,
319 const RepresentationType& rep_type);
320
321protected:
325 virtual OptionPtr getOption(Pkt& pkt);
326};
327
344public:
351 TokenRelay6Option(const int8_t nest_level, const uint16_t option_code,
352 const RepresentationType& rep_type)
353 :TokenOption(option_code, rep_type), nest_level_(nest_level) {}
354
362 int8_t getNest() const {
363 return (nest_level_);
364 }
365
366protected:
370 virtual OptionPtr getOption(Pkt& pkt);
371
372 int8_t nest_level_;
373};
374
385class TokenPkt : public Token {
386public:
387
393 LEN
394 };
395
398 : type_(type) {}
399
407 void evaluate(Pkt& pkt, ValueStack& values);
408
414 return (type_);
415 }
416
417private:
419 MetadataType type_;
420};
421
435class TokenPkt4 : public Token {
436public:
437
449 };
450
453 : type_(type) {}
454
464 void evaluate(Pkt& pkt, ValueStack& values);
465
471 return (type_);
472 }
473
474private:
476 FieldType type_;
477};
478
489class TokenPkt6 : public Token {
490public:
494 TRANSID
495 };
496
499 : type_(type) {}
500
510 void evaluate(Pkt& pkt, ValueStack& values);
511
517 return(type_);
518 }
519
520private:
522 FieldType type_;
523};
524
540class TokenRelay6Field : public Token {
541public:
542
546 LINKADDR
547 };
548
554 TokenRelay6Field(const int8_t nest_level, const FieldType type)
555 : nest_level_(nest_level), type_(type) {}
556
564 void evaluate(Pkt& pkt, ValueStack& values);
565
573 int8_t getNest() const {
574 return (nest_level_);
575 }
576
584 return (type_);
585 }
586
587protected:
589 int8_t nest_level_;
591};
592
597class TokenEqual : public Token {
598public:
601
614 void evaluate(Pkt& pkt, ValueStack& values);
615};
616
622class TokenSubstring : public Token {
623public:
626
671 void evaluate(Pkt& pkt, ValueStack& values);
672};
673
678class TokenConcat : public Token {
679public:
682
694 void evaluate(Pkt& pkt, ValueStack& values);
695};
696
706class TokenIfElse : public Token {
707public:
710
725 void evaluate(Pkt& pkt, ValueStack& values);
726};
727
736class TokenToHexString : public Token {
737public:
740
764 void evaluate(Pkt& pkt, ValueStack& values);
765};
766
771class TokenNot : public Token {
772public:
775
789 void evaluate(Pkt& pkt, ValueStack& values);
790};
791
795class TokenAnd : public Token {
796public:
799
814 void evaluate(Pkt& pkt, ValueStack& values);
815};
816
820class TokenOr : public Token {
821public:
824
839 void evaluate(Pkt& pkt, ValueStack& values);
840};
841
845class TokenMember : public Token {
846public:
850 TokenMember(const std::string& client_class)
851 :client_class_(client_class){
852 }
853
859 void evaluate(Pkt& pkt, ValueStack& values);
860
868 return (client_class_);
869 }
870
871protected:
874};
875
890class TokenVendor : public TokenOption {
891public:
892
898 DATA
899 };
900
906 TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field);
907
908
920 TokenVendor(Option::Universe u, uint32_t vendor_id, RepresentationType repr,
921 uint16_t option_code = 0);
922
928 uint32_t getVendorId() const;
929
935 FieldType getField() const;
936
959 virtual void evaluate(Pkt& pkt, ValueStack& values);
960
961protected:
970 virtual OptionPtr getOption(Pkt& pkt);
971
977
982 uint32_t vendor_id_;
983
986};
987
1006public:
1007
1013 TokenVendorClass(Option::Universe u, uint32_t vendor_id, RepresentationType repr);
1014
1021 TokenVendorClass(Option::Universe u, uint32_t vendor_id, FieldType field,
1022 uint16_t index = 0);
1023
1028 uint16_t getDataIndex() const;
1029
1030protected:
1031
1055 void evaluate(Pkt& pkt, ValueStack& values);
1056
1058 uint16_t index_;
1059};
1060
1061}; // end of isc::dhcp namespace
1062}; // end of isc namespace
1063
1064#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:67
Base class for classes representing DHCP messages.
Definition: pkt.h:90
Token that represents logical and operator.
Definition: token.h:795
void evaluate(Pkt &pkt, ValueStack &values)
Logical and.
Definition: token.cc:691
TokenAnd()
Constructor (does nothing)
Definition: token.h:798
Token that represents concat operator (concatenates two other tokens)
Definition: token.h:678
void evaluate(Pkt &pkt, ValueStack &values)
Concatenate two values.
Definition: token.cc:576
TokenConcat()
Constructor (does nothing)
Definition: token.h:681
Token that represents equality operator (compares two other tokens)
Definition: token.h:597
void evaluate(Pkt &pkt, ValueStack &values)
Compare two values.
Definition: token.cc:455
TokenEqual()
Constructor (does nothing)
Definition: token.h:600
Token representing a constant string in hexadecimal format.
Definition: token.h:137
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:72
std::string value_
Constant value.
Definition: token.h:155
Token that represents an alternative.
Definition: token.h:706
TokenIfElse()
Constructor (does nothing)
Definition: token.h:709
void evaluate(Pkt &pkt, ValueStack &values)
Alternative.
Definition: token.cc:599
Token representing an unsigned 32 bit integer.
Definition: token.h:164
uint32_t getInteger() const
Returns integer value.
Definition: token.h:179
uint32_t int_value_
value as integer (stored for testing only)
Definition: token.h:184
Token representing an IP address as a constant string.
Definition: token.h:191
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack after decoding)
Definition: token.cc:97
std::string value_
< Constant value (empty string if the IP address cannot be converted)
Definition: token.h:207
Token that represents client class membership.
Definition: token.h:845
const ClientClass & getClientClass() const
Returns client class name.
Definition: token.h:867
ClientClass client_class_
The client class name.
Definition: token.h:873
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (check if client_class_ was added to packet client classes)
Definition: token.cc:747
TokenMember(const std::string &client_class)
Constructor.
Definition: token.h:850
Token that represents logical negation operator.
Definition: token.h:771
TokenNot()
Constructor (does nothing)
Definition: token.h:774
void evaluate(Pkt &pkt, ValueStack &values)
Logical negation.
Definition: token.cc:668
Token that represents a value of an option.
Definition: token.h:219
uint16_t getCode() const
Returns option-code.
Definition: token.h:262
RepresentationType getRepresentation() const
Returns representation-type.
Definition: token.h:272
virtual OptionPtr getOption(Pkt &pkt)
Attempts to retrieve an option.
Definition: token.cc:107
void evaluate(Pkt &pkt, ValueStack &values)
Evaluates the values of the option.
Definition: token.cc:112
RepresentationType representation_type_
Representation type.
Definition: token.h:297
uint16_t option_code_
Code of the option to be extracted.
Definition: token.h:296
RepresentationType
Token representation type.
Definition: token.h:229
TokenOption(const uint16_t option_code, const RepresentationType &rep_type)
Constructor that takes an option code as a parameter.
Definition: token.h:243
virtual std::string pushFailure(ValueStack &values)
Auxiliary method that puts string representing a failure.
Definition: token.cc:150
Token that represents logical or operator.
Definition: token.h:820
void evaluate(Pkt &pkt, ValueStack &values)
Logical or.
Definition: token.cc:719
TokenOr()
Constructor (does nothing)
Definition: token.h:823
Token that represents fields of a DHCPv4 packet.
Definition: token.h:435
FieldType getType()
Returns field type.
Definition: token.h:470
FieldType
enum value that determines the field.
Definition: token.h:439
@ CIADDR
ciaddr (IPv4 address)
Definition: token.h:442
@ HLEN
hlen (hardware address length)
Definition: token.h:445
@ HTYPE
htype (hardware address type)
Definition: token.h:446
@ GIADDR
giaddr (IPv4 address)
Definition: token.h:441
@ CHADDR
chaddr field (up to 16 bytes link-layer address)
Definition: token.h:440
@ YIADDR
yiaddr (IPv4 address)
Definition: token.h:443
@ SIADDR
siaddr (IPv4 address)
Definition: token.h:444
@ TRANSID
transaction-id (xid)
Definition: token.h:448
@ MSGTYPE
message type (not really a field, content of option 53)
Definition: token.h:447
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
Definition: token.cc:265
TokenPkt4(const FieldType type)
Constructor (does nothing)
Definition: token.h:452
Token that represents fields of DHCPv6 packet.
Definition: token.h:489
TokenPkt6(const FieldType type)
Constructor (does nothing)
Definition: token.h:498
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value of the specified packet.
Definition: token.cc:351
FieldType getType()
Returns field type.
Definition: token.h:516
FieldType
enum value that determines the field.
Definition: token.h:492
@ TRANSID
transaction id (integer but manipulated as a string)
Definition: token.h:494
@ MSGTYPE
msg type
Definition: token.h:493
Token that represents meta data of a DHCP packet.
Definition: token.h:385
MetadataType
enum value that determines the field.
Definition: token.h:389
@ LEN
length (4 octets)
Definition: token.h:393
@ DST
destination (IP address)
Definition: token.h:392
@ IFACE
interface name (string)
Definition: token.h:390
@ SRC
source (IP address)
Definition: token.h:391
MetadataType getType()
Returns metadata type.
Definition: token.h:413
void evaluate(Pkt &pkt, ValueStack &values)
Gets a value from the specified packet.
Definition: token.cc:213
TokenPkt(const MetadataType type)
Constructor (does nothing)
Definition: token.h:397
Represents a sub-option inserted by the DHCPv4 relay.
Definition: token.h:311
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified sub-option of option 82 from the packet.
Definition: token.cc:164
Token that represents a value of a field within a DHCPv6 relay encapsulation.
Definition: token.h:540
FieldType type_
field to get
Definition: token.h:590
void evaluate(Pkt &pkt, ValueStack &values)
Extracts the specified field from the requested relay.
Definition: token.cc:392
FieldType
enum value that determines the field.
Definition: token.h:544
@ LINKADDR
Link address field (IPv6 address)
Definition: token.h:546
@ PEERADDR
Peer address field (IPv6 address)
Definition: token.h:545
int8_t nest_level_
Specifies field of the DHCPv6 relay option to get.
Definition: token.h:589
FieldType getType()
Returns field type.
Definition: token.h:583
TokenRelay6Field(const int8_t nest_level, const FieldType type)
Constructor that takes a nesting level and field type as parameters.
Definition: token.h:554
int8_t getNest() const
Returns nest-level.
Definition: token.h:573
Token that represents a value of an option within a DHCPv6 relay encapsulation.
Definition: token.h:343
int8_t nest_level_
nesting level of the relay block to use
Definition: token.h:372
int8_t getNest() const
Returns nest-level.
Definition: token.h:362
virtual OptionPtr getOption(Pkt &pkt)
Attempts to obtain specified option from the specified relay block.
Definition: token.cc:176
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:351
The order where Token subtypes are declared should be:
Definition: token.h:114
std::string value_
Constant value.
Definition: token.h:130
TokenString(const std::string &str)
Value is set during token construction.
Definition: token.h:119
void evaluate(Pkt &pkt, ValueStack &values)
Token evaluation (puts value of the constant string on the stack)
Definition: token.cc:34
Token that represents the substring operator (returns a portion of the supplied string)
Definition: token.h:622
void evaluate(Pkt &pkt, ValueStack &values)
Extract a substring from a string.
Definition: token.cc:480
TokenSubstring()
Constructor (does nothing)
Definition: token.h:625
Token that converts to hexadecimal string.
Definition: token.h:736
TokenToHexString()
Constructor (does nothing)
Definition: token.h:739
void evaluate(Pkt &pkt, ValueStack &values)
Convert a binary value to its hexadecimal string representation.
Definition: token.cc:635
Token that represents vendor class options in DHCPv4 and DHCPv6.
Definition: token.h:1005
uint16_t getDataIndex() const
Returns data index.
Definition: token.cc:887
uint16_t index_
Data chunk index.
Definition: token.h:1058
void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Definition: token.cc:891
Token that represents vendor options in DHCPv4 and DHCPv6.
Definition: token.h:890
FieldType
Specifies a field of the vendor option.
Definition: token.h:894
@ DATA
data chunk, used in derived vendor-class only
Definition: token.h:898
@ EXISTS
vendor[123].exists
Definition: token.h:897
@ ENTERPRISE_ID
enterprise-id field (vendor-info, vendor-class)
Definition: token.h:896
@ SUBOPTION
If this token fetches a suboption, not a field.
Definition: token.h:895
Option::Universe universe_
Universe (V4 or V6)
Definition: token.h:976
uint32_t vendor_id_
Enterprise-id value.
Definition: token.h:982
FieldType field_
Specifies which field should be accessed.
Definition: token.h:985
uint32_t getVendorId() const
Returns enterprise-id.
Definition: token.cc:776
virtual OptionPtr getOption(Pkt &pkt)
Attempts to get a suboption.
Definition: token.cc:853
virtual void evaluate(Pkt &pkt, ValueStack &values)
This is a method for evaluating a packet.
Definition: token.cc:784
FieldType getField() const
Returns field.
Definition: token.cc:780
Base class for all tokens.
Definition: token.h:63
virtual void evaluate(Pkt &pkt, ValueStack &values)=0
This is a generic method for evaluating a packet.
virtual ~Token()
Virtual destructor.
Definition: token.h:81
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
Definition: token.h:90
#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:37
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:38
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-dhcp-ddns.