option_definition.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // Copyright (C) 2012-2013 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. #include <dhcp/dhcp6.h>
  15. #include <dhcp/option4_addrlst.h>
  16. #include <dhcp/option6_addrlst.h>
  17. #include <dhcp/option6_ia.h>
  18. #include <dhcp/option6_iaaddr.h>
  19. #include <dhcp/option_custom.h>
  20. #include <dhcp/option_definition.h>
  21. #include <dhcp/option_int.h>
  22. #include <dhcp/option_int_array.h>
  23. #include <dhcp/option_space.h>
  24. #include <dhcp/option_string.h>
  25. #include <util/encode/hex.h>
  26. #include <util/strutil.h>
  27. #include <boost/algorithm/string/classification.hpp>
  28. #include <boost/algorithm/string/predicate.hpp>
  29. using namespace std;
  30. using namespace isc::util;
  31. namespace isc {
  32. namespace dhcp {
  33. OptionDefinition::OptionDefinition(const std::string& name,
  34. const uint16_t code,
  35. const std::string& type,
  36. const bool array_type /* = false */)
  37. : name_(name),
  38. code_(code),
  39. type_(OPT_UNKNOWN_TYPE),
  40. array_type_(array_type),
  41. encapsulated_space_("") {
  42. // Data type is held as enum value by this class.
  43. // Use the provided option type string to get the
  44. // corresponding enum value.
  45. type_ = OptionDataTypeUtil::getDataType(type);
  46. }
  47. OptionDefinition::OptionDefinition(const std::string& name,
  48. const uint16_t code,
  49. const OptionDataType type,
  50. const bool array_type /* = false */)
  51. : name_(name),
  52. code_(code),
  53. type_(type),
  54. array_type_(array_type),
  55. encapsulated_space_("") {
  56. }
  57. OptionDefinition::OptionDefinition(const std::string& name,
  58. const uint16_t code,
  59. const std::string& type,
  60. const char* encapsulated_space)
  61. : name_(name),
  62. code_(code),
  63. // Data type is held as enum value by this class.
  64. // Use the provided option type string to get the
  65. // corresponding enum value.
  66. type_(OptionDataTypeUtil::getDataType(type)),
  67. array_type_(false),
  68. encapsulated_space_(encapsulated_space) {
  69. }
  70. OptionDefinition::OptionDefinition(const std::string& name,
  71. const uint16_t code,
  72. const OptionDataType type,
  73. const char* encapsulated_space)
  74. : name_(name),
  75. code_(code),
  76. type_(type),
  77. array_type_(false),
  78. encapsulated_space_(encapsulated_space) {
  79. }
  80. void
  81. OptionDefinition::addRecordField(const std::string& data_type_name) {
  82. OptionDataType data_type = OptionDataTypeUtil::getDataType(data_type_name);
  83. addRecordField(data_type);
  84. }
  85. void
  86. OptionDefinition::addRecordField(const OptionDataType data_type) {
  87. if (type_ != OPT_RECORD_TYPE) {
  88. isc_throw(isc::InvalidOperation, "'record' option type must be used"
  89. " to add data fields to the record");
  90. }
  91. if (data_type >= OPT_RECORD_TYPE ||
  92. data_type == OPT_ANY_ADDRESS_TYPE ||
  93. data_type == OPT_EMPTY_TYPE) {
  94. isc_throw(isc::BadValue, "attempted to add invalid data type to the record.");
  95. }
  96. record_fields_.push_back(data_type);
  97. }
  98. OptionPtr
  99. OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
  100. OptionBufferConstIter begin,
  101. OptionBufferConstIter end) const {
  102. try {
  103. switch(type_) {
  104. case OPT_EMPTY_TYPE:
  105. return (factoryEmpty(u, type));
  106. case OPT_BINARY_TYPE:
  107. return (factoryGeneric(u, type, begin, end));
  108. case OPT_UINT8_TYPE:
  109. return (array_type_ ? factoryGeneric(u, type, begin, end) :
  110. factoryInteger<uint8_t>(u, type, begin, end));
  111. case OPT_INT8_TYPE:
  112. return (array_type_ ? factoryGeneric(u, type, begin, end) :
  113. factoryInteger<int8_t>(u, type, begin, end));
  114. case OPT_UINT16_TYPE:
  115. return (array_type_ ? factoryIntegerArray<uint16_t>(u, type, begin, end) :
  116. factoryInteger<uint16_t>(u, type, begin, end));
  117. case OPT_INT16_TYPE:
  118. return (array_type_ ? factoryIntegerArray<uint16_t>(u, type, begin, end) :
  119. factoryInteger<int16_t>(u, type, begin, end));
  120. case OPT_UINT32_TYPE:
  121. return (array_type_ ? factoryIntegerArray<uint32_t>(u, type, begin, end) :
  122. factoryInteger<uint32_t>(u, type, begin, end));
  123. case OPT_INT32_TYPE:
  124. return (array_type_ ? factoryIntegerArray<uint32_t>(u, type, begin, end) :
  125. factoryInteger<int32_t>(u, type, begin, end));
  126. case OPT_IPV4_ADDRESS_TYPE:
  127. // If definition specifies that an option is an array
  128. // of IPv4 addresses we return an instance of specialized
  129. // class (OptionAddrLst4). For non-array types there is no
  130. // specialized class yet implemented so we drop through
  131. // to return an instance of OptionCustom.
  132. if (array_type_) {
  133. return (factoryAddrList4(type, begin, end));
  134. }
  135. break;
  136. case OPT_IPV6_ADDRESS_TYPE:
  137. // Handle array type only here (see comments for
  138. // OPT_IPV4_ADDRESS_TYPE case).
  139. if (array_type_) {
  140. return (factoryAddrList6(type, begin, end));
  141. }
  142. break;
  143. case OPT_STRING_TYPE:
  144. return (OptionPtr(new OptionString(u, type, begin, end)));
  145. default:
  146. if (u == Option::V6) {
  147. if ((code_ == D6O_IA_NA || code_ == D6O_IA_PD) &&
  148. haveIA6Format()) {
  149. // Return Option6IA instance for IA_PD and IA_NA option
  150. // types only. We don't want to return Option6IA for other
  151. // options that comprise 3 UINT32 data fields because
  152. // Option6IA accessors' and modifiers' names are derived
  153. // from the IA_NA and IA_PD options' field names: IAID,
  154. // T1, T2. Using functions such as getIAID, getT1 etc. for
  155. // options other than IA_NA and IA_PD would be bad practice
  156. // and cause confusion.
  157. return (factoryIA6(type, begin, end));
  158. } else if (code_ == D6O_IAADDR && haveIAAddr6Format()) {
  159. // Rerurn Option6IAAddr option instance for the IAADDR
  160. // option only for the same reasons as described in
  161. // for IA_NA and IA_PD above.
  162. return (factoryIAAddr6(type, begin, end));
  163. }
  164. }
  165. }
  166. return (OptionPtr(new OptionCustom(*this, u, begin, end)));
  167. } catch (const Exception& ex) {
  168. isc_throw(InvalidOptionValue, ex.what());
  169. }
  170. }
  171. OptionPtr
  172. OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
  173. const OptionBuffer& buf) const {
  174. return (optionFactory(u, type, buf.begin(), buf.end()));
  175. }
  176. OptionPtr
  177. OptionDefinition::optionFactory(Option::Universe u, uint16_t type,
  178. const std::vector<std::string>& values) const {
  179. OptionBuffer buf;
  180. if (!array_type_ && type_ != OPT_RECORD_TYPE) {
  181. if (values.empty()) {
  182. isc_throw(InvalidOptionValue, "no option value specified");
  183. }
  184. writeToBuffer(util::str::trim(values[0]), type_, buf);
  185. } else if (array_type_ && type_ != OPT_RECORD_TYPE) {
  186. for (size_t i = 0; i < values.size(); ++i) {
  187. writeToBuffer(util::str::trim(values[i]), type_, buf);
  188. }
  189. } else if (type_ == OPT_RECORD_TYPE) {
  190. const RecordFieldsCollection& records = getRecordFields();
  191. if (records.size() > values.size()) {
  192. isc_throw(InvalidOptionValue, "number of data fields for the option"
  193. << " type '" << getCode() << "' is greater than number"
  194. << " of values provided.");
  195. }
  196. for (size_t i = 0; i < records.size(); ++i) {
  197. writeToBuffer(util::str::trim(values[i]),
  198. records[i], buf);
  199. }
  200. }
  201. return (optionFactory(u, type, buf.begin(), buf.end()));
  202. }
  203. void
  204. OptionDefinition::validate() const {
  205. using namespace boost::algorithm;
  206. std::ostringstream err_str;
  207. // Allowed characters in the option name are: lower or
  208. // upper case letters, digits, underscores and hyphens.
  209. // Empty option spaces are not allowed.
  210. if (!all(name_, boost::is_from_range('a', 'z') ||
  211. boost::is_from_range('A', 'Z') ||
  212. boost::is_digit() ||
  213. boost::is_any_of(std::string("-_"))) ||
  214. name_.empty() ||
  215. // Hyphens and underscores are not allowed at the beginning
  216. // and at the end of the option name.
  217. all(find_head(name_, 1), boost::is_any_of(std::string("-_"))) ||
  218. all(find_tail(name_, 1), boost::is_any_of(std::string("-_")))) {
  219. err_str << "invalid option name '" << name_ << "'";
  220. } else if (!encapsulated_space_.empty() &&
  221. !OptionSpace::validateName(encapsulated_space_)) {
  222. err_str << "invalid encapsulated option space name: '"
  223. << encapsulated_space_ << "'";
  224. } else if (type_ >= OPT_UNKNOWN_TYPE) {
  225. // Option definition must be of a known type.
  226. err_str << "option type value " << type_ << " is out of range.";
  227. } else if (array_type_) {
  228. if (type_ == OPT_STRING_TYPE) {
  229. // Array of strings is not allowed because there is no way
  230. // to determine the size of a particular string and thus there
  231. // it no way to tell when other data fields begin.
  232. err_str << "array of strings is not a valid option definition.";
  233. } else if (type_ == OPT_BINARY_TYPE) {
  234. err_str << "array of binary values is not a valid option definition.";
  235. } else if (type_ == OPT_EMPTY_TYPE) {
  236. err_str << "array of empty value is not a valid option definition.";
  237. }
  238. } else if (type_ == OPT_RECORD_TYPE) {
  239. // At least two data fields should be added to the record. Otherwise
  240. // non-record option definition could be used.
  241. if (getRecordFields().size() < 2) {
  242. err_str << "invalid number of data fields: " << getRecordFields().size()
  243. << " specified for the option of type 'record'. Expected at"
  244. << " least 2 fields.";
  245. } else {
  246. // If the number of fields is valid we have to check if their order
  247. // is valid too. We check that string or binary data fields are not
  248. // laid before other fields. But we allow that they are laid at the end of
  249. // an option.
  250. const RecordFieldsCollection& fields = getRecordFields();
  251. for (RecordFieldsConstIter it = fields.begin();
  252. it != fields.end(); ++it) {
  253. if (*it == OPT_STRING_TYPE &&
  254. it < fields.end() - 1) {
  255. err_str << "string data field can't be laid before data fields"
  256. << " of other types.";
  257. break;
  258. }
  259. if (*it == OPT_BINARY_TYPE &&
  260. it < fields.end() - 1) {
  261. err_str << "binary data field can't be laid before data fields"
  262. << " of other types.";
  263. }
  264. /// Empty type is not allowed within a record.
  265. if (*it == OPT_EMPTY_TYPE) {
  266. err_str << "empty data type can't be stored as a field in an"
  267. << " option record.";
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. // Non-empty error string means that we have hit the error. We throw
  274. // exception and include error string.
  275. if (!err_str.str().empty()) {
  276. isc_throw(MalformedOptionDefinition, err_str.str());
  277. }
  278. }
  279. bool
  280. OptionDefinition::haveIAx6Format(OptionDataType first_type) const {
  281. return (haveType(OPT_RECORD_TYPE) &&
  282. record_fields_.size() == 3 &&
  283. record_fields_[0] == first_type &&
  284. record_fields_[1] == OPT_UINT32_TYPE &&
  285. record_fields_[2] == OPT_UINT32_TYPE);
  286. }
  287. bool
  288. OptionDefinition::haveIA6Format() const {
  289. // Expect that IA_NA option format is defined as record.
  290. // Although it consists of 3 elements of the same (uint32)
  291. // type it can't be defined as array of uint32 elements because
  292. // arrays do not impose limitations on number of elements in
  293. // the array while this limitation is needed for IA_NA - need
  294. // exactly 3 elements.
  295. return (haveIAx6Format(OPT_UINT32_TYPE));
  296. }
  297. bool
  298. OptionDefinition::haveIAAddr6Format() const {
  299. return (haveIAx6Format(OPT_IPV6_ADDRESS_TYPE));
  300. }
  301. template<typename T>
  302. T OptionDefinition::lexicalCastWithRangeCheck(const std::string& value_str) const {
  303. // Lexical cast in case of our data types make sense only
  304. // for uintX_t, intX_t and bool type.
  305. if (!OptionDataTypeTraits<T>::integer_type &&
  306. OptionDataTypeTraits<T>::type != OPT_BOOLEAN_TYPE) {
  307. isc_throw(BadDataTypeCast, "unable to do lexical cast to non-integer and"
  308. << " non-boolean data type");
  309. }
  310. // We use the 64-bit value here because it has wider range than
  311. // any other type we use here and it allows to detect out of
  312. // bounds conditions e.g. negative value specified for uintX_t
  313. // data type. Obviously if the value exceeds the limits of int64
  314. // this function will not handle that properly.
  315. int64_t result = 0;
  316. try {
  317. result = boost::lexical_cast<int64_t>(value_str);
  318. } catch (const boost::bad_lexical_cast& ex) {
  319. // Prepare error message here.
  320. std::string data_type_str = "boolean";
  321. if (OptionDataTypeTraits<T>::integer_type) {
  322. data_type_str = "integer";
  323. }
  324. isc_throw(BadDataTypeCast, "unable to do lexical cast to " << data_type_str
  325. << " data type for value " << value_str << ": " << ex.what());
  326. }
  327. // Perform range checks for integer values only (exclude bool values).
  328. if (OptionDataTypeTraits<T>::integer_type) {
  329. if (result > numeric_limits<T>::max() ||
  330. result < numeric_limits<T>::min()) {
  331. isc_throw(BadDataTypeCast, "unable to do lexical cast for value "
  332. << value_str << ". This value is expected to be in the range of "
  333. << numeric_limits<T>::min() << ".." << numeric_limits<T>::max());
  334. }
  335. }
  336. return (static_cast<T>(result));
  337. }
  338. void
  339. OptionDefinition::writeToBuffer(const std::string& value,
  340. const OptionDataType type,
  341. OptionBuffer& buf) const {
  342. // We are going to write value given by value argument to the buffer.
  343. // The actual type of the value is given by second argument. Check
  344. // this argument to determine how to write this value to the buffer.
  345. switch (type) {
  346. case OPT_BINARY_TYPE:
  347. OptionDataTypeUtil::writeBinary(value, buf);
  348. return;
  349. case OPT_BOOLEAN_TYPE:
  350. // We encode the true value as 1 and false as 0 on 8 bits.
  351. // That way we actually waste 7 bits but it seems to be the
  352. // simpler way to encode boolean.
  353. // @todo Consider if any other encode methods can be used.
  354. OptionDataTypeUtil::writeBool(lexicalCastWithRangeCheck<bool>(value), buf);
  355. return;
  356. case OPT_INT8_TYPE:
  357. OptionDataTypeUtil::writeInt<uint8_t>(lexicalCastWithRangeCheck<int8_t>(value),
  358. buf);
  359. return;
  360. case OPT_INT16_TYPE:
  361. OptionDataTypeUtil::writeInt<uint16_t>(lexicalCastWithRangeCheck<int16_t>(value),
  362. buf);
  363. return;
  364. case OPT_INT32_TYPE:
  365. OptionDataTypeUtil::writeInt<uint32_t>(lexicalCastWithRangeCheck<int32_t>(value),
  366. buf);
  367. return;
  368. case OPT_UINT8_TYPE:
  369. OptionDataTypeUtil::writeInt<uint8_t>(lexicalCastWithRangeCheck<uint8_t>(value),
  370. buf);
  371. return;
  372. case OPT_UINT16_TYPE:
  373. OptionDataTypeUtil::writeInt<uint16_t>(lexicalCastWithRangeCheck<uint16_t>(value),
  374. buf);
  375. return;
  376. case OPT_UINT32_TYPE:
  377. OptionDataTypeUtil::writeInt<uint32_t>(lexicalCastWithRangeCheck<uint32_t>(value),
  378. buf);
  379. return;
  380. case OPT_IPV4_ADDRESS_TYPE:
  381. case OPT_IPV6_ADDRESS_TYPE:
  382. {
  383. asiolink::IOAddress address(value);
  384. if (!address.isV4() && !address.isV6()) {
  385. isc_throw(BadDataTypeCast, "provided address " << address.toText()
  386. << " is not a valid IPv4 or IPv6 address.");
  387. }
  388. OptionDataTypeUtil::writeAddress(address, buf);
  389. return;
  390. }
  391. case OPT_STRING_TYPE:
  392. OptionDataTypeUtil::writeString(value, buf);
  393. return;
  394. case OPT_FQDN_TYPE:
  395. OptionDataTypeUtil::writeFqdn(value, buf);
  396. return;
  397. default:
  398. // We hit this point because invalid option data type has been specified
  399. // This may be the case because 'empty' or 'record' data type has been
  400. // specified. We don't throw exception here because it will be thrown
  401. // at the exit point from this function.
  402. ;
  403. }
  404. isc_throw(isc::BadValue, "attempt to write invalid option data field type"
  405. " into the option buffer: " << type);
  406. }
  407. OptionPtr
  408. OptionDefinition::factoryAddrList4(uint16_t type,
  409. OptionBufferConstIter begin,
  410. OptionBufferConstIter end) {
  411. boost::shared_ptr<Option4AddrLst> option(new Option4AddrLst(type, begin, end));
  412. return (option);
  413. }
  414. OptionPtr
  415. OptionDefinition::factoryAddrList6(uint16_t type,
  416. OptionBufferConstIter begin,
  417. OptionBufferConstIter end) {
  418. boost::shared_ptr<Option6AddrLst> option(new Option6AddrLst(type, begin, end));
  419. return (option);
  420. }
  421. OptionPtr
  422. OptionDefinition::factoryEmpty(Option::Universe u, uint16_t type) {
  423. OptionPtr option(new Option(u, type));
  424. return (option);
  425. }
  426. OptionPtr
  427. OptionDefinition::factoryGeneric(Option::Universe u, uint16_t type,
  428. OptionBufferConstIter begin,
  429. OptionBufferConstIter end) {
  430. OptionPtr option(new Option(u, type, begin, end));
  431. return (option);
  432. }
  433. OptionPtr
  434. OptionDefinition::factoryIA6(uint16_t type,
  435. OptionBufferConstIter begin,
  436. OptionBufferConstIter end) {
  437. if (std::distance(begin, end) < Option6IA::OPTION6_IA_LEN) {
  438. isc_throw(isc::OutOfRange, "input option buffer has invalid size, expected "
  439. "at least " << Option6IA::OPTION6_IA_LEN << " bytes");
  440. }
  441. boost::shared_ptr<Option6IA> option(new Option6IA(type, begin, end));
  442. return (option);
  443. }
  444. OptionPtr
  445. OptionDefinition::factoryIAAddr6(uint16_t type,
  446. OptionBufferConstIter begin,
  447. OptionBufferConstIter end) {
  448. if (std::distance(begin, end) < Option6IAAddr::OPTION6_IAADDR_LEN) {
  449. isc_throw(isc::OutOfRange, "input option buffer has invalid size, expected "
  450. " at least " << Option6IAAddr::OPTION6_IAADDR_LEN << " bytes");
  451. }
  452. boost::shared_ptr<Option6IAAddr> option(new Option6IAAddr(type, begin, end));
  453. return (option);
  454. }
  455. } // end of isc::dhcp namespace
  456. } // end of isc namespace