21#include <boost/array.hpp>
22#include <boost/make_shared.hpp>
23#include <boost/static_assert.hpp>
24#include <mysqld_error.h>
87const size_t LIMITS_TEXT_MAX_LEN = 512;
89boost::array<TaggedStatement, MySqlLeaseMgr::NUM_STATEMENTS>
90tagged_statements = { {
92 "DELETE FROM lease4 WHERE address = ? AND expire = ?"},
95 "WHERE state = ? AND expire < ?"},
97 "DELETE FROM lease6 WHERE address = ? AND expire = ?"},
100 "WHERE state = ? AND expire < ?"},
102 "SELECT address, hwaddr, client_id, "
103 "valid_lifetime, expire, subnet_id, "
104 "fqdn_fwd, fqdn_rev, hostname, "
105 "state, user_context, relay_id, remote_id, pool_id "
108 "SELECT address, hwaddr, client_id, "
109 "valid_lifetime, expire, subnet_id, "
110 "fqdn_fwd, fqdn_rev, hostname, "
111 "state, user_context, relay_id, remote_id, pool_id "
113 "WHERE address = ?"},
115 "SELECT address, hwaddr, client_id, "
116 "valid_lifetime, expire, subnet_id, "
117 "fqdn_fwd, fqdn_rev, hostname, "
118 "state, user_context, relay_id, remote_id, pool_id "
120 "WHERE client_id = ?"},
122 "SELECT address, hwaddr, client_id, "
123 "valid_lifetime, expire, subnet_id, "
124 "fqdn_fwd, fqdn_rev, hostname, "
125 "state, user_context, relay_id, remote_id, pool_id "
127 "WHERE client_id = ? AND subnet_id = ?"},
129 "SELECT address, hwaddr, client_id, "
130 "valid_lifetime, expire, subnet_id, "
131 "fqdn_fwd, fqdn_rev, hostname, "
132 "state, user_context, relay_id, remote_id, pool_id "
136 "SELECT address, hwaddr, client_id, "
137 "valid_lifetime, expire, subnet_id, "
138 "fqdn_fwd, fqdn_rev, hostname, "
139 "state, user_context, relay_id, remote_id, pool_id "
141 "WHERE hwaddr = ? AND subnet_id = ?"},
143 "SELECT address, hwaddr, client_id, "
144 "valid_lifetime, expire, subnet_id, "
145 "fqdn_fwd, fqdn_rev, hostname, "
146 "state, user_context, relay_id, remote_id, pool_id "
152 "SELECT address, hwaddr, client_id, "
153 "valid_lifetime, expire, subnet_id, "
154 "fqdn_fwd, fqdn_rev, hostname, "
155 "state, user_context, relay_id, remote_id, pool_id "
157 "WHERE address > ? AND user_context IS NOT NULL "
161 "SELECT address, hwaddr, client_id, "
162 "valid_lifetime, expire, subnet_id, "
163 "fqdn_fwd, fqdn_rev, hostname, "
164 "state, user_context, relay_id, remote_id, pool_id "
166 "WHERE subnet_id = ?"},
168 "SELECT address, hwaddr, client_id, "
169 "valid_lifetime, expire, subnet_id, "
170 "fqdn_fwd, fqdn_rev, hostname, "
171 "state, user_context, relay_id, remote_id, pool_id "
173 "WHERE hostname = ?"},
175 "SELECT address, hwaddr, client_id, "
176 "valid_lifetime, expire, subnet_id, "
177 "fqdn_fwd, fqdn_rev, hostname, "
178 "state, user_context, relay_id, remote_id, pool_id "
181 "AND valid_lifetime != 4294967295 "
183 "ORDER BY expire ASC "
186 "SELECT address, hwaddr, client_id, "
187 "valid_lifetime, expire, subnet_id, "
188 "fqdn_fwd, fqdn_rev, hostname, "
189 "state, user_context, relay_id, remote_id, pool_id "
191 "WHERE relay_id = ? and address > ? "
195 "SELECT address, hwaddr, client_id, "
196 "valid_lifetime, expire, subnet_id, "
197 "fqdn_fwd, fqdn_rev, hostname, "
198 "state, user_context, relay_id, remote_id, pool_id "
200 "WHERE relay_id = ? and address > ? "
201 " and UNIX_TIMESTAMP(expire) - IF"
202 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
207 "SELECT address, hwaddr, client_id, "
208 "valid_lifetime, expire, subnet_id, "
209 "fqdn_fwd, fqdn_rev, hostname, "
210 "state, user_context, relay_id, remote_id, pool_id "
212 "WHERE relay_id = ? and address > ? "
213 " and UNIX_TIMESTAMP(expire) - IF"
214 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
216 " and UNIX_TIMESTAMP(expire) - IF"
217 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
222 "SELECT address, hwaddr, client_id, "
223 "valid_lifetime, expire, subnet_id, "
224 "fqdn_fwd, fqdn_rev, hostname, "
225 "state, user_context, relay_id, remote_id, pool_id "
227 "WHERE relay_id = ? and address > ? "
228 " and UNIX_TIMESTAMP(expire) - IF"
229 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
234 "SELECT address, hwaddr, client_id, "
235 "valid_lifetime, expire, subnet_id, "
236 "fqdn_fwd, fqdn_rev, hostname, "
237 "state, user_context, relay_id, remote_id, pool_id "
239 "WHERE remote_id = ? and address > ? "
243 "SELECT address, hwaddr, client_id, "
244 "valid_lifetime, expire, subnet_id, "
245 "fqdn_fwd, fqdn_rev, hostname, "
246 "state, user_context, relay_id, remote_id, pool_id "
248 "WHERE remote_id = ? and address > ? "
249 " and UNIX_TIMESTAMP(expire) - IF"
250 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
255 "SELECT address, hwaddr, client_id, "
256 "valid_lifetime, expire, subnet_id, "
257 "fqdn_fwd, fqdn_rev, hostname, "
258 "state, user_context, relay_id, remote_id, pool_id "
260 "WHERE remote_id = ? and address > ? "
261 " and UNIX_TIMESTAMP(expire) - IF"
262 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
264 " and UNIX_TIMESTAMP(expire) - IF"
265 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
270 "SELECT address, hwaddr, client_id, "
271 "valid_lifetime, expire, subnet_id, "
272 "fqdn_fwd, fqdn_rev, hostname, "
273 "state, user_context, relay_id, remote_id, pool_id "
275 "WHERE remote_id = ? and address > ? "
276 " and UNIX_TIMESTAMP(expire) - IF"
277 "(valid_lifetime = 4294967295, 0, valid_lifetime)"
282 "SELECT address, duid, valid_lifetime, "
283 "expire, subnet_id, pref_lifetime, "
284 "lease_type, iaid, prefix_len, "
285 "fqdn_fwd, fqdn_rev, hostname, "
286 "hwaddr, hwtype, hwaddr_source, "
287 "state, user_context, pool_id "
290 "SELECT address, duid, valid_lifetime, "
291 "expire, subnet_id, pref_lifetime, "
292 "lease_type, iaid, prefix_len, "
293 "fqdn_fwd, fqdn_rev, hostname, "
294 "hwaddr, hwtype, hwaddr_source, "
295 "state, user_context, pool_id "
297 "WHERE address = ? AND lease_type = ?"},
299 "SELECT address, duid, valid_lifetime, "
300 "expire, subnet_id, pref_lifetime, "
301 "lease_type, iaid, prefix_len, "
302 "fqdn_fwd, fqdn_rev, hostname, "
303 "hwaddr, hwtype, hwaddr_source, "
304 "state, user_context, pool_id "
306 "WHERE duid = ? AND iaid = ? AND lease_type = ?"},
308 "SELECT address, duid, valid_lifetime, "
309 "expire, subnet_id, pref_lifetime, "
310 "lease_type, iaid, prefix_len, "
311 "fqdn_fwd, fqdn_rev, hostname, "
312 "hwaddr, hwtype, hwaddr_source, "
313 "state, user_context, pool_id "
315 "WHERE duid = ? AND iaid = ? AND subnet_id = ? "
316 "AND lease_type = ?"},
318 "SELECT address, duid, valid_lifetime, "
319 "expire, subnet_id, pref_lifetime, "
320 "lease_type, iaid, prefix_len, "
321 "fqdn_fwd, fqdn_rev, hostname, "
322 "hwaddr, hwtype, hwaddr_source, "
323 "state, user_context, pool_id "
329 "SELECT address, duid, valid_lifetime, "
330 "expire, subnet_id, pref_lifetime, "
331 "lease_type, iaid, prefix_len, "
332 "fqdn_fwd, fqdn_rev, hostname, "
333 "hwaddr, hwtype, hwaddr_source, "
334 "state, user_context, pool_id "
336 "WHERE address > ? AND user_context IS NOT NULL "
340 "SELECT address, duid, valid_lifetime, "
341 "expire, subnet_id, pref_lifetime, "
342 "lease_type, iaid, prefix_len, "
343 "fqdn_fwd, fqdn_rev, hostname, "
344 "hwaddr, hwtype, hwaddr_source, "
345 "state, user_context, pool_id "
347 "WHERE subnet_id = ?"},
349 "SELECT address, duid, valid_lifetime, "
350 "expire, subnet_id, pref_lifetime, "
351 "lease_type, iaid, prefix_len, "
352 "fqdn_fwd, fqdn_rev, hostname, "
353 "hwaddr, hwtype, hwaddr_source, "
354 "state, user_context, pool_id "
356 "WHERE subnet_id = ? AND address > ? "
360 "SELECT address, duid, valid_lifetime, "
361 "expire, subnet_id, pref_lifetime, "
362 "lease_type, iaid, prefix_len, "
363 "fqdn_fwd, fqdn_rev, hostname, "
364 "hwaddr, hwtype, hwaddr_source, "
365 "state, user_context, pool_id "
369 "SELECT address, duid, valid_lifetime, "
370 "expire, subnet_id, pref_lifetime, "
371 "lease_type, iaid, prefix_len, "
372 "fqdn_fwd, fqdn_rev, hostname, "
373 "hwaddr, hwtype, hwaddr_source, "
374 "state, user_context, pool_id "
376 "WHERE hostname = ?"},
378 "SELECT address, duid, valid_lifetime, "
379 "expire, subnet_id, pref_lifetime, "
380 "lease_type, iaid, prefix_len, "
381 "fqdn_fwd, fqdn_rev, hostname, "
382 "hwaddr, hwtype, hwaddr_source, "
383 "state, user_context, pool_id "
386 "AND valid_lifetime != 4294967295 "
388 "ORDER BY expire ASC "
391 "INSERT INTO lease4(address, hwaddr, client_id, "
392 "valid_lifetime, expire, subnet_id, "
393 "fqdn_fwd, fqdn_rev, hostname, "
394 "state, user_context, relay_id, remote_id, pool_id) "
395 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
397 "INSERT INTO lease6(address, duid, valid_lifetime, "
398 "expire, subnet_id, pref_lifetime, "
399 "lease_type, iaid, prefix_len, "
400 "fqdn_fwd, fqdn_rev, hostname, "
401 "hwaddr, hwtype, hwaddr_source, "
402 "state, user_context, pool_id) "
403 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
405 "UPDATE lease4 SET address = ?, hwaddr = ?, "
406 "client_id = ?, valid_lifetime = ?, expire = ?, "
407 "subnet_id = ?, fqdn_fwd = ?, fqdn_rev = ?, "
409 "state = ?, user_context = ?, "
410 "relay_id = ?, remote_id = ?, pool_id = ? "
411 "WHERE address = ? AND expire = ?"},
413 "UPDATE lease6 SET address = ?, duid = ?, "
414 "valid_lifetime = ?, expire = ?, subnet_id = ?, "
415 "pref_lifetime = ?, lease_type = ?, iaid = ?, "
416 "prefix_len = ?, fqdn_fwd = ?, fqdn_rev = ?, "
417 "hostname = ?, hwaddr = ?, hwtype = ?, hwaddr_source = ?, "
418 "state = ?, user_context = ?, pool_id = ? "
419 "WHERE address = ? AND expire = ?"},
421 "SELECT subnet_id, state, leases as state_count "
422 "FROM lease4_stat ORDER BY subnet_id, state"},
424 "SELECT subnet_id, state, leases as state_count "
426 "WHERE subnet_id = ? "
429 "SELECT subnet_id, state, leases as state_count "
431 "WHERE subnet_id >= ? and subnet_id <= ? "
432 "ORDER BY subnet_id, state"},
434 "SELECT subnet_id, pool_id, state, leases as state_count "
435 "FROM lease4_pool_stat ORDER BY subnet_id, pool_id, state"},
437 "SELECT subnet_id, lease_type, state, leases as state_count "
438 "FROM lease6_stat ORDER BY subnet_id, lease_type, state"},
440 "SELECT subnet_id, lease_type, state, leases as state_count "
442 "WHERE subnet_id = ? "
443 "ORDER BY lease_type, state"},
445 "SELECT subnet_id, lease_type, state, leases as state_count "
447 "WHERE subnet_id >= ? and subnet_id <= ? "
448 "ORDER BY subnet_id, lease_type, state"},
450 "SELECT subnet_id, pool_id, lease_type, state, leases as state_count "
451 "FROM lease6_pool_stat ORDER BY subnet_id, pool_id, lease_type, state"},
453 "SELECT checkLease4Limits(?)"},
455 "SELECT checkLease6Limits(?)"},
457 "SELECT isJsonSupported()"},
460 "FROM lease4_stat_by_client_class "
461 "WHERE client_class = ?"},
464 "FROM lease6_stat_by_client_class "
465 "WHERE client_class = ? AND lease_type = ?"},
467 "DELETE FROM lease6_relay_id"},
469 "DELETE FROM lease6_remote_id"},
471 "DELETE FROM lease6_relay_id WHERE lease_addr = ?"},
473 "DELETE FROM lease6_remote_id WHERE lease_addr = ?"},
475 "INSERT INTO lease6_relay_id(relay_id, lease_addr) "
478 "INSERT INTO lease6_remote_id(remote_id, lease_addr) "
481 "SELECT l.address, l.duid, l.valid_lifetime, "
482 "l.expire, l.subnet_id, l.pref_lifetime, "
483 "l.lease_type, l.iaid, l.prefix_len, "
484 "l.fqdn_fwd, l.fqdn_rev, l.hostname, "
485 "l.hwaddr, l.hwtype, l.hwaddr_source, "
486 "l.state, l.user_context, l.pool_id "
488 "INNER JOIN lease6_relay_id AS r "
489 " ON l.address = r.lease_addr "
490 " WHERE r.relay_id = ? AND r.lease_addr > ? "
491 "GROUP BY l.address "
492 "ORDER BY l.address "
495 "SELECT l.address, l.duid, l.valid_lifetime, "
496 "l.expire, l.subnet_id, l.pref_lifetime, "
497 "l.lease_type, l.iaid, l.prefix_len, "
498 "l.fqdn_fwd, l.fqdn_rev, l.hostname, "
499 "l.hwaddr, l.hwtype, l.hwaddr_source, "
500 "l.state, l.user_context, l.pool_id "
502 "INNER JOIN lease6_remote_id AS r "
503 " ON l.address = r.lease_addr "
504 " WHERE r.remote_id = ? AND r.lease_addr > ? "
505 "GROUP BY l.address "
506 "ORDER BY l.address "
509 "SELECT COUNT(*) FROM lease6_relay_id"},
511 "SELECT COUNT(*) FROM lease6_remote_id"},
542 for (
size_t i = 0; i < count; ++i) {
544 bind[i].error = &
error[i];
563 std::string result =
"";
566 for (
size_t i = 0; i < count; ++i) {
568 if (!result.empty()) {
575 if (result.empty()) {
601 static const size_t ADDRESS_COL = 0;
602 static const size_t HWADDR_COL = 1;
603 static const size_t CLIENT_ID_COL = 2;
604 static const size_t VALID_LIFETIME_COL = 3;
605 static const size_t EXPIRE_COL = 4;
606 static const size_t SUBNET_ID_COL = 5;
607 static const size_t FQDN_FWD_COL = 6;
608 static const size_t FQDN_REV_COL = 7;
609 static const size_t HOSTNAME_COL = 8;
610 static const size_t STATE_COL = 9;
611 static const size_t USER_CONTEXT_COL = 10;
612 static const size_t RELAY_ID_COL = 11;
613 static const size_t REMOTE_ID_COL = 12;
614 static const size_t POOL_ID_COL = 13;
617 static const size_t LEASE_COLUMNS = 14;
626 client_id_length_(0), client_id_null_(
MLM_FALSE),
627 subnet_id_(0), pool_id_(0), valid_lifetime_(0),
628 fqdn_fwd_(false), fqdn_rev_(false), hostname_length_(0),
629 state_(0), user_context_length_(0),
630 user_context_null_(
MLM_FALSE), relay_id_length_(0),
631 relay_id_null_(
MLM_FALSE), remote_id_length_(0),
633 memset(hwaddr_buffer_, 0,
sizeof(hwaddr_buffer_));
634 memset(client_id_buffer_, 0,
sizeof(client_id_buffer_));
635 memset(hostname_buffer_, 0,
sizeof(hostname_buffer_));
636 memset(user_context_, 0,
sizeof(user_context_));
637 memset(relay_id_buffer_, 0,
sizeof(relay_id_buffer_));
638 memset(remote_id_buffer_, 0,
sizeof(remote_id_buffer_));
639 std::fill(&error_[0], &error_[LEASE_COLUMNS],
MLM_FALSE);
642 columns_[ADDRESS_COL] =
"address";
643 columns_[HWADDR_COL] =
"hwaddr";
644 columns_[CLIENT_ID_COL] =
"client_id";
645 columns_[VALID_LIFETIME_COL] =
"valid_lifetime";
646 columns_[EXPIRE_COL] =
"expire";
647 columns_[SUBNET_ID_COL] =
"subnet_id";
648 columns_[FQDN_FWD_COL] =
"fqdn_fwd";
649 columns_[FQDN_REV_COL] =
"fqdn_rev";
650 columns_[HOSTNAME_COL] =
"hostname";
651 columns_[STATE_COL] =
"state";
652 columns_[USER_CONTEXT_COL] =
"user_context";
653 columns_[RELAY_ID_COL] =
"relay_id";
654 columns_[REMOTE_ID_COL] =
"remote_id";
655 columns_[POOL_ID_COL] =
"pool_id";
656 BOOST_STATIC_ASSERT(13 < LEASE_COLUMNS);
678 memset(bind_, 0,
sizeof(bind_));
687 addr4_ = lease_->addr_.toUint32();
688 bind_[0].buffer_type = MYSQL_TYPE_LONG;
689 bind_[0].buffer =
reinterpret_cast<char*
>(&addr4_);
697 hwaddr_ = hwaddr->hwaddr_;
698 hwaddr_length_ = hwaddr->hwaddr_.size();
705 if (hwaddr_.empty()) {
709 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
710 bind_[1].buffer =
reinterpret_cast<char*
>(&(hwaddr_[0]));
711 bind_[1].buffer_length = hwaddr_length_;
712 bind_[1].length = &hwaddr_length_;
714 bind_[1].buffer_type = MYSQL_TYPE_NULL;
721 bind_[1].buffer = NULL;
722 bind_[1].is_null = &hwaddr_null_;
726 if (lease_->client_id_) {
727 client_id_ = lease_->client_id_->getClientId();
728 client_id_length_ = client_id_.size();
735 if (client_id_.empty()) {
736 client_id_.resize(1);
739 bind_[2].buffer_type = MYSQL_TYPE_BLOB;
740 bind_[2].buffer =
reinterpret_cast<char*
>(&client_id_[0]);
741 bind_[2].buffer_length = client_id_length_;
742 bind_[2].length = &client_id_length_;
746 bind_[2].buffer_type = MYSQL_TYPE_NULL;
753 bind_[2].buffer = NULL;
754 bind_[2].is_null = &client_id_null_;
758 bind_[3].buffer_type = MYSQL_TYPE_LONG;
759 bind_[3].buffer =
reinterpret_cast<char*
>(&lease_->valid_lft_);
772 uint32_t valid_lft = lease_->valid_lft_;
778 bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
779 bind_[4].buffer =
reinterpret_cast<char*
>(&expire_);
780 bind_[4].buffer_length =
sizeof(expire_);
786 bind_[5].buffer_type = MYSQL_TYPE_LONG;
787 bind_[5].buffer =
reinterpret_cast<char*
>(&lease_->subnet_id_);
793 bind_[6].buffer_type = MYSQL_TYPE_TINY;
794 bind_[6].buffer =
reinterpret_cast<char*
>(&lease_->fqdn_fwd_);
800 bind_[7].buffer_type = MYSQL_TYPE_TINY;
801 bind_[7].buffer =
reinterpret_cast<char*
>(&lease_->fqdn_rev_);
810 bind_[8].buffer_type = MYSQL_TYPE_STRING;
811 bind_[8].buffer =
const_cast<char*
>(lease_->hostname_.c_str());
812 bind_[8].buffer_length = lease_->hostname_.length();
817 bind_[9].buffer_type = MYSQL_TYPE_LONG;
818 bind_[9].buffer =
reinterpret_cast<char*
>(&lease_->state_);
826 bind_[10].buffer_type = MYSQL_TYPE_STRING;
827 std::string ctx_txt = ctx->str();
829 bind_[10].buffer = user_context_;
830 bind_[10].buffer_length = ctx_txt.length();
834 bind_[10].buffer_type = MYSQL_TYPE_NULL;
838 relay_id_ = lease_->relay_id_;
839 if (!relay_id_.empty()) {
840 bind_[11].buffer_type = MYSQL_TYPE_BLOB;
841 bind_[11].buffer =
reinterpret_cast<char*
>(&relay_id_[0]);
842 relay_id_length_ = relay_id_.size();
843 bind_[11].buffer_length = relay_id_length_;
844 bind_[11].length = &relay_id_length_;
846 bind_[11].buffer_type = MYSQL_TYPE_NULL;
848 bind_[11].buffer = NULL;
849 bind_[11].is_null = &relay_id_null_;
853 remote_id_ = lease_->remote_id_;
854 if (!remote_id_.empty()) {
855 bind_[12].buffer_type = MYSQL_TYPE_BLOB;
856 bind_[12].buffer =
reinterpret_cast<char*
>(&remote_id_[0]);
857 remote_id_length_ = remote_id_.size();
858 bind_[12].buffer_length = remote_id_length_;
859 bind_[12].length = &remote_id_length_;
861 bind_[12].buffer_type = MYSQL_TYPE_NULL;
863 bind_[12].buffer = NULL;
864 bind_[12].is_null = &remote_id_null_;
869 bind_[13].buffer_type = MYSQL_TYPE_LONG;
870 bind_[13].buffer =
reinterpret_cast<char*
>(&lease_->pool_id_);
879 BOOST_STATIC_ASSERT(13 < LEASE_COLUMNS);
881 }
catch (
const std::exception& ex) {
883 "Could not create bind array from Lease4: "
884 << lease_->addr_.toText() <<
", reason: " << ex.what());
889 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
904 memset(bind_, 0,
sizeof(bind_));
907 bind_[0].buffer_type = MYSQL_TYPE_LONG;
908 bind_[0].buffer =
reinterpret_cast<char*
>(&addr4_);
915 hwaddr_length_ =
sizeof(hwaddr_buffer_);
916 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
917 bind_[1].buffer =
reinterpret_cast<char*
>(hwaddr_buffer_);
918 bind_[1].buffer_length = hwaddr_length_;
919 bind_[1].length = &hwaddr_length_;
920 bind_[1].is_null = &hwaddr_null_;
923 client_id_length_ =
sizeof(client_id_buffer_);
924 bind_[2].buffer_type = MYSQL_TYPE_BLOB;
925 bind_[2].buffer =
reinterpret_cast<char*
>(client_id_buffer_);
926 bind_[2].buffer_length = client_id_length_;
927 bind_[2].length = &client_id_length_;
928 bind_[2].is_null = &client_id_null_;
933 bind_[3].buffer_type = MYSQL_TYPE_LONG;
934 bind_[3].buffer =
reinterpret_cast<char*
>(&valid_lifetime_);
940 bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
941 bind_[4].buffer =
reinterpret_cast<char*
>(&expire_);
942 bind_[4].buffer_length =
sizeof(expire_);
947 bind_[5].buffer_type = MYSQL_TYPE_LONG;
948 bind_[5].buffer =
reinterpret_cast<char*
>(&subnet_id_);
954 bind_[6].buffer_type = MYSQL_TYPE_TINY;
955 bind_[6].buffer =
reinterpret_cast<char*
>(&fqdn_fwd_);
961 bind_[7].buffer_type = MYSQL_TYPE_TINY;
962 bind_[7].buffer =
reinterpret_cast<char*
>(&fqdn_rev_);
971 hostname_length_ =
sizeof(hostname_buffer_);
972 bind_[8].buffer_type = MYSQL_TYPE_STRING;
973 bind_[8].buffer =
reinterpret_cast<char*
>(hostname_buffer_);
974 bind_[8].buffer_length = hostname_length_;
975 bind_[8].length = &hostname_length_;
980 bind_[9].buffer_type = MYSQL_TYPE_LONG;
981 bind_[9].buffer =
reinterpret_cast<char*
>(&state_);
988 user_context_length_ =
sizeof(user_context_);
989 bind_[10].buffer_type = MYSQL_TYPE_STRING;
990 bind_[10].buffer =
reinterpret_cast<char*
>(user_context_);
991 bind_[10].buffer_length = user_context_length_;
992 bind_[10].length = &user_context_length_;
993 bind_[10].is_null = &user_context_null_;
996 relay_id_length_ =
sizeof(relay_id_buffer_);
997 bind_[11].buffer_type = MYSQL_TYPE_BLOB;
998 bind_[11].buffer =
reinterpret_cast<char*
>(relay_id_buffer_);
999 bind_[11].buffer_length = relay_id_length_;
1000 bind_[11].length = &relay_id_length_;
1001 bind_[11].is_null = &relay_id_null_;
1004 remote_id_length_ =
sizeof(remote_id_buffer_);
1005 bind_[12].buffer_type = MYSQL_TYPE_BLOB;
1006 bind_[12].buffer =
reinterpret_cast<char*
>(remote_id_buffer_);
1007 bind_[12].buffer_length = remote_id_length_;
1008 bind_[12].length = &remote_id_length_;
1009 bind_[12].is_null = &remote_id_null_;
1012 bind_[13].buffer_type = MYSQL_TYPE_LONG;
1013 bind_[13].buffer =
reinterpret_cast<char*
>(&pool_id_);
1022 BOOST_STATIC_ASSERT(13 < LEASE_COLUMNS);
1026 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
1043 uint32_t valid_lft = valid_lifetime_;
1051 client_id_length_ = 0;
1056 std::string hostname(hostname_buffer_,
1057 hostname_buffer_ + hostname_length_);
1066 std::string user_context;
1068 user_context_[user_context_length_] =
'\0';
1069 user_context.assign(user_context_);
1074 if (!user_context.empty()) {
1078 <<
"' is not a JSON map");
1082 Lease4Ptr lease(boost::make_shared<Lease4>(addr4_, hwaddr,
1085 valid_lifetime_, cltt,
1086 subnet_id_, fqdn_fwd_,
1087 fqdn_rev_, hostname));
1090 lease->state_ = state_;
1093 lease->setContext(ctx);
1098 lease->relay_id_.assign(relay_id_buffer_,
1099 relay_id_buffer_ + relay_id_length_);
1104 lease->remote_id_.assign(remote_id_buffer_,
1105 remote_id_buffer_ + remote_id_length_);
1109 lease->pool_id_ = pool_id_;
1134 MYSQL_BIND bind_[LEASE_COLUMNS];
1135 std::string columns_[LEASE_COLUMNS];
1136 my_bool error_[LEASE_COLUMNS];
1138 std::vector<uint8_t> hwaddr_;
1140 unsigned long hwaddr_length_;
1142 std::vector<uint8_t> client_id_;
1144 unsigned long client_id_length_;
1147 uint32_t subnet_id_;
1149 uint32_t valid_lifetime_;
1153 unsigned long hostname_length_;
1156 unsigned long user_context_length_;
1158 std::vector<uint8_t> relay_id_;
1160 unsigned long relay_id_length_;
1162 std::vector<uint8_t> remote_id_;
1164 unsigned long remote_id_length_;
1187 static const size_t ADDRESS_COL = 0;
1188 static const size_t DUID_COL = 1;
1189 static const size_t VALID_LIFETIME_COL = 2;
1190 static const size_t EXPIRE_COL = 3;
1191 static const size_t SUBNET_ID_COL = 4;
1192 static const size_t PREF_LIFETIME_COL = 5;
1193 static const size_t LEASE_TYPE_COL = 6;
1194 static const size_t IAID_COL = 7;
1195 static const size_t PREFIX_LEN_COL = 8;
1196 static const size_t FQDN_FWD_COL = 9;
1197 static const size_t FQDN_REV_COL = 10;
1198 static const size_t HOSTNAME_COL = 11;
1199 static const size_t HWADDR_COL = 12;
1200 static const size_t HWTYPE_COL = 13;
1201 static const size_t HWADDR_SOURCE_COL = 14;
1202 static const size_t STATE_COL = 15;
1203 static const size_t USER_CONTEXT_COL = 16;
1204 static const size_t POOL_ID_COL = 17;
1207 static const size_t LEASE_COLUMNS = 18;
1216 hwaddr_null_(
MLM_FALSE), duid_length_(0),
1217 iaid_(0), lease_type_(0), prefix_len_(0),
1218 pref_lifetime_(0), subnet_id_(0), pool_id_(0),
1219 valid_lifetime_(0), fqdn_fwd_(false), fqdn_rev_(false),
1220 hostname_length_(0), hwtype_(0), hwaddr_source_(0),
1221 state_(0), user_context_length_(0),
1223 memset(addr6_buffer_, 0,
sizeof(addr6_buffer_));
1224 memset(duid_buffer_, 0,
sizeof(duid_buffer_));
1225 memset(hostname_buffer_, 0,
sizeof(hostname_buffer_));
1226 memset(hwaddr_buffer_, 0,
sizeof(hwaddr_buffer_));
1227 memset(user_context_, 0,
sizeof(user_context_));
1228 std::fill(&error_[0], &error_[LEASE_COLUMNS],
MLM_FALSE);
1231 columns_[ADDRESS_COL] =
"address";
1232 columns_[DUID_COL] =
"duid";
1233 columns_[VALID_LIFETIME_COL] =
"valid_lifetime";
1234 columns_[EXPIRE_COL] =
"expire";
1235 columns_[SUBNET_ID_COL] =
"subnet_id";
1236 columns_[PREF_LIFETIME_COL] =
"pref_lifetime";
1237 columns_[LEASE_TYPE_COL] =
"lease_type";
1238 columns_[IAID_COL] =
"iaid";
1239 columns_[PREFIX_LEN_COL] =
"prefix_len";
1240 columns_[FQDN_FWD_COL] =
"fqdn_fwd";
1241 columns_[FQDN_REV_COL] =
"fqdn_rev";
1242 columns_[HOSTNAME_COL] =
"hostname";
1243 columns_[HWADDR_COL] =
"hwaddr";
1244 columns_[HWTYPE_COL] =
"hwtype";
1245 columns_[HWADDR_SOURCE_COL] =
"hwaddr_source";
1246 columns_[STATE_COL] =
"state";
1247 columns_[USER_CONTEXT_COL] =
"user_context";
1248 columns_[POOL_ID_COL] =
"pool_id";
1249 BOOST_STATIC_ASSERT(17 < LEASE_COLUMNS);
1270 memset(bind_, 0,
sizeof(bind_));
1274 addr6_ = lease->addr_.toBytes();
1275 if (addr6_.size() != 16) {
1280 bind_[0].buffer_type = MYSQL_TYPE_BLOB;
1281 bind_[0].buffer =
reinterpret_cast<char*
>(&addr6_[0]);
1282 bind_[0].buffer_length = 16;
1283 bind_[0].length = &addr6_length_;
1288 if (!lease_->duid_) {
1290 <<
" is missing mandatory client-id.");
1292 duid_ = lease_->duid_->getDuid();
1293 duid_length_ = duid_.size();
1295 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
1296 bind_[1].buffer =
reinterpret_cast<char*
>(&(duid_[0]));
1297 bind_[1].buffer_length = duid_length_;
1298 bind_[1].length = &duid_length_;
1303 bind_[2].buffer_type = MYSQL_TYPE_LONG;
1304 bind_[2].buffer =
reinterpret_cast<char*
>(&lease_->valid_lft_);
1317 uint32_t valid_lft = lease_->valid_lft_;
1323 bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
1324 bind_[3].buffer =
reinterpret_cast<char*
>(&expire_);
1325 bind_[3].buffer_length =
sizeof(expire_);
1331 bind_[4].buffer_type = MYSQL_TYPE_LONG;
1332 bind_[4].buffer =
reinterpret_cast<char*
>(&lease_->subnet_id_);
1339 bind_[5].buffer_type = MYSQL_TYPE_LONG;
1340 bind_[5].buffer =
reinterpret_cast<char*
>(&lease_->preferred_lft_);
1347 lease_type_ = lease_->type_;
1348 bind_[6].buffer_type = MYSQL_TYPE_TINY;
1349 bind_[6].buffer =
reinterpret_cast<char*
>(&lease_type_);
1356 bind_[7].buffer_type = MYSQL_TYPE_LONG;
1357 bind_[7].buffer =
reinterpret_cast<char*
>(&lease_->iaid_);
1364 bind_[8].buffer_type = MYSQL_TYPE_TINY;
1365 bind_[8].buffer =
reinterpret_cast<char*
>(&lease_->prefixlen_);
1371 bind_[9].buffer_type = MYSQL_TYPE_TINY;
1372 bind_[9].buffer =
reinterpret_cast<char*
>(&lease_->fqdn_fwd_);
1378 bind_[10].buffer_type = MYSQL_TYPE_TINY;
1379 bind_[10].buffer =
reinterpret_cast<char*
>(&lease_->fqdn_rev_);
1385 bind_[11].buffer_type = MYSQL_TYPE_STRING;
1386 bind_[11].buffer =
const_cast<char*
>(lease_->hostname_.c_str());
1387 bind_[11].buffer_length = lease_->hostname_.length();
1394 hwaddr_ = hwaddr->hwaddr_;
1395 hwaddr_length_ = hwaddr->hwaddr_.size();
1402 if (hwaddr_.empty()) {
1406 bind_[12].buffer_type = MYSQL_TYPE_BLOB;
1407 bind_[12].buffer =
reinterpret_cast<char*
>(&(hwaddr_[0]));
1408 bind_[12].buffer_length = hwaddr_length_;
1409 bind_[12].length = &hwaddr_length_;
1411 bind_[12].buffer_type = MYSQL_TYPE_NULL;
1418 bind_[12].buffer = NULL;
1419 bind_[12].is_null = &hwaddr_null_;
1424 hwtype_ = lease->hwaddr_->htype_;
1425 bind_[13].buffer_type = MYSQL_TYPE_SHORT;
1426 bind_[13].buffer =
reinterpret_cast<char*
>(&hwtype_);
1430 bind_[13].buffer_type = MYSQL_TYPE_NULL;
1437 bind_[13].buffer = NULL;
1438 bind_[13].is_null = &hwaddr_null_;
1443 hwaddr_source_ = lease->hwaddr_->source_;
1444 bind_[14].buffer_type = MYSQL_TYPE_LONG;
1445 bind_[14].buffer =
reinterpret_cast<char*
>(&hwaddr_source_);
1449 bind_[14].buffer_type = MYSQL_TYPE_NULL;
1456 bind_[14].buffer = NULL;
1457 bind_[14].is_null = &hwaddr_null_;
1461 bind_[15].buffer_type = MYSQL_TYPE_LONG;
1462 bind_[15].buffer =
reinterpret_cast<char*
>(&lease_->state_);
1470 bind_[16].buffer_type = MYSQL_TYPE_STRING;
1471 std::string ctx_txt = ctx->str();
1473 bind_[16].buffer = user_context_;
1474 bind_[16].buffer_length = ctx_txt.length();
1478 bind_[16].buffer_type = MYSQL_TYPE_NULL;
1483 bind_[17].buffer_type = MYSQL_TYPE_LONG;
1484 bind_[17].buffer =
reinterpret_cast<char*
>(&lease_->pool_id_);
1493 BOOST_STATIC_ASSERT(17 < LEASE_COLUMNS);
1495 }
catch (
const std::exception& ex) {
1497 "Could not create bind array from Lease6: "
1498 << lease_->addr_.toText() <<
", reason: " << ex.what());
1503 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
1521 memset(bind_, 0,
sizeof(bind_));
1525 bind_[0].buffer_type = MYSQL_TYPE_BLOB;
1526 bind_[0].buffer =
reinterpret_cast<char*
>(addr6_buffer_);
1527 bind_[0].buffer_length = addr6_length_;
1528 bind_[0].length = &addr6_length_;
1533 duid_length_ =
sizeof(duid_buffer_);
1534 bind_[1].buffer_type = MYSQL_TYPE_BLOB;
1535 bind_[1].buffer =
reinterpret_cast<char*
>(duid_buffer_);
1536 bind_[1].buffer_length = duid_length_;
1537 bind_[1].length = &duid_length_;
1542 bind_[2].buffer_type = MYSQL_TYPE_LONG;
1543 bind_[2].buffer =
reinterpret_cast<char*
>(&valid_lifetime_);
1549 bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
1550 bind_[3].buffer =
reinterpret_cast<char*
>(&expire_);
1551 bind_[3].buffer_length =
sizeof(expire_);
1556 bind_[4].buffer_type = MYSQL_TYPE_LONG;
1557 bind_[4].buffer =
reinterpret_cast<char*
>(&subnet_id_);
1563 bind_[5].buffer_type = MYSQL_TYPE_LONG;
1564 bind_[5].buffer =
reinterpret_cast<char*
>(&pref_lifetime_);
1570 bind_[6].buffer_type = MYSQL_TYPE_TINY;
1571 bind_[6].buffer =
reinterpret_cast<char*
>(&lease_type_);
1577 bind_[7].buffer_type = MYSQL_TYPE_LONG;
1578 bind_[7].buffer =
reinterpret_cast<char*
>(&iaid_);
1584 bind_[8].buffer_type = MYSQL_TYPE_TINY;
1585 bind_[8].buffer =
reinterpret_cast<char*
>(&prefix_len_);
1591 bind_[9].buffer_type = MYSQL_TYPE_TINY;
1592 bind_[9].buffer =
reinterpret_cast<char*
>(&fqdn_fwd_);
1598 bind_[10].buffer_type = MYSQL_TYPE_TINY;
1599 bind_[10].buffer =
reinterpret_cast<char*
>(&fqdn_rev_);
1605 hostname_length_ =
sizeof(hostname_buffer_);
1606 bind_[11].buffer_type = MYSQL_TYPE_STRING;
1607 bind_[11].buffer =
reinterpret_cast<char*
>(hostname_buffer_);
1608 bind_[11].buffer_length = hostname_length_;
1609 bind_[11].length = &hostname_length_;
1615 hwaddr_length_ =
sizeof(hwaddr_buffer_);
1616 bind_[12].buffer_type = MYSQL_TYPE_BLOB;
1617 bind_[12].buffer =
reinterpret_cast<char*
>(hwaddr_buffer_);
1618 bind_[12].buffer_length = hwaddr_length_;
1619 bind_[12].length = &hwaddr_length_;
1620 bind_[12].is_null = &hwaddr_null_;
1623 bind_[13].buffer_type = MYSQL_TYPE_SHORT;
1624 bind_[13].buffer =
reinterpret_cast<char*
>(&hwtype_);
1628 bind_[14].buffer_type = MYSQL_TYPE_LONG;
1629 bind_[14].buffer =
reinterpret_cast<char*
>(&hwaddr_source_);
1633 bind_[15].buffer_type = MYSQL_TYPE_LONG;
1634 bind_[15].buffer =
reinterpret_cast<char*
>(&state_);
1641 user_context_length_ =
sizeof(user_context_);
1642 bind_[16].buffer_type = MYSQL_TYPE_STRING;
1643 bind_[16].buffer =
reinterpret_cast<char*
>(user_context_);
1644 bind_[16].buffer_length = user_context_length_;
1645 bind_[16].length = &user_context_length_;
1646 bind_[16].is_null = &user_context_null_;
1649 bind_[17].buffer_type = MYSQL_TYPE_LONG;
1650 bind_[17].buffer =
reinterpret_cast<char*
>(&pool_id_);
1659 BOOST_STATIC_ASSERT(17 < LEASE_COLUMNS);
1663 return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
1679 std::string address = addr.
toText();
1684 switch (lease_type_) {
1699 static_cast<int>(lease_type_) <<
") for lease with "
1700 <<
"address " << address <<
". Only 0, 1, or 2 are "
1709 DuidPtr duid_ptr(
new DUID(duid_buffer_, duid_length_));
1713 std::string hostname(hostname_buffer_,
1714 hostname_buffer_ + hostname_length_);
1719 hwaddr.reset(
new HWAddr(hwaddr_buffer_, hwaddr_length_, hwtype_));
1720 hwaddr->source_ = hwaddr_source_;
1724 std::string user_context;
1726 user_context_[user_context_length_] =
'\0';
1727 user_context.assign(user_context_);
1732 if (!user_context.empty()) {
1736 <<
"' is not a JSON map");
1742 Lease6Ptr result(boost::make_shared<Lease6>(type, addr, duid_ptr, iaid_,
1744 valid_lifetime_, subnet_id_,
1745 fqdn_fwd_, fqdn_rev_,
1750 uint32_t valid_lft = valid_lifetime_;
1756 result->cltt_ = cltt;
1757 result->current_cltt_ = cltt;
1760 result->state_ = state_;
1763 result->setContext(ctx);
1767 result->pool_id_ = pool_id_;
1791 std::vector<uint8_t> addr6_;
1792 uint8_t addr6_buffer_[16];
1793 unsigned long addr6_length_;
1794 MYSQL_BIND bind_[LEASE_COLUMNS];
1795 std::string columns_[LEASE_COLUMNS];
1796 my_bool error_[LEASE_COLUMNS];
1798 std::vector<uint8_t> hwaddr_;
1800 unsigned long hwaddr_length_;
1802 std::vector<uint8_t> duid_;
1804 unsigned long duid_length_;
1807 uint8_t lease_type_;
1808 uint8_t prefix_len_;
1809 uint32_t pref_lifetime_;
1810 uint32_t subnet_id_;
1812 uint32_t valid_lifetime_;
1816 unsigned long hostname_length_;
1818 uint32_t hwaddr_source_;
1821 unsigned long user_context_length_;
1845 const bool fetch_type,
const bool fetch_pool =
false)
1846 : conn_(conn), statement_index_(statement_index), statement_(NULL),
1847 fetch_type_(fetch_type), fetch_pool_(fetch_pool),
1850 bind_(fetch_type_ ? (fetch_pool_ ? 5 : 4) : (fetch_pool_ ? 4 : 3)),
1851 subnet_id_(0), pool_id_(0), lease_type_(
Lease::TYPE_NA),
1852 state_(
Lease::STATE_DEFAULT), state_count_(0) {
1853 validateStatement();
1866 const bool fetch_type,
const SubnetID& subnet_id)
1867 :
LeaseStatsQuery(subnet_id), conn_(conn), statement_index_(statement_index),
1868 statement_(NULL), fetch_type_(fetch_type), fetch_pool_(false),
1871 bind_(fetch_type_ ? 4 : 3), subnet_id_(0), pool_id_(0),
1872 lease_type_(
Lease::TYPE_NA), state_(
Lease::STATE_DEFAULT),
1874 validateStatement();
1890 const bool fetch_type,
const SubnetID& first_subnet_id,
1893 statement_index_(statement_index), statement_(NULL),
1894 fetch_type_(fetch_type), fetch_pool_(false),
1897 bind_(fetch_type_ ? 4 : 3), subnet_id_(0), pool_id_(0),
1898 lease_type_(
Lease::TYPE_NA), state_(
Lease::STATE_DEFAULT),
1900 validateStatement();
1905 (void) mysql_stmt_free_result(statement_);
1918 MYSQL_BIND inbind[2];
1919 memset(inbind, 0,
sizeof(inbind));
1922 inbind[0].buffer_type = MYSQL_TYPE_LONG;
1928 inbind[1].buffer_type = MYSQL_TYPE_LONG;
1934 int status = mysql_stmt_bind_param(statement_, &inbind[0]);
1935 conn_.
checkError(status, statement_index_,
"unable to bind parameters");
1940 bind_[col].buffer_type = MYSQL_TYPE_LONG;
1941 bind_[col].buffer =
reinterpret_cast<char*
>(&subnet_id_);
1948 bind_[col].buffer_type = MYSQL_TYPE_LONG;
1949 bind_[col].buffer =
reinterpret_cast<char*
>(&pool_id_);
1957 bind_[col].buffer_type = MYSQL_TYPE_LONG;
1958 bind_[col].buffer =
reinterpret_cast<char*
>(&lease_type_);
1966 bind_[col].buffer_type = MYSQL_TYPE_LONG;
1967 bind_[col].buffer =
reinterpret_cast<char*
>(&state_);
1972 bind_[col].buffer_type = MYSQL_TYPE_LONGLONG;
1973 bind_[col].buffer =
reinterpret_cast<char*
>(&state_count_);
1978 int status = mysql_stmt_bind_result(statement_, &bind_[0]);
1979 conn_.
checkError(status, statement_index_,
"outbound binding failed");
1983 conn_.
checkError(status, statement_index_,
"unable to execute");
1987 status = mysql_stmt_store_result(statement_);
1988 conn_.
checkError(status, statement_index_,
"results storage failed");
2007 bool have_row =
false;
2008 int status = mysql_stmt_fetch(statement_);
2010 row.subnet_id_ = subnet_id_;
2011 row.pool_id_ = pool_id_;
2012 row.lease_type_ =
static_cast<Lease::Type>(lease_type_);
2013 row.lease_state_ = state_;
2014 if (state_count_ >= 0) {
2015 row.state_count_ = state_count_;
2017 row.state_count_ = 0;
2018 if (!negative_count_) {
2019 negative_count_ =
true;
2024 }
else if (status != MYSQL_NO_DATA) {
2025 conn_.
checkError(status, statement_index_,
"getNextRow failed");
2036 void validateStatement() {
2039 " - invalid statement index" << statement_index_);
2049 size_t statement_index_;
2052 MYSQL_STMT *statement_;
2061 std::vector<MYSQL_BIND> bind_;
2064 uint32_t subnet_id_;
2070 uint32_t lease_type_;
2076 int64_t state_count_;
2079 static bool negative_count_;
2083bool MySqlLeaseStatsQuery::negative_count_ =
false;
2090 : conn_(parameters, io_service_accessor, db_reconnect_callback) {
2095MySqlLeaseMgr::MySqlLeaseContextAlloc::MySqlLeaseContextAlloc(
2102 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2103 if (!mgr_.pool_->pool_.empty()) {
2104 ctx_ = mgr_.pool_->pool_.back();
2105 mgr_.pool_->pool_.pop_back();
2109 ctx_ = mgr_.createContext();
2113 if (mgr_.pool_->pool_.empty()) {
2116 ctx_ = mgr_.pool_->pool_.back();
2120MySqlLeaseMgr::MySqlLeaseContextAlloc::~MySqlLeaseContextAlloc() {
2123 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2124 mgr_.pool_->pool_.push_back(ctx_);
2131MySqlLeaseMgr::MySqlLeaseTrackingContextAlloc::MySqlLeaseTrackingContextAlloc(
2138 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2139 if (mgr_.hasCallbacks() && !mgr_.tryLock(lease)) {
2142 if (!mgr_.pool_->pool_.empty()) {
2143 ctx_ = mgr_.pool_->pool_.back();
2144 mgr_.pool_->pool_.pop_back();
2148 ctx_ = mgr_.createContext();
2152 if (mgr_.pool_->pool_.empty()) {
2155 ctx_ = mgr_.pool_->pool_.back();
2159MySqlLeaseMgr::MySqlLeaseTrackingContextAlloc::~MySqlLeaseTrackingContextAlloc() {
2162 lock_guard<mutex> lock(mgr_.pool_->mutex_);
2166 mgr_.pool_->pool_.push_back(ctx_);
2180 timer_name_ =
"MySqlLeaseMgr[";
2181 timer_name_ += boost::lexical_cast<std::string>(
reinterpret_cast<uint64_t
>(
this));
2182 timer_name_ +=
"]DbReconnectTimer";
2204 bool reopened =
false;
2206 const std::string timer_name = db_reconnect_ctl->timerName();
2213 }
catch (
const std::exception& ex) {
2229 if (!db_reconnect_ctl->checkRetries()) {
2232 .arg(db_reconnect_ctl->maxRetries());
2245 .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
2246 .arg(db_reconnect_ctl->maxRetries())
2247 .arg(db_reconnect_ctl->retryInterval());
2253 db_reconnect_ctl->retryInterval(),
2271 ctx->conn_.makeReconnectCtl(timer_name_);
2274 ctx->conn_.openDatabase();
2277 if (ctx->conn_.getTls()) {
2278 std::string cipher = ctx->conn_.getTlsCipher();
2279 if (cipher.empty()) {
2289 ctx->conn_.prepareStatements(tagged_statements.begin(),
2290 tagged_statements.end());
2302 std::stringstream tmp;
2305 tmp <<
", library " << mysql_get_client_info();
2315 StatementIndex stindex,
2316 std::vector<MYSQL_BIND>& bind) {
2318 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), &bind[0]);
2319 checkError(ctx, status, stindex,
"unable to bind parameters");
2328 if (mysql_errno(ctx->conn_.mysql_) == ER_DUP_ENTRY) {
2331 checkError(ctx, status, stindex,
"unable to execute");
2341 .arg(lease->addr_.toText());
2344 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
2348 std::vector<MYSQL_BIND> bind = ctx->exchange4_->createBindForSend(lease);
2355 lease->updateCurrentExpirationTime();
2368 .arg(lease->addr_.toText())
2374 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
2378 std::vector<MYSQL_BIND> bind = ctx->exchange6_->createBindForSend(lease);
2385 lease->updateCurrentExpirationTime();
2424template <
typename Exchange,
typename LeaseCollection>
2427 StatementIndex stindex,
2430 LeaseCollection& result,
2431 bool single)
const {
2436 status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
2437 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
2442 std::vector<MYSQL_BIND> outbind = exchange->createBindForReceive();
2443 status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &outbind[0]);
2444 checkError(ctx, status, stindex,
"unable to bind SELECT clause parameters");
2448 checkError(ctx, status, stindex,
"unable to execute");
2452 status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
2453 checkError(ctx, status, stindex,
"unable to set up for storing all results");
2460 while ((status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex))) == 0) {
2462 result.push_back(exchange->getLeaseData());
2467 ctx->conn_.text_statements_[stindex] <<
">");
2470 if (single && (++count > 1)) {
2472 "database where only one was expected for query "
2473 << ctx->conn_.text_statements_[stindex]);
2480 checkError(ctx, status, stindex,
"unable to fetch results");
2481 }
else if (status == MYSQL_DATA_TRUNCATED) {
2484 <<
" returned truncated data: columns affected are "
2485 << exchange->getErrorColumns());
2491 StatementIndex stindex, MYSQL_BIND* bind,
2499 getLeaseCollection(ctx, stindex, bind, ctx->exchange4_, collection,
true);
2502 if (collection.empty()) {
2505 result = *collection.begin();
2511 StatementIndex stindex, MYSQL_BIND* bind,
2519 getLeaseCollection(ctx, stindex, bind, ctx->exchange6_, collection,
true);
2522 if (collection.empty()) {
2525 result = *collection.begin();
2538 MYSQL_BIND inbind[1];
2539 memset(inbind, 0,
sizeof(inbind));
2542 inbind[0].buffer_type = MYSQL_TYPE_LONG;
2543 inbind[0].buffer =
reinterpret_cast<char*
>(&addr4);
2550 MySqlLeaseContextAlloc get_context(*
this);
2564 MYSQL_BIND inbind[1];
2565 memset(inbind, 0,
sizeof(inbind));
2567 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2569 unsigned long hwaddr_length = hwaddr.
hwaddr_.size();
2573 uint8_t single_byte_data = 0;
2580 uint8_t* data = !hwaddr.
hwaddr_.empty() ?
const_cast<uint8_t*
>(&hwaddr.
hwaddr_[0])
2581 : &single_byte_data;
2583 inbind[0].buffer =
reinterpret_cast<char*
>(data);
2584 inbind[0].buffer_length = hwaddr_length;
2585 inbind[0].length = &hwaddr_length;
2591 MySqlLeaseContextAlloc get_context(*
this);
2606 MYSQL_BIND inbind[2];
2607 memset(inbind, 0,
sizeof(inbind));
2609 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2611 unsigned long hwaddr_length = hwaddr.
hwaddr_.size();
2615 std::vector<uint8_t> single_byte_vec(1);
2622 uint8_t* data = !hwaddr.
hwaddr_.empty() ?
const_cast<uint8_t*
>(&hwaddr.
hwaddr_[0])
2623 : &single_byte_vec[0];
2625 inbind[0].buffer =
reinterpret_cast<char*
>(data);
2626 inbind[0].buffer_length = hwaddr_length;
2627 inbind[0].length = &hwaddr_length;
2629 inbind[1].buffer_type = MYSQL_TYPE_LONG;
2630 inbind[1].buffer =
reinterpret_cast<char*
>(&subnet_id);
2637 MySqlLeaseContextAlloc get_context(*
this);
2648 .arg(clientid.toText());
2651 MYSQL_BIND inbind[1];
2652 memset(inbind, 0,
sizeof(inbind));
2654 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2656 std::vector<uint8_t> client_data = clientid.getClientId();
2657 unsigned long client_data_length = client_data.size();
2661 if (client_data.empty()) {
2662 client_data.resize(1);
2665 inbind[0].buffer =
reinterpret_cast<char*
>(&client_data[0]);
2666 inbind[0].buffer_length = client_data_length;
2667 inbind[0].length = &client_data_length;
2673 MySqlLeaseContextAlloc get_context(*
this);
2685 .arg(clientid.toText());
2688 MYSQL_BIND inbind[2];
2689 memset(inbind, 0,
sizeof(inbind));
2691 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2693 std::vector<uint8_t> client_data = clientid.getClientId();
2694 unsigned long client_data_length = client_data.size();
2698 if (client_data.empty()) {
2699 client_data.resize(1);
2702 inbind[0].buffer =
reinterpret_cast<char*
>(&client_data[0]);
2703 inbind[0].buffer_length = client_data_length;
2704 inbind[0].length = &client_data_length;
2706 inbind[1].buffer_type = MYSQL_TYPE_LONG;
2707 inbind[1].buffer =
reinterpret_cast<char*
>(&subnet_id);
2714 MySqlLeaseContextAlloc get_context(*
this);
2728 MYSQL_BIND inbind[1];
2729 memset(inbind, 0,
sizeof(inbind));
2732 inbind[0].buffer_type = MYSQL_TYPE_LONG;
2733 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_id);
2740 MySqlLeaseContextAlloc get_context(*
this);
2754 MYSQL_BIND inbind[1];
2755 memset(inbind, 0,
sizeof(inbind));
2758 inbind[0].buffer_type = MYSQL_TYPE_STRING;
2759 inbind[0].buffer =
const_cast<char*
>(hostname.c_str());
2760 inbind[0].buffer_length = hostname.length();
2766 MySqlLeaseContextAlloc get_context(*
this);
2781 MySqlLeaseContextAlloc get_context(*
this);
2784 getLeaseCollection(ctx,
GET_LEASE4, 0, result);
2793 if (!lower_bound_address.isV4()) {
2795 "retrieving leases from the lease database, got "
2796 << lower_bound_address);
2800 .arg(page_size.page_size_)
2801 .arg(lower_bound_address.toText());
2804 MYSQL_BIND inbind[2];
2805 memset(inbind, 0,
sizeof(inbind));
2808 uint32_t lb_address_data = lower_bound_address.toUint32();
2809 inbind[0].buffer_type = MYSQL_TYPE_LONG;
2810 inbind[0].buffer =
reinterpret_cast<char*
>(&lb_address_data);
2814 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
2815 inbind[1].buffer_type = MYSQL_TYPE_LONG;
2816 inbind[1].buffer =
reinterpret_cast<char*
>(&ps);
2823 MySqlLeaseContextAlloc get_context(*
this);
2839 MYSQL_BIND inbind[2];
2840 memset(inbind, 0,
sizeof(inbind));
2843 std::vector<uint8_t>addr6 = addr.
toBytes();
2844 if (addr6.size() != 16) {
2848 unsigned long addr6_length = 16;
2849 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2850 inbind[0].buffer =
reinterpret_cast<char*
>(&addr6[0]);
2851 inbind[0].buffer_length = 16;
2852 inbind[0].length = &addr6_length;
2855 inbind[1].buffer_type = MYSQL_TYPE_TINY;
2856 inbind[1].buffer =
reinterpret_cast<char*
>(&lease_type);
2862 MySqlLeaseContextAlloc get_context(*
this);
2872 uint32_t iaid)
const {
2879 MYSQL_BIND inbind[3];
2880 memset(inbind, 0,
sizeof(inbind));
2894 const vector<uint8_t>& duid_vector = duid.
getDuid();
2895 unsigned long duid_length = duid_vector.size();
2902 uint8_t single_byte_data = 0;
2903 uint8_t* data = !duid_vector.empty() ?
const_cast<uint8_t*
>(&duid_vector[0])
2904 : &single_byte_data;
2906 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2907 inbind[0].buffer =
reinterpret_cast<char*
>(data);
2908 inbind[0].buffer_length = duid_length;
2909 inbind[0].length = &duid_length;
2912 inbind[1].buffer_type = MYSQL_TYPE_LONG;
2913 inbind[1].buffer =
reinterpret_cast<char*
>(&iaid);
2917 inbind[2].buffer_type = MYSQL_TYPE_TINY;
2918 inbind[2].buffer =
reinterpret_cast<char*
>(&lease_type);
2925 MySqlLeaseContextAlloc get_context(*
this);
2935 uint32_t iaid,
SubnetID subnet_id)
const {
2943 MYSQL_BIND inbind[4];
2944 memset(inbind, 0,
sizeof(inbind));
2948 const vector<uint8_t>& duid_vector = duid.
getDuid();
2949 unsigned long duid_length = duid_vector.size();
2950 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
2951 inbind[0].buffer =
reinterpret_cast<char*
>(
2952 const_cast<uint8_t*
>(&duid_vector[0]));
2953 inbind[0].buffer_length = duid_length;
2954 inbind[0].length = &duid_length;
2957 inbind[1].buffer_type = MYSQL_TYPE_LONG;
2958 inbind[1].buffer =
reinterpret_cast<char*
>(&iaid);
2962 inbind[2].buffer_type = MYSQL_TYPE_LONG;
2963 inbind[2].buffer =
reinterpret_cast<char*
>(&subnet_id);
2967 inbind[3].buffer_type = MYSQL_TYPE_TINY;
2968 inbind[3].buffer =
reinterpret_cast<char*
>(&lease_type);
2975 MySqlLeaseContextAlloc get_context(*
this);
2989 MYSQL_BIND inbind[1];
2990 memset(inbind, 0,
sizeof(inbind));
2993 inbind[0].buffer_type = MYSQL_TYPE_LONG;
2994 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_id);
3001 MySqlLeaseContextAlloc get_context(*
this);
3015 .arg(page_size.page_size_)
3016 .arg(lower_bound_address.toText())
3020 if (!lower_bound_address.isV6()) {
3022 "retrieving leases from the lease database, got "
3023 << lower_bound_address);
3028 MYSQL_BIND inbind[3];
3029 memset(inbind, 0,
sizeof(inbind));
3032 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3033 inbind[0].buffer =
reinterpret_cast<char*
>(&subnet_id);
3037 std::vector<uint8_t> lb_addr_data = lower_bound_address.toBytes();
3038 unsigned long lb_addr_size = lb_addr_data.size();
3039 if (lb_addr_size != 16) {
3042 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
3043 inbind[1].buffer =
reinterpret_cast<char*
>(&lb_addr_data[0]);
3044 inbind[1].buffer_length = lb_addr_size;
3045 inbind[1].length = &lb_addr_size;
3048 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
3049 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3050 inbind[2].buffer =
reinterpret_cast<char*
>(&ps);
3054 MySqlLeaseContextAlloc get_context(*
this);
3070 MySqlLeaseContextAlloc get_context(*
this);
3073 getLeaseCollection(ctx,
GET_LEASE6, 0, result);
3084 MYSQL_BIND inbind[1];
3085 memset(inbind, 0,
sizeof(inbind));
3087 const vector<uint8_t>& duid_vector = duid.
getDuid();
3088 unsigned long duid_length = duid_vector.size();
3090 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3091 inbind[0].buffer =
reinterpret_cast<char*
>(
3092 const_cast<uint8_t*
>(&duid_vector[0]));
3093 inbind[0].buffer_length = duid_length;
3094 inbind[0].length = &duid_length;
3099 MySqlLeaseContextAlloc get_context(*
this);
3113 MYSQL_BIND inbind[1];
3114 memset(inbind, 0,
sizeof(inbind));
3117 inbind[0].buffer_type = MYSQL_TYPE_STRING;
3118 inbind[0].buffer =
const_cast<char*
>(hostname.c_str());
3119 inbind[0].buffer_length = hostname.length();
3125 MySqlLeaseContextAlloc get_context(*
this);
3137 if (!lower_bound_address.isV6()) {
3139 "retrieving leases from the lease database, got "
3140 << lower_bound_address);
3144 .arg(page_size.page_size_)
3145 .arg(lower_bound_address.toText());
3148 MYSQL_BIND inbind[2];
3149 memset(inbind, 0,
sizeof(inbind));
3152 std::vector<uint8_t>lb_addr = lower_bound_address.toBytes();
3153 if (lb_addr.size() != 16) {
3157 unsigned long lb_addr_length = 16;
3158 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3159 inbind[0].buffer =
reinterpret_cast<char*
>(&lb_addr[0]);
3160 inbind[0].buffer_length = 16;
3161 inbind[0].length = &lb_addr_length;
3164 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
3165 inbind[1].buffer_type = MYSQL_TYPE_LONG;
3166 inbind[1].buffer =
reinterpret_cast<char*
>(&ps);
3173 MySqlLeaseContextAlloc get_context(*
this);
3183 const size_t max_leases)
const {
3191 const size_t max_leases)
const {
3197template<
typename LeaseCollection>
3199MySqlLeaseMgr::getExpiredLeasesCommon(LeaseCollection& expired_leases,
3200 const size_t max_leases,
3201 StatementIndex statement_index)
const {
3203 MYSQL_BIND inbind[3];
3204 memset(inbind, 0,
sizeof(inbind));
3208 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3209 inbind[0].buffer =
reinterpret_cast<char*
>(&state);
3213 MYSQL_TIME expire_time;
3215 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3216 inbind[1].buffer =
reinterpret_cast<char*
>(&expire_time);
3217 inbind[1].buffer_length =
sizeof(expire_time);
3221 uint32_t limit = max_leases > 0 ?
static_cast<uint32_t
>(max_leases) :
3222 std::numeric_limits<uint32_t>::max();
3223 inbind[2].buffer_type = MYSQL_TYPE_LONG;
3224 inbind[2].buffer =
reinterpret_cast<char*
>(&limit);
3228 MySqlLeaseContextAlloc get_context(*
this);
3232 getLeaseCollection(ctx, statement_index, inbind, expired_leases);
3239template <
typename LeasePtr>
3242 StatementIndex stindex,
3247 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
3248 checkError(ctx, status, stindex,
"unable to bind parameters");
3252 checkError(ctx, status, stindex,
"unable to execute");
3256 int affected_rows = mysql_stmt_affected_rows(ctx->conn_.getStatement(stindex));
3259 if (affected_rows == 1) {
3264 if (affected_rows == 0) {
3266 lease->addr_.toText() <<
" as it does not exist");
3272 "that had the address " << lease->addr_.toText());
3280 .arg(lease->addr_.toText());
3283 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
3287 std::vector<MYSQL_BIND> bind = ctx->exchange4_->createBindForSend(lease);
3290 MYSQL_BIND inbind[2];
3291 memset(inbind, 0,
sizeof(inbind));
3293 uint32_t addr4 = lease->addr_.toUint32();
3294 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3295 inbind[0].buffer =
reinterpret_cast<char*
>(&addr4);
3298 bind.push_back(inbind[0]);
3303 uint32_t valid_lft = lease->current_valid_lft_;
3309 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3310 inbind[1].buffer =
reinterpret_cast<char*
>(&expire);
3311 inbind[1].buffer_length =
sizeof(expire);
3313 bind.push_back(inbind[1]);
3316 updateLeaseCommon(ctx, stindex, &bind[0], lease);
3319 lease->updateCurrentExpirationTime();
3332 .arg(lease->addr_.toText())
3340 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
3344 std::vector<MYSQL_BIND> bind = ctx->exchange6_->createBindForSend(lease);
3347 MYSQL_BIND inbind[2];
3348 memset(inbind, 0,
sizeof(inbind));
3351 std::vector<uint8_t>addr6 = lease->addr_.toBytes();
3352 if (addr6.size() != 16) {
3356 unsigned long addr6_length = 16;
3357 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3358 inbind[0].buffer =
reinterpret_cast<char*
>(&addr6[0]);
3359 inbind[0].buffer_length = 16;
3360 inbind[0].length = &addr6_length;
3362 bind.push_back(inbind[0]);
3367 uint32_t valid_lft = lease->current_valid_lft_;
3373 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3374 inbind[1].buffer =
reinterpret_cast<char*
>(&expire);
3375 inbind[1].buffer_length =
sizeof(expire);
3377 bind.push_back(inbind[1]);
3380 updateLeaseCommon(ctx, stindex, &bind[0], lease);
3383 lease->updateCurrentExpirationTime();
3387 switch (recorded_action) {
3415 StatementIndex stindex,
3418 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
3419 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
3423 checkError(ctx, status, stindex,
"unable to execute");
3427 return (mysql_stmt_affected_rows(ctx->conn_.getStatement(stindex)));
3437 MYSQL_BIND inbind[2];
3438 memset(inbind, 0,
sizeof(inbind));
3442 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3443 inbind[0].buffer =
reinterpret_cast<char*
>(&addr4);
3449 uint32_t valid_lft = lease->current_valid_lft_;
3455 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3456 inbind[1].buffer =
reinterpret_cast<char*
>(&expire);
3457 inbind[1].buffer_length =
sizeof(expire);
3460 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
3463 auto affected_rows = deleteLeaseCommon(ctx,
DELETE_LEASE4, inbind);
3466 if (affected_rows == 1) {
3474 if (affected_rows == 0) {
3481 "that had the address " << lease->addr_.toText());
3494 MYSQL_BIND inbind[2];
3495 memset(inbind, 0,
sizeof(inbind));
3498 std::vector<uint8_t>addr6 = addr.
toBytes();
3499 if (addr6.size() != 16) {
3503 unsigned long addr6_length = 16;
3504 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
3505 inbind[0].buffer =
reinterpret_cast<char*
>(&addr6[0]);
3506 inbind[0].buffer_length = 16;
3507 inbind[0].length = &addr6_length;
3512 uint32_t valid_lft = lease->current_valid_lft_;
3518 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3519 inbind[1].buffer =
reinterpret_cast<char*
>(&expire);
3520 inbind[1].buffer_length =
sizeof(expire);
3523 MySqlLeaseTrackingContextAlloc get_context(*
this, lease);
3526 auto affected_rows = deleteLeaseCommon(ctx,
DELETE_LEASE6, inbind);
3529 if (affected_rows == 1) {
3541 if (affected_rows == 0) {
3548 "that had the address " << lease->addr_.toText());
3566MySqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(
const uint32_t secs,
3567 StatementIndex statement_index) {
3569 MYSQL_BIND inbind[2];
3570 memset(inbind, 0,
sizeof(inbind));
3574 inbind[0].buffer_type = MYSQL_TYPE_LONG;
3575 inbind[0].buffer =
reinterpret_cast<char*
>(&state);
3579 MYSQL_TIME expire_time;
3581 inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
3582 inbind[1].buffer =
reinterpret_cast<char*
>(&expire_time);
3583 inbind[1].buffer_length =
sizeof(expire_time);
3586 MySqlLeaseContextAlloc get_context(*
this);
3590 uint64_t deleted_leases = deleteLeaseCommon(ctx, statement_index, inbind);
3592 .arg(deleted_leases);
3594 return (deleted_leases);
3598MySqlLeaseMgr::checkLimits(
ConstElementPtr const& user_context, StatementIndex
const stindex)
const {
3600 if (!user_context) {
3605 MySqlLeaseContextAlloc get_context(*
this);
3617 std::string limit_text;
3618 ctx->conn_.selectQuery(stindex, in_bindings, out_bindings,
3620 limit_text = result[0]->getString();
3627MySqlLeaseMgr::checkLimits4(
ConstElementPtr const& user_context)
const {
3632MySqlLeaseMgr::checkLimits6(
ConstElementPtr const& user_context)
const {
3637MySqlLeaseMgr::isJsonSupported()
const {
3639 MySqlLeaseContextAlloc get_context(*
this);
3649 bool json_supported(
false);
3652 json_supported = result[0]->getBool();
3655 return json_supported;
3659MySqlLeaseMgr::getClassLeaseCount(
const ClientClass& client_class,
3662 MySqlLeaseContextAlloc get_context(*
this);
3670 in_bindings.push_back(MySqlBinding::createInteger<uint8_t>(ltype));
3673 MySqlBinding::createInteger<int64_t>()
3680 ctx->conn_.selectQuery(stindex, in_bindings, out_bindings,
3682 count = result[0]->getInteger<int64_t>();
3689MySqlLeaseMgr::recountClassLeases4() {
3694MySqlLeaseMgr::recountClassLeases6() {
3699MySqlLeaseMgr::clearClassLeaseCounts() {
3704MySqlLeaseMgr::writeLeases4(
const std::string&) {
3709MySqlLeaseMgr::writeLeases6(
const std::string&) {
3716 MySqlLeaseContextAlloc get_context(*
this);
3729 MySqlLeaseContextAlloc get_context(*
this);
3742 MySqlLeaseContextAlloc get_context(*
this);
3757 MySqlLeaseContextAlloc get_context(*
this);
3772 MySqlLeaseContextAlloc get_context(*
this);
3785 MySqlLeaseContextAlloc get_context(*
this);
3798 MySqlLeaseContextAlloc get_context(*
this);
3813 MySqlLeaseContextAlloc get_context(*
this);
3840 MySqlLeaseContextAlloc get_context(*
this);
3843 std::string name =
"";
3845 name = ctx->conn_.getParameter(
"name");
3854 return (std::string(
"MySQL Database"));
3857std::pair<uint32_t, uint32_t>
3879 int status, StatementIndex index,
3880 const char* what)
const {
3881 ctx->conn_.checkError(status, index, what);
3886 deleteRelayId6(addr);
3887 deleteRemoteId6(addr);
3891MySqlLeaseMgr::deleteRelayId6(
const IOAddress& addr) {
3893 MySqlLeaseContextAlloc get_context(*
this);
3898 memset(bind, 0,
sizeof(bind));
3900 std::vector<uint8_t> addr_data = addr.
toBytes();
3902 unsigned long addr_size = addr_data.size();
3903 bind[0].buffer_type = MYSQL_TYPE_BLOB;
3904 bind[0].buffer =
reinterpret_cast<char*
>(&addr_data[0]);
3905 bind[0].buffer_length = addr_size;
3906 bind[0].length = &addr_size;
3912 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
3913 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
3917 checkError(ctx, status, stindex,
"unable to execute");
3921MySqlLeaseMgr::deleteRemoteId6(
const IOAddress& addr) {
3923 MySqlLeaseContextAlloc get_context(*
this);
3928 memset(bind, 0,
sizeof(bind));
3930 std::vector<uint8_t> addr_data = addr.
toBytes();
3932 unsigned long addr_size = addr_data.size();
3933 bind[0].buffer_type = MYSQL_TYPE_BLOB;
3934 bind[0].buffer =
reinterpret_cast<char*
>(&addr_data[0]);
3935 bind[0].buffer_length = addr_size;
3936 bind[0].length = &addr_size;
3942 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
3943 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
3947 checkError(ctx, status, stindex,
"unable to execute");
3952 const vector<uint8_t>& relay_id) {
3954 MySqlLeaseContextAlloc get_context(*
this);
3959 memset(bind, 0,
sizeof(bind));
3961 unsigned long relay_id_size = relay_id.size();
3962 if (relay_id_size == 0) {
3965 std::vector<uint8_t> relay_id_data = relay_id;
3966 bind[0].buffer_type = MYSQL_TYPE_BLOB;
3967 bind[0].buffer =
reinterpret_cast<char*
>(&relay_id_data[0]);
3968 bind[0].buffer_length = relay_id_size;
3969 bind[0].length = &relay_id_size;
3972 std::vector<uint8_t> lease_addr_data = lease_addr.toBytes();
3973 unsigned long lease_addr_length = lease_addr_data.size();
3974 if (lease_addr_length != 16) {
3977 bind[1].buffer_type = MYSQL_TYPE_BLOB;
3978 bind[1].buffer =
reinterpret_cast<char*
>(&lease_addr_data[0]);
3979 bind[1].buffer_length = lease_addr_length;
3980 bind[1].length = &lease_addr_length;
3986 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
3987 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
3991 checkError(ctx, status, stindex,
"unable to execute");
3996 const vector<uint8_t>& remote_id) {
3998 MySqlLeaseContextAlloc get_context(*
this);
4003 memset(bind, 0,
sizeof(bind));
4005 unsigned long remote_id_size = remote_id.size();
4006 if (remote_id_size == 0) {
4009 std::vector<uint8_t> remote_id_data = remote_id;
4010 bind[0].buffer_type = MYSQL_TYPE_BLOB;
4011 bind[0].buffer =
reinterpret_cast<char*
>(&remote_id_data[0]);
4012 bind[0].buffer_length = remote_id_size;
4013 bind[0].length = &remote_id_size;
4016 std::vector<uint8_t> lease_addr_data = lease_addr.toBytes();
4017 unsigned long lease_addr_length = lease_addr_data.size();
4018 if (lease_addr_length != 16) {
4021 bind[1].buffer_type = MYSQL_TYPE_BLOB;
4022 bind[1].buffer =
reinterpret_cast<char*
>(&lease_addr_data[0]);
4023 bind[1].buffer_length = lease_addr_length;
4024 bind[1].length = &lease_addr_length;
4030 int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
4031 checkError(ctx, status, stindex,
"unable to bind WHERE clause parameter");
4035 checkError(ctx, status, stindex,
"unable to execute");
4042 std::stringstream tmp;
4045 for (
auto const& it : id) {
4049 tmp << std::setw(2) << std::setfill(
'0')
4050 <<
static_cast<unsigned int>(it);
4062 const time_t& qry_start_time ,
4063 const time_t& qry_end_time ) {
4066 .arg(page_size.page_size_)
4067 .arg(lower_bound_address.toText())
4068 .arg(idToText(relay_id))
4069 .arg(qry_start_time)
4073 if (!lower_bound_address.isV4()) {
4075 "retrieving leases from the lease database, got "
4076 << lower_bound_address);
4080 if ((qry_start_time < 0) || (qry_end_time < 0)) {
4084 bool have_qst = (qry_start_time > 0);
4085 bool have_qet = (qry_end_time > 0);
4088 if (have_qst && have_qet && (qry_start_time > qry_end_time)) {
4093 size_t bindings = 3;
4100 MYSQL_BIND zeroed_out;
4101 memset(&zeroed_out, 0,
sizeof(zeroed_out));
4102 vector<MYSQL_BIND> inbind(bindings, zeroed_out);
4104 std::vector<uint8_t> relay_id_data = relay_id;
4105 unsigned long relay_id_length = relay_id.size();
4109 if (relay_id_data.empty()) {
4110 relay_id_data.resize(1);
4114 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
4115 inbind[0].buffer =
reinterpret_cast<char*
>(&relay_id_data[0]);
4116 inbind[0].buffer_length = relay_id_length;
4117 inbind[0].length = &relay_id_length;
4120 uint32_t lb_address_data = lower_bound_address.toUint32();
4121 inbind[1].buffer_type = MYSQL_TYPE_LONG;
4122 inbind[1].buffer =
reinterpret_cast<char*
>(&lb_address_data);
4127 uint32_t start_time =
static_cast<uint32_t
>(qry_start_time);
4129 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4130 inbind[index].buffer =
reinterpret_cast<char*
>(&start_time);
4131 inbind[index].is_unsigned =
MLM_TRUE;
4136 uint32_t end_time =
static_cast<uint32_t
>(qry_end_time);
4138 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4139 inbind[index].buffer =
reinterpret_cast<char*
>(&end_time);
4140 inbind[index].is_unsigned =
MLM_TRUE;
4145 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4146 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4147 inbind[index].buffer =
reinterpret_cast<char*
>(&ps);
4148 inbind[index].is_unsigned =
MLM_TRUE;
4151 if (have_qst && !have_qet) {
4153 }
else if (have_qst && have_qet) {
4155 }
else if (!have_qst && have_qet) {
4163 MySqlLeaseContextAlloc get_context(*
this);
4166 getLeaseCollection(ctx, stindex, inbind.data(), result);
4175 const time_t& qry_start_time ,
4176 const time_t& qry_end_time ) {
4179 .arg(page_size.page_size_)
4180 .arg(lower_bound_address.toText())
4181 .arg(idToText(remote_id))
4182 .arg(qry_start_time)
4186 if (!lower_bound_address.isV4()) {
4188 "retrieving leases from the lease database, got "
4189 << lower_bound_address);
4193 if ((qry_start_time < 0) || (qry_end_time < 0)) {
4197 bool have_qst = (qry_start_time > 0);
4198 bool have_qet = (qry_end_time > 0);
4201 if (have_qst && have_qet && (qry_start_time > qry_end_time)) {
4206 size_t bindings = 3;
4213 MYSQL_BIND zeroed_out;
4214 memset(&zeroed_out, 0,
sizeof(zeroed_out));
4215 vector<MYSQL_BIND> inbind(bindings, zeroed_out);
4217 std::vector<uint8_t> remote_id_data = remote_id;
4218 unsigned long remote_id_length = remote_id.size();
4222 if (remote_id_data.empty()) {
4223 remote_id_data.resize(1);
4227 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
4228 inbind[0].buffer =
reinterpret_cast<char*
>(&remote_id_data[0]);
4229 inbind[0].buffer_length = remote_id_length;
4230 inbind[0].length = &remote_id_length;
4233 uint32_t lb_address_data = lower_bound_address.toUint32();
4234 inbind[1].buffer_type = MYSQL_TYPE_LONG;
4235 inbind[1].buffer =
reinterpret_cast<char*
>(&lb_address_data);
4240 uint32_t start_time =
static_cast<uint32_t
>(qry_start_time);
4242 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4243 inbind[index].buffer =
reinterpret_cast<char*
>(&start_time);
4244 inbind[index].is_unsigned =
MLM_TRUE;
4249 uint32_t end_time =
static_cast<uint32_t
>(qry_end_time);
4251 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4252 inbind[index].buffer =
reinterpret_cast<char*
>(&end_time);
4253 inbind[index].is_unsigned =
MLM_TRUE;
4258 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4259 inbind[index].buffer_type = MYSQL_TYPE_LONG;
4260 inbind[index].buffer =
reinterpret_cast<char*
>(&ps);
4261 inbind[index].is_unsigned =
MLM_TRUE;
4264 if (have_qst && !have_qet) {
4266 }
else if (have_qst && have_qet) {
4268 }
else if (!have_qst && have_qet) {
4276 MySqlLeaseContextAlloc get_context(*
this);
4279 getLeaseCollection(ctx, stindex, inbind.data(), result);
4287 getConsistency()->getExtendedInfoSanityCheck();
4296 .arg(start_addr.toText())
4300 MYSQL_BIND inbind[2];
4301 memset(inbind, 0,
sizeof(inbind));
4304 uint32_t start_addr_data = start_addr.toUint32();
4305 inbind[0].buffer_type = MYSQL_TYPE_LONG;
4306 inbind[0].buffer =
reinterpret_cast<char*
>(&start_addr_data);
4310 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4311 inbind[1].buffer_type = MYSQL_TYPE_LONG;
4312 inbind[1].buffer =
reinterpret_cast<char*
>(&ps);
4319 MySqlLeaseContextAlloc get_context(*
this);
4325 if (leases.empty()) {
4331 start_addr = leases.back()->addr_;
4332 for (
auto const& lease : leases) {
4334 vector<uint8_t> previous_relay_id = lease->relay_id_;
4335 vector<uint8_t> previous_remote_id = lease->remote_id_;
4336 if (!previous_user_context &&
4337 previous_relay_id.empty() &&
4338 previous_remote_id.empty()) {
4343 lease->relay_id_.clear();
4344 lease->remote_id_.clear();
4347 (previous_relay_id != lease->relay_id_) ||
4348 (previous_remote_id != lease->remote_id_)) {
4356 }
catch (
const std::exception& ex) {
4360 .arg(lease->addr_.toText())
4379 .arg(page_size.page_size_)
4380 .arg(lower_bound_address.toText())
4381 .arg(relay_id.toText());
4384 if (!lower_bound_address.isV6()) {
4386 "retrieving leases from the lease database, got "
4387 << lower_bound_address);
4390 std::vector<uint8_t> relay_id_data = relay_id.getDuid();
4391 unsigned long relay_id_size = relay_id_data.size();
4392 if (relay_id_size == 0) {
4397 MYSQL_BIND inbind[3];
4398 memset(inbind, 0,
sizeof(inbind));
4400 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
4401 inbind[0].buffer =
reinterpret_cast<char*
>(&relay_id_data[0]);
4402 inbind[0].buffer_length = relay_id_size;
4403 inbind[0].length = &relay_id_size;
4406 std::vector<uint8_t> lb_addr_data = lower_bound_address.toBytes();
4407 unsigned long lb_addr_size = lb_addr_data.size();
4408 if (lb_addr_size != 16) {
4411 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
4412 inbind[1].buffer =
reinterpret_cast<char*
>(&lb_addr_data[0]);
4413 inbind[1].buffer_length = lb_addr_size;
4414 inbind[1].length = &lb_addr_size;
4417 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4418 inbind[2].buffer_type = MYSQL_TYPE_LONG;
4419 inbind[2].buffer =
reinterpret_cast<char*
>(&ps);
4423 MySqlLeaseContextAlloc get_context(*
this);
4437 .arg(page_size.page_size_)
4438 .arg(lower_bound_address.toText())
4439 .arg(idToText(remote_id));
4442 if (!lower_bound_address.isV6()) {
4444 "retrieving leases from the lease database, got "
4445 << lower_bound_address);
4448 std::vector<uint8_t> remote_id_data = remote_id;
4449 unsigned long remote_id_size = remote_id_data.size();
4450 if (remote_id_size == 0) {
4455 MYSQL_BIND inbind[3];
4456 memset(inbind, 0,
sizeof(inbind));
4458 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
4459 inbind[0].buffer =
reinterpret_cast<char*
>(&remote_id_data[0]);
4460 inbind[0].buffer_length = remote_id_size;
4461 inbind[0].length = &remote_id_size;
4464 std::vector<uint8_t> lb_addr_data = lower_bound_address.toBytes();
4465 unsigned long lb_addr_size = lb_addr_data.size();
4466 if (lb_addr_size != 16) {
4469 inbind[1].buffer_type = MYSQL_TYPE_BLOB;
4470 inbind[1].buffer =
reinterpret_cast<char*
>(&lb_addr_data[0]);
4471 inbind[1].buffer_length = lb_addr_size;
4472 inbind[1].length = &lb_addr_size;
4475 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4476 inbind[2].buffer_type = MYSQL_TYPE_LONG;
4477 inbind[2].buffer =
reinterpret_cast<char*
>(&ps);
4481 MySqlLeaseContextAlloc get_context(*
this);
4493 getConsistency()->getExtendedInfoSanityCheck();
4507 .arg(start_addr.toText())
4511 MYSQL_BIND inbind[2];
4512 memset(inbind, 0,
sizeof(inbind));
4515 std::vector<uint8_t>start_addr_bytes = start_addr.toBytes();
4516 if (start_addr_bytes.size() != 16) {
4520 unsigned long start_addr_size = 16;
4521 inbind[0].buffer_type = MYSQL_TYPE_BLOB;
4522 inbind[0].buffer =
reinterpret_cast<char*
>(&start_addr_bytes[0]);
4523 inbind[0].buffer_length = 16;
4524 inbind[0].length = &start_addr_size;
4527 uint32_t ps =
static_cast<uint32_t
>(page_size.page_size_);
4528 inbind[1].buffer_type = MYSQL_TYPE_LONG;
4529 inbind[1].buffer =
reinterpret_cast<char*
>(&ps);
4536 MySqlLeaseContextAlloc get_context(*
this);
4542 if (leases.empty()) {
4548 start_addr = leases.back()->addr_;
4549 for (
auto const& lease : leases) {
4557 if (modified || added) {
4564 }
catch (
const std::exception& ex) {
4568 .arg(lease->addr_.toText())
4584 MySqlLeaseContextAlloc get_context(*
this);
4590 checkError(ctx, status, stindex,
"unable to execute");
4596 checkError(ctx, status, stindex,
"unable to execute");
4603 MySqlLeaseContextAlloc get_context(*
this);
4610 memset(bind, 0,
sizeof(bind));
4613 bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
4614 bind[0].buffer =
reinterpret_cast<char*
>(&count);
4616 int status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &bind[0]);
4617 checkError(ctx, status, stindex,
"unable to bind SELECT clause parameters");
4622 checkError(ctx, status, stindex,
"unable to execute");
4625 status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
4626 checkError(ctx, status, stindex,
"unable to store result");
4631 status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex));
4633 checkError(ctx, status, stindex,
"unable to fetch results");
4635 return (
static_cast<size_t>(count));
4641 MySqlLeaseContextAlloc get_context(*
this);
4648 memset(bind, 0,
sizeof(bind));
4651 bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
4652 bind[0].buffer =
reinterpret_cast<char*
>(&count);
4654 int status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &bind[0]);
4655 checkError(ctx, status, stindex,
"unable to bind SELECT clause parameters");
4660 checkError(ctx, status, stindex,
"unable to execute");
4663 status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
4664 checkError(ctx, status, stindex,
"unable to store result");
4669 status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex));
4671 checkError(ctx, status, stindex,
"unable to fetch results");
4673 return (
static_cast<size_t>(count));
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 a function is not implemented.
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
static const IOAddress & IPV4_ZERO_ADDRESS()
Returns an address set to all zeros.
std::string toText() const
Convert the address to a string.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
static const IOAddress & IPV6_ZERO_ADDRESS()
Returns an IPv6 zero address.
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Exception thrown on failure to execute a database function.
Invalid address family used as input to Lease Manager.
Multiple lease records found where one expected.
static MySqlBindingPtr createString(const unsigned long length)
Creates binding of text type for receiving data.
static MySqlBindingPtr createBool()
Creates binding having a bool type for receiving data.
Common MySQL Connector Pool.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Convert time_t value to database time.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap ¶meters, const IOServiceAccessorPtr &ac=IOServiceAccessorPtr(), const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Get the schema version.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Convert Database Time to Lease Times.
void checkError(const int status, const StatementIndex &index, const char *what)
Check Error and Throw Exception.
MYSQL_STMT * getStatement(StatementIndex index) const
Returns a prepared statement by an index.
static void ensureSchemaVersion(const ParameterMap ¶meters, const DbCallback &cb=DbCallback(), const std::string &timer_name=std::string())
Retrieve schema version, validate it against the hardcoded version, and attempt to initialize the sch...
Fetch and Release MySQL Results.
static CfgMgr & instance()
returns a single instance of Configuration Manager
Holds Client identifier or client IPv4 address.
static constexpr size_t MAX_CLIENT_ID_LEN
Maximum size of a client ID.
Holds DUID (DHCPv6 Unique Identifier)
static constexpr size_t MAX_DUID_LEN
maximum duid size
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
std::string toText() const
Returns textual representation of the identifier (e.g.
static void recreate(const std::string &dbaccess, bool preserve_callbacks=true)
Recreate an instance of a lease manager with optionally preserving registered callbacks.
void setExtendedInfoTablesEnabled(const bool enabled)
Modifies the setting whether the lease6 extended info tables are enabled.
static bool upgradeLease6ExtendedInfo(const Lease6Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
Upgrade a V6 lease user context to the new extended info entry.
bool getExtendedInfoTablesEnabled() const
Returns the setting indicating if lease6 extended info tables are enabled.
static void extractLease4ExtendedInfo(const Lease4Ptr &lease, bool ignore_errors=true)
Extract relay and remote identifiers from the extended info.
static bool upgradeLease4ExtendedInfo(const Lease4Ptr &lease, CfgConsistency::ExtendedInfoSanity check=CfgConsistency::EXTENDED_INFO_CHECK_FIX)
The following queries are used to fulfill Bulk Lease Query queries.
virtual bool addExtendedInfo6(const Lease6Ptr &lease)
Extract extended info from a lease6 and add it into tables.
Wraps value holding size of the page with leases.
Base class for fulfilling a statistical lease data query.
SubnetID first_subnet_id_
First (or only) subnet_id in the selection criteria.
SelectMode getSelectMode() const
Returns the selection criteria mode The value returned is based upon the constructor variant used and...
SubnetID last_subnet_id_
Last subnet_id in the selection criteria when a range is given.
Exchange MySQL and Lease4 Data.
std::vector< MYSQL_BIND > createBindForReceive()
Create BIND array to receive data.
MySqlLease4Exchange()
Constructor.
std::vector< MYSQL_BIND > createBindForSend(const Lease4Ptr &lease)
Create MYSQL_BIND objects for Lease4 Pointer.
std::string getErrorColumns()
Return columns in error.
Lease4Ptr getLeaseData()
Copy Received Data into Lease4 Object.
Exchange MySQL and Lease6 Data.
std::vector< MYSQL_BIND > createBindForSend(const Lease6Ptr &lease)
Create MYSQL_BIND objects for Lease6 Pointer.
std::string getErrorColumns()
Return columns in error.
Lease6Ptr getLeaseData()
Copy Received Data into Lease6 Object.
std::vector< MYSQL_BIND > createBindForReceive()
Create BIND array to receive data.
MySqlLease6Exchange()
Constructor.
MySQL Lease Context Pool.
MySqlLeaseContext(const db::DatabaseConnection::ParameterMap ¶meters, db::IOServiceAccessorPtr io_service_accessor, db::DbCallback db_reconnect_callback)
Constructor.
Common MySQL and Lease Data Methods.
static std::string getColumnsInError(my_bool *error, std::string *names, size_t count)
Return columns in error.
static void setErrorIndicators(MYSQL_BIND *bind, my_bool *error, size_t count)
Set error indicators.
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const override
Returns existing IPv6 lease for a given IPv6 address.
virtual std::string getDescription() const override
Returns description of the backend.
virtual size_t wipeLeases6(const SubnetID &subnet_id) override
Removed specified IPv6 leases.
virtual bool deleteLease(const Lease4Ptr &lease) override
Deletes an IPv4 lease.
virtual size_t byRemoteId6size() const override
Return the by-remote-id table size.
static std::string getDBVersion()
Local version of getDBVersion() class method.
virtual bool addLease(const Lease4Ptr &lease) override
Adds an IPv4 lease.
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) override
Deletes all expired-reclaimed DHCPv6 leases.
virtual Lease4Collection getLeases4ByRemoteId(const OptionBuffer &remote_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size, const time_t &qry_start_time=0, const time_t &qry_end_time=0) override
Returns existing IPv4 leases with a given remote-id.
virtual LeaseStatsQueryPtr startLeaseStatsQuery4() override
Creates and runs the IPv4 lease stats query.
virtual Lease4Collection getLeases4ByRelayId(const OptionBuffer &relay_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size, const time_t &qry_start_time=0, const time_t &qry_end_time=0) override
The following queries are used to fulfill Bulk Lease Query queries.
virtual void rollback() override
Rollback Transactions.
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id) override
Creates and runs the IPv4 lease stats query for a single subnet.
virtual void wipeExtendedInfoTables6() override
Wipe by-relay-id table (v6).
virtual Lease6Collection getLeases6() const override
Returns all IPv6 leases.
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const override
Returns a collection of expired DHCPv4 leases.
virtual std::pair< uint32_t, uint32_t > getVersion(const std::string &timer_name=std::string()) const override
Returns backend version.
virtual Lease6Collection getLeases6ByRemoteId(const OptionBuffer &remote_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) override
Returns existing IPv6 leases with a given remote-id.
virtual void addRemoteId6(const isc::asiolink::IOAddress &lease_addr, const std::vector< uint8_t > &remote_id) override
Add lease6 extended info into by-remote-id table.
virtual void commit() override
Commit Transactions.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id) override
Creates and runs the IPv6 lease stats query for a single subnet.
MySqlLeaseContextPtr createContext() const
Create a new context.
virtual void deleteExtendedInfo6(const isc::asiolink::IOAddress &addr) override
Extended information / Bulk Lease Query shared interface.
virtual LeaseStatsQueryPtr startPoolLeaseStatsQuery4() override
Creates and runs the IPv4 lease stats query for all subnets and pools.
virtual Lease6Collection getLeases6ByRelayId(const DUID &relay_id, const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) override
Returns existing IPv6 leases with a given relay-id.
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const override
Returns a collection of expired DHCPv6 leases.
virtual void updateLease6(const Lease6Ptr &lease6) override
Updates IPv6 lease.
MySqlLeaseMgr(const db::DatabaseConnection::ParameterMap ¶meters)
Constructor.
virtual LeaseStatsQueryPtr startPoolLeaseStatsQuery6() override
Creates and runs the IPv6 lease stats query for all subnets and pools.
virtual std::string getName() const override
Returns backend name.
StatementIndex
Statement Tags.
@ SUBNET_RANGE_LEASE6_STATS
@ GET_LEASE6_DUID_IAID_SUBID
@ SUBNET_RANGE_LEASE4_STATS
@ GET_LEASE4_REMOTEID_QST
@ GET_LEASE6_COUNT_BY_CLASS
@ GET_LEASE4_HWADDR_SUBID
@ GET_LEASE4_REMOTEID_QSET
@ GET_LEASE4_COUNT_BY_CLASS
@ DELETE_LEASE4_STATE_EXPIRED
@ GET_LEASE4_RELAYID_QSET
@ GET_LEASE4_CLIENTID_SUBID
@ DELETE_LEASE6_STATE_EXPIRED
@ GET_LEASE4_REMOTEID_QET
virtual LeaseStatsQueryPtr startLeaseStatsQuery6() override
Creates and runs the IPv6 lease stats query.
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const override
Returns an IPv4 lease for specified IPv4 address.
virtual size_t upgradeExtendedInfo4(const LeasePageSize &page_size) override
Upgrade extended info (v4).
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id) override
Creates and runs the IPv6 lease stats query for a single subnet.
virtual void updateLease4(const Lease4Ptr &lease4) override
Updates IPv4 lease.
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id) override
Creates and runs the IPv4 lease stats query for a single subnet.
virtual ~MySqlLeaseMgr()
Destructor (closes database)
virtual size_t upgradeExtendedInfo6(const LeasePageSize &page_size) override
Upgrade extended info (v6).
virtual void addRelayId6(const isc::asiolink::IOAddress &lease_addr, const std::vector< uint8_t > &relay_id) override
Add lease6 extended info into by-relay-id table.
virtual size_t wipeLeases4(const SubnetID &subnet_id) override
Removes specified IPv4 leases.
virtual Lease4Collection getLeases4() const override
Returns all IPv4 leases.
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) override
Deletes all expired-reclaimed DHCPv4 leases.
static bool dbReconnect(util::ReconnectCtlPtr db_reconnect_ctl)
Attempts to reconnect the server to the lease DB backend manager.
virtual size_t byRelayId6size() const override
Return the by-relay-id table size.
MySql derivation of the statistical lease data query.
void start() override
Creates the IPv4 lease statistical data result set.
MySqlLeaseStatsQuery(MySqlConnection &conn, const size_t statement_index, const bool fetch_type, const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Constructor to query for the stats for a range of subnets.
MySqlLeaseStatsQuery(MySqlConnection &conn, const size_t statement_index, const bool fetch_type, const SubnetID &subnet_id)
Constructor to query for a single subnet's stats.
virtual ~MySqlLeaseStatsQuery()
Destructor.
bool getNextRow(LeaseStatsRow &row) override
Fetches the next row in the result set.
MySqlLeaseStatsQuery(MySqlConnection &conn, const size_t statement_index, const bool fetch_type, const bool fetch_pool=false)
Constructor to query for all subnets' stats.
Attempt to update lease that was not there.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
Introduces callbacks into the LeaseMgr.
void trackAddLease(const LeasePtr &lease)
Invokes the callbacks when a new lease is added.
void unlock(const LeasePtr &lease)
Attempts to unlock a lease.
void trackUpdateLease(const LeasePtr &lease)
Invokes the callbacks when a lease is updated.
void trackDeleteLease(const LeasePtr &lease)
Invokes the callbacks when a lease is deleted.
bool hasCallbacks() const
Checks if any callbacks have been registered.
RAII class creating a critical section.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< const Element > ConstElementPtr
const my_bool MLM_FALSE
MySQL false value.
const uint32_t MYSQL_SCHEMA_VERSION_MAJOR
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
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.
std::vector< MySqlBindingPtr > MySqlBindingCollection
Collection of bindings.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME4
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED6
const isc::log::MessageID DHCPSRV_MYSQL_ADD_ADDR4
std::string ClientClass
Defines a single class name.
const isc::log::MessageID DHCPSRV_MYSQL_COMMIT
const isc::log::MessageID DHCPSRV_MYSQL_DELETE_EXPIRED_RECLAIMED6
const isc::log::MessageID DHCPSRV_MYSQL_NEGATIVE_LEASES_STAT
const isc::log::MessageID DHCPSRV_MYSQL_GET_SUBID_PAGE6
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO6
const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_DUID
const isc::log::MessageID DHCPSRV_MYSQL_GET_PAGE4
const isc::log::MessageID DHCPSRV_MYSQL_GET_CLIENTID
const isc::log::MessageID DHCPSRV_MYSQL_NO_TLS
const isc::log::MessageID DHCPSRV_MYSQL_GET4
boost::shared_ptr< CfgDbAccess > CfgDbAccessPtr
A pointer to the CfgDbAccess.
boost::shared_ptr< DUID > DuidPtr
const isc::log::MessageID DHCPSRV_MYSQL_DELETE_ADDR
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO6_ERROR
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
const isc::log::MessageID DHCPSRV_MYSQL_LEASE_DB_RECONNECT_ATTEMPT_FAILED
const isc::log::MessageID DHCPSRV_MYSQL_GET_REMOTEID6
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO4_PAGE
const isc::log::MessageID DHCPSRV_MYSQL_GET_ADDR4
const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_SUBID_DUID
const isc::log::MessageID DHCPSRV_MYSQL_UPDATE_ADDR6
const isc::log::MessageID DHCPSRV_MYSQL_GET_REMOTEID4
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
const int DHCPSRV_DBG_TRACE_DETAIL
Additional information.
const isc::log::MessageID DHCPSRV_MYSQL_GET_SUBID6
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO4
const isc::log::MessageID DHCPSRV_MYSQL_GET_PAGE6
const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED4
const isc::log::MessageID DHCPSRV_MYSQL_UPDATE_ADDR4
const isc::log::MessageID DHCPSRV_MYSQL_GET_HWADDR
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO4_ERROR
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
const isc::log::MessageID DHCPSRV_MYSQL_ADD_ADDR6
const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME6
const isc::log::MessageID DHCPSRV_MYSQL_GET_VERSION
const isc::log::MessageID DHCPSRV_MYSQL_LEASE_DB_RECONNECT_ATTEMPT_SCHEDULE
const size_t HOSTNAME_MAX_LEN
Maximum length of the hostname stored in DNS.
const isc::log::MessageID DHCPSRV_MYSQL_GET_RELAYID4
const isc::log::MessageID DHCPSRV_MYSQL_GET_SUBID4
const isc::log::MessageID DHCPSRV_MYSQL_GET6
const size_t USER_CONTEXT_MAX_LEN
Maximum length of user context.
const isc::log::MessageID DHCPSRV_MYSQL_LEASE_DB_RECONNECT_FAILED
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
const isc::log::MessageID DHCPSRV_MYSQL_UPGRADE_EXTENDED_INFO6_PAGE
const isc::log::MessageID DHCPSRV_MYSQL_GET_SUBID_CLIENTID
const isc::log::MessageID DHCPSRV_MYSQL_DELETE_EXPIRED_RECLAIMED4
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
const isc::log::MessageID DHCPSRV_MYSQL_ROLLBACK
@ HTYPE_ETHER
Ethernet 10Mbps.
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
boost::shared_ptr< MySqlLeaseContext > MySqlLeaseContextPtr
Type of pointers to contexts.
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
const isc::log::MessageID DHCPSRV_MYSQL_GET_ADDR6
const isc::log::MessageID DHCPSRV_MYSQL_GET_SUBID_HWADDR
const isc::log::MessageID DHCPSRV_MYSQL_DELETED_EXPIRED_RECLAIMED
const isc::log::MessageID DHCPSRV_MYSQL_GET_RELAYID6
const isc::log::MessageID DHCPSRV_MYSQL_GET_DUID
const isc::log::MessageID DHCPSRV_MYSQL_TLS_CIPHER
PerfMonMgrPtr mgr
PerfMonMgr singleton.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
Hardware type that represents information from DHCPv4 packet.
static const size_t MAX_HWADDR_LEN
Maximum size of a hardware address.
std::vector< uint8_t > hwaddr_
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
ExtendedInfoAction
Action on extended info tables.
@ ACTION_UPDATE
update extended info tables.
@ ACTION_DELETE
delete reference to the lease
@ ACTION_IGNORE
ignore extended info,
Contains a single row of lease statistical data.
a common structure for IPv4 and IPv6 leases
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire)
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Type
Type of lease or pool.
@ TYPE_TA
the lease contains temporary IPv6 address
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
@ TYPE_NA
the lease contains non-temporary IPv6 address