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> 33 #include <mysqld_error.h> 53 const uint8_t MAX_IDENTIFIER_TYPE =
static_cast<uint8_t
>(Host::LAST_IDENTIFIER_TYPE);
83 class MySqlHostExchange {
87 static const size_t HOST_COLUMNS = 14;
97 MySqlHostExchange(
const size_t additional_columns_num = 0)
98 : columns_num_(HOST_COLUMNS + additional_columns_num),
99 bind_(columns_num_), columns_(columns_num_),
100 error_(columns_num_,
MLM_FALSE), host_id_(0),
101 dhcp_identifier_length_(0), dhcp_identifier_type_(0),
102 dhcp4_subnet_id_(SUBNET_ID_UNUSED),
103 dhcp6_subnet_id_(SUBNET_ID_UNUSED), ipv4_address_(0),
104 hostname_length_(0), dhcp4_client_classes_length_(0),
105 dhcp6_client_classes_length_(0),
106 user_context_length_(0),
107 dhcp4_next_server_(0),
108 dhcp4_server_hostname_length_(0),
109 dhcp4_boot_file_name_length_(0),
123 memset(dhcp_identifier_buffer_, 0,
sizeof(dhcp_identifier_buffer_));
124 memset(hostname_, 0,
sizeof(hostname_));
125 memset(dhcp4_client_classes_, 0,
sizeof(dhcp4_client_classes_));
126 memset(dhcp6_client_classes_, 0,
sizeof(dhcp6_client_classes_));
127 memset(user_context_, 0,
sizeof(user_context_));
128 memset(dhcp4_server_hostname_, 0,
sizeof(dhcp4_server_hostname_));
129 memset(dhcp4_boot_file_name_, 0,
sizeof(dhcp4_boot_file_name_));
130 memset(auth_key_, 0,
sizeof(auth_key_));
135 columns_[0] =
"host_id";
136 columns_[1] =
"dhcp_identifier";
137 columns_[2] =
"dhcp_identifier_type";
138 columns_[3] =
"dhcp4_subnet_id";
139 columns_[4] =
"dhcp6_subnet_id";
140 columns_[5] =
"ipv4_address";
141 columns_[6] =
"hostname";
142 columns_[7] =
"dhcp4_client_classes";
143 columns_[8] =
"dhcp6_client_classes";
144 columns_[9] =
"user_context";
145 columns_[10] =
"dhcp4_next_server";
146 columns_[11] =
"dhcp4_server_hostname";
147 columns_[12] =
"dhcp4_boot_file_name";
148 columns_[13] =
"auth_key";
150 BOOST_STATIC_ASSERT(13 < HOST_COLUMNS);
154 virtual ~MySqlHostExchange() {
171 size_t findAvailColumn()
const {
172 std::vector<std::string>::const_iterator empty_column =
173 std::find(columns_.begin(), columns_.end(), std::string());
174 return (std::distance(columns_.begin(), empty_column));
180 uint64_t getHostId()
const {
194 static void setErrorIndicators(std::vector<MYSQL_BIND>& bind,
195 std::vector<my_bools>&
error) {
196 for (
size_t i = 0; i < error.size(); ++i) {
198 bind[i].error =
reinterpret_cast<my_bool*
>(&error[i]);
215 static std::string getColumnsInError(std::vector<my_bools>& error,
216 const std::vector<std::string>& names) {
217 std::string result =
"";
220 for (
size_t i = 0; i < names.size(); ++i) {
222 if (!result.empty()) {
229 if (result.empty()) {
248 std::vector<MYSQL_BIND> createBindForSend(
const HostPtr& host,
const bool unique_ip) {
256 memset(&bind_[0], 0,
sizeof(MYSQL_BIND) * bind_.size());
265 bind_[0].buffer_type = MYSQL_TYPE_LONG;
266 bind_[0].buffer =
reinterpret_cast<char*
>(&host_id_);
270 dhcp_identifier_length_ = host->getIdentifier().size();
271 memcpy(static_cast<void*>(dhcp_identifier_buffer_),
272 &(host->getIdentifier())[0],
273 host->getIdentifier().size());
275 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
276 bind_[1].buffer = dhcp_identifier_buffer_;
277 bind_[1].buffer_length = dhcp_identifier_length_;
278 bind_[1].length = &dhcp_identifier_length_;
281 dhcp_identifier_type_ =
static_cast<uint8_t
>(host->getIdentifierType());
282 bind_[2].buffer_type = MYSQL_TYPE_TINY;
283 bind_[2].buffer =
reinterpret_cast<char*
>(&dhcp_identifier_type_);
289 dhcp4_subnet_id_ = host->getIPv4SubnetID();
290 dhcp4_subnet_id_null_ = host->getIPv4SubnetID() == SUBNET_ID_UNUSED ?
MLM_TRUE :
MLM_FALSE;
291 bind_[3].buffer_type = MYSQL_TYPE_LONG;
292 bind_[3].buffer =
reinterpret_cast<char*
>(&dhcp4_subnet_id_);
294 bind_[3].is_null = &dhcp4_subnet_id_null_;
299 dhcp6_subnet_id_ = host->getIPv6SubnetID();
300 dhcp6_subnet_id_null_ = host->getIPv6SubnetID() == SUBNET_ID_UNUSED ?
MLM_TRUE :
MLM_FALSE;
301 bind_[4].buffer_type = MYSQL_TYPE_LONG;
302 bind_[4].buffer =
reinterpret_cast<char*
>(&dhcp6_subnet_id_);
304 bind_[4].is_null = &dhcp6_subnet_id_null_;
309 ipv4_address_ = host->getIPv4Reservation().toUint32();
311 bind_[5].buffer_type = MYSQL_TYPE_LONG;
312 bind_[5].buffer =
reinterpret_cast<char*
>(&ipv4_address_);
314 bind_[5].is_null = &ipv4_address_null_;
318 hostname_length_ = host->getHostname().length();
319 bind_[6].buffer_type = MYSQL_TYPE_STRING;
320 bind_[6].buffer =
reinterpret_cast<char*
>(hostname_);
321 bind_[6].buffer_length = hostname_length_;
324 bind_[7].buffer_type = MYSQL_TYPE_STRING;
326 string classes4_txt = host->getClientClasses4().toText(
",");
328 bind_[7].buffer = dhcp4_client_classes_;
329 bind_[7].buffer_length = classes4_txt.length();
332 bind_[8].buffer_type = MYSQL_TYPE_STRING;
334 string classes6_txt = host->getClientClasses6().toText(
",");
336 bind_[8].buffer = dhcp6_client_classes_;
337 bind_[8].buffer_length = classes6_txt.length();
342 bind_[9].buffer_type = MYSQL_TYPE_STRING;
343 string ctx_txt = ctx->str();
345 bind_[9].buffer = user_context_;
346 bind_[9].buffer_length = ctx_txt.length();
348 bind_[9].buffer_type = MYSQL_TYPE_NULL;
354 dhcp4_next_server_ = host->getNextServer().toUint32();
355 bind_[10].buffer_type = MYSQL_TYPE_LONG;
356 bind_[10].buffer =
reinterpret_cast<char*
>(&dhcp4_next_server_);
362 bind_[11].buffer_type = MYSQL_TYPE_STRING;
363 std::string server_hostname = host->getServerHostname();
364 strncpy(dhcp4_server_hostname_, server_hostname.c_str(),
366 bind_[11].buffer = dhcp4_server_hostname_;
367 bind_[11].buffer_length = server_hostname.length();
370 bind_[12].buffer_type = MYSQL_TYPE_STRING;
371 std::string boot_file_name = host->getBootFileName();
372 strncpy(dhcp4_boot_file_name_, boot_file_name.c_str(),
374 bind_[12].buffer = dhcp4_boot_file_name_;
375 bind_[12].buffer_length = boot_file_name.length();
378 bind_[13].buffer_type = MYSQL_TYPE_STRING;
379 std::string auth_key = host->getKey().toText();
382 bind_[13].buffer = auth_key_;
383 bind_[13].buffer_length = auth_key.length();
385 }
catch (
const std::exception& ex) {
387 "Could not create bind array from Host: " 388 << host->getHostname() <<
", reason: " << ex.
what());
392 std::vector<MYSQL_BIND> vec(bind_.begin(), bind_.begin() + HOST_COLUMNS);
398 vec.push_back(bind_[5]);
399 vec.push_back(bind_[3]);
411 virtual std::vector<MYSQL_BIND> createBindForReceive() {
418 memset(&bind_[0], 0,
sizeof(MYSQL_BIND) * bind_.size());
421 bind_[0].buffer_type = MYSQL_TYPE_LONG;
422 bind_[0].buffer =
reinterpret_cast<char*
>(&host_id_);
426 dhcp_identifier_length_ =
sizeof(dhcp_identifier_buffer_);
427 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
428 bind_[1].buffer =
reinterpret_cast<char*
>(dhcp_identifier_buffer_);
429 bind_[1].buffer_length = dhcp_identifier_length_;
430 bind_[1].length = &dhcp_identifier_length_;
433 bind_[2].buffer_type = MYSQL_TYPE_TINY;
434 bind_[2].buffer =
reinterpret_cast<char*
>(&dhcp_identifier_type_);
439 bind_[3].buffer_type = MYSQL_TYPE_LONG;
440 bind_[3].buffer =
reinterpret_cast<char*
>(&dhcp4_subnet_id_);
442 bind_[3].is_null = &dhcp4_subnet_id_null_;
446 bind_[4].buffer_type = MYSQL_TYPE_LONG;
447 bind_[4].buffer =
reinterpret_cast<char*
>(&dhcp6_subnet_id_);
449 bind_[4].is_null = &dhcp6_subnet_id_null_;
453 bind_[5].buffer_type = MYSQL_TYPE_LONG;
454 bind_[5].buffer =
reinterpret_cast<char*
>(&ipv4_address_);
456 bind_[5].is_null = &ipv4_address_null_;
460 hostname_length_ =
sizeof(hostname_);
461 bind_[6].buffer_type = MYSQL_TYPE_STRING;
462 bind_[6].buffer =
reinterpret_cast<char*
>(hostname_);
463 bind_[6].buffer_length = hostname_length_;
464 bind_[6].length = &hostname_length_;
465 bind_[6].is_null = &hostname_null_;
469 dhcp4_client_classes_length_ =
sizeof(dhcp4_client_classes_);
470 bind_[7].buffer_type = MYSQL_TYPE_STRING;
471 bind_[7].buffer =
reinterpret_cast<char*
>(dhcp4_client_classes_);
472 bind_[7].buffer_length = dhcp4_client_classes_length_;
473 bind_[7].length = &dhcp4_client_classes_length_;
474 bind_[7].is_null = &dhcp4_client_classes_null_;
478 dhcp6_client_classes_length_ =
sizeof(dhcp6_client_classes_);
479 bind_[8].buffer_type = MYSQL_TYPE_STRING;
480 bind_[8].buffer =
reinterpret_cast<char*
>(dhcp6_client_classes_);
481 bind_[8].buffer_length = dhcp6_client_classes_length_;
482 bind_[8].length = &dhcp6_client_classes_length_;
483 bind_[8].is_null = &dhcp6_client_classes_null_;
487 user_context_length_ =
sizeof(user_context_);
488 bind_[9].buffer_type = MYSQL_TYPE_STRING;
489 bind_[9].buffer =
reinterpret_cast<char*
>(user_context_);
490 bind_[9].buffer_length = user_context_length_;
491 bind_[9].length = &user_context_length_;
492 bind_[9].is_null = &user_context_null_;
496 bind_[10].buffer_type = MYSQL_TYPE_LONG;
497 bind_[10].buffer =
reinterpret_cast<char*
>(&dhcp4_next_server_);
499 bind_[10].is_null = &dhcp4_next_server_null_;
503 dhcp4_server_hostname_length_ =
sizeof(dhcp4_server_hostname_);
504 bind_[11].buffer_type = MYSQL_TYPE_STRING;
505 bind_[11].buffer =
reinterpret_cast<char*
>(dhcp4_server_hostname_);
506 bind_[11].buffer_length = dhcp4_server_hostname_length_;
507 bind_[11].length = &dhcp4_server_hostname_length_;
508 bind_[11].is_null = &dhcp4_server_hostname_null_;
512 dhcp4_boot_file_name_length_ =
sizeof(dhcp4_boot_file_name_);
513 bind_[12].buffer_type = MYSQL_TYPE_STRING;
514 bind_[12].buffer =
reinterpret_cast<char*
>(dhcp4_boot_file_name_);
515 bind_[12].buffer_length = dhcp4_boot_file_name_length_;
516 bind_[12].length = &dhcp4_boot_file_name_length_;
517 bind_[12].is_null = &dhcp4_boot_file_name_null_;
521 auth_key_length_ =
sizeof(auth_key_);
522 bind_[13].buffer_type = MYSQL_TYPE_STRING;
523 bind_[13].buffer =
reinterpret_cast<char*
>(auth_key_);
524 bind_[13].buffer_length = auth_key_length_;
525 bind_[13].length = &auth_key_length_;
526 bind_[13].is_null = &auth_key_null_;
529 setErrorIndicators(bind_, error_);
546 if (dhcp_identifier_type_ > MAX_IDENTIFIER_TYPE) {
548 << static_cast<int>(dhcp_identifier_type_));
557 SubnetID ipv4_subnet_id(SUBNET_ID_UNUSED);
558 if (dhcp4_subnet_id_null_ ==
MLM_FALSE) {
559 ipv4_subnet_id =
static_cast<SubnetID>(dhcp4_subnet_id_);
564 SubnetID ipv6_subnet_id(SUBNET_ID_UNUSED);
565 if (dhcp6_subnet_id_null_ ==
MLM_FALSE) {
566 ipv6_subnet_id =
static_cast<SubnetID>(dhcp6_subnet_id_);
573 ipv4_reservation = asiolink::IOAddress(ipv4_address_);
578 std::string hostname;
580 hostname = std::string(hostname_, hostname_length_);
584 std::string dhcp4_client_classes;
585 if (dhcp4_client_classes_null_ ==
MLM_FALSE) {
586 dhcp4_client_classes = std::string(dhcp4_client_classes_,
587 dhcp4_client_classes_length_);
591 std::string dhcp6_client_classes;
592 if (dhcp6_client_classes_null_ ==
MLM_FALSE) {
593 dhcp6_client_classes = std::string(dhcp6_client_classes_,
594 dhcp6_client_classes_length_);
598 std::string user_context;
600 user_context_[user_context_length_] =
'\0';
601 user_context.assign(user_context_);
606 if (dhcp4_next_server_null_ ==
MLM_FALSE) {
607 next_server = asiolink::IOAddress(dhcp4_next_server_);
611 std::string dhcp4_server_hostname;
612 if (dhcp4_server_hostname_null_ ==
MLM_FALSE) {
613 dhcp4_server_hostname = std::string(dhcp4_server_hostname_,
614 dhcp4_server_hostname_length_);
618 std::string dhcp4_boot_file_name;
619 if (dhcp4_boot_file_name_null_ ==
MLM_FALSE) {
620 dhcp4_boot_file_name = std::string(dhcp4_boot_file_name_,
621 dhcp4_boot_file_name_length_);
625 std::string auth_key;
627 auth_key = std::string(auth_key_, auth_key_length_);
631 HostPtr h(
new Host(dhcp_identifier_buffer_, dhcp_identifier_length_,
632 type, ipv4_subnet_id, ipv6_subnet_id, ipv4_reservation,
633 hostname, dhcp4_client_classes, dhcp6_client_classes,
634 next_server, dhcp4_server_hostname,
635 dhcp4_boot_file_name,
AuthKey(auth_key)));
636 h->setHostId(host_id_);
639 if (!user_context.empty()) {
642 if (!ctx || (ctx->getType() != Element::map)) {
644 <<
"' is not a JSON map");
649 <<
"' is invalid JSON: " << ex.
what());
674 if (hosts.empty() || (hosts.back()->getHostId() != getHostId())) {
677 host = retrieveHost();
678 hosts.push_back(host);
692 std::string getErrorColumns() {
693 return (getColumnsInError(error_, columns_));
702 std::vector<MYSQL_BIND> bind_;
705 std::vector<std::string> columns_;
708 std::vector<my_bools> error_;
721 uint8_t dhcp_identifier_buffer_[DUID::MAX_DUID_LEN];
724 unsigned long dhcp_identifier_length_;
728 uint8_t dhcp_identifier_type_;
731 uint32_t dhcp4_subnet_id_;
734 uint32_t dhcp6_subnet_id_;
737 uint32_t ipv4_address_;
743 unsigned long hostname_length_;
750 unsigned long dhcp4_client_classes_length_;
757 unsigned long dhcp6_client_classes_length_;
763 unsigned long user_context_length_;
766 uint32_t dhcp4_next_server_;
772 unsigned long dhcp4_server_hostname_length_;
778 unsigned long dhcp4_boot_file_name_length_;
784 unsigned long auth_key_length_;
803 my_bool dhcp4_client_classes_null_;
807 my_bool dhcp6_client_classes_null_;
813 my_bool dhcp4_next_server_null_;
816 my_bool dhcp4_server_hostname_null_;
819 my_bool dhcp4_boot_file_name_null_;
836 class MySqlHostWithOptionsExchange :
public MySqlHostExchange {
840 static const size_t OPTION_COLUMNS = 7;
856 class OptionProcessor {
866 const size_t start_column)
867 : universe_(universe), start_column_(start_column), option_id_(0),
868 code_(0), value_length_(0), formatted_value_length_(0),
869 space_length_(0), persistent_(false), user_context_length_(0),
873 option_id_index_(start_column), code_index_(start_column_ + 1),
874 value_index_(start_column_ + 2),
875 formatted_value_index_(start_column_ + 3),
876 space_index_(start_column_ + 4),
877 persistent_index_(start_column_ + 5),
878 user_context_index_(start_column_ + 6),
879 most_recent_option_id_(0) {
881 memset(value_, 0,
sizeof(value_));
882 memset(formatted_value_, 0,
sizeof(formatted_value_));
883 memset(space_, 0,
sizeof(space_));
884 memset(user_context_, 0,
sizeof(user_context_));
888 uint64_t getOptionId()
const {
913 if ((option_id_null_ ==
MLM_TRUE) ||
914 (most_recent_option_id_ >= option_id_)) {
920 most_recent_option_id_ = option_id_;
927 space_[space_length_] =
'\0';
928 space.assign(space_);
933 space = (universe_ == Option::V4 ?
938 std::string formatted_value;
939 if (formatted_value_null_ ==
MLM_FALSE) {
940 formatted_value_[formatted_value_length_] =
'\0';
941 formatted_value.assign(formatted_value_);
945 std::string user_context;
947 user_context_[user_context_length_] =
'\0';
948 user_context.assign(user_context_);
964 uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(space);
966 def = LibDHCP::getVendorOptionDef(universe_, vendor_id,
code_);
973 def = LibDHCP::getRuntimeOptionDef(space,
code_);
981 option.reset(
new Option(universe_,
code_, buf.begin(),
988 if (formatted_value.empty()) {
990 option = def->optionFactory(universe_,
code_, buf.begin(),
995 std::vector<std::string> split_vec;
996 boost::split(split_vec, formatted_value, boost::is_any_of(
","));
997 option = def->optionFactory(universe_,
code_, split_vec);
1004 if (!user_context.empty()) {
1007 if (!ctx || (ctx->getType() != Element::map)) {
1009 <<
"' is no a JSON map");
1011 desc.setContext(ctx);
1014 <<
"' is invalid JSON: " << ex.
what());
1018 cfg->add(desc, space);
1025 void setColumnNames(std::vector<std::string>& columns) {
1026 columns[option_id_index_] =
"option_id";
1027 columns[code_index_] =
"code";
1028 columns[value_index_] =
"value";
1029 columns[formatted_value_index_] =
"formatted_value";
1030 columns[space_index_] =
"space";
1031 columns[persistent_index_] =
"persistent";
1032 columns[user_context_index_] =
"user_context";
1041 void setBindFields(std::vector<MYSQL_BIND>& bind) {
1045 most_recent_option_id_ = 0;
1049 persistent_ =
false;
1057 memset(value_, 0,
sizeof(value_));
1058 memset(formatted_value_, 0,
sizeof(formatted_value_));
1059 memset(space_, 0,
sizeof(space_));
1060 memset(user_context_, 0,
sizeof(user_context_));
1063 bind[option_id_index_].buffer_type = MYSQL_TYPE_LONG;
1064 bind[option_id_index_].buffer =
reinterpret_cast<char*
>(&option_id_);
1065 bind[option_id_index_].is_unsigned =
MLM_TRUE;
1066 bind[option_id_index_].is_null = &option_id_null_;
1069 bind[code_index_].buffer_type = MYSQL_TYPE_SHORT;
1070 bind[code_index_].buffer =
reinterpret_cast<char*
>(&
code_);
1071 bind[code_index_].is_unsigned =
MLM_TRUE;
1072 bind[code_index_].is_null = &code_null_;
1075 value_length_ =
sizeof(value_);
1076 bind[value_index_].buffer_type = MYSQL_TYPE_BLOB;
1077 bind[value_index_].buffer =
reinterpret_cast<char*
>(value_);
1078 bind[value_index_].buffer_length = value_length_;
1079 bind[value_index_].length = &value_length_;
1080 bind[value_index_].is_null = &value_null_;
1083 formatted_value_length_ =
sizeof(formatted_value_);
1084 bind[formatted_value_index_].buffer_type = MYSQL_TYPE_STRING;
1085 bind[formatted_value_index_].buffer =
reinterpret_cast<char*
>(formatted_value_);
1086 bind[formatted_value_index_].buffer_length = formatted_value_length_;
1087 bind[formatted_value_index_].length = &formatted_value_length_;
1088 bind[formatted_value_index_].is_null = &formatted_value_null_;
1091 space_length_ =
sizeof(space_);
1092 bind[space_index_].buffer_type = MYSQL_TYPE_STRING;
1093 bind[space_index_].buffer =
reinterpret_cast<char*
>(space_);
1094 bind[space_index_].buffer_length = space_length_;
1095 bind[space_index_].length = &space_length_;
1096 bind[space_index_].is_null = &space_null_;
1099 bind[persistent_index_].buffer_type = MYSQL_TYPE_TINY;
1100 bind[persistent_index_].buffer =
reinterpret_cast<char*
>(&persistent_);
1101 bind[persistent_index_].is_unsigned =
MLM_TRUE;
1104 user_context_length_ =
sizeof(user_context_);
1105 bind[user_context_index_].buffer_type = MYSQL_TYPE_STRING;
1106 bind[user_context_index_].buffer =
reinterpret_cast<char*
>(user_context_);
1107 bind[user_context_index_].buffer_length = user_context_length_;
1108 bind[user_context_index_].length = &user_context_length_;
1109 bind[user_context_index_].is_null = &user_context_null_;
1118 size_t start_column_;
1121 uint32_t option_id_;
1130 unsigned long value_length_;
1136 unsigned long formatted_value_length_;
1142 unsigned long space_length_;
1152 unsigned long user_context_length_;
1168 my_bool formatted_value_null_;
1179 size_t option_id_index_;
1186 size_t value_index_;
1189 size_t formatted_value_index_;
1192 size_t space_index_;
1195 size_t persistent_index_;
1199 size_t user_context_index_;
1202 uint32_t most_recent_option_id_;
1206 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
1216 enum FetchedOptions {
1230 MySqlHostWithOptionsExchange(
const FetchedOptions& fetched_options,
1231 const size_t additional_columns_num = 0)
1232 : MySqlHostExchange(getRequiredColumnsNum(fetched_options)
1233 + additional_columns_num),
1234 opt_proc4_(), opt_proc6_() {
1237 if ((fetched_options == DHCP4_ONLY) ||
1238 (fetched_options == DHCP4_AND_DHCP6)) {
1239 opt_proc4_.reset(
new OptionProcessor(Option::V4,
1240 findAvailColumn()));
1241 opt_proc4_->setColumnNames(columns_);
1245 if ((fetched_options == DHCP6_ONLY) ||
1246 (fetched_options == DHCP4_AND_DHCP6)) {
1247 opt_proc6_.reset(
new OptionProcessor(Option::V6,
1248 findAvailColumn()));
1249 opt_proc6_->setColumnNames(columns_);
1264 if (!hosts.empty()) {
1273 most_recent_host = boost::const_pointer_cast<
Host>(hosts.back());
1279 if (!most_recent_host || (most_recent_host->getHostId() < getHostId())) {
1280 HostPtr host = retrieveHost();
1281 hosts.push_back(host);
1282 most_recent_host = host;
1288 opt_proc4_->retrieveOption(cfg);
1294 opt_proc6_->retrieveOption(cfg);
1301 virtual std::vector<MYSQL_BIND> createBindForReceive() {
1303 static_cast<void>(MySqlHostExchange::createBindForReceive());
1307 opt_proc4_->setBindFields(bind_);
1312 opt_proc6_->setBindFields(bind_);
1316 setErrorIndicators(bind_, error_);
1334 static size_t getRequiredColumnsNum(
const FetchedOptions& fetched_options) {
1335 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
1342 OptionProcessorPtr opt_proc4_;
1347 OptionProcessorPtr opt_proc6_;
1362 class MySqlHostIPv6Exchange :
public MySqlHostWithOptionsExchange {
1366 static const size_t RESERVATION_COLUMNS = 5;
1374 MySqlHostIPv6Exchange(
const FetchedOptions& fetched_options)
1375 : MySqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
1377 reserv_type_(0), reserv_type_null_(
MLM_FALSE),
1378 ipv6_address_buffer_len_(0), prefix_len_(0), iaid_(0),
1379 reservation_id_index_(findAvailColumn()),
1380 address_index_(reservation_id_index_ + 1),
1381 prefix_len_index_(reservation_id_index_ + 2),
1382 type_index_(reservation_id_index_ + 3),
1383 iaid_index_(reservation_id_index_ + 4),
1384 most_recent_reservation_id_(0) {
1386 memset(ipv6_address_buffer_, 0,
sizeof(ipv6_address_buffer_));
1389 columns_[reservation_id_index_] =
"reservation_id";
1390 columns_[address_index_] =
"address";
1391 columns_[prefix_len_index_] =
"prefix_len";
1392 columns_[type_index_] =
"type";
1393 columns_[iaid_index_] =
"dhcp6_iaid";
1399 uint32_t getReservationId()
const {
1401 return (reservation_id_);
1416 switch (reserv_type_) {
1418 type = IPv6Resrv::TYPE_NA;
1422 type = IPv6Resrv::TYPE_PD;
1427 "invalid IPv6 reservation type returned: " 1428 << static_cast<int>(reserv_type_)
1429 <<
". Only 0 or 2 are allowed.");
1432 ipv6_address_buffer_[ipv6_address_buffer_len_] =
'\0';
1433 std::string address = ipv6_address_buffer_;
1460 MySqlHostWithOptionsExchange::processFetchedData(hosts);
1462 if (getReservationId() == 0) {
1466 if (hosts.empty()) {
1468 " IPv6 reservation");
1470 HostPtr host = boost::const_pointer_cast<
Host>(hosts.back());
1474 if (getReservationId() > most_recent_reservation_id_) {
1475 most_recent_reservation_id_ = getReservationId();
1477 if (most_recent_reservation_id_ > 0) {
1478 host->addReservation(retrieveReservation());
1491 virtual std::vector<MYSQL_BIND> createBindForReceive() {
1494 most_recent_reservation_id_ = 0;
1497 static_cast<void>(MySqlHostWithOptionsExchange::createBindForReceive());
1500 bind_[reservation_id_index_].buffer_type = MYSQL_TYPE_LONG;
1501 bind_[reservation_id_index_].buffer =
reinterpret_cast<char*
>(&reservation_id_);
1502 bind_[reservation_id_index_].is_unsigned =
MLM_TRUE;
1505 ipv6_address_buffer_len_ =
sizeof(ipv6_address_buffer_) - 1;
1506 bind_[address_index_].buffer_type = MYSQL_TYPE_STRING;
1507 bind_[address_index_].buffer = ipv6_address_buffer_;
1508 bind_[address_index_].buffer_length = ipv6_address_buffer_len_;
1509 bind_[address_index_].length = &ipv6_address_buffer_len_;
1512 bind_[prefix_len_index_].buffer_type = MYSQL_TYPE_TINY;
1513 bind_[prefix_len_index_].buffer =
reinterpret_cast<char*
>(&prefix_len_);
1514 bind_[prefix_len_index_].is_unsigned =
MLM_TRUE;
1518 bind_[type_index_].buffer_type = MYSQL_TYPE_TINY;
1519 bind_[type_index_].buffer =
reinterpret_cast<char*
>(&reserv_type_);
1520 bind_[type_index_].is_unsigned =
MLM_TRUE;
1521 bind_[type_index_].is_null = &reserv_type_null_;
1524 bind_[iaid_index_].buffer_type = MYSQL_TYPE_LONG;
1525 bind_[iaid_index_].buffer =
reinterpret_cast<char*
>(&iaid_);
1526 bind_[iaid_index_].is_unsigned =
MLM_TRUE;
1529 setErrorIndicators(bind_, error_);
1537 uint32_t reservation_id_;
1540 uint8_t reserv_type_;
1552 unsigned long ipv6_address_buffer_len_;
1555 uint8_t prefix_len_;
1562 size_t reservation_id_index_;
1566 size_t address_index_;
1569 size_t prefix_len_index_;
1580 uint32_t most_recent_reservation_id_;
1593 class MySqlIPv6ReservationExchange {
1597 static const size_t RESRV_COLUMNS = 6;
1604 MySqlIPv6ReservationExchange()
1605 : host_id_(0), address_(
"::"), address_len_(0), prefix_len_(0), type_(0),
1609 std::fill(&error_[0], &error_[RESRV_COLUMNS],
MLM_FALSE);
1612 columns_[0] =
"host_id";
1613 columns_[1] =
"address";
1614 columns_[2] =
"prefix_len";
1615 columns_[3] =
"type";
1616 columns_[4] =
"dhcp6_iaid";
1617 BOOST_STATIC_ASSERT(4 < RESRV_COLUMNS);
1634 std::vector<MYSQL_BIND> createBindForSend(
const IPv6Resrv& resv,
1636 const bool unique_ip) {
1647 memset(bind_, 0,
sizeof(bind_));
1654 address_len_ = address_.length();
1655 bind_[0].buffer_type = MYSQL_TYPE_BLOB;
1656 bind_[0].buffer =
reinterpret_cast<char*
> 1657 (
const_cast<char*
>(address_.c_str()));
1658 bind_[0].buffer_length = address_len_;
1659 bind_[0].length = &address_len_;
1663 bind_[1].buffer_type = MYSQL_TYPE_TINY;
1664 bind_[1].buffer =
reinterpret_cast<char*
>(&prefix_len_);
1669 type_ = resv.
getType() == IPv6Resrv::TYPE_NA ? 0 : 2;
1670 bind_[2].buffer_type = MYSQL_TYPE_TINY;
1671 bind_[2].buffer =
reinterpret_cast<char*
>(&type_);
1677 bind_[3].buffer_type = MYSQL_TYPE_LONG;
1678 bind_[3].buffer =
reinterpret_cast<char*
>(&iaid_);
1682 bind_[4].buffer_type = MYSQL_TYPE_LONG;
1683 bind_[4].buffer =
reinterpret_cast<char*
>(&host_id_);
1686 }
catch (
const std::exception& ex) {
1688 "Could not create bind array from IPv6 Reservation: " 1689 << resv_.toText() <<
", reason: " << ex.
what());
1695 std::vector<MYSQL_BIND> vec(&bind_[0], &bind_[RESRV_COLUMNS-1]);
1701 vec.push_back(bind_[0]);
1702 vec.push_back(bind_[1]);
1714 std::string address_;
1717 unsigned long address_len_;
1720 uint8_t prefix_len_;
1732 MYSQL_BIND bind_[RESRV_COLUMNS];
1735 std::string columns_[RESRV_COLUMNS];
1739 my_bool error_[RESRV_COLUMNS];
1745 class MySqlOptionExchange {
1749 static const size_t OPTION_COLUMNS = 10;
1754 MySqlOptionExchange()
1755 : type_(0), value_len_(0), formatted_value_len_(0), space_(),
1756 space_len_(0), persistent_(false), user_context_(),
1757 user_context_len_(0), subnet_id_(SUBNET_ID_UNUSED),
1758 host_id_(0), option_() {
1760 BOOST_STATIC_ASSERT(9 < OPTION_COLUMNS);
1766 std::vector<MYSQL_BIND> createBindForSend(
const OptionDescriptor& opt_desc,
1767 const std::string& opt_space,
1775 memset(bind_, 0,
sizeof(bind_));
1781 bind_[0].buffer_type = MYSQL_TYPE_NULL;
1784 type_ = option_->getType();
1785 bind_[1].buffer_type = MYSQL_TYPE_SHORT;
1786 bind_[1].buffer =
reinterpret_cast<char*
>(&type_);
1797 const char* buf_ptr =
static_cast<const char*
>(buf.getData());
1798 value_.assign(buf_ptr + opt_desc.
option_->getHeaderLen(),
1799 buf_ptr + buf.getLength());
1800 value_len_ = value_.size();
1801 bind_[2].buffer_type = MYSQL_TYPE_BLOB;
1802 bind_[2].buffer = &value_[0];
1803 bind_[2].buffer_length = value_len_;
1804 bind_[2].length = &value_len_;
1810 bind_[2].buffer_type = MYSQL_TYPE_NULL;
1816 bind_[3].buffer_type = MYSQL_TYPE_STRING;
1818 bind_[3].buffer_length = formatted_value_len_;
1819 bind_[3].length = &formatted_value_len_;
1822 bind_[3].buffer_type = MYSQL_TYPE_NULL;
1827 space_len_ = space_.size();
1828 bind_[4].buffer_type = MYSQL_TYPE_STRING;
1829 bind_[4].buffer =
const_cast<char*
>(space_.c_str());
1830 bind_[4].buffer_length = space_len_;
1831 bind_[4].length = &space_len_;
1835 bind_[5].buffer_type = MYSQL_TYPE_TINY;
1836 bind_[5].buffer =
reinterpret_cast<char*
>(&persistent_);
1842 user_context_ = ctx->str();
1843 user_context_len_ = user_context_.size();
1844 bind_[6].buffer_type = MYSQL_TYPE_STRING;
1845 bind_[6].buffer =
const_cast<char*
>(user_context_.c_str());
1846 bind_[6].buffer_length = user_context_len_;
1847 bind_[6].length = &user_context_len_;
1849 bind_[6].buffer_type = MYSQL_TYPE_NULL;
1854 subnet_id_ = subnet_id;
1855 bind_[7].buffer_type = MYSQL_TYPE_LONG;
1856 bind_[7].buffer =
reinterpret_cast<char*
>(subnet_id_);
1860 bind_[7].buffer_type = MYSQL_TYPE_NULL;
1865 bind_[8].buffer_type = MYSQL_TYPE_LONG;
1866 bind_[8].buffer =
reinterpret_cast<char*
>(&host_id_);
1869 }
catch (
const std::exception& ex) {
1871 "Could not create bind array for inserting DHCP " 1872 "option: " << option_->toText() <<
", reason: " 1876 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[OPTION_COLUMNS]));
1885 std::vector<uint8_t> value_;
1888 unsigned long value_len_;
1891 unsigned long formatted_value_len_;
1897 unsigned long space_len_;
1904 std::string user_context_;
1907 unsigned long user_context_len_;
1910 uint32_t subnet_id_;
1919 MYSQL_BIND bind_[OPTION_COLUMNS];
2105 std::pair<uint32_t, uint32_t> getVersion()
const;
2117 std::vector<MYSQL_BIND>& bind);
2152 const std::string& opt_space,
2166 const uint64_t host_id);
2182 const char* what)
const;
2205 boost::shared_ptr<MySqlHostExchange> exchange,
2229 const uint8_t* identifier_begin,
2230 const size_t identifier_len,
2232 boost::shared_ptr<MySqlHostExchange> exchange)
const;
2266 typedef boost::array<TaggedStatement, MySqlHostDataSourceImpl::NUM_STATEMENTS>
2267 TaggedStatementArray;
2271 TaggedStatementArray tagged_statements = { {
2276 {MySqlHostDataSourceImpl::GET_HOST_DHCPID,
2277 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2278 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, " 2279 "h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, " 2281 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2282 "h.dhcp4_boot_file_name, h.auth_key, " 2283 "o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, " 2284 "o4.persistent, o4.user_context, " 2285 "o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, " 2286 "o6.persistent, o6.user_context, " 2287 "r.reservation_id, r.address, r.prefix_len, r.type, " 2290 "LEFT JOIN dhcp4_options AS o4 " 2291 "ON h.host_id = o4.host_id " 2292 "LEFT JOIN dhcp6_options AS o6 " 2293 "ON h.host_id = o6.host_id " 2294 "LEFT JOIN ipv6_reservations AS r " 2295 "ON h.host_id = r.host_id " 2296 "WHERE dhcp_identifier = ? AND dhcp_identifier_type = ? " 2297 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"},
2302 {MySqlHostDataSourceImpl::GET_HOST_ADDR,
2303 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2304 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2305 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2306 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2307 "h.dhcp4_boot_file_name, h.auth_key, " 2308 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2309 "o.persistent, o.user_context " 2311 "LEFT JOIN dhcp4_options AS o " 2312 "ON h.host_id = o.host_id " 2313 "WHERE ipv4_address = ? " 2314 "ORDER BY h.host_id, o.option_id"},
2319 {MySqlHostDataSourceImpl::GET_HOST_SUBID4_DHCPID,
2320 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2321 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2322 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2323 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2324 "h.dhcp4_boot_file_name, h.auth_key, " 2325 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2326 "o.persistent, o.user_context " 2328 "LEFT JOIN dhcp4_options AS o " 2329 "ON h.host_id = o.host_id " 2330 "WHERE h.dhcp4_subnet_id = ? AND h.dhcp_identifier_type = ? " 2331 "AND h.dhcp_identifier = ? " 2332 "ORDER BY h.host_id, o.option_id"},
2337 {MySqlHostDataSourceImpl::GET_HOST_SUBID6_DHCPID,
2338 "SELECT h.host_id, h.dhcp_identifier, " 2339 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2340 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2341 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2342 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2343 "h.dhcp4_boot_file_name, h.auth_key, " 2344 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2345 "o.persistent, o.user_context, " 2346 "r.reservation_id, r.address, r.prefix_len, r.type, " 2349 "LEFT JOIN dhcp6_options AS o " 2350 "ON h.host_id = o.host_id " 2351 "LEFT JOIN ipv6_reservations AS r " 2352 "ON h.host_id = r.host_id " 2353 "WHERE h.dhcp6_subnet_id = ? AND h.dhcp_identifier_type = ? " 2354 "AND h.dhcp_identifier = ? " 2355 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2361 {MySqlHostDataSourceImpl::GET_HOST_SUBID_ADDR,
2362 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2363 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2364 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2365 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2366 "h.dhcp4_boot_file_name, h.auth_key, " 2367 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2368 "o.persistent, o.user_context " 2370 "LEFT JOIN dhcp4_options AS o " 2371 "ON h.host_id = o.host_id " 2372 "WHERE h.dhcp4_subnet_id = ? AND h.ipv4_address = ? " 2373 "ORDER BY h.host_id, o.option_id"},
2381 {MySqlHostDataSourceImpl::GET_HOST_PREFIX,
2382 "SELECT h.host_id, h.dhcp_identifier, " 2383 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2384 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2385 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2386 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2387 "h.dhcp4_boot_file_name, h.auth_key, " 2388 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2389 "o.persistent, o.user_context," 2390 "r.reservation_id, r.address, r.prefix_len, r.type, " 2393 "LEFT JOIN dhcp6_options AS o " 2394 "ON h.host_id = o.host_id " 2395 "LEFT JOIN ipv6_reservations AS r " 2396 "ON h.host_id = r.host_id " 2397 "WHERE h.host_id = " 2398 "( SELECT host_id FROM ipv6_reservations " 2399 "WHERE address = ? AND prefix_len = ? ) " 2400 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2408 {MySqlHostDataSourceImpl::GET_HOST_SUBID6_ADDR,
2409 "SELECT h.host_id, h.dhcp_identifier, " 2410 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2411 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2412 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2413 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2414 "h.dhcp4_boot_file_name, h.auth_key, " 2415 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2416 "o.persistent, o.user_context, " 2417 "r.reservation_id, r.address, r.prefix_len, r.type, " 2420 "LEFT JOIN dhcp6_options AS o " 2421 "ON h.host_id = o.host_id " 2422 "LEFT JOIN ipv6_reservations AS r " 2423 "ON h.host_id = r.host_id " 2424 "WHERE h.dhcp6_subnet_id = ? AND r.address = ? " 2425 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2430 {MySqlHostDataSourceImpl::GET_HOST_SUBID4,
2431 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2432 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2433 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2434 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2435 "h.dhcp4_boot_file_name, h.auth_key, " 2436 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2437 "o.persistent, o.user_context " 2439 "LEFT JOIN dhcp4_options AS o " 2440 "ON h.host_id = o.host_id " 2441 "WHERE h.dhcp4_subnet_id = ? " 2442 "ORDER BY h.host_id, o.option_id"},
2448 {MySqlHostDataSourceImpl::GET_HOST_SUBID6,
2449 "SELECT h.host_id, h.dhcp_identifier, " 2450 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2451 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2452 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2453 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2454 "h.dhcp4_boot_file_name, h.auth_key, " 2455 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2456 "o.persistent, o.user_context, " 2457 "r.reservation_id, r.address, r.prefix_len, r.type, " 2460 "LEFT JOIN dhcp6_options AS o " 2461 "ON h.host_id = o.host_id " 2462 "LEFT JOIN ipv6_reservations AS r " 2463 "ON h.host_id = r.host_id " 2464 "WHERE h.dhcp6_subnet_id = ? " 2465 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2471 {MySqlHostDataSourceImpl::GET_HOST_HOSTNAME,
2472 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2473 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, " 2474 "h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, " 2476 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2477 "h.dhcp4_boot_file_name, h.auth_key, " 2478 "o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, " 2479 "o4.persistent, o4.user_context, " 2480 "o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, " 2481 "o6.persistent, o6.user_context, " 2482 "r.reservation_id, r.address, r.prefix_len, r.type, " 2485 "LEFT JOIN dhcp4_options AS o4 " 2486 "ON h.host_id = o4.host_id " 2487 "LEFT JOIN dhcp6_options AS o6 " 2488 "ON h.host_id = o6.host_id " 2489 "LEFT JOIN ipv6_reservations AS r " 2490 "ON h.host_id = r.host_id " 2491 "WHERE h.hostname = ? " 2492 "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"},
2497 {MySqlHostDataSourceImpl::GET_HOST_HOSTNAME_SUBID4,
2498 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2499 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2500 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2501 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2502 "h.dhcp4_boot_file_name, h.auth_key, " 2503 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2504 "o.persistent, o.user_context " 2506 "LEFT JOIN dhcp4_options AS o " 2507 "ON h.host_id = o.host_id " 2508 "WHERE h.hostname = ? AND h.dhcp4_subnet_id = ? " 2509 "ORDER BY h.host_id, o.option_id"},
2514 {MySqlHostDataSourceImpl::GET_HOST_HOSTNAME_SUBID6,
2515 "SELECT h.host_id, h.dhcp_identifier, " 2516 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2517 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2518 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2519 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2520 "h.dhcp4_boot_file_name, h.auth_key, " 2521 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2522 "o.persistent, o.user_context, " 2523 "r.reservation_id, r.address, r.prefix_len, r.type, " 2526 "LEFT JOIN dhcp6_options AS o " 2527 "ON h.host_id = o.host_id " 2528 "LEFT JOIN ipv6_reservations AS r " 2529 "ON h.host_id = r.host_id " 2530 "WHERE h.hostname = ? AND h.dhcp6_subnet_id = ? " 2531 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2538 {MySqlHostDataSourceImpl::GET_HOST_SUBID4_PAGE,
2539 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2540 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2541 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2542 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2543 "h.dhcp4_boot_file_name, h.auth_key, " 2544 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2545 "o.persistent, o.user_context " 2546 "FROM ( SELECT * FROM hosts AS h " 2547 "WHERE h.dhcp4_subnet_id = ? AND h.host_id > ? " 2548 "ORDER BY h.host_id " 2550 "LEFT JOIN dhcp4_options AS o " 2551 "ON h.host_id = o.host_id " 2552 "ORDER BY h.host_id, o.option_id"},
2559 {MySqlHostDataSourceImpl::GET_HOST_SUBID6_PAGE,
2560 "SELECT h.host_id, h.dhcp_identifier, " 2561 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2562 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2563 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2564 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2565 "h.dhcp4_boot_file_name, h.auth_key, " 2566 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2567 "o.persistent, o.user_context, " 2568 "r.reservation_id, r.address, r.prefix_len, r.type, " 2570 "FROM ( SELECT * FROM hosts AS h " 2571 "WHERE h.dhcp6_subnet_id = ? AND h.host_id > ? " 2572 "ORDER BY h.host_id " 2574 "LEFT JOIN dhcp6_options AS o " 2575 "ON h.host_id = o.host_id " 2576 "LEFT JOIN ipv6_reservations AS r " 2577 "ON h.host_id = r.host_id " 2578 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2585 {MySqlHostDataSourceImpl::GET_HOST_PAGE4,
2586 "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, " 2587 "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2588 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2589 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2590 "h.dhcp4_boot_file_name, h.auth_key, " 2591 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2592 "o.persistent, o.user_context " 2593 "FROM ( SELECT * FROM hosts AS h " 2594 "WHERE h.host_id > ? " 2595 "ORDER BY h.host_id " 2597 "LEFT JOIN dhcp4_options AS o " 2598 "ON h.host_id = o.host_id " 2599 "ORDER BY h.host_id, o.option_id"},
2606 {MySqlHostDataSourceImpl::GET_HOST_PAGE6,
2607 "SELECT h.host_id, h.dhcp_identifier, " 2608 "h.dhcp_identifier_type, h.dhcp4_subnet_id, " 2609 "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, " 2610 "h.dhcp4_client_classes, h.dhcp6_client_classes, h.user_context, " 2611 "h.dhcp4_next_server, h.dhcp4_server_hostname, " 2612 "h.dhcp4_boot_file_name, h.auth_key, " 2613 "o.option_id, o.code, o.value, o.formatted_value, o.space, " 2614 "o.persistent, o.user_context, " 2615 "r.reservation_id, r.address, r.prefix_len, r.type, " 2617 "FROM ( SELECT * FROM hosts AS h " 2618 "WHERE h.host_id > ? " 2619 "ORDER BY h.host_id " 2621 "LEFT JOIN dhcp6_options AS o " 2622 "ON h.host_id = o.host_id " 2623 "LEFT JOIN ipv6_reservations AS r " 2624 "ON h.host_id = r.host_id " 2625 "ORDER BY h.host_id, o.option_id, r.reservation_id"},
2629 {MySqlHostDataSourceImpl::INSERT_HOST_NON_UNIQUE_IP,
2630 "INSERT INTO hosts(host_id, dhcp_identifier, dhcp_identifier_type, " 2631 "dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, " 2632 "dhcp4_client_classes, dhcp6_client_classes, " 2633 "user_context, dhcp4_next_server, " 2634 "dhcp4_server_hostname, dhcp4_boot_file_name, auth_key) " 2635 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
2648 {MySqlHostDataSourceImpl::INSERT_HOST_UNIQUE_IP,
2649 "INSERT INTO hosts(host_id, dhcp_identifier, dhcp_identifier_type, " 2650 "dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, " 2651 "dhcp4_client_classes, dhcp6_client_classes, " 2652 "user_context, dhcp4_next_server, " 2653 "dhcp4_server_hostname, dhcp4_boot_file_name, auth_key) " 2654 "SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM DUAL " 2655 "WHERE NOT EXISTS (" 2656 "SELECT 1 FROM hosts " 2657 "WHERE ipv4_address = ? AND dhcp4_subnet_id = ? " 2663 {MySqlHostDataSourceImpl::INSERT_V6_RESRV_NON_UNIQUE,
2664 "INSERT INTO ipv6_reservations(address, prefix_len, type, " 2665 "dhcp6_iaid, host_id) " 2666 "VALUES (?, ?, ?, ?, ?)"},
2670 {MySqlHostDataSourceImpl::INSERT_V6_RESRV_UNIQUE,
2671 "INSERT INTO ipv6_reservations(address, prefix_len, type, " 2672 "dhcp6_iaid, host_id) " 2673 "SELECT ?, ?, ?, ?, ? FROM DUAL " 2674 "WHERE NOT EXISTS (" 2675 "SELECT 1 FROM ipv6_reservations " 2676 "WHERE address = ? AND prefix_len = ? " 2682 {MySqlHostDataSourceImpl::INSERT_V4_HOST_OPTION,
2683 "INSERT INTO dhcp4_options(option_id, code, value, formatted_value, space, " 2684 "persistent, user_context, dhcp4_subnet_id, host_id, scope_id) " 2685 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 3)"},
2689 {MySqlHostDataSourceImpl::INSERT_V6_HOST_OPTION,
2690 "INSERT INTO dhcp6_options(option_id, code, value, formatted_value, space, " 2691 "persistent, user_context, dhcp6_subnet_id, host_id, scope_id) " 2692 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 3)"},
2695 {MySqlHostDataSourceImpl::DEL_HOST_ADDR4,
2696 "DELETE FROM hosts WHERE dhcp4_subnet_id = ? AND ipv4_address = ?"},
2699 {MySqlHostDataSourceImpl::DEL_HOST_ADDR6,
2700 "DELETE h FROM hosts AS h " 2701 "INNER JOIN ipv6_reservations AS r " 2702 "ON h.host_id = r.host_id " 2703 "WHERE h.dhcp6_subnet_id = ? AND r.address = ?"},
2706 {MySqlHostDataSourceImpl::DEL_HOST_SUBID4_ID,
2707 "DELETE FROM hosts WHERE dhcp4_subnet_id = ? AND dhcp_identifier_type=? " 2708 "AND dhcp_identifier = ?"},
2711 {MySqlHostDataSourceImpl::DEL_HOST_SUBID6_ID,
2712 "DELETE FROM hosts WHERE dhcp6_subnet_id = ? AND dhcp_identifier_type=? " 2713 "AND dhcp_identifier = ?"}
2724 : conn_(parameters, io_service_accessor, db_reconnect_callback),
2725 is_readonly_(true) {
2733 if (MultiThreadingMgr::instance().getMode()) {
2737 lock_guard<mutex> lock(mgr_.
pool_->mutex_);
2738 if (!mgr_.
pool_->pool_.empty()) {
2740 mgr_.
pool_->pool_.pop_back();
2748 if (mgr_.
pool_->pool_.empty()) {
2756 if (MultiThreadingMgr::instance().getMode()) {
2758 lock_guard<mutex> lock(mgr_.
pool_->mutex_);
2760 if (
ctx_->conn_.isUnusable()) {
2763 }
else if (
ctx_->conn_.isUnusable()) {
2769 : parameters_(parameters), ip_reservations_unique_(true), unusable_(false),
2774 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2780 std::pair<uint32_t, uint32_t> db_version =
getVersion();
2781 if (code_version != db_version) {
2783 "MySQL schema version mismatch: need version: " 2784 << code_version.first <<
"." << code_version.second
2785 <<
" found version: " << db_version.first <<
"." 2786 << db_version.second);
2803 ctx->conn_.openDatabase();
2806 if (ctx->conn_.getTls()) {
2807 std::string cipher = ctx->conn_.getTlsCipher();
2808 if (cipher.empty()) {
2820 ctx->conn_.prepareStatements(tagged_statements.begin(),
2825 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2829 if (!ctx->is_readonly_) {
2832 tagged_statements.end());
2839 ctx->host_ipv4_exchange_.reset(
new MySqlHostWithOptionsExchange(MySqlHostWithOptionsExchange::DHCP4_ONLY));
2840 ctx->host_ipv6_exchange_.reset(
new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::DHCP6_ONLY));
2841 ctx->host_ipv46_exchange_.reset(
new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2842 ctx->host_ipv6_reservation_exchange_.reset(
new MySqlIPv6ReservationExchange());
2843 ctx->host_option_exchange_.reset(
new MySqlOptionExchange());
2863 bool reopened =
false;
2865 const std::string timer_name = db_reconnect_ctl->timerName();
2870 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2871 for (std::string& hds : host_db_access_list) {
2878 }
catch (
const std::exception& ex) {
2894 if (!db_reconnect_ctl->checkRetries()) {
2897 .arg(db_reconnect_ctl->maxRetries());
2910 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2911 .arg(db_reconnect_ctl->maxRetries())
2912 .arg(db_reconnect_ctl->retryInterval());
2918 db_reconnect_ctl->retryInterval(),
2927 std::pair<uint32_t, uint32_t>
2938 std::vector<MYSQL_BIND>& bind) {
2940 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
2941 checkError(ctx, status, stindex,
"unable to bind parameters");
2948 if (mysql_errno(ctx->conn_.mysql_) == ER_DUP_ENTRY) {
2951 checkError(ctx, status, stindex,
"unable to execute");
2959 my_ulonglong numrows = mysql_stmt_affected_rows(ctx->conn_.statements_[stindex]);
2970 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
2971 checkError(ctx, status, stindex,
"unable to bind parameters");
2977 checkError(ctx, status, stindex,
"unable to execute");
2981 my_ulonglong numrows = mysql_stmt_affected_rows(ctx->conn_.statements_[stindex]);
2983 return (numrows != 0);
2990 std::vector<MYSQL_BIND> bind = ctx->host_ipv6_reservation_exchange_->
3000 const std::string& opt_space,
3003 std::vector<MYSQL_BIND> bind = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space, subnet_id,
id);
3012 const uint64_t host_id) {
3015 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
3016 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
3017 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
3018 vendor_spaces.end());
3022 for (
auto space = option_spaces.begin(); space != option_spaces.end(); ++space) {
3024 if (options && !options->empty()) {
3025 for (
auto opt = options->begin(); opt != options->end(); ++opt) {
3036 const char* what)
const {
3037 ctx->conn_.checkError(status, index, what);
3044 boost::shared_ptr<MySqlHostExchange> exchange,
3046 bool single)
const {
3049 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
3050 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
3054 std::vector<MYSQL_BIND> outbind = exchange->createBindForReceive();
3055 status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &outbind[0]);
3056 checkError(ctx, status, stindex,
"unable to bind SELECT clause parameters");
3060 checkError(ctx, status, stindex,
"unable to execute");
3064 status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
3065 checkError(ctx, status, stindex,
"unable to set up for storing all results");
3072 while ((status = mysql_stmt_fetch(ctx->conn_.statements_[stindex])) ==
3075 exchange->processFetchedData(result);
3080 ctx->conn_.text_statements_[stindex] <<
">");
3083 if (single && (result.size() > 1)) {
3085 "database where only one was expected for query " 3086 << ctx->conn_.text_statements_[stindex]);
3094 checkError(ctx, status, stindex,
"unable to fetch results");
3096 }
else if (status == MYSQL_DATA_TRUNCATED) {
3099 <<
" returned truncated data: columns affected are " 3100 << exchange->getErrorColumns());
3108 const uint8_t* identifier_begin,
3109 const size_t identifier_len,
3111 boost::shared_ptr<MySqlHostExchange> exchange)
const {
3114 MYSQL_BIND inbind[3];
3115 memset(inbind, 0,
sizeof(inbind));
3117 uint32_t subnet_buffer =
static_cast<uint32_t
>(subnet_id);
3118 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3119 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_buffer);
3123 std::vector<char> identifier_vec(identifier_begin,
3124 identifier_begin + identifier_len);
3125 unsigned long length = identifier_vec.size();
3126 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3127 inbind[2].buffer = &identifier_vec[0];
3128 inbind[2].buffer_length = length;
3129 inbind[2].length = &length;
3132 char identifier_type_copy =
static_cast<char>(identifier_type);
3133 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3134 inbind[1].buffer =
reinterpret_cast<char*
>(&identifier_type_copy);
3142 if (!collection.empty()) {
3143 result = *collection.begin();
3151 if (ctx->is_readonly_) {
3153 " operate in read only mode");
3166 return (impl_->parameters_);
3176 impl_->checkReadOnly(ctx);
3188 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
3189 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
3192 std::vector<MYSQL_BIND> bind = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
3199 uint64_t host_id = mysql_insert_id(ctx->conn_.mysql_);
3205 cfg_option4, host_id);
3212 cfg_option6, host_id);
3217 if (std::distance(v6resv.first, v6resv.second) > 0) {
3220 impl_->addResv(ctx, resv->second, host_id);
3236 impl_->checkReadOnly(ctx);
3239 MYSQL_BIND inbind[2];
3241 uint32_t subnet = subnet_id;
3242 memset(inbind, 0,
sizeof(inbind));
3243 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3244 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3250 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3251 inbind[1].buffer =
reinterpret_cast<char*
>(&addr4);
3258 std::string addr_str = addr.
toText();
3259 unsigned long addr_len = addr_str.size();
3260 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3261 inbind[1].buffer =
reinterpret_cast<char*
> 3262 (
const_cast<char*
>(addr_str.c_str()));
3263 inbind[1].length = &addr_len;
3264 inbind[1].buffer_length = addr_len;
3272 const uint8_t* identifier_begin,
3273 const size_t identifier_len) {
3279 impl_->checkReadOnly(ctx);
3282 MYSQL_BIND inbind[3];
3285 memset(inbind, 0,
sizeof(inbind));
3286 uint32_t subnet =
static_cast<uint32_t
>(subnet_id);
3287 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3288 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3292 char identifier_type_copy =
static_cast<char>(identifier_type);
3293 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3294 inbind[1].buffer =
reinterpret_cast<char*
>(&identifier_type_copy);
3298 std::vector<char> identifier_vec(identifier_begin,
3299 identifier_begin + identifier_len);
3300 unsigned long length = identifier_vec.size();
3301 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3302 inbind[2].buffer = &identifier_vec[0];
3303 inbind[2].buffer_length = length;
3304 inbind[2].length = &length;
3313 const uint8_t* identifier_begin,
3314 const size_t identifier_len) {
3320 impl_->checkReadOnly(ctx);
3323 MYSQL_BIND inbind[3];
3326 memset(inbind, 0,
sizeof(inbind));
3327 uint32_t subnet =
static_cast<uint32_t
>(subnet_id);
3328 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3329 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3333 char identifier_type_copy =
static_cast<char>(identifier_type);
3334 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3335 inbind[1].buffer =
reinterpret_cast<char*
>(&identifier_type_copy);
3339 std::vector<char> identifier_vec(identifier_begin,
3340 identifier_begin + identifier_len);
3341 unsigned long length = identifier_vec.size();
3342 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3343 inbind[2].buffer = &identifier_vec[0];
3344 inbind[2].buffer_length = length;
3345 inbind[2].length = &length;
3353 const uint8_t* identifier_begin,
3354 const size_t identifier_len)
const {
3360 MYSQL_BIND inbind[2];
3361 memset(inbind, 0,
sizeof(inbind));
3364 char identifier_type_copy =
static_cast<char>(identifier_type);
3365 inbind[1].buffer = &identifier_type_copy;
3366 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3370 std::vector<char> identifier_vec(identifier_begin,
3371 identifier_begin + identifier_len);
3372 unsigned long int length = identifier_vec.size();
3373 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3374 inbind[0].buffer = &identifier_vec[0];
3375 inbind[0].buffer_length = length;
3376 inbind[0].length = &length;
3380 ctx->host_ipv46_exchange_, result,
false);
3392 MYSQL_BIND inbind[1];
3393 memset(inbind, 0,
sizeof(inbind));
3394 uint32_t subnet = subnet_id;
3395 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3396 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3401 ctx->host_ipv4_exchange_, result,
false);
3413 MYSQL_BIND inbind[1];
3414 memset(inbind, 0,
sizeof(inbind));
3415 uint32_t subnet = subnet_id;
3416 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3417 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3422 ctx->host_ipv6_exchange_, result,
false);
3434 MYSQL_BIND inbind[1];
3435 memset(inbind, 0,
sizeof(inbind));
3440 unsigned long length = hostname.length();
3441 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3442 inbind[0].buffer =
reinterpret_cast<char*
>(hostname_);
3443 inbind[0].buffer_length = length;
3444 inbind[0].length = &length;
3448 ctx->host_ipv46_exchange_, result,
false);
3461 MYSQL_BIND inbind[2];
3462 memset(inbind, 0,
sizeof(inbind));
3467 unsigned long length = hostname.length();
3468 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3469 inbind[0].buffer =
reinterpret_cast<char*
>(hostname_);
3470 inbind[0].buffer_length = length;
3471 inbind[0].length = &length;
3474 uint32_t subnet = subnet_id;
3475 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3476 inbind[1].buffer =
reinterpret_cast<char*
>(&subnet);
3481 ctx->host_ipv4_exchange_, result,
false);
3494 MYSQL_BIND inbind[2];
3495 memset(inbind, 0,
sizeof(inbind));
3500 unsigned long length = hostname.length();
3501 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3502 inbind[0].buffer =
reinterpret_cast<char*
>(hostname_);
3503 inbind[0].buffer_length = length;
3504 inbind[0].length = &length;
3507 uint32_t subnet = subnet_id;
3508 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3509 inbind[1].buffer =
reinterpret_cast<char*
>(&subnet);
3514 ctx->host_ipv6_exchange_, result,
false);
3522 uint64_t lower_host_id,
3529 MYSQL_BIND inbind[3];
3530 memset(inbind, 0,
sizeof(inbind));
3533 uint32_t subnet = subnet_id;
3534 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3535 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3539 uint32_t host_id = lower_host_id;
3540 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3541 inbind[1].buffer =
reinterpret_cast<char*
>(&host_id);
3545 uint32_t page_size_data = page_size.
page_size_;
3546 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3547 inbind[2].buffer =
reinterpret_cast<char*
>(&page_size_data);
3552 ctx->host_ipv4_exchange_, result,
false);
3560 uint64_t lower_host_id,
3567 MYSQL_BIND inbind[3];
3568 memset(inbind, 0,
sizeof(inbind));
3571 uint32_t subnet = subnet_id;
3572 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3573 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3577 uint32_t host_id = lower_host_id;
3578 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3579 inbind[1].buffer =
reinterpret_cast<char*
>(&host_id);
3583 uint32_t page_size_data = page_size.
page_size_;
3584 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3585 inbind[2].buffer =
reinterpret_cast<char*
>(&page_size_data);
3590 ctx->host_ipv6_exchange_, result,
false);
3597 uint64_t lower_host_id,
3604 MYSQL_BIND inbind[2];
3605 memset(inbind, 0,
sizeof(inbind));
3608 uint32_t host_id = lower_host_id;
3609 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3610 inbind[0].buffer =
reinterpret_cast<char*
>(&host_id);
3614 uint32_t page_size_data = page_size.
page_size_;
3615 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3616 inbind[1].buffer =
reinterpret_cast<char*
>(&page_size_data);
3621 ctx->host_ipv4_exchange_, result,
false);
3628 uint64_t lower_host_id,
3635 MYSQL_BIND inbind[2];
3636 memset(inbind, 0,
sizeof(inbind));
3639 uint32_t host_id = lower_host_id;
3640 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3641 inbind[0].buffer =
reinterpret_cast<char*
>(&host_id);
3645 uint32_t page_size_data = page_size.
page_size_;
3646 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3647 inbind[1].buffer =
reinterpret_cast<char*
>(&page_size_data);
3652 ctx->host_ipv6_exchange_, result,
false);
3664 MYSQL_BIND inbind[1];
3665 memset(inbind, 0,
sizeof(inbind));
3667 uint32_t addr4 = address.
toUint32();
3668 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3669 inbind[0].buffer =
reinterpret_cast<char*
>(&addr4);
3674 ctx->host_ipv4_exchange_, result,
false);
3682 const uint8_t* identifier_begin,
3683 const size_t identifier_len)
const {
3688 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3690 ctx->host_ipv4_exchange_));
3696 if (!address.
isV4()) {
3698 "wrong address type, address supplied is an IPv6 address");
3706 MYSQL_BIND inbind[2];
3707 uint32_t subnet = subnet_id;
3708 memset(inbind, 0,
sizeof(inbind));
3709 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3710 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3713 uint32_t addr4 = address.
toUint32();
3714 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3715 inbind[1].buffer =
reinterpret_cast<char*
>(&addr4);
3720 ctx->host_ipv4_exchange_, collection,
true);
3724 if (!collection.empty()) {
3725 result = *collection.begin();
3734 if (!address.
isV4()) {
3736 "wrong address type, address supplied is an IPv6 address");
3744 MYSQL_BIND inbind[2];
3745 uint32_t subnet = subnet_id;
3746 memset(inbind, 0,
sizeof(inbind));
3747 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3748 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet);
3751 uint32_t addr4 = address.
toUint32();
3752 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3753 inbind[1].buffer =
reinterpret_cast<char*
>(&addr4);
3758 ctx->host_ipv4_exchange_, collection,
false);
3759 return (collection);
3765 const uint8_t* identifier_begin,
3766 const size_t identifier_len)
const {
3771 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3773 ctx->host_ipv6_exchange_));
3778 const uint8_t prefix_len)
const {
3779 if (!prefix.
isV6()) {
3781 "wrong address type, address supplied is an IPv4 address");
3789 MYSQL_BIND inbind[2];
3790 memset(inbind, 0,
sizeof(inbind));
3792 std::string addr6 = prefix.
toText();
3793 unsigned long addr6_length = addr6.size();
3795 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3796 inbind[0].buffer =
reinterpret_cast<char*
> 3797 (
const_cast<char*
>(addr6.c_str()));
3798 inbind[0].length = &addr6_length;
3799 inbind[0].buffer_length = addr6_length;
3801 uint8_t tmp = prefix_len;
3802 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3803 inbind[1].buffer =
reinterpret_cast<char*
>(&tmp);
3808 ctx->host_ipv6_exchange_, collection,
true);
3812 if (!collection.empty()) {
3813 result = *collection.begin();
3822 if (!address.
isV6()) {
3824 "wrong address type, address supplied is an IPv4 address");
3832 MYSQL_BIND inbind[2];
3833 memset(inbind, 0,
sizeof(inbind));
3835 uint32_t subnet_buffer =
static_cast<uint32_t
>(subnet_id);
3836 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3837 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_buffer);
3840 std::string addr6 = address.
toText();
3841 unsigned long addr6_length = addr6.size();
3843 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3844 inbind[1].buffer =
reinterpret_cast<char*
> 3845 (
const_cast<char*
>(addr6.c_str()));
3846 inbind[1].length = &addr6_length;
3847 inbind[1].buffer_length = addr6_length;
3851 ctx->host_ipv6_exchange_, collection,
true);
3855 if (!collection.empty()) {
3856 result = *collection.begin();
3865 if (!address.
isV6()) {
3867 "wrong address type, address supplied is an IPv4 address");
3875 MYSQL_BIND inbind[2];
3876 memset(inbind, 0,
sizeof(inbind));
3878 uint32_t subnet_buffer =
static_cast<uint32_t
>(subnet_id);
3879 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3880 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_buffer);
3883 std::string addr6 = address.
toText();
3884 unsigned long addr6_length = addr6.size();
3886 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3887 inbind[1].buffer =
reinterpret_cast<char*
> 3888 (
const_cast<char*
>(addr6.c_str()));
3889 inbind[1].length = &addr6_length;
3890 inbind[1].buffer_length = addr6_length;
3894 ctx->host_ipv6_exchange_, collection,
false);
3895 return (collection);
3902 std::string name =
"";
3908 name = ctx->conn_.getParameter(
"name");
3917 return (std::string(
"Host data source that stores host information" 3918 "in MySQL database"));
3921 std::pair<uint32_t, uint32_t>
3923 return(impl_->getVersion());
3933 impl_->checkReadOnly(ctx);
3934 if (ctx->conn_.isTransactionStarted()) {
3935 ctx->conn_.commit();
3946 impl_->checkReadOnly(ctx);
3947 if (ctx->conn_.isTransactionStarted()) {
3948 ctx->conn_.rollback();
3954 impl_->ip_reservations_unique_ = unique;
3960 return (impl_->unusable_);
RAII class creating a critical section.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
const size_t ADDRESS6_TEXT_MAX_LEN
Maximum size of an IPv6 address represented as a text string.
bool my_bool
my_bool type in MySQL 8.x.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the host DB backend manager.
const size_t OPTION_FORMATTED_VALUE_MAX_LEN
Maximum length of option value specified in textual format.
boost::shared_ptr< MySqlHostContextPool > MySqlHostContextPoolPtr
Type of pointers to context pools.
virtual std::string getDescription() const
Returns description of the backend.
Wraps value holding size of the page with host reservations.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Fetch and Release MySQL Results.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
const size_t USER_CONTEXT_MAX_LEN
Maximum length of user context.
bool delStatement(MySqlHostContextPtr &ctx, StatementIndex stindex, MYSQL_BIND *bind)
Executes statements that delete records.
MySqlHostDataSourceImpl(const DatabaseConnection::ParameterMap ¶meters)
Constructor.
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
const size_t CLIENT_CLASSES_MAX_LEN
Maximum length of classes stored in a dhcp4/6_client_classes columns.
virtual void rollback()
Rollback Transactions.
std::vector< MySqlHostContextPtr > pool_
The vector of available contexts.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
uint8_t getPrefixLen() const
Returns prefix length.
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)
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
virtual ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
const size_t BOOT_FILE_NAME_MAX_LEN
Maximum length of the boot file name.
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters)
Get the schema version.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
void commit()
Commits transaction.
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_GET_VERSION
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
static CfgMgr & instance()
returns a single instance of Configuration Manager
const size_t page_size_
Holds page size.
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)
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
boost::shared_ptr< Option > OptionPtr
const uint32_t MYSQL_SCHEMA_VERSION_MAJOR
Universe
defines option universe DHCPv4 or DHCPv6
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
virtual bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
void addStatement(MySqlHostContextPtr &ctx, MySqlHostDataSourceImpl::StatementIndex stindex, std::vector< MYSQL_BIND > &bind)
Executes statements which inserts a row into one of the tables.
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE
const uint32_t MYSQL_SCHEMA_VERSION_MINOR
std::string timer_name_
Timer name used to register database reconnect timer.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
void addResv(MySqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
Inserts IPv6 Reservation into ipv6_reservation table.
bool unusable_
Indicates if there is at least one connection that can no longer be used for normal operations...
IPv6 reservation for a host.
const size_t OPTION_SPACE_MAX_LEN
Maximum length of option space name.
std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
MySqlConnection conn_
MySQL connection.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Exception thrown on failure to open database.
const size_t SERVER_HOSTNAME_MAX_LEN
Maximum length of the server hostname.
const size_t HOSTNAME_MAX_LEN
Maximum length of the hostname stored in DNS.
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
void checkError(MySqlHostContextPtr &ctx, const int status, const StatementIndex index, const char *what) const
Check Error and Throw Exception.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
Multiple lease records found where one expected.
DatabaseConnection::ParameterMap parameters_
The parameters.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
std::string formatted_value_
Option value in textual (CSV) format.
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
ConstHostPtr getHost(MySqlHostContextPtr &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< MySqlHostExchange > exchange) const
Retrieves a host by subnet and client's unique identifier.
const my_bool MLM_FALSE
MySQL false value.
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
boost::shared_ptr< MySqlHostIPv6Exchange > host_ipv46_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts, DHCPv4 and DHCPv6 options, and IPv6 reservations using a single query.
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
boost::shared_ptr< MySqlHostWithOptionsExchange > host_ipv4_exchange_
The exchange objects are used for transfer of data to/from the database.
~MySqlHostDataSourceImpl()
Destructor.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
bool persistent_
Persistence flag.
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.
A generic exception that is thrown when an unexpected error condition occurs.
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED
virtual void commit()
Commit Transactions.
virtual ~MySqlHostDataSource()
Virtual destructor.
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
virtual std::string getName() const
Returns backend name.
void checkReadOnly(MySqlHostContextPtr &ctx) const
Throws exception if database is read only.
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
void addOptions(MySqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
Inserts multiple options into the database.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
Represents a device with IPv4 and/or IPv6 reservations.
Type
Type of the reservation.
bool isV6() const
Convenience function to check for an IPv6 address.
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.
bool isV4() const
Convenience function to check for an IPv4 address.
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_READONLY
OptionPtr option_
Option instance.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
StatementIndex
Statement Tags.
const isc::log::MessageID DHCPSRV_MYSQL_NO_TLS
Defines the logger used by the top-level component of kea-lfc.
Type getType() const
Returns reservation type.
MySqlHostContextPtr ctx_
The context.
boost::shared_ptr< MySqlOptionExchange > host_option_exchange_
Pointer to an object representing an exchange which can be used to insert DHCPv4 or DHCPv6 option int...
bool ip_reservations_unique_
Holds the setting whether the IP reservations must be unique or may be non-unique.
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_FAILED
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.
MySqlHostContextPoolPtr pool_
The pool of contexts.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
std::string toText() const
Convert the address to a string.
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
Implementation of the MySqlHostDataSource.
MySqlHostContextAlloc(MySqlHostDataSourceImpl &mgr)
Constructor.
void addOption(MySqlHostContextPtr &ctx, const MySqlHostDataSourceImpl::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.
const int MLM_MYSQL_FETCH_FAILURE
MySQL fetch failure code.
A wrapper interface for the ASIO library.
#define DHCP6_OPTION_SPACE
RAII object representing MySQL transaction.
#define DHCP4_OPTION_SPACE
global std option spaces
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
bool is_readonly_
Indicates if the database is opened in read only mode.
boost::shared_ptr< MySqlHostContext > MySqlHostContextPtr
Type of pointers to contexts.
~MySqlHostContextAlloc()
Destructor.
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Attempt to modify data in read-only database.
boost::shared_ptr< MySqlHostIPv6Exchange > host_ipv6_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts, DHCPv6 options and IPv6 reservations.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
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...
boost::shared_ptr< MySqlIPv6ReservationExchange > host_ipv6_reservation_exchange_
Pointer to an object representing an exchange which can be used to insert new IPv6 reservation...
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
const isc::log::MessageID DHCPSRV_MYSQL_TLS_CIPHER
IdentifierType
Type of the host identifier.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
const size_t TEXT_AUTH_KEY_LEN
Maximum length of authentication keys (coded in hexadecimal).
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
The IOAddress class represents an IP addresses (version agnostic)
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static const StatementIndex WRITE_STMTS_BEGIN
Index of first statement performing write to the database.
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
uint64_t HostID
HostID (used only when storing in MySQL or PostgreSQL backends)
A template representing an optional value.
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.
std::mutex mutex_
The mutex to protect pool access.
void getHostCollection(MySqlHostContextPtr &ctx, StatementIndex stindex, MYSQL_BIND *bind, boost::shared_ptr< MySqlHostExchange > exchange, ConstHostCollection &result, bool single) const
Creates collection of Host objects with associated information such as IPv6 reservations and/or DHCP ...
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)
Attempts to delete hosts by (subnet-id, address)
Exception thrown on failure to execute a database function.
const my_bool MLM_TRUE
MySQL true value.
MySqlHostDataSource(const db::DatabaseConnection::ParameterMap ¶meters)
Constructor.
MySqlHostContextPtr createContext() const
Create a new context.
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
Database duplicate entry error.
virtual void add(const HostPtr &host)
Adds a new host to the collection.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Common MySQL Connector Pool.
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.