26 #include <boost/algorithm/string/split.hpp>
27 #include <boost/algorithm/string/classification.hpp>
28 #include <boost/array.hpp>
29 #include <boost/pointer_cast.hpp>
30 #include <boost/static_assert.hpp>
56 const uint8_t MAX_IDENTIFIER_TYPE =
static_cast<uint8_t
>(Host::LAST_IDENTIFIER_TYPE);
59 const size_t DHCP_IDENTIFIER_MAX_LEN = ClientId::MAX_CLIENT_ID_LEN;
94 static const size_t HOST_ID_COL = 0;
95 static const size_t DHCP_IDENTIFIER_COL = 1;
96 static const size_t DHCP_IDENTIFIER_TYPE_COL = 2;
97 static const size_t DHCP4_SUBNET_ID_COL = 3;
98 static const size_t DHCP6_SUBNET_ID_COL = 4;
99 static const size_t IPV4_ADDRESS_COL = 5;
100 static const size_t HOSTNAME_COL = 6;
101 static const size_t DHCP4_CLIENT_CLASSES_COL = 7;
102 static const size_t DHCP6_CLIENT_CLASSES_COL = 8;
103 static const size_t USER_CONTEXT_COL = 9;
104 static const size_t DHCP4_NEXT_SERVER_COL = 10;
105 static const size_t DHCP4_SERVER_HOSTNAME_COL = 11;
106 static const size_t DHCP4_BOOT_FILE_NAME_COL = 12;
107 static const size_t AUTH_KEY_COL = 13;
109 static const size_t HOST_COLUMNS = 14;
119 PgSqlHostExchange(
const size_t additional_columns_num = 0)
125 columns_[HOST_ID_COL] =
"host_id";
126 columns_[DHCP_IDENTIFIER_COL] =
"dhcp_identifier";
127 columns_[DHCP_IDENTIFIER_TYPE_COL] =
"dhcp_identifier_type";
128 columns_[DHCP4_SUBNET_ID_COL] =
"dhcp4_subnet_id";
129 columns_[DHCP6_SUBNET_ID_COL] =
"dhcp6_subnet_id";
130 columns_[IPV4_ADDRESS_COL] =
"ipv4_address";
131 columns_[HOSTNAME_COL] =
"hostname";
132 columns_[DHCP4_CLIENT_CLASSES_COL] =
"dhcp4_client_classes";
133 columns_[DHCP6_CLIENT_CLASSES_COL] =
"dhcp6_client_classes";
134 columns_[USER_CONTEXT_COL] =
"user_context";
135 columns_[DHCP4_NEXT_SERVER_COL] =
"dhcp4_next_server";
136 columns_[DHCP4_SERVER_HOSTNAME_COL] =
"dhcp4_server_hostname";
137 columns_[DHCP4_BOOT_FILE_NAME_COL] =
"dhcp4_boot_file_name";
138 columns_[AUTH_KEY_COL] =
"auth_key";
140 BOOST_STATIC_ASSERT(12 < HOST_COLUMNS);
144 virtual ~PgSqlHostExchange() {
152 virtual void clear() {
170 size_t findAvailColumn()
const {
171 std::vector<std::string>::const_iterator empty_column =
172 std::find(columns_.begin(), columns_.end(), std::string());
173 return (std::distance(columns_.begin(), empty_column));
182 getColumnValue(r, row, HOST_ID_COL, host_id);
215 bind_array->add(host->getIdentifier());
218 bind_array->add(host->getIdentifierType());
221 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED) {
222 bind_array->addNull();
224 bind_array->add(host->getIPv4SubnetID());
228 if (host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
229 bind_array->addNull();
231 bind_array->add(host->getIPv6SubnetID());
235 bind_array->add((host->getIPv4Reservation()));
238 bind_array->add(host->getHostname());
242 bind_array->addTempString(host->getClientClasses4().toText(
","));
245 bind_array->addTempString(host->getClientClasses6().toText(
","));
250 std::string user_context_ = ctx->str();
251 bind_array->addTempString(user_context_);
253 bind_array->addNull();
257 bind_array->add((host->getNextServer()));
260 bind_array->add(host->getServerHostname());
263 bind_array->add(host->getBootFileName());
266 std::string key = host->getKey().toText();
268 bind_array->addNull();
270 bind_array->addTempString(key);
277 bind_array->add(host->getIPv4Reservation());
278 bind_array->add(host->getIPv4SubnetID());
281 }
catch (
const std::exception& ex) {
284 "Could not create bind array from Host: "
285 << host->getHostname() <<
", reason: " << ex.
what());
309 HostID row_host_id = getHostId(r, row);
314 if (hosts.empty() || row_host_id != hosts.back()->getHostId()) {
315 HostPtr host = retrieveHost(r, row, row_host_id);
316 hosts.push_back(host);
332 const HostID& peeked_host_id = 0) {
336 HostID host_id = (peeked_host_id ? peeked_host_id : getHostId(r,row));
339 uint8_t identifier_value[DHCP_IDENTIFIER_MAX_LEN];
340 size_t identifier_len;
341 convertFromBytea(r, row, DHCP_IDENTIFIER_COL, identifier_value,
342 sizeof(identifier_value), identifier_len);
346 getColumnValue(r, row, DHCP_IDENTIFIER_TYPE_COL, type);
347 if (type > MAX_IDENTIFIER_TYPE) {
349 <<
static_cast<int>(type));
356 uint32_t subnet_id(SUBNET_ID_UNUSED);
357 if (!isColumnNull(r, row, DHCP4_SUBNET_ID_COL)) {
358 getColumnValue(r, row, DHCP4_SUBNET_ID_COL, subnet_id);
363 subnet_id = SUBNET_ID_UNUSED;
364 if (!isColumnNull(r, row, DHCP6_SUBNET_ID_COL)) {
365 getColumnValue(r, row, DHCP6_SUBNET_ID_COL, subnet_id);
371 if (!isColumnNull(r, row, IPV4_ADDRESS_COL)) {
372 getColumnValue(r, row, IPV4_ADDRESS_COL, addr4);
377 std::string hostname;
378 if (!isColumnNull(r, row, HOSTNAME_COL)) {
379 getColumnValue(r, row, HOSTNAME_COL, hostname);
383 std::string dhcp4_client_classes;
384 if (!isColumnNull(r, row, DHCP4_CLIENT_CLASSES_COL)) {
385 getColumnValue(r, row, DHCP4_CLIENT_CLASSES_COL, dhcp4_client_classes);
389 std::string dhcp6_client_classes;
390 if (!isColumnNull(r, row, DHCP6_CLIENT_CLASSES_COL)) {
391 getColumnValue(r, row, DHCP6_CLIENT_CLASSES_COL, dhcp6_client_classes);
395 std::string user_context;
396 if (!isColumnNull(r, row, USER_CONTEXT_COL)) {
397 getColumnValue(r, row, USER_CONTEXT_COL, user_context);
401 uint32_t dhcp4_next_server_as_uint32(0);
402 if (!isColumnNull(r, row, DHCP4_NEXT_SERVER_COL)) {
403 getColumnValue(r, row, DHCP4_NEXT_SERVER_COL, dhcp4_next_server_as_uint32);
408 std::string dhcp4_server_hostname;
409 if (!isColumnNull(r, row, DHCP4_SERVER_HOSTNAME_COL)) {
410 getColumnValue(r, row, DHCP4_SERVER_HOSTNAME_COL, dhcp4_server_hostname);
414 std::string dhcp4_boot_file_name;
415 if (!isColumnNull(r, row, DHCP4_BOOT_FILE_NAME_COL)) {
416 getColumnValue(r, row, DHCP4_BOOT_FILE_NAME_COL, dhcp4_boot_file_name);
420 std::string auth_key;
421 if (!isColumnNull(r, row, AUTH_KEY_COL)) {
422 getColumnValue(r, row, AUTH_KEY_COL, auth_key);
428 host.reset(
new Host(identifier_value, identifier_len,
429 identifier_type, dhcp4_subnet_id,
430 dhcp6_subnet_id, ipv4_reservation, hostname,
431 dhcp4_client_classes, dhcp6_client_classes,
432 dhcp4_next_server, dhcp4_server_hostname,
433 dhcp4_boot_file_name,
AuthKey(auth_key)));
436 if (!user_context.empty()) {
439 if (!ctx || (ctx->getType() != Element::map)) {
441 <<
"' is not a JSON map");
443 host->setContext(ctx);
446 <<
"' is invalid JSON: " << ex.
what());
450 host->setHostId(host_id);
473 class PgSqlHostWithOptionsExchange :
public PgSqlHostExchange {
477 static const size_t OPTION_COLUMNS = 8;
493 class OptionProcessor {
503 const size_t start_column)
504 : universe_(universe), start_column_(start_column),
505 option_id_index_(start_column), code_index_(start_column_ + 1),
506 value_index_(start_column_ + 2),
507 formatted_value_index_(start_column_ + 3),
508 space_index_(start_column_ + 4),
509 persistent_index_(start_column_ + 5),
510 cancelled_index_(start_column_ + 6),
511 user_context_index_(start_column_ + 7),
512 most_recent_option_id_(0) {
520 most_recent_option_id_ = 0;
559 if (PgSqlExchange::isColumnNull(r, row, option_id_index_)) {
565 PgSqlExchange::getColumnValue(r, row, option_id_index_, option_id);
570 if (most_recent_option_id_ >= option_id) {
576 most_recent_option_id_ = option_id;
580 PgSqlExchange::getColumnValue(r, row, code_index_, code);
585 if (!isColumnNull(r, row, value_index_)) {
586 PgSqlExchange::convertFromBytea(r, row, value_index_, value,
587 sizeof(value), value_len);
591 std::string formatted_value;
592 if (!isColumnNull(r, row, formatted_value_index_)) {
593 PgSqlExchange::getColumnValue(r, row, formatted_value_index_,
599 if (!isColumnNull(r, row, space_index_)) {
600 PgSqlExchange::getColumnValue(r, row, space_index_, space);
605 space = (universe_ == Option::V4 ?
611 PgSqlExchange::getColumnValue(r, row, persistent_index_,
616 PgSqlExchange::getColumnValue(r, row, cancelled_index_,
620 std::string user_context;
621 if (!isColumnNull(r, row, user_context_index_)) {
622 PgSqlExchange::getColumnValue(r, row, user_context_index_,
641 uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(space);
643 def = LibDHCP::getVendorOptionDef(universe_, vendor_id,
651 def = LibDHCP::getRuntimeOptionDef(space, code);
656 def = LibDHCP::getLastResortOptionDef(space, code);
664 option.reset(
new Option(universe_, code, buf.begin(),
671 if (formatted_value.empty()) {
673 option = def->optionFactory(universe_, code, buf.begin(),
678 std::vector<std::string> split_vec;
679 boost::split(split_vec, formatted_value,
680 boost::is_any_of(
","));
681 option = def->optionFactory(universe_, code, split_vec);
689 if (!user_context.empty()) {
692 if (!ctx || (ctx->getType() != Element::map)) {
694 <<
"' is no a JSON map");
696 desc.setContext(ctx);
699 <<
"' is invalid JSON: " << ex.
what());
703 cfg->add(desc, space);
710 void setColumnNames(std::vector<std::string>& columns) {
711 columns[option_id_index_] =
"option_id";
712 columns[code_index_] =
"code";
713 columns[value_index_] =
"value";
714 columns[formatted_value_index_] =
"formatted_value";
715 columns[space_index_] =
"space";
716 columns[persistent_index_] =
"persistent";
717 columns[cancelled_index_] =
"cancelled";
718 columns[user_context_index_] =
"user_context";
726 size_t start_column_;
732 size_t option_id_index_;
742 size_t formatted_value_index_;
748 size_t persistent_index_;
751 size_t cancelled_index_;
755 size_t user_context_index_;
758 uint64_t most_recent_option_id_;
762 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
772 enum FetchedOptions {
786 PgSqlHostWithOptionsExchange(
const FetchedOptions& fetched_options,
787 const size_t additional_columns_num = 0)
788 : PgSqlHostExchange(getRequiredColumnsNum(fetched_options)
789 + additional_columns_num),
790 opt_proc4_(), opt_proc6_() {
793 if ((fetched_options == DHCP4_ONLY) ||
794 (fetched_options == DHCP4_AND_DHCP6)) {
795 opt_proc4_.reset(
new OptionProcessor(Option::V4,
797 opt_proc4_->setColumnNames(columns_);
801 if ((fetched_options == DHCP6_ONLY) ||
802 (fetched_options == DHCP4_AND_DHCP6)) {
803 opt_proc6_.reset(
new OptionProcessor(Option::V6,
805 opt_proc6_->setColumnNames(columns_);
814 virtual void clear() {
815 PgSqlHostExchange::clear();
839 current_host = retrieveHost(r, row);
840 hosts.push_back(current_host);
845 HostID row_host_id = getHostId(r, row);
846 current_host = boost::const_pointer_cast<Host>(hosts.back());
850 if (row_host_id > current_host->getHostId()) {
851 current_host = retrieveHost(r, row, row_host_id);
852 hosts.push_back(current_host);
859 opt_proc4_->retrieveOption(cfg, r, row);
865 opt_proc6_->retrieveOption(cfg, r, row);
882 static size_t getRequiredColumnsNum(
const FetchedOptions& fetched_options) {
883 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
890 OptionProcessorPtr opt_proc4_;
895 OptionProcessorPtr opt_proc6_;
910 class PgSqlHostIPv6Exchange :
public PgSqlHostWithOptionsExchange {
914 static const size_t RESERVATION_COLUMNS = 5;
922 PgSqlHostIPv6Exchange(
const FetchedOptions& fetched_options)
923 : PgSqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
924 reservation_id_index_(findAvailColumn()),
925 address_index_(reservation_id_index_ + 1),
926 prefix_len_index_(reservation_id_index_ + 2),
927 type_index_(reservation_id_index_ + 3),
928 iaid_index_(reservation_id_index_ + 4),
929 most_recent_reservation_id_(0) {
932 columns_[reservation_id_index_] =
"reservation_id";
933 columns_[address_index_] =
"address";
934 columns_[prefix_len_index_] =
"prefix_len";
935 columns_[type_index_] =
"type";
936 columns_[iaid_index_] =
"dhcp6_iaid";
938 BOOST_STATIC_ASSERT(4 < RESERVATION_COLUMNS);
947 PgSqlHostWithOptionsExchange::clear();
948 most_recent_reservation_id_ = 0;
954 uint64_t getReservationId(
const PgSqlResult& r,
int row)
const {
955 uint64_t resv_id = 0;
956 if (!isColumnNull(r, row, reservation_id_index_)) {
957 getColumnValue(r, row, reservation_id_index_, resv_id);
971 getColumnValue(r, row, type_index_, tmp);
977 resv_type = IPv6Resrv::TYPE_NA;
981 resv_type = IPv6Resrv::TYPE_PD;
986 "invalid IPv6 reservation type returned: "
987 << tmp <<
". Only 0 or 2 are allowed.");
995 getColumnValue(r, row, prefix_len_index_, prefix_len);
1004 return (reservation);
1031 PgSqlHostWithOptionsExchange::processRowData(hosts, r, row);
1034 if (hosts.empty()) {
1036 " IPv6 reservation");
1041 uint64_t reservation_id = getReservationId(r, row);
1042 if (reservation_id && (reservation_id > most_recent_reservation_id_)) {
1043 HostPtr host = boost::const_pointer_cast<Host>(hosts.back());
1044 host->addReservation(retrieveReservation(r, row));
1045 most_recent_reservation_id_ = reservation_id;
1052 size_t reservation_id_index_;
1056 size_t address_index_;
1059 size_t prefix_len_index_;
1070 uint64_t most_recent_reservation_id_;
1087 static const size_t RESRV_COLUMNS = 6;
1094 PgSqlIPv6ReservationExchange()
1098 columns_[0] =
"host_id";
1099 columns_[1] =
"address";
1100 columns_[2] =
"prefix_len";
1101 columns_[3] =
"type";
1102 columns_[4] =
"dhcp6_iaid";
1104 BOOST_STATIC_ASSERT(5 < RESRV_COLUMNS);
1123 const bool unique_ip) {
1140 uint16_t type = resv.
getType() == IPv6Resrv::TYPE_NA ? 0 : 2;
1141 bind_array->add(type);
1145 bind_array->addNull();
1148 bind_array->add(host_id);
1157 }
catch (
const std::exception& ex) {
1159 "Could not create bind array from IPv6 Reservation: "
1160 << resv_.toText() <<
", reason: " << ex.
what());
1163 return (bind_array);
1177 static const size_t OPTION_ID_COL = 0;
1178 static const size_t CODE_COL = 1;
1179 static const size_t VALUE_COL = 2;
1180 static const size_t FORMATTED_VALUE_COL = 3;
1181 static const size_t SPACE_COL = 4;
1182 static const size_t PERSISTENT_COL = 5;
1183 static const size_t CANCELLED_COL = 6;
1184 static const size_t USER_CONTEXT_COL = 7;
1185 static const size_t DHCP_SUBNET_ID_COL = 8;
1186 static const size_t HOST_ID_COL = 9;
1188 static const size_t OPTION_COLUMNS = 10;
1193 PgSqlOptionExchange()
1195 value_len_(0), option_() {
1196 columns_[OPTION_ID_COL] =
"option_id";
1197 columns_[CODE_COL] =
"code";
1198 columns_[VALUE_COL] =
"value";
1199 columns_[FORMATTED_VALUE_COL] =
"formatted_value";
1200 columns_[SPACE_COL] =
"space";
1201 columns_[PERSISTENT_COL] =
"persistent";
1202 columns_[CANCELLED_COL] =
"cancelled";
1203 columns_[USER_CONTEXT_COL] =
"user_context";
1204 columns_[DHCP_SUBNET_ID_COL] =
"dhcp_subnet_id";
1205 columns_[HOST_ID_COL] =
"host_id";
1207 BOOST_STATIC_ASSERT(10 <= OPTION_COLUMNS);
1219 const std::string& opt_space,
1232 bind_array->add(option_->getType());
1242 const char* buf_ptr =
static_cast<const char*
>(buf.getData());
1243 value_.assign(buf_ptr + opt_desc.
option_->getHeaderLen(),
1244 buf_ptr + buf.getLength());
1245 value_len_ = value_.size();
1246 bind_array->add(value_);
1250 bind_array->addNull(PsqlBindArray::BINARY_FMT);
1257 bind_array->addNull();
1261 if (!opt_space.empty()) {
1262 bind_array->addTempString(opt_space);
1264 bind_array->addNull();
1276 std::string user_context_ = ctx->str();
1277 bind_array->addTempString(user_context_);
1279 bind_array->addNull();
1286 bind_array->add(host_id);
1288 }
catch (
const std::exception& ex) {
1290 "Could not create bind array for inserting DHCP "
1291 "host option: " << option_->toText() <<
", reason: "
1295 return (bind_array);
1301 std::vector<uint8_t> value_;
1502 const bool return_last_id =
false);
1536 const std::string& opt_space,
1551 const uint64_t host_id);
1574 boost::shared_ptr<PgSqlHostExchange> exchange,
1598 const uint8_t* identifier_begin,
1599 const size_t identifier_len,
1601 boost::shared_ptr<PgSqlHostExchange> exchange)
const;
1622 std::pair<uint32_t, uint32_t> getVersion()
const;
1645 typedef boost::array<PgSqlTaggedStatement, PgSqlHostDataSourceImpl::NUM_STATEMENTS>
1646 TaggedStatementArray;
1650 TaggedStatementArray tagged_statements = { {
1659 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1660 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1661 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1663 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1664 " h.dhcp4_boot_file_name, h.auth_key, "
1665 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1666 " o4.persistent, o4.cancelled, o4.user_context, "
1667 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1668 " o6.persistent, o6.cancelled, o6.user_context, "
1669 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1671 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1672 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1673 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1674 "WHERE dhcp_identifier = $1 AND dhcp_identifier_type = $2 "
1675 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1685 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1686 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1687 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1688 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1689 " h.dhcp4_boot_file_name, h.auth_key, "
1690 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1691 " o.persistent, o.cancelled, o.user_context "
1693 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1694 "WHERE ipv4_address = $1 "
1695 "ORDER BY h.host_id, o.option_id"
1704 "get_host_subid4_dhcpid",
1705 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1706 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1707 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1708 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1709 " h.dhcp4_boot_file_name, h.auth_key, "
1710 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1711 " o.persistent, o.cancelled, o.user_context "
1713 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1714 "WHERE h.dhcp4_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1715 " AND h.dhcp_identifier = $3 "
1716 "ORDER BY h.host_id, o.option_id"
1725 "get_host_subid6_dhcpid",
1726 "SELECT h.host_id, h.dhcp_identifier, "
1727 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1728 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1729 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1730 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1731 " h.dhcp4_boot_file_name, h.auth_key, "
1732 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1733 " o.persistent, o.cancelled, o.user_context, "
1734 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1736 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1737 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1738 "WHERE h.dhcp6_subnet_id = $1 AND h.dhcp_identifier_type = $2 "
1739 " AND h.dhcp_identifier = $3 "
1740 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1750 "get_host_subid_addr",
1751 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1752 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1753 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1754 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1755 " h.dhcp4_boot_file_name, h.auth_key, "
1756 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1757 " o.persistent, o.cancelled, o.user_context "
1759 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1760 "WHERE h.dhcp4_subnet_id = $1 AND h.ipv4_address = $2 "
1761 "ORDER BY h.host_id, o.option_id"
1774 "SELECT h.host_id, h.dhcp_identifier, "
1775 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1776 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1777 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1778 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1779 " h.dhcp4_boot_file_name, h.auth_key, "
1780 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1781 " o.persistent, o.cancelled, o.user_context, "
1782 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1785 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1786 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1787 "WHERE h.host_id = "
1788 " (SELECT host_id FROM ipv6_reservations "
1789 " WHERE address = cast($1 as inet) AND prefix_len = $2) "
1790 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1802 "get_host_subid6_addr",
1803 "SELECT h.host_id, h.dhcp_identifier, "
1804 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1805 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1806 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1807 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1808 " h.dhcp4_boot_file_name, h.auth_key, "
1809 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1810 " o.persistent, o.cancelled, o.user_context, "
1811 " r.reservation_id, host(r.address), r.prefix_len, r.type, "
1814 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1815 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1816 "WHERE h.dhcp6_subnet_id = $1 AND h.host_id IN "
1817 " (SELECT host_id FROM ipv6_reservations "
1818 " WHERE address = cast($2 as inet)) "
1819 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1833 "SELECT h.host_id, h.dhcp_identifier, "
1834 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1835 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1836 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1837 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1838 " h.dhcp4_boot_file_name, h.auth_key, "
1839 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1840 " o.persistent, o.cancelled, o.user_context, "
1841 " r.reservation_id, r.address, r.prefix_len, r.type, "
1844 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1845 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1846 "WHERE h.host_id IN "
1847 " (SELECT host_id FROM ipv6_reservations "
1848 " WHERE address = cast($1 as inet)) "
1849 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1861 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1862 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1863 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1864 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1865 " h.dhcp4_boot_file_name, h.auth_key, "
1866 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1867 " o.persistent, o.cancelled, o.user_context "
1869 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1870 "WHERE h.dhcp4_subnet_id = $1 "
1871 "ORDER BY h.host_id, o.option_id"
1887 "SELECT h.host_id, h.dhcp_identifier, "
1888 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1889 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1890 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1891 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1892 " h.dhcp4_boot_file_name, h.auth_key, "
1893 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1894 " o.persistent, o.cancelled, o.user_context, "
1895 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1897 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1898 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1899 "WHERE h.dhcp6_subnet_id = $1 "
1900 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1911 "get_host_hostname",
1912 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1913 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
1914 " h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
1916 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1917 " h.dhcp4_boot_file_name, h.auth_key, "
1918 " o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
1919 " o4.persistent, o4.cancelled, o4.user_context, "
1920 " o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
1921 " o6.persistent, o6.cancelled, o6.user_context, "
1922 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1924 "LEFT JOIN dhcp4_options AS o4 ON h.host_id = o4.host_id "
1925 "LEFT JOIN dhcp6_options AS o6 ON h.host_id = o6.host_id "
1926 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1927 "WHERE lower(h.hostname) = $1 "
1928 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"
1938 "get_host_hostname_subid4",
1939 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1940 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1941 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1942 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1943 " h.dhcp4_boot_file_name, h.auth_key, "
1944 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1945 " o.persistent, o.cancelled, o.user_context "
1947 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1948 "WHERE lower(h.hostname) = $1 AND h.dhcp4_subnet_id = $2 "
1949 "ORDER BY h.host_id, o.option_id"
1961 "get_host_hostname_subid6",
1962 "SELECT h.host_id, h.dhcp_identifier, "
1963 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
1964 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1965 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1966 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1967 " h.dhcp4_boot_file_name, h.auth_key, "
1968 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1969 " o.persistent, o.cancelled, o.user_context, "
1970 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
1972 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
1973 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
1974 "WHERE lower(h.hostname) = $1 AND h.dhcp6_subnet_id = $2 "
1975 "ORDER BY h.host_id, o.option_id, r.reservation_id"
1985 "get_host_subid4_page",
1986 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
1987 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
1988 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
1989 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
1990 " h.dhcp4_boot_file_name, h.auth_key, "
1991 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
1992 " o.persistent, o.cancelled, o.user_context "
1993 "FROM ( SELECT * FROM hosts AS h "
1994 " WHERE h.dhcp4_subnet_id = $1 AND h.host_id > $2 "
1995 " ORDER BY h.host_id "
1997 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
1998 "ORDER BY h.host_id, o.option_id"
2010 "get_host_subid6_page",
2011 "SELECT h.host_id, h.dhcp_identifier, "
2012 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2013 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2014 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2015 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2016 " h.dhcp4_boot_file_name, h.auth_key, "
2017 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2018 " o.persistent, o.cancelled, o.user_context, "
2019 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2020 "FROM ( SELECT * FROM hosts AS h "
2021 " WHERE h.dhcp6_subnet_id = $1 AND h.host_id > $2 "
2022 " ORDER BY h.host_id "
2024 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2025 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2026 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2037 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
2038 " h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2039 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2040 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2041 " h.dhcp4_boot_file_name, h.auth_key, "
2042 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2043 " o.persistent, o.cancelled, o.user_context "
2044 "FROM ( SELECT * FROM hosts AS h "
2045 " WHERE h.host_id > $1 "
2046 " ORDER BY h.host_id "
2048 "LEFT JOIN dhcp4_options AS o ON h.host_id = o.host_id "
2049 "ORDER BY h.host_id, o.option_id"
2062 "SELECT h.host_id, h.dhcp_identifier, "
2063 " h.dhcp_identifier_type, h.dhcp4_subnet_id, "
2064 " h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
2065 " h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, "
2066 " h.dhcp4_next_server, h.dhcp4_server_hostname, "
2067 " h.dhcp4_boot_file_name, h.auth_key, "
2068 " o.option_id, o.code, o.value, o.formatted_value, o.space, "
2069 " o.persistent, o.cancelled, o.user_context, "
2070 " r.reservation_id, host(r.address), r.prefix_len, r.type, r.dhcp6_iaid "
2071 "FROM ( SELECT * FROM hosts AS h "
2072 " WHERE h.host_id > $1 "
2073 " ORDER BY h.host_id "
2075 "LEFT JOIN dhcp6_options AS o ON h.host_id = o.host_id "
2076 "LEFT JOIN ipv6_reservations AS r ON h.host_id = r.host_id "
2077 "ORDER BY h.host_id, o.option_id, r.reservation_id"
2088 "insert_host_non_unique_ip",
2089 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2090 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2091 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2092 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2093 "VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 ) "
2114 "insert_host_unique_ip",
2115 "INSERT INTO hosts(dhcp_identifier, dhcp_identifier_type, "
2116 " dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
2117 " dhcp4_client_classes, dhcp6_client_classes, user_context, "
2118 " dhcp4_next_server, dhcp4_server_hostname, dhcp4_boot_file_name, auth_key)"
2119 " SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13"
2120 " WHERE NOT EXISTS ("
2121 " SELECT 1 FROM hosts WHERE ipv4_address = $14 AND dhcp4_subnet_id = $15"
2132 "insert_v6_resrv_non_unique",
2133 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2134 " dhcp6_iaid, host_id) "
2135 "VALUES (cast($1 as inet), $2, $3, $4, $5)"
2143 "insert_v6_resrv_unique",
2144 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2145 " dhcp6_iaid, host_id) "
2146 "SELECT cast($1 as inet), $2, $3, $4, $5 "
2147 " WHERE NOT EXISTS ("
2148 " SELECT 1 FROM ipv6_reservations"
2149 " WHERE address = cast($6 as inet) AND prefix_len = $7"
2160 "insert_v4_host_option",
2161 "INSERT INTO dhcp4_options(code, value, formatted_value, space, "
2162 " persistent, cancelled, user_context, host_id, scope_id) "
2163 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2172 "insert_v6_host_option",
2173 "INSERT INTO dhcp6_options(code, value, formatted_value, space, "
2174 " persistent, cancelled, user_context, host_id, scope_id) "
2175 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 3)"
2183 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 AND ipv4_address = $2"
2191 "DELETE FROM hosts USING ipv6_reservations "
2192 " WHERE dhcp6_subnet_id = $1 AND ipv6_reservations.address = cast($2 as inet)"
2199 "del_host_subid4_id",
2200 "DELETE FROM hosts WHERE dhcp4_subnet_id = $1 "
2201 "AND dhcp_identifier_type = $2 "
2202 "AND dhcp_identifier = $3"
2209 "del_host_subid6_id",
2210 "DELETE FROM hosts WHERE dhcp6_subnet_id = $1 "
2211 "AND dhcp_identifier_type = $2 "
2212 "AND dhcp_identifier = $3"
2224 : conn_(parameters, io_service_accessor, db_reconnect_callback),
2225 is_readonly_(true) {
2233 if (MultiThreadingMgr::instance().getMode()) {
2237 lock_guard<mutex> lock(mgr_.
pool_->mutex_);
2238 if (!mgr_.
pool_->pool_.empty()) {
2240 mgr_.
pool_->pool_.pop_back();
2248 if (mgr_.
pool_->pool_.empty()) {
2256 if (MultiThreadingMgr::instance().getMode()) {
2258 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2259 mgr_.pool_->pool_.push_back(ctx_);
2260 if (ctx_->conn_.isUnusable()) {
2261 mgr_.unusable_ =
true;
2263 }
else if (ctx_->conn_.isUnusable()) {
2264 mgr_.unusable_ =
true;
2269 : parameters_(parameters), ip_reservations_unique_(true), unusable_(false),
2274 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2279 tls += parameters.count(
"trust-anchor");
2280 tls += parameters.count(
"cert-file");
2281 tls += parameters.count(
"key-file");
2282 tls += parameters.count(
"cipher-list");
2283 #ifdef HAVE_PGSQL_SSL
2295 <<
"backend (built with this feature disabled)");
2302 std::pair<uint32_t, uint32_t> db_version =
getVersion();
2303 if (code_version != db_version) {
2305 "PostgreSQL schema version mismatch: need version: "
2306 << code_version.first <<
"." << code_version.second
2307 <<
" found version: " << db_version.first <<
"."
2308 << db_version.second);
2325 ctx->conn_.openDatabase();
2328 ctx->conn_.prepareStatements(tagged_statements.begin(),
2333 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2337 if (!ctx->is_readonly_) {
2339 tagged_statements.end());
2344 ctx->host_ipv4_exchange_.reset(
new PgSqlHostWithOptionsExchange(PgSqlHostWithOptionsExchange::DHCP4_ONLY));
2345 ctx->host_ipv6_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP6_ONLY));
2346 ctx->host_ipv46_exchange_.reset(
new PgSqlHostIPv6Exchange(PgSqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2347 ctx->host_ipv6_reservation_exchange_.reset(
new PgSqlIPv6ReservationExchange());
2348 ctx->host_option_exchange_.reset(
new PgSqlOptionExchange());
2368 bool reopened =
false;
2370 const std::string timer_name = db_reconnect_ctl->timerName();
2375 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2376 for (std::string& hds : host_db_access_list) {
2383 }
catch (
const std::exception& ex) {
2399 if (!db_reconnect_ctl->checkRetries()) {
2402 .arg(db_reconnect_ctl->maxRetries());
2415 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2416 .arg(db_reconnect_ctl->maxRetries())
2417 .arg(db_reconnect_ctl->retryInterval());
2423 db_reconnect_ctl->retryInterval(),
2436 const bool return_last_id) {
2437 uint64_t last_id = 0;
2438 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2439 tagged_statements[stindex].nbparams,
2440 &bind_array->values_[0],
2441 &bind_array->lengths_[0],
2442 &bind_array->formats_[0], 0));
2444 int s = PQresultStatus(r);
2446 if (s != PGRES_COMMAND_OK) {
2454 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2458 char* rows_affected = PQcmdTuples(r);
2459 if (!rows_affected) {
2461 "Could not retrieve the number of affected rows.");
2469 if (rows_affected[0] ==
'0') {
2473 if (return_last_id) {
2484 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2485 tagged_statements[stindex].nbparams,
2486 &bind_array->values_[0],
2487 &bind_array->lengths_[0],
2488 &bind_array->formats_[0], 0));
2490 int s = PQresultStatus(r);
2492 if (s != PGRES_COMMAND_OK) {
2495 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2500 char* rows_deleted = PQcmdTuples(r);
2501 if (!rows_deleted) {
2503 "Could not retrieve the number of deleted rows.");
2505 return (rows_deleted[0] !=
'0');
2524 const std::string& opt_space,
2527 PsqlBindArrayPtr bind_array = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space,
id);
2536 const uint64_t host_id) {
2539 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
2540 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
2541 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
2542 vendor_spaces.end());
2546 for (
auto space = option_spaces.begin(); space != option_spaces.end(); ++space) {
2548 if (options && !options->empty()) {
2549 for (
auto opt = options->begin(); opt != options->end(); ++opt) {
2560 boost::shared_ptr<PgSqlHostExchange> exchange,
2562 bool single)
const {
2565 PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
2566 tagged_statements[stindex].nbparams,
2567 &bind_array->values_[0],
2568 &bind_array->lengths_[0],
2569 &bind_array->formats_[0], 0));
2571 ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
2574 for (
int row = 0; row < rows; ++row) {
2575 exchange->processRowData(result, r, row);
2577 if (single && result.size() > 1) {
2579 "database where only one was expected for query "
2580 << tagged_statements[stindex].name);
2589 const uint8_t* identifier_begin,
2590 const size_t identifier_len,
2592 boost::shared_ptr<PgSqlHostExchange> exchange)
const {
2598 bind_array->add(subnet_id);
2601 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2604 bind_array->add(identifier_begin, identifier_len);
2611 if (!collection.empty()) {
2612 result = *collection.begin();
2618 std::pair<uint32_t, uint32_t>
2627 if (ctx->is_readonly_) {
2629 " to operate in read only mode");
2644 return (impl_->parameters_);
2654 impl_->checkReadOnly(ctx);
2666 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
2667 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
2670 PsqlBindArrayPtr bind_array = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
2673 uint32_t host_id = impl_->addStatement(ctx,
2682 cfg_option4, host_id);
2689 cfg_option6, host_id);
2694 if (std::distance(v6resv.first, v6resv.second) > 0) {
2697 impl_->addResv(ctx, resv->second, host_id);
2713 impl_->checkReadOnly(ctx);
2716 bind_array->add(subnet_id);
2720 bind_array->add(addr);
2726 bind_array->addTempString(addr.
toText());
2735 const uint8_t* identifier_begin,
2736 const size_t identifier_len) {
2742 impl_->checkReadOnly(ctx);
2747 bind_array->add(subnet_id);
2750 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2753 bind_array->add(identifier_begin, identifier_len);
2762 const uint8_t* identifier_begin,
2763 const size_t identifier_len) {
2769 impl_->checkReadOnly(ctx);
2774 bind_array->add(subnet_id);
2777 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2780 bind_array->add(identifier_begin, identifier_len);
2788 const uint8_t* identifier_begin,
2789 const size_t identifier_len)
const {
2798 bind_array->add(identifier_begin, identifier_len);
2801 bind_array->add(
static_cast<uint8_t
>(identifier_type));
2805 bind_array, ctx->host_ipv46_exchange_, result,
false);
2820 bind_array->add(subnet_id);
2824 bind_array, ctx->host_ipv4_exchange_, result,
false);
2839 bind_array->add(subnet_id);
2843 bind_array, ctx->host_ipv6_exchange_, result,
false);
2858 bind_array->add(hostname);
2862 bind_array, ctx->host_ipv46_exchange_, result,
false);
2878 bind_array->add(hostname);
2881 bind_array->add(subnet_id);
2885 bind_array, ctx->host_ipv4_exchange_, result,
false);
2901 bind_array->add(hostname);
2904 bind_array->add(subnet_id);
2908 bind_array, ctx->host_ipv6_exchange_, result,
false);
2916 uint64_t lower_host_id,
2926 bind_array->add(subnet_id);
2929 bind_array->add(lower_host_id);
2932 string page_size_data =
2933 boost::lexical_cast<std::string>(page_size.
page_size_);
2934 bind_array->add(page_size_data);
2938 bind_array, ctx->host_ipv4_exchange_, result,
false);
2946 uint64_t lower_host_id,
2956 bind_array->add(subnet_id);
2959 bind_array->add(lower_host_id);
2962 string page_size_data =
2963 boost::lexical_cast<std::string>(page_size.
page_size_);
2964 bind_array->add(page_size_data);
2968 bind_array, ctx->host_ipv6_exchange_, result,
false);
2975 uint64_t lower_host_id,
2985 bind_array->add(lower_host_id);
2988 string page_size_data =
2989 boost::lexical_cast<std::string>(page_size.
page_size_);
2990 bind_array->add(page_size_data);
2994 bind_array, ctx->host_ipv4_exchange_, result,
false);
3001 uint64_t lower_host_id,
3011 bind_array->add(lower_host_id);
3014 string page_size_data =
3015 boost::lexical_cast<std::string>(page_size.
page_size_);
3016 bind_array->add(page_size_data);
3020 bind_array, ctx->host_ipv6_exchange_, result,
false);
3035 bind_array->add(address);
3039 bind_array, ctx->host_ipv4_exchange_, result,
false);
3047 const uint8_t* identifier_begin,
3048 const size_t identifier_len)
const {
3053 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3055 ctx->host_ipv4_exchange_));
3065 if (!address.
isV4()) {
3067 " wrong address type, address supplied is an IPv6 address");
3074 bind_array->add(subnet_id);
3077 bind_array->add(address);
3081 bind_array, ctx->host_ipv4_exchange_, collection,
true);
3085 if (!collection.empty()) {
3086 result = *collection.begin();
3099 if (!address.
isV4()) {
3101 " wrong address type, address supplied is an IPv6 address");
3108 bind_array->add(subnet_id);
3111 bind_array->add(address);
3115 bind_array, ctx->host_ipv4_exchange_, collection,
false);
3116 return (collection);
3122 const uint8_t* identifier_begin,
3123 const size_t identifier_len)
const {
3128 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3130 ctx->host_ipv6_exchange_));
3135 const uint8_t prefix_len)
const {
3136 if (!prefix.
isV6()) {
3138 "wrong address type, address supplied is an IPv4 address");
3149 bind_array->add(prefix);
3152 bind_array->add(prefix_len);
3156 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3160 if (!collection.empty()) {
3161 result = *collection.begin();
3170 if (!address.
isV6()) {
3172 "wrong address type, address supplied is an IPv4 address");
3183 bind_array->add(subnet_id);
3186 bind_array->add(address);
3190 bind_array, ctx->host_ipv6_exchange_, collection,
true);
3194 if (!collection.empty()) {
3195 result = *collection.begin();
3204 if (!address.
isV6()) {
3206 "wrong address type, address supplied is an IPv4 address");
3217 bind_array->add(subnet_id);
3220 bind_array->add(address);
3224 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3225 return (collection);
3230 if (!address.
isV6()) {
3232 "wrong address type, address supplied is an IPv4 address");
3243 bind_array->add(address);
3247 bind_array, ctx->host_ipv6_exchange_, collection,
false);
3248 return (collection);
3258 impl_->checkReadOnly(ctx);
3281 std::string name =
"";
3287 name = ctx->conn_.getParameter(
"name");
3296 return (std::string(
"Host data source that stores host information"
3297 "in PostgreSQL database"));
3300 std::pair<uint32_t, uint32_t>
3302 return(impl_->getVersion());
3312 impl_->checkReadOnly(ctx);
3313 ctx->conn_.commit();
3323 impl_->checkReadOnly(ctx);
3324 ctx->conn_.rollback();
3329 impl_->ip_reservations_unique_ = unique;
3335 return (impl_->unusable_);
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This is a base class for exceptions thrown from the DNS library module.
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.
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
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.
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 std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
Base class for marshalling data to and from PostgreSQL.
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 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).
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
Wraps value holding size of the page with host reservations.
const size_t page_size_
Holds page size.
Represents a device with IPv4 and/or IPv6 reservations.
IdentifierType
Type of the host identifier.
IPv6 reservation for a host.
Type getType() const
Returns reservation type.
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Type
Type of the reservation.
uint8_t getPrefixLen() const
Returns prefix length.
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.
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.
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.
std::pair< uint32_t, uint32_t > getVersion() const
Returns PostgreSQL schema version of the open database.
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 std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
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.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
RAII class creating a critical section.
A template representing an optional value.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
#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.
A wrapper interface for the ASIO library.
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.
const uint32_t PGSQL_SCHEMA_VERSION_MINOR
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.
const uint32_t PGSQL_SCHEMA_VERSION_MAJOR
Define the PostgreSQL backend version.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
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.