libdhcp++.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright (C) 2011-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 <config.h>
  15. #include <dhcp/dhcp4.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/option_vendor.h>
  20. #include <dhcp/option6_ia.h>
  21. #include <dhcp/option6_iaaddr.h>
  22. #include <dhcp/option_definition.h>
  23. #include <dhcp/option_int_array.h>
  24. #include <dhcp/std_option_defs.h>
  25. #include <dhcp/docsis3_option_defs.h>
  26. #include <exceptions/exceptions.h>
  27. #include <util/buffer.h>
  28. #include <dhcp/option_definition.h>
  29. #include <boost/shared_array.hpp>
  30. #include <boost/shared_ptr.hpp>
  31. using namespace std;
  32. using namespace isc::dhcp;
  33. using namespace isc::util;
  34. // static array with factories for options
  35. std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
  36. // static array with factories for options
  37. std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
  38. // Static container with DHCPv4 option definitions.
  39. OptionDefContainer LibDHCP::v4option_defs_;
  40. // Static container with DHCPv6 option definitions.
  41. OptionDefContainer LibDHCP::v6option_defs_;
  42. VendorOptionDefContainers LibDHCP::vendor4_defs_;
  43. VendorOptionDefContainers LibDHCP::vendor6_defs_;
  44. // Let's keep it in .cc file. Moving it to .h would require including optionDefParams
  45. // definitions there
  46. void initOptionSpace(OptionDefContainer& defs,
  47. const OptionDefParams* params,
  48. size_t params_size);
  49. const OptionDefContainer&
  50. LibDHCP::getOptionDefs(const Option::Universe u) {
  51. switch (u) {
  52. case Option::V4:
  53. if (v4option_defs_.empty()) {
  54. initStdOptionDefs4();
  55. initVendorOptsDocsis4();
  56. }
  57. return (v4option_defs_);
  58. case Option::V6:
  59. if (v6option_defs_.empty()) {
  60. initStdOptionDefs6();
  61. initVendorOptsDocsis6();
  62. }
  63. return (v6option_defs_);
  64. default:
  65. isc_throw(isc::BadValue, "invalid universe " << u << " specified");
  66. }
  67. }
  68. const OptionDefContainer*
  69. LibDHCP::getVendorOption4Defs(uint32_t vendor_id) {
  70. if (vendor_id == VENDOR_ID_CABLE_LABS &&
  71. vendor4_defs_.find(VENDOR_ID_CABLE_LABS) == vendor4_defs_.end()) {
  72. initVendorOptsDocsis4();
  73. }
  74. VendorOptionDefContainers::const_iterator def = vendor4_defs_.find(vendor_id);
  75. if (def == vendor4_defs_.end()) {
  76. // No such vendor-id space
  77. return (NULL);
  78. }
  79. return (&(def->second));
  80. }
  81. const OptionDefContainer*
  82. LibDHCP::getVendorOption6Defs(uint32_t vendor_id) {
  83. if (vendor_id == VENDOR_ID_CABLE_LABS &&
  84. vendor6_defs_.find(VENDOR_ID_CABLE_LABS) == vendor6_defs_.end()) {
  85. initVendorOptsDocsis6();
  86. }
  87. VendorOptionDefContainers::const_iterator def = vendor6_defs_.find(vendor_id);
  88. if (def == vendor6_defs_.end()) {
  89. // No such vendor-id space
  90. return (NULL);
  91. }
  92. return (&(def->second));
  93. }
  94. OptionDefinitionPtr
  95. LibDHCP::getOptionDef(const Option::Universe u, const uint16_t code) {
  96. const OptionDefContainer& defs = getOptionDefs(u);
  97. const OptionDefContainerTypeIndex& idx = defs.get<1>();
  98. const OptionDefContainerTypeRange& range = idx.equal_range(code);
  99. if (range.first != range.second) {
  100. return (*range.first);
  101. }
  102. return (OptionDefinitionPtr());
  103. }
  104. OptionDefinitionPtr
  105. LibDHCP::getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id,
  106. const uint16_t code) {
  107. const OptionDefContainer* defs = NULL;
  108. if (u == Option::V4) {
  109. defs = getVendorOption4Defs(vendor_id);
  110. } else if (u == Option::V6) {
  111. defs = getVendorOption6Defs(vendor_id);
  112. }
  113. if (!defs) {
  114. // Weird universe or unknown vendor_id. We don't care. No definitions
  115. // one way or another
  116. // What is it anyway?
  117. return (OptionDefinitionPtr());
  118. }
  119. const OptionDefContainerTypeIndex& idx = defs->get<1>();
  120. const OptionDefContainerTypeRange& range = idx.equal_range(code);
  121. if (range.first != range.second) {
  122. return (*range.first);
  123. }
  124. return (OptionDefinitionPtr());
  125. }
  126. bool
  127. LibDHCP::isStandardOption(const Option::Universe u, const uint16_t code) {
  128. if (u == Option::V6) {
  129. if (code < 79 &&
  130. code != 10 &&
  131. code != 35) {
  132. return (true);
  133. }
  134. } else if (u == Option::V4) {
  135. if (!(code == 84 ||
  136. code == 96 ||
  137. (code > 101 && code < 112) ||
  138. code == 115 ||
  139. code == 126 ||
  140. code == 127 ||
  141. (code > 146 && code < 150) ||
  142. (code > 177 && code < 208) ||
  143. (code > 213 && code < 220) ||
  144. (code > 221 && code < 224))) {
  145. return (true);
  146. }
  147. }
  148. return (false);
  149. }
  150. OptionPtr
  151. LibDHCP::optionFactory(Option::Universe u,
  152. uint16_t type,
  153. const OptionBuffer& buf) {
  154. FactoryMap::iterator it;
  155. if (u == Option::V4) {
  156. it = v4factories_.find(type);
  157. if (it == v4factories_.end()) {
  158. isc_throw(BadValue, "factory function not registered "
  159. "for DHCP v4 option type " << type);
  160. }
  161. } else if (u == Option::V6) {
  162. it = v6factories_.find(type);
  163. if (it == v6factories_.end()) {
  164. isc_throw(BadValue, "factory function not registered "
  165. "for DHCPv6 option type " << type);
  166. }
  167. } else {
  168. isc_throw(BadValue, "invalid universe specified (expected "
  169. "Option::V4 or Option::V6");
  170. }
  171. return (it->second(u, type, buf));
  172. }
  173. size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
  174. isc::dhcp::OptionCollection& options,
  175. size_t* relay_msg_offset /* = 0 */,
  176. size_t* relay_msg_len /* = 0 */) {
  177. size_t offset = 0;
  178. size_t length = buf.size();
  179. // Get the list of stdandard option definitions.
  180. const OptionDefContainer& option_defs = LibDHCP::getOptionDefs(Option::V6);
  181. // Get the search index #1. It allows to search for option definitions
  182. // using option code.
  183. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  184. // The buffer being read comprises a set of options, each starting with
  185. // a two-byte type code and a two-byte length field.
  186. while (offset + 4 <= length) {
  187. uint16_t opt_type = isc::util::readUint16(&buf[offset]);
  188. offset += 2;
  189. uint16_t opt_len = isc::util::readUint16(&buf[offset]);
  190. offset += 2;
  191. if (offset + opt_len > length) {
  192. // @todo: consider throwing exception here.
  193. return (offset);
  194. }
  195. if (opt_type == D6O_RELAY_MSG && relay_msg_offset && relay_msg_len) {
  196. // remember offset of the beginning of the relay-msg option
  197. *relay_msg_offset = offset;
  198. *relay_msg_len = opt_len;
  199. // do not create that relay-msg option
  200. offset += opt_len;
  201. continue;
  202. }
  203. if (opt_type == D6O_VENDOR_OPTS) {
  204. if (offset + 4 > length) {
  205. // Truncated vendor-option. There is expected at least 4 bytes
  206. // long enterprise-id field
  207. return (offset);
  208. }
  209. // Parse this as vendor option
  210. OptionPtr vendor_opt(new OptionVendor(Option::V6, buf.begin() + offset,
  211. buf.begin() + offset + opt_len));
  212. options.insert(std::make_pair(opt_type, vendor_opt));
  213. offset += opt_len;
  214. continue;
  215. }
  216. // Get all definitions with the particular option code. Note that option
  217. // code is non-unique within this container however at this point we
  218. // expect to get one option definition with the particular code. If more
  219. // are returned we report an error.
  220. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  221. // Get the number of returned option definitions for the option code.
  222. size_t num_defs = distance(range.first, range.second);
  223. OptionPtr opt;
  224. if (num_defs > 1) {
  225. // Multiple options of the same code are not supported right now!
  226. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  227. " for option type " << opt_type << " returned. Currently it is not"
  228. " supported to initialize multiple option definitions"
  229. " for the same option code. This will be supported once"
  230. " support for option spaces is implemented");
  231. } else if (num_defs == 0) {
  232. // @todo Don't crash if definition does not exist because only a few
  233. // option definitions are initialized right now. In the future
  234. // we will initialize definitions for all options and we will
  235. // remove this elseif. For now, return generic option.
  236. opt = OptionPtr(new Option(Option::V6, opt_type,
  237. buf.begin() + offset,
  238. buf.begin() + offset + opt_len));
  239. } else {
  240. // The option definition has been found. Use it to create
  241. // the option instance from the provided buffer chunk.
  242. const OptionDefinitionPtr& def = *(range.first);
  243. assert(def);
  244. opt = def->optionFactory(Option::V6, opt_type,
  245. buf.begin() + offset,
  246. buf.begin() + offset + opt_len);
  247. }
  248. // add option to options
  249. options.insert(std::make_pair(opt_type, opt));
  250. offset += opt_len;
  251. }
  252. return (offset);
  253. }
  254. size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
  255. isc::dhcp::OptionCollection& options) {
  256. size_t offset = 0;
  257. // Get the list of stdandard option definitions.
  258. const OptionDefContainer& option_defs = LibDHCP::getOptionDefs(Option::V4);
  259. // Get the search index #1. It allows to search for option definitions
  260. // using option code.
  261. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  262. // The buffer being read comprises a set of options, each starting with
  263. // a one-byte type code and a one-byte length field.
  264. while (offset + 1 <= buf.size()) {
  265. uint8_t opt_type = buf[offset++];
  266. // DHO_END is a special, one octet long option
  267. if (opt_type == DHO_END)
  268. return (offset); // just return. Don't need to add DHO_END option
  269. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  270. // in case we receive a message without DHO_END.
  271. if (opt_type == DHO_PAD)
  272. continue;
  273. if (offset + 1 >= buf.size()) {
  274. // opt_type must be cast to integer so as it is not treated as
  275. // unsigned char value (a number is presented in error message).
  276. isc_throw(OutOfRange, "Attempt to parse truncated option "
  277. << static_cast<int>(opt_type));
  278. }
  279. uint8_t opt_len = buf[offset++];
  280. if (offset + opt_len > buf.size()) {
  281. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  282. << offset + opt_len << " bytes from " << buf.size()
  283. << "-byte long buffer.");
  284. }
  285. // Get all definitions with the particular option code. Note that option code
  286. // is non-unique within this container however at this point we expect
  287. // to get one option definition with the particular code. If more are
  288. // returned we report an error.
  289. const OptionDefContainerTypeRange& range = idx.equal_range(opt_type);
  290. // Get the number of returned option definitions for the option code.
  291. size_t num_defs = distance(range.first, range.second);
  292. OptionPtr opt;
  293. if (num_defs > 1) {
  294. // Multiple options of the same code are not supported right now!
  295. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  296. " for option type " << static_cast<int>(opt_type)
  297. << " returned. Currently it is not supported to initialize"
  298. << " multiple option definitions for the same option code."
  299. << " This will be supported once support for option spaces"
  300. << " is implemented");
  301. } else if (num_defs == 0) {
  302. opt = OptionPtr(new Option(Option::V4, opt_type,
  303. buf.begin() + offset,
  304. buf.begin() + offset + opt_len));
  305. } else {
  306. // The option definition has been found. Use it to create
  307. // the option instance from the provided buffer chunk.
  308. const OptionDefinitionPtr& def = *(range.first);
  309. assert(def);
  310. opt = def->optionFactory(Option::V4, opt_type,
  311. buf.begin() + offset,
  312. buf.begin() + offset + opt_len);
  313. }
  314. options.insert(std::make_pair(opt_type, opt));
  315. offset += opt_len;
  316. }
  317. return (offset);
  318. }
  319. size_t LibDHCP::unpackVendorOptions6(uint32_t vendor_id,
  320. const OptionBuffer& buf,
  321. isc::dhcp::OptionCollection& options) {
  322. size_t offset = 0;
  323. size_t length = buf.size();
  324. // Get the list of option definitions for this particular vendor-id
  325. const OptionDefContainer* option_defs = LibDHCP::getVendorOption6Defs(vendor_id);
  326. // Get the search index #1. It allows to search for option definitions
  327. // using option code. If there's no such vendor-id space, we're out of luck
  328. // anyway.
  329. const OptionDefContainerTypeIndex* idx = NULL;
  330. if (option_defs) {
  331. idx = &(option_defs->get<1>());
  332. }
  333. // The buffer being read comprises a set of options, each starting with
  334. // a two-byte type code and a two-byte length field.
  335. while (offset + 4 <= length) {
  336. uint16_t opt_type = isc::util::readUint16(&buf[offset]);
  337. offset += 2;
  338. uint16_t opt_len = isc::util::readUint16(&buf[offset]);
  339. offset += 2;
  340. if (offset + opt_len > length) {
  341. // @todo: consider throwing exception here.
  342. return (offset);
  343. }
  344. OptionPtr opt;
  345. opt.reset();
  346. // If there is a definition for such a vendor option...
  347. if (idx) {
  348. // Get all definitions with the particular option code. Note that option
  349. // code is non-unique within this container however at this point we
  350. // expect to get one option definition with the particular code. If more
  351. // are returned we report an error.
  352. const OptionDefContainerTypeRange& range = idx->equal_range(opt_type);
  353. // Get the number of returned option definitions for the option code.
  354. size_t num_defs = distance(range.first, range.second);
  355. if (num_defs > 1) {
  356. // Multiple options of the same code are not supported right now!
  357. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  358. " for option type " << opt_type << " returned. Currently it is not"
  359. " supported to initialize multiple option definitions"
  360. " for the same option code. This will be supported once"
  361. " support for option spaces is implemented");
  362. } else if (num_defs == 1) {
  363. // The option definition has been found. Use it to create
  364. // the option instance from the provided buffer chunk.
  365. const OptionDefinitionPtr& def = *(range.first);
  366. assert(def);
  367. opt = def->optionFactory(Option::V6, opt_type,
  368. buf.begin() + offset,
  369. buf.begin() + offset + opt_len);
  370. }
  371. }
  372. // This can happen in one of 2 cases:
  373. // 1. we do not have definitions for that vendor-space
  374. // 2. we do have definitions, but that particular option was not defined
  375. if (!opt) {
  376. opt = OptionPtr(new Option(Option::V6, opt_type,
  377. buf.begin() + offset,
  378. buf.begin() + offset + opt_len));
  379. }
  380. // add option to options
  381. if (opt) {
  382. options.insert(std::make_pair(opt_type, opt));
  383. }
  384. offset += opt_len;
  385. }
  386. return (offset);
  387. }
  388. size_t LibDHCP::unpackVendorOptions4(uint32_t vendor_id, const OptionBuffer& buf,
  389. isc::dhcp::OptionCollection& options) {
  390. size_t offset = 0;
  391. // Get the list of stdandard option definitions.
  392. const OptionDefContainer* option_defs = LibDHCP::getVendorOption4Defs(vendor_id);
  393. // Get the search index #1. It allows to search for option definitions
  394. // using option code.
  395. const OptionDefContainerTypeIndex* idx = NULL;
  396. if (option_defs) {
  397. idx = &(option_defs->get<1>());
  398. }
  399. // The buffer being read comprises a set of options, each starting with
  400. // a one-byte type code and a one-byte length field.
  401. while (offset + 1 <= buf.size()) {
  402. // Note that Vendor-Specific info option (RFC3925) has a different option
  403. // format than Vendor-Spec info for DHCPv6. (there's additional layer of
  404. // data-length
  405. uint8_t data_len = buf[offset++];
  406. if (offset + data_len > buf.size()) {
  407. // Truncated data-option
  408. return (offset);
  409. }
  410. uint8_t offset_end = offset + data_len;
  411. // beginning of data-chunk parser
  412. while (offset + 1 <= offset_end) {
  413. uint8_t opt_type = buf[offset++];
  414. // DHO_END is a special, one octet long option
  415. if (opt_type == DHO_END)
  416. return (offset); // just return. Don't need to add DHO_END option
  417. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  418. // in case we receive a message without DHO_END.
  419. if (opt_type == DHO_PAD)
  420. continue;
  421. if (offset + 1 >= buf.size()) {
  422. // opt_type must be cast to integer so as it is not treated as
  423. // unsigned char value (a number is presented in error message).
  424. isc_throw(OutOfRange, "Attempt to parse truncated option "
  425. << static_cast<int>(opt_type));
  426. }
  427. uint8_t opt_len = buf[offset++];
  428. if (offset + opt_len > buf.size()) {
  429. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  430. << offset + opt_len << " bytes from " << buf.size()
  431. << "-byte long buffer.");
  432. }
  433. OptionPtr opt;
  434. opt.reset();
  435. if (idx) {
  436. // Get all definitions with the particular option code. Note that option code
  437. // is non-unique within this container however at this point we expect
  438. // to get one option definition with the particular code. If more are
  439. // returned we report an error.
  440. const OptionDefContainerTypeRange& range = idx->equal_range(opt_type);
  441. // Get the number of returned option definitions for the option code.
  442. size_t num_defs = distance(range.first, range.second);
  443. if (num_defs > 1) {
  444. // Multiple options of the same code are not supported right now!
  445. isc_throw(isc::Unexpected, "Internal error: multiple option definitions"
  446. " for option type " << static_cast<int>(opt_type)
  447. << " returned. Currently it is not supported to initialize"
  448. << " multiple option definitions for the same option code."
  449. << " This will be supported once support for option spaces"
  450. << " is implemented");
  451. } else if (num_defs == 1) {
  452. // The option definition has been found. Use it to create
  453. // the option instance from the provided buffer chunk.
  454. const OptionDefinitionPtr& def = *(range.first);
  455. assert(def);
  456. opt = def->optionFactory(Option::V4, opt_type,
  457. buf.begin() + offset,
  458. buf.begin() + offset + opt_len);
  459. }
  460. }
  461. if (!opt) {
  462. opt = OptionPtr(new Option(Option::V4, opt_type,
  463. buf.begin() + offset,
  464. buf.begin() + offset + opt_len));
  465. }
  466. options.insert(std::make_pair(opt_type, opt));
  467. offset += opt_len;
  468. } // end of data-chunk
  469. }
  470. return (offset);
  471. }
  472. void
  473. LibDHCP::packOptions(isc::util::OutputBuffer& buf,
  474. const OptionCollection& options) {
  475. for (OptionCollection::const_iterator it = options.begin();
  476. it != options.end(); ++it) {
  477. it->second->pack(buf);
  478. }
  479. }
  480. void LibDHCP::OptionFactoryRegister(Option::Universe u,
  481. uint16_t opt_type,
  482. Option::Factory* factory) {
  483. switch (u) {
  484. case Option::V6: {
  485. if (v6factories_.find(opt_type) != v6factories_.end()) {
  486. isc_throw(BadValue, "There is already DHCPv6 factory registered "
  487. << "for option type " << opt_type);
  488. }
  489. v6factories_[opt_type]=factory;
  490. return;
  491. }
  492. case Option::V4:
  493. {
  494. // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
  495. // instantiated as an Option object, but rather consumed during packet parsing.
  496. if (opt_type == 0) {
  497. isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
  498. }
  499. // Option 255 is never instantiated as an option object. It is special
  500. // (a one-octet equal 255) option that is added at the end of all options
  501. // during packet assembly. It is also silently consumed during packet parsing.
  502. if (opt_type > 254) {
  503. isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
  504. }
  505. if (v4factories_.find(opt_type)!=v4factories_.end()) {
  506. isc_throw(BadValue, "There is already DHCPv4 factory registered "
  507. << "for option type " << opt_type);
  508. }
  509. v4factories_[opt_type]=factory;
  510. return;
  511. }
  512. default:
  513. isc_throw(BadValue, "Invalid universe type specified.");
  514. }
  515. return;
  516. }
  517. void
  518. LibDHCP::initStdOptionDefs4() {
  519. v4option_defs_.clear();
  520. // Now let's add all option definitions.
  521. for (int i = 0; i < OPTION_DEF_PARAMS_SIZE4; ++i) {
  522. std::string encapsulates(OPTION_DEF_PARAMS4[i].encapsulates);
  523. if (!encapsulates.empty() && OPTION_DEF_PARAMS4[i].array) {
  524. isc_throw(isc::BadValue, "invalid standard option definition: "
  525. << "option with code '" << OPTION_DEF_PARAMS4[i].code
  526. << "' may not encapsulate option space '"
  527. << encapsulates << "' because the definition"
  528. << " indicates that this option comprises an array"
  529. << " of values");
  530. }
  531. // Depending whether the option encapsulates an option space or not
  532. // we pick different constructor to create an instance of the option
  533. // definition.
  534. OptionDefinitionPtr definition;
  535. if (encapsulates.empty()) {
  536. // Option does not encapsulate any option space.
  537. definition.reset(new OptionDefinition(OPTION_DEF_PARAMS4[i].name,
  538. OPTION_DEF_PARAMS4[i].code,
  539. OPTION_DEF_PARAMS4[i].type,
  540. OPTION_DEF_PARAMS4[i].array));
  541. } else {
  542. // Option does encapsulate an option space.
  543. definition.reset(new OptionDefinition(OPTION_DEF_PARAMS4[i].name,
  544. OPTION_DEF_PARAMS4[i].code,
  545. OPTION_DEF_PARAMS4[i].type,
  546. OPTION_DEF_PARAMS4[i].encapsulates));
  547. }
  548. for (int rec = 0; rec < OPTION_DEF_PARAMS4[i].records_size; ++rec) {
  549. definition->addRecordField(OPTION_DEF_PARAMS4[i].records[rec]);
  550. }
  551. // Sanity check if the option is valid.
  552. try {
  553. definition->validate();
  554. } catch (const Exception& ex) {
  555. // This is unlikely event that validation fails and may
  556. // be only caused by programming error. To guarantee the
  557. // data consistency we clear all option definitions that
  558. // have been added so far and pass the exception forward.
  559. v4option_defs_.clear();
  560. throw;
  561. }
  562. v4option_defs_.push_back(definition);
  563. }
  564. }
  565. void
  566. LibDHCP::initStdOptionDefs6() {
  567. initOptionSpace(v6option_defs_, OPTION_DEF_PARAMS6, OPTION_DEF_PARAMS_SIZE6);
  568. }
  569. void
  570. LibDHCP::initVendorOptsDocsis4() {
  571. initOptionSpace(vendor4_defs_[VENDOR_ID_CABLE_LABS], DOCSIS3_V4_DEFS, DOCSIS3_V4_DEFS_SIZE);
  572. }
  573. void
  574. LibDHCP::initVendorOptsDocsis6() {
  575. vendor6_defs_[VENDOR_ID_CABLE_LABS] = OptionDefContainer();
  576. initOptionSpace(vendor6_defs_[VENDOR_ID_CABLE_LABS], DOCSIS3_V6_DEFS, DOCSIS3_V6_DEFS_SIZE);
  577. }
  578. void initOptionSpace(OptionDefContainer& defs,
  579. const OptionDefParams* params,
  580. size_t params_size) {
  581. defs.clear();
  582. for (int i = 0; i < params_size; ++i) {
  583. std::string encapsulates(params[i].encapsulates);
  584. if (!encapsulates.empty() && params[i].array) {
  585. isc_throw(isc::BadValue, "invalid standard option definition: "
  586. << "option with code '" << params[i].code
  587. << "' may not encapsulate option space '"
  588. << encapsulates << "' because the definition"
  589. << " indicates that this option comprises an array"
  590. << " of values");
  591. }
  592. // Depending whether an option encapsulates an option space or not
  593. // we pick different constructor to create an instance of the option
  594. // definition.
  595. OptionDefinitionPtr definition;
  596. if (encapsulates.empty()) {
  597. // Option does not encapsulate any option space.
  598. definition.reset(new OptionDefinition(params[i].name,
  599. params[i].code,
  600. params[i].type,
  601. params[i].array));
  602. } else {
  603. // Option does encapsulate an option space.
  604. definition.reset(new OptionDefinition(params[i].name,
  605. params[i].code,
  606. params[i].type,
  607. params[i].encapsulates));
  608. }
  609. for (int rec = 0; rec < params[i].records_size; ++rec) {
  610. definition->addRecordField(params[i].records[rec]);
  611. }
  612. try {
  613. definition->validate();
  614. } catch (const isc::Exception& ex) {
  615. // This is unlikely event that validation fails and may
  616. // be only caused by programming error. To guarantee the
  617. // data consistency we clear all option definitions that
  618. // have been added so far and pass the exception forward.
  619. defs.clear();
  620. throw;
  621. }
  622. defs.push_back(definition);
  623. }
  624. }