token.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef TOKEN_H
  15. #define TOKEN_H
  16. #include <exceptions/exceptions.h>
  17. #include <dhcp/pkt.h>
  18. #include <stack>
  19. namespace isc {
  20. namespace dhcp {
  21. class Token;
  22. /// @brief Pointer to a single Token
  23. typedef boost::shared_ptr<Token> TokenPtr;
  24. /// This is a structure that holds an expression converted to RPN
  25. ///
  26. /// For example expression: option[123] == 'foo' will be converted to:
  27. /// [0] = option[123] (TokenOption object)
  28. /// [1] = 'foo' (TokenString object)
  29. /// [2] = == operator (TokenEqual object)
  30. typedef std::vector<TokenPtr> Expression;
  31. /// Evaluated values are stored as a stack of strings
  32. typedef std::stack<std::string> ValueStack;
  33. /// @brief EvalBadStack is thrown when more or less parameters are on the
  34. /// stack than expected.
  35. class EvalBadStack : public Exception {
  36. public:
  37. EvalBadStack(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) { };
  39. };
  40. /// @brief EvalTypeError is thrown when a value on the stack has a content
  41. /// with an unexpected type.
  42. class EvalTypeError : public Exception {
  43. public:
  44. EvalTypeError(const char* file, size_t line, const char* what) :
  45. isc::Exception(file, line, what) { };
  46. };
  47. /// @brief Base class for all tokens
  48. ///
  49. /// It provides an interface for all tokens and storage for string representation
  50. /// (all tokens evaluate to string).
  51. ///
  52. /// This class represents a single token. Examples of a token are:
  53. /// - "foo" (a constant string)
  54. /// - option[123] (a token that extracts value of option 123)
  55. /// - == (an operator that compares two other tokens)
  56. /// - substring(a,b,c) (an operator that takes three arguments: a string,
  57. /// first character and length)
  58. class Token {
  59. public:
  60. /// @brief This is a generic method for evaluating a packet.
  61. ///
  62. /// We need to pass the packet being evaluated and possibly previously
  63. /// evaluated values. Specific implementations may ignore the packet altogether
  64. /// and just put their own value on the stack (constant tokens), look at the
  65. /// packet and put some data extracted from it on the stack (option tokens),
  66. /// or pop arguments from the stack and put back the result (operators).
  67. ///
  68. /// The parameters passed will be:
  69. ///
  70. /// @param pkt - packet being classified
  71. /// @param values - stack of values with previously evaluated tokens
  72. virtual void evaluate(const Pkt& pkt, ValueStack& values) = 0;
  73. /// @brief Virtual destructor
  74. virtual ~Token() {}
  75. };
  76. /// @brief Token representing a constant string
  77. ///
  78. /// This token holds value of a constant string, e.g. it represents
  79. /// "MSFT" in expression option[vendor-class] == "MSFT"
  80. class TokenString : public Token {
  81. public:
  82. /// Value is set during token construction.
  83. ///
  84. /// @param str constant string to be represented.
  85. TokenString(const std::string& str)
  86. :value_(str){
  87. }
  88. /// @brief Token evaluation (puts value of the constant string on the stack)
  89. ///
  90. /// @param pkt (ignored)
  91. /// @param values (represented string will be pushed here)
  92. void evaluate(const Pkt& pkt, ValueStack& values);
  93. protected:
  94. std::string value_; ///< Constant value
  95. };
  96. /// @brief Token representing a constant string in hexadecimal format
  97. ///
  98. /// This token holds value of a constant string giving in an hexadecimal
  99. /// format, for instance 0x666f6f is "foo"
  100. class TokenHexString : public Token {
  101. public:
  102. /// Value is set during token construction.
  103. ///
  104. /// @param str constant string to be represented
  105. /// (must be "0x" or "0X" followed by a string of hexadecimal digits
  106. /// or decoding will fail)
  107. TokenHexString(const std::string& str);
  108. /// @brief Token evaluation (puts value of the constant string on
  109. /// the stack after decoding or an empty string if decoding fails
  110. /// (note it should not if the parser is correct)
  111. ///
  112. /// @param pkt (ignored)
  113. /// @param values (represented string will be pushed here)
  114. void evaluate(const Pkt& pkt, ValueStack& values);
  115. protected:
  116. std::string value_; ///< Constant value
  117. };
  118. /// @brief Token that represents a value of an option
  119. ///
  120. /// This represents a reference to a given option, e.g. in the expression
  121. /// option[vendor-class] == "MSFT", it represents option[vendor-class]
  122. ///
  123. /// During the evaluation it tries to extract the value of the specified
  124. /// option. If the option is not found, an empty string ("") is returned.
  125. class TokenOption : public Token {
  126. public:
  127. /// @brief Constructor that takes an option code as a parameter
  128. /// @param option_code code of the option
  129. ///
  130. /// Note: There is no constructor that takes option_name, as it would
  131. /// introduce complex dependency of the libkea-eval on libdhcpsrv.
  132. ///
  133. /// @param option_code code of the option to be represented.
  134. TokenOption(uint16_t option_code)
  135. :option_code_(option_code) {}
  136. /// @brief Evaluates the values of the option
  137. ///
  138. /// This token represents a value of the option, so this method attempts
  139. /// to extract the option from the packet and put its value on the stack.
  140. /// If the option is not there, an empty string ("") is put on the stack.
  141. ///
  142. /// @param pkt specified option will be extracted from this packet (if present)
  143. /// @param values value of the option will be pushed here (or "")
  144. void evaluate(const Pkt& pkt, ValueStack& values);
  145. /// @brief Returns option-code
  146. ///
  147. /// This method is used in testing to determine if the parser had
  148. /// instantiated TokenOption with correct parameters.
  149. ///
  150. /// @return option-code of the option this token expects to extract.
  151. uint16_t getCode() const {
  152. return (option_code_);
  153. }
  154. private:
  155. uint16_t option_code_; ///< code of the option to be extracted
  156. };
  157. /// @brief Token that represents equality operator (compares two other tokens)
  158. ///
  159. /// For example in the expression option[vendor-class] == "MSFT" this token
  160. /// represents the equal (==) sign.
  161. class TokenEqual : public Token {
  162. public:
  163. /// @brief Constructor (does nothing)
  164. TokenEqual() {}
  165. /// @brief Compare two values.
  166. ///
  167. /// Evaluation does not use packet information, but rather consumes the last
  168. /// two parameters. It does a simple string comparison and sets the value to
  169. /// either "true" or "false". It requires at least two parameters to be
  170. /// present on stack.
  171. ///
  172. /// @throw EvalBadStack if there are less than 2 values on stack
  173. ///
  174. /// @param pkt (unused)
  175. /// @param values - stack of values (2 arguments will be popped, 1 result
  176. /// will be pushed)
  177. void evaluate(const Pkt& pkt, ValueStack& values);
  178. };
  179. /// @brief Token that represents the substring operator (returns a portion
  180. /// of the supplied string)
  181. ///
  182. /// This token represents substring(str, start, len) An operator that takes three
  183. /// arguments: a string, the first character and the length.
  184. class TokenSubstring : public Token {
  185. public:
  186. /// @brief Constructor (does nothing)
  187. TokenSubstring() {}
  188. /// @brief Extract a substring from a string
  189. ///
  190. /// Evaluation does not use packet information. It requires at least
  191. /// three values to be present on the stack. It will consume the top
  192. /// three values on the stack as parameters and push the resulting substring
  193. /// onto the stack. From the top it expects the values on the stack as:
  194. /// - len
  195. /// - start
  196. /// - str
  197. ///
  198. /// str is the string to extract a substring from. If it is empty, an empty
  199. /// string is pushed onto the value stack.
  200. ///
  201. /// start is the postion from which the code starts extracting the substring.
  202. /// 0 is the first character and a negative number starts from the end, with
  203. /// -1 being the last character. If the starting point is outside of the
  204. /// original string an empty string is pushed onto the value stack.
  205. ///
  206. /// length is the number of characters from the string to extract.
  207. /// "all" means all remaining characters from start to the end of string.
  208. /// A negative number means to go from start towards the beginning of
  209. /// the string, but doesn't include start.
  210. /// If length is longer than the remaining portion of string
  211. /// then the entire remaining portion is placed on the value stack.
  212. ///
  213. /// The following examples all use the base string "foobar", the first number
  214. /// is the starting position and the second is the length. Note that
  215. /// a negative length only selects which characters to extract it does not
  216. /// indicate an attempt to reverse the string.
  217. /// - 0, all => "foobar"
  218. /// - 0, 6 => "foobar"
  219. /// - 0, 4 => "foob"
  220. /// - 2, all => "obar"
  221. /// - 2, 6 => "obar"
  222. /// - -1, all => "r"
  223. /// - -1, -4 => "ooba"
  224. ///
  225. /// @throw EvalBadStack if there are less than 3 values on stack
  226. /// @throw EvalTypeError if start is not a number or length a number or
  227. /// the special value "all".
  228. ///
  229. /// @param pkt (unused)
  230. /// @param values - stack of values (3 arguments will be popped, 1 result
  231. /// will be pushed)
  232. void evaluate(const Pkt& pkt, ValueStack& values);
  233. };
  234. }; // end of isc::dhcp namespace
  235. }; // end of isc namespace
  236. #endif