From: Marcin Siodelski Date: Tue, 19 Mar 2019 12:26:00 +0000 (+0100) Subject: [#103,!277] DHCPv4 server is using CBControlDHCPv4. X-Git-Tag: Kea-1.6.0-beta~327 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=96c63ac17a855e9cd15027ae9740c73add0ad257;p=thirdparty%2Fkea.git [#103,!277] DHCPv4 server is using CBControlDHCPv4. --- diff --git a/src/bin/dhcp4/dhcp4_srv.cc b/src/bin/dhcp4/dhcp4_srv.cc index 40bbe2ca29..9f37271225 100644 --- a/src/bin/dhcp4/dhcp4_srv.cc +++ b/src/bin/dhcp4/dhcp4_srv.cc @@ -447,7 +447,8 @@ Dhcpv4Srv::Dhcpv4Srv(uint16_t server_port, uint16_t client_port, : io_service_(new IOService()), shutdown_(true), alloc_engine_(), server_port_(server_port), use_bcast_(use_bcast), client_port_(client_port), - network_state_(new NetworkState(NetworkState::DHCPv4)) { + network_state_(new NetworkState(NetworkState::DHCPv4)), + cb_control_(new CBControlDHCPv4()) { LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET) .arg(server_port); diff --git a/src/bin/dhcp4/dhcp4_srv.h b/src/bin/dhcp4/dhcp4_srv.h index ccc1ce88a8..a92f9f1f7c 100644 --- a/src/bin/dhcp4/dhcp4_srv.h +++ b/src/bin/dhcp4/dhcp4_srv.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -243,6 +244,15 @@ public: return (network_state_); } + /// @brief Returns an object which controls access to the configuration + /// backends. + /// + /// @return Pointer to the instance of the object which controls + /// access to the configuration backends. + CBControlDHCPv4Ptr getCBControl() const { + return (cb_control_); + } + /// @brief returns Kea version on stdout and exit. /// redeclaration/redefinition. @ref isc::process::Daemon::getVersion() static std::string getVersion(bool extended); @@ -973,6 +983,9 @@ protected: /// disabled subnet/network scopes. NetworkStatePtr network_state_; + /// @brief Controls access to the configuration backends. + CBControlDHCPv4Ptr cb_control_; + public: /// Class methods for DHCPv4-over-DHCPv6 handler diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index 549e0957bd..9851dbf16e 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -623,7 +624,7 @@ configureDhcp4Server(Dhcpv4Srv& server, isc::data::ConstElementPtr config_set, libraries.loadLibraries(); // If there are config backends, fetch and merge into staging config - databaseConfigFetch(srv_cfg); + server.getCBControl()->databaseConfigFetch(srv_cfg, false); } catch (const isc::Exception& ex) { LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what()); @@ -656,101 +657,5 @@ configureDhcp4Server(Dhcpv4Srv& server, isc::data::ConstElementPtr config_set, return (answer); } -void databaseConfigFetch(const SrvConfigPtr& srv_cfg) { - - ConfigBackendDHCPv4Mgr& mgr = ConfigBackendDHCPv4Mgr::instance(); - - // Close any existing CB databasess, then open all in srv_cfg (if any) - if (!databaseConfigConnect(srv_cfg)) { - // There are no CB databases so we're done - return; - } - - LOG_INFO(dhcp4_logger, DHCP4_CONFIG_FETCH); - - // For now we find data based on first backend that has it. - BackendSelector backend_selector(BackendSelector::Type::UNSPEC); - - // Use the server_tag if set, otherwise use ALL. - std::string server_tag = srv_cfg->getServerTag(); - ServerSelector& server_selector = (server_tag.empty()? ServerSelector::ALL() - : ServerSelector::ONE(server_tag)); - // Create the external config into which we'll fetch backend config data. - SrvConfigPtr external_cfg = CfgMgr::instance().createExternalCfg(); - - // First let's fetch the globals and add them to external config. - data::StampedValueCollection globals; - globals = mgr.getPool()->getAllGlobalParameters4(backend_selector, server_selector); - addGlobalsToConfig(external_cfg, globals); - - // Now we fetch the option definitions and add them. - OptionDefContainer option_defs = mgr.getPool()->getAllOptionDefs4(backend_selector, - server_selector); - for (auto option_def = option_defs.begin(); option_def != option_defs.end(); ++option_def) { - external_cfg->getCfgOptionDef()->add((*option_def), (*option_def)->getOptionSpaceName()); - } - - // Next fetch the options. They are returned as a container of OptionDescriptors. - OptionContainer options = mgr.getPool()->getAllOptions4(backend_selector, server_selector); - for (auto option = options.begin(); option != options.end(); ++option) { - external_cfg->getCfgOption()->add((*option), (*option).space_name_); - } - - // Now fetch the shared networks. - SharedNetwork4Collection networks = mgr.getPool()->getAllSharedNetworks4(backend_selector, - server_selector); - for (auto network = networks.begin(); network != networks.end(); ++network) { - external_cfg->getCfgSharedNetworks4()->add((*network)); - } - - // Next we fetch subnets. - Subnet4Collection subnets = mgr.getPool()->getAllSubnets4(backend_selector, server_selector); - for (auto subnet = subnets.begin(); subnet != subnets.end(); ++subnet) { - external_cfg->getCfgSubnets4()->add((*subnet)); - } - - // Now we merge the fecthed configuration into the staging configuration. - CfgMgr::instance().mergeIntoStagingCfg(external_cfg->getSequence()); - LOG_INFO(dhcp4_logger, DHCP4_CONFIG_MERGED); -} - -bool databaseConfigConnect(const SrvConfigPtr& srv_cfg) { - // We need to get rid of any existing backends. These would be any - // opened by previous configuration cycle. - ConfigBackendDHCPv4Mgr& mgr = ConfigBackendDHCPv4Mgr::instance(); - mgr.delAllBackends(); - - // Fetch the config-control info. - ConstConfigControlInfoPtr config_ctl = srv_cfg->getConfigControlInfo(); - if (!config_ctl || config_ctl->getConfigDatabases().empty()) { - // No config dbs, nothing to do. - return (false); - } - - // Iterate over the configured DBs and instantiate them. - for (auto db : config_ctl->getConfigDatabases()) { - LOG_INFO(dhcp4_logger, DHCP4_OPEN_CONFIG_DB) - .arg(db.redactedAccessString()); - mgr.addBackend(db.getAccessString()); - } - - // Let the caller know we have opened DBs. - return (true); -} - - -void addGlobalsToConfig(SrvConfigPtr external_cfg, data::StampedValueCollection& cb_globals) { - const auto& index = cb_globals.get(); - for (auto cb_global = index.begin(); cb_global != index.end(); ++cb_global) { - - if ((*cb_global)->amNull()) { - continue; - } - - external_cfg->addConfiguredGlobal((*cb_global)->getName(), - (*cb_global)->getElementValue()); - } -} - }; // end of isc::dhcp namespace }; // end of isc namespace diff --git a/src/bin/dhcp4/json_config_parser.h b/src/bin/dhcp4/json_config_parser.h index 9a3cfe5131..8d1cb9057e 100644 --- a/src/bin/dhcp4/json_config_parser.h +++ b/src/bin/dhcp4/json_config_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -60,47 +60,6 @@ configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set, bool check_only = false); -/// @brief Fetch and merge data from config backends into the staging config -/// -/// If the given SrvConfig specifies one or more config backends it calls -/// @c databaseConfigConnect() to open connections to them, otherwise it -/// simply returns. Next it creates an external SrvConfig instance, -/// and populates with data it fetches from the config backends. -/// Finally, it merges this external config into the staging config. -/// -/// @param srv_cfg server configuration that (may) specify the backends -/// should be merged -void -databaseConfigFetch(const SrvConfigPtr& srv_cfg); - -/// @brief Attempts to connect to configured CB databases -/// -/// First, this function will close all existing CB backends. It -/// will then attempt to connect to all of the CB backends defined -/// in the given SrvConfig (if any). -/// -/// It will return true if there are configured CB databases, -/// and false otherwise. Any errors encountered along the way -/// should generate throws. -/// -/// @param srv_cfg Server configuration from which to get -/// the config-control information to use. -/// -/// @return True if there are configured CB databases, false if not. -bool -databaseConfigConnect(const SrvConfigPtr& srv_cfg); - -/// @brief Adds globals fetched from config backend(s) to a SrvConfig instance -/// -/// Iterates over the given collection of global parameters and adds them to the -/// given configuration's list of configured globals. -/// -/// @param external_cfg SrvConfig instance to update -/// @param cb_globals collection of global parameters supplied by configuration -/// backend -void addGlobalsToConfig(SrvConfigPtr external_cfg, - data::StampedValueCollection& cb_globals); - }; // end of isc::dhcp namespace }; // end of isc namespace