51 impl_->setMaximum(maximum);
56 return (
impl_->getMaximum());
62 const size_t )
const {
112 const dhcp:: HostPageSize& )
const {
131 const uint8_t* identifier_begin,
132 const size_t identifier_len)
const {
143 host =
impl_->get4(subnet_id, identifier_type,
144 identifier_begin, identifier_len);
152 .arg(host->toText());
166 if (!address.
isV4()) {
172 host =
impl_->get4(subnet_id, address);
179 .arg(host->toText());
193 const uint8_t* identifier_begin,
194 const size_t identifier_len)
const {
205 host =
impl_->get6(subnet_id, identifier_type,
206 identifier_begin, identifier_len);
214 .arg(host->toText());
221 const uint8_t prefix_len)
const {
225 .arg(
static_cast<int>(prefix_len));
230 host =
impl_->get6(prefix, prefix_len);
236 .arg(
static_cast<int>(prefix_len))
237 .arg(host->toText());
251 if (!address.
isV6()) {
258 host =
impl_->get6(subnet_id, address);
265 .arg(host->toText());
288 if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED &&
289 host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
296 ret =
impl_->add(host);
300 .arg(host->toText());
304 .arg(host->toText());
315 txt =
impl_->del4(subnet_id, addr);
324 }
else if (addr.
isV6()) {
327 txt =
impl_->del6(subnet_id, addr);
343 const uint8_t* identifier_begin,
344 const size_t identifier_len) {
348 txt =
impl_->del4(subnet_id, identifier_type,
349 identifier_begin, identifier_len);
366 const uint8_t* identifier_begin,
367 const size_t identifier_len) {
371 txt =
impl_->del6(subnet_id, identifier_type,
372 identifier_begin, identifier_len);
404 return (
impl_->toElement());
410 return (
impl_->insert(host, overwrite));
416 return (
impl_->remove(host));
432 return (
impl_->size());
438 return (
impl_->capacity());
443 const uint8_t* identifier_begin,
444 const size_t identifier_len)
const {
446 return (
impl_->get(identifier_type, identifier_begin, identifier_len));
456 count =
impl_->size();
457 }
catch (
const std::exception& ex) {
467 msg << count <<
" entries.";
484 }
catch (
const std::exception& ex) {
501 size_t before =
impl_->size();
502 string txt =
"(missing parameters)";
517 "please use cache-clear command");
528 how_many =
static_cast<size_t>(val);
529 impl_->flush(how_many);
530 }
catch (
const std::exception& ex) {
540 msg <<
"Cache flushed (" << before -
impl_->size() <<
" entries removed).";
554 dump =
impl_->toElement();
555 count = dump->size();
556 }
catch (
const std::exception& ex) {
566 msg << count <<
" entries returned.";
577 vector<uint8_t> ident;
578 string txt =
"(missing parameters)";
597 unsigned have_ident = 0;
599 for (
auto const& cfg : map) {
600 if (cfg.first ==
"hw-address") {
603 ident_txt = cfg.second->stringValue();
604 }
else if (cfg.first ==
"duid") {
607 ident_txt = cfg.second->stringValue();
608 }
else if (cfg.first ==
"circuit-id") {
611 ident_txt = cfg.second->stringValue();
612 }
else if (cfg.first ==
"client-id") {
615 ident_txt = cfg.second->stringValue();
616 }
else if (cfg.first ==
"flex-id") {
619 ident_txt = cfg.second->stringValue();
622 "unknown parameter '" << cfg.first <<
"'");
627 if (have_ident == 0) {
630 if (have_ident > 1) {
633 if (ident_txt.empty()) {
647 if (!ident.empty()) {
649 hosts =
impl_->get(id_type, &ident[0], ident.size());
651 for (
auto const& host : hosts) {
654 count = result->size();
655 }
catch (
const std::exception& ex) {
665 msg << count <<
" entries returned.";
677 size_t overwritten = 0;
678 string txt =
"(missing parameters)";
696 overwritten +=
impl_->insert(entry,
true);
697 }
catch (
const std::exception& ex) {
699 "Insert host (" << entry->toText()
700 <<
") failed: " << ex.what());
706 for (
auto const& entry : entries) {
710 overwritten +=
impl_->insert(entry,
true);
711 }
catch (
const std::exception& ex) {
713 <<
" host (" << entry->toText()
714 <<
"): " << ex.what());
718 }
catch (
const std::exception& ex) {
730 msg << count <<
" entries inserted (" << overwritten
731 <<
" overwritten by more recent entries).";
743 string txt =
"(missing parameters)";
764 }
catch (
const std::exception& ex) {
768 ofstream out(filename, ios::trunc);
771 <<
"' for writing.");
774 dump =
impl_->toElement();
775 count = dump->size();
778 }
catch (
const std::exception& ex) {
789 msg << count <<
" entries dumped to '" << filename <<
"'.";
800 size_t overwritten = 0;
801 string txt =
"(missing parameters)";
817 if (filename.empty()) {
828 for (
auto const& entry : entries) {
831 overwritten +=
impl_->insert(entry,
true);
832 }
catch (
const std::exception& ex) {
834 <<
" host ( " << entry->toText()
835 <<
"): " << ex.what());
838 }
catch (
const std::exception& ex) {
850 msg << count <<
" entries loaded from '" << filename <<
"' ("
851 << overwritten <<
" overwritten by more recent entries).";
858 uint16_t family = AF_UNIX;
862 vector<uint8_t> ident;
863 string txt =
"(missing parameters)";
881 bool have_ident =
false;
884 for (
auto const& cfg : map) {
885 if (cfg.first ==
"subnet-id") {
886 if (family != AF_UNIX) {
890 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
891 }
else if (cfg.first ==
"subnet-id4") {
892 if (family != AF_UNIX) {
896 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
897 }
else if (cfg.first ==
"subnet-id6") {
898 if (family != AF_UNIX) {
902 subnet_id =
static_cast<uint32_t
>(cfg.second->intValue());
903 }
else if (cfg.first ==
"ip-address") {
904 addr_txt = cfg.second->stringValue();
905 }
else if (cfg.first ==
"hw-address") {
911 ident_txt = cfg.second->stringValue();
912 }
else if (cfg.first ==
"duid") {
918 ident_txt = cfg.second->stringValue();
919 }
else if (cfg.first ==
"circuit-id") {
925 ident_txt = cfg.second->stringValue();
926 }
else if (cfg.first ==
"client-id") {
932 ident_txt = cfg.second->stringValue();
933 }
else if (cfg.first ==
"flex-id") {
939 ident_txt = cfg.second->stringValue();
942 "unknown parameter '" << cfg.first <<
"'");
948 if (valid && !addr_txt.empty()) {
952 if (family == AF_UNIX) {
957 if (family == AF_INET6) {
964 }
else if (addr.
isV6()) {
965 if (family == AF_INET) {
974 "invalid ip-address '" << addr_txt <<
"'");
979 "either ip-address or an identifier is required");
981 if (ident_txt.empty()) {
984 if ((family != AF_INET) && (family != AF_INET6)) {
986 "either subnet-id4 or subnet-id6 is required");
995 "invalid identifier '" << ident_txt <<
"'");
1006 if (!addr_txt.empty()) {
1007 if (family == AF_INET) {
1008 del_txt =
impl_->del4(subnet_id, addr);
1010 del_txt =
impl_->del6(subnet_id, addr);
1012 }
else if (ident.empty()) {
1014 }
else if (family == AF_INET) {
1015 del_txt =
impl_->del4(subnet_id, id_type, &ident[0], ident.size());
1017 del_txt =
impl_->del6(subnet_id, id_type, &ident[0], ident.size());
1019 }
catch (
const std::exception& ex) {
1029 if (!del_txt.empty()) {
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
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...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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 DHCP data.
static CfgMgr & instance()
returns a single instance of Configuration Manager
Wraps value holding size of the page with host reservations.
std::string getIdentifierAsText() const
Returns host identifier in a textual form.
IdentifierType
Type of the host identifier.
@ IDENT_FLEX
Flexible host identifier.
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.
A generic exception that is thrown if a parameter given violates security check but enforcement is la...
#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_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn 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_PATH_SECURITY_WARNING
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.