25#include <boost/lexical_cast.hpp>
26#include <boost/shared_array.hpp>
27#include <boost/shared_ptr.hpp>
45 { STANDARD_V4_OPTION_DEFINITIONS, STANDARD_V4_OPTION_DEFINITIONS_SIZE,
DHCP4_OPTION_SPACE },
46 { STANDARD_V6_OPTION_DEFINITIONS, STANDARD_V6_OPTION_DEFINITIONS_SIZE,
DHCP6_OPTION_SPACE },
66map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
69map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
94bool LibDHCP::initialized_ = LibDHCP::initOptionDefs();
97LibDHCP::getOptionDefs(
const string& space) {
98 auto const& container = option_defs_.find(space);
99 if (container != option_defs_.end()) {
100 return (container->second);
115 }
else if (ENTERPRISE_ID_ISC == vendor_id) {
128 if (range.first != range.second) {
129 return (*range.first);
140 if (range.first != range.second) {
141 return (*range.first);
149 const string& name) {
158 if (range.first != range.second) {
159 return (*range.first);
167 const uint16_t code) {
179 if (range.first != range.second) {
180 return (*range.first);
191 if (range.first != range.second) {
192 return (*range.first);
203 if (range.first != range.second) {
204 return (*range.first);
212 return (runtime_option_defs_.getValue().getItems(space));
219 for (
auto const& name : option_space_names) {
221 for (
auto const& def : *container) {
226 runtime_option_defs_ = defs_copy;
231 runtime_option_defs_.reset();
236 runtime_option_defs_.revert();
241 runtime_option_defs_.commit();
249 if (range.first != range.second) {
250 return (*range.first);
261 if (range.first != range.second) {
262 return (*range.first);
281 ((code >= 224) && (code <= 254))));
288 FactoryMap::iterator it;
290 it = v4factories_.find(type);
291 if (it == v4factories_.end()) {
293 "for DHCP v4 option type " << type);
296 it = v6factories_.find(type);
297 if (it == v6factories_.end()) {
299 "for DHCPv6 option type " << type);
303 "Option::V4 or Option::V6");
305 return (it->second(u, type, buf));
311 size_t* relay_msg_offset ,
312 size_t* relay_msg_len ) {
314 size_t length = buf.size();
315 size_t last_offset = 0;
334 while (offset < length) {
336 last_offset = offset;
339 if (offset + 4 > length) {
341 return (last_offset);
345 uint16_t opt_type =
readUint16(&buf[offset], 2);
348 uint16_t opt_len =
readUint16(&buf[offset], 2);
351 if (offset + opt_len > length) {
360 return (last_offset);
363 if (opt_type ==
D6O_RELAY_MSG && relay_msg_offset && relay_msg_len) {
365 *relay_msg_offset = offset;
366 *relay_msg_len = opt_len;
374 if (offset + 4 > length) {
378 return (last_offset);
383 buf.begin() + offset + opt_len));
384 options.insert(std::make_pair(opt_type, vendor_opt));
401 range = idx.equal_range(opt_type);
402 num_defs = std::distance(range.first, range.second);
408 range = runtime_idx.equal_range(opt_type);
409 num_defs = std::distance(range.first, range.second);
416 " definitions for option type " << opt_type <<
417 " returned. Currently it is not supported to initialize"
418 " multiple option definitions for the same option code."
419 " This will be supported once support for option spaces"
421 }
else if (num_defs == 0) {
428 buf.begin() + offset,
429 buf.begin() + offset + opt_len));
436 opt = def->optionFactory(
Option::V6, opt_type,
437 buf.begin() + offset,
438 buf.begin() + offset + opt_len);
446 options.insert(std::make_pair(opt_type, opt));
452 last_offset = offset;
453 return (last_offset);
461 size_t last_offset = 0;
478 bool flex_pad = (check && (runtime_idx.count(
DHO_PAD) == 0));
479 bool flex_end = (check && (runtime_idx.count(
DHO_END) == 0));
483 while (offset < buf.size()) {
485 last_offset = offset;
488 uint8_t opt_type = buf[offset++];
493 if ((opt_type ==
DHO_END) && (space_is_dhcp4 || flex_end)) {
497 return (last_offset);
504 if ((opt_type ==
DHO_PAD) && (space_is_dhcp4 || flex_pad)) {
508 if (offset + 1 > buf.size()) {
516 return (last_offset);
519 uint8_t opt_len = buf[offset++];
520 if (offset + opt_len > buf.size()) {
525 return (last_offset);
533 if (space_is_dhcp4 && opt_len == 0 && opt_type ==
DHO_HOST_NAME) {
549 range = idx.equal_range(opt_type);
550 num_defs = std::distance(range.first, range.second);
556 range = runtime_idx.equal_range(opt_type);
557 num_defs = std::distance(range.first, range.second);
565 for (
auto const& existing : deferred) {
566 if (existing == opt_type) {
572 deferred.push_back(opt_type);
576 if (space_is_dhcp4 &&
586 " definitions for option type " <<
587 static_cast<int>(opt_type) <<
588 " returned. Currently it is not supported to initialize"
589 " multiple option definitions for the same option code."
590 " This will be supported once support for option spaces"
592 }
else if (num_defs == 0) {
594 buf.begin() + offset,
595 buf.begin() + offset + opt_len));
603 opt = def->optionFactory(
Option::V4, opt_type,
604 buf.begin() + offset,
605 buf.begin() + offset + opt_len);
613 options.insert(std::make_pair(opt_type, opt));
618 last_offset = offset;
619 return (last_offset);
628 bool found_suboptions =
false;
630 for (
auto const& option : options) {
634 if (sub_options.size()) {
639 if (found_suboptions) {
648 for (
auto const& old_option :
copy) {
649 if (old_option.first == option.first) {
651 data.insert(data.end(), old_option.second->getData().begin(),
652 old_option.second->getData().end());
653 suboptions.insert(old_option.second->getOptions().begin(),
654 old_option.second->getOptions().end());
664 copy.erase(option.first);
667 candidate->getType(), data));
669 new_option->getMutableOptions() = suboptions;
671 copy.insert(make_pair(candidate->getType(), new_option));
681 if ((found <= 1) && !found_suboptions) {
694 typedef vector<OpaqueDataTuple> TuplesCollection;
695 map<uint32_t, TuplesCollection> vendors_tuples;
697 for (
auto it = range.first; it != range.second; ++it) {
699 auto const& data = it->second->getData();
701 while ((size = data.size() - offset) != 0) {
702 if (size <
sizeof(uint32_t)) {
705 "Truncated vendor-class information option"
706 <<
", length=" << size);
708 uint32_t vendor_id =
readUint32(&data[offset], data.size());
713 data.begin() + offset, data.end());
714 vendors_tuples[vendor_id].push_back(tuple);
715 offset += tuple.getTotalLength();
725 if (vendors_tuples.empty()) {
731 for (
auto const& vendor : vendors_tuples) {
732 if (vendor.second.empty()) {
737 for (
size_t i = 0; i < vendor.second.size(); ++i) {
739 vendor_opt->setTuple(0, vendor.second[0]);
741 vendor_opt->addTuple(vendor.second[i]);
754 map<uint32_t, OptionCollection> vendors_data;
756 for (
auto it = range.first; it != range.second; ++it) {
758 auto const& data = it->second->getData();
760 while ((size = data.size() - offset) != 0) {
761 if (size <
sizeof(uint32_t)) {
764 "Truncated vendor-specific information option"
765 <<
", length=" << size);
767 uint32_t vendor_id =
readUint32(&data[offset], data.size());
769 const OptionBuffer vendor_buffer(data.begin() + offset, data.end());
772 vendors_data[vendor_id]);
782 if (vendors_data.empty()) {
788 for (
auto const& vendor : vendors_data) {
790 for (
auto const& option : vendor.second) {
791 vendor_opt->addOption(option.second);
803 extendVivco(options);
804 extendVivso(options);
811 size_t length = buf.size();
822 idx = &(option_defs->get<1>());
827 while (offset < length) {
828 if (offset + 4 > length) {
830 "Vendor option parse failed: truncated header");
833 uint16_t opt_type =
readUint16(&buf[offset], 2);
836 uint16_t opt_len =
readUint16(&buf[offset], 2);
839 if (offset + opt_len > length) {
841 "Vendor option parse failed. Tried to parse "
842 << offset + opt_len <<
" bytes from " << length
843 <<
"-byte long buffer.");
857 idx->equal_range(opt_type);
860 size_t num_defs = std::distance(range.first, range.second);
866 " definitions for option type " << opt_type <<
867 " returned. Currently it is not supported to"
868 " initialize multiple option definitions for the"
869 " same option code. This will be supported once"
870 " support for option spaces is implemented");
871 }
else if (num_defs == 1) {
876 opt = def->optionFactory(
Option::V6, opt_type,
877 buf.begin() + offset,
878 buf.begin() + offset + opt_len);
889 buf.begin() + offset,
890 buf.begin() + offset + opt_len));
895 options.insert(std::make_pair(opt_type, opt));
915 idx = &(option_defs->get<1>());
920 while (offset < buf.size()) {
924 uint8_t data_len = buf[offset++];
926 if (offset + data_len > buf.size()) {
929 "Attempt to parse truncated vendor option");
932 uint8_t offset_end = offset + data_len;
935 while (offset < offset_end) {
936 uint8_t opt_type = buf[offset++];
940 if (offset + 1 > offset_end) {
945 "Attempt to parse truncated vendor option "
946 <<
static_cast<int>(opt_type));
949 uint8_t opt_len = buf[offset++];
950 if (offset + opt_len > offset_end) {
952 "Option parse failed. Tried to parse "
953 << offset + opt_len <<
" bytes from " << buf.size()
954 <<
"-byte long buffer.");
967 idx->equal_range(opt_type);
970 size_t num_defs = std::distance(range.first, range.second);
976 " option definitions for option type "
977 << opt_type <<
" returned. Currently it is"
978 " not supported to initialize multiple option"
979 " definitions for the same option code."
980 " This will be supported once support for"
981 " option spaces is implemented");
982 }
else if (num_defs == 1) {
987 opt = def->optionFactory(
Option::V4, opt_type,
988 buf.begin() + offset,
989 buf.begin() + offset + opt_len);
995 buf.begin() + offset,
996 buf.begin() + offset + opt_len));
999 options.insert(std::make_pair(opt_type, opt));
1011 bool top,
bool check) {
1021 if (x != options.end()) {
1022 x->second->pack(buf, check);
1026 for (
auto const& option : options) {
1028 switch (option.first) {
1035 end = option.second;
1038 option.second->pack(buf, check);
1044 for (
auto const& option : agent) {
1045 option.second->pack(buf, check);
1050 end->pack(buf, check);
1058 bool result =
false;
1064 if (tries == std::numeric_limits<uint16_t>::max()) {
1066 << tries <<
" times.");
1073 for (
auto const& option : options) {
1078 bool updated =
false;
1079 bool found_suboptions =
false;
1094 if (sub_options.size()) {
1098 used + candidate->getHeaderLen());
1113 if (found_suboptions || candidate->len() > (255 - used)) {
1116 scoped_options.push_back(candidate_scoped_options);
1119 copy.erase(option.first);
1129 for (
auto sub_option : candidate->getMutableOptions()) {
1131 candidate->getType(),
1133 data_sub_option->addOption(sub_option.second);
1134 distinct_options.insert(make_pair(candidate->getType(), data_sub_option));
1143 candidate->getType(),
1145 candidate->getData().end())));
1147 data_option->pack(buf,
false);
1148 uint32_t header_len = candidate->getHeaderLen();
1150 if (used >= 255 - header_len) {
1152 << candidate->getType() <<
" after parent already used "
1157 uint8_t len = 255 - header_len - used;
1162 uint32_t size = buf.
getLength() - header_len;
1180 copy.erase(option.first);
1183 uint32_t offset = 0;
1186 for (; offset != size;) {
1190 if (size - offset < len) {
1191 len = size - offset;
1195 const uint8_t* data =
static_cast<const uint8_t*
>(buf.
getData());
1198 candidate->getType(),
1200 data + offset + len)));
1205 copy.insert(make_pair(candidate->getType(), new_option));
1207 }
else if ((candidate->len() > (255 - used)) && size) {
1212 copy.insert(make_pair(candidate->getType(), data_option));
1217 copy.insert(distinct_options.begin(), distinct_options.end());
1237 for (
auto const& option : options) {
1238 option.second->pack(buf);
1248 if (v6factories_.find(opt_type) != v6factories_.end()) {
1250 <<
"for option type " << opt_type);
1252 v6factories_[opt_type] = factory;
1259 if (opt_type == 0) {
1265 if (opt_type > 254) {
1268 if (v4factories_.find(opt_type) != v4factories_.end()) {
1270 <<
"for option type " << opt_type);
1272 v4factories_[opt_type] = factory;
1283LibDHCP::initOptionDefs() {
1284 for (uint32_t i = 0; OPTION_DEF_PARAMS[i].optionDefParams; ++i) {
1285 string space = OPTION_DEF_PARAMS[i].space;
1288 OPTION_DEF_PARAMS[i].optionDefParams,
1289 OPTION_DEF_PARAMS[i].size);
1298 if ((option_space.size() < 8) || (option_space.substr(0,7) !=
"vendor-")) {
1305 string x = option_space.substr(7);
1307 check = boost::lexical_cast<int64_t>(x);
1308 }
catch (
const boost::bad_lexical_cast &) {
1312 if ((check < 0) || (check > std::numeric_limits<uint32_t>::max())) {
1317 return (
static_cast<uint32_t
>(check));
1322 size_t params_size) {
1332 for (
size_t i = 0; i < params_size; ++i) {
1333 string encapsulates(params[i].encapsulates);
1334 if (!encapsulates.empty() && params[i].
array) {
1336 <<
"option with code '" << params[i].code
1337 <<
"' may not encapsulate option space '"
1338 << encapsulates <<
"' because the definition"
1339 <<
" indicates that this option comprises an array"
1347 if (encapsulates.empty()) {
1360 params[i].encapsulates));
1364 for (
size_t rec = 0; rec < params[i].
records_size; ++rec) {
1365 definition->addRecordField(params[i].records[rec]);
1369 definition->validate();
1381 static_cast<void>(defs->push_back(definition));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This is a base class for exceptions thrown from the DNS library module.
A generic exception that is thrown when an unexpected error condition occurs.
static size_t unpackOptions4(const OptionBuffer &buf, const std::string &option_space, isc::dhcp::OptionCollection &options, std::list< uint16_t > &deferred, bool flexible_pad_end=false)
Parses provided buffer as DHCPv4 options and creates Option objects.
static void OptionFactoryRegister(Option::Universe u, uint16_t type, Option::Factory *factory)
Registers factory method that produces options of specific option types.
static size_t unpackVendorOptions6(const uint32_t vendor_id, const OptionBuffer &buf, isc::dhcp::OptionCollection &options)
Parses provided buffer as DHCPv6 vendor options and creates Option objects.
static const OptionDefContainerPtr getOptionDefs(const std::string &space)
Returns collection of option definitions.
static bool shouldDeferOptionUnpack(const std::string &space, const uint16_t code)
Checks if an option unpacking has to be deferred.
static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
static void setRuntimeOptionDefs(const OptionDefSpaceContainer &defs)
Copies option definitions created at runtime.
static OptionDefinitionPtr getOptionDef(const std::string &space, const uint16_t code)
Return the first option definition matching a particular option code.
static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id, const uint16_t code)
Returns vendor option definition for a given vendor-id and code.
static OptionDefContainerPtr getLastResortOptionDefs(const std::string &space)
Returns last resort option definitions for specified option space name.
static OptionDefContainerPtr getRuntimeOptionDefs(const std::string &space)
Returns runtime (non-standard) option definitions for specified option space name.
static void commitRuntimeOptionDefs()
Commits runtime option definitions.
static void clearRuntimeOptionDefs()
Removes runtime option definitions.
static void extendVendorOptions4(isc::dhcp::OptionCollection &options)
Extend vendor options from fused options in multiple OptionVendor or OptionVendorClass options and ad...
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false, bool check=true)
Stores DHCPv4 options in a buffer.
static bool splitOptions4(isc::dhcp::OptionCollection &options, ScopedOptionsCopyContainer &scopedOptions, uint32_t used=0)
Split long options in multiple options with the same option code (RFC3396).
static void revertRuntimeOptionDefs()
Reverts uncommitted changes to runtime option definitions.
static const OptionDefContainerPtr getVendorOptionDefs(Option::Universe u, const uint32_t vendor_id)
Returns option definitions for given universe and vendor.
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
static OptionDefinitionPtr getRuntimeOptionDef(const std::string &space, const uint16_t code)
Returns runtime (non-standard) option definition by space and option code.
static size_t unpackOptions6(const OptionBuffer &buf, const std::string &option_space, isc::dhcp::OptionCollection &options, size_t *relay_msg_offset=0, size_t *relay_msg_len=0)
Parses provided buffer as DHCPv6 options and creates Option objects.
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
static OptionDefinitionPtr getLastResortOptionDef(const std::string &space, const uint16_t code)
Returns last resort option definition by space and option code.
static bool fuseOptions4(isc::dhcp::OptionCollection &options)
Fuse multiple options with the same option code in long options (RFC3396).
static size_t unpackVendorOptions4(const uint32_t vendor_id, const OptionBuffer &buf, isc::dhcp::OptionCollection &options)
Parses provided buffer as DHCPv4 vendor options and creates Option objects.
Exception to be thrown when the operation on OpaqueDataTuple object results in an error.
Represents a single instance of the opaque data preceded by length.
Class of option definition space container.
void addItem(const OptionDefinitionPtr &def)
Adds a new option definition to the container.
Base class representing a DHCP option definition.
std::list< Selector > getOptionSpaceNames() const
Get a list of existing option spaces.
ItemsContainerPtr getItems(const Selector &option_space) const
Get all items for the particular option space.
This class encapsulates DHCPv6 Vendor Class and DHCPv4 V-I Vendor Class options.
This class represents vendor-specific information option.
Universe
defines option universe DHCPv4 or DHCPv6
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Exception thrown during option unpacking This exception is thrown when an error has occurred,...
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
size_t getLength() const
Return the length of data written in the buffer.
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
This class implements set/commit mechanism for a single object.
#define DOCSIS3_V6_OPTION_SPACE
#define VENDOR_ID_CABLE_LABS
#define DOCSIS3_V4_OPTION_SPACE
global docsis3 option spaces
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
void initOptionSpace(OptionDefContainerPtr &defs, const OptionDefParams *params, size_t params_size)
const OptionDefContainerPtr null_option_def_container_(new OptionDefContainer())
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
boost::shared_ptr< OptionVendor > OptionVendorPtr
Pointer to a vendor option.
@ DHO_VENDOR_ENCAPSULATED_OPTIONS
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
const OptionDefParams DOCSIS3_V4_OPTION_DEFINITIONS[]
Definitions of standard DHCPv4 options.
const char * DOCSIS3_CLASS_EROUTER
The class as specified in vendor-class option by the devices.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
const int DOCSIS3_V6_OPTION_DEFINITIONS_SIZE
Number of option definitions defined.
std::pair< OptionDefContainerNameIndex::const_iterator, OptionDefContainerNameIndex::const_iterator > OptionDefContainerNameRange
Pair of iterators to represent the range of options definitions having the same option name.
const char * DOCSIS3_CLASS_MODEM
DOCSIS3.0 compatible cable modem.
boost::shared_ptr< OptionVendorClass > OptionVendorClassPtr
Defines a pointer to the OptionVendorClass.
std::map< std::string, OptionDefContainerPtr > OptionDefContainers
Container that holds option definitions for various option spaces.
std::shared_ptr< ScopedSubOptionsCopy > ScopedOptionsCopyPtr
A pointer to a ScopedSubOptionsCopy object.
OptionDefContainer::nth_index< 2 >::type OptionDefContainerNameIndex
Type of the index #2 - option name.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
std::pair< OptionDefContainerTypeIndex::const_iterator, OptionDefContainerTypeIndex::const_iterator > OptionDefContainerTypeRange
Pair of iterators to represent the range of options definitions having the same option type value.
boost::multi_index_container< OptionDefinitionPtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< boost::multi_index::const_mem_fun< OptionDefinition, uint16_t, &OptionDefinition::getCode > >, boost::multi_index::hashed_non_unique< boost::multi_index::const_mem_fun< OptionDefinition, std::string, &OptionDefinition::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::StampedElement::getModificationTime > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< OptionIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > > > > OptionDefContainer
Multi index container for DHCP option definitions.
const int DOCSIS3_V4_OPTION_DEFINITIONS_SIZE
Number of option definitions defined.
OptionDefContainer::nth_index< 1 >::type OptionDefContainerTypeIndex
Type of the index #1 - option type.
boost::shared_ptr< Option > OptionPtr
std::vector< ScopedOptionsCopyPtr > ScopedOptionsCopyContainer
A container of ScopedOptionsCopyPtr objects.
const OptionDefParams DOCSIS3_V6_OPTION_DEFINITIONS[]
Definitions of standard DHCPv6 options.
boost::shared_ptr< OptionDefContainer > OptionDefContainerPtr
Pointer to an option definition container.
uint32_t readUint32(const void *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Defines the logger used by the top-level component of kea-lfc.
#define V4V6_BIND_OPTION_SPACE
#define LAST_RESORT_V4_OPTION_SPACE
#define DHCP4_OPTION_SPACE
global std option spaces
#define ISC_V6_OPTION_SPACE
#define V4V6_RULE_OPTION_SPACE
#define MAPE_V6_OPTION_SPACE
#define DHCP_AGENT_OPTION_SPACE
encapsulated option spaces
#define LW_V6_OPTION_SPACE
#define DHCP6_OPTION_SPACE
#define MAPT_V6_OPTION_SPACE
Encapsulation of option definition parameters and the structure size.
Parameters being used to make up an option definition.