Coverage Report

Created: 2025-11-16 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea/src/lib/dhcpsrv/iterative_allocator.h
Line
Count
Source
1
// Copyright (C) 2022-2023 Internet Systems Consortium, Inc. ("ISC")
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
#ifndef ITERATIVE_ALLOCATOR_H
8
#define ITERATIVE_ALLOCATOR_H
9
10
#include <dhcpsrv/allocator.h>
11
#include <dhcpsrv/iterative_allocation_state.h>
12
#include <dhcpsrv/lease.h>
13
14
#include <cstdint>
15
16
namespace isc {
17
namespace dhcp {
18
19
/// @brief Address/prefix allocator that iterates over all addresses.
20
///
21
/// This class implements an iterative algorithm that returns all addresses in
22
/// a pool iteratively, one after another. Once the last address is reached,
23
/// it starts allocating from the beginning of the first pool (i.e. it loops
24
/// over).
25
class IterativeAllocator : public Allocator {
26
public:
27
28
    /// @brief Constructor.
29
    ///
30
    /// @param type specifies the type of allocated leases.
31
    /// @param subnet weak pointer to the subnet owning the allocator.
32
    IterativeAllocator(Lease::Type type, const WeakSubnetPtr& subnet);
33
34
    /// @brief Returns the allocator type string.
35
    ///
36
    /// @return iterative string.
37
7.18k
    virtual std::string getType() const {
38
7.18k
        return ("iterative");
39
7.18k
    }
40
41
private:
42
43
    /// @brief Returns the next address from the pools in the subnet.
44
    ///
45
    /// Internal thread-unsafe implementation of the @c pickAddress.
46
    ///
47
    /// @param client_classes list of classes client belongs to.
48
    /// @param duid client DUID (ignored).
49
    /// @param hint client hint (ignored).
50
    ///
51
    /// @return next offered address.
52
    virtual asiolink::IOAddress pickAddressInternal(const ClientClasses& client_classes,
53
                                                    const IdentifierBaseTypePtr& duid,
54
                                                    const asiolink::IOAddress& hint);
55
56
    /// @brief Picks a delegated prefix.
57
    ///
58
    /// Internal thread-unsafe implementation of the @c pickPrefix.
59
    ///
60
    /// @param client_classes list of classes client belongs to.
61
    /// @param pool the selected pool satisfying all required conditions.
62
    /// @param duid Client's DUID.
63
    /// @param prefix_length_match type which indicates the selection criteria
64
    ///        for the pools relative to the provided hint prefix length.
65
    /// @param hint Client's hint.
66
    /// @param hint_prefix_length the hint prefix length that the client
67
    ///        provided. The 0 value means that there is no hint and that any
68
    ///        pool will suffice.
69
    ///
70
    /// @return the next prefix.
71
    virtual asiolink::IOAddress pickPrefixInternal(const ClientClasses& client_classes,
72
                                                   Pool6Ptr& pool,
73
                                                   const IdentifierBaseTypePtr& duid,
74
                                                   PrefixLenMatchType prefix_length_match,
75
                                                   const asiolink::IOAddress& hint,
76
                                                   uint8_t hint_prefix_length);
77
78
    /// @brief Convenience function returning subnet allocation state instance.
79
    ///
80
    /// It creates a new subnet state instance and assigns it to the subnet
81
    /// if it hasn't been initialized.
82
    ///
83
    /// @return allocation state instance for the subnet.
84
    SubnetIterativeAllocationStatePtr getSubnetState() const;
85
86
    /// @brief Convenience function returning pool allocation state instance.
87
    ///
88
    /// It creates a new pool state instance and assigns it to the pool
89
    /// if it hasn't been initialized.
90
    ///
91
    /// @param pool pool instance.
92
    /// @return allocation state instance for the pool.
93
    PoolIterativeAllocationStatePtr getPoolState(const PoolPtr& pool) const;
94
95
protected:
96
97
    /// @brief Returns the next prefix.
98
    ///
99
    /// This method works for IPv6 addresses only. It increases the
100
    /// specified prefix by a given prefix_len. For example, 2001:db8::
101
    /// increased by prefix length /32 will become 2001:db9::. This method
102
    /// is used to iterate over IPv6 prefix pools
103
    ///
104
    /// @param prefix prefix to be increased.
105
    /// @param prefix_len length of the prefix to be increased.
106
    ///
107
    /// @return next prefix.
108
    static asiolink::IOAddress increasePrefix(const asiolink::IOAddress& prefix,
109
                                              const uint8_t prefix_len);
110
111
    /// @brief Returns the next address or prefix.
112
    ///
113
    /// This method works for IPv4 addresses, IPv6 addresses and
114
    /// IPv6 prefixes.
115
    ///
116
    /// @param address address or prefix to be increased
117
    /// @param prefix true when the previous argument is a prefix.
118
    /// @param prefix_len length of the prefix.
119
    ///
120
    /// @return result address or prefix
121
    static asiolink::IOAddress increaseAddress(const asiolink::IOAddress& address,
122
                                               bool prefix,
123
                                               const uint8_t prefix_len);
124
};
125
126
}  // namespace dhcp
127
}  // end of namespace isc
128
129
#endif  // ITERATIVE_ALLOCATOR_H