nameserver_address_store.cc 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16
#include <config.h>

17
#include <boost/shared_ptr.hpp>
Michal Vaner's avatar
Michal Vaner committed
18
#include <boost/foreach.hpp>
19
#include <boost/bind.hpp>
20

Michal Vaner's avatar
Michal Vaner committed
21
#include <config.h>
Michal Vaner's avatar
Michal Vaner committed
22
#include <dns/rdataclass.h>
23

24
#include "locks.h"
25 26
#include "hash_table.h"
#include "lru_list.h"
27 28 29 30 31
#include "hash_deleter.h"
#include "nsas_entry_compare.h"
#include "nameserver_entry.h"
#include "nameserver_address_store.h"
#include "zone_entry.h"
32
#include "address_request_callback.h"
33

Michal Vaner's avatar
Michal Vaner committed
34 35 36
using namespace isc::dns;
using namespace std;

37 38 39 40 41 42 43 44
namespace isc {
namespace nsas {

// Constructor.
//
// The LRU lists are set equal to three times the size of the respective
// hash table, on the assumption that three elements is the longest linear
// search we want to do when looking up names in the hash table.
45
NameserverAddressStore::NameserverAddressStore(
46 47
    boost::shared_ptr<isc::resolve::ResolverInterface> resolver,
    uint32_t zonehashsize, uint32_t nshashsize) :
48 49 50 51 52 53 54 55 56 57
    zone_hash_(new HashTable<ZoneEntry>(new NsasEntryCompare<ZoneEntry>,
        zonehashsize)),
    nameserver_hash_(new HashTable<NameserverEntry>(
        new NsasEntryCompare<NameserverEntry>, nshashsize)),
    zone_lru_(new LruList<ZoneEntry>((3 * zonehashsize),
        new HashDeleter<ZoneEntry>(*zone_hash_))),
    nameserver_lru_(new LruList<NameserverEntry>((3 * nshashsize),
        new HashDeleter<NameserverEntry>(*nameserver_hash_))),
    resolver_(resolver)
{ }
Michal Vaner's avatar
Michal Vaner committed
58

59 60
namespace {

61
/*
62 63 64 65
 * We use pointers here so there's no call to any copy constructor.
 * It is easier for the compiler to inline it and prove that there's
 * no need to copy anything. In that case, the bind should not be
 * called at all to create the object, just call the function.
66
 */
67
boost::shared_ptr<ZoneEntry>
68 69 70
newZone(
    const boost::shared_ptr<isc::resolve::ResolverInterface>* resolver,
    const string* zone, const RRClass* class_code,
71 72
    const boost::shared_ptr<HashTable<NameserverEntry> >* ns_hash,
    const boost::shared_ptr<LruList<NameserverEntry> >* ns_lru)
73
{
74
    boost::shared_ptr<ZoneEntry> result(new ZoneEntry(*resolver, *zone, *class_code,
75 76
        *ns_hash, *ns_lru));
    return (result);
77 78 79 80
}

}

Michal Vaner's avatar
Michal Vaner committed
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 89 90 91
    if (zone_obj.first) {
        zone_lru_->add(zone_obj.second);
    } else {
        zone_lru_->touch(zone_obj.second);
Michal Vaner's avatar
Michal Vaner committed
92
    }
93
    zone_obj.second->addCallback(callback, family);
94 95 96 97
}

} // namespace nsas
} // namespace isc