summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/host_container.h
blob: 5d9f49e2f902a480914880897df4a166f29cbfba (plain)
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
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HOST_CONTAINER_H
#define HOST_CONTAINER_H

#include <dhcpsrv/host.h>
#include <dhcpsrv/subnet_id.h>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>

namespace isc {
namespace dhcp {

/// @brief Multi-index container holding host reservations.
///
/// This container holds a collection of @c Host objects which can be retrieved
/// using various parameters. The typical use of this container is to search for
/// all @c Host objects which are identified by a specified identifier, i.e.
/// HW address or DUID.
///
/// @todo This container will be extended to search for @c Host objects
/// associated with a specific IPv4 address or IPv6 prefix/length.
///
/// @see http://www.boost.org/doc/libs/1_56_0/libs/multi_index/doc/index.html
typedef boost::multi_index_container<
    // This container stores pointers to Host objects.
    HostPtr,
    // Start specification of indexes here.
    boost::multi_index::indexed_by<
        // First index is used to search for the host using one of the
        // identifiers, i.e. HW address or DUID. The elements of this
        // index are non-unique because there may be multiple reservations
        // for the same host belonging to a different subnets.
        boost::multi_index::ordered_non_unique<
            // The index comprises actual identifier (HW address or DUID) in
            // a binary form and a type of the identifier which indicates
            // that it is HW address or DUID.
            boost::multi_index::composite_key<
                // Composite key uses members of the Host class.
                Host,
                // Binary identifier is retrieved from the Host class using
                // a getIdentifier method.
                boost::multi_index::const_mem_fun<
                    Host, const std::vector<uint8_t>&,
                    &Host::getIdentifier
                >,
                // Identifier type is retrieved from the Host class using
                // a getIdentifierType method.
                boost::multi_index::const_mem_fun<
                    Host, Host::IdentifierType,
                    &Host::getIdentifierType
                >
            >
        >,

        // Second index is used to search for the host using reserved IPv4
        // address.
        boost::multi_index::ordered_non_unique<
            // Index using values returned by the @c Host::getIPv4Resrvation.
            boost::multi_index::const_mem_fun<Host, const asiolink::IOAddress&,
                                               &Host::getIPv4Reservation>
        >
    >
> HostContainer;

/// @brief First index type in the @c HostContainer.
///
/// This index allows for searching for @c Host objects using an
/// identifier + identifier type tuple.
typedef HostContainer::nth_index<0>::type HostContainerIndex0;

/// @brief Results range returned using the @c HostContainerIndex0.
typedef std::pair<HostContainerIndex0::iterator,
                  HostContainerIndex0::iterator> HostContainerIndex0Range;

/// @brief Second index type in the @c HostContainer.
///
/// This index allows for searching for @c Host objects using a
/// reserved IPv4 address.
typedef HostContainer::nth_index<1>::type HostContainerIndex1;

/// @brief Results range returned using the @c HostContainerIndex1.
typedef std::pair<HostContainerIndex1::iterator,
                  HostContainerIndex1::iterator> HostContainerIndex1Range;

/// @brief Defines one entry for the Host Container for v6 hosts
///
/// It's essentially a pair of (IPv6 reservation, Host pointer).
/// This structure is used as an intermediate structure in HostContainer6.
/// For a single host that has reservations for X addresses or prefixes, there
/// will be X HostResrv6Tuple structures.
struct HostResrv6Tuple {

    /// @brief Default constructor.
    ///
    /// @param resrv IPv6 address/prefix reservation
    /// @param host pointer to the host object
    HostResrv6Tuple(const IPv6Resrv& resrv, const HostPtr& host)
    :resrv_(resrv), host_(host), subnet_id_(host ? host->getIPv6SubnetID() : 0) {
    }

    /// @brief Address or prefix reservation.
    const IPv6Resrv resrv_;

    /// @brief Pointer to the host object.
    HostPtr host_;

    /// @brief Value of the IPv6 Subnet-id
    const SubnetID subnet_id_;

    /// @brief Key extractor (used in the second composite key)
    const asiolink::IOAddress& getKey() const {
        return (resrv_.getPrefix());
    }
};

/// @brief Multi-index container holding IPv6 reservations.
///
/// This container holds HostResrv6Tuples, i.e. pairs of (IPv6Resrv, HostPtr)
/// pieces of information. This is needed for efficiently finding a host
/// for a given IPv6 address or prefix.
typedef boost::multi_index_container<

    // This containers stores (IPv6Resrv, HostPtr) tuples
    HostResrv6Tuple,

    // Start specification of indexes here.
    boost::multi_index::indexed_by<

        // First index is used to search by an address.
        boost::multi_index::ordered_non_unique<

            // Address is extracted by calling IPv6Resrv::getPrefix()
            // and it will return an IOAddress object.
            boost::multi_index::const_mem_fun<
                HostResrv6Tuple, const asiolink::IOAddress&, &HostResrv6Tuple::getKey>
        >,

        // Second index is used to search by (subnet_id, address) pair.
        // This is
        boost::multi_index::ordered_unique<

            /// This is a composite key. It uses two keys: subnet-id and
            /// IPv6 address reservation.
            boost::multi_index::composite_key<

                // Composite key uses members of the HostResrv6Tuple class.
                HostResrv6Tuple,

                // First key extractor. Gets subnet-id as a member of the
                // HostResrv6Tuple structure.
                boost::multi_index::member<HostResrv6Tuple, const SubnetID,
                    &HostResrv6Tuple::subnet_id_>,

                // Second key extractor. Address is extracted by calling
                // IPv6Resrv::getPrefix() and it will return an IOAddress object.
                boost::multi_index::const_mem_fun<
                    HostResrv6Tuple, const asiolink::IOAddress&,
                    &HostResrv6Tuple::getKey
                >
           >
        >
    >
> HostContainer6;

/// @brief First index type in the @c HostContainer6.
///
/// This index allows for searching for @c Host objects using an
/// address.
typedef HostContainer6::nth_index<0>::type HostContainer6Index0;

/// @brief Results range returned using the @c HostContainer6Index0.
typedef std::pair<HostContainer6Index0::iterator,
                  HostContainer6Index0::iterator> HostContainer6Index0Range;

/// @brief Second index type in the @c HostContainer6.
///
/// This index allows for searching for @c Host objects using a
/// reserved (SubnetID, IPv6 address) tuple.
typedef HostContainer6::nth_index<1>::type HostContainer6Index1;

/// @brief Results range returned using the @c HostContainer6Index1.
typedef std::pair<HostContainer6Index1::iterator,
                  HostContainer6Index1::iterator> HostContainer6Index1Range;

}; // end of isc::dhcp namespace
}; // end of isc namespace

#endif // HOST_CONTAINER_H