47 impl_->setMaximum(maximum);
52 return (
impl_->getMaximum());
58 const size_t )
const {
108 const dhcp:: HostPageSize& )
const {
127 const uint8_t* identifier_begin,
128 const size_t identifier_len)
const {
139 host =
impl_->get4(subnet_id, identifier_type,
140 identifier_begin, identifier_len);
148 .arg(host->toText());
162 if (!address.
isV4()) {
168 host =
impl_->get4(subnet_id, address);
175 .arg(host->toText());
189 const uint8_t* identifier_begin,
190 const size_t identifier_len)
const {
201 host =
impl_->get6(subnet_id, identifier_type,
202 identifier_begin, identifier_len);
210 .arg(host->toText());
217 const uint8_t prefix_len)
const {
221 .arg(
static_cast<int>(prefix_len));
226 host =
impl_->get6(prefix, prefix_len);
232 .arg(
static_cast<int>(prefix_len))
233 .arg(host->toText());
247 if (!address.
isV6()) {
254 host =
impl_->get6(subnet_id, address);
261 .arg(host->toText());
284 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED &&
285 host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
292 ret =
impl_->add(host);
296 .arg(host->toText());
300 .arg(host->toText());
311 txt =
impl_->del4(subnet_id, addr);
320 }
else if (addr.
isV6()) {
323 txt =
impl_->del6(subnet_id, addr);
339 const uint8_t* identifier_begin,
340 const size_t identifier_len) {
344 txt =
impl_->del4(subnet_id, identifier_type,
345 identifier_begin, identifier_len);
362 const uint8_t* identifier_begin,
363 const size_t identifier_len) {
367 txt =
impl_->del6(subnet_id, identifier_type,
368 identifier_begin, identifier_len);
400 return (
impl_->toElement());
406 return (
impl_->insert(host, overwrite));
412 return (
impl_->remove(host));
428 return (
impl_->size());
434 return (
impl_->capacity());
439 const uint8_t* identifier_begin,
440 const size_t identifier_len)
const {
442 return (
impl_->get(identifier_type, identifier_begin, identifier_len));
452 count =
impl_->size();
453 }
catch (
const std::exception& ex) {
463 msg << count <<
" entries.";
480 }
catch (
const std::exception& ex) {
497 size_t before =
impl_->size();
498 string txt =
"(missing parameters)";
513 "please use cache-clear command");
524 how_many =
static_cast<size_t>(val);
525 impl_->flush(how_many);
526 }
catch (
const std::exception& ex) {
536 msg <<
"Cache flushed (" << before -
impl_->size() <<
" entries removed).";
550 dump =
impl_->toElement();
551 count = dump->size();
552 }
catch (
const std::exception& ex) {
562 msg << count <<
" entries returned.";
573 vector<uint8_t> ident;
574 string txt =
"(missing parameters)";
593 unsigned have_ident = 0;
595 for (
auto const& cfg : map) {
596 if (cfg.first ==
"hw-address") {
599 ident_txt = cfg.second->stringValue();
600 }
else if (cfg.first ==
"duid") {
603 ident_txt = cfg.second->stringValue();
604 }
else if (cfg.first ==
"circuit-id") {
607 ident_txt = cfg.second->stringValue();
608 }
else if (cfg.first ==
"client-id") {
611 ident_txt = cfg.second->stringValue();
612 }
else if (cfg.first ==
"flex-id") {
615 ident_txt = cfg.second->stringValue();
618 "unknown parameter '" << cfg.first <<
"'");
623 if (have_ident == 0) {
626 if (have_ident > 1) {
629 if (ident_txt.empty()) {
645 hosts =
impl_->get(id_type, &ident[0], ident.size());
647 for (
auto const& host : hosts) {
650 count = result->size();
651 }
catch (
const std::exception& ex) {
661 msg << count <<
" entries returned.";
673 size_t overwritten = 0;
674 string txt =
"(missing parameters)";
692 overwritten +=
impl_->insert(entry,
true);
693 }
catch (
const std::exception& ex) {
695 "Insert host (" << entry->toText()
696 <<
") failed: " << ex.what());
702 for (
auto const& entry : entries) {
706 overwritten +=
impl_->insert(entry,
true);
707 }
catch (
const std::exception& ex) {
709 <<
" host (" << entry->toText()
710 <<
"): " << ex.what());
714 }
catch (
const std::exception& ex) {
726 msg << count <<
" entries inserted (" << overwritten
727 <<
" overwritten by more recent entries).";
739 string txt =
"(missing parameters)";
756 }
catch (
const std::exception& ex) {
760 ofstream out(filename, ios::trunc);
763 <<
"' for writing.");
766 dump =
impl_->toElement();
767 count = dump->size();
770 }
catch (
const std::exception& ex) {
781 msg << count <<
" entries dumped to '" << filename <<
"'.";
792 size_t overwritten = 0;
793 string txt =
"(missing parameters)";
809 if (filename.empty()) {
820 for (
auto const& entry : entries) {
823 overwritten +=
impl_->insert(entry,
true);
824 }
catch (
const std::exception& ex) {
826 <<
" host ( " << entry->toText()
827 <<
"): " << ex.what());
830 }
catch (
const std::exception& ex) {
842 msg << count <<
" entries loaded from '" << filename <<
"' ("
843 << overwritten <<
" overwritten by more recent entries).";
850 uint16_t family = AF_UNIX;
854 vector<uint8_t> ident;
855 string txt =
"(missing parameters)";
873 bool have_ident =
false;
876 for (
auto const& cfg : map) {
877 if (cfg.first ==
"subnet-id") {
878 if (family != AF_UNIX) {
882 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
883 }
else if (cfg.first ==
"subnet-id4") {
884 if (family != AF_UNIX) {
888 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
889 }
else if (cfg.first ==
"subnet-id6") {
890 if (family != AF_UNIX) {
894 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
895 }
else if (cfg.first ==
"ip-address") {
896 addr_txt = cfg.second->stringValue();
897 }
else if (cfg.first ==
"hw-address") {
903 ident_txt = cfg.second->stringValue();
904 }
else if (cfg.first ==
"duid") {
910 ident_txt = cfg.second->stringValue();
911 }
else if (cfg.first ==
"circuit-id") {
917 ident_txt = cfg.second->stringValue();
918 }
else if (cfg.first ==
"client-id") {
924 ident_txt = cfg.second->stringValue();
925 }
else if (cfg.first ==
"flex-id") {
931 ident_txt = cfg.second->stringValue();
934 "unknown parameter '" << cfg.first <<
"'");
940 if (valid && !addr_txt.empty()) {
944 if (family == AF_UNIX) {
949 if (family == AF_INET6) {
956 }
else if (addr.
isV6()) {
957 if (family == AF_INET) {
966 "invalid ip-address '" << addr_txt <<
"'");
971 "either ip-address or an identifier is required");
973 if (ident_txt.empty()) {
976 if ((family != AF_INET) && (family != AF_INET6)) {
978 "either subnet-id4 or subnet-id6 is required");
987 "invalid identifier '" << ident_txt <<
"'");
998 if (!addr_txt.empty()) {
999 if (family == AF_INET) {
1000 del_txt =
impl_->del4(subnet_id, addr);
1002 del_txt =
impl_->del6(subnet_id, addr);
1004 }
else if (family == AF_INET) {
1005 del_txt =
impl_->del4(subnet_id, id_type, &ident[0], ident.size());
1007 del_txt =
impl_->del6(subnet_id, id_type, &ident[0], ident.size());
1009 }
catch (
const std::exception& ex) {
1019 if (!del_txt.empty()) {
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr fromJSONFile(const std::string &file_name, bool preproc=false)
Reads contents of specified file and interprets it as JSON.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
bool isV4Zero() const
Convenience function to check if it is an IPv4 zero address.
std::string toText() const
Convert the address to a string.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
bool isV6Zero() const
Convenience function to check if it is an IPv4 zero address.
void setErrorResponse(hooks::CalloutHandle &handle, const std::string &text, int status=CONTROL_RESULT_ERROR)
Set the callout argument "response" to indicate an error.
data::ConstElementPtr cmd_args_
Stores the command arguments extracted by a call to extractCommand.
void extractCommand(hooks::CalloutHandle &handle)
Extracts the command name and arguments from a Callout handle.
void setSuccessResponse(hooks::CalloutHandle &handle, const std::string &text)
Set the callout argument "response" to indicate success.
void setResponse(hooks::CalloutHandle &handle, data::ConstElementPtr &response)
Set the callout argument "response" to the given response.
Database duplicate entry error.
std::string validatePath(const std::string data_path) const
Validates a file path against the supported directory for DHDP data.
static CfgMgr & instance()
returns a single instance of Configuration Manager
Wraps value holding size of the page with host reservations.
IdentifierType
Type of the host identifier.
@ IDENT_FLEX
Flexible host identifier.
std::string getIdentifierAsText() const
Returns host identifier in a textual form.
Per-packet callout handle.
Configuration parser for Host Cache.
static const int64_t MAXIMUM
Absolute maximum number of elements.
void parse(HostCache &hcref, const data::ConstElementPtr &config)
Parses Host Cache configuration.
Host cache content parser.
dhcp::HostCollection parse(const data::ConstElementPtr &entry_list)
Parses Host Cache entries.
dhcp::HostPtr parse(const data::ConstElementPtr &entry)
Parses Host Cache entry.
Host Cache hooks library implementation.
virtual size_t size() const
Return the number of entries.
virtual void configure(const data::ConstElementPtr &config)
Parses configuration.
virtual bool setIPReservationsUnique(const bool unique)
Controls whether IP reservations are unique or non-unique.
boost::shared_ptr< HostCacheImpl > impl_
Implementation.
virtual size_t getMaximum() const
Get maximum number of cached hosts.
virtual void flush(size_t count)
Flush entries.
int cacheLoadHandler(hooks::CalloutHandle &handle)
cache-load command handler.
virtual dhcp::ConstHostCollection getAll(const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
BaseHostDataSource methods.
virtual bool del6(const dhcp::SubnetID &subnet_id, const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet-id6, identifier, identifier-type)
virtual bool del4(const dhcp::SubnetID &subnet_id, const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len)
Attempts to delete a host by (subnet-id4, identifier, identifier-type)
virtual dhcp::ConstHostCollection getAllbyHostname4(const std::string &hostname, const dhcp::SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv4 subnet.
virtual ~HostCache()
Destructor.
void update(isc::dhcp::HostPtr const &host)
Implements isc::dhcp::BaseHostDataSource::update() for HostCache.
virtual dhcp::ConstHostPtr get6(const dhcp::SubnetID &subnet_id, const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv6 subnet.
int cacheRemoveHandler(hooks::CalloutHandle &handle)
cache-remove command handler.
virtual size_t capacity() const
Return the maximum number of entries.
virtual void setMaximum(size_t maximum)
Set maximum number of cached hosts (0 means unbound).
virtual void add(const dhcp::HostPtr &host)
Adds a new host to the collection.
int cacheInsertHandler(hooks::CalloutHandle &handle)
cache-insert command handler.
boost::scoped_ptr< std::mutex > mutex_
mutex
virtual bool del(const dhcp::SubnetID &subnet_id, const asiolink::IOAddress &addr)
Attempts to delete a host by (subnet-id, address)
virtual size_t insert(const dhcp::ConstHostPtr &host, bool overwrite)
Insert a host into the cache.
int cacheGetByIdHandler(hooks::CalloutHandle &handle)
cache-get-by-id command handler.
int cacheWriteHandler(hooks::CalloutHandle &handle)
cache-write command handler.
virtual dhcp::ConstHostCollection getPage4(const dhcp::SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const dhcp::HostPageSize &page_size) const
Return range of hosts in a DHCPv4 subnet.
virtual dhcp::ConstHostCollection getAll6(const dhcp::SubnetID &subnet_id) const
Return all hosts in a DHCPv6 subnet.
virtual dhcp::ConstHostCollection getAll4(const dhcp::SubnetID &subnet_id) const
Return all hosts in a DHCPv4 subnet.
int cacheFlushHandler(hooks::CalloutHandle &handle)
cache-flush command handler.
virtual dhcp::ConstHostCollection getAllbyHostname6(const std::string &hostname, const dhcp::SubnetID &subnet_id) const
Return all hosts with a hostname in a DHCPv6 subnet.
virtual dhcp::ConstHostCollection getAllbyHostname(const std::string &hostname) const
Return all hosts with a hostname.
virtual dhcp::ConstHostCollection get(const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Return all hosts connected to any subnet for which reservations have been made using a specified iden...
int cacheSizeHandler(hooks::CalloutHandle &handle)
Command handlers.
virtual bool remove(const dhcp::HostPtr &host)
Remove a host from the cache.
virtual data::ElementPtr toElement() const
Returns the whole content of the cache as Element tree.
virtual dhcp::ConstHostCollection getPage6(const dhcp::SubnetID &subnet_id, size_t &source_index, uint64_t lower_host_id, const dhcp::HostPageSize &page_size) const
Return range of hosts in a DHCPv6 subnet.
virtual dhcp::ConstHostPtr get4(const dhcp::SubnetID &subnet_id, const dhcp::Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len) const
Returns a host connected to the IPv4 subnet.
int cacheGetHandler(hooks::CalloutHandle &handle)
cache-get command handler.
int cacheClearHandler(hooks::CalloutHandle &handle)
cache-clear command handler.
RAII class creating a critical section.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
const int CONTROL_RESULT_EMPTY
Status code indicating that the specified command was completed correctly, but failed to produce any ...
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
void prettyPrint(ConstElementPtr element, std::ostream &out, unsigned indent, unsigned step)
Pretty prints the data into stream.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
boost::shared_ptr< Host > HostPtr
Pointer to the Host object.
std::vector< ConstHostPtr > ConstHostCollection
Collection of the const Host objects.
std::vector< HostPtr > HostCollection
Collection of the Host objects.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
boost::shared_ptr< const Host > ConstHostPtr
Const pointer to the Host object.
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS4_HOST
const isc::log::MessageID HOST_CACHE_ADD_DUPLICATE
const isc::log::MessageID HOST_CACHE_COMMAND_REMOVE_FAILED
const isc::log::MessageID HOST_CACHE_COMMAND_CLEAR_FAILED
const isc::log::MessageID HOST_CACHE_COMMAND_GET_BY_ID
const isc::log::MessageID HOST_CACHE_COMMAND_FLUSH
const isc::log::MessageID HOST_CACHE_COMMAND_LOAD_FAILED
const isc::log::MessageID HOST_CACHE_COMMAND_GET
const isc::log::MessageID HOST_CACHE_ADD
const int HOST_CACHE_DBG_RESULTS
Records the results of the lookups.
const isc::log::MessageID HOST_CACHE_COMMAND_INSERT
const isc::log::MessageID HOST_CACHE_COMMAND_WRITE_FAILED
const isc::log::MessageID HOST_CACHE_DEL_SUBNET_ID_IDENTIFIER4
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER_HOST
const isc::log::MessageID HOST_CACHE_COMMAND_SIZE_FAILED
const isc::log::MessageID HOST_CACHE_COMMAND_FLUSH_FAILED
const isc::log::MessageID HOST_CACHE_COMMAND_LOAD
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER
const isc::log::MessageID HOST_CACHE_DEL_SUBNET_ID_ADDRESS4
const isc::log::MessageID HOST_CACHE_GET_ONE_PREFIX
const isc::log::MessageID HOST_CACHE_COMMAND_GET_BY_ID_FAILED
const isc::log::MessageID HOST_CACHE_DEL_SUBNET_ID_ADDRESS6
const isc::log::MessageID HOST_CACHE_GET_ONE_PREFIX_HOST
const isc::log::MessageID HOST_CACHE_COMMAND_CLEAR
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS6
const isc::log::MessageID HOST_CACHE_COMMAND_SIZE
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS4
const isc::log::MessageID HOST_CACHE_COMMAND_REMOVE
const isc::log::MessageID HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS6_HOST
const isc::log::MessageID HOST_CACHE_COMMAND_GET_FAILED
ElementPtr toElement(const ConstHostPtr &host)
Unparse a host cache entry.
const isc::log::MessageID HOST_CACHE_COMMAND_WRITE
const int HOST_CACHE_DBG_TRACE
Host Cache hooks library logging levels.
const isc::log::MessageID HOST_CACHE_COMMAND_INSERT_FAILED
const isc::log::MessageID HOST_CACHE_DEL_SUBNET_ID_IDENTIFIER6
isc::log::Logger host_cache_logger("host-cache-hooks")
Host Cache Logger.
void decodeFormattedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
vector< uint8_t > quotedStringToBinary(const string "ed_string)
Converts a string in quotes into vector.
Defines the logger used by the top-level component of kea-lfc.
RAII lock object to protect the code in the same scope with a mutex.