nameserver_address_store.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "locks.h"
  21. #include "hash_table.h"
  22. #include "lru_list.h"
  23. #include "hash_deleter.h"
  24. #include "nsas_entry_compare.h"
  25. #include "nameserver_entry.h"
  26. #include "nameserver_address_store.h"
  27. #include "zone_entry.h"
  28. #include "address_request_callback.h"
  29. using namespace isc::dns;
  30. using namespace std;
  31. namespace isc {
  32. namespace nsas {
  33. // Constructor.
  34. //
  35. // The LRU lists are set equal to three times the size of the respective
  36. // hash table, on the assumption that three elements is the longest linear
  37. // search we want to do when looking up names in the hash table.
  38. NameserverAddressStore::NameserverAddressStore(
  39. boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
  40. uint32_t zonehashsize, uint32_t nshashsize) :
  41. zone_hash_(new HashTable<ZoneEntry>(new NsasEntryCompare<ZoneEntry>,
  42. zonehashsize)),
  43. nameserver_hash_(new HashTable<NameserverEntry>(
  44. new NsasEntryCompare<NameserverEntry>, nshashsize)),
  45. zone_lru_(new LruList<ZoneEntry>((3 * zonehashsize),
  46. new HashDeleter<ZoneEntry>(*zone_hash_))),
  47. nameserver_lru_(new LruList<NameserverEntry>((3 * nshashsize),
  48. new HashDeleter<NameserverEntry>(*nameserver_hash_))),
  49. resolver_(resolver)
  50. { }
  51. namespace {
  52. /*
  53. * We use pointers here so there's no call to any copy constructor.
  54. * It is easier for the compiler to inline it and prove that there's
  55. * no need to copy anything. In that case, the bind should not be
  56. * called at all to create the object, just call the function.
  57. */
  58. boost::shared_ptr<ZoneEntry>
  59. newZone(
  60. const boost::shared_ptr<isc::resolve::ResolverInterface>* resolver,
  61. const string* zone, const RRClass* class_code,
  62. const boost::shared_ptr<HashTable<NameserverEntry> >* ns_hash,
  63. const boost::shared_ptr<LruList<NameserverEntry> >* ns_lru)
  64. {
  65. boost::shared_ptr<ZoneEntry> result(new ZoneEntry(*resolver, *zone, *class_code,
  66. *ns_hash, *ns_lru));
  67. return (result);
  68. }
  69. }
  70. void
  71. NameserverAddressStore::lookup(const string& zone, const RRClass& class_code,
  72. boost::shared_ptr<AddressRequestCallback> callback, AddressFamily family)
  73. {
  74. pair<bool, boost::shared_ptr<ZoneEntry> > zone_obj(zone_hash_->getOrAdd(HashKey(
  75. zone, class_code), boost::bind(newZone, &resolver_, &zone, &class_code,
  76. &nameserver_hash_, &nameserver_lru_)));
  77. if (zone_obj.first) {
  78. zone_lru_->add(zone_obj.second);
  79. } else {
  80. zone_lru_->touch(zone_obj.second);
  81. }
  82. zone_obj.second->addCallback(callback, family);
  83. }
  84. void
  85. NameserverAddressStore::cancel(const string& zone,
  86. const RRClass& class_code,
  87. const boost::shared_ptr<AddressRequestCallback>& callback,
  88. AddressFamily family)
  89. {
  90. boost::shared_ptr<ZoneEntry> entry(zone_hash_->get(HashKey(zone,
  91. class_code)));
  92. if (entry) {
  93. entry->removeCallback(callback, family);
  94. }
  95. }
  96. } // namespace nsas
  97. } // namespace isc