Kea: option_definition.h Source File

| Kea 3.2.0-git

option_definition.h
Go to the documentation of this file.
1// Copyright (C) 2012-2026 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>
12#include <dhcp/option_space_container.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
25namespace isc {
26namespace dhcp {
27
30 class InvalidOptionValue : public Exception {
31public:
32 InvalidOptionValue(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) { }
34};
35
37 class MalformedOptionDefinition : public Exception {
38public:
39 MalformedOptionDefinition(const char* file, size_t line, const char* what) :
40 isc::Exception(file, line, what) { }
41};
42
45 class DuplicateOptionDefinition : public Exception {
46public:
47 DuplicateOptionDefinition(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) { }
49};
50
52class OptionDefinition;
53
55 typedef boost::shared_ptr<OptionDefinition> OptionDefinitionPtr;
56
64template<typename T>
65class OptionInt;
66
74template<typename T>
75class OptionIntArray;
76
138 class OptionDefinition : public data::StampedElement {
139public:
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
308 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 }
327
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
390 data::ConstElementPtr getContext() const {
391 return (user_context_.getContext());
392 }
393
396 void setContext(const data::ConstElementPtr& ctx) {
397 user_context_.setContext(ctx);
398 }
399
406 void contextToElement(data::ElementPtr map) const {
407 user_context_.contextToElement(map);
408 }
409
420 void validate() const;
421
444 OptionPtr optionFactory(Option::Universe u,
445 uint16_t type,
446 OptionBufferConstIter begin,
447 OptionBufferConstIter end,
448 bool convenient_notation = false,
449 size_t rec_level = 0) const;
450
467 OptionPtr optionFactory(Option::Universe u, uint16_t type,
468 const OptionBuffer& buf = OptionBuffer()) const;
469
494 OptionPtr optionFactory(Option::Universe u, uint16_t type,
495 const std::vector<std::string>& values) const;
496
507 static OptionPtr factoryAddrList4(uint16_t type,
508 OptionBufferConstIter begin,
509 OptionBufferConstIter end);
510
521 static OptionPtr factoryAddrList6(uint16_t type,
522 OptionBufferConstIter begin,
523 OptionBufferConstIter end);
524
529 static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
530
537 static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
538 OptionBufferConstIter begin,
539 OptionBufferConstIter end);
540
550 static OptionPtr factoryIA6(uint16_t type,
551 OptionBufferConstIter begin,
552 OptionBufferConstIter end,
553 size_t rec_level = 0);
554
564 static OptionPtr factoryIAAddr6(uint16_t type,
565 OptionBufferConstIter begin,
566 OptionBufferConstIter end,
567 size_t rec_level = 0);
568
578 static OptionPtr factoryIAPrefix6(uint16_t type,
579 OptionBufferConstIter begin,
580 OptionBufferConstIter end,
581 size_t rec_level = 0);
582
593 static OptionPtr factoryOpaqueDataTuples(Option::Universe u,
594 uint16_t type,
595 OptionBufferConstIter begin,
596 OptionBufferConstIter end);
597
610 static OptionPtr factoryOpaqueDataTuples(Option::Universe u,
611 uint16_t type,
612 OptionBufferConstIter begin,
613 OptionBufferConstIter end,
614 OpaqueDataTuple::LengthFieldType length_field_type);
615
629 template<typename T>
630 static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
631 const std::string& encapsulated_space,
632 OptionBufferConstIter begin,
633 OptionBufferConstIter end,
634 size_t rec_level = 0) {
635 boost::shared_ptr<OptionInt<T> > option(new OptionInt<T>(u, type, 0));
636 option->setEncapsulatedSpace(encapsulated_space);
637 option->unpack(begin, end, rec_level);
638 return (option);
639 }
640
650 template<typename T>
651 static OptionPtr factoryIntegerArray(Option::Universe u,
652 uint16_t type,
653 OptionBufferConstIter begin,
654 OptionBufferConstIter end) {
655 OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
656 return (option);
657 }
658
659private:
660
662 bool haveCompressedFqdnListFormat() const;
663
673 OptionPtr factoryFqdnList(Option::Universe u,
674 OptionBufferConstIter begin,
675 OptionBufferConstIter end) const;
676
698 OptionPtr factorySpecialFormatOption(Option::Universe u,
699 OptionBufferConstIter begin,
700 OptionBufferConstIter end,
701 bool convenient_notation = false,
702 size_t rec_level = 0) const;
703
707 inline bool haveType(const OptionDataType type) const {
708 return (type == type_);
709 }
710
714 inline bool haveSpace(const std::string& space) const {
715 return (space == option_space_name_);
716 }
717
736 bool convertToBool(const std::string& value_str) const;
737
750 template<typename T>
751 T lexicalCastWithRangeCheck(const std::string& value_str) const;
752
769 void writeToBuffer(Option::Universe u, const std::string& value,
770 const OptionDataType type, OptionBuffer& buf) const;
771
773 std::string name_;
775 uint16_t code_;
777 OptionDataType type_;
779 bool array_type_;
781 std::string encapsulated_space_;
783 RecordFieldsCollection record_fields_;
785 data::UserContext user_context_;
787 std::string option_space_name_;
788};
789
790
805typedef boost::multi_index_container<
806 // Container comprises elements of OptionDefinition type.
807 OptionDefinitionPtr,
808 // Here we start enumerating various indexes.
809 boost::multi_index::indexed_by<
810 // Sequenced index allows accessing elements in the same way
811 // as elements in std::list. Sequenced is an index #0.
812 boost::multi_index::sequenced<>,
813 // Start definition of index #1.
814 boost::multi_index::hashed_non_unique<
815 // Use option type as the index key. The type is held
816 // in OptionDefinition object so we have to call
817 // OptionDefinition::getCode to retrieve this key
818 // for each element. The option code is non-unique so
819 // multiple elements with the same option code can
820 // be returned by this index.
821 boost::multi_index::const_mem_fun<
822 OptionDefinition,
823 uint16_t,
824 &OptionDefinition::getCode
825 >
826 >,
827 // Start definition of index #2
828 boost::multi_index::hashed_non_unique<
829 // Use option name as the index key. This value is
830 // returned by the @c OptionDefinition::getName
831 // method.
832 boost::multi_index::const_mem_fun<
833 OptionDefinition,
834 std::string,
835 &OptionDefinition::getName
836 >
837 >,
838 // Start definition of index #3
839 boost::multi_index::ordered_non_unique<
840 // Use option definition modification time as the index key.
841 // This value is returned by the BaseStampedElement::getModificationTime
842 boost::multi_index::const_mem_fun<
843 data::BaseStampedElement,
844 boost::posix_time::ptime,
845 &data::StampedElement::getModificationTime
846 >
847 >,
848 // Start definition of index #4.
849 // Use StampedElement::getId as a key.
850 boost::multi_index::hashed_non_unique<
851 boost::multi_index::tag<OptionIdIndexTag>,
852 boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
853 &data::BaseStampedElement::getId>
854 >
855 >
856> OptionDefContainer;
857
859 typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
860
862 typedef std::map<std::string, OptionDefContainerPtr> OptionDefContainers;
863
865 typedef std::map<uint32_t, OptionDefContainerPtr> VendorOptionDefContainers;
866
868 typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
873typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
874 OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
875
877 typedef OptionDefContainer::nth_index<2>::type OptionDefContainerNameIndex;
882typedef std::pair<OptionDefContainerNameIndex::const_iterator,
883 OptionDefContainerNameIndex::const_iterator> OptionDefContainerNameRange;
884
886typedef OptionSpaceContainer<
887 OptionDefContainer, OptionDefinitionPtr, std::string
888> BaseOptionDefSpaceContainer;
889
891 class OptionDefSpaceContainer : public BaseOptionDefSpaceContainer {
892public:
893
902 void addItem(const OptionDefinitionPtr& def) {
903 BaseOptionDefSpaceContainer::addItem(def, def->getOptionSpaceName());
904 }
905};
906
907} // namespace isc::dhcp
908} // namespace isc
909
910#endif // OPTION_DEFINITION_H
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::data::BaseStampedElement::getModificationTime
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
Definition base_stamped_element.h:61
isc::data::BaseStampedElement::getId
uint64_t getId() const
Returns element's database identifier.
Definition base_stamped_element.h:46
isc::data::StampedElement
This class represents configuration element which is associated with database identifier,...
Definition stamped_element.h:34
isc::dhcp::DuplicateOptionDefinition::DuplicateOptionDefinition
DuplicateOptionDefinition(const char *file, size_t line, const char *what)
Definition option_definition.h:47
isc::dhcp::InvalidOptionValue::InvalidOptionValue
InvalidOptionValue(const char *file, size_t line, const char *what)
Definition option_definition.h:32
isc::dhcp::MalformedOptionDefinition::MalformedOptionDefinition
MalformedOptionDefinition(const char *file, size_t line, const char *what)
Definition option_definition.h:39
isc::dhcp::OpaqueDataTuple::LengthFieldType
LengthFieldType
Size of the length field in the tuple.
Definition opaque_data_tuple.h:56
isc::dhcp::OptionDefSpaceContainer
Class of option definition space container.
Definition option_definition.h:891
isc::dhcp::OptionDefSpaceContainer::addItem
void addItem(const OptionDefinitionPtr &def)
Adds a new option definition to the container.
Definition option_definition.h:902
isc::dhcp::OptionDefinition
Base class representing a DHCP option definition.
Definition option_definition.h:138
isc::dhcp::OptionDefinition::getCode
uint16_t getCode() const
Return option code.
Definition option_definition.h:356
isc::dhcp::OptionDefinition::operator==
bool operator==(const OptionDefinition &other) const
Equality operator.
Definition option_definition.h:315
isc::dhcp::OptionDefinition::factoryInteger
static OptionPtr factoryInteger(Option::Universe u, uint16_t type, const std::string &encapsulated_space, OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level=0)
Factory function to create option with integer value.
Definition option_definition.h:630
isc::dhcp::OptionDefinition::factoryEmpty
static OptionPtr factoryEmpty(Option::Universe u, uint16_t type)
Empty option factory.
Definition option_definition.cc:762
isc::dhcp::OptionDefinition::factoryIntegerArray
static OptionPtr factoryIntegerArray(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory function to create option with array of integer values.
Definition option_definition.h:651
isc::dhcp::OptionDefinition::OptionDefinition
OptionDefinition(const std::string &name, const uint16_t code, const std::string &space, const std::string &type, const bool array_type=false)
Constructor.
Definition option_definition.cc:48
isc::dhcp::OptionDefinition::factoryIAPrefix6
static OptionPtr factoryIAPrefix6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level=0)
Factory for IAPREFIX-type of option.
Definition option_definition.cc:805
isc::dhcp::OptionDefinition::operator!=
bool operator!=(const OptionDefinition &other) const
Inequality operator.
Definition option_definition.h:324
isc::dhcp::OptionDefinition::getType
OptionDataType getType() const
Return option data type.
Definition option_definition.h:387
isc::dhcp::OptionDefinition::factoryIA6
static OptionPtr factoryIA6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level=0)
Factory for IA-type of option.
Definition option_definition.cc:776
isc::dhcp::OptionDefinition::factoryAddrList6
static OptionPtr factoryAddrList6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with address list.
Definition option_definition.cc:752
isc::dhcp::OptionDefinition::optionFactory
OptionPtr optionFactory(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end, bool convenient_notation=false, size_t rec_level=0) const
Option factory.
Definition option_definition.cc:186
isc::dhcp::OptionDefinition::factoryAddrList4
static OptionPtr factoryAddrList4(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with address list.
Definition option_definition.cc:743
isc::dhcp::OptionDefinition::setContext
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
Definition option_definition.h:396
isc::dhcp::OptionDefinition::getOptionSpaceName
std::string getOptionSpaceName() const
Returns option space name.
Definition option_definition.h:380
isc::dhcp::OptionDefinition::RecordFieldsConstIter
std::vector< OptionDataType >::const_iterator RecordFieldsConstIter
Const iterator for record data fields.
Definition option_definition.h:144
isc::dhcp::OptionDefinition::getContext
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition option_definition.h:390
isc::dhcp::OptionDefinition::factoryIAAddr6
static OptionPtr factoryIAAddr6(uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level=0)
Factory for IAADDR-type of option.
Definition option_definition.cc:790
isc::dhcp::OptionDefinition::addRecordField
void addRecordField(const std::string &data_type_name)
Adds data field to the record.
Definition option_definition.cc:161
isc::dhcp::OptionDefinition::equals
bool equals(const OptionDefinition &other) const
Check if option definition is equal to other.
Definition option_definition.cc:150
isc::dhcp::OptionDefinition::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition option_definition.h:406
isc::dhcp::OptionDefinition::getName
std::string getName() const
Return option name.
Definition option_definition.h:368
isc::dhcp::OptionDefinition::getRecordFields
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
Definition option_definition.h:373
isc::dhcp::OptionDefinition::validate
void validate() const
Check if the option definition is valid.
Definition option_definition.cc:373
isc::dhcp::OptionDefinition::RecordFieldsCollection
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
Definition option_definition.h:142
isc::dhcp::OptionDefinition::getEncapsulatedSpace
std::string getEncapsulatedSpace() const
Return name of the encapsulated option space.
Definition option_definition.h:361
isc::dhcp::OptionDefinition::create
static OptionDefinitionPtr create(const std::string &name, const uint16_t code, const std::string &space, const std::string &type, const bool array_type=false)
Factory function creating an instance of the OptionDefinition.
Definition option_definition.cc:114
isc::dhcp::OptionDefinition::factoryOpaqueDataTuples
static OptionPtr factoryOpaqueDataTuples(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create option with tuple list.
Definition option_definition.cc:821
isc::dhcp::OptionDefinition::getArrayType
bool getArrayType() const
Return array type indicator.
Definition option_definition.h:351
isc::dhcp::OptionDefinition::factoryGeneric
static OptionPtr factoryGeneric(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Factory to create generic option.
Definition option_definition.cc:768
isc::dhcp::OptionIntArray
Forward declaration to OptionIntArray.
Definition option_int_array.h:56
isc::dhcp::OptionInt
Forward declaration to OptionInt.
Definition option_int.h:49
isc::dhcp::OptionSpaceContainer
Simple container for option spaces holding various items.
Definition option_space_container.h:32
isc::dhcp::OptionSpaceContainer< OptionDefContainer, OptionDefinitionPtr, std::string >::addItem
void addItem(const OptionDefinitionPtr &item, const std::string &option_space)
Definition option_space_container.h:49
isc::dhcp::Option::Universe
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
isc::dhcp::BaseOptionDefSpaceContainer
OptionSpaceContainer< OptionDefContainer, OptionDefinitionPtr, std::string > BaseOptionDefSpaceContainer
Base type of option definition space container.
Definition option_definition.h:888
isc::dhcp::VendorOptionDefContainers
std::map< uint32_t, OptionDefContainerPtr > VendorOptionDefContainers
Container that holds various vendor option containers.
Definition option_definition.h:865
isc::dhcp::OptionDefinitionPtr
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
Definition option_definition.h:55
isc::dhcp::OptionDefContainerNameRange
std::pair< OptionDefContainerNameIndex::const_iterator, OptionDefContainerNameIndex::const_iterator > OptionDefContainerNameRange
Pair of iterators to represent the range of options definitions having the same option name.
Definition option_definition.h:883
isc::dhcp::OptionDefContainers
std::map< std::string, OptionDefContainerPtr > OptionDefContainers
Container that holds option definitions for various option spaces.
Definition option_definition.h:862
isc::dhcp::OptionDefContainerNameIndex
OptionDefContainer::nth_index< 2 >::type OptionDefContainerNameIndex
Type of the index #2 - option name.
Definition option_definition.h:877
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
isc::dhcp::OptionDefContainerTypeRange
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.
Definition option_definition.h:874
isc::dhcp::OptionDefContainer
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.
Definition option_definition.h:856
isc::dhcp::OptionDataType
OptionDataType
Data types of DHCP option fields.
Definition option_data_types.h:45
isc::dhcp::OptionDefContainerTypeIndex
OptionDefContainer::nth_index< 1 >::type OptionDefContainerTypeIndex
Type of the index #1 - option type.
Definition option_definition.h:868
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::dhcp::OptionDefContainerPtr
boost::shared_ptr< OptionDefContainer > OptionDefContainerPtr
Pointer to an option definition container.
Definition option_definition.h:859
isc::util::file
Definition filesystem.cc:29
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
option.h
option_data_types.h
option_space_container.h
stamped_element.h
user_context.h