123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- // Copyright (C) 2014-2016 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 CFG_HOSTS_H
- #define CFG_HOSTS_H
- #include <asiolink/io_address.h>
- #include <dhcp/duid.h>
- #include <dhcp/hwaddr.h>
- #include <dhcpsrv/base_host_data_source.h>
- #include <dhcpsrv/host.h>
- #include <dhcpsrv/host_container.h>
- #include <dhcpsrv/subnet_id.h>
- #include <dhcpsrv/writable_host_data_source.h>
- #include <boost/shared_ptr.hpp>
- #include <vector>
- namespace isc {
- namespace dhcp {
- /// @brief Represents the host reservations specified in the configuration file.
- ///
- /// This class holds a collection of the host reservations (@c Host objects)
- /// which can be retrieved using different search criteria.
- ///
- /// In the typical case the reservations are searched using the client's MAC
- /// address or DUID and a subnet that the client is connected to. The
- /// reservations can be also retrieved using other parameters, such as reserved
- /// IP address.
- ///
- /// The reservations are added to this object by the configuration parsers,
- /// when the new configuration is applied for the server. The reservations
- /// are retrieved by the @c HostMgr class when the server is allocating or
- /// renewing an address or prefix for the particular client.
- class CfgHosts : public BaseHostDataSource, public WritableHostDataSource {
- public:
- /// @brief Destructor.
- virtual ~CfgHosts() { }
- /// @brief Return all hosts for the specified HW address or DUID.
- ///
- /// This method returns all @c Host objects which represent reservations
- /// for the specified HW address or DUID. Note, that this method may
- /// return multiple reservations because a particular client may have
- /// reservations in multiple subnets and the same client may be identified
- /// by HW address or DUID. The server is unable to verify that the specific
- /// DUID and HW address belong to the same client, until the client sends
- /// a DHCP message.
- ///
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
- ///
- /// @return Collection of const @c Host objects.
- virtual ConstHostCollection
- getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const;
- /// @brief Non-const version of the @c getAll const method.
- ///
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
- ///
- /// @return Collection of non-const @c Host objects.
- virtual HostCollection
- getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr());
- /// @brief Return all hosts connected to any subnet for which reservations
- /// have been made using a specified identifier.
- ///
- /// This method returns all @c Host objects which represent reservations
- /// for a specified identifier. This method may return multiple hosts
- /// because a particular client may have reservations in multiple subnets.
- ///
- /// @param identifier_type One of the supported identifier types.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Collection of const @c Host objects.
- virtual ConstHostCollection
- getAll(const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin, const size_t identifier_len) const;
- /// @brief Non-const version of the @c getAll const method.
- ///
- /// This method returns all @c Host objects which represent reservations
- /// for a specified identifier. This method may return multiple hosts
- /// because a particular client may have reservations in multiple subnets.
- ///
- /// @param identifier_type One of the supported identifier types.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Collection of non-const @c Host objects.
- virtual HostCollection
- getAll(const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin,
- const size_t identifier_len);
- /// @brief Returns a collection of hosts using the specified IPv4 address.
- ///
- /// This method may return multiple @c Host objects if they are connected
- /// to different subnets.
- ///
- /// @param address IPv4 address for which the @c Host object is searched.
- ///
- /// @return Collection of const @c Host objects.
- virtual ConstHostCollection
- getAll4(const asiolink::IOAddress& address) const;
- /// @brief Returns a collection of hosts using the specified IPv4 address.
- ///
- /// This method may return multiple @c Host objects if they are connected
- /// to different subnets.
- ///
- /// @param address IPv4 address for which the @c Host object is searched.
- ///
- /// @return Collection of const @c Host objects.
- virtual HostCollection
- getAll4(const asiolink::IOAddress& address);
- /// @brief Returns a collection of hosts using the specified IPv6 address.
- ///
- /// This method may return multiple @c Host objects if they are connected
- /// to different subnets.
- ///
- /// @param address IPv6 address for which the @c Host object is searched.
- ///
- /// @return Collection of const @c Host objects.
- virtual ConstHostCollection
- getAll6(const asiolink::IOAddress& address) const;
- /// @brief Returns a collection of hosts using the specified IPv6 address.
- ///
- /// This method may return multiple @c Host objects if they are connected
- /// to different subnets.
- ///
- /// @param address IPv6 address for which the @c Host object is searched.
- ///
- /// @return Collection of const @c Host objects.
- virtual HostCollection
- getAll6(const asiolink::IOAddress& address);
- /// @brief Returns a host connected to the IPv4 subnet and matching
- /// specified identifiers.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid client id or NULL if not available.
- ///
- /// @return Const @c Host object using a specified HW address or DUID.
- /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
- /// been found.
- virtual ConstHostPtr
- get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
- const DuidPtr& duid = DuidPtr()) const;
- /// @brief Returns a host connected to the IPv4 subnet and matching
- /// specified identifiers.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid client id or NULL if not available.
- ///
- /// @return Non-const @c Host object using a specified HW address or DUID.
- /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
- /// been found.
- virtual HostPtr
- get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
- const DuidPtr& duid = DuidPtr());
- /// @brief Returns a host connected to the IPv4 subnet.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param identifier_type Identifier type.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Const @c Host object for which reservation has been made using
- /// the specified identifier.
- virtual ConstHostPtr
- get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin, const size_t identifier_len) const;
- /// @brief Returns a host connected to the IPv4 subnet.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param identifier_type Identifier type.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Non-const @c Host object for which reservation has been made
- /// using the specified identifier.
- virtual HostPtr
- get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin, const size_t identifier_len);
- /// @brief Returns a host connected to the IPv4 subnet and having
- /// a reservation for a specified IPv4 address.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param address reserved IPv4 address.
- ///
- /// @return Const @c Host object using a specified IPv4 address.
- virtual ConstHostPtr
- get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
- /// @brief Returns a host connected to the IPv6 subnet and matching
- /// the specified identifiers.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid DUID or NULL if not available.
- ///
- /// @return Const @c Host object using a specified HW address or DUID.
- /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
- /// been found.
- virtual ConstHostPtr
- get6(const SubnetID& subnet_id, const DuidPtr& duid,
- const HWAddrPtr& hwaddr = HWAddrPtr()) const;
- /// @brief Returns a host connected to the IPv6 subnet and matching the
- /// specified identifiers.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param hwaddr HW address of the client or NULL if no HW address
- /// available.
- /// @param duid DUID or NULL if not available.
- ///
- /// @return Non-const @c Host object using a specified HW address or DUID.
- /// @throw isc::dhcp::DuplicateHost if more than one candidate host has
- /// been found.
- virtual HostPtr
- get6(const SubnetID& subnet_id, const DuidPtr& duid,
- const HWAddrPtr& hwaddr = HWAddrPtr());
- /// @brief Returns a host connected to the IPv6 subnet.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param identifier_type Identifier type.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Const @c Host object for which reservation has been made using
- /// the specified identifier.
- virtual ConstHostPtr
- get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin, const size_t identifier_len) const;
- /// @brief Returns a host connected to the IPv6 subnet.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param identifier_type Identifier type.
- /// @param identifier_begin Pointer to a begining of a buffer containing
- /// an identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Non-const @c Host object for which reservation has been made
- /// using the specified identifier.
- virtual HostPtr
- get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
- const uint8_t* identifier_begin, const size_t identifier_len);
- /// @brief Returns a host using the specified IPv6 prefix.
- ///
- /// @param prefix IPv6 prefix for which the @c Host object is searched.
- /// @param prefix_len IPv6 prefix length.
- ///
- /// @return Const @c Host object for which specified prefix is reserved.
- virtual ConstHostPtr
- get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
- /// @brief Returns a host using the specified IPv6 prefix.
- ///
- /// @param prefix IPv6 prefix for which the @c Host object is searched.
- /// @param prefix_len IPv6 prefix length.
- ///
- /// @return Non-const @c Host object for which specified prefix is
- /// reserved.
- virtual HostPtr
- get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len);
- /// @brief Returns a host connected to the IPv6 subnet and having
- /// a reservation for a specified IPv6 address.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param address reserved IPv6 address.
- ///
- /// @return Const @c Host object using a specified IPv6 address.
- virtual ConstHostPtr
- get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
- /// @brief Returns a host connected to the IPv6 subnet and having
- /// a reservation for a specified IPv6 address.
- ///
- /// @param subnet_id Subnet identifier.
- /// @param address reserved IPv6 address.
- ///
- /// @return Const @c Host object using a specified IPv6 address.
- virtual HostPtr
- get6(const SubnetID& subnet_id, const asiolink::IOAddress& address);
- /// @brief Adds a new host to the collection.
- ///
- /// @param host Pointer to the new @c Host object being added.
- ///
- /// @throw DuplicateHost If a host for a particular HW address or DUID
- /// has already been added to the IPv4 or IPv6 subnet.
- virtual void add(const HostPtr& host);
- /// @brief Return backend type
- ///
- /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
- ///
- /// @return Type of the backend.
- virtual std::string getType() const {
- return (std::string("configuration file"));
- }
- private:
- /// @brief Returns @c Host objects for the specific identifier and type.
- ///
- /// This private method is called by the @c CfgHosts::getAllInternal
- /// method which finds the @c Host objects using specified identifier.
- /// The retrieved objects are appended to the @c storage container.
- ///
- /// @param identifier_type The type of the supplied identifier.
- /// @param identifier Pointer to a first byte of the identifier.
- /// @param identifier_len Length of the identifier.
- /// @param [out] storage Container to which the retreived objects are
- /// appended.
- /// @tparam One of the @c ConstHostCollection of @c HostCollection.
- template<typename Storage>
- void getAllInternal(const Host::IdentifierType& identifier_type,
- const uint8_t* identifier,
- const size_t identifier_len,
- Storage& storage) const;
- /// @brief Returns @c Host objects for the specified HW address or DUID.
- ///
- /// This private method is called by the @c CfgHosts::getAll methods to
- /// retrieve the @c Host objects using HW address or DUID. The retrieved
- /// objects are appended to the @c storage container.
- ///
- /// @param hwaddr HW address identifying a host.
- /// @param duid DUID identifying a host.
- /// @param [out] storage Container to which the retrieved objects are
- /// appended.
- /// @tparam One of the @c ConstHostCollection or @c HostCollection.
- template<typename Storage>
- void getAllInternal(const HWAddrPtr& hwaddr, const DuidPtr& duid,
- Storage& storage) const;
- /// @brief Returns @c Host objects for the specified IPv4 address.
- ///
- /// This private method is called by the @c CfgHosts::getAll4 methods
- /// to retrieve the @c Host for which the specified IPv4 address is
- /// reserved. The retrieved objects are appended to the @c storage
- /// container.
- ///
- /// @param address IPv4 address.
- /// @param [out] storage Container to which the retrieved objects are
- /// appended.
- /// @tparam One of the @c ConstHostCollection or @c HostCollection.
- template<typename Storage>
- void getAllInternal4(const asiolink::IOAddress& address,
- Storage& storage) const;
- /// @brief Returns @c Host objects for the specified IPv6 address.
- ///
- /// This private method is called by the @c CfgHosts::getAll6 methods
- /// to retrieve the @c Host for which the specified IPv6 address is
- /// reserved. The retrieved objects are appended to the @c storage
- /// container.
- ///
- /// @param address IPv6 address.
- /// @param [out] storage Container to which the retrieved objects are
- /// appended.
- /// @tparam One of the @c ConstHostCollection or @c HostCollection.
- template<typename Storage>
- void getAllInternal6(const asiolink::IOAddress& address,
- Storage& storage) const;
- /// @brief Returns @c Host objects for the specified (Subnet-id,IPv6 address) tuple.
- ///
- /// This private method is called by the @c CfgHosts::getAll6 methods
- /// to retrieve the @c Host for which the specified IPv6 address is
- /// reserved and is in specified subnet-id. The retrieved objects are
- /// appended to the @c storage container.
- ///
- /// @param subnet_id Subnet Identifier.
- /// @param address IPv6 address.
- /// @param [out] storage Container to which the retrieved objects are
- /// appended.
- /// @tparam One of the @c ConstHostCollection or @c HostCollection.
- template<typename Storage>
- void
- getAllInternal6(const SubnetID& subnet_id,
- const asiolink::IOAddress& address,
- Storage& storage) const;
- /// @brief Returns @c Host object connected to a subnet.
- ///
- /// This private method returns a pointer to the @c Host object using
- /// a specified identifier and connected to an IPv4 or IPv6 subnet.
- ///
- /// @param subnet_id IPv4 or IPv6 subnet identifier.
- /// @param subnet6 A boolean flag which indicates if the subnet identifier
- /// points to a IPv4 (if false) or IPv6 subnet (if true).
- /// @param identifier_type Indentifier type.
- /// @param identifier Pointer to a first byte of the buffer holding an
- /// identifier.
- /// @param identifier_len Identifier length.
- ///
- /// @return Pointer to the found host, or NULL if no host found.
- /// @throw isc::dhcp::DuplicateHost if method found more than one matching
- /// @c Host object.
- HostPtr
- getHostInternal(const SubnetID& subnet_id, const bool subnet6,
- const Host::IdentifierType& identifier_type,
- const uint8_t* identifier,
- const size_t identifier_len) const;
- /// @brief Returns the @c Host object holding reservation for the IPv6
- /// address and connected to the specific subnet.
- ///
- /// This private method is called by the public @c get6 method variants.
- ///
- /// @param subnet_id IPv6 subnet identifier.
- /// @param address IPv6 address.
- /// @tparam ReturnType One of @c HostPtr or @c ConstHostPtr
- /// @tparam One of the @c ConstHostCollection or @c HostCollection.
- ///
- /// @return Pointer to the found host, or NULL if no host found.
- /// @throw isc::dhcp::DuplicateHost if method found more than one matching
- /// @c Host object.
- template<typename ReturnType, typename Storage>
- ReturnType getHostInternal6(const SubnetID& subnet_id,
- const asiolink::IOAddress& adddress) const;
- template<typename ReturnType>
- ReturnType getHostInternal6(const asiolink::IOAddress& prefix,
- const uint8_t prefix_len) const;
- /// @brief Adds a new host to the v4 collection.
- ///
- /// This is an internal method called by public @ref add.
- ///
- /// @param host Pointer to the new @c Host object being added.
- ///
- /// @throw DuplicateHost If a host for a particular HW address or DUID
- /// has already been added to the IPv4 subnet.
- virtual void add4(const HostPtr& host);
- /// @brief Adds a new host to the v6 collection.
- ///
- /// This is an internal method called by public @ref add.
- ///
- /// @param host Pointer to the new @c Host object being added.
- ///
- /// @throw DuplicateHost If a host for a particular HW address or DUID
- /// or for the particular address or prefix has already been added to
- /// the IPv6 subnet.
- virtual void add6(const HostPtr& host);
- /// @brief Multi-index container holding @c Host objects.
- ///
- /// It can be used for finding hosts by the following criteria:
- /// - IPv4 address
- /// - DUID
- /// - HW/MAC address
- HostContainer hosts_;
- /// @brief Multi-index container holding @c Host objects with v6 reservations.
- ///
- /// It can be used for finding hosts by the following criteria:
- /// - IPv6 address
- /// - IPv6 prefix
- HostContainer6 hosts6_;
- };
- /// @name Pointers to the @c CfgHosts objects.
- //@{
- /// @brief Non-const pointer.
- typedef boost::shared_ptr<CfgHosts> CfgHostsPtr;
- /// @brief Const pointer.
- typedef boost::shared_ptr<const CfgHosts> ConstCfgHostsPtr;
- //@}
- }
- }
- #endif // CFG_HOSTS_H
|