Kea  2.1.7-git
dhcp_parsers.h
Go to the documentation of this file.
1 // Copyright (C) 2013-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 DHCP_PARSERS_H
8 #define DHCP_PARSERS_H
9 
10 #include <asiolink/io_address.h>
11 #include <cc/data.h>
12 #include <dhcp/option_definition.h>
14 #include <dhcpsrv/d2_client_cfg.h>
15 #include <dhcpsrv/cfg_iface.h>
16 #include <dhcpsrv/cfg_option.h>
17 #include <dhcpsrv/network.h>
18 #include <dhcpsrv/subnet.h>
19 #include <dhcpsrv/cfg_option_def.h>
20 #include <dhcpsrv/cfg_mac_source.h>
21 #include <dhcpsrv/srv_config.h>
24 #include <cc/simple_parser.h>
25 #include <exceptions/exceptions.h>
26 #include <util/optional.h>
27 
28 #include <boost/shared_ptr.hpp>
29 
30 #include <stdint.h>
31 #include <string>
32 #include <vector>
33 
34 namespace isc {
35 namespace dhcp {
36 
39 typedef OptionSpaceContainer<OptionContainer, OptionDescriptor,
40  std::string> OptionStorage;
42 typedef boost::shared_ptr<OptionStorage> OptionStoragePtr;
43 
55 template<typename ValueType>
56 class ValueStorage {
57 public:
69  void setParam(const std::string& name, const ValueType& value,
70  const data::Element::Position& position) {
71  values_[name] = value;
72  positions_[name] = position;
73  }
74 
83  ValueType getParam(const std::string& name) const {
84  typename std::map<std::string, ValueType>::const_iterator param
85  = values_.find(name);
86 
87  if (param == values_.end()) {
88  isc_throw(DhcpConfigError, "Missing parameter '"
89  << name << "'");
90  }
91 
92  return (param->second);
93  }
94 
109  getPosition(const std::string& name, const data::ConstElementPtr parent =
110  data::ConstElementPtr()) const {
111  typename std::map<std::string, data::Element::Position>::const_iterator
112  pos = positions_.find(name);
113  if (pos == positions_.end()) {
114  return (parent ? parent->getPosition() :
116  }
117 
118  return (pos->second);
119  }
120 
131  ValueType getOptionalParam(const std::string& name,
132  const ValueType& default_value) const {
133  typename std::map<std::string, ValueType>::const_iterator param
134  = values_.find(name);
135 
136  if (param == values_.end()) {
137  return (default_value);
138  }
139 
140  return (param->second);
141  }
142 
149  void delParam(const std::string& name) {
150  values_.erase(name);
151  positions_.erase(name);
152  }
153 
156  void clear() {
157  values_.clear();
158  positions_.clear();
159  }
160 
161 private:
163  std::map<std::string, ValueType> values_;
164 
170  std::map<std::string, data::Element::Position> positions_;
171 
172 };
173 
175 typedef std::pair<std::string, isc::data::ConstElementPtr> ConfigPair;
176 
179 typedef boost::shared_ptr<Uint32Storage> Uint32StoragePtr;
180 
183 typedef boost::shared_ptr<StringStorage> StringStoragePtr;
184 
187 typedef boost::shared_ptr<BooleanStorage> BooleanStoragePtr;
188 
196 public:
204  void parse(CfgMACSource& mac_sources, isc::data::ConstElementPtr value);
205 };
206 
212 public:
222  void parse(SrvConfig& srv_cfg, isc::data::ConstElementPtr value);
223 };
224 
229 public:
233  OptionDefParser(const uint16_t address_family);
234 
242 
243 private:
245  uint16_t address_family_;
246 };
247 
255 public:
259  OptionDefListParser(const uint16_t address_family);
260 
269  void parse(CfgOptionDefPtr cfg, isc::data::ConstElementPtr def_list);
270 
271 private:
273  uint16_t address_family_;
274 };
275 
280 typedef std::vector<PoolPtr> PoolStorage;
281 typedef boost::shared_ptr<PoolStorage> PoolStoragePtr;
282 
291 public:
292 
294  virtual ~PoolParser() {
295  }
296 
306  virtual void parse(PoolStoragePtr pools,
307  isc::data::ConstElementPtr pool_structure,
308  const uint16_t address_family);
309 
310 protected:
317  virtual PoolPtr poolMaker(isc::asiolink::IOAddress &addr, uint32_t len,
318  int32_t ptype = 0) = 0;
319 
326  virtual PoolPtr poolMaker(isc::asiolink::IOAddress &min,
328  int32_t ptype = 0) = 0;
329 
339  virtual boost::shared_ptr<OptionDataListParser>
340  createOptionDataListParser(const uint16_t address_family) const;
341 };
342 
351 class Pool4Parser : public PoolParser {
352 protected:
360  PoolPtr poolMaker (asiolink::IOAddress &addr, uint32_t len,
361  int32_t ignored);
362 
370  PoolPtr poolMaker (asiolink::IOAddress &min, asiolink::IOAddress &max,
371  int32_t ignored);
372 };
373 
380 public:
381 
383  virtual ~PoolsListParser() {
384  }
385 
393  virtual void parse(PoolStoragePtr pools,
394  isc::data::ConstElementPtr pools_list) = 0;
395 
396 protected:
397 
405  virtual boost::shared_ptr<PoolParser> createPoolConfigParser() const = 0;
406 };
407 
410 public:
411 
419  void parse(PoolStoragePtr pools, data::ConstElementPtr pools_list);
420 
421 protected:
422 
430  virtual boost::shared_ptr<PoolParser> createPoolConfigParser() const;
431 };
432 
441 public:
442 
445  explicit RelayInfoParser(const isc::dhcp::Option::Universe& family);
446 
461  void parse(const isc::dhcp::Network::RelayInfoPtr& relay_info,
462  isc::data::ConstElementPtr relay_elem);
463 
474  void addAddress(const std::string& name, const std::string& address_str,
475  isc::data::ConstElementPtr relay_elem,
476  const isc::dhcp::Network::RelayInfoPtr& relay_info);
477 private:
478 
480  Option::Universe family_;
481 };
482 
504 public:
505 
511  explicit SubnetConfigParser(uint16_t family, bool check_iface = true);
512 
514  virtual ~SubnetConfigParser() { }
515 
516 protected:
527 
534  virtual void initSubnet(isc::data::ConstElementPtr params,
535  isc::asiolink::IOAddress addr, uint8_t len) = 0;
536 
537 protected:
538 
544  void createSubnet(isc::data::ConstElementPtr data);
545 
553  virtual boost::shared_ptr<OptionDataListParser> createOptionDataListParser() const;
554 
562  virtual boost::shared_ptr<PoolsListParser>
563  createPoolsListParser() const = 0;
564 
566  PoolStoragePtr pools_;
567 
570 
572  uint16_t address_family_;
573 
576 
579 
582 };
583 
591 public:
598  Subnet4ConfigParser(bool check_iface = true);
599 
605  Subnet4Ptr parse(data::ConstElementPtr subnet);
606 
607 protected:
608 
615  void initSubnet(data::ConstElementPtr params,
616  asiolink::IOAddress addr, uint8_t len);
617 
623  void validateResv(const Subnet4Ptr& subnet, ConstHostPtr host);
624 
632  virtual boost::shared_ptr<PoolsListParser>
633  createPoolsListParser() const;
634 };
635 
642 public:
643 
648  Subnets4ListConfigParser(bool check_iface = true);
649 
652  }
653 
663  size_t parse(SrvConfigPtr cfg, data::ConstElementPtr subnets_list);
664 
670  size_t parse(Subnet4Collection& subnets,
671  data::ConstElementPtr subnets_list);
672 
673 protected:
674 
682  virtual boost::shared_ptr<Subnet4ConfigParser> createSubnetConfigParser() const;
683 
686 };
687 
696 class Pool6Parser : public PoolParser {
697 protected:
706  PoolPtr poolMaker (asiolink::IOAddress &addr, uint32_t len, int32_t ptype);
707 
716  PoolPtr poolMaker (asiolink::IOAddress &min, asiolink::IOAddress &max,
717  int32_t ptype);
718 };
719 
722 public:
723 
731  void parse(PoolStoragePtr pools, data::ConstElementPtr pools_list);
732 
733 protected:
734 
742  virtual boost::shared_ptr<PoolParser> createPoolConfigParser() const;
743 };
744 
763 public:
764 
767  PdPoolParser();
768 
770  virtual ~PdPoolParser() {
771  }
772 
783  void parse(PoolStoragePtr pools, data::ConstElementPtr pd_pool_);
784 
785 protected:
786 
794  virtual boost::shared_ptr<OptionDataListParser>
795  createOptionDataListParser() const;
796 
799 
802 
807 
812 };
813 
820 public:
821 
823  virtual ~PdPoolsListParser() {
824  }
825 
836  void parse(PoolStoragePtr pools, data::ConstElementPtr pd_pool_list);
837 
838 protected:
839 
847  virtual boost::shared_ptr<PdPoolParser>
848  createPdPoolConfigParser() const;
849 };
850 
858 public:
859 
866  Subnet6ConfigParser(bool check_iface = true);
867 
873  Subnet6Ptr parse(data::ConstElementPtr subnet);
874 
875 protected:
883  virtual void duplicate_option_warning(uint32_t code,
884  asiolink::IOAddress& addr);
885 
892  void initSubnet(isc::data::ConstElementPtr params,
893  isc::asiolink::IOAddress addr, uint8_t len);
894 
900  void validateResvs(const Subnet6Ptr& subnet, ConstHostPtr host);
901 
909  virtual boost::shared_ptr<PoolsListParser>
910  createPoolsListParser() const;
911 
919  virtual boost::shared_ptr<PdPoolsListParser>
920  createPdPoolsListParser() const;
921 };
922 
923 
930 public:
931 
936  Subnets6ListConfigParser(bool check_iface = true);
937 
940  }
941 
951  size_t parse(SrvConfigPtr cfg, data::ConstElementPtr subnets_list);
952 
958  size_t parse(Subnet6Collection& subnets,
959  data::ConstElementPtr subnets_list);
960 
961 protected:
962 
970  virtual boost::shared_ptr<Subnet6ConfigParser> createSubnetConfigParser() const;
971 
974 };
975 
982 public:
983 
1000  D2ClientConfigPtr parse(isc::data::ConstElementPtr d2_client_cfg);
1001 
1004 
1011  // to ElementPtr)
1013  static size_t setAllDefaults(isc::data::ConstElementPtr d2_config);
1014 
1015 private:
1016 
1025  getProtocol(isc::data::ConstElementPtr scope, const std::string& name);
1026 
1035  getFormat(isc::data::ConstElementPtr scope, const std::string& name);
1036 
1045  getMode(isc::data::ConstElementPtr scope, const std::string& name);
1046 };
1047 
1048 } // end of isc::dhcp namespace
1049 } // end of isc namespace
1050 
1051 #endif // DHCP_PARSERS_H
std::pair< std::string, isc::data::ConstElementPtr > ConfigPair
Combination of parameter name and configuration contents.
Definition: dhcp_parsers.h:175
Defines the D2ClientConfig class.
virtual ~PdPoolsListParser()
Virtual destructor.
Definition: dhcp_parsers.h:823
void setParam(const std::string &name, const ValueType &value, const data::Element::Position &position)
Stores the parameter, its value and the position in the store.
Definition: dhcp_parsers.h:69
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:706
isc::dhcp::Pool6Ptr pool_
Pointer to the created pool object.
Definition: dhcp_parsers.h:798
OptionSpaceContainer< OptionContainer, OptionDescriptor, std::string > OptionStorage
Collection of containers holding option spaces.
Definition: dhcp_parsers.h:40
this class parses a list of DHCP6 subnets
Definition: dhcp_parsers.h:929
static const Position & ZERO_POSITION()
Returns Position object with line_ and pos_ set to 0, and with an empty file name.
Definition: data.h:124
boost::shared_ptr< OptionStorage > OptionStoragePtr
Shared pointer to option storage.
Definition: dhcp_parsers.h:42
this class parses a single subnet
Definition: dhcp_parsers.h:503
Parser for IPv4 pool definitions.
Definition: dhcp_parsers.h:351
virtual ~PoolsListParser()
destructor.
Definition: dhcp_parsers.h:383
boost::shared_ptr< StringStorage > StringStoragePtr
Definition: dhcp_parsers.h:183
boost::shared_ptr< Pool6 > Pool6Ptr
a pointer an IPv6 Pool
Definition: pool.h:312
Specialization of the pool list parser for DHCPv6.
Definition: dhcp_parsers.h:721
static const isc::data::SimpleDefaults D2_CLIENT_CONFIG_DEFAULTS
Defaults for the D2 client configuration.
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet).
Common configuration parser for shared networks and subnets.
boost::shared_ptr< Network::RelayInfo > RelayInfoPtr
Pointer to the RelayInfo structure.
Definition: network.h:181
boost::shared_ptr< Uint32Storage > Uint32StoragePtr
Definition: dhcp_parsers.h:179
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition: srv_config.h:1165
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:524
This class parses a single IPv4 subnet.
Definition: dhcp_parsers.h:590
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
CfgOptionPtr options_
Pointer to the options configuration.
Definition: dhcp_parsers.h:578
NameChangeFormat
Defines the list of data wire formats supported.
Definition: ncr_msg.h:60
void clear()
Deletes all of the entries from the store.
Definition: dhcp_parsers.h:156
const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent=data::ConstElementPtr()) const
Returns position of the data element in the configuration string.
Definition: dhcp_parsers.h:109
this class parses list of DHCP4 subnets
Definition: dhcp_parsers.h:641
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
virtual ~SubnetConfigParser()
virtual destructor (does nothing)
Definition: dhcp_parsers.h:514
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition: pool.h:505
Parser for D2ClientConfig.
Definition: dhcp_parsers.h:981
ValueStorage< bool > BooleanStorage
Storage for parsed boolean values.
Definition: dhcp_parsers.h:186
Specialization of the pool list parser for DHCPv4.
Definition: dhcp_parsers.h:409
PoolStoragePtr pools_
Storage for pools belonging to this subnet.
Definition: dhcp_parsers.h:566
Parser for IPv6 prefix delegation definitions.
Definition: dhcp_parsers.h:762
parser for MAC/hardware acquisition sources
Definition: dhcp_parsers.h:195
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > >> Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:893
Wrapper class that holds MAC/hardware address sources.
parser for additional relay information
Definition: dhcp_parsers.h:440
To be removed. Please use ConfigError instead.
Parser for a single option definition.
Definition: dhcp_parsers.h:228
parser for a single pool definition
Definition: dhcp_parsers.h:290
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:788
Parser for a list of option definitions.
Definition: dhcp_parsers.h:254
A template class that stores named elements of a given data type.
Definition: dhcp_parsers.h:56
isc::data::ConstElementPtr client_class_
Client class (a client has to belong to to use this pd-pool)
Definition: dhcp_parsers.h:811
Represents the position of the data element within a configuration string.
Definition: data.h:92
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > >> Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:964
Specifies current DHCP configuration.
Definition: srv_config.h:177
CfgOptionPtr options_
A storage for pool specific option values.
Definition: dhcp_parsers.h:801
Defines the logger used by the top-level component of kea-lfc.
virtual ~Subnets4ListConfigParser()
Virtual destructor.
Definition: dhcp_parsers.h:651
bool check_iface_
Check if the specified interface exists in the system.
Definition: dhcp_parsers.h:973
Parser for a list of prefix delegation pools.
Definition: dhcp_parsers.h:819
Parser for IPv6 pool definitions.
Definition: dhcp_parsers.h:696
ReplaceClientNameMode
Defines the client name replacement modes.
Definition: d2_client_cfg.h:76
uint16_t address_family_
Address family: AF_INET or AF_INET6.
Definition: dhcp_parsers.h:572
Parser for a list of pools.
Definition: dhcp_parsers.h:379
ValueType getOptionalParam(const std::string &name, const ValueType &default_value) const
Returns the data value for an optional parameter.
Definition: dhcp_parsers.h:131
void delParam(const std::string &name)
Remove the parameter from the store.
Definition: dhcp_parsers.h:149
bool check_iface_
Check if the specified interface exists in the system.
Definition: dhcp_parsers.h:685
boost::multi_index_container< OptionDescriptor, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< KeyFromKeyExtractor< boost::multi_index::const_mem_fun< Option, uint16_t, &Option::getType >, boost::multi_index::member< OptionDescriptor, OptionPtr, &OptionDescriptor::option_ > > >, boost::multi_index::hashed_non_unique< boost::multi_index::member< OptionDescriptor, bool, &OptionDescriptor::persistent_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::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 > > >> OptionContainer
Multi index container for DHCP option descriptors.
Definition: cfg_option.h:269
isc::dhcp::SubnetPtr subnet_
Pointer to the created subnet object.
Definition: dhcp_parsers.h:569
ValueStorage< std::string > StringStorage
a collection of elements that store string values
Definition: dhcp_parsers.h:182
isc::dhcp::Network::RelayInfoPtr relay_info_
Pointer to relay information.
Definition: dhcp_parsers.h:575
virtual ~PoolParser()
destructor.
Definition: dhcp_parsers.h:294
NameChangeProtocol
Defines the list of socket protocols supported.
Definition: ncr_io.h:68
This class parses a single IPv6 subnet.
Definition: dhcp_parsers.h:857
boost::shared_ptr< PoolStorage > PoolStoragePtr
Definition: dhcp_parsers.h:281
ValueStorage< uint32_t > Uint32Storage
a collection of elements that store uint32 values
Definition: dhcp_parsers.h:178
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
isc::data::ConstElementPtr user_context_
User context (optional, may be null)
Definition: dhcp_parsers.h:806
virtual ~PdPoolParser()
Virtual destructor.
Definition: dhcp_parsers.h:770
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:672
Parser for the control-socket structure.
Definition: dhcp_parsers.h:211
ValueType getParam(const std::string &name) const
Returns the data value for the given parameter.
Definition: dhcp_parsers.h:83
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
bool check_iface_
Check if the specified interface exists in the system.
Definition: dhcp_parsers.h:581
boost::shared_ptr< BooleanStorage > BooleanStoragePtr
Definition: dhcp_parsers.h:187
std::vector< PoolPtr > PoolStorage
a collection of pools
Definition: dhcp_parsers.h:280
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition: subnet.h:515
virtual ~Subnets6ListConfigParser()
Virtual destructor.
Definition: dhcp_parsers.h:939