Kea  1.5.0
option_definition.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 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 
7 #ifndef OPTION_DEFINITION_H
8 #define OPTION_DEFINITION_H
9 
10 #include <dhcp/option.h>
11 #include <dhcp/option_data_types.h>
13 #include <cc/stamped_element.h>
14 #include <cc/user_context.h>
15 
16 #include <boost/multi_index/hashed_index.hpp>
17 #include <boost/multi_index/mem_fun.hpp>
18 #include <boost/multi_index/sequenced_index.hpp>
19 #include <boost/multi_index_container.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <map>
22 #include <string>
23 
24 namespace isc {
25 namespace dhcp {
26 
29 class InvalidOptionValue : public Exception {
30 public:
31  InvalidOptionValue(const char* file, size_t line, const char* what) :
32  isc::Exception(file, line, what) { };
33 };
34 
37 public:
38  MalformedOptionDefinition(const char* file, size_t line, const char* what) :
39  isc::Exception(file, line, what) { };
40 };
41 
45 public:
46  DuplicateOptionDefinition(const char* file, size_t line, const char* what) :
47  isc::Exception(file, line, what) { };
48 };
49 
52 
54 typedef boost::shared_ptr<OptionDefinition> OptionDefinitionPtr;
55 
63 template<typename T>
64 class OptionInt;
65 
73 template<typename T>
75 
138 public:
139 
141  typedef std::vector<OptionDataType> RecordFieldsCollection;
143  typedef std::vector<OptionDataType>::const_iterator RecordFieldsConstIter;
144 
152  explicit OptionDefinition(const std::string& name,
153  const uint16_t code,
154  const std::string& type,
155  const bool array_type = false);
156 
164  explicit OptionDefinition(const std::string& name,
165  const uint16_t code,
166  const OptionDataType type,
167  const bool array_type = false);
168 
183  explicit OptionDefinition(const std::string& name,
184  const uint16_t code,
185  const std::string& type,
186  const char* encapsulated_space);
187 
202  explicit OptionDefinition(const std::string& name,
203  const uint16_t code,
204  const OptionDataType type,
205  const char* encapsulated_space);
206 
207 
210 
211  bool equals(const OptionDefinition& other) const;
217 
223  bool operator==(const OptionDefinition& other) const {
224  return (equals(other));
225  }
226 
232  bool operator!=(const OptionDefinition& other) const {
233  return (!equals(other));
234  }
236 
243  void addRecordField(const std::string& data_type_name);
244 
251  void addRecordField(const OptionDataType data_type);
252 
259  bool getArrayType() const { return (array_type_); }
260 
264  uint16_t getCode() const { return (code_); }
265 
269  std::string getEncapsulatedSpace() const {
270  return (encapsulated_space_);
271  }
272 
276  std::string getName() const { return (name_); }
277 
282  return (record_fields_);
283  }
284 
288  OptionDataType getType() const { return (type_); };
289 
292  return (user_context_.getContext());
293  }
294 
298  user_context_.setContext(ctx);
299  }
300 
308  user_context_.contextToElement(map);
309  }
310 
324  std::string getOptionSpaceName() const {
325  return (option_space_name_);
326  }
327 
333  void setOptionSpaceName(const std::string& option_space_name) {
334  option_space_name_ = option_space_name;
335  }
336 
347  void validate() const;
348 
352  bool haveIA6Format() const;
353 
357  bool haveIAAddr6Format() const;
358 
362  bool haveIAPrefix6Format() const;
363 
368  bool haveClientFqdnFormat() const;
369 
383  bool haveFqdn4Format() const;
384 
395  bool haveVendor4Format() const;
396 
405  bool haveVendor6Format() const;
406 
410  bool haveVendorClass4Format() const;
411 
415  bool haveVendorClass6Format() const;
416 
424  bool haveServiceScopeFormat() const;
425 
429  bool haveStatusCodeFormat() const;
430 
434  bool haveOpaqueDataTuplesFormat() const;
435 
437  bool haveCompressedFqdnListFormat() const;
438 
456  OptionPtr optionFactory(Option::Universe u, uint16_t type,
457  OptionBufferConstIter begin,
458  OptionBufferConstIter end) const;
459 
476  OptionPtr optionFactory(Option::Universe u, uint16_t type,
477  const OptionBuffer& buf = OptionBuffer()) const;
478 
503  OptionPtr optionFactory(Option::Universe u, uint16_t type,
504  const std::vector<std::string>& values) const;
505 
516  static OptionPtr factoryAddrList4(uint16_t type,
517  OptionBufferConstIter begin,
519 
530  static OptionPtr factoryAddrList6(uint16_t type,
531  OptionBufferConstIter begin,
533 
538  static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
539 
546  static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
547  OptionBufferConstIter begin,
549 
558  static OptionPtr factoryIA6(uint16_t type,
559  OptionBufferConstIter begin,
561 
570  static OptionPtr factoryIAAddr6(uint16_t type,
571  OptionBufferConstIter begin,
573 
582  static OptionPtr factoryIAPrefix6(uint16_t type,
583  OptionBufferConstIter begin,
585 
597  uint16_t type,
598  OptionBufferConstIter begin,
600 
613  template<typename T>
614  static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
615  const std::string& encapsulated_space,
616  OptionBufferConstIter begin,
617  OptionBufferConstIter end) {
618  OptionPtr option(new OptionInt<T>(u, type, 0));
619  option->setEncapsulatedSpace(encapsulated_space);
620  option->unpack(begin, end);
621  return (option);
622  }
623 
633  template<typename T>
635  uint16_t type,
636  OptionBufferConstIter begin,
637  OptionBufferConstIter end) {
638  OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
639  return (option);
640  }
641 
642 private:
643 
653  OptionPtr factoryFqdnList(Option::Universe u,
654  OptionBufferConstIter begin,
655  OptionBufferConstIter end) const;
656 
673  OptionPtr factorySpecialFormatOption(Option::Universe u,
674  OptionBufferConstIter begin,
675  OptionBufferConstIter end) const;
676 
686  bool haveIAx6Format(const OptionDataType first_type) const;
687 
691  inline bool haveType(const OptionDataType type) const {
692  return (type == type_);
693  }
694 
713  bool convertToBool(const std::string& value_str) const;
714 
727  template<typename T>
728  T lexicalCastWithRangeCheck(const std::string& value_str) const;
729 
746  void writeToBuffer(Option::Universe u, const std::string& value,
747  const OptionDataType type, OptionBuffer& buf) const;
748 
750  std::string name_;
752  uint16_t code_;
754  OptionDataType type_;
756  bool array_type_;
758  std::string encapsulated_space_;
760  RecordFieldsCollection record_fields_;
762  data::UserContext user_context_;
764  std::string option_space_name_;
765 };
766 
767 
782 typedef boost::multi_index_container<
783  // Container comprises elements of OptionDefinition type.
785  // Here we start enumerating various indexes.
786  boost::multi_index::indexed_by<
787  // Sequenced index allows accessing elements in the same way
788  // as elements in std::list. Sequenced is an index #0.
789  boost::multi_index::sequenced<>,
790  // Start definition of index #1.
791  boost::multi_index::hashed_non_unique<
792  // Use option type as the index key. The type is held
793  // in OptionDefinition object so we have to call
794  // OptionDefinition::getCode to retrieve this key
795  // for each element. The option code is non-unique so
796  // multiple elements with the same option code can
797  // be returned by this index.
798  boost::multi_index::const_mem_fun<
799  OptionDefinition,
800  uint16_t,
802  >
803  >,
804  // Start definition of index #2
805  boost::multi_index::hashed_non_unique<
806  // Use option name as the index key. This value is
807  // returned by the @c OptionDefinition::getName
808  // method.
809  boost::multi_index::const_mem_fun<
810  OptionDefinition,
811  std::string,
813  >
814  >
815  >
817 
819 typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
820 
822 typedef std::map<std::string, OptionDefContainerPtr> OptionDefContainers;
823 
825 typedef std::map<uint32_t, OptionDefContainerPtr> VendorOptionDefContainers;
826 
828 typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
833 typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
834  OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
835 
837 typedef OptionDefContainer::nth_index<2>::type OptionDefContainerNameIndex;
842 typedef std::pair<OptionDefContainerNameIndex::const_iterator,
843  OptionDefContainerNameIndex::const_iterator> OptionDefContainerNameRange;
844 
845 typedef OptionSpaceContainer<
848 
849 
850 } // namespace isc::dhcp
851 } // namespace isc
852 
853 #endif // OPTION_DEFINITION_H
Simple container for option spaces holding various items.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
Definition: user_context.h:30
bool equals(const OptionDefinition &other) const
Check if option definition is equal to other.
This class represents configuration element which is associated with the modification timestamp.
Exception to be thrown when invalid option value has been specified for a particular option definitio...
Base class representing a DHCP option definition.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24
MalformedOptionDefinition(const char *file, size_t line, const char *what)
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
static OptionPtr factoryAddrList4(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with address list.
bool haveVendorClass4Format() const
Check if the option has format of DHCPv4 V-I Vendor Class option.
bool operator!=(const OptionDefinition &other) const
Inequality operator.
Base class for user context.
Definition: user_context.h:22
OptionDefContainer::nth_index< 1 >::type OptionDefContainerTypeIndex
Type of the index #1 - option type.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
OptionDefinition(const std::string &name, const uint16_t code, const std::string &type, const bool array_type=false)
Constructor.
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:67
static OptionPtr factoryIntegerArray(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory function to create option with array of integer values.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
bool haveIA6Format() const
Check if specified format is IA_NA option format.
static OptionPtr factoryEmpty(Option::Universe u, uint16_t type)
Empty option factory.
Forward declaration to OptionInt.
std::map< uint32_t, OptionDefContainerPtr > VendorOptionDefContainers
Container that holds various vendor option containers.
static OptionPtr factoryIA6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory for IA-type of option.
OptionPtr optionFactory(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end) const
Option factory.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static OptionPtr factoryOpaqueDataTuples(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with tuple list.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
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.
bool haveClientFqdnFormat() const
Check if specified format is OPTION_CLIENT_FQDN option format.
static OptionPtr factoryIAPrefix6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory for IAPREFIX-type of option.
bool haveIAPrefix6Format() const
Check if specified format is IAPREFIX option format.
OptionDataType
Data types of DHCP option fields.
uint16_t getCode() const
Return option code.
Forward declaration to OptionIntArray.
static OptionPtr factoryAddrList6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with address list.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
Exception to be thrown when the particular option definition duplicates existing option definition.
boost::shared_ptr< OptionDefContainer > OptionDefContainerPtr
Pointer to an option definition container.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
bool haveVendor6Format() const
Check if option has a format of the Vendor-Specific Information Option.
std::string getName() const
Return option name.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:31
std::map< std::string, OptionDefContainerPtr > OptionDefContainers
Container that holds option definitions for various option spaces.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
static OptionPtr factoryGeneric(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create generic option.
std::string getOptionSpaceName() const
Returns option space name.
bool haveVendorClass6Format() const
Check if the option has format of DHCPv6 Vendor Class option.
bool haveIAAddr6Format() const
Check if specified format is IAADDR option format.
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
bool haveFqdn4Format() const
Check if option has format of the DHCPv4 Client FQDN Option.
void setOptionSpaceName(const std::string &option_space_name)
Sets option space name for option definition.
void addRecordField(const std::string &data_type_name)
Adds data field to the record.
void validate() const
Check if the option definition is valid.
InvalidOptionValue(const char *file, size_t line, const char *what)
bool haveServiceScopeFormat() const
Check if option has format of the SLP Service Scope Option.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
std::vector< OptionDataType >::const_iterator RecordFieldsConstIter
Const iterator for record data fields.
OptionSpaceContainer< OptionDefContainer, OptionDefinitionPtr, std::string > OptionDefSpaceContainer
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 > > >> OptionDefContainer
Multi index container for DHCP option definitions.
static OptionPtr factoryIAAddr6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory for IAADDR-type of option.
DuplicateOptionDefinition(const char *file, size_t line, const char *what)
bool getArrayType() const
Return array type indicator.
bool haveOpaqueDataTuplesFormat() const
Check if the option has format of OpaqueDataTuples type options.
OptionDataType getType() const
Return option data type.
bool haveStatusCodeFormat() const
Check if the option has format of DHCPv6 Status Code option.
std::string getEncapsulatedSpace() const
Return name of the encapsulated option space.
static OptionPtr factoryInteger(Option::Universe u, uint16_t type, const std::string &encapsulated_space, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory function to create option with integer value.
bool haveCompressedFqdnListFormat() const
Check if the option has format of CompressedFqdnList options.
bool haveVendor4Format() const
Check if the option has format of Vendor-Identifying Vendor Specific Options.
OptionDefContainer::nth_index< 2 >::type OptionDefContainerNameIndex
Type of the index #2 - option name.
std::pair< OptionDefContainerNameIndex::const_iterator, OptionDefContainerNameIndex::const_iterator > OptionDefContainerNameRange
Pair of iterators to represent the range of options definitions having the same option name.
bool operator==(const OptionDefinition &other) const
Equality operator.
Exception to be thrown when option definition is invalid.