| Kea 3.0.2-git

libdhcp++.h
Go to the documentation of this file.
1// Copyright (C) 2011-2025 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 LIBDHCP_H
8#define LIBDHCP_H
9
10#include <dhcp/option_definition.h>
11#include <dhcp/option_space_container.h>
12#include <dhcp/option_space.h>
13#include <dhcp/pkt4.h>
14#include <dhcp/pkt6.h>
15#include <util/buffer.h>
16#include <util/staged_value.h>
17
18#include <iostream>
19#include <stdint.h>
20#include <string>
21
22 namespace isc {
23 namespace dhcp {
24
26 typedef ScopedPktOptionsCopy<Pkt4> ScopedPkt4OptionsCopy;
28 typedef ScopedPktOptionsCopy<Pkt6> ScopedPkt6OptionsCopy;
30 typedef std::shared_ptr<ScopedSubOptionsCopy> ScopedOptionsCopyPtr;
32 typedef std::vector<ScopedOptionsCopyPtr> ScopedOptionsCopyContainer;
33
34 struct ManagedScopedOptionsCopyContainer {
36 ManagedScopedOptionsCopyContainer() {
37 }
38
40 ~ManagedScopedOptionsCopyContainer() {
41 // Destroy the scoped options in same order so that parent options
42 // (stored last) are kept alive longer.
43 for (auto& scoped : scoped_options_) {
44 scoped.reset();
45 }
46 }
47
49 ScopedOptionsCopyContainer scoped_options_;
50};
51
52 class LibDHCP {
53
54 public:
55
57 typedef std::map<unsigned short, Option::Factory*> FactoryMap;
58
67 static const OptionDefContainerPtr getOptionDefs(const std::string& space);
68
77 static OptionDefinitionPtr getOptionDef(const std::string& space,
78 const uint16_t code);
79
87 static OptionDefinitionPtr getOptionDef(const std::string& space,
88 const std::string& name);
89
97 static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u,
98 const uint32_t vendor_id,
99 const uint16_t code);
100
110 static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u,
111 const uint32_t vendor_id,
112 const std::string& name);
113
114
122 static OptionDefinitionPtr getRuntimeOptionDef(const std::string& space,
123 const uint16_t code);
124
132 static OptionDefinitionPtr getRuntimeOptionDef(const std::string& space,
133 const std::string& name);
134
141 static OptionDefContainerPtr getRuntimeOptionDefs(const std::string& space);
142
149 static OptionDefinitionPtr getLastResortOptionDef(const std::string& space,
150 const uint16_t code);
151
158 static OptionDefinitionPtr getLastResortOptionDef(const std::string& space,
159 const std::string& name);
160
167 static OptionDefContainerPtr getLastResortOptionDefs(const std::string& space);
168
177 static bool shouldDeferOptionUnpack(const std::string& space,
178 const uint16_t code);
179
194 static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u,
195 uint16_t type,
196 const OptionBuffer& buf);
197
223 static void packOptions4(isc::util::OutputBuffer& buf,
224 const isc::dhcp::OptionCollection& options,
225 bool top = false, bool check = true);
226
238 static bool splitOptions4(isc::dhcp::OptionCollection& options,
239 ScopedOptionsCopyContainer& scopedOptions,
240 uint32_t used = 0);
241
256 static void packOptions6(isc::util::OutputBuffer& buf,
257 const isc::dhcp::OptionCollection& options);
258
264 static void splitNtpServerOptions6(isc::dhcp::OptionCollection& options);
265
295 static size_t unpackOptions6(const OptionBuffer& buf,
296 const std::string& option_space,
297 isc::dhcp::OptionCollection& options,
298 size_t* relay_msg_offset = 0,
299 size_t* relay_msg_len = 0);
300
306 static void extendVendorOptions4(isc::dhcp::OptionCollection& options);
307
327 static size_t unpackOptions4(const OptionBuffer& buf,
328 const std::string& option_space,
329 isc::dhcp::OptionCollection& options,
330 std::list<uint16_t>& deferred,
331 bool flexible_pad_end = false);
332
341 static void OptionFactoryRegister(Option::Universe u,
342 uint16_t type,
343 Option::Factory * factory);
344
352 static const OptionDefContainerPtr getVendorOptionDefs(Option::Universe u,
353 const uint32_t vendor_id);
354
370 static size_t unpackVendorOptions6(const uint32_t vendor_id,
371 const OptionBuffer& buf,
372 isc::dhcp::OptionCollection& options);
373
388 static size_t unpackVendorOptions4(const uint32_t vendor_id,
389 const OptionBuffer& buf,
390 isc::dhcp::OptionCollection& options);
391
392
402 static void setRuntimeOptionDefs(const OptionDefSpaceContainer& defs);
403
405 static void clearRuntimeOptionDefs();
406
408 static void revertRuntimeOptionDefs();
409
411 static void commitRuntimeOptionDefs();
412
426 static uint32_t optionSpaceToVendorId(const std::string& option_space);
427
429 static const OptionDefinition& DHO_DHCP_REQUESTED_ADDRESS_DEF();
430
432 static const OptionDefinition& DHO_DHCP_SERVER_IDENTIFIER_DEF();
433
435 static const OptionDefinition& DHO_DHCP_AGENT_OPTIONS_DEF();
436
438 static const OptionDefinition& DHO_SUBNET_SELECTION_DEF();
439
441 static const OptionDefinition& DHO_DOMAIN_SEARCH_DEF();
442
444 static const OptionDefinition& DHO_STATUS_CODE_DEF();
445
447 static const OptionDefinition& D6O_CLIENT_FQDN_DEF();
448
450 static const OptionDefinition& D6O_LQ_QUERY_DEF();
451
453 static const OptionDefinition& D6O_CLIENT_DATA_DEF();
454
456 static const OptionDefinition& D6O_LQ_RELAY_DATA_DEF();
457
459 static const OptionDefinition& D6O_NTP_SERVER_DEF();
460
462 static const OptionDefinition& D6O_BOOTFILE_URL_DEF();
463
465 static const OptionDefinition& D6O_RSOO_DEF();
466
468 static const OptionDefinition& D6O_IAADDR_DEF();
469
470 private:
471
479 static bool initOptionDefs();
480
482 static bool initialized_;
483
485 static FactoryMap v4factories_;
486
488 static FactoryMap v6factories_;
489
491 static OptionDefContainers option_defs_;
492
494 static util::StagedValue<OptionDefSpaceContainer> runtime_option_defs_;
495};
496
497}
498}
499
500#endif // LIBDHCP_H
buffer.h
isc::dhcp::LibDHCP
Definition libdhcp++.h:52
isc::dhcp::LibDHCP::unpackOptions4
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.
Definition libdhcp++.cc:471
isc::dhcp::LibDHCP::OptionFactoryRegister
static void OptionFactoryRegister(Option::Universe u, uint16_t type, Option::Factory *factory)
Registers factory method that produces options of specific option types.
Definition libdhcp++.cc:1277
isc::dhcp::LibDHCP::D6O_LQ_RELAY_DATA_DEF
static const OptionDefinition & D6O_LQ_RELAY_DATA_DEF()
Get definition of D6O_LQ_RELAY_DATA option.
Definition libdhcp++.cc:1597
isc::dhcp::LibDHCP::D6O_NTP_SERVER_DEF
static const OptionDefinition & D6O_NTP_SERVER_DEF()
Get definition of D6O_NTP_SERVER option.
Definition libdhcp++.cc:1615
isc::dhcp::LibDHCP::D6O_RSOO_DEF
static const OptionDefinition & D6O_RSOO_DEF()
Get definition of D6O_RSOO option.
Definition libdhcp++.cc:1651
isc::dhcp::LibDHCP::unpackVendorOptions6
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.
Definition libdhcp++.cc:823
isc::dhcp::LibDHCP::getOptionDefs
static const OptionDefContainerPtr getOptionDefs(const std::string &space)
Returns collection of option definitions.
Definition libdhcp++.cc:105
isc::dhcp::LibDHCP::splitNtpServerOptions6
static void splitNtpServerOptions6(isc::dhcp::OptionCollection &options)
Split NTP server option to one suboption per instance.
Definition libdhcp++.cc:1258
isc::dhcp::LibDHCP::shouldDeferOptionUnpack
static bool shouldDeferOptionUnpack(const std::string &space, const uint16_t code)
Checks if an option unpacking has to be deferred.
Definition libdhcp++.cc:286
isc::dhcp::LibDHCP::optionFactory
static isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition libdhcp++.cc:293
isc::dhcp::LibDHCP::setRuntimeOptionDefs
static void setRuntimeOptionDefs(const OptionDefSpaceContainer &defs)
Copies option definitions created at runtime.
Definition libdhcp++.cc:224
isc::dhcp::LibDHCP::getOptionDef
static OptionDefinitionPtr getOptionDef(const std::string &space, const uint16_t code)
Return the first option definition matching a particular option code.
Definition libdhcp++.cc:132
isc::dhcp::LibDHCP::getVendorOptionDef
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.
Definition libdhcp++.cc:174
isc::dhcp::LibDHCP::FactoryMap
std::map< unsigned short, Option::Factory * > FactoryMap
Map of factory functions.
Definition libdhcp++.h:57
isc::dhcp::LibDHCP::getLastResortOptionDefs
static OptionDefContainerPtr getLastResortOptionDefs(const std::string &space)
Returns last resort option definitions for specified option space name.
Definition libdhcp++.cc:277
isc::dhcp::LibDHCP::D6O_LQ_QUERY_DEF
static const OptionDefinition & D6O_LQ_QUERY_DEF()
Get definition of D6O_LQ_QUERY option.
Definition libdhcp++.cc:1561
isc::dhcp::LibDHCP::getRuntimeOptionDefs
static OptionDefContainerPtr getRuntimeOptionDefs(const std::string &space)
Returns runtime (non-standard) option definitions for specified option space name.
Definition libdhcp++.cc:219
isc::dhcp::LibDHCP::commitRuntimeOptionDefs
static void commitRuntimeOptionDefs()
Commits runtime option definitions.
Definition libdhcp++.cc:248
isc::dhcp::LibDHCP::clearRuntimeOptionDefs
static void clearRuntimeOptionDefs()
Removes runtime option definitions.
Definition libdhcp++.cc:238
isc::dhcp::LibDHCP::D6O_IAADDR_DEF
static const OptionDefinition & D6O_IAADDR_DEF()
Get definition of D6O_IAADDR option.
Definition libdhcp++.cc:1669
isc::dhcp::LibDHCP::extendVendorOptions4
static void extendVendorOptions4(isc::dhcp::OptionCollection &options)
Extend vendor options from fused options in multiple OptionVendor or OptionVendorClass options and ad...
Definition libdhcp++.cc:817
isc::dhcp::LibDHCP::D6O_CLIENT_DATA_DEF
static const OptionDefinition & D6O_CLIENT_DATA_DEF()
Get definition of D6O_CLIENT_DATA option.
Definition libdhcp++.cc:1579
isc::dhcp::LibDHCP::packOptions4
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false, bool check=true)
Stores DHCPv4 options in a buffer.
Definition libdhcp++.cc:1025
isc::dhcp::LibDHCP::splitOptions4
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).
Definition libdhcp++.cc:1070
isc::dhcp::LibDHCP::DHO_DHCP_REQUESTED_ADDRESS_DEF
static const OptionDefinition & DHO_DHCP_REQUESTED_ADDRESS_DEF()
Get definition of DHO_DHCP_REQUESTED_ADDRESS option.
Definition libdhcp++.cc:1435
isc::dhcp::LibDHCP::D6O_BOOTFILE_URL_DEF
static const OptionDefinition & D6O_BOOTFILE_URL_DEF()
Get definition of D6O_BOOTFILE_URL option.
Definition libdhcp++.cc:1633
isc::dhcp::LibDHCP::DHO_DHCP_SERVER_IDENTIFIER_DEF
static const OptionDefinition & DHO_DHCP_SERVER_IDENTIFIER_DEF()
Get definition of DHO_DHCP_SERVER_IDENTIFIER option.
Definition libdhcp++.cc:1453
isc::dhcp::LibDHCP::DHO_SUBNET_SELECTION_DEF
static const OptionDefinition & DHO_SUBNET_SELECTION_DEF()
Get definition of DHO_SUBNET_SELECTION option.
Definition libdhcp++.cc:1489
isc::dhcp::LibDHCP::revertRuntimeOptionDefs
static void revertRuntimeOptionDefs()
Reverts uncommitted changes to runtime option definitions.
Definition libdhcp++.cc:243
isc::dhcp::LibDHCP::DHO_DOMAIN_SEARCH_DEF
static const OptionDefinition & DHO_DOMAIN_SEARCH_DEF()
Get definition of DHO_DOMAIN_SEARCH option.
Definition libdhcp++.cc:1507
isc::dhcp::LibDHCP::getVendorOptionDefs
static const OptionDefContainerPtr getVendorOptionDefs(Option::Universe u, const uint32_t vendor_id)
Returns option definitions for given universe and vendor.
Definition libdhcp++.cc:115
isc::dhcp::LibDHCP::D6O_CLIENT_FQDN_DEF
static const OptionDefinition & D6O_CLIENT_FQDN_DEF()
Get definition of D6O_CLIENT_FQDN option.
Definition libdhcp++.cc:1543
isc::dhcp::LibDHCP::DHO_DHCP_AGENT_OPTIONS_DEF
static const OptionDefinition & DHO_DHCP_AGENT_OPTIONS_DEF()
Get definition of DHO_DHCP_AGENT_OPTIONS option.
Definition libdhcp++.cc:1471
isc::dhcp::LibDHCP::optionSpaceToVendorId
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
Definition libdhcp++.cc:1345
isc::dhcp::LibDHCP::getRuntimeOptionDef
static OptionDefinitionPtr getRuntimeOptionDef(const std::string &space, const uint16_t code)
Returns runtime (non-standard) option definition by space and option code.
Definition libdhcp++.cc:195
isc::dhcp::LibDHCP::unpackOptions6
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.
Definition libdhcp++.cc:317
isc::dhcp::LibDHCP::packOptions6
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition libdhcp++.cc:1251
isc::dhcp::LibDHCP::DHO_STATUS_CODE_DEF
static const OptionDefinition & DHO_STATUS_CODE_DEF()
Get definition of DHO_STATUS_CODE option.
Definition libdhcp++.cc:1525
isc::dhcp::LibDHCP::getLastResortOptionDef
static OptionDefinitionPtr getLastResortOptionDef(const std::string &space, const uint16_t code)
Returns last resort option definition by space and option code.
Definition libdhcp++.cc:253
isc::dhcp::LibDHCP::unpackVendorOptions4
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.
Definition libdhcp++.cc:919
isc::dhcp::OptionDefSpaceContainer
Class of option definition space container.
Definition option_definition.h:879
isc::dhcp::OptionDefinition
Base class representing a DHCP option definition.
Definition option_definition.h:138
isc::dhcp::Option::Universe
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
isc::dhcp::Option::Factory
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition option.h:103
isc::dhcp::ScopedPktOptionsCopy
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition pkt.h:1057
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::StagedValue
This class implements set/commit mechanism for a single object.
Definition staged_value.h:32
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::ScopedPkt4OptionsCopy
ScopedPktOptionsCopy< Pkt4 > ScopedPkt4OptionsCopy
A pointer to a ScopedPktOptionsCopy object instantiated using Pkt4.
Definition libdhcp++.h:26
isc::dhcp::OptionCollection
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition option.h:40
isc::dhcp::OptionDefinitionPtr
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
Definition option_definition.h:55
isc::dhcp::OptionDefContainers
std::map< std::string, OptionDefContainerPtr > OptionDefContainers
Container that holds option definitions for various option spaces.
Definition option_definition.h:850
isc::dhcp::ScopedOptionsCopyPtr
std::shared_ptr< ScopedSubOptionsCopy > ScopedOptionsCopyPtr
A pointer to a ScopedSubOptionsCopy object.
Definition libdhcp++.h:30
isc::dhcp::ScopedPkt6OptionsCopy
ScopedPktOptionsCopy< Pkt6 > ScopedPkt6OptionsCopy
A pointer to a ScopedPktOptionsCopy object instantiated using Pkt6.
Definition libdhcp++.h:28
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::dhcp::ScopedOptionsCopyContainer
std::vector< ScopedOptionsCopyPtr > ScopedOptionsCopyContainer
A container of ScopedOptionsCopyPtr objects.
Definition libdhcp++.h:32
isc::dhcp::OptionDefContainerPtr
boost::shared_ptr< OptionDefContainer > OptionDefContainerPtr
Pointer to an option definition container.
Definition option_definition.h:847
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
option_definition.h
option_space.h
option_space_container.h
pkt4.h
pkt6.h
staged_value.h
isc::dhcp::ManagedScopedOptionsCopyContainer::ManagedScopedOptionsCopyContainer
ManagedScopedOptionsCopyContainer()
Constructor.
Definition libdhcp++.h:36
isc::dhcp::ManagedScopedOptionsCopyContainer::scoped_options_
ScopedOptionsCopyContainer scoped_options_
The container.
Definition libdhcp++.h:49
isc::dhcp::ManagedScopedOptionsCopyContainer::~ManagedScopedOptionsCopyContainer
~ManagedScopedOptionsCopyContainer()
Destructor.
Definition libdhcp++.h:40