Kea  2.1.7-git
option_definition.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2021 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/ordered_index.hpp>
19 #include <boost/multi_index/sequenced_index.hpp>
20 #include <boost/multi_index_container.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <map>
23 #include <string>
24 
25 namespace isc {
26 namespace dhcp {
27 
30 class InvalidOptionValue : public Exception {
31 public:
32  InvalidOptionValue(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) { };
34 };
35 
38 public:
39  MalformedOptionDefinition(const char* file, size_t line, const char* what) :
40  isc::Exception(file, line, what) { };
41 };
42 
46 public:
47  DuplicateOptionDefinition(const char* file, size_t line, const char* what) :
48  isc::Exception(file, line, what) { };
49 };
50 
53 
55 typedef boost::shared_ptr<OptionDefinition> OptionDefinitionPtr;
56 
64 template<typename T>
65 class OptionInt;
66 
74 template<typename T>
76 
139 public:
140 
142  typedef std::vector<OptionDataType> RecordFieldsCollection;
144  typedef std::vector<OptionDataType>::const_iterator RecordFieldsConstIter;
145 
154  explicit OptionDefinition(const std::string& name,
155  const uint16_t code,
156  const std::string& space,
157  const std::string& type,
158  const bool array_type = false);
159 
168  explicit OptionDefinition(const std::string& name,
169  const uint16_t code,
170  const std::string& space,
171  const OptionDataType type,
172  const bool array_type = false);
173 
189  explicit OptionDefinition(const std::string& name,
190  const uint16_t code,
191  const std::string& space,
192  const std::string& type,
193  const char* encapsulated_space);
194 
210  explicit OptionDefinition(const std::string& name,
211  const uint16_t code,
212  const std::string& space,
213  const OptionDataType type,
214  const char* encapsulated_space);
215 
231  static OptionDefinitionPtr create(const std::string& name,
232  const uint16_t code,
233  const std::string& space,
234  const std::string& type,
235  const bool array_type = false);
236 
252  static OptionDefinitionPtr create(const std::string& name,
253  const uint16_t code,
254  const std::string& space,
255  const OptionDataType type,
256  const bool array_type = false);
257 
273  static OptionDefinitionPtr create(const std::string& name,
274  const uint16_t code,
275  const std::string& space,
276  const std::string& type,
277  const char* encapsulated_space);
278 
294  static OptionDefinitionPtr create(const std::string& name,
295  const uint16_t code,
296  const std::string& space,
297  const OptionDataType type,
298  const char* encapsulated_space);
299 
302 
303  bool equals(const OptionDefinition& other) const;
309 
315  bool operator==(const OptionDefinition& other) const {
316  return (equals(other));
317  }
318 
324  bool operator!=(const OptionDefinition& other) const {
325  return (!equals(other));
326  }
328 
335  void addRecordField(const std::string& data_type_name);
336 
343  void addRecordField(const OptionDataType data_type);
344 
351  bool getArrayType() const { return (array_type_); }
352 
356  uint16_t getCode() const { return (code_); }
357 
361  std::string getEncapsulatedSpace() const {
362  return (encapsulated_space_);
363  }
364 
368  std::string getName() const { return (name_); }
369 
373  const RecordFieldsCollection& getRecordFields() const {
374  return (record_fields_);
375  }
376 
380  std::string getOptionSpaceName() const {
381  return (option_space_name_);
382  }
383 
387  OptionDataType getType() const { return (type_); };
388 
391  return (user_context_.getContext());
392  }
393 
397  user_context_.setContext(ctx);
398  }
399 
407  user_context_.contextToElement(map);
408  }
409 
420  void validate() const;
421 
439  OptionPtr optionFactory(Option::Universe u, uint16_t type,
440  OptionBufferConstIter begin,
441  OptionBufferConstIter end) const;
442 
459  OptionPtr optionFactory(Option::Universe u, uint16_t type,
460  const OptionBuffer& buf = OptionBuffer()) const;
461 
486  OptionPtr optionFactory(Option::Universe u, uint16_t type,
487  const std::vector<std::string>& values) const;
488 
499  static OptionPtr factoryAddrList4(uint16_t type,
500  OptionBufferConstIter begin,
502 
513  static OptionPtr factoryAddrList6(uint16_t type,
514  OptionBufferConstIter begin,
516 
521  static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
522 
529  static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
530  OptionBufferConstIter begin,
532 
541  static OptionPtr factoryIA6(uint16_t type,
542  OptionBufferConstIter begin,
544 
553  static OptionPtr factoryIAAddr6(uint16_t type,
554  OptionBufferConstIter begin,
556 
565  static OptionPtr factoryIAPrefix6(uint16_t type,
566  OptionBufferConstIter begin,
568 
579  static OptionPtr factoryOpaqueDataTuples(Option::Universe u,
580  uint16_t type,
581  OptionBufferConstIter begin,
583 
596  template<typename T>
597  static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
598  const std::string& encapsulated_space,
599  OptionBufferConstIter begin,
600  OptionBufferConstIter end) {
601  OptionPtr option(new OptionInt<T>(u, type, 0));
602  option->setEncapsulatedSpace(encapsulated_space);
603  option->unpack(begin, end);
604  return (option);
605  }
606 
616  template<typename T>
618  uint16_t type,
619  OptionBufferConstIter begin,
620  OptionBufferConstIter end) {
621  OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
622  return (option);
623  }
624 
625 private:
626 
628  bool haveCompressedFqdnListFormat() const;
629 
639  OptionPtr factoryFqdnList(Option::Universe u,
640  OptionBufferConstIter begin,
641  OptionBufferConstIter end) const;
642 
659  OptionPtr factorySpecialFormatOption(Option::Universe u,
660  OptionBufferConstIter begin,
661  OptionBufferConstIter end) const;
662 
666  inline bool haveType(const OptionDataType type) const {
667  return (type == type_);
668  }
669 
673  inline bool haveSpace(const std::string& space) const {
674  return (space == option_space_name_);
675  }
676 
695  bool convertToBool(const std::string& value_str) const;
696 
709  template<typename T>
710  T lexicalCastWithRangeCheck(const std::string& value_str) const;
711 
728  void writeToBuffer(Option::Universe u, const std::string& value,
729  const OptionDataType type, OptionBuffer& buf) const;
730 
732  std::string name_;
734  uint16_t code_;
736  OptionDataType type_;
738  bool array_type_;
740  std::string encapsulated_space_;
742  RecordFieldsCollection record_fields_;
744  data::UserContext user_context_;
746  std::string option_space_name_;
747 };
748 
749 
764 typedef boost::multi_index_container<
765  // Container comprises elements of OptionDefinition type.
767  // Here we start enumerating various indexes.
768  boost::multi_index::indexed_by<
769  // Sequenced index allows accessing elements in the same way
770  // as elements in std::list. Sequenced is an index #0.
771  boost::multi_index::sequenced<>,
772  // Start definition of index #1.
773  boost::multi_index::hashed_non_unique<
774  // Use option type as the index key. The type is held
775  // in OptionDefinition object so we have to call
776  // OptionDefinition::getCode to retrieve this key
777  // for each element. The option code is non-unique so
778  // multiple elements with the same option code can
779  // be returned by this index.
780  boost::multi_index::const_mem_fun<
782  uint16_t,
784  >
785  >,
786  // Start definition of index #2
787  boost::multi_index::hashed_non_unique<
788  // Use option name as the index key. This value is
789  // returned by the @c OptionDefinition::getName
790  // method.
791  boost::multi_index::const_mem_fun<
792  OptionDefinition,
793  std::string,
795  >
796  >,
797  // Start definition of index #3
798  boost::multi_index::ordered_non_unique<
799  // Use option definition modification time as the index key.
800  // This value is returned by the BaseStampedElement::getModificationTime
801  boost::multi_index::const_mem_fun<
803  boost::posix_time::ptime,
805  >
806  >,
807  // Start definition of index #4.
808  // Use StampedElement::getId as a key.
809  boost::multi_index::hashed_non_unique<
810  boost::multi_index::tag<OptionIdIndexTag>,
811  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
813  >
814  >
816 
818 typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
819 
821 typedef std::map<std::string, OptionDefContainerPtr> OptionDefContainers;
822 
824 typedef std::map<uint32_t, OptionDefContainerPtr> VendorOptionDefContainers;
825 
827 typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
832 typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
833  OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
834 
836 typedef OptionDefContainer::nth_index<2>::type OptionDefContainerNameIndex;
841 typedef std::pair<OptionDefContainerNameIndex::const_iterator,
842  OptionDefContainerNameIndex::const_iterator> OptionDefContainerNameRange;
843 
845 typedef OptionSpaceContainer<
848 
851 public:
852 
861  void addItem(const OptionDefinitionPtr& def) {
862  BaseOptionDefSpaceContainer::addItem(def, def->getOptionSpaceName());
863  }
864 };
865 
866 } // namespace isc::dhcp
867 } // namespace isc
868 
869 #endif // OPTION_DEFINITION_H
Simple container for option spaces holding various items.
This class represents configuration element which is associated with database identifier, modification timestamp and servers.
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.
void addItem(const OptionDefinitionPtr &def)
Adds a new option definition to the container.
MalformedOptionDefinition(const char *file, size_t line, const char *what)
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
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:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
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:24
Forward declaration to OptionInt.
std::map< uint32_t, OptionDefContainerPtr > VendorOptionDefContainers
Container that holds various vendor option containers.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
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...
Class of option definition space container.
OptionDataType
Data types of DHCP option fields.
uint16_t getCode() const
Return option code.
Forward declaration to OptionIntArray.
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
uint64_t getId() const
Returns element&#39;s database identifier.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Exception to be thrown when the particular option definition duplicates existing option definition...
boost::shared_ptr< OptionDefContainer > OptionDefContainerPtr
Pointer to an option definition container.
OptionSpaceContainer< OptionDefContainer, OptionDefinitionPtr, std::string > BaseOptionDefSpaceContainer
Base type of option definition space container.
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:30
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-lfc.
uint16_t code_
std::string getOptionSpaceName() const
Returns option space name.
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
void addItem(const ItemType &item, const Selector &option_space)
Adds a new item to the option_space.
const Name & name_
Definition: dns/message.cc:693
This class represents configuration element which is associated with database identifier and the modi...
InvalidOptionValue(const char *file, size_t line, const char *what)
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
std::vector< OptionDataType >::const_iterator RecordFieldsConstIter
Const iterator for record data fields.
DuplicateOptionDefinition(const char *file, size_t line, const char *what)
bool getArrayType() const
Return array type indicator.
OptionDataType getType() const
Return option data type.
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.
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.