27#include <boost/algorithm/string/split.hpp>
28#include <boost/algorithm/string/classification.hpp>
29#include <boost/array.hpp>
30#include <boost/foreach.hpp>
31#include <boost/pointer_cast.hpp>
32#include <boost/static_assert.hpp>
96 static const size_t HOST_ID_COL = 0;
97 static const size_t DHCP_IDENTIFIER_COL = 1;
98 static const size_t DHCP_IDENTIFIER_TYPE_COL = 2;
99 static const size_t DHCP4_SUBNET_ID_COL = 3;
100 static const size_t DHCP6_SUBNET_ID_COL = 4;
101 static const size_t IPV4_ADDRESS_COL = 5;
102 static const size_t HOSTNAME_COL = 6;
103 static const size_t DHCP4_CLIENT_CLASSES_COL = 7;
104 static const size_t DHCP6_CLIENT_CLASSES_COL = 8;
105 static const size_t USER_CONTEXT_COL = 9;
106 static const size_t DHCP4_NEXT_SERVER_COL = 10;
107 static const size_t DHCP4_SERVER_HOSTNAME_COL = 11;
108 static const size_t DHCP4_BOOT_FILE_NAME_COL = 12;
109 static const size_t AUTH_KEY_COL = 13;
111 static const size_t HOST_COLUMNS = 14;
121 PgSqlHostExchange(
const size_t additional_columns_num = 0)
122 : PgSqlExchange(HOST_COLUMNS + additional_columns_num) {
127 columns_[HOST_ID_COL] =
"host_id";
128 columns_[DHCP_IDENTIFIER_COL] =
"dhcp_identifier";
129 columns_[DHCP_IDENTIFIER_TYPE_COL] =
"dhcp_identifier_type";
130 columns_[DHCP4_SUBNET_ID_COL] =
"dhcp4_subnet_id";
131 columns_[DHCP6_SUBNET_ID_COL] =
"dhcp6_subnet_id";
132 columns_[IPV4_ADDRESS_COL] =
"ipv4_address";
133 columns_[HOSTNAME_COL] =
"hostname";
134 columns_[DHCP4_CLIENT_CLASSES_COL] =
"dhcp4_client_classes";
135 columns_[DHCP6_CLIENT_CLASSES_COL] =
"dhcp6_client_classes";
136 columns_[USER_CONTEXT_COL] =
"user_context";
137 columns_[DHCP4_NEXT_SERVER_COL] =
"dhcp4_next_server";
138 columns_[DHCP4_SERVER_HOSTNAME_COL] =
"dhcp4_server_hostname";
139 columns_[DHCP4_BOOT_FILE_NAME_COL] =
"dhcp4_boot_file_name";
140 columns_[AUTH_KEY_COL] =
"auth_key";
142 BOOST_STATIC_ASSERT(12 < HOST_COLUMNS);
146 virtual ~PgSqlHostExchange() {
154 virtual void clear() {
172 size_t findAvailColumn()
const {
173 std::vector<std::string>::const_iterator empty_column =
174 std::find(columns_.begin(), columns_.end(), std::string());
175 return (std::distance(columns_.begin(), empty_column));
182 HostID getHostId(
const PgSqlResult& r,
int row) {
184 getColumnValue(r, row, HOST_ID_COL, host_id);
205 isc_throw(BadValue,
"createBindForSend:: host object is NULL");
217 bind_array->add(host->getIdentifier());
220 bind_array->add(host->getIdentifierType());
223 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED) {
224 bind_array->addNull();
226 bind_array->add(host->getIPv4SubnetID());
230 if (host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
231 bind_array->addNull();
233 bind_array->add(host->getIPv6SubnetID());
237 bind_array->add((host->getIPv4Reservation()));
240 bind_array->add(host->getHostname());
244 bind_array->addTempString(host->getClientClasses4().toText(
","));
247 bind_array->addTempString(host->getClientClasses6().toText(
","));
252 std::string user_context_ = ctx->str();
253 bind_array->addTempString(user_context_);
255 bind_array->addNull();
259 bind_array->add((host->getNextServer()));
262 bind_array->add(host->getServerHostname());
265 bind_array->add(host->getBootFileName());
268 std::string key = host->getKey().toText();
270 bind_array->addNull();
272 bind_array->addTempString(key);
279 bind_array->add(host->getIPv4Reservation());
280 bind_array->add(host->getIPv4SubnetID());
283 }
catch (
const std::exception& ex) {
286 "Could not create bind array from Host: "
287 << host->getHostname() <<
", reason: " << ex.what());
307 const PgSqlResult& r,
int row) {
311 HostID row_host_id = getHostId(r, row);
316 if (hosts.empty() || row_host_id != hosts.back()->getHostId()) {
317 HostPtr host = retrieveHost(r, row, row_host_id);
318 hosts.push_back(host);
333 HostPtr retrieveHost(
const PgSqlResult& r,
int row,
334 const HostID& peeked_host_id = 0) {
338 HostID host_id = (peeked_host_id ? peeked_host_id : getHostId(r,row));
341 uint8_t identifier_value[DHCP_IDENTIFIER_MAX_LEN];
342 size_t identifier_len;
343 convertFromBytea(r, row, DHCP_IDENTIFIER_COL, identifier_value,
344 sizeof(identifier_value), identifier_len);
348 getColumnValue(r, row, DHCP_IDENTIFIER_TYPE_COL, type);
349 if (type > MAX_IDENTIFIER_TYPE) {
350 isc_throw(BadValue,
"invalid dhcp identifier type returned: "
351 <<
static_cast<int>(type));
358 uint32_t subnet_id(SUBNET_ID_UNUSED);
359 if (!isColumnNull(r, row, DHCP4_SUBNET_ID_COL)) {
360 getColumnValue(r, row, DHCP4_SUBNET_ID_COL, subnet_id);
365 subnet_id = SUBNET_ID_UNUSED;
366 if (!isColumnNull(r, row, DHCP6_SUBNET_ID_COL)) {
367 getColumnValue(r, row, DHCP6_SUBNET_ID_COL, subnet_id);
373 if (!isColumnNull(r, row, IPV4_ADDRESS_COL)) {
374 getColumnValue(r, row, IPV4_ADDRESS_COL, addr4);
376 isc::asiolink::IOAddress ipv4_reservation(addr4);
379 std::string hostname;
380 if (!isColumnNull(r, row, HOSTNAME_COL)) {
381 getColumnValue(r, row, HOSTNAME_COL, hostname);
385 std::string dhcp4_client_classes;
386 if (!isColumnNull(r, row, DHCP4_CLIENT_CLASSES_COL)) {
387 getColumnValue(r, row, DHCP4_CLIENT_CLASSES_COL, dhcp4_client_classes);
391 std::string dhcp6_client_classes;
392 if (!isColumnNull(r, row, DHCP6_CLIENT_CLASSES_COL)) {
393 getColumnValue(r, row, DHCP6_CLIENT_CLASSES_COL, dhcp6_client_classes);
397 std::string user_context;
398 if (!isColumnNull(r, row, USER_CONTEXT_COL)) {
399 getColumnValue(r, row, USER_CONTEXT_COL, user_context);
403 uint32_t dhcp4_next_server_as_uint32(0);
404 if (!isColumnNull(r, row, DHCP4_NEXT_SERVER_COL)) {
405 getColumnValue(r, row, DHCP4_NEXT_SERVER_COL, dhcp4_next_server_as_uint32);
407 isc::asiolink::IOAddress dhcp4_next_server(dhcp4_next_server_as_uint32);
410 std::string dhcp4_server_hostname;
411 if (!isColumnNull(r, row, DHCP4_SERVER_HOSTNAME_COL)) {
412 getColumnValue(r, row, DHCP4_SERVER_HOSTNAME_COL, dhcp4_server_hostname);
416 std::string dhcp4_boot_file_name;
417 if (!isColumnNull(r, row, DHCP4_BOOT_FILE_NAME_COL)) {
418 getColumnValue(r, row, DHCP4_BOOT_FILE_NAME_COL, dhcp4_boot_file_name);
422 std::string auth_key;
423 if (!isColumnNull(r, row, AUTH_KEY_COL)) {
424 getColumnValue(r, row, AUTH_KEY_COL, auth_key);
430 host.reset(
new Host(identifier_value, identifier_len,
431 identifier_type, dhcp4_subnet_id,
432 dhcp6_subnet_id, ipv4_reservation, hostname,
433 dhcp4_client_classes, dhcp6_client_classes,
434 dhcp4_next_server, dhcp4_server_hostname,
435 dhcp4_boot_file_name, AuthKey(auth_key)));
438 if (!user_context.empty()) {
442 isc_throw(BadValue,
"user context '" << user_context
443 <<
"' is not a JSON map");
445 host->setContext(ctx);
446 }
catch (
const isc::data::JSONError& ex) {
447 isc_throw(BadValue,
"user context '" << user_context
448 <<
"' is invalid JSON: " << ex.
what());
452 host->setHostId(host_id);
453 }
catch (
const isc::Exception& ex) {
454 isc_throw(DbOperationError,
"Could not create host: " << ex.
what());
475class PgSqlHostWithOptionsExchange :
public PgSqlHostExchange {
479 static const size_t OPTION_COLUMNS = 8;
495 class OptionProcessor {
505 const size_t start_column)
506 : universe_(universe), start_column_(start_column),
507 option_id_index_(start_column), code_index_(start_column_ + 1),
508 value_index_(start_column_ + 2),
509 formatted_value_index_(start_column_ + 3),
510 space_index_(start_column_ + 4),
511 persistent_index_(start_column_ + 5),
512 cancelled_index_(start_column_ + 6),
513 user_context_index_(start_column_ + 7),
514 most_recent_option_id_(0) {
522 most_recent_option_id_ = 0;
557 void retrieveOption(
const CfgOptionPtr& cfg,
const PgSqlResult& r,
572 if (most_recent_option_id_ >= option_id) {
578 most_recent_option_id_ = option_id;
587 if (!isColumnNull(r, row, value_index_)) {
589 sizeof(value), value_len);
593 std::string formatted_value;
594 if (!isColumnNull(r, row, formatted_value_index_)) {
601 if (!isColumnNull(r, row, space_index_)) {
622 std::string user_context;
623 if (!isColumnNull(r, row, user_context_index_)) {
666 option.reset(
new Option(universe_, code, buf.begin(),
673 if (formatted_value.empty()) {
675 option = def->optionFactory(universe_, code, buf.begin(),
680 std::vector<std::string> split_vec;
681 boost::split(split_vec, formatted_value,
682 boost::is_any_of(
","));
683 option = def->optionFactory(universe_, code, split_vec);
687 OptionDescriptor desc(option, persistent, cancelled,
691 if (!user_context.empty()) {
695 isc_throw(BadValue,
"user context '" << user_context
696 <<
"' is no a JSON map");
698 desc.setContext(ctx);
699 }
catch (
const isc::data::JSONError& ex) {
700 isc_throw(BadValue,
"user context '" << user_context
701 <<
"' is invalid JSON: " << ex.
what());
705 cfg->add(desc, space);
712 void setColumnNames(std::vector<std::string>& columns) {
713 columns[option_id_index_] =
"option_id";
714 columns[code_index_] =
"code";
715 columns[value_index_] =
"value";
716 columns[formatted_value_index_] =
"formatted_value";
717 columns[space_index_] =
"space";
718 columns[persistent_index_] =
"persistent";
719 columns[cancelled_index_] =
"cancelled";
720 columns[user_context_index_] =
"user_context";
728 size_t start_column_;
735 size_t option_id_index_;
744 size_t formatted_value_index_;
750 size_t persistent_index_;
753 size_t cancelled_index_;
757 size_t user_context_index_;
760 uint64_t most_recent_option_id_;
764 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
774 enum FetchedOptions {
788 PgSqlHostWithOptionsExchange(
const FetchedOptions& fetched_options,
789 const size_t additional_columns_num = 0)
790 : PgSqlHostExchange(getRequiredColumnsNum(fetched_options)
791 + additional_columns_num),
792 opt_proc4_(), opt_proc6_() {
795 if ((fetched_options == DHCP4_ONLY) ||
796 (fetched_options == DHCP4_AND_DHCP6)) {
797 opt_proc4_.reset(
new OptionProcessor(
Option::V4,
799 opt_proc4_->setColumnNames(columns_);
803 if ((fetched_options == DHCP6_ONLY) ||
804 (fetched_options == DHCP4_AND_DHCP6)) {
805 opt_proc6_.reset(
new OptionProcessor(
Option::V6,
807 opt_proc6_->setColumnNames(columns_);
816 virtual void clear() {
817 PgSqlHostExchange::clear();
837 const PgSqlResult& r,
int row) {
841 current_host = retrieveHost(r, row);
842 hosts.push_back(current_host);
847 HostID row_host_id = getHostId(r, row);
848 current_host = boost::const_pointer_cast<Host>(hosts.back());
852 if (row_host_id > current_host->getHostId()) {
853 current_host = retrieveHost(r, row, row_host_id);
854 hosts.push_back(current_host);
861 opt_proc4_->retrieveOption(cfg, r, row);
867 opt_proc6_->retrieveOption(cfg, r, row);
884 static size_t getRequiredColumnsNum(
const FetchedOptions& fetched_options) {
885 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
892 OptionProcessorPtr opt_proc4_;
897 OptionProcessorPtr opt_proc6_;
912class PgSqlHostIPv6Exchange :
public PgSqlHostWithOptionsExchange {
916 static const size_t RESERVATION_COLUMNS = 5;
924 PgSqlHostIPv6Exchange(
const FetchedOptions& fetched_options)
925 : PgSqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
926 reservation_id_index_(findAvailColumn()),
927 address_index_(reservation_id_index_ + 1),
928 prefix_len_index_(reservation_id_index_ + 2),
929 type_index_(reservation_id_index_ + 3),
930 iaid_index_(reservation_id_index_ + 4),
931 most_recent_reservation_id_(0) {
934 columns_[reservation_id_index_] =
"reservation_id";
935 columns_[address_index_] =
"address";
936 columns_[prefix_len_index_] =
"prefix_len";
937 columns_[type_index_] =
"type";
938 columns_[iaid_index_] =
"dhcp6_iaid";
940 BOOST_STATIC_ASSERT(4 < RESERVATION_COLUMNS);
949 PgSqlHostWithOptionsExchange::clear();
950 most_recent_reservation_id_ = 0;
956 uint64_t getReservationId(
const PgSqlResult& r,
int row)
const {
957 uint64_t resv_id = 0;
958 if (!isColumnNull(r, row, reservation_id_index_)) {
959 getColumnValue(r, row, reservation_id_index_, resv_id);
969 IPv6Resrv retrieveReservation(
const PgSqlResult& r,
int row) {
973 getColumnValue(r, row, type_index_, tmp);
988 "invalid IPv6 reservation type returned: "
989 << tmp <<
". Only 0 or 2 are allowed.");
993 isc::asiolink::IOAddress address(getIPv6Value(r, row, address_index_));
997 getColumnValue(r, row, prefix_len_index_, prefix_len);
1005 IPv6Resrv reservation(resv_type, IOAddress(address), prefix_len);
1006 return (reservation);
1031 const PgSqlResult& r,
int row) {
1033 PgSqlHostWithOptionsExchange::processRowData(hosts, r, row);
1036 if (hosts.empty()) {
1037 isc_throw(Unexpected,
"no host information while retrieving"
1038 " IPv6 reservation");
1043 uint64_t reservation_id = getReservationId(r, row);
1044 if (reservation_id && (reservation_id > most_recent_reservation_id_)) {
1045 HostPtr host = boost::const_pointer_cast<Host>(hosts.back());
1046 host->addReservation(retrieveReservation(r, row));
1047 most_recent_reservation_id_ = reservation_id;
1055 size_t reservation_id_index_;
1058 size_t address_index_;
1061 size_t prefix_len_index_;
1072 uint64_t most_recent_reservation_id_;
1089 static const size_t RESRV_COLUMNS = 6;
1096 PgSqlIPv6ReservationExchange()
1097 : PgSqlExchange(RESRV_COLUMNS),
1098 resv_(IPv6Resrv::TYPE_NA, asiolink::IOAddress(
"::"), 128) {
1100 columns_[0] =
"host_id";
1101 columns_[1] =
"address";
1102 columns_[2] =
"prefix_len";
1103 columns_[3] =
"type";
1104 columns_[4] =
"dhcp6_iaid";
1106 BOOST_STATIC_ASSERT(5 < RESRV_COLUMNS);
1125 const bool unique_ip) {
1143 bind_array->add(type);
1147 bind_array->addNull();
1150 bind_array->add(host_id);
1159 }
catch (
const std::exception& ex) {
1161 "Could not create bind array from IPv6 Reservation: "
1162 << resv_.toText() <<
", reason: " << ex.what());
1165 return (bind_array);
1179 static const size_t OPTION_ID_COL = 0;
1180 static const size_t CODE_COL = 1;
1181 static const size_t VALUE_COL = 2;
1182 static const size_t FORMATTED_VALUE_COL = 3;
1183 static const size_t SPACE_COL = 4;
1184 static const size_t PERSISTENT_COL = 5;
1185 static const size_t CANCELLED_COL = 6;
1186 static const size_t USER_CONTEXT_COL = 7;
1187 static const size_t DHCP_SUBNET_ID_COL = 8;
1188 static const size_t HOST_ID_COL = 9;
1190 static const size_t OPTION_COLUMNS = 10;
1195 PgSqlOptionExchange()
1196 : PgSqlExchange(OPTION_COLUMNS), value_(),
1197 value_len_(0), option_() {
1198 columns_[OPTION_ID_COL] =
"option_id";
1199 columns_[CODE_COL] =
"code";
1200 columns_[VALUE_COL] =
"value";
1201 columns_[FORMATTED_VALUE_COL] =
"formatted_value";
1202 columns_[SPACE_COL] =
"space";
1203 columns_[PERSISTENT_COL] =
"persistent";
1204 columns_[CANCELLED_COL] =
"cancelled";
1205 columns_[USER_CONTEXT_COL] =
"user_context";
1206 columns_[DHCP_SUBNET_ID_COL] =
"dhcp_subnet_id";
1207 columns_[HOST_ID_COL] =
"host_id";
1209 BOOST_STATIC_ASSERT(10 <= OPTION_COLUMNS);
1221 const std::string& opt_space,
1234 bind_array->add(option_->getType());
1242 OutputBuffer buf(opt_desc.
option_->len());
1244 const uint8_t* buf_ptr = buf.getData();
1245 value_.assign(buf_ptr + opt_desc.
option_->getHeaderLen(),
1246 buf_ptr + buf.getLength());
1247 value_len_ = value_.size();
1248 bind_array->add(value_);
1259 bind_array->addNull();
1263 if (!opt_space.empty()) {
1264 bind_array->addTempString(opt_space);
1266 bind_array->addNull();
1278 std::string user_context_ = ctx->str();
1279 bind_array->addTempString(user_context_);
1281 bind_array->addNull();
1286 isc_throw(BadValue,
"host_id cannot be null");
1288 bind_array->add(host_id);
1290 }
catch (
const std::exception& ex) {
1292 "Could not create bind array for inserting DHCP "
1293 "host option: " << option_->toText() <<
", reason: "
1297 return (bind_array);
1303 std::vector<uint8_t> value_;
1504 const bool return_last_id =
false);
1538 const std::string& opt_space,
1553 const uint64_t host_id);
1576 boost::shared_ptr<PgSqlHostExchange> exchange,
1600 const uint8_t* identifier_begin,
1601 const size_t identifier_len,
1603 boost::shared_ptr<PgSqlHostExchange> exchange)
const;
1625 std::pair<uint32_t, uint32_t>
getVersion(
const std::string& timer_name = std::string())
const;
1648typedef boost::array<PgSqlTaggedStatement, PgSqlHostDataSourceImpl::NUM_STATEMENTS>
1649TaggedStatementArray;
1653TaggedStatementArray tagged_statements = { {
1662 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1663 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1664 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1666 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1667 " h.dhcp4_boot_file_name, h.auth_key, "
1668 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1669 " o4.persistent, o4.cancelled, o4.user_context, "
1670 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1671 " o6.persistent, o6.cancelled, o6.user_context, "
1672 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1674 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1675 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1676 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1677 "WHERE dhcp_identifier = $1 AND dhcp_identifier_type = $2 "
1678 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1688 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1689 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1690 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1691 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1692 " h.dhcp4_boot_file_name, h.auth_key, "
1693 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1694 " o.persistent, o.cancelled, o.user_context "
1696 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1697 "WHERE ipv4_address = $1 "
1698 "ORDER BY h.host_id, o.option_id"
1707 "get_host_subid4_dhcpid",
1708 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1709 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1710 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1711 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1712 " h.dhcp4_boot_file_name, h.auth_key, "
1713 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1714 " o.persistent, o.cancelled, o.user_context "
1716 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1717 "WHERE h.dhcp4_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1718 " AND h.dhcp_identifier = $3 "
1719 "ORDER BY h.host_id, o.option_id"
1728 "get_host_subid6_dhcpid",
1729 "SELECT h.host_id, h.dhcp_identifier, "
1730 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1731 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1732 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1733 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1734 " h.dhcp4_boot_file_name, h.auth_key, "
1735 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1736 " o.persistent, o.cancelled, o.user_context, "
1737 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1739 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1740 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1741 "WHERE h.dhcp6_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1742 " AND h.dhcp_identifier = $3 "
1743 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1753 "get_host_subid_addr",
1754 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1755 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1756 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1757 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1758 " h.dhcp4_boot_file_name, h.auth_key, "
1759 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1760 " o.persistent, o.cancelled, o.user_context "
1762 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1763 "WHERE h.dhcp4_subnet_id = $1 AND h.ipv4_address = $2 "
1764 "ORDER BY h.host_id, o.option_id"
1777 "SELECT h.host_id, h.dhcp_identifier, "
1778 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1779 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1780 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1781 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1782 " h.dhcp4_boot_file_name, h.auth_key, "
1783 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1784 " o.persistent, o.cancelled, o.user_context, "
1785 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1788 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1789 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1790 "WHERE h.host_id = "
1791 " (SELECT host_id FROM ipv6_reservations "
1792 " WHERE address = cast($1 as inet) AND prefix_len = $2) "
1793 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1805 "get_host_subid6_addr",
1806 "SELECT h.host_id, h.dhcp_identifier, "
1807 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1808 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1809 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1810 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1811 " h.dhcp4_boot_file_name, h.auth_key, "
1812 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1813 " o.persistent, o.cancelled, o.user_context, "
1814 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1817 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1818 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1819 "WHERE h.dhcp6_subnet_id = $1 AND h.host_id IN "
1820 " (SELECT host_id FROM ipv6_reservations "
1821 " WHERE address = cast($2 as inet)) "
1822 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1836 "SELECT h.host_id, h.dhcp_identifier, "
1837 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1838 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1839 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1840 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1841 " h.dhcp4_boot_file_name, h.auth_key, "
1842 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1843 " o.persistent, o.cancelled, o.user_context, "
1844 " r.reservation_id, r.address, r.prefix_len, r.type, "
1847 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1848 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1849 "WHERE h.host_id IN "
1850 " (SELECT host_id FROM ipv6_reservations "
1851 " WHERE address = cast($1 as inet)) "
1852 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1864 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1865 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1866 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1867 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1868 " h.dhcp4_boot_file_name, h.auth_key, "
1869 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1870 " o.persistent, o.cancelled, o.user_context "
1872 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1873 "WHERE h.dhcp4_subnet_id = $1 "
1874 "ORDER BY h.host_id, o.option_id"
1890 "SELECT h.host_id, h.dhcp_identifier, "
1891 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1892 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1893 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1894 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1895 " h.dhcp4_boot_file_name, h.auth_key, "
1896 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1897 " o.persistent, o.cancelled, o.user_context, "
1898 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1900 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1901 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1902 "WHERE h.dhcp6_subnet_id = $1 "
1903 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1914 "get_host_hostname",
1915 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1916 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1917 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1919 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1920 " h.dhcp4_boot_file_name, h.auth_key, "
1921 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1922 " o4.persistent, o4.cancelled, o4.user_context, "
1923 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1924 " o6.persistent, o6.cancelled, o6.user_context, "
1925 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1927 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1928 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1929 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1930 "WHERE lower(h.hostname) = $1 "
1931 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1941 "get_host_hostname_subid4",
1942 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1943 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1944 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1945 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1946 " h.dhcp4_boot_file_name, h.auth_key, "
1947 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1948 " o.persistent, o.cancelled, o.user_context "
1950 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1951 "WHERE lower(h.hostname) = $1 AND h.dhcp4_subnet_id = $2 "
1952 "ORDER BY h.host_id, o.option_id"
1964 "get_host_hostname_subid6",
1965 "SELECT h.host_id, h.dhcp_identifier, "
1966 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1967 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1968 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1969 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1970 " h.dhcp4_boot_file_name, h.auth_key, "
1971 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1972 " o.persistent, o.cancelled, o.user_context, "
1973 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1975 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1976 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1977 "WHERE lower(h.hostname) = $1 AND h.dhcp6_subnet_id = $2 "
1978 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1988 "get_host_subid4_page",
1989 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1990 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1991 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1992 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1993 " h.dhcp4_boot_file_name, h.auth_key, "
1994 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1995 " o.persistent, o.cancelled, o.user_context "
1996 "FROM ( SELECT * FROM hosts AS h "
1997 " WHERE h.dhcp4_subnet_id = $1 AND h.host_id > $2 "
1998 " ORDER BY h.host_id "
2000 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2001 "ORDER BY h.host_id, o.option_id"
2013 "get_host_subid6_page",
2014 "SELECT h.host_id, h.dhcp_identifier, "
2015 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2016 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2017 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2018 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2019 " h.dhcp4_boot_file_name, h.auth_key, "
2020 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2021 " o.persistent, o.cancelled, o.user_context, "
2022 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2023 "FROM ( SELECT * FROM hosts AS h "
2024 " WHERE h.dhcp6_subnet_id = $1 AND h.host_id > $2 "
2025 " ORDER BY h.host_id "
2027 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2028 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2029 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2040 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
2041 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2042 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2043 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2044 " h.dhcp4_boot_file_name, h.auth_key, "
2045 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2046 " o.persistent, o.cancelled, o.user_context "
2047 "FROM ( SELECT * FROM hosts AS h "
2048 " WHERE h.host_id > $1 "
2049 " ORDER BY h.host_id "
2051 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2052 "ORDER BY h.host_id, o.option_id"
2065 "SELECT h.host_id, h.dhcp_identifier, "
2066 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2067 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2068 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2069 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2070 " h.dhcp4_boot_file_name, h.auth_key, "
2071 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2072 " o.persistent, o.cancelled, o.user_context, "
2073 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2074 "FROM ( SELECT * FROM hosts AS h "
2075 " WHERE h.host_id > $1 "
2076 " ORDER BY h.host_id "
2078 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2079 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2080 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2091 "insert_host_non_unique_ip",
2092 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2093 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2094 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2095 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2096 "VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 ) "
2117 "insert_host_unique_ip",
2118 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2119 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2120 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2121 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2122 " SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13"
2123 " WHERE NOT EXISTS ("
2124 " SELECT 1 FROM hosts WHERE ipv4_address = $14 AND dhcp4_subnet_id = $15"
2135 "insert_v6_resrv_non_unique",
2136 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2137 " dhcp6_iaid, host_id) "
2138 "VALUES (cast($1 as inet), $2, $3, $4, $5)"
2146 "insert_v6_resrv_unique",
2147 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2148 " dhcp6_iaid, host_id) "
2149 "SELECT cast($1 as inet), $2, $3, $4, $5 "
2150 " WHERE NOT EXISTS ("
2151 " SELECT 1 FROM ipv6_reservations"
2152 " WHERE address = cast($6 as inet) AND prefix_len = $7"
2163 "insert_v4_host_option",
2164 "INSERT INTO dhcp4_options(code, value, formatted_value, space, "
2165 " persistent, cancelled, user_context, host_id, scope_id) "
2166 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2175 "insert_v6_host_option",
2176 "INSERT INTO dhcp6_options(code, value, formatted_value, space, "
2177 " persistent, cancelled, user_context, host_id, scope_id) "
2178 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2186 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 AND ipv4_address = $2"
2194 "DELETE FROM hosts USING ipv6_reservations "
2195 " WHERE hosts.host_id = ipv6_reservations.host_id"
2196 " AND dhcp6_subnet_id = $1 AND ipv6_reservations.address = cast($2 as inet)"
2203 "del_host_subid4_id",
2204 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 "
2205 "AND dhcp_identifier_type = $2 "
2206 "AND dhcp_identifier = $3"
2213 "del_host_subid6_id",
2214 "DELETE FROM hosts WHERE dhcp6_subnet_id = $1 "
2215 "AND dhcp_identifier_type = $2 "
2216 "AND dhcp_identifier = $3"
2228 :
conn_(parameters, io_service_accessor, db_reconnect_callback),
2241 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2242 if (!mgr_.pool_->pool_.empty()) {
2243 ctx_ = mgr_.pool_->pool_.back();
2244 mgr_.pool_->pool_.pop_back();
2248 ctx_ = mgr_.createContext();
2252 if (mgr_.pool_->pool_.empty()) {
2255 ctx_ = mgr_.pool_->pool_.back();
2262 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2263 mgr_.pool_->pool_.push_back(
ctx_);
2264 if (
ctx_->conn_.isUnusable()) {
2265 mgr_.unusable_ =
true;
2267 }
else if (
ctx_->conn_.isUnusable()) {
2268 mgr_.unusable_ =
true;
2277 tls += parameters.count(
"trust-anchor");
2278 tls += parameters.count(
"cert-file");
2279 tls += parameters.count(
"key-file");
2280 tls += parameters.count(
"cipher-list");
2281#ifdef HAVE_PGSQL_SSL
2293 <<
"backend (built with this feature disabled)");
2299 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2320 ctx->conn_.openDatabase();
2323 ctx->conn_.prepareStatements(tagged_statements.begin(),
2328 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2332 if (!ctx->is_readonly_) {
2334 tagged_statements.end());
2339 ctx->host_ipv4_exchange_.reset(
new PgSqlHostWithOptionsExchange(PgSqlHostWithOptionsExchange::DHCP4_ONLY));
2340 ctx->host_ipv6_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP6_ONLY));
2341 ctx->host_ipv46_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2342 ctx->host_ipv6_reservation_exchange_.reset(
new PgSqlIPv6ReservationExchange());
2343 ctx->host_option_exchange_.reset(
new PgSqlOptionExchange());
2363 bool reopened =
false;
2365 const std::string timer_name = db_reconnect_ctl->timerName();
2366 bool check = db_reconnect_ctl->checkRetries();
2371 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2372 for (std::string& hds : host_db_access_list) {
2379 }
catch (
const std::exception& ex) {
2398 .arg(db_reconnect_ctl->maxRetries());
2411 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2412 .arg(db_reconnect_ctl->maxRetries())
2413 .arg(db_reconnect_ctl->retryInterval());
2419 db_reconnect_ctl->retryInterval(),
2432 const bool return_last_id) {
2433 uint64_t last_id = 0;
2434 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2435 tagged_statements[stindex].nbparams,
2436 &bind_array->values_[0],
2437 &bind_array->lengths_[0],
2438 &bind_array->formats_[0], 0));
2440 int s = PQresultStatus(r);
2442 if (s != PGRES_COMMAND_OK) {
2450 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2454 char* rows_affected = PQcmdTuples(r);
2455 if (!rows_affected) {
2457 "Could not retrieve the number of affected rows.");
2465 if (rows_affected[0] ==
'0') {
2469 if (return_last_id) {
2480 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2481 tagged_statements[stindex].nbparams,
2482 &bind_array->values_[0],
2483 &bind_array->lengths_[0],
2484 &bind_array->formats_[0], 0));
2486 int s = PQresultStatus(r);
2488 if (s != PGRES_COMMAND_OK) {
2491 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2496 char* rows_deleted = PQcmdTuples(r);
2497 if (!rows_deleted) {
2499 "Could not retrieve the number of deleted rows.");
2501 return (rows_deleted[0] !=
'0');
2520 const std::string& opt_space,
2523 PsqlBindArrayPtr bind_array = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space,
id);
2532 const uint64_t host_id) {
2535 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
2536 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
2537 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
2538 vendor_spaces.end());
2542 for (
auto const& space : option_spaces) {
2544 if (options && !options->empty()) {
2545 for (
auto const& opt : *options) {
2556 boost::shared_ptr<PgSqlHostExchange> exchange,
2558 bool single)
const {
2561 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2562 tagged_statements[stindex].nbparams,
2563 &bind_array->values_[0],
2564 &bind_array->lengths_[0],
2565 &bind_array->formats_[0], 0));
2567 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2570 for (
int row = 0; row < rows; ++row) {
2571 exchange->processRowData(
result, r, row);
2573 if (single &&
result.size() > 1) {
2575 "database where only one was expected for query "
2576 << tagged_statements[stindex].name);
2585 const uint8_t* identifier_begin,
2586 const size_t identifier_len,
2588 boost::shared_ptr<PgSqlHostExchange> exchange)
const {
2594 bind_array->add(subnet_id);
2597 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2600 bind_array->add(identifier_begin, identifier_len);
2607 if (!collection.empty()) {
2608 result = *collection.begin();
2614std::pair<uint32_t, uint32_t>
2626 if (ctx->is_readonly_) {
2628 " to operate in read only mode");
2643 return (impl_->parameters_);
2653 impl_->checkReadOnly(ctx);
2665 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
2666 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
2669 PsqlBindArrayPtr bind_array = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
2672 uint32_t host_id = impl_->addStatement(ctx,
2681 cfg_option4, host_id);
2688 cfg_option6, host_id);
2693 if (std::distance(v6resv.first, v6resv.second) > 0) {
2694 BOOST_FOREACH(
auto const& resv, v6resv) {
2695 impl_->addResv(ctx, resv.second, host_id);
2711 impl_->checkReadOnly(ctx);
2714 bind_array->add(subnet_id);
2718 bind_array->add(addr);
2724 bind_array->addTempString(addr.
toText());
2733 const uint8_t* identifier_begin,
2734 const size_t identifier_len) {
2740 impl_->checkReadOnly(ctx);
2745 bind_array->add(subnet_id);
2748 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2751 bind_array->add(identifier_begin, identifier_len);
2760 const uint8_t* identifier_begin,
2761 const size_t identifier_len) {
2767 impl_->checkReadOnly(ctx);
2772 bind_array->add(subnet_id);
2775 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2778 bind_array->add(identifier_begin, identifier_len);
2786 const uint8_t* identifier_begin,
2787 const size_t identifier_len)
const {
2796 bind_array->add(identifier_begin, identifier_len);
2799 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2803 bind_array, ctx->host_ipv46_exchange_,
result,
false);
2818 bind_array->add(subnet_id);
2822 bind_array, ctx->host_ipv4_exchange_,
result,
false);
2837 bind_array->add(subnet_id);
2841 bind_array, ctx->host_ipv6_exchange_,
result,
false);
2856 bind_array->add(hostname);
2860 bind_array, ctx->host_ipv46_exchange_,
result,
false);
2876 bind_array->add(hostname);
2879 bind_array->add(subnet_id);
2883 bind_array, ctx->host_ipv4_exchange_,
result,
false);
2899 bind_array->add(hostname);
2902 bind_array->add(subnet_id);
2906 bind_array, ctx->host_ipv6_exchange_,
result,
false);
2914 uint64_t lower_host_id,
2924 bind_array->add(subnet_id);
2927 bind_array->add(lower_host_id);
2930 string page_size_data =
2931 boost::lexical_cast<std::string>(page_size.
page_size_);
2932 bind_array->add(page_size_data);
2936 bind_array, ctx->host_ipv4_exchange_,
result,
false);
2944 uint64_t lower_host_id,
2954 bind_array->add(subnet_id);
2957 bind_array->add(lower_host_id);
2960 string page_size_data =
2961 boost::lexical_cast<std::string>(page_size.
page_size_);
2962 bind_array->add(page_size_data);
2966 bind_array, ctx->host_ipv6_exchange_,
result,
false);
2973 uint64_t lower_host_id,
2983 bind_array->add(lower_host_id);
2986 string page_size_data =
2987 boost::lexical_cast<std::string>(page_size.
page_size_);
2988 bind_array->add(page_size_data);
2992 bind_array, ctx->host_ipv4_exchange_,
result,
false);
2999 uint64_t lower_host_id,
3009 bind_array->add(lower_host_id);
3012 string page_size_data =
3013 boost::lexical_cast<std::string>(page_size.
page_size_);
3014 bind_array->add(page_size_data);
3018 bind_array, ctx->host_ipv6_exchange_,
result,
false);
3033 bind_array->add(address);
3037 bind_array, ctx->host_ipv4_exchange_,
result,
false);
3045 const uint8_t* identifier_begin,
3046 const size_t identifier_len)
const {
3051 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3053 ctx->host_ipv4_exchange_));
3063 if (!address.
isV4()) {
3065 " wrong address type, address supplied is an IPv6 address");
3072 bind_array->add(subnet_id);
3075 bind_array->add(address);
3079 bind_array, ctx->host_ipv4_exchange_, collection,
true);
3083 if (!collection.empty()) {
3084 result = *collection.begin();
3097 if (!address.
isV4()) {
3099 " wrong address type, address supplied is an IPv6 address");
3106 bind_array->add(subnet_id);
3109 bind_array->add(address);
3113 bind_array, ctx->host_ipv4_exchange_, collection,
false);
3114 return (collection);
3120 const uint8_t* identifier_begin,
3121 const size_t identifier_len)
const {
3126 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3128 ctx->host_ipv6_exchange_));
3133 const uint8_t prefix_len)
const {
3134 if (!prefix.
isV6()) {
3136 "wrong address type, address supplied is an IPv4 address");
3147 bind_array->add(prefix);
3150 bind_array->add(prefix_len);
3154 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3158 if (!collection.empty()) {
3159 result = *collection.begin();
3168 if (!address.
isV6()) {
3170 "wrong address type, address supplied is an IPv4 address");
3181 bind_array->add(subnet_id);
3184 bind_array->add(address);
3188 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3192 if (!collection.empty()) {
3193 result = *collection.begin();
3202 if (!address.
isV6()) {
3204 "wrong address type, address supplied is an IPv4 address");
3215 bind_array->add(subnet_id);
3218 bind_array->add(address);
3222 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3223 return (collection);
3228 if (!address.
isV6()) {
3230 "wrong address type, address supplied is an IPv4 address");
3241 bind_array->add(address);
3245 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3246 return (collection);
3256 impl_->checkReadOnly(ctx);
3279 std::string name =
"";
3285 name = ctx->conn_.getParameter(
"name");
3294 return (std::string(
"Host data source that stores host information"
3295 "in PostgreSQL database"));
3298std::pair<uint32_t, uint32_t>
3300 return (impl_->getVersion(timer_name));
3310 impl_->checkReadOnly(ctx);
3311 ctx->conn_.commit();
3321 impl_->checkReadOnly(ctx);
3322 ctx->conn_.rollback();
3327 impl_->ip_reservations_unique_ = unique;
3333 return (impl_->unusable_);
when the call the UDPServer carries on at the same position As a result
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static std::string redactedAccessString(const ParameterMap ¶meters)
Redact database access string.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Database duplicate entry error.
Multiple lease records found where one expected.
Common PgSql Connector Pool.
static bool warned_about_tls
Emit the TLS support warning only once.
static const char DUPLICATE_KEY[]
Define the PgSql error state for a duplicate key error.
static void ensureSchemaVersion(const ParameterMap ¶meters, const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Retrieve schema version, validate it against the hardcoded version, and attempt to initialize the sch...
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters, const IOServiceAccessorPtr &ac=IOServiceAccessorPtr(), const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string(), unsigned int id=0)
Get the schema version.
Base class for marshalling data to and from PostgreSQL.
static void convertFromBytea(const PgSqlResult &r, const int row, const size_t col, uint8_t *buffer, const size_t buffer_size, size_t &bytes_converted)
Converts a column in a row in a result set to a binary bytes.
static bool isColumnNull(const PgSqlResult &r, const int row, const size_t col)
Returns true if a column within a row is null.
static void getColumnValue(const PgSqlResult &r, const int row, const size_t col, std::string &value)
Fetches text column value as a string.
RAII wrapper for PostgreSQL Result sets.
int getRows() const
Returns the number of rows in the result set.
RAII object representing a PostgreSQL transaction.
void commit()
Commits transaction.
Attempt to modify data in read-only database.
virtual void update(HostPtr const &host)
Attempts to update an existing host entry.
static CfgMgr & instance()
returns a single instance of Configuration Manager
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Wraps value holding size of the page with host reservations.
const size_t page_size_
Holds page size.
IdentifierType
Type of the host identifier.
static const IdentifierType LAST_IDENTIFIER_TYPE
Constant pointing to the last identifier of the IdentifierType enumeration.
IPv6 reservation for a host.
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Type getType() const
Returns reservation type.
Type
Type of the reservation.
uint8_t getPrefixLen() const
Returns prefix length.
static OptionDefinitionPtr getOptionDef(const std::string &space, const uint16_t code)
Return the first option definition matching a particular option code.
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.
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
static OptionDefinitionPtr getRuntimeOptionDef(const std::string &space, const uint16_t code)
Returns runtime (non-standard) option definition by space and option code.
static OptionDefinitionPtr getLastResortOptionDef(const std::string &space, const uint16_t code)
Returns last resort option definition by space and option code.
static const unsigned int DB_CONNECTION
The network state is being altered by the DB connection recovery mechanics.
OptionPtr option_
Option instance.
bool cancelled_
Cancelled flag.
std::string formatted_value_
Option value in textual (CSV) format.
bool persistent_
Persistence flag.
Universe
defines option universe DHCPv4 or DHCPv6
PostgreSQL Host Context Pool.
std::mutex mutex_
The mutex to protect pool access.
std::vector< PgSqlHostContextPtr > pool_
The vector of available contexts.
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv46_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< PgSqlIPv6ReservationExchange > host_ipv6_reservation_exchange_
Pointer to an object representing an exchange which can be used to insert new IPv6 reservation.
PgSqlHostContext(const DatabaseConnection::ParameterMap ¶meters, IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
PgSqlConnection conn_
PostgreSQL connection.
boost::shared_ptr< PgSqlHostWithOptionsExchange > host_ipv4_exchange_
The exchange objects are used for transfer of data to/from the database.
bool is_readonly_
Indicates if the database is opened in read only mode.
boost::shared_ptr< PgSqlHostIPv6Exchange > host_ipv6_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< PgSqlOptionExchange > host_option_exchange_
Pointer to an object representing an exchange which can be used to insert DHCPv4 or DHCPv6 option int...
Implementation of the PgSqlHostDataSource.
bool ip_reservations_unique_
Holds the setting whether the IP reservations must be unique or may be non-unique.
uint64_t addStatement(PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind, const bool return_last_id=false)
Executes statements which insert a row into one of the tables.
static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the host DB backend manager.
std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const
Returns PostgreSQL schema version of the open database.
void checkReadOnly(PgSqlHostContextPtr &ctx) const
Throws exception if database is read only.
StatementIndex
Statement Tags.
@ GET_HOST_HOSTNAME_SUBID6
@ INSERT_HOST_NON_UNIQUE_IP
@ INSERT_V6_RESRV_NON_UNIQUE
@ GET_HOST_HOSTNAME_SUBID4
DatabaseConnection::ParameterMap parameters_
The parameters.
PgSqlHostContextPoolPtr pool_
The pool of contexts.
void addOption(PgSqlHostContextPtr &ctx, const PgSqlHostDataSourceImpl::StatementIndex &stindex, const OptionDescriptor &opt_desc, const std::string &opt_space, const Optional< SubnetID > &subnet_id, const HostID &host_id)
Inserts a single DHCP option into the database.
void addOptions(PgSqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
Inserts multiple options into the database.
bool delStatement(PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind)
Executes statements that delete records.
static const StatementIndex WRITE_STMTS_BEGIN
Index of first statement performing write to the database.
~PgSqlHostDataSourceImpl()
Destructor.
void addResv(PgSqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
Inserts IPv6 Reservation into ipv6_reservation table.
std::string timer_name_
Timer name used to register database reconnect timer.
ConstHostPtr getHost(PgSqlHostContextPtr &ctx, const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, StatementIndex stindex, boost::shared_ptr< PgSqlHostExchange > exchange) const
Retrieves a host by subnet and client's unique identifier.
PgSqlHostContextPtr createContext() const
Create a new context.
void getHostCollection(PgSqlHostContextPtr &ctx, StatementIndex stindex, PsqlBindArrayPtr bind, boost::shared_ptr< PgSqlHostExchange > exchange, ConstHostCollection &result, bool single) const
Creates collection of Host objects with associated information such as IPv6 reservations and/or DHCP ...
PgSqlHostDataSourceImpl(const DatabaseConnection::ParameterMap ¶meters)
Constructor.
bool unusable_
Indicates if there is at least one connection that can no longer be used for normal operations.
PgSqlHostContextPtr ctx_
The context.
~PgSqlHostContextAlloc()
Destructor.
PgSqlHostContextAlloc(PgSqlHostDataSourceImpl &mgr)
Constructor.
virtual bool del6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet6-id, identifier type, identifier)
virtual ConstHostPtr get4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv4 subnet.
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
virtual std::string getName() const
Returns the name of the open database.
virtual ConstHostCollection getAll(const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Return all hosts connected to any subnet for which reservations have been made using a specified iden...
virtual std::string getDescription() const
Returns description of the backend.
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.
virtual ConstHostCollection getPage6(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv6 subnet.
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)
Attempts to delete hosts by (subnet-id, address)
virtual ConstHostCollection getPage4(const SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const HostPageSize &page_size) const
Returns range of hosts in a DHCPv4 subnet.
virtual void rollback()
Rollback Transactions.
virtual bool del4(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet4-id, identifier type, identifier)
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
virtual void commit()
Commit Transactions.
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
void update(HostPtr const &host)
Implements BaseHostDataSource::update() for PostgreSQL.
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
PgSqlHostDataSource(const db::DatabaseConnection::ParameterMap ¶meters)
Constructor.
virtual void add(const HostPtr &host)
Adds a new host to the collection.
virtual ConstHostPtr get6(const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv6 subnet.
virtual ~PgSqlHostDataSource()
Virtual destructor.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
virtual std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const
Returns backend version.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
RAII class creating a critical section.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
A template representing an optional value.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
boost::shared_ptr< PsqlBindArray > PsqlBindArrayPtr
Defines a smart pointer to PsqlBindArray.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
boost::shared_ptr< PgSqlHostContext > PgSqlHostContextPtr
Type of pointers to contexts.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_GET_VERSION
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
const isc::log::MessageID DHCPSRV_PGSQL_NO_TLS_SUPPORT
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED
const isc::log::MessageID DHCPSRV_PGSQL_TLS_SUPPORT
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
uint64_t HostID
HostID (used only when storing in MySQL or PostgreSQL backends)
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_RECONNECT_FAILED
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
boost::shared_ptr< PgSqlHostContextPool > PgSqlHostContextPoolPtr
Type of pointers to context pools.
const isc::log::MessageID DHCPSRV_PGSQL_HOST_DB_READONLY
boost::shared_ptr< Option > OptionPtr
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
static const int BINARY_FMT
Format value for binary data.