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