Kea 2.0.1
mysql_host_data_source.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
11#include <dhcp/libdhcp++.h>
12#include <dhcp/option.h>
14#include <dhcp/option_space.h>
16#include <dhcpsrv/cfg_option.h>
17#include <dhcpsrv/cfgmgr.h>
18#include <dhcpsrv/dhcpsrv_log.h>
19#include <dhcpsrv/host_mgr.h>
21#include <dhcpsrv/timer_mgr.h>
22#include <util/buffer.h>
24#include <util/optional.h>
25
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>
31
32#include <mysql.h>
33#include <mysqld_error.h>
34#include <stdint.h>
35
36#include <mutex>
37#include <string>
38
39using namespace isc;
40using namespace isc::asiolink;
41using namespace isc::db;
42using namespace isc::dhcp;
43using namespace isc::util;
44using namespace isc::data;
45using namespace std;
46
47namespace {
48
53const uint8_t MAX_IDENTIFIER_TYPE = static_cast<uint8_t>(Host::LAST_IDENTIFIER_TYPE);
54
83class MySqlHostExchange {
84private:
85
87 static const size_t HOST_COLUMNS = 14;
88
89public:
90
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),
110 auth_key_length_(0),
111 dhcp4_subnet_id_null_(MLM_FALSE),
112 dhcp6_subnet_id_null_(MLM_FALSE),
113 ipv4_address_null_(MLM_FALSE), hostname_null_(MLM_FALSE),
114 dhcp4_client_classes_null_(MLM_FALSE),
115 dhcp6_client_classes_null_(MLM_FALSE),
116 user_context_null_(MLM_FALSE),
117 dhcp4_next_server_null_(MLM_FALSE),
118 dhcp4_server_hostname_null_(MLM_FALSE),
119 dhcp4_boot_file_name_null_(MLM_FALSE),
120 auth_key_null_(MLM_FALSE) {
121
122 // Fill arrays with 0 so as they don't include any garbage.
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_));
131
132 // Set the column names for use by this class. This only comprises
133 // names used by the MySqlHostExchange class. Derived classes will
134 // need to set names for the columns they use.
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";
149
150 BOOST_STATIC_ASSERT(13 < HOST_COLUMNS);
151 };
152
154 virtual ~MySqlHostExchange() {
155 }
156
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));
175 }
176
180 uint64_t getHostId() const {
181 return (host_id_);
182 };
183
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) {
197 error[i] = MLM_FALSE;
198 bind[i].error = reinterpret_cast<my_bool*>(&error[i]);
199 }
200 };
201
215 static std::string getColumnsInError(std::vector<my_bools>& error,
216 const std::vector<std::string>& names) {
217 std::string result = "";
218
219 // Accumulate list of column names
220 for (size_t i = 0; i < names.size(); ++i) {
221 if (error[i] == MLM_TRUE) {
222 if (!result.empty()) {
223 result += ", ";
224 }
225 result += names[i];
226 }
227 }
228
229 if (result.empty()) {
230 result = "(None)";
231 }
232
233 return (result);
234 };
235
248 std::vector<MYSQL_BIND> createBindForSend(const HostPtr& host, const bool unique_ip) {
249 // Store host object to ensure it remains valid.
250 host_ = host;
251
252 // Initialize prior to constructing the array of MYSQL_BIND structures.
253 // It sets all fields, including is_null, to zero, so we need to set
254 // is_null only if it should be true. This gives up minor performance
255 // benefit while being safe approach.
256 memset(&bind_[0], 0, sizeof(MYSQL_BIND) * bind_.size());
257
258 // Set up the structures for the various components of the host structure.
259
260 try {
261 // host_id : INT UNSIGNED NOT NULL
262 // The host_id is auto_incremented by MySQL database,
263 // so we need to pass the NULL value
264 host_id_ = 0;
265 bind_[0].buffer_type = MYSQL_TYPE_LONG;
266 bind_[0].buffer = reinterpret_cast<char*>(&host_id_);
267 bind_[0].is_unsigned = MLM_TRUE;
268
269 // dhcp_identifier : VARBINARY(128) NOT NULL
270 dhcp_identifier_length_ = host->getIdentifier().size();
271 memcpy(static_cast<void*>(dhcp_identifier_buffer_),
272 &(host->getIdentifier())[0],
273 host->getIdentifier().size());
274
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_;
279
280 // dhcp_identifier_type : TINYINT NOT NULL
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_);
284 bind_[2].is_unsigned = MLM_TRUE;
285
286 // dhcp4_subnet_id : INT UNSIGNED NULL
287 // Can't take an address of intermediate object, so let's store it
288 // in dhcp4_subnet_id_
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_);
293 bind_[3].is_unsigned = MLM_TRUE;
294 bind_[3].is_null = &dhcp4_subnet_id_null_;
295
296 // dhcp6_subnet_id : INT UNSIGNED NULL
297 // Can't take an address of intermediate object, so let's store it
298 // in dhcp6_subnet_id_
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_);
303 bind_[4].is_unsigned = MLM_TRUE;
304 bind_[4].is_null = &dhcp6_subnet_id_null_;
305
306 // ipv4_address : INT UNSIGNED NULL
307 // The address in the Host structure is an IOAddress object. Convert
308 // this to an integer for storage.
309 ipv4_address_ = host->getIPv4Reservation().toUint32();
310 ipv4_address_null_ = ipv4_address_ == 0 ? MLM_TRUE : MLM_FALSE;
311 bind_[5].buffer_type = MYSQL_TYPE_LONG;
312 bind_[5].buffer = reinterpret_cast<char*>(&ipv4_address_);
313 bind_[5].is_unsigned = MLM_TRUE;
314 bind_[5].is_null = &ipv4_address_null_;
315
316 // hostname : VARCHAR(255) NULL
317 strncpy(hostname_, host->getHostname().c_str(), HOSTNAME_MAX_LEN - 1);
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_;
322
323 // dhcp4_client_classes : VARCHAR(255) NULL
324 bind_[7].buffer_type = MYSQL_TYPE_STRING;
325 // Override default separator to not include space after comma.
326 string classes4_txt = host->getClientClasses4().toText(",");
327 strncpy(dhcp4_client_classes_, classes4_txt.c_str(), CLIENT_CLASSES_MAX_LEN - 1);
328 bind_[7].buffer = dhcp4_client_classes_;
329 bind_[7].buffer_length = classes4_txt.length();
330
331 // dhcp6_client_classes : VARCHAR(255) NULL
332 bind_[8].buffer_type = MYSQL_TYPE_STRING;
333 // Override default separator to not include space after comma.
334 string classes6_txt = host->getClientClasses6().toText(",");
335 strncpy(dhcp6_client_classes_, classes6_txt.c_str(), CLIENT_CLASSES_MAX_LEN - 1);
336 bind_[8].buffer = dhcp6_client_classes_;
337 bind_[8].buffer_length = classes6_txt.length();
338
339 // user_context : TEXT NULL
340 ConstElementPtr ctx = host->getContext();
341 if (ctx) {
342 bind_[9].buffer_type = MYSQL_TYPE_STRING;
343 string ctx_txt = ctx->str();
344 strncpy(user_context_, ctx_txt.c_str(), USER_CONTEXT_MAX_LEN - 1);
345 bind_[9].buffer = user_context_;
346 bind_[9].buffer_length = ctx_txt.length();
347 } else {
348 bind_[9].buffer_type = MYSQL_TYPE_NULL;
349 }
350
351 // ipv4_address : INT UNSIGNED NULL
352 // The address in the Host structure is an IOAddress object. Convert
353 // this to an integer for storage.
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_);
357 bind_[10].is_unsigned = MLM_TRUE;
358 // bind_[10].is_null = &MLM_FALSE; // commented out for performance
359 // reasons, see memset() above
360
361 // dhcp4_server_hostname
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();
368
369 // dhcp4_boot_file_name
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();
376
377 // auth key
378 bind_[13].buffer_type = MYSQL_TYPE_STRING;
379 std::string auth_key = host->getKey().toText();
380 std::strncpy(auth_key_, auth_key.c_str(), TEXT_AUTH_KEY_LEN - 1);
381 auth_key_null_ = auth_key.empty() ? MLM_TRUE : MLM_FALSE;
382 bind_[13].buffer = auth_key_;
383 bind_[13].buffer_length = auth_key.length();
384
385 } catch (const std::exception& ex) {
387 "Could not create bind array from Host: "
388 << host->getHostname() << ", reason: " << ex.what());
389 }
390
391 // Add the data to the vector.
392 std::vector<MYSQL_BIND> vec(bind_.begin(), bind_.begin() + HOST_COLUMNS);
393
394 // When checking whether the IP is unique we need to bind the IPv4 address
395 // at the end of the query as it has additional binding for the IPv4
396 // address.
397 if (unique_ip) {
398 vec.push_back(bind_[5]); // ipv4_address
399 vec.push_back(bind_[3]); // subnet_id
400 }
401 return (vec);
402 };
403
411 virtual std::vector<MYSQL_BIND> createBindForReceive() {
412 // Initialize MYSQL_BIND array.
413 // It sets all fields, including is_null, to zero, so we need to set
414 // is_null only if it should be true. This gives up minor performance
415 // benefit while being safe approach. For improved readability, the
416 // code that explicitly sets is_null is there, but is commented out.
417 // This also takes care of setting bind_[X].is_null to MLM_FALSE.
418 memset(&bind_[0], 0, sizeof(MYSQL_BIND) * bind_.size());
419
420 // host_id : INT UNSIGNED NOT NULL
421 bind_[0].buffer_type = MYSQL_TYPE_LONG;
422 bind_[0].buffer = reinterpret_cast<char*>(&host_id_);
423 bind_[0].is_unsigned = MLM_TRUE;
424
425 // dhcp_identifier : VARBINARY(128) NOT NULL
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_;
431
432 // dhcp_identifier_type : TINYINT NOT NULL
433 bind_[2].buffer_type = MYSQL_TYPE_TINY;
434 bind_[2].buffer = reinterpret_cast<char*>(&dhcp_identifier_type_);
435 bind_[2].is_unsigned = MLM_TRUE;
436
437 // dhcp4_subnet_id : INT UNSIGNED NULL
438 dhcp4_subnet_id_null_ = MLM_FALSE;
439 bind_[3].buffer_type = MYSQL_TYPE_LONG;
440 bind_[3].buffer = reinterpret_cast<char*>(&dhcp4_subnet_id_);
441 bind_[3].is_unsigned = MLM_TRUE;
442 bind_[3].is_null = &dhcp4_subnet_id_null_;
443
444 // dhcp6_subnet_id : INT UNSIGNED NULL
445 dhcp6_subnet_id_null_ = MLM_FALSE;
446 bind_[4].buffer_type = MYSQL_TYPE_LONG;
447 bind_[4].buffer = reinterpret_cast<char*>(&dhcp6_subnet_id_);
448 bind_[4].is_unsigned = MLM_TRUE;
449 bind_[4].is_null = &dhcp6_subnet_id_null_;
450
451 // ipv4_address : INT UNSIGNED NULL
452 ipv4_address_null_ = MLM_FALSE;
453 bind_[5].buffer_type = MYSQL_TYPE_LONG;
454 bind_[5].buffer = reinterpret_cast<char*>(&ipv4_address_);
455 bind_[5].is_unsigned = MLM_TRUE;
456 bind_[5].is_null = &ipv4_address_null_;
457
458 // hostname : VARCHAR(255) NULL
459 hostname_null_ = MLM_FALSE;
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_;
466
467 // dhcp4_client_classes : VARCHAR(255) NULL
468 dhcp4_client_classes_null_ = MLM_FALSE;
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_;
475
476 // dhcp6_client_classes : VARCHAR(255) NULL
477 dhcp6_client_classes_null_ = MLM_FALSE;
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_;
484
485 // user_context : TEXT NULL
486 user_context_null_ = MLM_FALSE;
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_;
493
494 // dhcp4_next_server
495 dhcp4_next_server_null_ = MLM_FALSE;
496 bind_[10].buffer_type = MYSQL_TYPE_LONG;
497 bind_[10].buffer = reinterpret_cast<char*>(&dhcp4_next_server_);
498 bind_[10].is_unsigned = MLM_TRUE;
499 bind_[10].is_null = &dhcp4_next_server_null_;
500
501 // dhcp4_server_hostname
502 dhcp4_server_hostname_null_ = MLM_FALSE;
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_;
509
510 // dhcp4_boot_file_name
511 dhcp4_boot_file_name_null_ = MLM_FALSE;
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_;
518
519 // auth_key_
520 auth_key_null_ = MLM_FALSE;
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_;
527
528 // Add the error flags
529 setErrorIndicators(bind_, error_);
530
531 // Add the data to the vector. Note the end element is one after the
532 // end of the array.
533 return (bind_);
534 };
535
544 HostPtr retrieveHost() {
545 // Check if the identifier stored in the database is correct.
546 if (dhcp_identifier_type_ > MAX_IDENTIFIER_TYPE) {
547 isc_throw(BadValue, "invalid dhcp identifier type returned: "
548 << static_cast<int>(dhcp_identifier_type_));
549 }
550 // Set the dhcp identifier type in a variable of the appropriate
551 // data type.
553 static_cast<Host::IdentifierType>(dhcp_identifier_type_);
554
555 // Set DHCPv4 subnet ID to the value returned. If NULL returned,
556 // set to 0.
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_);
560 }
561
562 // Set DHCPv6 subnet ID to the value returned. If NULL returned,
563 // set to 0.
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_);
567 }
568
569 // Set IPv4 address reservation if it was given, if not, set IPv4 zero
570 // address
571 asiolink::IOAddress ipv4_reservation = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
572 if (ipv4_address_null_ == MLM_FALSE) {
573 ipv4_reservation = asiolink::IOAddress(ipv4_address_);
574 }
575
576 // Set hostname if non NULL value returned. Otherwise, leave an
577 // empty string.
578 std::string hostname;
579 if (hostname_null_ == MLM_FALSE) {
580 hostname = std::string(hostname_, hostname_length_);
581 }
582
583 // Set DHCPv4 client classes if non NULL value returned.
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_);
588 }
589
590 // Set DHCPv6 client classes if non NULL value returned.
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_);
595 }
596
597 // Convert user_context to string as well.
598 std::string user_context;
599 if (user_context_null_ == MLM_FALSE) {
600 user_context_[user_context_length_] = '\0';
601 user_context.assign(user_context_);
602 }
603
604 // Set next server value (siaddr) if non NULL value returned.
605 asiolink::IOAddress next_server = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
606 if (dhcp4_next_server_null_ == MLM_FALSE) {
607 next_server = asiolink::IOAddress(dhcp4_next_server_);
608 }
609
610 // Set server hostname (sname) if non NULL value returned.
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_);
615 }
616
617 // Set boot file name (file) if non NULL value returned.
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_);
622 }
623
624 // Set the auth key if a non empty array is retrieved
625 std::string auth_key;
626 if (auth_key_null_ == MLM_FALSE) {
627 auth_key = std::string(auth_key_, auth_key_length_);
628 }
629
630 // Create and return Host object from the data gathered.
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_);
637
638 // Set the user context if there is one.
639 if (!user_context.empty()) {
640 try {
641 ConstElementPtr ctx = Element::fromJSON(user_context);
642 if (!ctx || (ctx->getType() != Element::map)) {
643 isc_throw(BadValue, "user context '" << user_context
644 << "' is not a JSON map");
645 }
646 h->setContext(ctx);
647 } catch (const isc::data::JSONError& ex) {
648 isc_throw(BadValue, "user context '" << user_context
649 << "' is invalid JSON: " << ex.what());
650 }
651 }
652
653 return (h);
654 };
655
669 virtual void processFetchedData(ConstHostCollection& hosts) {
670 HostPtr host;
671 // Add new host only if there are no hosts yet or the host id of the
672 // most recently added host is different than the host id of the
673 // currently processed host.
674 if (hosts.empty() || (hosts.back()->getHostId() != getHostId())) {
675 // Create Host object from the fetched data and append it to the
676 // collection.
677 host = retrieveHost();
678 hosts.push_back(host);
679 }
680 }
681
692 std::string getErrorColumns() {
693 return (getColumnsInError(error_, columns_));
694 };
695
696protected:
697
699 size_t columns_num_;
700
702 std::vector<MYSQL_BIND> bind_;
703
705 std::vector<std::string> columns_;
706
708 std::vector<my_bools> error_;
709
712 HostPtr host_;
713
714private:
715
717 uint64_t host_id_;
718
721 uint8_t dhcp_identifier_buffer_[DUID::MAX_DUID_LEN];
722
724 unsigned long dhcp_identifier_length_;
725
728 uint8_t dhcp_identifier_type_;
729
731 uint32_t dhcp4_subnet_id_;
732
734 uint32_t dhcp6_subnet_id_;
735
737 uint32_t ipv4_address_;
738
740 char hostname_[HOSTNAME_MAX_LEN];
741
743 unsigned long hostname_length_;
744
746 char dhcp4_client_classes_[CLIENT_CLASSES_MAX_LEN];
747
750 unsigned long dhcp4_client_classes_length_;
751
753 char dhcp6_client_classes_[CLIENT_CLASSES_MAX_LEN];
754
757 unsigned long dhcp6_client_classes_length_;
758
760 char user_context_[USER_CONTEXT_MAX_LEN];
761
763 unsigned long user_context_length_;
764
766 uint32_t dhcp4_next_server_;
767
769 char dhcp4_server_hostname_[SERVER_HOSTNAME_MAX_LEN];
770
772 unsigned long dhcp4_server_hostname_length_;
773
775 char dhcp4_boot_file_name_[BOOT_FILE_NAME_MAX_LEN];
776
778 unsigned long dhcp4_boot_file_name_length_;
779
781 char auth_key_[TEXT_AUTH_KEY_LEN];
782
784 unsigned long auth_key_length_;
785
788
789
790 my_bool dhcp4_subnet_id_null_;
791
793 my_bool dhcp6_subnet_id_null_;
794
796 my_bool ipv4_address_null_;
797
799 my_bool hostname_null_;
800
803 my_bool dhcp4_client_classes_null_;
804
807 my_bool dhcp6_client_classes_null_;
808
810 my_bool user_context_null_;
811
813 my_bool dhcp4_next_server_null_;
814
816 my_bool dhcp4_server_hostname_null_;
817
819 my_bool dhcp4_boot_file_name_null_;
820
822 my_bool auth_key_null_;
823
825};
826
836class MySqlHostWithOptionsExchange : public MySqlHostExchange {
837private:
838
840 static const size_t OPTION_COLUMNS = 7;
841
856 class OptionProcessor {
857 public:
858
865 OptionProcessor(const Option::Universe& universe,
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),
870 option_id_null_(MLM_FALSE), code_null_(MLM_FALSE),
871 value_null_(MLM_FALSE), formatted_value_null_(MLM_FALSE),
872 space_null_(MLM_FALSE), user_context_null_(MLM_FALSE),
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) {
880
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_));
885 }
886
888 uint64_t getOptionId() const {
889 if (option_id_null_ == MLM_FALSE) {
890 return (option_id_);
891 }
892 return (0);
893 }
894
907 void retrieveOption(const CfgOptionPtr& cfg) {
908 // option_id may be NULL if dhcp4_options or dhcp6_options table
909 // doesn't contain any options for the particular host. Also, the
910 // current option id must be greater than id if the most recent
911 // option because options are ordered by option id. Otherwise
912 // we assume that this is already processed option.
913 if ((option_id_null_ == MLM_TRUE) ||
914 (most_recent_option_id_ >= option_id_)) {
915 return;
916 }
917
918 // Remember current option id as the most recent processed one. We
919 // will be comparing it with option ids in subsequent rows.
920 most_recent_option_id_ = option_id_;
921
922 // Convert it to string object for easier comparison.
923 std::string space;
924 if (space_null_ == MLM_FALSE) {
925 // Typically, the string values returned by the database are not
926 // NULL terminated.
927 space_[space_length_] = '\0';
928 space.assign(space_);
929 }
930
931 // If empty or null space provided, use a default top level space.
932 if (space.empty()) {
933 space = (universe_ == Option::V4 ?
935 }
936
937 // Convert formatted_value to string as well.
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_);
942 }
943
944 // Convert user_context to string as well.
945 std::string user_context;
946 if (user_context_null_ == MLM_FALSE) {
947 user_context_[user_context_length_] = '\0';
948 user_context.assign(user_context_);
949 }
950
951 // Options are held in a binary or textual format in the database.
952 // This is similar to having an option specified in a server
953 // configuration file. Such option is converted to appropriate C++
954 // class, using option definition. Thus, we need to find the
955 // option definition for this option code and option space.
956
957 // Check if this is a standard option.
958 OptionDefinitionPtr def = LibDHCP::getOptionDef(space, code_);
959
960 // Otherwise, we may check if this an option encapsulated within the
961 // vendor space.
962 if (!def && (space != DHCP4_OPTION_SPACE) &&
963 (space != DHCP6_OPTION_SPACE)) {
964 uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(space);
965 if (vendor_id > 0) {
966 def = LibDHCP::getVendorOptionDef(universe_, vendor_id, code_);
967 }
968 }
969
970 // In all other cases, we use runtime option definitions, which
971 // should be also registered within the libdhcp++.
972 if (!def) {
973 def = LibDHCP::getRuntimeOptionDef(space, code_);
974 }
975
976 OptionPtr option;
977
978 if (!def) {
979 // If no definition found, we use generic option type.
980 OptionBuffer buf(value_, value_ + value_length_);
981 option.reset(new Option(universe_, code_, buf.begin(),
982 buf.end()));
983 } else {
984 // The option value may be specified in textual or binary format
985 // in the database. If formatted_value is empty, the binary
986 // format is used. Depending on the format we use a different
987 // variant of the optionFactory function.
988 if (formatted_value.empty()) {
989 OptionBuffer buf(value_, value_ + value_length_);
990 option = def->optionFactory(universe_, code_, buf.begin(),
991 buf.end());
992 } else {
993 // Spit the value specified in comma separated values
994 // format.
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);
998 }
999 }
1000
1001 OptionDescriptor desc(option, persistent_, formatted_value);
1002
1003 // Set the user context if there is one into the option descriptor.
1004 if (!user_context.empty()) {
1005 try {
1006 ConstElementPtr ctx = Element::fromJSON(user_context);
1007 if (!ctx || (ctx->getType() != Element::map)) {
1008 isc_throw(BadValue, "user context '" << user_context
1009 << "' is no a JSON map");
1010 }
1011 desc.setContext(ctx);
1012 } catch (const isc::data::JSONError& ex) {
1013 isc_throw(BadValue, "user context '" << user_context
1014 << "' is invalid JSON: " << ex.what());
1015 }
1016 }
1017
1018 cfg->add(desc, space);
1019 }
1020
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";
1033 }
1034
1041 void setBindFields(std::vector<MYSQL_BIND>& bind) {
1042 // This method is called just before making a new query, so we
1043 // reset the most_recent_option_id_ and other exchange members to
1044 // start over with options processing.
1045 most_recent_option_id_ = 0;
1046
1047 option_id_ = 0;
1048 code_ = 0;
1049 persistent_ = false;
1050 option_id_null_ = MLM_FALSE;
1051 code_null_ = MLM_FALSE;
1052 value_null_ = MLM_FALSE;
1053 formatted_value_null_ = MLM_FALSE;
1054 space_null_ = MLM_FALSE;
1055 user_context_null_ = MLM_FALSE;
1056
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_));
1061
1062 // option_id : INT UNSIGNED NOT NULL AUTO_INCREMENT,
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_;
1067
1068 // code : TINYINT OR SHORT UNSIGNED NOT 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_;
1073
1074 // value : BLOB 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_;
1081
1082 // formatted_value : TEXT 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_;
1089
1090 // space : VARCHAR(128) 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_;
1097
1098 // persistent : TINYINT(1) NOT NULL DEFAULT 0
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;
1102
1103 // user_context : TEXT NULL
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_;
1110 }
1111
1112 private:
1113
1115 Option::Universe universe_;
1116
1118 size_t start_column_;
1119
1121 uint32_t option_id_;
1122
1124 uint16_t code_;
1125
1127 uint8_t value_[OPTION_VALUE_MAX_LEN];
1128
1130 unsigned long value_length_;
1131
1133 char formatted_value_[OPTION_FORMATTED_VALUE_MAX_LEN];
1134
1136 unsigned long formatted_value_length_;
1137
1139 char space_[OPTION_SPACE_MAX_LEN];
1140
1142 unsigned long space_length_;
1143
1146 bool persistent_;
1147
1149 char user_context_[USER_CONTEXT_MAX_LEN];
1150
1152 unsigned long user_context_length_;
1153
1156
1157
1158 my_bool option_id_null_;
1159
1161 my_bool code_null_;
1162
1164 my_bool value_null_;
1165
1168 my_bool formatted_value_null_;
1169
1171 my_bool space_null_;
1172
1174 my_bool user_context_null_;
1176
1178
1179
1180 size_t option_id_index_;
1181
1183 size_t code_index_;
1184
1186 size_t value_index_;
1187
1189 size_t formatted_value_index_;
1190
1192 size_t space_index_;
1193
1195 size_t persistent_index_;
1197
1199 size_t user_context_index_;
1200
1202 uint32_t most_recent_option_id_;
1203 };
1204
1206 typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
1207
1208public:
1209
1216 enum FetchedOptions {
1217 DHCP4_ONLY,
1218 DHCP6_ONLY,
1219 DHCP4_AND_DHCP6
1220 };
1221
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_() {
1235
1236 // Create option processor for DHCPv4 options, if required.
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_);
1242 }
1243
1244 // Create option processor for DHCPv6 options, if required.
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_);
1250 }
1251 }
1252
1261 virtual void processFetchedData(ConstHostCollection& hosts) {
1262 // Holds pointer to the previously parsed host.
1263 HostPtr most_recent_host;
1264 if (!hosts.empty()) {
1265 // Const cast is not very elegant way to deal with it, but
1266 // there is a good reason to use it here. This method is called
1267 // to build a collection of const hosts to be returned to the
1268 // caller. If we wanted to use non-const collection we'd need
1269 // to copy the whole collection before returning it, which has
1270 // performance implications. Alternatively, we could store the
1271 // most recently added host in a class member but this would
1272 // make the code less readable.
1273 most_recent_host = boost::const_pointer_cast<Host>(hosts.back());
1274 }
1275
1276 // If no host has been parsed yet or we're at the row holding next
1277 // host, we create a new host object and put it at the end of the
1278 // list.
1279 if (!most_recent_host || (most_recent_host->getHostId() < getHostId())) {
1280 HostPtr host = retrieveHost();
1281 hosts.push_back(host);
1282 most_recent_host = host;
1283 }
1284
1285 // Parse DHCPv4 options if required to do so.
1286 if (opt_proc4_) {
1287 CfgOptionPtr cfg = most_recent_host->getCfgOption4();
1288 opt_proc4_->retrieveOption(cfg);
1289 }
1290
1291 // Parse DHCPv6 options if required to do so.
1292 if (opt_proc6_) {
1293 CfgOptionPtr cfg = most_recent_host->getCfgOption6();
1294 opt_proc6_->retrieveOption(cfg);
1295 }
1296 }
1297
1301 virtual std::vector<MYSQL_BIND> createBindForReceive() {
1302 // The following call sets bind_ values between 0 and 8.
1303 static_cast<void>(MySqlHostExchange::createBindForReceive());
1304
1305 // Bind variables for DHCPv4 options.
1306 if (opt_proc4_) {
1307 opt_proc4_->setBindFields(bind_);
1308 }
1309
1310 // Bind variables for DHCPv6 options.
1311 if (opt_proc6_) {
1312 opt_proc6_->setBindFields(bind_);
1313 }
1314
1315 // Add the error flags
1316 setErrorIndicators(bind_, error_);
1317
1318 return (bind_);
1319 };
1320
1321private:
1322
1334 static size_t getRequiredColumnsNum(const FetchedOptions& fetched_options) {
1335 return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
1336 OPTION_COLUMNS);
1337 }
1338
1342 OptionProcessorPtr opt_proc4_;
1343
1347 OptionProcessorPtr opt_proc6_;
1348};
1349
1362class MySqlHostIPv6Exchange : public MySqlHostWithOptionsExchange {
1363private:
1364
1366 static const size_t RESERVATION_COLUMNS = 5;
1367
1368public:
1369
1374 MySqlHostIPv6Exchange(const FetchedOptions& fetched_options)
1375 : MySqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
1376 reservation_id_(0),
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) {
1385
1386 memset(ipv6_address_buffer_, 0, sizeof(ipv6_address_buffer_));
1387
1388 // Provide names of additional columns returned by the queries.
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";
1394 }
1395
1399 uint32_t getReservationId() const {
1400 if (reserv_type_null_ == MLM_FALSE) {
1401 return (reservation_id_);
1402 }
1403 return (0);
1404 };
1405
1412 IPv6Resrv retrieveReservation() {
1413 // Set the IPv6 Reservation type (0 = IA_NA, 2 = IA_PD)
1414 IPv6Resrv::Type type = IPv6Resrv::TYPE_NA;
1415
1416 switch (reserv_type_) {
1417 case 0:
1418 type = IPv6Resrv::TYPE_NA;
1419 break;
1420
1421 case 2:
1422 type = IPv6Resrv::TYPE_PD;
1423 break;
1424
1425 default:
1427 "invalid IPv6 reservation type returned: "
1428 << static_cast<int>(reserv_type_)
1429 << ". Only 0 or 2 are allowed.");
1430 }
1431
1432 ipv6_address_buffer_[ipv6_address_buffer_len_] = '\0';
1433 std::string address = ipv6_address_buffer_;
1434 IPv6Resrv r(type, IOAddress(address), prefix_len_);
1435 return (r);
1436 };
1437
1457 virtual void processFetchedData(ConstHostCollection& hosts) {
1458
1459 // Call parent class to fetch host information and options.
1460 MySqlHostWithOptionsExchange::processFetchedData(hosts);
1461
1462 if (getReservationId() == 0) {
1463 return;
1464 }
1465
1466 if (hosts.empty()) {
1467 isc_throw(Unexpected, "no host information while retrieving"
1468 " IPv6 reservation");
1469 }
1470 HostPtr host = boost::const_pointer_cast<Host>(hosts.back());
1471
1472 // If we're dealing with a new reservation, let's add it to the
1473 // host.
1474 if (getReservationId() > most_recent_reservation_id_) {
1475 most_recent_reservation_id_ = getReservationId();
1476
1477 if (most_recent_reservation_id_ > 0) {
1478 host->addReservation(retrieveReservation());
1479 }
1480 }
1481 }
1482
1491 virtual std::vector<MYSQL_BIND> createBindForReceive() {
1492 // Reset most recent reservation id value because we're now making
1493 // a new SELECT query.
1494 most_recent_reservation_id_ = 0;
1495
1496 // Bind values supported by parent classes.
1497 static_cast<void>(MySqlHostWithOptionsExchange::createBindForReceive());
1498
1499 // reservation_id : INT UNSIGNED NOT NULL AUTO_INCREMENT
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;
1503
1504 // IPv6 address/prefix VARCHAR(39)
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_;
1510
1511 // prefix_len : TINYINT
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;
1515
1516 // (reservation) type : TINYINT
1517 reserv_type_null_ = MLM_FALSE;
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_;
1522
1523 // dhcp6_iaid INT UNSIGNED
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;
1527
1528 // Add the error flags
1529 setErrorIndicators(bind_, error_);
1530
1531 return (bind_);
1532 };
1533
1534private:
1535
1537 uint32_t reservation_id_;
1538
1540 uint8_t reserv_type_;
1541
1546 my_bool reserv_type_null_;
1547
1549 char ipv6_address_buffer_[ADDRESS6_TEXT_MAX_LEN + 1];
1550
1552 unsigned long ipv6_address_buffer_len_;
1553
1555 uint8_t prefix_len_;
1556
1558 uint32_t iaid_;
1559
1561
1562
1563 size_t reservation_id_index_;
1564
1566 size_t address_index_;
1567
1569 size_t prefix_len_index_;
1570
1572 size_t type_index_;
1573
1575 size_t iaid_index_;
1576
1578
1580 uint32_t most_recent_reservation_id_;
1581};
1582
1593class MySqlIPv6ReservationExchange {
1594private:
1595
1597 static const size_t RESRV_COLUMNS = 6;
1598
1599public:
1600
1604 MySqlIPv6ReservationExchange()
1605 : host_id_(0), address_("::"), address_len_(0), prefix_len_(0), type_(0),
1606 iaid_(0), resv_(IPv6Resrv::TYPE_NA, asiolink::IOAddress("::"), 128) {
1607
1608 // Reset error table.
1609 std::fill(&error_[0], &error_[RESRV_COLUMNS], MLM_FALSE);
1610
1611 // Set the column names (for error messages)
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);
1618 }
1619
1634 std::vector<MYSQL_BIND> createBindForSend(const IPv6Resrv& resv,
1635 const HostID& id,
1636 const bool unique_ip) {
1637
1638 // Store the values to ensure they remain valid.
1639 resv_ = resv;
1640 host_id_ = id;
1641
1642 // Initialize prior to constructing the array of MYSQL_BIND structures.
1643 // It sets all fields, including is_null, to zero, so we need to set
1644 // is_null only if it should be true. This gives up minor performance
1645 // benefit while being safe approach. For improved readability, the
1646 // code that explicitly sets is_null is there, but is commented out.
1647 memset(bind_, 0, sizeof(bind_));
1648
1649 // Set up the structures for the various components of the host structure.
1650
1651 try {
1652 // address VARCHAR(39)
1653 address_ = resv.getPrefix().toText();
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_;
1660
1661 // prefix_len tinyint
1662 prefix_len_ = resv.getPrefixLen();
1663 bind_[1].buffer_type = MYSQL_TYPE_TINY;
1664 bind_[1].buffer = reinterpret_cast<char*>(&prefix_len_);
1665 bind_[1].is_unsigned = MLM_TRUE;
1666
1667 // type tinyint
1668 // See lease6_types for values (0 = IA_NA, 1 = IA_TA, 2 = IA_PD)
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_);
1672 bind_[2].is_unsigned = MLM_TRUE;
1673
1674 // dhcp6_iaid INT UNSIGNED
1676 iaid_ = 0;
1677 bind_[3].buffer_type = MYSQL_TYPE_LONG;
1678 bind_[3].buffer = reinterpret_cast<char*>(&iaid_);
1679 bind_[3].is_unsigned = MLM_TRUE;
1680
1681 // host_id INT UNSIGNED NOT NULL
1682 bind_[4].buffer_type = MYSQL_TYPE_LONG;
1683 bind_[4].buffer = reinterpret_cast<char*>(&host_id_);
1684 bind_[4].is_unsigned = MLM_TRUE;
1685
1686 } catch (const std::exception& ex) {
1688 "Could not create bind array from IPv6 Reservation: "
1689 << resv_.toText() << ", reason: " << ex.what());
1690 }
1691
1692 // Add the data to the vector. Note the end element is one after the
1693 // end of the array.
1694 // RESRV_COLUMNS -1 as we do not set reservation_id.
1695 std::vector<MYSQL_BIND> vec(&bind_[0], &bind_[RESRV_COLUMNS-1]);
1696
1697 // When checking whether the IP is unique we need to bind the IPv6 address
1698 // and prefix length at the end of the query as it has additional binding
1699 // for the IPv6 address and prefix length.
1700 if (unique_ip) {
1701 vec.push_back(bind_[0]); // address
1702 vec.push_back(bind_[1]); // prefix_len
1703 }
1704
1705 return (vec);
1706 }
1707
1708private:
1709
1711 uint64_t host_id_;
1712
1714 std::string address_;
1715
1717 unsigned long address_len_;
1718
1720 uint8_t prefix_len_;
1721
1723 uint8_t type_;
1724
1726 uint8_t iaid_;
1727
1729 IPv6Resrv resv_;
1730
1732 MYSQL_BIND bind_[RESRV_COLUMNS];
1733
1735 std::string columns_[RESRV_COLUMNS];
1736
1739 my_bool error_[RESRV_COLUMNS];
1740};
1741
1745class MySqlOptionExchange {
1746private:
1747
1749 static const size_t OPTION_COLUMNS = 10;
1750
1751public:
1752
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_() {
1759
1760 BOOST_STATIC_ASSERT(9 < OPTION_COLUMNS);
1761 }
1762
1766 std::vector<MYSQL_BIND> createBindForSend(const OptionDescriptor& opt_desc,
1767 const std::string& opt_space,
1768 const Optional<SubnetID>& subnet_id,
1769 const HostID& host_id) {
1770
1771 // Hold pointer to the option to make sure it remains valid until
1772 // we complete a query.
1773 option_ = opt_desc.option_;
1774
1775 memset(bind_, 0, sizeof(bind_));
1776
1777 try {
1778 // option_id: INT UNSIGNED NOT NULL
1779 // The option_id is auto_incremented, so we need to pass the NULL
1780 // value.
1781 bind_[0].buffer_type = MYSQL_TYPE_NULL;
1782
1783 // code: SMALLINT UNSIGNED NOT NULL
1784 type_ = option_->getType();
1785 bind_[1].buffer_type = MYSQL_TYPE_SHORT;
1786 bind_[1].buffer = reinterpret_cast<char*>(&type_);
1787 bind_[1].is_unsigned = MLM_TRUE;
1788
1789 // value: BLOB NULL
1790 if (opt_desc.formatted_value_.empty() &&
1791 (opt_desc.option_->len() > opt_desc.option_->getHeaderLen())) {
1792 // The formatted_value is empty and the option value is
1793 // non-empty so we need to prepare on-wire format for the
1794 // option and store it in the database as a blob.
1795 OutputBuffer buf(opt_desc.option_->len());
1796 opt_desc.option_->pack(buf);
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_;
1805
1806 } else {
1807 // No value or formatted_value specified. In this case, the
1808 // value blob is NULL.
1809 value_.clear();
1810 bind_[2].buffer_type = MYSQL_TYPE_NULL;
1811 }
1812
1813 // formatted_value: TEXT NULL,
1814 if (!opt_desc.formatted_value_.empty()) {
1815 formatted_value_len_ = opt_desc.formatted_value_.size();
1816 bind_[3].buffer_type = MYSQL_TYPE_STRING;
1817 bind_[3].buffer = const_cast<char*>(opt_desc.formatted_value_.c_str());
1818 bind_[3].buffer_length = formatted_value_len_;
1819 bind_[3].length = &formatted_value_len_;
1820
1821 } else {
1822 bind_[3].buffer_type = MYSQL_TYPE_NULL;
1823 }
1824
1825 // space: VARCHAR(128) NULL
1826 space_ = opt_space;
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_;
1832
1833 // persistent: TINYINT(1) NOT NULL DEFAULT 0
1834 persistent_ = opt_desc.persistent_;
1835 bind_[5].buffer_type = MYSQL_TYPE_TINY;
1836 bind_[5].buffer = reinterpret_cast<char*>(&persistent_);
1837 bind_[5].is_unsigned = MLM_TRUE;
1838
1839 // user_context: TEST NULL,
1840 ConstElementPtr ctx = opt_desc.getContext();
1841 if (ctx) {
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_;
1848 } else {
1849 bind_[6].buffer_type = MYSQL_TYPE_NULL;
1850 }
1851
1852 // dhcp4_subnet_id: INT UNSIGNED NULL
1853 if (!subnet_id.unspecified()) {
1854 subnet_id_ = subnet_id;
1855 bind_[7].buffer_type = MYSQL_TYPE_LONG;
1856 bind_[7].buffer = reinterpret_cast<char*>(subnet_id_);
1857 bind_[7].is_unsigned = MLM_TRUE;
1858
1859 } else {
1860 bind_[7].buffer_type = MYSQL_TYPE_NULL;
1861 }
1862
1863 // host_id: INT UNSIGNED NOT NULL
1864 host_id_ = host_id;
1865 bind_[8].buffer_type = MYSQL_TYPE_LONG;
1866 bind_[8].buffer = reinterpret_cast<char*>(&host_id_);
1867 bind_[8].is_unsigned = MLM_TRUE;
1868
1869 } catch (const std::exception& ex) {
1871 "Could not create bind array for inserting DHCP "
1872 "option: " << option_->toText() << ", reason: "
1873 << ex.what());
1874 }
1875
1876 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[OPTION_COLUMNS]));
1877 }
1878
1879private:
1880
1882 uint16_t type_;
1883
1885 std::vector<uint8_t> value_;
1886
1888 unsigned long value_len_;
1889
1891 unsigned long formatted_value_len_;
1892
1894 std::string space_;
1895
1897 unsigned long space_len_;
1898
1901 bool persistent_;
1902
1904 std::string user_context_;
1905
1907 unsigned long user_context_len_;
1908
1910 uint32_t subnet_id_;
1911
1913 uint32_t host_id_;
1914
1916 OptionPtr option_;
1917
1919 MYSQL_BIND bind_[OPTION_COLUMNS];
1920};
1921
1922} // namespace
1923
1924namespace isc {
1925namespace dhcp {
1926
1937public:
1938
1945 IOServiceAccessorPtr io_service_accessor,
1946 db::DbCallback db_reconnect_callback);
1947
1952
1955 boost::shared_ptr<MySqlHostWithOptionsExchange> host_ipv4_exchange_;
1956
1959 boost::shared_ptr<MySqlHostIPv6Exchange> host_ipv6_exchange_;
1960
1964 boost::shared_ptr<MySqlHostIPv6Exchange> host_ipv46_exchange_;
1965
1968 boost::shared_ptr<MySqlIPv6ReservationExchange> host_ipv6_reservation_exchange_;
1969
1973 boost::shared_ptr<MySqlOptionExchange> host_option_exchange_;
1974
1977
1980};
1981
1989public:
1990
1992 std::vector<MySqlHostContextPtr> pool_;
1993
1995 std::mutex mutex_;
1996};
1997
1999typedef boost::shared_ptr<MySqlHostContextPool> MySqlHostContextPoolPtr;
2000
2003public:
2004
2014 GET_HOST_DHCPID, // Gets hosts by host identifier
2015 GET_HOST_ADDR, // Gets hosts by IPv4 address
2016 GET_HOST_SUBID4_DHCPID, // Gets host by IPv4 SubnetID, HW address/DUID
2017 GET_HOST_SUBID6_DHCPID, // Gets host by IPv6 SubnetID, HW address/DUID
2018 GET_HOST_SUBID_ADDR, // Gets host by IPv4 SubnetID and IPv4 address
2019 GET_HOST_PREFIX, // Gets host by IPv6 prefix
2020 GET_HOST_SUBID6_ADDR, // Gets host by IPv6 SubnetID and IPv6 prefix
2021 GET_HOST_SUBID4, // Gets hosts by IPv4 SubnetID
2022 GET_HOST_SUBID6, // Gets hosts by IPv6 SubnetID
2023 GET_HOST_HOSTNAME, // Gets hosts by hostname
2024 GET_HOST_HOSTNAME_SUBID4, // Gets hosts by hostname and IPv4 SubnetID
2025 GET_HOST_HOSTNAME_SUBID6, // Gets hosts by hostname and IPv6 SubnetID
2026 GET_HOST_SUBID4_PAGE, // Gets hosts by IPv4 SubnetID beginning by HID
2027 GET_HOST_SUBID6_PAGE, // Gets hosts by IPv6 SubnetID beginning by HID
2028 GET_HOST_PAGE4, // Gets v4 hosts beginning by HID
2029 GET_HOST_PAGE6, // Gets v6 hosts beginning by HID
2030 INSERT_HOST_NON_UNIQUE_IP, // Insert new host to collection with allowing IP duplicates
2031 INSERT_HOST_UNIQUE_IP, // Insert new host to collection with checking for IP duplicates
2032 INSERT_V6_RESRV_NON_UNIQUE,// Insert v6 reservation without checking that it is unique
2033 INSERT_V6_RESRV_UNIQUE, // Insert v6 reservation with checking that it is unique
2034 INSERT_V4_HOST_OPTION, // Insert DHCPv4 option
2035 INSERT_V6_HOST_OPTION, // Insert DHCPv6 option
2036 DEL_HOST_ADDR4, // Delete v4 host (subnet-id, addr4)
2037 DEL_HOST_ADDR6, // Delete v6 host (subnet-id, addr6)
2038 DEL_HOST_SUBID4_ID, // Delete v4 host (subnet-id, ident.type, identifier)
2039 DEL_HOST_SUBID6_ID, // Delete v6 host (subnet-id, ident.type, identifier)
2040 NUM_STATEMENTS // Number of statements
2042
2049
2055
2058
2081 static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl);
2082
2093
2105 std::pair<uint32_t, uint32_t> getVersion() const;
2106
2117 std::vector<MYSQL_BIND>& bind);
2118
2128 StatementIndex stindex,
2129 MYSQL_BIND* bind);
2130
2136 void addResv(MySqlHostContextPtr& ctx,
2137 const IPv6Resrv& resv,
2138 const HostID& id);
2139
2151 const OptionDescriptor& opt_desc,
2152 const std::string& opt_space,
2153 const Optional<SubnetID>& subnet_id,
2154 const HostID& host_id);
2155
2164 const StatementIndex& stindex,
2165 const ConstCfgOptionPtr& options_cfg,
2166 const uint64_t host_id);
2167
2180 const int status,
2181 const StatementIndex index,
2182 const char* what) const;
2183
2203 StatementIndex stindex,
2204 MYSQL_BIND* bind,
2205 boost::shared_ptr<MySqlHostExchange> exchange,
2206 ConstHostCollection& result,
2207 bool single) const;
2208
2227 const SubnetID& subnet_id,
2228 const Host::IdentifierType& identifier_type,
2229 const uint8_t* identifier_begin,
2230 const size_t identifier_len,
2231 StatementIndex stindex,
2232 boost::shared_ptr<MySqlHostExchange> exchange) const;
2233
2243 void checkReadOnly(MySqlHostContextPtr& ctx) const;
2244
2247
2251
2254
2258
2260 std::string timer_name_;
2261};
2262
2263namespace {
2264
2266typedef boost::array<TaggedStatement, MySqlHostDataSourceImpl::NUM_STATEMENTS>
2267TaggedStatementArray;
2268
2271TaggedStatementArray tagged_statements = { {
2272 // Retrieves host information, IPv6 reservations and both DHCPv4 and
2273 // DHCPv6 options associated with the host. The LEFT JOIN clause is used
2274 // to retrieve information from 4 different tables using a single query.
2275 // Hence, this query returns multiple rows for a single host.
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, "
2280 "h.user_context, "
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, "
2288 "r.dhcp6_iaid "
2289 "FROM hosts AS h "
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"},
2298
2299 // Retrieves host information along with the DHCPv4 options associated with
2300 // it. Left joining the dhcp4_options table results in multiple rows being
2301 // returned for the same host. The host is retrieved by IPv4 address.
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 "
2310 "FROM hosts AS h "
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"},
2315
2316 // Retrieves host information and DHCPv4 options using subnet identifier
2317 // and client's identifier. Left joining the dhcp4_options table results in
2318 // multiple rows being returned for the same host.
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 "
2327 "FROM hosts AS h "
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"},
2333
2334 // Retrieves host information, IPv6 reservations and DHCPv6 options
2335 // associated with a host. The number of rows returned is a multiplication
2336 // of number of IPv6 reservations and DHCPv6 options.
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, "
2347 "r.dhcp6_iaid "
2348 "FROM hosts AS h "
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"},
2356
2357 // Retrieves host information and DHCPv4 options for the host using subnet
2358 // identifier and IPv4 reservation. Left joining the dhcp4_options table
2359 // results in multiple rows being returned for the host. The number of
2360 // rows depends on the number of options defined for the host.
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 "
2369 "FROM hosts AS h "
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"},
2374
2375 // Retrieves host information, IPv6 reservations and DHCPv6 options
2376 // associated with a host using prefix and prefix length. This query
2377 // returns host information for a single host. However, multiple rows
2378 // are returned due to left joining IPv6 reservations and DHCPv6 options.
2379 // The number of rows returned is multiplication of number of existing
2380 // IPv6 reservations and DHCPv6 options.
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, "
2391 "r.dhcp6_iaid "
2392 "FROM hosts AS h "
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"},
2401
2402 // Retrieves host information, IPv6 reservations and DHCPv6 options
2403 // associated with a host using IPv6 subnet id and prefix. This query
2404 // returns host information for a single host. However, multiple rows
2405 // are returned due to left joining IPv6 reservations and DHCPv6 options.
2406 // The number of rows returned is multiplication of number of existing
2407 // IPv6 reservations and DHCPv6 options.
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, "
2418 "r.dhcp6_iaid "
2419 "FROM hosts AS h "
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"},
2426
2427 // Retrieves host information along with the DHCPv4 options associated with
2428 // it. Left joining the dhcp4_options table results in multiple rows being
2429 // returned for the same host. Hosts are retrieved by IPv4 subnet id.
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 "
2438 "FROM hosts AS h "
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"},
2443
2444 // Retrieves host information, IPv6 reservations and DHCPv6 options
2445 // associated with a host. The number of rows returned is a multiplication
2446 // of number of IPv6 reservations and DHCPv6 options. Hosts are retrieved
2447 // by IPv6 subnet id.
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, "
2458 "r.dhcp6_iaid "
2459 "FROM hosts AS h "
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"},
2466
2467 // Retrieves host information, IPv6 reservations and both DHCPv4 and
2468 // DHCPv6 options associated with the host. The LEFT JOIN clause is used
2469 // to retrieve information from 4 different tables using a single query.
2470 // Hence, this query returns multiple rows for a single host.
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, "
2475 "h.user_context, "
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, "
2483 "r.dhcp6_iaid "
2484 "FROM hosts AS h "
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"},
2493
2494 // Retrieves host information and DHCPv4 options using hostname and
2495 // subnet identifier. Left joining the dhcp4_options table results in
2496 // multiple rows being returned for the same host.
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 "
2505 "FROM hosts AS h "
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"},
2510
2511 // Retrieves host information, IPv6 reservations and DHCPv6 options
2512 // using hostname and subnet identifier. The number of rows returned
2513 // is a multiplication of number of IPv6 reservations and DHCPv6 options.
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, "
2524 "r.dhcp6_iaid "
2525 "FROM hosts AS h "
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"},
2532
2533 // Retrieves host information along with the DHCPv4 options associated with
2534 // it. Left joining the dhcp4_options table results in multiple rows being
2535 // returned for the same host. Hosts are retrieved by IPv4 subnet id
2536 // and with a host id greater than the start one.
2537 // The number of hosts returned is lower or equal to the limit.
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 "
2549 "LIMIT ? ) AS h "
2550 "LEFT JOIN dhcp4_options AS o "
2551 "ON h.host_id = o.host_id "
2552 "ORDER BY h.host_id, o.option_id"},
2553
2554 // Retrieves host information, IPv6 reservations and DHCPv6 options
2555 // associated with a host. The number of rows returned is a multiplication
2556 // of number of IPv6 reservations and DHCPv6 options. Hosts are retrieved
2557 // by IPv6 subnet id and with a host id greater than the start one.
2558 // The number of hosts returned is lower or equal to the limit.
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, "
2569 "r.dhcp6_iaid "
2570 "FROM ( SELECT * FROM hosts AS h "
2571 "WHERE h.dhcp6_subnet_id = ? AND h.host_id > ? "
2572 "ORDER BY h.host_id "
2573 "LIMIT ? ) AS h "
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"},
2579
2580 // Retrieves host information along with the DHCPv4 options associated with
2581 // it. Left joining the dhcp4_options table results in multiple rows being
2582 // returned for the same host. Hosts are retrieved with a host id greater
2583 // than the start one.
2584 // The number of hosts returned is lower or equal to the limit.
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 "
2596 "LIMIT ? ) AS h "
2597 "LEFT JOIN dhcp4_options AS o "
2598 "ON h.host_id = o.host_id "
2599 "ORDER BY h.host_id, o.option_id"},
2600
2601 // Retrieves host information, IPv6 reservations and DHCPv6 options
2602 // associated with a host. The number of rows returned is a multiplication
2603 // of number of IPv6 reservations and DHCPv6 options. Hosts are retrieved
2604 // with a host id greater than the start one.
2605 // The number of hosts returned is lower or equal to the limit.
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, "
2616 "r.dhcp6_iaid "
2617 "FROM ( SELECT * FROM hosts AS h "
2618 "WHERE h.host_id > ? "
2619 "ORDER BY h.host_id "
2620 "LIMIT ? ) AS h "
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"},
2626
2627 // Inserts a host into the 'hosts' table without checking that there is
2628 // a reservation for the IP address.
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
2636
2637 // Inserts a host into the 'hosts' table with checking that reserved IP
2638 // address is unique. The innermost query checks if there is at least
2639 // one host for the given IP/subnet combination. For checking whether
2640 // hosts exists or not it doesn't matter if we select actual columns,
2641 // thus SELECT 1 was used as an optimization to avoid selecting data
2642 // that will be ignored anyway. If the host does not exist the new
2643 // host is inserted. DUAL is a special MySQL table from which we can
2644 // select the values to be inserted. If the host with the given IP
2645 // address already exists the new host won't be inserted. The caller
2646 // can check the number of affected rows to detect that there was
2647 // a duplicate host in the database.
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 = ? "
2658 "LIMIT 1"
2659 ")"},
2660
2661 // Inserts a single IPv6 reservation into 'reservations' table without
2662 // checking that the inserted reservation is unique.
2664 "INSERT INTO ipv6_reservations(address, prefix_len, type, "
2665 "dhcp6_iaid, host_id) "
2666 "VALUES (?, ?, ?, ?, ?)"},
2667
2668 // Inserts a single IPv6 reservation into 'reservations' table with
2669 // checking that the inserted reservation is 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 = ? "
2677 "LIMIT 1"
2678 ")"},
2679
2680 // Inserts a single DHCPv4 option into 'dhcp4_options' table.
2681 // Using fixed scope_id = 3, which associates an option with host.
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)"},
2686
2687 // Inserts a single DHCPv6 option into 'dhcp6_options' table.
2688 // Using fixed scope_id = 3, which associates an option with host.
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)"},
2693
2694 // Delete IPv4 reservations by subnet id and reserved address.
2696 "DELETE FROM hosts WHERE dhcp4_subnet_id = ? AND ipv4_address = ?"},
2697
2698 // Delete IPv6 reservations by subnet id and reserved address/prefix.
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 = ?"},
2704
2705 // Delete a single IPv4 reservation by subnet id and identifier.
2707 "DELETE FROM hosts WHERE dhcp4_subnet_id = ? AND dhcp_identifier_type=? "
2708 "AND dhcp_identifier = ?"},
2709
2710 // Delete a single IPv6 reservation by subnet id and identifier.
2712 "DELETE FROM hosts WHERE dhcp6_subnet_id = ? AND dhcp_identifier_type=? "
2713 "AND dhcp_identifier = ?"}
2714 }
2715};
2716
2717} // namespace
2718
2719// MySqlHostContext Constructor
2720
2722 IOServiceAccessorPtr io_service_accessor,
2723 db::DbCallback db_reconnect_callback)
2724 : conn_(parameters, io_service_accessor, db_reconnect_callback),
2725 is_readonly_(true) {
2726}
2727
2728// MySqlHostContextAlloc Constructor and Destructor
2729
2731 MySqlHostDataSourceImpl& mgr) : ctx_(), mgr_(mgr) {
2732
2733 if (MultiThreadingMgr::instance().getMode()) {
2734 // multi-threaded
2735 {
2736 // we need to protect the whole pool_ operation, hence extra scope {}
2737 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2738 if (!mgr_.pool_->pool_.empty()) {
2739 ctx_ = mgr_.pool_->pool_.back();
2740 mgr_.pool_->pool_.pop_back();
2741 }
2742 }
2743 if (!ctx_) {
2744 ctx_ = mgr_.createContext();
2745 }
2746 } else {
2747 // single-threaded
2748 if (mgr_.pool_->pool_.empty()) {
2749 isc_throw(Unexpected, "No available MySQL host context?!");
2750 }
2751 ctx_ = mgr_.pool_->pool_.back();
2752 }
2753}
2754
2756 if (MultiThreadingMgr::instance().getMode()) {
2757 // multi-threaded
2758 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2759 mgr_.pool_->pool_.push_back(ctx_);
2760 if (ctx_->conn_.isUnusable()) {
2761 mgr_.unusable_ = true;
2762 }
2763 } else if (ctx_->conn_.isUnusable()) {
2764 mgr_.unusable_ = true;
2765 }
2766}
2767
2769 : parameters_(parameters), ip_reservations_unique_(true), unusable_(false),
2770 timer_name_("") {
2771
2772 // Create unique timer name per instance.
2773 timer_name_ = "MySqlHostMgr[";
2774 timer_name_ += boost::lexical_cast<std::string>(reinterpret_cast<uint64_t>(this));
2775 timer_name_ += "]DbReconnectTimer";
2776
2777 // Validate the schema version first.
2778 std::pair<uint32_t, uint32_t> code_version(MYSQL_SCHEMA_VERSION_MAJOR,
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);
2787 }
2788
2789 // Create an initial context.
2790 pool_.reset(new MySqlHostContextPool());
2791 pool_->pool_.push_back(createContext());
2792}
2793
2794// Create context.
2795
2801
2802 // Open the database.
2803 ctx->conn_.openDatabase();
2804
2805 // Prepare query statements. Those are will be only used to retrieve
2806 // information from the database, so they can be used even if the
2807 // database is read only for the current user.
2808 ctx->conn_.prepareStatements(tagged_statements.begin(),
2809 tagged_statements.begin() + WRITE_STMTS_BEGIN);
2810
2811 // Check if the backend is explicitly configured to operate with
2812 // read only access to the database.
2813 ctx->is_readonly_ = ctx->conn_.configuredReadOnly();
2814
2815 // If we are using read-write mode for the database we also prepare
2816 // statements for INSERTS etc.
2817 if (!ctx->is_readonly_) {
2818 // Prepare statements for writing to the database, e.g. INSERT.
2819 ctx->conn_.prepareStatements(tagged_statements.begin() + WRITE_STMTS_BEGIN,
2820 tagged_statements.end());
2821 } else {
2823 }
2824
2825 // Create the exchange objects for use in exchanging data between the
2826 // program and the database.
2827 ctx->host_ipv4_exchange_.reset(new MySqlHostWithOptionsExchange(MySqlHostWithOptionsExchange::DHCP4_ONLY));
2828 ctx->host_ipv6_exchange_.reset(new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::DHCP6_ONLY));
2829 ctx->host_ipv46_exchange_.reset(new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::DHCP4_AND_DHCP6));
2830 ctx->host_ipv6_reservation_exchange_.reset(new MySqlIPv6ReservationExchange());
2831 ctx->host_option_exchange_.reset(new MySqlOptionExchange());
2832
2833 // Create ReconnectCtl for this connection.
2834 ctx->conn_.makeReconnectCtl(timer_name_);
2835
2836 return (ctx);
2837}
2838
2840}
2841
2842bool
2845
2846 // Invoke application layer connection lost callback.
2847 if (!DatabaseConnection::invokeDbLostCallback(db_reconnect_ctl)) {
2848 return (false);
2849 }
2850
2851 bool reopened = false;
2852
2853 const std::string timer_name = db_reconnect_ctl->timerName();
2854
2855 // At least one connection was lost.
2856 try {
2857 CfgDbAccessPtr cfg_db = CfgMgr::instance().getCurrentCfg()->getCfgDbAccess();
2858 std::list<std::string> host_db_access_list = cfg_db->getHostDbAccessStringList();
2859 for (std::string& hds : host_db_access_list) {
2860 auto parameters = DatabaseConnection::parse(hds);
2861 if (HostMgr::delBackend("mysql", hds, true)) {
2863 }
2864 }
2865 reopened = true;
2866 } catch (const std::exception& ex) {
2868 .arg(ex.what());
2869 }
2870
2871 if (reopened) {
2872 // Cancel the timer.
2873 if (TimerMgr::instance()->isTimerRegistered(timer_name)) {
2874 TimerMgr::instance()->unregisterTimer(timer_name);
2875 }
2876
2877 // Invoke application layer connection recovered callback.
2878 if (!DatabaseConnection::invokeDbRecoveredCallback(db_reconnect_ctl)) {
2879 return (false);
2880 }
2881 } else {
2882 if (!db_reconnect_ctl->checkRetries()) {
2883 // We're out of retries, log it and initiate shutdown.
2885 .arg(db_reconnect_ctl->maxRetries());
2886
2887 // Cancel the timer.
2888 if (TimerMgr::instance()->isTimerRegistered(timer_name)) {
2889 TimerMgr::instance()->unregisterTimer(timer_name);
2890 }
2891
2892 // Invoke application layer connection failed callback.
2894 return (false);
2895 }
2896
2898 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2899 .arg(db_reconnect_ctl->maxRetries())
2900 .arg(db_reconnect_ctl->retryInterval());
2901
2902 // Start the timer.
2903 if (!TimerMgr::instance()->isTimerRegistered(timer_name)) {
2904 TimerMgr::instance()->registerTimer(timer_name,
2905 std::bind(&MySqlHostDataSourceImpl::dbReconnect, db_reconnect_ctl),
2906 db_reconnect_ctl->retryInterval(),
2908 }
2909 TimerMgr::instance()->setup(timer_name);
2910 }
2911
2912 return (true);
2913}
2914
2915std::pair<uint32_t, uint32_t>
2919
2921}
2922
2923void
2925 StatementIndex stindex,
2926 std::vector<MYSQL_BIND>& bind) {
2927 // Bind the parameters to the statement
2928 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
2929 checkError(ctx, status, stindex, "unable to bind parameters");
2930
2931 // Execute the statement
2932 status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
2933
2934 if (status != 0) {
2935 // Failure: check for the special case of duplicate entry.
2936 if (mysql_errno(ctx->conn_.mysql_) == ER_DUP_ENTRY) {
2937 isc_throw(DuplicateEntry, "Database duplicate entry error");
2938 }
2939 checkError(ctx, status, stindex, "unable to execute");
2940 }
2941
2942 // If the number of rows inserted is 0 it means that the query detected
2943 // an attempt to insert duplicated data for which there is no unique
2944 // index in the database. Unique indexes are not created in the database
2945 // when it may be sometimes allowed to insert duplicated records per
2946 // server's configuration.
2947 my_ulonglong numrows = mysql_stmt_affected_rows(ctx->conn_.statements_[stindex]);
2948 if (numrows == 0) {
2949 isc_throw(DuplicateEntry, "Database duplicate entry error");
2950 }
2951}
2952
2953bool
2955 StatementIndex stindex,
2956 MYSQL_BIND* bind) {
2957 // Bind the parameters to the statement
2958 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
2959 checkError(ctx, status, stindex, "unable to bind parameters");
2960
2961 // Execute the statement
2962 status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
2963
2964 if (status != 0) {
2965 checkError(ctx, status, stindex, "unable to execute");
2966 }
2967
2968 // Let's check how many hosts were deleted.
2969 my_ulonglong numrows = mysql_stmt_affected_rows(ctx->conn_.statements_[stindex]);
2970
2971 return (numrows != 0);
2972}
2973
2974void
2976 const IPv6Resrv& resv,
2977 const HostID& id) {
2978 std::vector<MYSQL_BIND> bind = ctx->host_ipv6_reservation_exchange_->
2979 createBindForSend(resv, id, ip_reservations_unique_);
2980
2982}
2983
2984void
2986 const StatementIndex& stindex,
2987 const OptionDescriptor& opt_desc,
2988 const std::string& opt_space,
2989 const Optional<SubnetID>& subnet_id,
2990 const HostID& id) {
2991 std::vector<MYSQL_BIND> bind = ctx->host_option_exchange_->createBindForSend(opt_desc, opt_space, subnet_id, id);
2992
2993 addStatement(ctx, stindex, bind);
2994}
2995
2996void
2998 const StatementIndex& stindex,
2999 const ConstCfgOptionPtr& options_cfg,
3000 const uint64_t host_id) {
3001 // Get option space names and vendor space names and combine them within a
3002 // single list.
3003 std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
3004 std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
3005 option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
3006 vendor_spaces.end());
3007
3008 // For each option space retrieve all options and insert them into the
3009 // database.
3010 for (auto space = option_spaces.begin(); space != option_spaces.end(); ++space) {
3011 OptionContainerPtr options = options_cfg->getAll(*space);
3012 if (options && !options->empty()) {
3013 for (auto opt = options->begin(); opt != options->end(); ++opt) {
3014 addOption(ctx, stindex, *opt, *space, Optional<SubnetID>(), host_id);
3015 }
3016 }
3017 }
3018}
3019
3020void
3022 const int status,
3023 const StatementIndex index,
3024 const char* what) const {
3025 ctx->conn_.checkError(status, index, what);
3026}
3027
3028void
3030 StatementIndex stindex,
3031 MYSQL_BIND* bind,
3032 boost::shared_ptr<MySqlHostExchange> exchange,
3033 ConstHostCollection& result,
3034 bool single) const {
3035
3036 // Bind the selection parameters to the statement
3037 int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
3038 checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
3039
3040 // Set up the MYSQL_BIND array for the data being returned and bind it to
3041 // the statement.
3042 std::vector<MYSQL_BIND> outbind = exchange->createBindForReceive();
3043 status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &outbind[0]);
3044 checkError(ctx, status, stindex, "unable to bind SELECT clause parameters");
3045
3046 // Execute the statement
3047 status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
3048 checkError(ctx, status, stindex, "unable to execute");
3049
3050 // Ensure that all the lease information is retrieved in one go to avoid
3051 // overhead of going back and forth between client and server.
3052 status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
3053 checkError(ctx, status, stindex, "unable to set up for storing all results");
3054
3055 // Set up the fetch "release" object to release resources associated
3056 // with the call to mysql_stmt_fetch when this method exits, then
3057 // retrieve the data. mysql_stmt_fetch return value equal to 0 represents
3058 // successful data fetch.
3059 MySqlFreeResult fetch_release(ctx->conn_.statements_[stindex]);
3060 while ((status = mysql_stmt_fetch(ctx->conn_.statements_[stindex])) ==
3062 try {
3063 exchange->processFetchedData(result);
3064
3065 } catch (const isc::BadValue& ex) {
3066 // Rethrow the exception with a bit more data.
3067 isc_throw(BadValue, ex.what() << ". Statement is <" <<
3068 ctx->conn_.text_statements_[stindex] << ">");
3069 }
3070
3071 if (single && (result.size() > 1)) {
3072 isc_throw(MultipleRecords, "multiple records were found in the "
3073 "database where only one was expected for query "
3074 << ctx->conn_.text_statements_[stindex]);
3075 }
3076 }
3077
3078 // How did the fetch end?
3079 // If mysql_stmt_fetch return value is equal to 1 an error occurred.
3080 if (status == MLM_MYSQL_FETCH_FAILURE) {
3081 // Error - unable to fetch results
3082 checkError(ctx, status, stindex, "unable to fetch results");
3083
3084 } else if (status == MYSQL_DATA_TRUNCATED) {
3085 // Data truncated - throw an exception indicating what was at fault
3086 isc_throw(DataTruncated, ctx->conn_.text_statements_[stindex]
3087 << " returned truncated data: columns affected are "
3088 << exchange->getErrorColumns());
3089 }
3090}
3091
3094 const SubnetID& subnet_id,
3095 const Host::IdentifierType& identifier_type,
3096 const uint8_t* identifier_begin,
3097 const size_t identifier_len,
3098 StatementIndex stindex,
3099 boost::shared_ptr<MySqlHostExchange> exchange) const {
3100
3101 // Set up the WHERE clause value
3102 MYSQL_BIND inbind[3];
3103 memset(inbind, 0, sizeof(inbind));
3104
3105 uint32_t subnet_buffer = static_cast<uint32_t>(subnet_id);
3106 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3107 inbind[0].buffer = reinterpret_cast<char*>(&subnet_buffer);
3108 inbind[0].is_unsigned = MLM_TRUE;
3109
3110 // Identifier value.
3111 std::vector<char> identifier_vec(identifier_begin,
3112 identifier_begin + identifier_len);
3113 unsigned long length = identifier_vec.size();
3114 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3115 inbind[2].buffer = &identifier_vec[0];
3116 inbind[2].buffer_length = length;
3117 inbind[2].length = &length;
3118
3119 // Identifier type.
3120 char identifier_type_copy = static_cast<char>(identifier_type);
3121 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3122 inbind[1].buffer = reinterpret_cast<char*>(&identifier_type_copy);
3123 inbind[1].is_unsigned = MLM_TRUE;
3124
3125 ConstHostCollection collection;
3126 getHostCollection(ctx, stindex, inbind, exchange, collection, true);
3127
3128 // Return single record if present, else clear the host.
3129 ConstHostPtr result;
3130 if (!collection.empty()) {
3131 result = *collection.begin();
3132 }
3133
3134 return (result);
3135}
3136
3137void
3139 if (ctx->is_readonly_) {
3140 isc_throw(ReadOnlyDb, "MySQL host database backend is configured to"
3141 " operate in read only mode");
3142 }
3143}
3144
3146 : impl_(new MySqlHostDataSourceImpl(parameters)) {
3147}
3148
3150}
3151
3154 return (impl_->parameters_);
3155}
3156
3157void
3159 // Get a context
3160 MySqlHostContextAlloc get_context(*impl_);
3161 MySqlHostContextPtr ctx = get_context.ctx_;
3162
3163 // If operating in read-only mode, throw exception.
3164 impl_->checkReadOnly(ctx);
3165
3166 // Initiate MySQL transaction as we will have to make multiple queries
3167 // to insert host information into multiple tables. If that fails on
3168 // any stage, the transaction will be rolled back by the destructor of
3169 // the MySqlTransaction class.
3170 MySqlTransaction transaction(ctx->conn_);
3171
3172 // If we're configured to check that an IP reservation within a given subnet
3173 // is unique, the IP reservation exists and the subnet is actually set
3174 // we will be using a special query that checks for uniqueness. Otherwise,
3175 // we will use a regular insert statement.
3176 bool unique_ip = impl_->ip_reservations_unique_ && !host->getIPv4Reservation().isV4Zero()
3177 && host->getIPv4SubnetID() != SUBNET_ID_UNUSED;
3178
3179 // Create the MYSQL_BIND array for the host
3180 std::vector<MYSQL_BIND> bind = ctx->host_ipv4_exchange_->createBindForSend(host, unique_ip);
3181
3182 // ... and insert the host.
3183 impl_->addStatement(ctx, unique_ip ? MySqlHostDataSourceImpl::INSERT_HOST_UNIQUE_IP :
3185
3186 // Gets the last inserted hosts id
3187 uint64_t host_id = mysql_insert_id(ctx->conn_.mysql_);
3188
3189 // Insert DHCPv4 options.
3190 ConstCfgOptionPtr cfg_option4 = host->getCfgOption4();
3191 if (cfg_option4) {
3193 cfg_option4, host_id);
3194 }
3195
3196 // Insert DHCPv6 options.
3197 ConstCfgOptionPtr cfg_option6 = host->getCfgOption6();
3198 if (cfg_option6) {
3200 cfg_option6, host_id);
3201 }
3202
3203 // Insert IPv6 reservations.
3204 IPv6ResrvRange v6resv = host->getIPv6Reservations();
3205 if (std::distance(v6resv.first, v6resv.second) > 0) {
3206 for (IPv6ResrvIterator resv = v6resv.first; resv != v6resv.second;
3207 ++resv) {
3208 impl_->addResv(ctx, resv->second, host_id);
3209 }
3210 }
3211
3212 // Everything went fine, so explicitly commit the transaction.
3213 transaction.commit();
3214}
3215
3216bool
3218 const asiolink::IOAddress& addr) {
3219 // Get a context
3220 MySqlHostContextAlloc get_context(*impl_);
3221 MySqlHostContextPtr ctx = get_context.ctx_;
3222
3223 // If operating in read-only mode, throw exception.
3224 impl_->checkReadOnly(ctx);
3225
3226 // Set up the WHERE clause value
3227 MYSQL_BIND inbind[2];
3228
3229 uint32_t subnet = subnet_id;
3230 memset(inbind, 0, sizeof(inbind));
3231 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3232 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3233 inbind[0].is_unsigned = MLM_TRUE;
3234
3235 // v4
3236 if (addr.isV4()) {
3237 uint32_t addr4 = addr.toUint32();
3238 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3239 inbind[1].buffer = reinterpret_cast<char*>(&addr4);
3240 inbind[1].is_unsigned = MLM_TRUE;
3241
3242 return (impl_->delStatement(ctx, MySqlHostDataSourceImpl::DEL_HOST_ADDR4, inbind));
3243 }
3244
3245 // v6
3246 std::string addr_str = addr.toText();
3247 unsigned long addr_len = addr_str.size();
3248 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3249 inbind[1].buffer = reinterpret_cast<char*>
3250 (const_cast<char*>(addr_str.c_str()));
3251 inbind[1].length = &addr_len;
3252 inbind[1].buffer_length = addr_len;
3253
3254 return (impl_->delStatement(ctx, MySqlHostDataSourceImpl::DEL_HOST_ADDR6, inbind));
3255}
3256
3257bool
3259 const Host::IdentifierType& identifier_type,
3260 const uint8_t* identifier_begin,
3261 const size_t identifier_len) {
3262 // Get a context
3263 MySqlHostContextAlloc get_context(*impl_);
3264 MySqlHostContextPtr ctx = get_context.ctx_;
3265
3266 // If operating in read-only mode, throw exception.
3267 impl_->checkReadOnly(ctx);
3268
3269 // Set up the WHERE clause value
3270 MYSQL_BIND inbind[3];
3271
3272 // subnet-id
3273 memset(inbind, 0, sizeof(inbind));
3274 uint32_t subnet = static_cast<uint32_t>(subnet_id);
3275 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3276 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3277 inbind[0].is_unsigned = MLM_TRUE;
3278
3279 // identifier type
3280 char identifier_type_copy = static_cast<char>(identifier_type);
3281 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3282 inbind[1].buffer = reinterpret_cast<char*>(&identifier_type_copy);
3283 inbind[1].is_unsigned = MLM_TRUE;
3284
3285 // identifier value
3286 std::vector<char> identifier_vec(identifier_begin,
3287 identifier_begin + identifier_len);
3288 unsigned long length = identifier_vec.size();
3289 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3290 inbind[2].buffer = &identifier_vec[0];
3291 inbind[2].buffer_length = length;
3292 inbind[2].length = &length;
3293
3294 ConstHostCollection collection;
3295 return (impl_->delStatement(ctx, MySqlHostDataSourceImpl::DEL_HOST_SUBID4_ID, inbind));
3296}
3297
3298bool
3300 const Host::IdentifierType& identifier_type,
3301 const uint8_t* identifier_begin,
3302 const size_t identifier_len) {
3303 // Get a context
3304 MySqlHostContextAlloc get_context(*impl_);
3305 MySqlHostContextPtr ctx = get_context.ctx_;
3306
3307 // If operating in read-only mode, throw exception.
3308 impl_->checkReadOnly(ctx);
3309
3310 // Set up the WHERE clause value
3311 MYSQL_BIND inbind[3];
3312
3313 // subnet-id
3314 memset(inbind, 0, sizeof(inbind));
3315 uint32_t subnet = static_cast<uint32_t>(subnet_id);
3316 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3317 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3318 inbind[0].is_unsigned = MLM_TRUE;
3319
3320 // identifier type
3321 char identifier_type_copy = static_cast<char>(identifier_type);
3322 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3323 inbind[1].buffer = reinterpret_cast<char*>(&identifier_type_copy);
3324 inbind[1].is_unsigned = MLM_TRUE;
3325
3326 // identifier value
3327 std::vector<char> identifier_vec(identifier_begin,
3328 identifier_begin + identifier_len);
3329 unsigned long length = identifier_vec.size();
3330 inbind[2].buffer_type = MYSQL_TYPE_BLOB;
3331 inbind[2].buffer = &identifier_vec[0];
3332 inbind[2].buffer_length = length;
3333 inbind[2].length = &length;
3334
3335 ConstHostCollection collection;
3336 return (impl_->delStatement(ctx, MySqlHostDataSourceImpl::DEL_HOST_SUBID6_ID, inbind));
3337}
3338
3341 const uint8_t* identifier_begin,
3342 const size_t identifier_len) const {
3343 // Get a context
3344 MySqlHostContextAlloc get_context(*impl_);
3345 MySqlHostContextPtr ctx = get_context.ctx_;
3346
3347 // Set up the WHERE clause value
3348 MYSQL_BIND inbind[2];
3349 memset(inbind, 0, sizeof(inbind));
3350
3351 // Identifier type.
3352 char identifier_type_copy = static_cast<char>(identifier_type);
3353 inbind[1].buffer = &identifier_type_copy;
3354 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3355 inbind[1].is_unsigned = MLM_TRUE;
3356
3357 // Identifier value.
3358 std::vector<char> identifier_vec(identifier_begin,
3359 identifier_begin + identifier_len);
3360 unsigned long int length = identifier_vec.size();
3361 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3362 inbind[0].buffer = &identifier_vec[0];
3363 inbind[0].buffer_length = length;
3364 inbind[0].length = &length;
3365
3366 ConstHostCollection result;
3367 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_DHCPID, inbind,
3368 ctx->host_ipv46_exchange_, result, false);
3369
3370 return (result);
3371}
3372
3375 // Get a context
3376 MySqlHostContextAlloc get_context(*impl_);
3377 MySqlHostContextPtr ctx = get_context.ctx_;
3378
3379 // Set up the WHERE clause value
3380 MYSQL_BIND inbind[1];
3381 memset(inbind, 0, sizeof(inbind));
3382 uint32_t subnet = subnet_id;
3383 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3384 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3385 inbind[0].is_unsigned = MLM_TRUE;
3386
3387 ConstHostCollection result;
3388 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID4, inbind,
3389 ctx->host_ipv4_exchange_, result, false);
3390
3391 return (result);
3392}
3393
3396 // Get a context
3397 MySqlHostContextAlloc get_context(*impl_);
3398 MySqlHostContextPtr ctx = get_context.ctx_;
3399
3400 // Set up the WHERE clause value
3401 MYSQL_BIND inbind[1];
3402 memset(inbind, 0, sizeof(inbind));
3403 uint32_t subnet = subnet_id;
3404 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3405 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3406 inbind[0].is_unsigned = MLM_TRUE;
3407
3408 ConstHostCollection result;
3409 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID6, inbind,
3410 ctx->host_ipv6_exchange_, result, false);
3411
3412 return (result);
3413}
3414
3416MySqlHostDataSource::getAllbyHostname(const std::string& hostname) const {
3417 // Get a context
3418 MySqlHostContextAlloc get_context(*impl_);
3419 MySqlHostContextPtr ctx = get_context.ctx_;
3420
3421 // Set up the WHERE clause value
3422 MYSQL_BIND inbind[1];
3423 memset(inbind, 0, sizeof(inbind));
3424
3425 // Hostname
3426 char hostname_[HOSTNAME_MAX_LEN];
3427 strncpy(hostname_, hostname.c_str(), HOSTNAME_MAX_LEN - 1);
3428 unsigned long length = hostname.length();
3429 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3430 inbind[0].buffer = reinterpret_cast<char*>(hostname_);
3431 inbind[0].buffer_length = length;
3432 inbind[0].length = &length;
3433
3434 ConstHostCollection result;
3435 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_HOSTNAME, inbind,
3436 ctx->host_ipv46_exchange_, result, false);
3437
3438 return (result);
3439}
3440
3442MySqlHostDataSource::getAllbyHostname4(const std::string& hostname,
3443 const SubnetID& subnet_id) const {
3444 // Get a context
3445 MySqlHostContextAlloc get_context(*impl_);
3446 MySqlHostContextPtr ctx = get_context.ctx_;
3447
3448 // Set up the WHERE clause value
3449 MYSQL_BIND inbind[2];
3450 memset(inbind, 0, sizeof(inbind));
3451
3452 // Hostname
3453 char hostname_[HOSTNAME_MAX_LEN];
3454 strncpy(hostname_, hostname.c_str(), HOSTNAME_MAX_LEN - 1);
3455 unsigned long length = hostname.length();
3456 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3457 inbind[0].buffer = reinterpret_cast<char*>(hostname_);
3458 inbind[0].buffer_length = length;
3459 inbind[0].length = &length;
3460
3461 // Subnet ID
3462 uint32_t subnet = subnet_id;
3463 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3464 inbind[1].buffer = reinterpret_cast<char*>(&subnet);
3465 inbind[1].is_unsigned = MLM_TRUE;
3466
3467 ConstHostCollection result;
3468 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_HOSTNAME_SUBID4, inbind,
3469 ctx->host_ipv4_exchange_, result, false);
3470
3471 return (result);
3472}
3473
3475MySqlHostDataSource::getAllbyHostname6(const std::string& hostname,
3476 const SubnetID& subnet_id) const {
3477 // Get a context
3478 MySqlHostContextAlloc get_context(*impl_);
3479 MySqlHostContextPtr ctx = get_context.ctx_;
3480
3481 // Set up the WHERE clause value
3482 MYSQL_BIND inbind[2];
3483 memset(inbind, 0, sizeof(inbind));
3484
3485 // Hostname
3486 char hostname_[HOSTNAME_MAX_LEN];
3487 strncpy(hostname_, hostname.c_str(), HOSTNAME_MAX_LEN - 1);
3488 unsigned long length = hostname.length();
3489 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3490 inbind[0].buffer = reinterpret_cast<char*>(hostname_);
3491 inbind[0].buffer_length = length;
3492 inbind[0].length = &length;
3493
3494 // Subnet ID
3495 uint32_t subnet = subnet_id;
3496 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3497 inbind[1].buffer = reinterpret_cast<char*>(&subnet);
3498 inbind[1].is_unsigned = MLM_TRUE;
3499
3500 ConstHostCollection result;
3501 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_HOSTNAME_SUBID6, inbind,
3502 ctx->host_ipv6_exchange_, result, false);
3503
3504 return (result);
3505}
3506
3509 size_t& /*source_index*/,
3510 uint64_t lower_host_id,
3511 const HostPageSize& page_size) const {
3512 // Get a context
3513 MySqlHostContextAlloc get_context(*impl_);
3514 MySqlHostContextPtr ctx = get_context.ctx_;
3515
3516 // Set up the WHERE clause value
3517 MYSQL_BIND inbind[3];
3518 memset(inbind, 0, sizeof(inbind));
3519
3520 // Bind subnet id
3521 uint32_t subnet = subnet_id;
3522 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3523 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3524 inbind[0].is_unsigned = MLM_TRUE;
3525
3526 // Bind lower host id
3527 uint32_t host_id = lower_host_id;
3528 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3529 inbind[1].buffer = reinterpret_cast<char*>(&host_id);
3530 inbind[1].is_unsigned = MLM_TRUE;
3531
3532 // Bind page size value
3533 uint32_t page_size_data = page_size.page_size_;
3534 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3535 inbind[2].buffer = reinterpret_cast<char*>(&page_size_data);
3536 inbind[2].is_unsigned = MLM_TRUE;
3537
3538 ConstHostCollection result;
3539 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID4_PAGE, inbind,
3540 ctx->host_ipv4_exchange_, result, false);
3541
3542 return (result);
3543}
3544
3547 size_t& /*source_index*/,
3548 uint64_t lower_host_id,
3549 const HostPageSize& page_size) const {
3550 // Get a context
3551 MySqlHostContextAlloc get_context(*impl_);
3552 MySqlHostContextPtr ctx = get_context.ctx_;
3553
3554 // Set up the WHERE clause value
3555 MYSQL_BIND inbind[3];
3556 memset(inbind, 0, sizeof(inbind));
3557
3558 // Bind subnet id
3559 uint32_t subnet = subnet_id;
3560 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3561 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3562 inbind[0].is_unsigned = MLM_TRUE;
3563
3564 // Bind lower host id
3565 uint32_t host_id = lower_host_id;
3566 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3567 inbind[1].buffer = reinterpret_cast<char*>(&host_id);
3568 inbind[1].is_unsigned = MLM_TRUE;
3569
3570 // Bind page size value
3571 uint32_t page_size_data = page_size.page_size_;
3572 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3573 inbind[2].buffer = reinterpret_cast<char*>(&page_size_data);
3574 inbind[2].is_unsigned = MLM_TRUE;
3575
3576 ConstHostCollection result;
3577 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID6_PAGE, inbind,
3578 ctx->host_ipv6_exchange_, result, false);
3579
3580 return (result);
3581}
3582
3584MySqlHostDataSource::getPage4(size_t& /*source_index*/,
3585 uint64_t lower_host_id,
3586 const HostPageSize& page_size) const {
3587 // Get a context
3588 MySqlHostContextAlloc get_context(*impl_);
3589 MySqlHostContextPtr ctx = get_context.ctx_;
3590
3591 // Set up the WHERE clause value
3592 MYSQL_BIND inbind[2];
3593 memset(inbind, 0, sizeof(inbind));
3594
3595 // Bind lower host id
3596 uint32_t host_id = lower_host_id;
3597 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3598 inbind[0].buffer = reinterpret_cast<char*>(&host_id);
3599 inbind[0].is_unsigned = MLM_TRUE;
3600
3601 // Bind page size value
3602 uint32_t page_size_data = page_size.page_size_;
3603 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3604 inbind[1].buffer = reinterpret_cast<char*>(&page_size_data);
3605 inbind[1].is_unsigned = MLM_TRUE;
3606
3607 ConstHostCollection result;
3608 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_PAGE4, inbind,
3609 ctx->host_ipv4_exchange_, result, false);
3610
3611 return (result);
3612}
3613
3615MySqlHostDataSource::getPage6(size_t& /*source_index*/,
3616 uint64_t lower_host_id,
3617 const HostPageSize& page_size) const {
3618 // Get a context
3619 MySqlHostContextAlloc get_context(*impl_);
3620 MySqlHostContextPtr ctx = get_context.ctx_;
3621
3622 // Set up the WHERE clause value
3623 MYSQL_BIND inbind[2];
3624 memset(inbind, 0, sizeof(inbind));
3625
3626 // Bind lower host id
3627 uint32_t host_id = lower_host_id;
3628 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3629 inbind[0].buffer = reinterpret_cast<char*>(&host_id);
3630 inbind[0].is_unsigned = MLM_TRUE;
3631
3632 // Bind page size value
3633 uint32_t page_size_data = page_size.page_size_;
3634 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3635 inbind[1].buffer = reinterpret_cast<char*>(&page_size_data);
3636 inbind[1].is_unsigned = MLM_TRUE;
3637
3638 ConstHostCollection result;
3639 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_PAGE6, inbind,
3640 ctx->host_ipv6_exchange_, result, false);
3641
3642 return (result);
3643}
3644
3647 // Get a context
3648 MySqlHostContextAlloc get_context(*impl_);
3649 MySqlHostContextPtr ctx = get_context.ctx_;
3650
3651 // Set up the WHERE clause value
3652 MYSQL_BIND inbind[1];
3653 memset(inbind, 0, sizeof(inbind));
3654
3655 uint32_t addr4 = address.toUint32();
3656 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3657 inbind[0].buffer = reinterpret_cast<char*>(&addr4);
3658 inbind[0].is_unsigned = MLM_TRUE;
3659
3660 ConstHostCollection result;
3661 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_ADDR, inbind,
3662 ctx->host_ipv4_exchange_, result, false);
3663
3664 return (result);
3665}
3666
3669 const Host::IdentifierType& identifier_type,
3670 const uint8_t* identifier_begin,
3671 const size_t identifier_len) const {
3672 // Get a context
3673 MySqlHostContextAlloc get_context(*impl_);
3674 MySqlHostContextPtr ctx = get_context.ctx_;
3675
3676 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3678 ctx->host_ipv4_exchange_));
3679}
3680
3683 const asiolink::IOAddress& address) const {
3684 if (!address.isV4()) {
3685 isc_throw(BadValue, "MySqlHostDataSource::get4(id, address): "
3686 "wrong address type, address supplied is an IPv6 address");
3687 }
3688
3689 // Get a context
3690 MySqlHostContextAlloc get_context(*impl_);
3691 MySqlHostContextPtr ctx = get_context.ctx_;
3692
3693 // Set up the WHERE clause value
3694 MYSQL_BIND inbind[2];
3695 uint32_t subnet = subnet_id;
3696 memset(inbind, 0, sizeof(inbind));
3697 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3698 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3699 inbind[0].is_unsigned = MLM_TRUE;
3700
3701 uint32_t addr4 = address.toUint32();
3702 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3703 inbind[1].buffer = reinterpret_cast<char*>(&addr4);
3704 inbind[1].is_unsigned = MLM_TRUE;
3705
3706 ConstHostCollection collection;
3707 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID_ADDR, inbind,
3708 ctx->host_ipv4_exchange_, collection, true);
3709
3710 // Return single record if present, else clear the host.
3711 ConstHostPtr result;
3712 if (!collection.empty()) {
3713 result = *collection.begin();
3714 }
3715
3716 return (result);
3717}
3718
3721 const asiolink::IOAddress& address) const {
3722 if (!address.isV4()) {
3723 isc_throw(BadValue, "MySqlHostDataSource::getAll4(id, address): "
3724 "wrong address type, address supplied is an IPv6 address");
3725 }
3726
3727 // Get a context
3728 MySqlHostContextAlloc get_context(*impl_);
3729 MySqlHostContextPtr ctx = get_context.ctx_;
3730
3731 // Set up the WHERE clause value
3732 MYSQL_BIND inbind[2];
3733 uint32_t subnet = subnet_id;
3734 memset(inbind, 0, sizeof(inbind));
3735 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3736 inbind[0].buffer = reinterpret_cast<char*>(&subnet);
3737 inbind[0].is_unsigned = MLM_TRUE;
3738
3739 uint32_t addr4 = address.toUint32();
3740 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3741 inbind[1].buffer = reinterpret_cast<char*>(&addr4);
3742 inbind[1].is_unsigned = MLM_TRUE;
3743
3744 ConstHostCollection collection;
3745 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID_ADDR, inbind,
3746 ctx->host_ipv4_exchange_, collection, false);
3747 return (collection);
3748}
3749
3752 const Host::IdentifierType& identifier_type,
3753 const uint8_t* identifier_begin,
3754 const size_t identifier_len) const {
3755 // Get a context
3756 MySqlHostContextAlloc get_context(*impl_);
3757 MySqlHostContextPtr ctx = get_context.ctx_;
3758
3759 return (impl_->getHost(ctx, subnet_id, identifier_type, identifier_begin, identifier_len,
3761 ctx->host_ipv6_exchange_));
3762}
3763
3766 const uint8_t prefix_len) const {
3767 if (!prefix.isV6()) {
3768 isc_throw(BadValue, "MySqlHostDataSource::get6(prefix, prefix_len): "
3769 "wrong address type, address supplied is an IPv4 address");
3770 }
3771
3772 // Get a context
3773 MySqlHostContextAlloc get_context(*impl_);
3774 MySqlHostContextPtr ctx = get_context.ctx_;
3775
3776 // Set up the WHERE clause value
3777 MYSQL_BIND inbind[2];
3778 memset(inbind, 0, sizeof(inbind));
3779
3780 std::string addr6 = prefix.toText();
3781 unsigned long addr6_length = addr6.size();
3782
3783 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3784 inbind[0].buffer = reinterpret_cast<char*>
3785 (const_cast<char*>(addr6.c_str()));
3786 inbind[0].length = &addr6_length;
3787 inbind[0].buffer_length = addr6_length;
3788
3789 uint8_t tmp = prefix_len;
3790 inbind[1].buffer_type = MYSQL_TYPE_TINY;
3791 inbind[1].buffer = reinterpret_cast<char*>(&tmp);
3792 inbind[1].is_unsigned = MLM_TRUE;
3793
3794 ConstHostCollection collection;
3795 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_PREFIX, inbind,
3796 ctx->host_ipv6_exchange_, collection, true);
3797
3798 // Return single record if present, else clear the host.
3799 ConstHostPtr result;
3800 if (!collection.empty()) {
3801 result = *collection.begin();
3802 }
3803
3804 return (result);
3805}
3806
3809 const asiolink::IOAddress& address) const {
3810 if (!address.isV6()) {
3811 isc_throw(BadValue, "MySqlHostDataSource::get6(id, address): "
3812 "wrong address type, address supplied is an IPv4 address");
3813 }
3814
3815 // Get a context
3816 MySqlHostContextAlloc get_context(*impl_);
3817 MySqlHostContextPtr ctx = get_context.ctx_;
3818
3819 // Set up the WHERE clause value
3820 MYSQL_BIND inbind[2];
3821 memset(inbind, 0, sizeof(inbind));
3822
3823 uint32_t subnet_buffer = static_cast<uint32_t>(subnet_id);
3824 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3825 inbind[0].buffer = reinterpret_cast<char*>(&subnet_buffer);
3826 inbind[0].is_unsigned = MLM_TRUE;
3827
3828 std::string addr6 = address.toText();
3829 unsigned long addr6_length = addr6.size();
3830
3831 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3832 inbind[1].buffer = reinterpret_cast<char*>
3833 (const_cast<char*>(addr6.c_str()));
3834 inbind[1].length = &addr6_length;
3835 inbind[1].buffer_length = addr6_length;
3836
3837 ConstHostCollection collection;
3838 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID6_ADDR, inbind,
3839 ctx->host_ipv6_exchange_, collection, true);
3840
3841 // Return single record if present, else clear the host.
3842 ConstHostPtr result;
3843 if (!collection.empty()) {
3844 result = *collection.begin();
3845 }
3846
3847 return (result);
3848}
3849
3852 const asiolink::IOAddress& address) const {
3853 if (!address.isV6()) {
3854 isc_throw(BadValue, "MySqlHostDataSource::getAll6(id, address): "
3855 "wrong address type, address supplied is an IPv4 address");
3856 }
3857
3858 // Get a context
3859 MySqlHostContextAlloc get_context(*impl_);
3860 MySqlHostContextPtr ctx = get_context.ctx_;
3861
3862 // Set up the WHERE clause value
3863 MYSQL_BIND inbind[2];
3864 memset(inbind, 0, sizeof(inbind));
3865
3866 uint32_t subnet_buffer = static_cast<uint32_t>(subnet_id);
3867 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3868 inbind[0].buffer = reinterpret_cast<char*>(&subnet_buffer);
3869 inbind[0].is_unsigned = MLM_TRUE;
3870
3871 std::string addr6 = address.toText();
3872 unsigned long addr6_length = addr6.size();
3873
3874 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3875 inbind[1].buffer = reinterpret_cast<char*>
3876 (const_cast<char*>(addr6.c_str()));
3877 inbind[1].length = &addr6_length;
3878 inbind[1].buffer_length = addr6_length;
3879
3880 ConstHostCollection collection;
3881 impl_->getHostCollection(ctx, MySqlHostDataSourceImpl::GET_HOST_SUBID6_ADDR, inbind,
3882 ctx->host_ipv6_exchange_, collection, false);
3883 return (collection);
3884}
3885
3886// Miscellaneous database methods.
3887
3888std::string
3890 std::string name = "";
3891 // Get a context
3892 MySqlHostContextAlloc get_context(*impl_);
3893 MySqlHostContextPtr ctx = get_context.ctx_;
3894
3895 try {
3896 name = ctx->conn_.getParameter("name");
3897 } catch (...) {
3898 // Return an empty name
3899 }
3900 return (name);
3901}
3902
3903std::string
3905 return (std::string("Host data source that stores host information"
3906 "in MySQL database"));
3907}
3908
3909std::pair<uint32_t, uint32_t>
3911 return(impl_->getVersion());
3912}
3913
3914void
3916 // Get a context
3917 MySqlHostContextAlloc get_context(*impl_);
3918 MySqlHostContextPtr ctx = get_context.ctx_;
3919
3920 // If operating in read-only mode, throw exception.
3921 impl_->checkReadOnly(ctx);
3922 if (ctx->conn_.isTransactionStarted()) {
3923 ctx->conn_.commit();
3924 }
3925}
3926
3927void
3929 // Get a context
3930 MySqlHostContextAlloc get_context(*impl_);
3931 MySqlHostContextPtr ctx = get_context.ctx_;
3932
3933 // If operating in read-only mode, throw exception.
3934 impl_->checkReadOnly(ctx);
3935 if (ctx->conn_.isTransactionStarted()) {
3936 ctx->conn_.rollback();
3937 }
3938}
3939
3940bool
3942 impl_->ip_reservations_unique_ = unique;
3943 return (true);
3944}
3945
3946bool
3948 return (impl_->unusable_);
3949}
3950
3951} // namespace dhcp
3952} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
A standard Data module exception that is thrown if a parse error is encountered when constructing an ...
Definition: data.h:47
Data is truncated.
Definition: db_exceptions.h:35
static bool invokeDbLostCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool invokeDbFailedCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static bool invokeDbRecoveredCallback(const ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Database duplicate entry error.
Definition: db_exceptions.h:42
Multiple lease records found where one expected.
Definition: db_exceptions.h:28
Common MySQL Connector Pool.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap &parameters)
Get the schema version.
Fetch and Release MySQL Results.
RAII object representing MySQL transaction.
void commit()
Commits transaction.
Attempt to modify data in read-only database.
Definition: db_exceptions.h:56
Authentication keys.
Definition: host.h:75
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:161
static bool delBackend(const std::string &db_type)
Delete an alternate host backend (aka host data source).
Definition: host_mgr.cc:53
static void addBackend(const std::string &access)
Add an alternate host backend (aka host data source).
Definition: host_mgr.cc:48
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
Definition: host_mgr.h:643
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.
Definition: host.h:297
IdentifierType
Type of the host identifier.
Definition: host.h:307
IPv6 reservation for a host.
Definition: host.h:161
const asiolink::IOAddress & getPrefix() const
Returns prefix for the reservation.
Definition: host.h:190
Type getType() const
Returns reservation type.
Definition: host.h:204
Type
Type of the reservation.
Definition: host.h:167
uint8_t getPrefixLen() const
Returns prefix length.
Definition: host.h:195
std::vector< MySqlHostContextPtr > pool_
The vector of available contexts.
std::mutex mutex_
The mutex to protect pool access.
MySqlHostContext(const DatabaseConnection::ParameterMap &parameters, IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
boost::shared_ptr< MySqlHostIPv6Exchange > host_ipv46_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< MySqlHostIPv6Exchange > host_ipv6_exchange_
Pointer to an object representing an exchange which can be used to retrieve hosts,...
boost::shared_ptr< MySqlIPv6ReservationExchange > host_ipv6_reservation_exchange_
Pointer to an object representing an exchange which can be used to insert new IPv6 reservation.
boost::shared_ptr< MySqlHostWithOptionsExchange > host_ipv4_exchange_
The exchange objects are used for transfer of data to/from the database.
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...
MySqlConnection conn_
MySQL connection.
bool is_readonly_
Indicates if the database is opened in read only mode.
Implementation of the MySqlHostDataSource.
void checkReadOnly(MySqlHostContextPtr &ctx) const
Throws exception if database is read only.
void checkError(MySqlHostContextPtr &ctx, const int status, const StatementIndex index, const char *what) const
Check Error and Throw Exception.
std::string timer_name_
Timer name used to register database reconnect timer.
std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
DatabaseConnection::ParameterMap parameters_
The parameters.
MySqlHostContextPtr createContext() const
Create a new context.
bool delStatement(MySqlHostContextPtr &ctx, StatementIndex stindex, MYSQL_BIND *bind)
Executes statements that delete records.
MySqlHostDataSourceImpl(const DatabaseConnection::ParameterMap &parameters)
Constructor.
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.
void addStatement(MySqlHostContextPtr &ctx, MySqlHostDataSourceImpl::StatementIndex stindex, std::vector< MYSQL_BIND > &bind)
Executes statements which inserts a row into one of the tables.
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 ...
bool ip_reservations_unique_
Holds the setting whether the IP reservations must be unique or may be non-unique.
void addOptions(MySqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
Inserts multiple options into the database.
MySqlHostContextPoolPtr pool_
The pool of contexts.
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.
bool unusable_
Indicates if there is at least one connection that can no longer be used for normal operations.
void addResv(MySqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
Inserts IPv6 Reservation into ipv6_reservation table.
static bool dbReconnect(ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the host DB backend manager.
static const StatementIndex WRITE_STMTS_BEGIN
Index of first statement performing write to the database.
MySqlHostContextAlloc(MySqlHostDataSourceImpl &mgr)
Constructor.
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 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 void commit()
Commit Transactions.
virtual bool del(const SubnetID &subnet_id, const asiolink::IOAddress &addr)
Attempts to delete hosts by (subnet-id, address)
virtual ConstHostCollection getAll4(const SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
virtual std::pair< uint32_t, uint32_t > getVersion() const
Returns backend version.
virtual ConstHostCollection getAll6(const SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
virtual std::string getDescription() const
Returns description of the backend.
MySqlHostDataSource(const db::DatabaseConnection::ParameterMap &parameters)
Constructor.
virtual ~MySqlHostDataSource()
Virtual destructor.
virtual ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
virtual isc::db::DatabaseConnection::ParameterMap getParameters() const
Return backend parameters.
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 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 ConstHostCollection getAllbyHostname6(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
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 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 bool isUnusable()
Flag which indicates if the host manager has at least one unusable connection.
virtual ConstHostCollection getAllbyHostname4(const std::string &hostname, const SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.
virtual void rollback()
Rollback Transactions.
virtual std::string getName() const
Returns backend name.
virtual void add(const HostPtr &host)
Adds a new host to the collection.
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 setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
Option descriptor.
Definition: cfg_option.h:42
OptionPtr option_
Option instance.
Definition: cfg_option.h:45
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:66
bool persistent_
Persistence flag.
Definition: cfg_option.h:51
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
Definition: timer_mgr.cc:449
RAII class creating a critical section.
A template representing an optional value.
Definition: optional.h:36
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition: optional.h:121
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
#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.
Definition: macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
const my_bool MLM_FALSE
MySQL false value.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
const uint32_t MYSQL_SCHEMA_VERSION_MAJOR
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
@ error
Definition: db_log.h:118
const uint32_t MYSQL_SCHEMA_VERSION_MINOR
const my_bool MLM_TRUE
MySQL true value.
bool my_bool
my_bool type in MySQL 8.x.
const int MLM_MYSQL_FETCH_FAILURE
MySQL fetch failure code.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
IPv6ResrvCollection::const_iterator IPv6ResrvIterator
Definition: host.h:241
const size_t OPTION_FORMATTED_VALUE_MAX_LEN
Maximum length of option value specified in textual format.
Definition: host.h:48
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_GET_VERSION
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:706
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
Definition: host.h:785
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
Definition: host.h:791
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
const size_t CLIENT_CLASSES_MAX_LEN
Maximum length of classes stored in a dhcp4/6_client_classes columns.
Definition: host.h:36
std::pair< IPv6ResrvIterator, IPv6ResrvIterator > IPv6ResrvRange
Definition: host.h:243
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
Definition: dhcpsrv_log.h:38
const size_t TEXT_AUTH_KEY_LEN
Maximum length of authentication keys (coded in hexadecimal).
Definition: host.h:66
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_FAILED
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
uint64_t HostID
HostID (used only when storing in MySQL, PostgreSQL or Cassandra)
Definition: host.h:69
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:272
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
Definition: host.h:788
boost::shared_ptr< MySqlHostContextPool > MySqlHostContextPoolPtr
Type of pointers to context pools.
boost::shared_ptr< MySqlHostContext > MySqlHostContextPtr
Type of pointers to contexts.
const size_t USER_CONTEXT_MAX_LEN
Maximum length of user context.
Definition: host.h:54
const size_t OPTION_VALUE_MAX_LEN
Maximum length of option value.
Definition: host.h:45
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
const size_t SERVER_HOSTNAME_MAX_LEN
Maximum length of the server hostname.
Definition: host.h:57
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_READONLY
const size_t BOOT_FILE_NAME_MAX_LEN
Maximum length of the boot file name.
Definition: host.h:60
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
const isc::log::MessageID DHCPSRV_MYSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
const size_t OPTION_SPACE_MAX_LEN
Maximum length of option space name.
Definition: host.h:51
Definition: edns.h:19
Defines the logger used by the top-level component of kea-lfc.
uint16_t code_
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24