token.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // Copyright (C) 2015-2016 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. #include <eval/token.h>
  7. #include <eval/eval_log.h>
  8. #include <eval/eval_context.h>
  9. #include <util/encode/hex.h>
  10. #include <util/io_utilities.h>
  11. #include <asiolink/io_address.h>
  12. #include <dhcp/pkt4.h>
  13. #include <dhcp/pkt6.h>
  14. #include <boost/lexical_cast.hpp>
  15. #include <dhcp/dhcp4.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/option_vendor.h>
  18. #include <dhcp/option_vendor_class.h>
  19. #include <cstring>
  20. #include <string>
  21. using namespace isc::dhcp;
  22. using namespace isc::util;
  23. using namespace std;
  24. using isc::util::encode::toHex;
  25. void
  26. TokenString::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  27. // Literals only push, nothing to pop
  28. values.push(value_);
  29. // Log what we pushed
  30. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_STRING)
  31. .arg('\'' + value_ + '\'');
  32. }
  33. TokenHexString::TokenHexString(const string& str) : value_("") {
  34. // Check string starts "0x" or "0x" and has at least one additional character.
  35. if ((str.size() < 3) ||
  36. (str[0] != '0') ||
  37. ((str[1] != 'x') && (str[1] != 'X'))) {
  38. return;
  39. }
  40. string digits = str.substr(2);
  41. // Transform string of hexadecimal digits into binary format
  42. vector<uint8_t> binary;
  43. try {
  44. // The decodeHex function expects that the string contains an
  45. // even number of digits. If we don't meet this requirement,
  46. // we have to insert a leading 0.
  47. if ((digits.length() % 2) != 0) {
  48. digits = digits.insert(0, "0");
  49. }
  50. util::encode::decodeHex(digits, binary);
  51. } catch (...) {
  52. return;
  53. }
  54. // Convert to a string (note that binary.size() cannot be 0)
  55. value_.resize(binary.size());
  56. memmove(&value_[0], &binary[0], binary.size());
  57. }
  58. void
  59. TokenHexString::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  60. // Literals only push, nothing to pop
  61. values.push(value_);
  62. // Log what we pushed
  63. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_HEXSTRING)
  64. .arg(toHex(value_));
  65. }
  66. TokenIpAddress::TokenIpAddress(const string& addr) : value_("") {
  67. // Transform IP address into binary format
  68. vector<uint8_t> binary;
  69. try {
  70. asiolink::IOAddress ip(addr);
  71. binary = ip.toBytes();
  72. } catch (...) {
  73. return;
  74. }
  75. // Convert to a string (note that binary.size() is 4 or 16, so not 0)
  76. value_.resize(binary.size());
  77. memmove(&value_[0], &binary[0], binary.size());
  78. }
  79. void
  80. TokenIpAddress::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  81. // Literals only push, nothing to pop
  82. values.push(value_);
  83. // Log what we pushed
  84. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_IPADDRESS)
  85. .arg(toHex(value_));
  86. }
  87. OptionPtr
  88. TokenOption::getOption(Pkt& pkt) {
  89. return (pkt.getOption(option_code_));
  90. }
  91. void
  92. TokenOption::evaluate(Pkt& pkt, ValueStack& values) {
  93. OptionPtr opt = getOption(pkt);
  94. std::string opt_str;
  95. if (opt) {
  96. if (representation_type_ == TEXTUAL) {
  97. opt_str = opt->toString();
  98. } else if (representation_type_ == HEXADECIMAL) {
  99. std::vector<uint8_t> binary = opt->toBinary();
  100. opt_str.resize(binary.size());
  101. if (!binary.empty()) {
  102. memmove(&opt_str[0], &binary[0], binary.size());
  103. }
  104. } else {
  105. opt_str = "true";
  106. }
  107. } else if (representation_type_ == EXISTS) {
  108. opt_str = "false";
  109. }
  110. // Push value of the option or empty string if there was no such option
  111. // in the packet.
  112. values.push(opt_str);
  113. // Log what we pushed, both exists and textual are simple text
  114. // and can be output directly. We also include the code number
  115. // of the requested option.
  116. if (representation_type_ == HEXADECIMAL) {
  117. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_OPTION)
  118. .arg(option_code_)
  119. .arg(toHex(opt_str));
  120. } else {
  121. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_OPTION)
  122. .arg(option_code_)
  123. .arg('\'' + opt_str + '\'');
  124. }
  125. }
  126. std::string
  127. TokenOption::pushFailure(ValueStack& values) {
  128. std::string txt;
  129. if (representation_type_ == EXISTS) {
  130. txt = "false";
  131. }
  132. values.push(txt);
  133. return (txt);
  134. }
  135. TokenRelay4Option::TokenRelay4Option(const uint16_t option_code,
  136. const RepresentationType& rep_type)
  137. :TokenOption(option_code, rep_type) {
  138. }
  139. OptionPtr TokenRelay4Option::getOption(Pkt& pkt) {
  140. // Check if there is Relay Agent Option.
  141. OptionPtr rai = pkt.getOption(DHO_DHCP_AGENT_OPTIONS);
  142. if (!rai) {
  143. return (OptionPtr());
  144. }
  145. // If there is, try to return its suboption
  146. return (rai->getOption(option_code_));
  147. }
  148. OptionPtr TokenRelay6Option::getOption(Pkt& pkt) {
  149. try {
  150. // Check if it's a Pkt6. If it's not the dynamic_cast will
  151. // throw std::bad_cast.
  152. Pkt6& pkt6 = dynamic_cast<Pkt6&>(pkt);
  153. try {
  154. // Now that we have the right type of packet we can
  155. // get the option and return it.
  156. return(pkt6.getRelayOption(option_code_, nest_level_));
  157. }
  158. catch (const isc::OutOfRange&) {
  159. // The only exception we expect is OutOfRange if the nest
  160. // level is out of range of the encapsulations, for example
  161. // if nest_level_ is 4 and there are only 2 encapsulations.
  162. // We return a NULL in that case.
  163. return (OptionPtr());
  164. }
  165. } catch (const std::bad_cast&) {
  166. isc_throw(EvalTypeError, "Specified packet is not Pkt6");
  167. }
  168. }
  169. void
  170. TokenPkt::evaluate(Pkt& pkt, ValueStack& values) {
  171. string value;
  172. vector<uint8_t> binary;
  173. string type_str;
  174. bool is_binary = true;
  175. bool print_hex = true;
  176. switch (type_) {
  177. case IFACE:
  178. is_binary = false;
  179. print_hex = false;
  180. value = pkt.getIface();
  181. type_str = "iface";
  182. break;
  183. case SRC:
  184. binary = pkt.getRemoteAddr().toBytes();
  185. type_str = "src";
  186. break;
  187. case DST:
  188. binary = pkt.getLocalAddr().toBytes();
  189. type_str = "dst";
  190. break;
  191. case LEN:
  192. // len() returns a size_t but in fact it can't be very large
  193. // (with UDP transport it fits in 16 bits)
  194. // the len() method is not const because of DHCPv6 relays.
  195. // We assume here it has no bad side effects...
  196. value = EvalContext::fromUint32(static_cast<uint32_t>(const_cast<Pkt&>(pkt).len()));
  197. is_binary = false;
  198. type_str = "len";
  199. break;
  200. default:
  201. isc_throw(EvalTypeError, "Bad meta data specified: "
  202. << static_cast<int>(type_) );
  203. }
  204. if (is_binary) {
  205. value.resize(binary.size());
  206. if (!binary.empty()) {
  207. memmove(&value[0], &binary[0], binary.size());
  208. }
  209. }
  210. values.push(value);
  211. // Log what we pushed
  212. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT)
  213. .arg(type_str)
  214. .arg(print_hex ? toHex(value) : value);
  215. }
  216. void
  217. TokenPkt4::evaluate(Pkt& pkt, ValueStack& values) {
  218. vector<uint8_t> binary;
  219. string value;
  220. string type_str;
  221. try {
  222. // Check if it's a Pkt4. If it's not, the dynamic_cast will throw
  223. // std::bad_cast (failed dynamic_cast returns NULL for pointers and
  224. // throws for references).
  225. const Pkt4& pkt4 = dynamic_cast<const Pkt4&>(pkt);
  226. switch (type_) {
  227. case CHADDR: {
  228. HWAddrPtr hwaddr = pkt4.getHWAddr();
  229. if (!hwaddr) {
  230. // This should never happen. Every Pkt4 should always have
  231. // a hardware address.
  232. isc_throw(EvalTypeError,
  233. "Packet does not have hardware address");
  234. }
  235. binary = hwaddr->hwaddr_;
  236. type_str = "mac";
  237. break;
  238. }
  239. case GIADDR:
  240. binary = pkt4.getGiaddr().toBytes();
  241. type_str = "giaddr";
  242. break;
  243. case CIADDR:
  244. binary = pkt4.getCiaddr().toBytes();
  245. type_str = "ciaddr";
  246. break;
  247. case YIADDR:
  248. binary = pkt4.getYiaddr().toBytes();
  249. type_str = "yiaddr";
  250. break;
  251. case SIADDR:
  252. binary = pkt4.getSiaddr().toBytes();
  253. type_str = "siaddr";
  254. break;
  255. case HLEN:
  256. // Pad the uint8_t field to 4 bytes.
  257. value = EvalContext::fromUint32(pkt4.getHlen());
  258. type_str = "hlen";
  259. break;
  260. case HTYPE:
  261. // Pad the uint8_t field to 4 bytes.
  262. value = EvalContext::fromUint32(pkt4.getHtype());
  263. type_str = "htype";
  264. break;
  265. case MSGTYPE:
  266. value = EvalContext::fromUint32(pkt4.getType());
  267. type_str = "msgtype";
  268. break;
  269. case TRANSID:
  270. value = EvalContext::fromUint32(pkt4.getTransid());
  271. type_str = "transid";
  272. break;
  273. default:
  274. isc_throw(EvalTypeError, "Bad field specified: "
  275. << static_cast<int>(type_) );
  276. }
  277. } catch (const std::bad_cast&) {
  278. isc_throw(EvalTypeError, "Specified packet is not a Pkt4");
  279. }
  280. if (!binary.empty()) {
  281. value.resize(binary.size());
  282. memmove(&value[0], &binary[0], binary.size());
  283. }
  284. values.push(value);
  285. // Log what we pushed
  286. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT4)
  287. .arg(type_str)
  288. .arg(toHex(value));
  289. }
  290. void
  291. TokenPkt6::evaluate(Pkt& pkt, ValueStack& values) {
  292. string value;
  293. string type_str;
  294. try {
  295. // Check if it's a Pkt6. If it's not the dynamic_cast will throw
  296. // std::bad_cast (failed dynamic_cast returns NULL for pointers and
  297. // throws for references).
  298. const Pkt6& pkt6 = dynamic_cast<const Pkt6&>(pkt);
  299. switch (type_) {
  300. case MSGTYPE: {
  301. // msg type is an uint8_t integer. We want a 4 byte string so 0 pad.
  302. value = EvalContext::fromUint32(pkt6.getType());
  303. type_str = "msgtype";
  304. break;
  305. }
  306. case TRANSID: {
  307. // transaction id is an uint32_t integer. We want a 4 byte string so copy
  308. value = EvalContext::fromUint32(pkt6.getTransid());
  309. type_str = "transid";
  310. break;
  311. }
  312. default:
  313. isc_throw(EvalTypeError, "Bad field specified: "
  314. << static_cast<int>(type_) );
  315. }
  316. } catch (const std::bad_cast&) {
  317. isc_throw(EvalTypeError, "Specified packet is not Pkt6");
  318. }
  319. values.push(value);
  320. // Log what we pushed
  321. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_PKT6)
  322. .arg(type_str)
  323. .arg(toHex(value));
  324. }
  325. void
  326. TokenRelay6Field::evaluate(Pkt& pkt, ValueStack& values) {
  327. vector<uint8_t> binary;
  328. string type_str;
  329. try {
  330. // Check if it's a Pkt6. If it's not the dynamic_cast will
  331. // throw std::bad_cast.
  332. const Pkt6& pkt6 = dynamic_cast<const Pkt6&>(pkt);
  333. try {
  334. switch (type_) {
  335. // Now that we have the right type of packet we can
  336. // get the option and return it.
  337. case LINKADDR:
  338. type_str = "linkaddr";
  339. binary = pkt6.getRelay6LinkAddress(nest_level_).toBytes();
  340. break;
  341. case PEERADDR:
  342. type_str = "peeraddr";
  343. binary = pkt6.getRelay6PeerAddress(nest_level_).toBytes();
  344. break;
  345. }
  346. } catch (const isc::OutOfRange&) {
  347. // The only exception we expect is OutOfRange if the nest
  348. // level is invalid. We push "" in that case.
  349. values.push("");
  350. // Log what we pushed
  351. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_RELAY6_RANGE)
  352. .arg(type_str)
  353. .arg(unsigned(nest_level_))
  354. .arg("0x");
  355. return;
  356. }
  357. } catch (const std::bad_cast&) {
  358. isc_throw(EvalTypeError, "Specified packet is not Pkt6");
  359. }
  360. string value;
  361. value.resize(binary.size());
  362. if (!binary.empty()) {
  363. memmove(&value[0], &binary[0], binary.size());
  364. }
  365. values.push(value);
  366. // Log what we pushed
  367. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_RELAY6)
  368. .arg(type_str)
  369. .arg(unsigned(nest_level_))
  370. .arg(toHex(value));
  371. }
  372. void
  373. TokenEqual::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  374. if (values.size() < 2) {
  375. isc_throw(EvalBadStack, "Incorrect stack order. Expected at least "
  376. "2 values for == operator, got " << values.size());
  377. }
  378. string op1 = values.top();
  379. values.pop();
  380. string op2 = values.top();
  381. values.pop(); // Dammit, std::stack interface is awkward.
  382. if (op1 == op2)
  383. values.push("true");
  384. else
  385. values.push("false");
  386. // Log what we popped and pushed
  387. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_EQUAL)
  388. .arg(toHex(op1))
  389. .arg(toHex(op2))
  390. .arg('\'' + values.top() + '\'');
  391. }
  392. void
  393. TokenSubstring::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  394. if (values.size() < 3) {
  395. isc_throw(EvalBadStack, "Incorrect stack order. Expected at least "
  396. "3 values for substring operator, got " << values.size());
  397. }
  398. string len_str = values.top();
  399. values.pop();
  400. string start_str = values.top();
  401. values.pop();
  402. string string_str = values.top();
  403. values.pop();
  404. // If we have no string to start with we push an empty string and leave
  405. if (string_str.empty()) {
  406. values.push("");
  407. // Log what we popped and pushed
  408. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_SUBSTRING_EMPTY)
  409. .arg(len_str)
  410. .arg(start_str)
  411. .arg("0x")
  412. .arg("0x");
  413. return;
  414. }
  415. // Convert the starting position and length from strings to numbers
  416. // the length may also be "all" in which case simply make it the
  417. // length of the string.
  418. // If we have a problem push an empty string and leave
  419. int start_pos;
  420. int length;
  421. try {
  422. start_pos = boost::lexical_cast<int>(start_str);
  423. } catch (const boost::bad_lexical_cast&) {
  424. isc_throw(EvalTypeError, "the parameter '" << start_str
  425. << "' for the starting postion of the substring "
  426. << "couldn't be converted to an integer.");
  427. }
  428. try {
  429. if (len_str == "all") {
  430. length = string_str.length();
  431. } else {
  432. length = boost::lexical_cast<int>(len_str);
  433. }
  434. } catch (const boost::bad_lexical_cast&) {
  435. isc_throw(EvalTypeError, "the parameter '" << len_str
  436. << "' for the length of the substring "
  437. << "couldn't be converted to an integer.");
  438. }
  439. const int string_length = string_str.length();
  440. // If the starting postion is outside of the string push an
  441. // empty string and leave
  442. if ((start_pos < -string_length) || (start_pos >= string_length)) {
  443. values.push("");
  444. // Log what we popped and pushed
  445. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_SUBSTRING_RANGE)
  446. .arg(len_str)
  447. .arg(start_str)
  448. .arg(toHex(string_str))
  449. .arg("0x");
  450. return;
  451. }
  452. // Adjust the values to be something for substr. We first figure out
  453. // the starting postion, then update it and the length to get the
  454. // characters before or after it depending on the sign of length
  455. if (start_pos < 0) {
  456. start_pos = string_length + start_pos;
  457. }
  458. if (length < 0) {
  459. length = -length;
  460. if (length <= start_pos){
  461. start_pos -= length;
  462. } else {
  463. length = start_pos;
  464. start_pos = 0;
  465. }
  466. }
  467. // and finally get the substring
  468. values.push(string_str.substr(start_pos, length));
  469. // Log what we popped and pushed
  470. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_SUBSTRING)
  471. .arg(len_str)
  472. .arg(start_str)
  473. .arg(toHex(string_str))
  474. .arg(toHex(values.top()));
  475. }
  476. void
  477. TokenConcat::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  478. if (values.size() < 2) {
  479. isc_throw(EvalBadStack, "Incorrect stack order. Expected at least "
  480. "2 values for concat, got " << values.size());
  481. }
  482. string op1 = values.top();
  483. values.pop();
  484. string op2 = values.top();
  485. values.pop(); // Dammit, std::stack interface is awkward.
  486. // The top of the stack was evaluated last so this is the right order
  487. values.push(op2 + op1);
  488. // Log what we popped and pushed
  489. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_CONCAT)
  490. .arg(toHex(op1))
  491. .arg(toHex(op2))
  492. .arg(toHex(values.top()));
  493. }
  494. void
  495. TokenNot::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  496. if (values.size() == 0) {
  497. isc_throw(EvalBadStack, "Incorrect empty stack.");
  498. }
  499. string op = values.top();
  500. values.pop();
  501. bool val = toBool(op);
  502. if (!val) {
  503. values.push("true");
  504. } else {
  505. values.push("false");
  506. }
  507. // Log what we popped and pushed
  508. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_NOT)
  509. .arg('\'' + op + '\'')
  510. .arg('\'' + values.top() + '\'');
  511. }
  512. void
  513. TokenAnd::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  514. if (values.size() < 2) {
  515. isc_throw(EvalBadStack, "Incorrect stack order. Expected at least "
  516. "2 values for and operator, got " << values.size());
  517. }
  518. string op1 = values.top();
  519. values.pop();
  520. bool val1 = toBool(op1);
  521. string op2 = values.top();
  522. values.pop(); // Dammit, std::stack interface is awkward.
  523. bool val2 = toBool(op2);
  524. if (val1 && val2) {
  525. values.push("true");
  526. } else {
  527. values.push("false");
  528. }
  529. // Log what we popped and pushed
  530. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_AND)
  531. .arg('\'' + op1 + '\'')
  532. .arg('\'' + op2 + '\'')
  533. .arg('\'' + values.top() + '\'');
  534. }
  535. void
  536. TokenOr::evaluate(Pkt& /*pkt*/, ValueStack& values) {
  537. if (values.size() < 2) {
  538. isc_throw(EvalBadStack, "Incorrect stack order. Expected at least "
  539. "2 values for or operator, got " << values.size());
  540. }
  541. string op1 = values.top();
  542. values.pop();
  543. bool val1 = toBool(op1);
  544. string op2 = values.top();
  545. values.pop(); // Dammit, std::stack interface is awkward.
  546. bool val2 = toBool(op2);
  547. if (val1 || val2) {
  548. values.push("true");
  549. } else {
  550. values.push("false");
  551. }
  552. // Log what we popped and pushed
  553. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_OR)
  554. .arg('\'' + op1 + '\'')
  555. .arg('\'' + op2 + '\'')
  556. .arg('\'' + values.top() + '\'');
  557. }
  558. TokenVendor::TokenVendor(Option::Universe u, uint32_t vendor_id, RepresentationType repr,
  559. uint16_t option_code)
  560. :TokenOption(option_code, repr), universe_(u), vendor_id_(vendor_id),
  561. field_(option_code ? SUBOPTION : EXISTS)
  562. {
  563. }
  564. TokenVendor::TokenVendor(Option::Universe u, uint32_t vendor_id, FieldType field)
  565. :TokenOption(0, TokenOption::HEXADECIMAL), universe_(u), vendor_id_(vendor_id),
  566. field_(field)
  567. {
  568. if (field_ == EXISTS) {
  569. representation_type_ = TokenOption::EXISTS;
  570. }
  571. }
  572. uint32_t TokenVendor::getVendorId() const {
  573. return (vendor_id_);
  574. }
  575. TokenVendor::FieldType TokenVendor::getField() const {
  576. return (field_);
  577. }
  578. void TokenVendor::evaluate(Pkt& pkt, ValueStack& values) {
  579. // Get the option first.
  580. uint16_t code = 0;
  581. switch (universe_) {
  582. case Option::V4:
  583. code = DHO_VIVSO_SUBOPTIONS;
  584. break;
  585. case Option::V6:
  586. code = D6O_VENDOR_OPTS;
  587. break;
  588. }
  589. OptionPtr opt = pkt.getOption(code);
  590. OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt);
  591. if (!vendor) {
  592. // There's no vendor option, give up.
  593. std::string txt = pushFailure(values);
  594. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_NO_OPTION)
  595. .arg(code)
  596. .arg(txt);
  597. return;
  598. }
  599. if (vendor_id_ && (vendor_id_ != vendor->getVendorId())) {
  600. // There is vendor option, but it has other vendor-id value
  601. // than we're looking for. (0 means accept any vendor-id)
  602. std::string txt = pushFailure(values);
  603. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH)
  604. .arg(vendor_id_)
  605. .arg(vendor->getVendorId())
  606. .arg(txt);
  607. return;
  608. }
  609. switch (field_) {
  610. case ENTERPRISE_ID:
  611. {
  612. // Extract enterprise-id
  613. string txt(sizeof(uint32_t), 0);
  614. uint32_t value = htonl(vendor->getVendorId());
  615. memcpy(&txt[0], &value, sizeof(uint32_t));
  616. values.push(txt);
  617. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_ENTERPRISE_ID)
  618. .arg(vendor->getVendorId())
  619. .arg(util::encode::encodeHex(std::vector<uint8_t>(txt.begin(),
  620. txt.end())));
  621. return;
  622. }
  623. case SUBOPTION:
  624. /// This is vendor[X].option[Y].exists, let's try to
  625. /// extract the option
  626. TokenOption::evaluate(pkt, values);
  627. return;
  628. case EXISTS:
  629. // We already passed all the checks: the option is there and has specified
  630. // enterprise-id.
  631. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_EXISTS)
  632. .arg(vendor->getVendorId())
  633. .arg("true");
  634. values.push("true");
  635. return;
  636. case DATA:
  637. // This is for vendor-class option, we can skip it here.
  638. isc_throw(EvalTypeError, "Field None is not valid for vendor-class");
  639. return;
  640. }
  641. }
  642. OptionPtr TokenVendor::getOption(Pkt& pkt) {
  643. uint16_t code = 0;
  644. switch (universe_) {
  645. case Option::V4:
  646. code = DHO_VIVSO_SUBOPTIONS;
  647. break;
  648. case Option::V6:
  649. code = D6O_VENDOR_OPTS;
  650. break;
  651. }
  652. OptionPtr opt = pkt.getOption(code);
  653. if (!opt) {
  654. // If vendor option is not found, return NULL
  655. return (opt);
  656. }
  657. // If vendor option is found, try to return its
  658. // encapsulated option.
  659. return (opt->getOption(option_code_));
  660. }
  661. TokenVendorClass::TokenVendorClass(Option::Universe u, uint32_t vendor_id,
  662. RepresentationType repr)
  663. :TokenVendor(u, vendor_id, repr, 0), index_(0) {
  664. }
  665. TokenVendorClass::TokenVendorClass(Option::Universe u, uint32_t vendor_id,
  666. FieldType field, uint16_t index)
  667. :TokenVendor(u, vendor_id, TokenOption::HEXADECIMAL, 0), index_(index)
  668. {
  669. field_ = field;
  670. }
  671. uint16_t TokenVendorClass::getDataIndex() const {
  672. return (index_);
  673. }
  674. void TokenVendorClass::evaluate(Pkt& pkt, ValueStack& values) {
  675. // Get the option first.
  676. uint16_t code = 0;
  677. switch (universe_) {
  678. case Option::V4:
  679. code = DHO_VIVCO_SUBOPTIONS;
  680. break;
  681. case Option::V6:
  682. code = D6O_VENDOR_CLASS;
  683. break;
  684. }
  685. OptionPtr opt = pkt.getOption(code);
  686. OptionVendorClassPtr vendor = boost::dynamic_pointer_cast<OptionVendorClass>(opt);
  687. if (!vendor) {
  688. // There's no vendor class option, give up.
  689. std::string txt = pushFailure(values);
  690. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_NO_OPTION)
  691. .arg(code)
  692. .arg(txt);
  693. return;
  694. }
  695. if (vendor_id_ && (vendor_id_ != vendor->getVendorId())) {
  696. // There is vendor option, but it has other vendor-id value
  697. // than we're looking for. (0 means accept any vendor-id)
  698. std::string txt = pushFailure(values);
  699. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH)
  700. .arg(vendor_id_)
  701. .arg(vendor->getVendorId())
  702. .arg(txt);
  703. return;
  704. }
  705. switch (field_) {
  706. case ENTERPRISE_ID:
  707. {
  708. // Extract enterprise-id
  709. string txt(sizeof(uint32_t), 0);
  710. uint32_t value = htonl(vendor->getVendorId());
  711. memcpy(&txt[0], &value, sizeof(uint32_t));
  712. values.push(txt);
  713. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID)
  714. .arg(vendor->getVendorId())
  715. .arg(util::encode::encodeHex(std::vector<uint8_t>(txt.begin(),
  716. txt.end())));
  717. return;
  718. }
  719. case SUBOPTION:
  720. // Extract sub-options
  721. isc_throw(EvalTypeError, "Field None is not valid for vendor-class");
  722. return;
  723. case EXISTS:
  724. // We already passed all the checks: the option is there and has specified
  725. // enterprise-id.
  726. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_EXISTS)
  727. .arg(vendor->getVendorId())
  728. .arg("true");
  729. values.push("true");
  730. return;
  731. case DATA:
  732. {
  733. size_t max = vendor->getTuplesNum();
  734. if (index_ + 1 > max) {
  735. // The index specified is out of bounds, e.g. there are only
  736. // 2 tuples and index specified is 5.
  737. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND)
  738. .arg(index_)
  739. .arg(vendor->getVendorId())
  740. .arg(max)
  741. .arg("");
  742. values.push("");
  743. return;
  744. }
  745. OpaqueDataTuple tuple = vendor->getTuple(index_);
  746. OpaqueDataTuple::Buffer buf = tuple.getData();
  747. string txt(buf.begin(), buf.end());
  748. LOG_DEBUG(eval_logger, EVAL_DBG_STACK, EVAL_DEBUG_VENDOR_CLASS_DATA)
  749. .arg(index_)
  750. .arg(max)
  751. .arg(txt);
  752. values.push(txt);
  753. return;
  754. }
  755. default:
  756. isc_throw(EvalTypeError, "Invalid field specified." << field_);
  757. }
  758. }
  759. TokenInteger::TokenInteger(const uint32_t value)
  760. :TokenString(EvalContext::fromUint32(value)), int_value_(value) {
  761. }