nameserver_address_store.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/foreach.hpp>
  17. #include <boost/bind.hpp>
  18. #include <config.h>
  19. #include <dns/rdataclass.h>
  20. #include <util/locks.h>
  21. #include <util/lru_list.h>
  22. #include <log/logger.h>
  23. #include "hash_table.h"
  24. #include "hash_deleter.h"
  25. #include "nsas_entry_compare.h"
  26. #include "nameserver_entry.h"
  27. #include "nameserver_address_store.h"
  28. #include "zone_entry.h"
  29. #include "glue_hints.h"
  30. #include "address_request_callback.h"
  31. #include "nsas_log.h"
  32. using namespace isc::dns;
  33. using namespace std;
  34. namespace isc {
  35. namespace nsas {
  36. // Constructor.
  37. //
  38. // The LRU lists are set equal to three times the size of the respective
  39. // hash table, on the assumption that three elements is the longest linear
  40. // search we want to do when looking up names in the hash table.
  41. NameserverAddressStore::NameserverAddressStore(
  42. boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
  43. uint32_t zonehashsize, uint32_t nshashsize) :
  44. zone_hash_(new HashTable<ZoneEntry>(new NsasEntryCompare<ZoneEntry>,
  45. zonehashsize)),
  46. nameserver_hash_(new HashTable<NameserverEntry>(
  47. new NsasEntryCompare<NameserverEntry>, nshashsize)),
  48. zone_lru_(new isc::util::LruList<ZoneEntry>((3 * zonehashsize),
  49. new HashDeleter<ZoneEntry>(*zone_hash_))),
  50. nameserver_lru_(new isc::util::LruList<NameserverEntry>((3 * nshashsize),
  51. new HashDeleter<NameserverEntry>(*nameserver_hash_))),
  52. resolver_(resolver.get())
  53. { }
  54. namespace {
  55. /*
  56. * We use pointers here so there's no call to any copy constructor.
  57. * It is easier for the compiler to inline it and prove that there's
  58. * no need to copy anything. In that case, the bind should not be
  59. * called at all to create the object, just call the function.
  60. */
  61. boost::shared_ptr<ZoneEntry>
  62. newZone(
  63. isc::resolve::ResolverInterface* resolver,
  64. const string* zone, const RRClass* class_code,
  65. const boost::shared_ptr<HashTable<NameserverEntry> >* ns_hash,
  66. const boost::shared_ptr<isc::util::LruList<NameserverEntry> >* ns_lru)
  67. {
  68. boost::shared_ptr<ZoneEntry> result(new ZoneEntry(resolver, *zone, *class_code,
  69. *ns_hash, *ns_lru));
  70. return (result);
  71. }
  72. }
  73. void
  74. NameserverAddressStore::lookup(const string& zone, const RRClass& class_code,
  75. boost::shared_ptr<AddressRequestCallback> callback, AddressFamily family,
  76. const GlueHints& glue_hints)
  77. {
  78. LOG_DEBUG(nsas_logger, NSAS_DBG_TRACE, NSAS_SEARCH_ZONE_NS).arg(zone);
  79. pair<bool, boost::shared_ptr<ZoneEntry> > zone_obj(
  80. zone_hash_->getOrAdd(HashKey(zone, class_code),
  81. boost::bind(newZone, resolver_, &zone, &class_code,
  82. &nameserver_hash_, &nameserver_lru_)));
  83. if (zone_obj.first) {
  84. zone_lru_->add(zone_obj.second);
  85. } else {
  86. zone_lru_->touch(zone_obj.second);
  87. }
  88. zone_obj.second->addCallback(callback, family, glue_hints);
  89. }
  90. void
  91. NameserverAddressStore::cancel(const string& zone,
  92. const RRClass& class_code,
  93. const boost::shared_ptr<AddressRequestCallback>& callback,
  94. AddressFamily family)
  95. {
  96. LOG_DEBUG(nsas_logger, NSAS_DBG_TRACE, NSAS_LOOKUP_CANCEL).arg(zone);
  97. boost::shared_ptr<ZoneEntry> entry(zone_hash_->get(HashKey(zone,
  98. class_code)));
  99. if (entry) {
  100. entry->removeCallback(callback, family);
  101. }
  102. }
  103. } // namespace nsas
  104. } // namespace isc