Kea: libdhcp++.h Source File

| Kea 3.2.0-git

libdhcp++.h
Go to the documentation of this file.
1// Copyright (C) 2011-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 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
22namespace isc {
23namespace 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
54public:
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
299 static size_t unpackOptions6(const OptionBuffer& buf,
300 const std::string& option_space,
301 isc::dhcp::OptionCollection& options,
302 size_t* relay_msg_offset = 0,
303 size_t* relay_msg_len = 0,
304 size_t rec_level = 0);
305
311 static void extendVendorOptions4(isc::dhcp::OptionCollection& options);
312
332 static size_t unpackOptions4(const OptionBuffer& buf,
333 const std::string& option_space,
334 isc::dhcp::OptionCollection& options,
335 std::list<uint16_t>& deferred,
336 bool flexible_pad_end = false);
337
346 static void OptionFactoryRegister(Option::Universe u,
347 uint16_t type,
348 Option::Factory * factory);
349
357 static const OptionDefContainerPtr getVendorOptionDefs(Option::Universe u,
358 const uint32_t vendor_id);
359
375 static size_t unpackVendorOptions6(const uint32_t vendor_id,
376 const OptionBuffer& buf,
377 isc::dhcp::OptionCollection& options);
378
393 static size_t unpackVendorOptions4(const uint32_t vendor_id,
394 const OptionBuffer& buf,
395 isc::dhcp::OptionCollection& options);
396
397
407 static void setRuntimeOptionDefs(const OptionDefSpaceContainer& defs);
408
410 static void clearRuntimeOptionDefs();
411
413 static void revertRuntimeOptionDefs();
414
416 static void commitRuntimeOptionDefs();
417
431 static uint32_t optionSpaceToVendorId(const std::string& option_space);
432
434 static const OptionDefinition& DHO_DHCP_REQUESTED_ADDRESS_DEF();
435
437 static const OptionDefinition& DHO_DHCP_SERVER_IDENTIFIER_DEF();
438
440 static const OptionDefinition& DHO_DHCP_AGENT_OPTIONS_DEF();
441
443 static const OptionDefinition& DHO_SUBNET_SELECTION_DEF();
444
446 static const OptionDefinition& DHO_DOMAIN_SEARCH_DEF();
447
449 static const OptionDefinition& DHO_STATUS_CODE_DEF();
450
452 static const OptionDefinition& D6O_CLIENT_FQDN_DEF();
453
455 static const OptionDefinition& D6O_LQ_QUERY_DEF();
456
458 static const OptionDefinition& D6O_CLIENT_DATA_DEF();
459
461 static const OptionDefinition& D6O_LQ_RELAY_DATA_DEF();
462
464 static const OptionDefinition& D6O_NTP_SERVER_DEF();
465
467 static const OptionDefinition& D6O_BOOTFILE_URL_DEF();
468
470 static const OptionDefinition& D6O_RSOO_DEF();
471
473 static const OptionDefinition& D6O_IAADDR_DEF();
474
480 static size_t MAX_RECURSION_LEVEL;
481
482private:
483
491 static bool initOptionDefs();
492
494 static bool initialized_;
495
497 static FactoryMap v4factories_;
498
500 static FactoryMap v6factories_;
501
503 static OptionDefContainers option_defs_;
504
506 static util::StagedValue<OptionDefSpaceContainer> runtime_option_defs_;
507};
508
509}
510}
511
512#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:480
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:1286
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:1606
isc::dhcp::LibDHCP::D6O_NTP_SERVER_DEF
static const OptionDefinition & D6O_NTP_SERVER_DEF()
Get definition of D6O_NTP_SERVER option.
Definition libdhcp++.cc:1624
isc::dhcp::LibDHCP::D6O_RSOO_DEF
static const OptionDefinition & D6O_RSOO_DEF()
Get definition of D6O_RSOO option.
Definition libdhcp++.cc:1660
isc::dhcp::LibDHCP::MAX_RECURSION_LEVEL
static size_t MAX_RECURSION_LEVEL
Maximum level of recursion unpacking options.
Definition libdhcp++.h:480
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:832
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:1267
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::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, size_t rec_level=0)
Parses provided buffer as DHCPv6 options and creates Option objects.
Definition libdhcp++.cc:320
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:1570
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:1678
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:826
isc::dhcp::LibDHCP::D6O_CLIENT_DATA_DEF
static const OptionDefinition & D6O_CLIENT_DATA_DEF()
Get definition of D6O_CLIENT_DATA option.
Definition libdhcp++.cc:1588
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:1034
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:1079
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:1444
isc::dhcp::LibDHCP::D6O_BOOTFILE_URL_DEF
static const OptionDefinition & D6O_BOOTFILE_URL_DEF()
Get definition of D6O_BOOTFILE_URL option.
Definition libdhcp++.cc:1642
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:1462
isc::dhcp::LibDHCP::DHO_SUBNET_SELECTION_DEF
static const OptionDefinition & DHO_SUBNET_SELECTION_DEF()
Get definition of DHO_SUBNET_SELECTION option.
Definition libdhcp++.cc:1498
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:1516
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:1552
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:1480
isc::dhcp::LibDHCP::optionSpaceToVendorId
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
Definition libdhcp++.cc:1354
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::packOptions6
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition libdhcp++.cc:1260
isc::dhcp::LibDHCP::DHO_STATUS_CODE_DEF
static const OptionDefinition & DHO_STATUS_CODE_DEF()
Get definition of DHO_STATUS_CODE option.
Definition libdhcp++.cc:1534
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:928
isc::dhcp::OptionDefSpaceContainer
Class of option definition space container.
Definition option_definition.h:891
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:1058
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:862
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:859
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
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