mysql_lease_mgr.cc 86.7 KB
Newer Older
1
// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config.h>
16

17
#include <asiolink/io_address.h>
Stephen Morris's avatar
Stephen Morris committed
18
#include <dhcp/duid.h>
19
#include <dhcp/hwaddr.h>
20
#include <dhcpsrv/dhcpsrv_log.h>
21
#include <dhcpsrv/mysql_lease_mgr.h>
22

23
#include <boost/static_assert.hpp>
24
#include <mysqld_error.h>
25
26
27

#include <iostream>
#include <iomanip>
28
#include <sstream>
29
30
31
#include <string>
#include <time.h>

32
using namespace isc;
33
using namespace isc::dhcp;
34
35
using namespace std;

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// @file
///
/// This file holds the implementation of the Lease Manager using MySQL.  The
/// implementation uses MySQL's C API, as it comes as standard with the MySQL
/// client libraries.
///
/// In general, each of the database access methods corresponds to one SQL
/// statement.  To avoid the overhead of parsing a statement every time it is
/// used, when the database is opened "prepared statements" are created -
/// essentially doing the SQL parsing up front.  Every time a method is used
/// to access data, the corresponding prepared statement is referenced. Each
/// prepared statement contains a set of placeholders for data, each
/// placeholder being for:
///
/// - data being added to the database (as in adding or updating a lease)
/// - data being retrieved from the database (as in getting lease information)
/// - selection criteria used to determine which records to update/retrieve.
///
/// All such data is associated with the prepared statment using an array of
/// MYSQL_BIND structures.  Each element in the array corresponds to one
/// parameter in the prepared statement - the first element in the array is
/// associated with the first parameter, the second element with the second
/// parameter etc.
///
/// Within this file, the setting up of the MYSQL_BIND arrays for data being
/// passed to and retrieved from the database is handled in the
/// isc::dhcp::MySqlLease4Exchange and isc::dhcp::MySqlLease6Exchange classes.
/// The classes also hold intermediate variables required for exchanging some
/// of the data.
///
/// With these exchange objects in place, many of the methods follow similar
/// logic:
/// - Set up the MYSQL_BIND array for data being transferred to/from the
///   database.  For data being transferred to the database, some of the
///   data is extracted from the lease to intermediate variables, whilst
///   in other cases the MYSQL_BIND arrays point to the data in the lease.
/// - Set up the MYSQL_BIND array for the data selection parameters.
/// - Bind these arrays to the prepared statement.
/// - Execute the statement.
/// - If there is output, copy the data from the bound variables to the output
///   lease object.

78
79
namespace {
///@{
80
81

/// @brief Maximum size of database fields
82
83
84
85
86
///
/// The following constants define buffer sizes for variable length database
/// fields.  The values should be greater than or equal to the length set in
/// the schema definition.
///
87
88
89
/// The exception is the length of any VARCHAR fields: buffers for these should
/// be set greater than or equal to the length of the field plus 1: this allows
/// for the insertion of a trailing null whatever data is returned.
90

91
92
93
94
95
96
/// @brief Maximum size of an IPv6 address represented as a text string.
///
/// This is 32 hexadecimal characters written in 8 groups of four, plus seven
/// colon separators.
const size_t ADDRESS6_TEXT_MAX_LEN = 39;

97
98
99
100
/// @brief MySQL True/False constants
///
/// Declare typed values so as to avoid problems of data conversion.  These
/// are local to the file but are given the prefix MLM (MySql Lease Manager) to
101
/// avoid any likely conflicts with variables in header files named TRUE or
102
/// FALSE.
103
104
105
106

const my_bool MLM_FALSE = 0;                ///< False value
const my_bool MLM_TRUE = 1;                 ///< True value

107
108
109
110
111
112
/// @brief Maximum length of the hostname stored in DNS.
///
/// This length is restricted by the length of the domain-name carried
/// in the Client FQDN %Option (see RFC4702 and RFC4704).
const size_t HOSTNAME_MAX_LEN = 255;

113
///@}
114
115
116

/// @brief MySQL Selection Statements
///
117
118
119
/// Each statement is associated with an index, which is used to reference the
/// associated prepared statement.

120
121
122
123
124
125
struct TaggedStatement {
    MySqlLeaseMgr::StatementIndex index;
    const char*                   text;
};

TaggedStatement tagged_statements[] = {
126
127
    {MySqlLeaseMgr::DELETE_LEASE4,
                    "DELETE FROM lease4 WHERE address = ?"},
128
129
    {MySqlLeaseMgr::DELETE_LEASE6,
                    "DELETE FROM lease6 WHERE address = ?"},
130
131
    {MySqlLeaseMgr::GET_LEASE4_ADDR,
                    "SELECT address, hwaddr, client_id, "
132
133
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname "
134
135
136
137
                            "FROM lease4 "
                            "WHERE address = ?"},
    {MySqlLeaseMgr::GET_LEASE4_CLIENTID,
                    "SELECT address, hwaddr, client_id, "
138
139
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname "
140
141
                            "FROM lease4 "
                            "WHERE client_id = ?"},
142
143
    {MySqlLeaseMgr::GET_LEASE4_CLIENTID_SUBID,
                    "SELECT address, hwaddr, client_id, "
144
145
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname "
146
147
                            "FROM lease4 "
                            "WHERE client_id = ? AND subnet_id = ?"},
148
149
    {MySqlLeaseMgr::GET_LEASE4_HWADDR,
                    "SELECT address, hwaddr, client_id, "
150
151
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname "
152
153
                            "FROM lease4 "
                            "WHERE hwaddr = ?"},
154
155
    {MySqlLeaseMgr::GET_LEASE4_HWADDR_SUBID,
                    "SELECT address, hwaddr, client_id, "
156
157
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname "
158
159
                            "FROM lease4 "
                            "WHERE hwaddr = ? AND subnet_id = ?"},
160
161
162
    {MySqlLeaseMgr::GET_LEASE6_ADDR,
                    "SELECT address, duid, valid_lifetime, "
                        "expire, subnet_id, pref_lifetime, "
163
                        "lease_type, iaid, prefix_len, "
164
165
                        "fqdn_fwd, fqdn_rev, hostname, "
                        "hwaddr, hwtype, hwaddr_source "
166
                            "FROM lease6 "
167
                            "WHERE address = ? AND lease_type = ?"},
168
169
170
    {MySqlLeaseMgr::GET_LEASE6_DUID_IAID,
                    "SELECT address, duid, valid_lifetime, "
                        "expire, subnet_id, pref_lifetime, "
171
                        "lease_type, iaid, prefix_len, "
172
173
                        "fqdn_fwd, fqdn_rev, hostname, "
                        "hwaddr, hwtype, hwaddr_source "
174
                            "FROM lease6 "
175
                            "WHERE duid = ? AND iaid = ? AND lease_type = ?"},
176
177
178
    {MySqlLeaseMgr::GET_LEASE6_DUID_IAID_SUBID,
                    "SELECT address, duid, valid_lifetime, "
                        "expire, subnet_id, pref_lifetime, "
179
                        "lease_type, iaid, prefix_len, "
180
181
                        "fqdn_fwd, fqdn_rev, hostname, "
                        "hwaddr, hwtype, hwaddr_source "
182
                            "FROM lease6 "
183
184
                            "WHERE duid = ? AND iaid = ? AND subnet_id = ? "
                            "AND lease_type = ?"},
185
186
    {MySqlLeaseMgr::GET_VERSION,
                    "SELECT version, minor FROM schema_version"},
187
188
    {MySqlLeaseMgr::INSERT_LEASE4,
                    "INSERT INTO lease4(address, hwaddr, client_id, "
189
190
191
                        "valid_lifetime, expire, subnet_id, "
                        "fqdn_fwd, fqdn_rev, hostname) "
                            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"},
192
193
194
    {MySqlLeaseMgr::INSERT_LEASE6,
                    "INSERT INTO lease6(address, duid, valid_lifetime, "
                        "expire, subnet_id, pref_lifetime, "
195
                        "lease_type, iaid, prefix_len, "
196
197
198
                        "fqdn_fwd, fqdn_rev, hostname, "
                        "hwaddr, hwtype, hwaddr_source) "
                            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
199
200
201
    {MySqlLeaseMgr::UPDATE_LEASE4,
                    "UPDATE lease4 SET address = ?, hwaddr = ?, "
                        "client_id = ?, valid_lifetime = ?, expire = ?, "
202
203
                        "subnet_id = ?, fqdn_fwd = ?, fqdn_rev = ?, "
                        "hostname = ? "
204
                            "WHERE address = ?"},
205
206
207
208
    {MySqlLeaseMgr::UPDATE_LEASE6,
                    "UPDATE lease6 SET address = ?, duid = ?, "
                        "valid_lifetime = ?, expire = ?, subnet_id = ?, "
                        "pref_lifetime = ?, lease_type = ?, iaid = ?, "
209
                        "prefix_len = ?, fqdn_fwd = ?, fqdn_rev = ?, "
210
                        "hostname = ?, hwaddr = ?, hwtype = ?, hwaddr_source = ? "
211
212
213
                            "WHERE address = ?"},
    // End of list sentinel
    {MySqlLeaseMgr::NUM_STATEMENTS, NULL}
214
215
};

216
217
};  // Anonymous namespace

218
219


220
221
222
namespace isc {
namespace dhcp {

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/// @brief Common MySQL and Lease Data Methods
///
/// The MySqlLease4Exchange and MySqlLease6Exchange classes provide the
/// functionaility to set up binding information between variables in the
/// program and data extracted from the database.  This class is the common
/// base to both of them, containing some common methods.

class MySqlLeaseExchange {
public:
    /// @brief Set error indicators
    ///
    /// Sets the error indicator for each of the MYSQL_BIND elements.  It points
    /// the "error" field within an element of the input array to the
    /// corresponding element of the passed error array.
    ///
    /// @param bind Array of BIND elements
    /// @param error Array of error elements.  If there is an error in getting
    ///        data associated with one of the "bind" elements, the
    ///        corresponding element in the error array is set to MLM_TRUE.
    /// @param count Size of each of the arrays.
    void setErrorIndicators(MYSQL_BIND* bind, my_bool* error, size_t count) {
        for (size_t i = 0; i < count; ++i) {
            error[i] = MLM_FALSE;
            bind[i].error = reinterpret_cast<char*>(&error[i]);
        }
    }

    /// @brief Return columns in error
    ///
    /// If an error is returned from a fetch (in particular, a truncated
    /// status), this method can be called to get the names of the fields in
    /// error.  It returns a string comprising the names of the fields
    /// separated by commas.  In the case of there being no error indicators
    /// set, it returns the string "(None)".
    ///
    /// @param error Array of error elements.  An element is set to MLM_TRUE
    ///        if the corresponding column in the database is the source of
    ///        the error.
    /// @param names Array of column names, the same size as the error array.
    /// @param count Size of each of the arrays.
    std::string getColumnsInError(my_bool* error, std::string* names,
                                  size_t count) {
        std::string result = "";

        // Accumulate list of column names
        for (size_t i = 0; i < count; ++i) {
            if (error[i] == MLM_TRUE) {
                if (!result.empty()) {
                    result += ", ";
                }
                result += names[i];
            }
        }

        if (result.empty()) {
            result = "(None)";
        }

        return (result);
    }
};


286
287
288
289
290
/// @brief Exchange MySQL and Lease4 Data
///
/// On any MySQL operation, arrays of MYSQL_BIND structures must be built to
/// describe the parameters in the prepared statements.  Where information is
/// inserted or retrieved - INSERT, UPDATE, SELECT - a large amount of that
291
/// structure is identical.  This class handles the creation of that array.
292
293
///
/// Owing to the MySQL API, the process requires some intermediate variables
294
/// to hold things like data length etc.  This object holds those variables.
295
296
297
298
///
/// @note There are no unit tests for this class.  It is tested indirectly
/// in all MySqlLeaseMgr::xxx4() calls where it is used.

299
class MySqlLease4Exchange : public MySqlLeaseExchange {
300
    /// @brief Set number of database columns for this lease structure
301
    static const size_t LEASE_COLUMNS = 9;
302

303
public:
304
305
    /// @brief Constructor
    ///
306
    /// The initialization of the variables here is only to satisfy cppcheck -
307
    /// all variables are initialized/set in the methods before they are used.
308
    MySqlLease4Exchange() : addr4_(0), hwaddr_length_(0), client_id_length_(0),
309
                            client_id_null_(MLM_FALSE),
310
                            fqdn_fwd_(false), fqdn_rev_(false), hostname_length_(0) {
311
312
        memset(hwaddr_buffer_, 0, sizeof(hwaddr_buffer_));
        memset(client_id_buffer_, 0, sizeof(client_id_buffer_));
313
        memset(hostname_buffer_, 0, sizeof(hostname_buffer_));
314
        std::fill(&error_[0], &error_[LEASE_COLUMNS], MLM_FALSE);
315

316
317
318
319
320
321
322
        // Set the column names (for error messages)
        columns_[0] = "address";
        columns_[1] = "hwaddr";
        columns_[2] = "client_id";
        columns_[3] = "valid_lifetime";
        columns_[4] = "expire";
        columns_[5] = "subnet_id";
323
324
325
326
        columns_[6] = "fqdn_fwd";
        columns_[7] = "fqdn_rev";
        columns_[8] = "hostname";
        BOOST_STATIC_ASSERT(8 < LEASE_COLUMNS);
327
328
329
330
    }

    /// @brief Create MYSQL_BIND objects for Lease4 Pointer
    ///
331
332
    /// Fills in the MYSQL_BIND array for sending data in the Lease4 object to
    /// the database.
333
    ///
334
335
    /// @param lease Lease object to be added to the database.  None of the
    ///        fields in the lease are modified - the lease data is only read.
336
337
338
    ///
    /// @return Vector of MySQL BIND objects representing the data to be added.
    std::vector<MYSQL_BIND> createBindForSend(const Lease4Ptr& lease) {
339

340
341
342
        // Store lease object to ensure it remains valid.
        lease_ = lease;

343
        // Initialize prior to constructing the array of MYSQL_BIND structures.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
344
345
346
347
        // It sets all fields, including is_null, to zero, so we need to set
        // is_null only if it should be true. This gives up minor performance
        // benefit while being safe approach. For improved readability, the
        // code that explicitly sets is_null is there, but is commented out.
348
349
        memset(bind_, 0, sizeof(bind_));

350
351
352
        // Set up the structures for the various components of the lease4
        // structure.

353
        // Address: uint32_t
354
        // The address in the Lease structure is an IOAddress object.  Convert
355
        // this to an integer for storage.
356
357
358
        addr4_ = static_cast<uint32_t>(lease_->addr_);
        bind_[0].buffer_type = MYSQL_TYPE_LONG;
        bind_[0].buffer = reinterpret_cast<char*>(&addr4_);
359
        bind_[0].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
360
361
        // bind_[0].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
362

363
        // hwaddr: varbinary(128)
364
365
        // For speed, we avoid copying the data into temporary storage and
        // instead extract it from the lease structure directly.
366
        hwaddr_length_ = lease_->hwaddr_->hwaddr_.size();
367
        bind_[1].buffer_type = MYSQL_TYPE_BLOB;
368
        bind_[1].buffer = reinterpret_cast<char*>(&(lease_->hwaddr_->hwaddr_[0]));
369
370
        bind_[1].buffer_length = hwaddr_length_;
        bind_[1].length = &hwaddr_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
371
372
        // bind_[1].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
373

374
        // client_id: varbinary(128)
375
376
377
378
379
380
381
        if (lease_->client_id_) {
            client_id_ = lease_->client_id_->getClientId();
            client_id_length_ = client_id_.size();
            bind_[2].buffer_type = MYSQL_TYPE_BLOB;
            bind_[2].buffer = reinterpret_cast<char*>(&client_id_[0]);
            bind_[2].buffer_length = client_id_length_;
            bind_[2].length = &client_id_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
382
383
            // bind_[2].is_null = &MLM_FALSE; // commented out for performance
                                              // reasons, see memset() above
384
385
386
387
388
389
390
391
        } else {
            bind_[2].buffer_type = MYSQL_TYPE_NULL;

            // According to http://dev.mysql.com/doc/refman/5.5/en/
            // c-api-prepared-statement-data-structures.html, the other
            // fields doesn't matter if type is set to MYSQL_TYPE_NULL,
            // but let's set them to some sane values in case earlier versions
            // didn't have that assumption.
392
            client_id_null_ = MLM_TRUE;
393
            bind_[2].buffer = NULL;
394
            bind_[2].is_null = &client_id_null_;
395
        }
396
397
398
399

        // valid lifetime: unsigned int
        bind_[3].buffer_type = MYSQL_TYPE_LONG;
        bind_[3].buffer = reinterpret_cast<char*>(&lease_->valid_lft_);
400
        bind_[3].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
401
402
        // bind_[3].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
403
404
405
406

        // expire: timestamp
        // The lease structure holds the client last transmission time (cltt_)
        // For convenience for external tools, this is converted to lease
407
        // expiry time (expire).  The relationship is given by:
408
409
410
        //
        // expire = cltt_ + valid_lft_
        //
411
        // @todo Handle overflows - a large enough valid_lft_ could cause
412
        //       an overflow on a 32-bit system.
413
414
415
416
417
        MySqlLeaseMgr::convertToDatabaseTime(lease_->cltt_, lease_->valid_lft_,
                                             expire_);
        bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
        bind_[4].buffer = reinterpret_cast<char*>(&expire_);
        bind_[4].buffer_length = sizeof(expire_);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
418
419
        // bind_[4].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
420
421
422
423
424

        // subnet_id: unsigned int
        // Can use lease_->subnet_id_ directly as it is of type uint32_t.
        bind_[5].buffer_type = MYSQL_TYPE_LONG;
        bind_[5].buffer = reinterpret_cast<char*>(&lease_->subnet_id_);
425
        bind_[5].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
426
427
        // bind_[5].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
428

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        // fqdn_fwd: boolean
        bind_[6].buffer_type = MYSQL_TYPE_TINY;
        bind_[6].buffer = reinterpret_cast<char*>(&lease_->fqdn_fwd_);
        bind_[6].is_unsigned = MLM_TRUE;
        // bind_[6].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // fqdn_rev: boolean
        bind_[7].buffer_type = MYSQL_TYPE_TINY;
        bind_[7].buffer = reinterpret_cast<char*>(&lease_->fqdn_rev_);
        bind_[7].is_unsigned = MLM_TRUE;
        // bind_[7].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // hostname: varchar(255)
        bind_[8].buffer_type = MYSQL_TYPE_VARCHAR;
        bind_[8].buffer = const_cast<char*>(lease_->hostname_.c_str());
        bind_[8].buffer_length = lease_->hostname_.length();
        // bind_[8].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

450
451
452
453
        // Add the error flags
        setErrorIndicators(bind_, error_, LEASE_COLUMNS);

        // .. and check that we have the numbers correct at compile time.
454
        BOOST_STATIC_ASSERT(8 < LEASE_COLUMNS);
455

456
457
        // Add the data to the vector.  Note the end element is one after the
        // end of the array.
458
        return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
459
460
461
462
463
    }

    /// @brief Create BIND array to receive data
    ///
    /// Creates a MYSQL_BIND array to receive Lease4 data from the database.
464
    /// After data is successfully received, getLeaseData() can be used to copy
465
466
467
468
    /// it to a Lease6 object.
    ///
    std::vector<MYSQL_BIND> createBindForReceive() {

469
        // Initialize MYSQL_BIND array.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
470
471
472
473
        // It sets all fields, including is_null, to zero, so we need to set
        // is_null only if it should be true. This gives up minor performance
        // benefit while being safe approach. For improved readability, the
        // code that explicitly sets is_null is there, but is commented out.
474
475
476
477
478
        memset(bind_, 0, sizeof(bind_));

        // address:  uint32_t
        bind_[0].buffer_type = MYSQL_TYPE_LONG;
        bind_[0].buffer = reinterpret_cast<char*>(&addr4_);
479
        bind_[0].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
480
481
        // bind_[0].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
482
483
484
485
486
487
488

        // hwaddr: varbinary(20)
        hwaddr_length_ = sizeof(hwaddr_buffer_);
        bind_[1].buffer_type = MYSQL_TYPE_BLOB;
        bind_[1].buffer = reinterpret_cast<char*>(hwaddr_buffer_);
        bind_[1].buffer_length = hwaddr_length_;
        bind_[1].length = &hwaddr_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
489
490
        // bind_[1].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
491
492
493
494
495
496
497

        // client_id: varbinary(128)
        client_id_length_ = sizeof(client_id_buffer_);
        bind_[2].buffer_type = MYSQL_TYPE_BLOB;
        bind_[2].buffer = reinterpret_cast<char*>(client_id_buffer_);
        bind_[2].buffer_length = client_id_length_;
        bind_[2].length = &client_id_length_;
498
        bind_[2].is_null = &client_id_null_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
499
500
        // bind_[2].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
501
502
503
504

        // lease_time: unsigned int
        bind_[3].buffer_type = MYSQL_TYPE_LONG;
        bind_[3].buffer = reinterpret_cast<char*>(&valid_lifetime_);
505
        bind_[3].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
506
507
        // bind_[3].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
508
509
510
511
512

        // expire: timestamp
        bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
        bind_[4].buffer = reinterpret_cast<char*>(&expire_);
        bind_[4].buffer_length = sizeof(expire_);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
513
514
        // bind_[4].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
515
516
517
518

        // subnet_id: unsigned int
        bind_[5].buffer_type = MYSQL_TYPE_LONG;
        bind_[5].buffer = reinterpret_cast<char*>(&subnet_id_);
519
        bind_[5].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
520
521
        // bind_[5].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
522

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
        // fqdn_fwd: boolean
        bind_[6].buffer_type = MYSQL_TYPE_TINY;
        bind_[6].buffer = reinterpret_cast<char*>(&fqdn_fwd_);
        bind_[6].is_unsigned = MLM_TRUE;
        // bind_[6].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // fqdn_rev: boolean
        bind_[7].buffer_type = MYSQL_TYPE_TINY;
        bind_[7].buffer = reinterpret_cast<char*>(&fqdn_rev_);
        bind_[7].is_unsigned = MLM_TRUE;
        // bind_[7].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // hostname: varchar(255)
        hostname_length_ = sizeof(hostname_buffer_);
        bind_[8].buffer_type = MYSQL_TYPE_STRING;
        bind_[8].buffer = reinterpret_cast<char*>(hostname_buffer_);
        bind_[8].buffer_length = hostname_length_;
        bind_[8].length = &hostname_length_;
        // bind_[8].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

546
547
548
549
        // Add the error flags
        setErrorIndicators(bind_, error_, LEASE_COLUMNS);

        // .. and check that we have the numbers correct at compile time.
550
        BOOST_STATIC_ASSERT(8 < LEASE_COLUMNS);
551

552
553
        // Add the data to the vector.  Note the end element is one after the
        // end of the array.
554
        return(std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
555
556
557
558
559
    }

    /// @brief Copy Received Data into Lease6 Object
    ///
    /// Called after the MYSQL_BIND array created by createBindForReceive()
560
    /// has been used, this copies data from the internal member variables
561
    /// into a Lease4 objec.
562
    ///
563
    /// @return Lease4Ptr Pointer to a Lease6 object holding the relevant
564
565
    ///         data.
    Lease4Ptr getLeaseData() {
566
567
        // Convert times received from the database to times for the lease
        // structure
568
569
570
        time_t cltt = 0;
        MySqlLeaseMgr::convertFromDatabaseTime(expire_, valid_lifetime_, cltt);

571
572
573
574
575
        if (client_id_null_==MLM_TRUE) {
            // There's no client-id, so we pass client-id_length_ set to 0
            client_id_length_ = 0;
        }

576
577
578
579
580
        // Hostname is passed to Lease4 as a string object. We have to create
        // it from the buffer holding hostname and the buffer length.
        std::string hostname(hostname_buffer_,
                             hostname_buffer_ + hostname_length_);

581
582
583
        // Recreate the hardware address.
        HWAddrPtr hwaddr(new HWAddr(hwaddr_buffer_, hwaddr_length_, HTYPE_ETHER));

584
        // note that T1 and T2 are not stored
585
        return (Lease4Ptr(new Lease4(addr4_, hwaddr,
586
                                     client_id_buffer_, client_id_length_,
587
588
                                     valid_lifetime_, 0, 0, cltt, subnet_id_,
                                     fqdn_fwd_, fqdn_rev_, hostname)));
589
590
    }

591
592
593
594
595
596
597
598
599
600
601
602
603
604
    /// @brief Return columns in error
    ///
    /// If an error is returned from a fetch (in particular, a truncated
    /// status), this method can be called to get the names of the fields in
    /// error.  It returns a string comprising the names of the fields
    /// separated by commas.  In the case of there being no error indicators
    /// set, it returns the string "(None)".
    ///
    /// @return Comma-separated list of columns in error, or the string
    ///         "(None)".
    std::string getErrorColumns() {
        return (getColumnsInError(error_, columns_, LEASE_COLUMNS));
    }

605
private:
606

607
    // Note: All array lengths are equal to the corresponding variable in the
608
609
    //       schema.
    // Note: Arrays are declared fixed length for speed of creation
610
    uint32_t        addr4_;             ///< IPv4 address
611
612
613
    MYSQL_BIND      bind_[LEASE_COLUMNS]; ///< Bind array
    std::string     columns_[LEASE_COLUMNS];///< Column names
    my_bool         error_[LEASE_COLUMNS];  ///< Error array
614
    std::vector<uint8_t> hwaddr_;       ///< Hardware address
615
    uint8_t         hwaddr_buffer_[HWAddr::MAX_HWADDR_LEN];
616
                                        ///< Hardware address buffer
617
618
    unsigned long   hwaddr_length_;     ///< Hardware address length
    std::vector<uint8_t> client_id_;    ///< Client identification
Stephen Morris's avatar
Stephen Morris committed
619
    uint8_t         client_id_buffer_[ClientId::MAX_CLIENT_ID_LEN];
620
                                        ///< Client ID buffer
621
    unsigned long   client_id_length_;  ///< Client ID address length
622
623
    my_bool         client_id_null_;    ///< Is Client ID null?

624
625
626
    MYSQL_TIME      expire_;            ///< Lease expiry time
    Lease4Ptr       lease_;             ///< Pointer to lease object
    uint32_t        subnet_id_;         ///< Subnet identification
627
    uint32_t        valid_lifetime_;    ///< Lease time
628
629
630
631
632
633
634
635
636

    my_bool         fqdn_fwd_;          ///< Has forward DNS update been
                                        ///< performed
    my_bool         fqdn_rev_;          ///< Has reverse DNS update been
                                        ///< performed
    char            hostname_buffer_[HOSTNAME_MAX_LEN];
                                        ///< Client hostname
    unsigned long   hostname_length_;   ///< Client hostname length

637
638
639
640
};



641
/// @brief Exchange MySQL and Lease6 Data
642
///
643
644
/// On any MySQL operation, arrays of MYSQL_BIND structures must be built to
/// describe the parameters in the prepared statements.  Where information is
645
/// inserted or retrieved - INSERT, UPDATE, SELECT - a large amount of that
646
/// structure is identical.  This class handles the creation of that array.
647
///
648
/// Owing to the MySQL API, the process requires some intermediate variables
649
/// to hold things like data length etc.  This object holds those variables.
650
651
652
///
/// @note There are no unit tests for this class.  It is tested indirectly
/// in all MySqlLeaseMgr::xxx6() calls where it is used.
653

654
class MySqlLease6Exchange : public MySqlLeaseExchange {
655
    /// @brief Set number of database columns for this lease structure
656
    static const size_t LEASE_COLUMNS = 15;
657

658
659
public:
    /// @brief Constructor
660
    ///
661
662
    /// The initialization of the variables here is nonly to satisfy cppcheck -
    /// all variables are initialized/set in the methods before they are used.
663
664
    MySqlLease6Exchange() : addr6_length_(0), duid_length_(0),
                            fqdn_fwd_(false), fqdn_rev_(false),
665
666
                            hostname_length_(0), hwaddr_length_(0),
                            hwaddr_null_(MLM_FALSE), hwtype_(0), hwaddr_source_(0) {
667
668
        memset(addr6_buffer_, 0, sizeof(addr6_buffer_));
        memset(duid_buffer_, 0, sizeof(duid_buffer_));
669
        memset(hostname_buffer_, 0, sizeof(hostname_buffer_));
670
        memset(hwaddr_buffer_, 0, sizeof(hwaddr_buffer_));
671
        std::fill(&error_[0], &error_[LEASE_COLUMNS], MLM_FALSE);
672

673
        // Set the column names (for error messages)
674
675
676
677
678
679
680
681
682
683
684
685
        columns_[0]  = "address";
        columns_[1]  = "duid";
        columns_[2]  = "valid_lifetime";
        columns_[3]  = "expire";
        columns_[4]  = "subnet_id";
        columns_[5]  = "pref_lifetime";
        columns_[6]  = "lease_type";
        columns_[7]  = "iaid";
        columns_[8]  = "prefix_len";
        columns_[9]  = "fqdn_fwd";
        columns_[10] = "fqdn_rev";
        columns_[11] = "hostname";
686
687
688
689
        columns_[12] = "hwaddr";
        columns_[13] = "hwtype";
        columns_[14] = "hwaddr_source";
        BOOST_STATIC_ASSERT(14 < LEASE_COLUMNS);
690
691
692
693
    }

    /// @brief Create MYSQL_BIND objects for Lease6 Pointer
    ///
694
695
    /// Fills in the MYSQL_BIND array for sending data in the Lease4 object to
    /// the database.
696
    ///
697
698
699
700
    /// @param lease Lease object to be added to the database.
    ///
    /// @return Vector of MySQL BIND objects representing the data to be added.
    std::vector<MYSQL_BIND> createBindForSend(const Lease6Ptr& lease) {
701
702
703
        // Store lease object to ensure it remains valid.
        lease_ = lease;

704
705
        // Ensure bind_ array clear for constructing the MYSQL_BIND structures
        // for this lease.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
706
707
708
709
        // It sets all fields, including is_null, to zero, so we need to set
        // is_null only if it should be true. This gives up minor performance
        // benefit while being safe approach. For improved readability, the
        // code that explicitly sets is_null is there, but is commented out.
710
711
        memset(bind_, 0, sizeof(bind_));

712
        // address: varchar(39)
713
714
715
        addr6_ = lease_->addr_.toText();
        addr6_length_ = addr6_.size();

716
717
718
        // In the following statement, the string is being read.  However, the
        // MySQL C interface does not use "const", so the "buffer" element
        // is declared as "char*" instead of "const char*".  To resolve this,
719
        // the "const" is discarded.  (Note that the address of addr6_.c_str()
720
        // is guaranteed to be valid until the next non-const operation on
721
722
        // addr6_.)
        //
723
724
725
        // The const_cast could be avoided by copying the string to a writeable
        // buffer and storing the address of that in the "buffer" element.
        // However, this introduces a copy operation (with additional overhead)
726
        // purely to get round the structures introduced by design of the
727
728
729
        // MySQL interface (which uses the area pointed to by "buffer" as input
        // when specifying query parameters and as output when retrieving data).
        // For that reason, "const_cast" has been used.
730
731
732
733
        bind_[0].buffer_type = MYSQL_TYPE_STRING;
        bind_[0].buffer = const_cast<char*>(addr6_.c_str());
        bind_[0].buffer_length = addr6_length_;
        bind_[0].length = &addr6_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
734
735
        // bind_[0].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
736

737
        // duid: varchar(128)
738
739
740
741
        if (!lease_->duid_) {
            isc_throw(DbOperationError, "lease6 for address " << addr6_
                      << " is missing mandatory client-id.");
        }
742
743
        duid_ = lease_->duid_->getDuid();
        duid_length_ = duid_.size();
744

745
746
747
748
        bind_[1].buffer_type = MYSQL_TYPE_BLOB;
        bind_[1].buffer = reinterpret_cast<char*>(&(duid_[0]));
        bind_[1].buffer_length = duid_length_;
        bind_[1].length = &duid_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
749
750
        // bind_[1].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
751

752
        // valid lifetime: unsigned int
753
        bind_[2].buffer_type = MYSQL_TYPE_LONG;
754
        bind_[2].buffer = reinterpret_cast<char*>(&lease_->valid_lft_);
755
        bind_[2].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
756
757
        // bind_[2].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
758
759

        // expire: timestamp
760
761
762
763
764
        // The lease structure holds the client last transmission time (cltt_)
        // For convenience for external tools, this is converted to lease
        /// expiry time (expire).  The relationship is given by:
        //
        // expire = cltt_ + valid_lft_
765
        //
766
        // @todo Handle overflows
767
768
        MySqlLeaseMgr::convertToDatabaseTime(lease_->cltt_, lease_->valid_lft_,
                                             expire_);
769
770
771
        bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
        bind_[3].buffer = reinterpret_cast<char*>(&expire_);
        bind_[3].buffer_length = sizeof(expire_);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
772
773
        // bind_[3].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
774
775
776

        // subnet_id: unsigned int
        // Can use lease_->subnet_id_ directly as it is of type uint32_t.
777
778
        bind_[4].buffer_type = MYSQL_TYPE_LONG;
        bind_[4].buffer = reinterpret_cast<char*>(&lease_->subnet_id_);
779
        bind_[4].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
780
781
        // bind_[4].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
782
783
784

        // pref_lifetime: unsigned int
        // Can use lease_->preferred_lft_ directly as it is of type uint32_t.
785
786
        bind_[5].buffer_type = MYSQL_TYPE_LONG;
        bind_[5].buffer = reinterpret_cast<char*>(&lease_->preferred_lft_);
787
        bind_[5].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
788
789
        // bind_[5].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
790
791

        // lease_type: tinyint
792
        // Must convert to uint8_t as lease_->type_ is a LeaseType variable.
793
        lease_type_ = lease_->type_;
794
795
        bind_[6].buffer_type = MYSQL_TYPE_TINY;
        bind_[6].buffer = reinterpret_cast<char*>(&lease_type_);
796
        bind_[6].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
797
798
        // bind_[6].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
799
800
801

        // iaid: unsigned int
        // Can use lease_->iaid_ directly as it is of type uint32_t.
802
803
        bind_[7].buffer_type = MYSQL_TYPE_LONG;
        bind_[7].buffer = reinterpret_cast<char*>(&lease_->iaid_);
804
        bind_[7].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
805
806
        // bind_[7].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
807
808
809

        // prefix_len: unsigned tinyint
        // Can use lease_->prefixlen_ directly as it is uint32_t.
810
811
        bind_[8].buffer_type = MYSQL_TYPE_TINY;
        bind_[8].buffer = reinterpret_cast<char*>(&lease_->prefixlen_);
812
        bind_[8].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
813
814
        // bind_[8].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
815

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
        // fqdn_fwd: boolean
        bind_[9].buffer_type = MYSQL_TYPE_TINY;
        bind_[9].buffer = reinterpret_cast<char*>(&lease_->fqdn_fwd_);
        bind_[9].is_unsigned = MLM_TRUE;
        // bind_[7].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // fqdn_rev: boolean
        bind_[10].buffer_type = MYSQL_TYPE_TINY;
        bind_[10].buffer = reinterpret_cast<char*>(&lease_->fqdn_rev_);
        bind_[10].is_unsigned = MLM_TRUE;
        // bind_[10].is_null = &MLM_FALSE; // commented out for performance
                                           // reasons, see memset() above

        // hostname: varchar(255)
        bind_[11].buffer_type = MYSQL_TYPE_VARCHAR;
        bind_[11].buffer = const_cast<char*>(lease_->hostname_.c_str());
        bind_[11].buffer_length = lease_->hostname_.length();
        // bind_[11].is_null = &MLM_FALSE; // commented out for performance
                                           // reasons, see memset() above

837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
        // hwaddr: varbinary(20) - hardware/MAC address
        HWAddrPtr hwaddr = lease_->hwaddr_;
        if (hwaddr) {
            hwaddr_ = hwaddr->hwaddr_;
            hwaddr_length_ = hwaddr->hwaddr_.size();

            bind_[12].buffer_type = MYSQL_TYPE_BLOB;
            bind_[12].buffer = reinterpret_cast<char*>(&(hwaddr_[0]));
            bind_[12].buffer_length = hwaddr_length_;
            bind_[12].length = &hwaddr_length_;
        } else {
            bind_[12].buffer_type = MYSQL_TYPE_NULL;

            // According to http://dev.mysql.com/doc/refman/5.5/en/
            // c-api-prepared-statement-data-structures.html, the other
            // fields doesn't matter if type is set to MYSQL_TYPE_NULL,
            // but let's set them to some sane values in case earlier versions
            // didn't have that assumption.
            hwaddr_null_ = MLM_TRUE;
            bind_[12].buffer = NULL;
            bind_[12].is_null = &hwaddr_null_;
        }

        // hwtype
        if (hwaddr) {
            hwtype_ = lease->hwaddr_->htype_;
            bind_[13].buffer_type = MYSQL_TYPE_SHORT;
            bind_[13].buffer = reinterpret_cast<char*>(&hwtype_);
            bind_[13].is_unsigned = MLM_TRUE;
        } else {
            hwtype_ = 0;
            bind_[13].buffer_type = MYSQL_TYPE_NULL;
            // According to http://dev.mysql.com/doc/refman/5.5/en/
            // c-api-prepared-statement-data-structures.html, the other
            // fields doesn't matter if type is set to MYSQL_TYPE_NULL,
            // but let's set them to some sane values in case earlier versions
            // didn't have that assumption.
            hwaddr_null_ = MLM_TRUE;
            bind_[13].buffer = NULL;
            bind_[13].is_null = &hwaddr_null_;
        }

        /// Hardware source
        if (hwaddr) {
            hwaddr_source_ = lease->hwaddr_->source_;
            bind_[14].buffer_type = MYSQL_TYPE_LONG;
            bind_[14].buffer = reinterpret_cast<char*>(&hwaddr_source_);
            bind_[14].is_unsigned = MLM_TRUE;
        } else {
            hwaddr_source_ = 0;

            bind_[14].buffer_type = MYSQL_TYPE_NULL;
            // According to http://dev.mysql.com/doc/refman/5.5/en/
            // c-api-prepared-statement-data-structures.html, the other
            // fields doesn't matter if type is set to MYSQL_TYPE_NULL,
            // but let's set them to some sane values in case earlier versions
            // didn't have that assumption.
            hwaddr_null_ = MLM_TRUE;
            bind_[14].buffer = NULL;
            bind_[14].is_null = &hwaddr_null_;
        }

899
900
901
902
        // Add the error flags
        setErrorIndicators(bind_, error_, LEASE_COLUMNS);

        // .. and check that we have the numbers correct at compile time.
903
        BOOST_STATIC_ASSERT(14 < LEASE_COLUMNS);
904

905
906
        // Add the data to the vector.  Note the end element is one after the
        // end of the array.
907
        return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
908
909
    }

910
911
912
913
914
915
    /// @brief Create BIND array to receive data
    ///
    /// Creates a MYSQL_BIND array to receive Lease6 data from the database.
    /// After data is successfully received, getLeaseData() is used to copy
    /// it to a Lease6 object.
    ///
916
917
    /// @return Vector of MySQL BIND objects passed to the MySQL data retrieval
    ///         functions.
918
    std::vector<MYSQL_BIND> createBindForReceive() {
919

920
        // Initialize MYSQL_BIND array.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
921
922
923
924
        // It sets all fields, including is_null, to zero, so we need to set
        // is_null only if it should be true. This gives up minor performance
        // benefit while being safe approach. For improved readability, the
        // code that explicitly sets is_null is there, but is commented out.
925
926
        memset(bind_, 0, sizeof(bind_));

927
        // address:  varchar(39)
928
        // A Lease6_ address has a maximum of 39 characters.  The array is
929
930
        // one byte longer than this to guarantee that we can always null
        // terminate it whatever is returned.
931
932
933
934
935
        addr6_length_ = sizeof(addr6_buffer_) - 1;
        bind_[0].buffer_type = MYSQL_TYPE_STRING;
        bind_[0].buffer = addr6_buffer_;
        bind_[0].buffer_length = addr6_length_;
        bind_[0].length = &addr6_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
936
937
        // bind_[0].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
938
939

        // client_id: varbinary(128)
940
        duid_length_ = sizeof(duid_buffer_);
941
942
943
944
        bind_[1].buffer_type = MYSQL_TYPE_BLOB;
        bind_[1].buffer = reinterpret_cast<char*>(duid_buffer_);
        bind_[1].buffer_length = duid_length_;
        bind_[1].length = &duid_length_;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
945
946
        // bind_[1].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
947
948

        // lease_time: unsigned int
949
950
        bind_[2].buffer_type = MYSQL_TYPE_LONG;
        bind_[2].buffer = reinterpret_cast<char*>(&valid_lifetime_);
951
        bind_[2].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
952
953
        // bind_[2].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
954
955

        // expire: timestamp
956
957
958
        bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
        bind_[3].buffer = reinterpret_cast<char*>(&expire_);
        bind_[3].buffer_length = sizeof(expire_);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
959
960
        // bind_[3].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
961
962

        // subnet_id: unsigned int
963
964
        bind_[4].buffer_type = MYSQL_TYPE_LONG;
        bind_[4].buffer = reinterpret_cast<char*>(&subnet_id_);
965
        bind_[4].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
966
967
        // bind_[4].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
968
969

        // pref_lifetime: unsigned int
970
        bind_[5].buffer_type = MYSQL_TYPE_LONG;
971
        bind_[5].buffer = reinterpret_cast<char*>(&pref_lifetime_);
972
        bind_[5].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
973
974
        // bind_[5].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
975

976
977
978
        // lease_type: tinyint
        bind_[6].buffer_type = MYSQL_TYPE_TINY;
        bind_[6].buffer = reinterpret_cast<char*>(&lease_type_);
979
        bind_[6].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
980
981
        // bind_[6].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
982

983
984
985
        // iaid: unsigned int
        bind_[7].buffer_type = MYSQL_TYPE_LONG;
        bind_[7].buffer = reinterpret_cast<char*>(&iaid_);
986
        bind_[7].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
987
988
        // bind_[7].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
989

990
        // prefix_len: unsigned tinyint
991
992
        bind_[8].buffer_type = MYSQL_TYPE_TINY;
        bind_[8].buffer = reinterpret_cast<char*>(&prefixlen_);
993
        bind_[8].is_unsigned = MLM_TRUE;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
994
995
        // bind_[8].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above
996

997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
        // fqdn_fwd: boolean
        bind_[9].buffer_type = MYSQL_TYPE_TINY;
        bind_[9].buffer = reinterpret_cast<char*>(&fqdn_fwd_);
        bind_[9].is_unsigned = MLM_TRUE;
        // bind_[9].is_null = &MLM_FALSE; // commented out for performance
                                          // reasons, see memset() above

        // fqdn_rev: boolean
        bind_[10].buffer_type = MYSQL_TYPE_TINY;
        bind_[10].buffer = reinterpret_cast<char*>(&fqdn_rev_);
        bind_[10].is_unsigned = MLM_TRUE;
        // bind_[10].is_null = &MLM_FALSE; // commented out for performance
                                           // reasons, see memset() above

        // hostname: varchar(255)
        hostname_length_ = sizeof(hostname_buffer_);
        bind_[11].buffer_type = MYSQL_TYPE_STRING;
        bind_[11].buffer = reinterpret_cast<char*>(hostname_buffer_);
        bind_[11].buffer_length = hostname_length_;
        bind_[11].length = &hostname_length_;
        // bind_[11].is_null = &MLM_FALSE; // commented out for performance
                                           // reasons, see memset() above

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
        // hardware address
        // hwaddr: varbinary(20)
        hwaddr_null_ = MLM_FALSE;
        hwaddr_length_ = sizeof(hwaddr_buffer_);
        bind_[12].buffer_type = MYSQL_TYPE_BLOB;
        bind_[12].buffer = reinterpret_cast<char*>(hwaddr_buffer_);
        bind_[12].buffer_length = hwaddr_length_;
        bind_[12].length = &hwaddr_length_;
        bind_[12].is_null = &hwaddr_null_;

        // hardware type: unsigned short int (16 bits)
        bind_[13].buffer_type = MYSQL_TYPE_SHORT;
        bind_[13].buffer = reinterpret_cast<char*>(&hwtype_);
        bind_[13].is_unsigned = MLM_TRUE;

        // hardware source: unsigned int (32 bits)
        bind_[14].buffer_type = MYSQL_TYPE_LONG;
        bind_[14].buffer = reinterpret_cast<char*>(&hwaddr_source_);
        bind_[14].is_unsigned = MLM_TRUE;

1040
1041
1042
1043
        // Add the error flags
        setErrorIndicators(bind_, error_, LEASE_COLUMNS);

        // .. and check that we have the numbers correct at compile time.
1044
        BOOST_STATIC_ASSERT(14 < LEASE_COLUMNS);
1045

1046
1047
        // Add the data to the vector.  Note the end element is one after the
        // end of the array.
1048
        return(std::vector<MYSQL_BIND>(&bind_[0], &bind_[LEASE_COLUMNS]));
1049
1050
1051
1052
1053
    }

    /// @brief Copy Received Data into Lease6 Object
    ///
    /// Called after the MYSQL_BIND array created by createBindForReceive()
1054
    /// has been used, this copies data from the internal member variables
1055
1056
1057
1058
1059
    /// into a Lease6 object.
    ///
    /// @return Lease6Ptr Pointer to a Lease6 object holding the relevant
    ///         data.
    ///
1060
    /// @throw isc::BadValue Unable to convert Lease Type value in database
1061
    Lease6Ptr getLeaseData() {
1062
1063
        // The address buffer is declared larger than the buffer size passed
        // to the access function so that we can always append a null byte.
1064
        // Create the IOAddress object corresponding to the received data.
1065
1066
        addr6_buffer_[addr6_length_] = '\0';
        std::string address = addr6_buffer_;
1067
        isc::asiolink::IOAddress addr(address);
1068

1069
        // Set the lease type in a variable of the appropriate data type, which
1070
        // has been initialized with an arbitrary (but valid) value.
1071
        Lease::Type type = Lease::TYPE_NA;
1072
        switch (lease_type_) {
1073
1074
            case Lease::TYPE_NA:
                type = Lease::TYPE_NA;
1075
1076
                break;

1077
1078
            case Lease::TYPE_TA:
                type = Lease::TYPE_TA;
1079
1080
                break;

1081
1082
            case Lease::TYPE_PD:
                type = Lease::TYPE_PD;
1083
1084
1085
                break;

            default:
1086
                isc_throw(BadValue, "invalid lease type returned (" <<
1087
1088
1089
                          static_cast<int>(lease_type_) << ") for lease with "
                          << "address " << address << ". Only 0, 1, or 2 are "
                          << "allowed.");
1090
        }
1091
1092
1093
1094

        // Set up DUID,
        DuidPtr duid_ptr(new DUID(duid_buffer_, duid_length_));

1095
1096
1097
1098
1099
        // Hostname is passed to Lease6 as a string object, so we have to
        // create it from the hostname buffer and length.
        std::string hostname(hostname_buffer_,
                             hostname_buffer_ + hostname_length_);

1100
        /// Set hardware address if it was set
1101
        HWAddrPtr hwaddr;
1102
1103
1104
1105
        if (hwaddr_null_ == MLM_FALSE) {
            hwaddr.reset(new HWAddr(hwaddr_buffer_, hwaddr_length_, hwtype_));
            hwaddr->source_ = hwaddr_source_;
        }
1106

1107
1108
1109
1110
        // Create the lease and set the cltt (after converting from the
        // expire time retrieved from the database).
        Lease6Ptr result(new Lease6(type, addr, duid_ptr, iaid_,
                                    pref_lifetime_, valid_lifetime_, 0, 0,
1111
                                    subnet_id_, fqdn_fwd_, fqdn_rev_,
1112
                                    hostname, hwaddr, prefixlen_));
1113
1114
1115
        time_t cltt = 0;
        MySqlLeaseMgr::convertFromDatabaseTime(expire_, valid_lifetime_, cltt);
        result->cltt_ = cltt;
1116