1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <host_cache.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <host_cache_impl.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <boost/foreach.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;

namespace isc {
namespace host_cache {

HostCacheImpl::HostCacheImpl() : maximum_(0) {
}

HostCacheImpl::~HostCacheImpl() {
}

bool
HostCacheImpl::insertResv6(const HostPtr& host) {
    if (!host || (host->getIPv6SubnetID() == SUBNET_ID_UNUSED)) {
        return (true);
    }
    const IPv6ResrvRange& resrv = host->getIPv6Reservations();
    if (std::distance(resrv.first, resrv.second) == 0) {
        return (true);
    }
    // For each IPv6 reservation, insert (address, host) tuple.
    BOOST_FOREACH(auto const& it, resrv) {
        auto ret = cache6_.insert(HostResrv6Tuple(it.second, host));
        // No reason to fail but check anyway.
        if (!ret.second) {
            return (false);
        }
    }
    return (true);
}

void
HostCacheImpl::removeResv6(const HostPtr& host) {
    if (!host || (host->getIPv6SubnetID() == SUBNET_ID_UNUSED)) {
        return;
    }
    const IPv6ResrvRange& resrv = host->getIPv6Reservations();
    if (std::distance(resrv.first, resrv.second) == 0) {
        return;
    }
    // For each IPv6 reservation, remove it.
    const SubnetID& subnet_id = host->getIPv6SubnetID();
    Resv6ContainerAddressIndex& idx0 =
        cache6_.get<Resv6AddressIndexTag>();
    Resv6ContainerSubnetAddressIndex& idx1 =
        cache6_.get<Resv6SubnetAddressIndexTag>();
    BOOST_FOREACH(auto const& it, resrv) {
        const IOAddress& prefix = it.second.getPrefix();
        if (it.first == IPv6Resrv::TYPE_NA) {
            Resv6ContainerSubnetAddressIndexRange r =
                make_pair(idx1.lower_bound(boost::make_tuple(subnet_id, prefix)),
                          idx1.upper_bound(boost::make_tuple(subnet_id, prefix)));
            for (Resv6ContainerSubnetAddressIndex::iterator p = r.first;
                 p != r.second; ++p) {
                if (p->host_ != host) {
                    // Should not happen
                    continue;
                }
                idx1.erase(p);
                break;
            }
        } else if (it.first == IPv6Resrv::TYPE_PD) {
            uint8_t prefix_len = it.second.getPrefixLen();
            Resv6ContainerAddressIndexRange r =
                make_pair(idx0.lower_bound(prefix), idx0.upper_bound(prefix));
            for (Resv6ContainerAddressIndex::iterator p = r.first;
                 p != r.second; ++p) {
                if (p->resrv_.getPrefixLen() == prefix_len) {
                    if (p->host_ != host) {
                        // Should not happen
                        continue;
                    }
                    idx0.erase(p);
                    break;
                }
            }
        }
    }
}

HostPtr
HostCacheImpl::relocate(HostPtr host) {
    if (host) {
        const HostContainerHashedIndex& idx = cache_.get<HostHashedIndexTag>();
        HostContainerHashedIndex::iterator it = idx.find(host);
        if (it == idx.end()) {
            // Should not happen
            // Remove related reservations unconditionally
            removeResv6(host);
            return (HostPtr());
        }
        cache_.relocate(cache_.end(), cache_.project<HostSequencedIndexTag>(it));
    }
    return (host);
}

ConstHostPtr
HostCacheImpl::get4(const dhcp::SubnetID& subnet_id,
                    const dhcp::Host::IdentifierType& identifier_type,
                    const uint8_t* identifier_begin,
                    const size_t identifier_len) {
    HostContainerSequencedIndex::iterator host =
        getHostInternal(subnet_id, false, identifier_type,
                        identifier_begin, identifier_len);
    if (host == cache_.end()) {
        return (ConstHostPtr());
    }
    cache_.relocate(cache_.end(), host);
    return (*host);
}

ConstHostPtr
HostCacheImpl::get4(const dhcp::SubnetID& subnet_id,
                    const asiolink::IOAddress& address) {
    HostContainerSequencedIndex::iterator host =
        getHostInternal4(subnet_id, address);
    if (host == cache_.end()) {
        return (ConstHostPtr());
    }
    cache_.relocate(cache_.end(), host);
    return (*host);
}

ConstHostPtr
HostCacheImpl::get6(const dhcp::SubnetID& subnet_id,
                    const dhcp::Host::IdentifierType& identifier_type,
                    const uint8_t* identifier_begin,
                    const size_t identifier_len) {
    HostContainerSequencedIndex::iterator host =
        getHostInternal(subnet_id, true, identifier_type,
                        identifier_begin, identifier_len);
    if (host == cache_.end()) {
        return (ConstHostPtr());
    }
    cache_.relocate(cache_.end(), host);
    return (*host);
}

ConstHostPtr
HostCacheImpl::get6(const asiolink::IOAddress& prefix,
                    const uint8_t prefix_len) {
    HostPtr host = getHostInternal6(prefix, prefix_len);
    return (relocate(host));
}

ConstHostPtr
HostCacheImpl::get6(const dhcp::SubnetID& subnet_id,
                    const asiolink::IOAddress& address) {
    HostPtr host = getHostInternal6(subnet_id, address);
    return (relocate(host));
}

HostContainerSequencedIndex::iterator
HostCacheImpl::getHostInternal(const SubnetID& subnet_id, const bool subnet6,
                               const Host::IdentifierType& identifier_type,
                               const uint8_t* identifier,
                               const size_t identifier_len) {
    // Use the identifier and identifier type as a composite key.
    const HostContainerIdentifierIndex& idx =
        cache_.get<HostIdentifierIndexTag>();
    boost::tuple<const vector<uint8_t>, const Host::IdentifierType> t =
        boost::make_tuple(vector<uint8_t>(identifier,
                                          identifier + identifier_len),
                          identifier_type);

    for (HostContainerIdentifierIndex::iterator host = idx.lower_bound(t);
         host != idx.upper_bound(t); ++host) {
        // Check if this is IPv4 subnet or IPv6 subnet.
        SubnetID host_subnet_id = subnet6 ? (*host)->getIPv6SubnetID() :
            (*host)->getIPv4SubnetID();

        if (subnet_id == host_subnet_id) {
            return (cache_.project<HostSequencedIndexTag>(host));
        }
    }
    return (cache_.end());
}

HostContainerSequencedIndex::iterator
HostCacheImpl::getHostInternal4(const SubnetID& subnet_id,
                                const IOAddress& address) {
    // Search for the Host using the reserved IPv4 address as a key.
    const HostContainerAddress4Index& idx =
        cache_.get<HostAddress4IndexTag>();
    HostContainerAddress4IndexRange r = idx.equal_range(address);
    for (HostContainerAddress4Index::iterator host = r.first;
         host != r.second; ++host) {
        // Find matching subnet ID.
        if ((*host)->getIPv4SubnetID() == subnet_id) {
            return (cache_.project<HostSequencedIndexTag>(host));
        }
    }
    return (cache_.end());
}

HostPtr
HostCacheImpl::getHostInternal6(const asiolink::IOAddress& prefix,
                                const uint8_t prefix_len) {
    // Let's get all reservations for the prefix
    const Resv6ContainerAddressIndex& idx =
        cache6_.get<Resv6AddressIndexTag>();
    Resv6ContainerAddressIndexRange r = make_pair(idx.lower_bound(prefix),
                                                  idx.upper_bound(prefix));
    BOOST_FOREACH(auto const& resrv, r) {
        if (resrv.resrv_.getPrefixLen() == prefix_len) {
            return (resrv.host_);
        }
    }
    return (HostPtr());
}

HostPtr
HostCacheImpl::getHostInternal6(const dhcp::SubnetID& subnet_id,
                                const asiolink::IOAddress& address) {
    // Search for the reservation that match subnet_id, address.
    const Resv6ContainerSubnetAddressIndex& idx =
        cache6_.get<Resv6SubnetAddressIndexTag>();
    // The index is ordered unique so the range size can be only 0 or 1.
    Resv6ContainerSubnetAddressIndexRange r =
        make_pair(idx.lower_bound(boost::make_tuple(subnet_id, address)),
                  idx.upper_bound(boost::make_tuple(subnet_id, address)));

    BOOST_FOREACH(auto const& resrv, r) {
        return (resrv.host_);
    }
    return (HostPtr());
}

size_t
HostCacheImpl::insert(const ConstHostPtr& host, bool overwrite) {
    HWAddrPtr hwaddr = host->getHWAddress();
    DuidPtr duid = host->getDuid();
    const vector<uint8_t>& id = host->getIdentifier();
    size_t conflicts = 0;

    // Check for duplicates for the specified IPv4 subnet.
    if (host->getIPv4SubnetID() != SUBNET_ID_UNUSED) {
        // Identifier
        if (!id.empty()) {
            HostContainerSequencedIndex::iterator dup;
            while ((dup = getHostInternal(host->getIPv4SubnetID(), false,
                                          host->getIdentifierType(),
                                          &id[0], id.size()))
                   != cache_.end()) {
                ++conflicts;
                if (!overwrite) {
                    return (conflicts);
                } else {
                    removeResv6(*dup);
                    cache_.erase(dup);
                }
            }
        }
        // Reserved IPv4 address.
        const asiolink::IOAddress& address = host->getIPv4Reservation();
        if (!address.isV4Zero()) {
            HostContainerSequencedIndex::iterator dup;
            while ((dup = getHostInternal4(host->getIPv4SubnetID(),
                                           address)) != cache_.end()) {
                ++conflicts;
                if (!overwrite) {
                    return (conflicts);
                } else {
                    removeResv6(*dup);
                    cache_.erase(dup);
                }
            }
        }
    }

    // Check for duplicates for the specified IPv6 subnet.
    if (host->getIPv6SubnetID() != SUBNET_ID_UNUSED) {
        // Identifier.
        if (!id.empty()) {
            HostContainerSequencedIndex::iterator dup;
            while ((dup = getHostInternal(host->getIPv6SubnetID(), true,
                                          host->getIdentifierType(),
                                          &id[0], id.size()))
                   != cache_.end()) {
                ++conflicts;
                if (!overwrite) {
                    return (conflicts);
                } else {
                    removeResv6(*dup);
                    cache_.erase(dup);
                }
            }
        }
        // Reservations.
        const IPv6ResrvRange& resrv = host->getIPv6Reservations();
        BOOST_FOREACH(auto const& it, resrv) {
            if (it.first == IPv6Resrv::TYPE_NA) {
                HostPtr dup;
                while ((dup = getHostInternal6(host->getIPv6SubnetID(),
                                               it.second.getPrefix()))) {
                    ++conflicts;
                    if (!overwrite) {
                        return (conflicts);
                    } else {
                        remove(dup);
                    }
                }
            } else if (it.first == IPv6Resrv::TYPE_PD) {
                HostPtr dup;
                while ((dup = getHostInternal6(it.second.getPrefix(),
                                               it.second.getPrefixLen()))) {
                    ++conflicts;
                    if (!overwrite) {
                        return (conflicts);
                    } else {
                        remove(dup);
                    }
                }
            }
        }
    }

    // This is a new instance - add a copy of it.
    HostPtr host_copy(new Host(*host));
    auto ret = cache_.push_back(host_copy);
    if (!ret.second) {
        // isc_throw(Unexpected, "can't insert");
        ++conflicts;
        return (conflicts);
    } else if (!insertResv6(host_copy)) {
        // isc_throw(Unexpected, "can't insert IPv6 reservation");
        ++conflicts;
        remove(host_copy);
        return (conflicts);
    }

    // Adjust cache size
    if (maximum_ && (cache_.size() > maximum_)) {
        flush(cache_.size() - maximum_);
    }

    return (conflicts);
}

void
HostCacheImpl::update(ConstHostPtr const& host) {
    if (!host) {
        return;
    }

    // At least one subnet ID must be used.
    if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED &&
        host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
        return;
    }

    /// This is not an upsert, so if the host doesn't exist yet, throw an error.
    /// @todo: If this function ever ends up being used, this brute force search
    /// should be replaced with a host ID index.
    bool found(false);
    for (HostPtr const& h : cache_) {
        if (h->getHostId() == host->getHostId()) {
            found = true;
            break;
        }
    }
    if (!found) {
        isc_throw(NotFound, "host ID " << host->getHostId() << " not found");
    }

    // We're sure the host exists, so an insert with overwrite is equivalent to
    // an update.
    insert(host, /* overwrite = */ true);
}

bool
HostCacheImpl::add(const HostPtr& host) {
    return (insert(host, false) == 0);
}

string
HostCacheImpl::del4(const dhcp::SubnetID& subnet_id,
                    const asiolink::IOAddress& addr) {
    string txt;
    // Search for the Host using the reserved IPv4 address as a key.
    HostContainerAddress4Index& idx = cache_.get<HostAddress4IndexTag>();
    HostContainerAddress4IndexRange r = idx.equal_range(addr);
    for (HostContainerAddress4Index::iterator host = r.first;
         host != r.second; ++host) {
        // Find matching subnet ID.
        if ((*host)->getIPv4SubnetID() == subnet_id) {
            txt = (*host)->toText();
            removeResv6(*host);
            idx.erase(host);
            return (txt);
        }
    }
    return (txt);
}

string
HostCacheImpl::del6(const dhcp::SubnetID& subnet_id,
                    const asiolink::IOAddress& addr) {
    string txt;
    // Search for the reservation that match subnet_id, address.
    const Resv6ContainerSubnetAddressIndex& idx =
        cache6_.get<Resv6SubnetAddressIndexTag>();
    Resv6ContainerSubnetAddressIndex::iterator resrv =
        idx.find(boost::make_tuple(subnet_id, addr));
    if (resrv == idx.end()) {
        return (txt);
    }
    txt = resrv->host_->toText();
    remove(resrv->host_);
    return (txt);
}

string
HostCacheImpl::del4(const dhcp::SubnetID& subnet_id,
                    const dhcp::Host::IdentifierType& identifier_type,
                    const uint8_t* identifier_begin,
                    const size_t identifier_len) {
    string txt;
    // Use the identifier and identifier type as a composite key.
    HostContainerIdentifierIndex& idx = cache_.get<HostIdentifierIndexTag>();
    boost::tuple<const vector<uint8_t>, const Host::IdentifierType> t =
        boost::make_tuple(vector<uint8_t>(identifier_begin,
                                          identifier_begin + identifier_len),
                          identifier_type);
    for (HostContainerIdentifierIndex::iterator host = idx.lower_bound(t);
         host != idx.upper_bound(t); ++host) {
        SubnetID host_subnet_id = (*host)->getIPv4SubnetID();
        if (subnet_id == host_subnet_id) {
            txt = (*host)->toText();
            removeResv6(*host);
            idx.erase(host);
            return (txt);
        }
    }
    return (txt);
}

string
HostCacheImpl::del6(const dhcp::SubnetID& subnet_id,
                    const dhcp::Host::IdentifierType& identifier_type,
                    const uint8_t* identifier_begin,
                    const size_t identifier_len) {
    string txt;
    // Use the identifier and identifier type as a composite key.
    HostContainerIdentifierIndex& idx =
        cache_.get<HostIdentifierIndexTag>();
    boost::tuple<const vector<uint8_t>, const Host::IdentifierType> t =
        boost::make_tuple(vector<uint8_t>(identifier_begin,
                                          identifier_begin + identifier_len),
                          identifier_type);
    for (HostContainerIdentifierIndex::iterator host = idx.lower_bound(t);
         host != idx.upper_bound(t); ++host) {
        SubnetID host_subnet_id = (*host)->getIPv6SubnetID();
        if (subnet_id == host_subnet_id) {
            txt = (*host)->toText();
            removeResv6(*host);
            idx.erase(host);
            return (txt);
        }
    }
    return (txt);
}

bool
HostCacheImpl::remove(const HostPtr& host) {
    // Using the hashed index
    HostContainerHashedIndex& idx = cache_.get<HostHashedIndexTag>();
    HostContainerHashedIndex::iterator host_it = idx.find(host);
    // Remove related reservations unconditionally
    removeResv6(host);
    if (host_it == idx.end()) {
        return (false);
    }
    idx.erase(host_it);
    return (true);
}

void
HostCacheImpl::flush(size_t count) {
    // Flush all entries is handled by clear.
    if (count == 0) {
        return;
    }
    HostContainerSequencedIndex& idx = cache_.get<HostSequencedIndexTag>();
    HostContainerSequencedIndex::iterator host = idx.begin();
    while ((host != idx.end()) && (count > 0)) {
        removeResv6(*host);
        idx.erase(host);
        host = idx.begin();
        --count;
    }
}

ElementPtr
HostCacheImpl::toElement() const {
    ElementPtr result = Element::createList();
    // Iterate using sequenced index to keep the order
    const HostContainerSequencedIndex& idx =
        cache_.get<HostSequencedIndexTag>();
    for (auto const& host : idx) {
        // Convert host to element representation
        ElementPtr map = isc::host_cache::toElement(host);
        // Push it on the list
        result->add(map);
    }
    return (result);
}

ConstHostCollection
HostCacheImpl::get(const dhcp::Host::IdentifierType& identifier_type,
                   const uint8_t* identifier_begin,
                   const size_t identifier_len) const {
    // Use the identifier and identifier type as a composite key.
    const HostContainerIdentifierIndex& idx =
        cache_.get<HostIdentifierIndexTag>();
    boost::tuple<const vector<uint8_t>, const Host::IdentifierType> t =
        boost::make_tuple(vector<uint8_t>(identifier_begin,
                                          identifier_begin + identifier_len),
                          identifier_type);
    ConstHostCollection result;
    for (HostContainerIdentifierIndex::iterator host = idx.lower_bound(t);
         host != idx.upper_bound(t); ++host) {
        result.push_back(*host);
    }
    return (result);
}

} // end of namespace isc::host_cache
} // end of namespace isc