resolver.h 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2009  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
#ifndef RESOLVER_H
#define RESOLVER_H 1
17 18

#include <string>
Michal Vaner's avatar
Michal Vaner committed
19 20
#include <vector>
#include <utility>
21

22 23
#include <boost/shared_ptr.hpp>

24 25
#include <cc/data.h>
#include <config/ccsession.h>
26
#include <acl/dns.h>
27
#include <dns/message.h>
28
#include <util/buffer.h>
29

30 31 32 33
#include <asiodns/dns_server.h>
#include <asiodns/dns_service.h>
#include <asiodns/dns_lookup.h>
#include <asiodns/dns_answer.h>
34 35 36
#include <asiolink/io_message.h>
#include <asiolink/io_service.h>
#include <asiolink/simple_callback.h>
37

38 39 40
#include <nsas/nameserver_address_store.h>
#include <cache/resolver_cache.h>

41
#include <resolve/resolver_interface.h>
42

43
class ResolverImpl;
44

Michal Vaner's avatar
Michal Vaner committed
45 46 47 48 49 50 51 52 53
/**
 * \short The recursive nameserver.
 *
 * It is a concreate class implementing recursive DNS server protocol
 * processing. It is responsible for handling incoming DNS requests. It parses
 * them, passes them deeper into the resolving machinery and then creates the
 * answer. It doesn't really know about chasing referrals and similar, it
 * simply plugs the parts that know into the network handling code.
 */
54
class Resolver : public isc::resolve::ResolverInterface {
55 56 57 58 59 60 61
    ///
    /// \name Constructors, Assignment Operator and Destructor.
    ///
    /// Note: The copy constructor and the assignment operator are
    /// intentionally defined as private.
    //@{
private:
62 63
    Resolver(const Resolver& source);
    Resolver& operator=(const Resolver& source);
64 65
public:
    /// The constructor.
66 67
    Resolver();
    ~Resolver();
68
    //@}
69

70 71 72
    virtual void resolve(
        const isc::dns::QuestionPtr& question,
        const isc::resolve::ResolverInterface::CallbackPtr& callback);
73

74
    /// \brief Process an incoming DNS message, then signal 'server' to resume 
Evan Hunt's avatar
Evan Hunt committed
75 76 77 78 79 80
    ///
    /// A DNS query (or other message) has been received by a \c DNSServer
    /// object.  Find an answer, then post the \c DNSServer object on the
    /// I/O service queue and return.  When the server resumes, it can
    /// send the reply.
    ///
Evan Hunt's avatar
Evan Hunt committed
81
    /// \param io_message The raw message received
82 83 84 85
    /// \param query_message Pointer to the query Message object we
    /// received from the client
    /// \param answer_message Pointer to the anwer Message object we
    /// shall return to the client
Evan Hunt's avatar
Evan Hunt committed
86 87
    /// \param buffer Pointer to an \c OutputBuffer for the resposne
    /// \param server Pointer to the \c DNSServer
88
    void processMessage(const isc::asiolink::IOMessage& io_message,
89
                        isc::dns::MessagePtr query_message,
90
                        isc::dns::MessagePtr answer_message,
91
                        isc::util::OutputBufferPtr buffer,
92
                        isc::asiodns::DNSServer* server);
93 94 95

    /// \brief Set and get the config session
    isc::config::ModuleCCSession* getConfigSession() const;
96 97
    void setConfigSession(isc::config::ModuleCCSession* config_session);

98 99 100 101 102 103 104
    /// \brief Handle commands from the config session.
    ///
    /// By default, or if \c startup is set to false explicitly, this method
    /// will ignore any other configuration parameters if listen_on fails;
    /// if \c startup is true, it will install as many parameters as possible.
    isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr config,
                                            bool startup = false);
105

106
    /// \brief Assign an ASIO IO Service queue to this Resolver object
107
    void setDNSService(isc::asiodns::DNSServiceBase& dnss);
108

109 110
    /// \brief Assign a NameserverAddressStore to this Resolver object
    void setNameserverAddressStore(isc::nsas::NameserverAddressStore &nsas);
111

112 113
    /// \brief Assign a cache to this Resolver object
    void setCache(isc::cache::ResolverCache& cache);
114 115

    /// \brief Return this object's ASIO IO Service queue
116
    isc::asiodns::DNSServiceBase& getDNSService() const { return (*dnss_); }
117

118 119 120 121 122 123 124 125 126
    /// \brief Returns this object's NSAS
    isc::nsas::NameserverAddressStore& getNameserverAddressStore() const {
        return *nsas_;
    };

    /// \brief Returns this object's ResolverCache
    isc::cache::ResolverCache& getResolverCache() const {
        return *cache_;
    };
127

128
    /// \brief Return pointer to the DNS Lookup callback function
129
    isc::asiodns::DNSLookup* getDNSLookupProvider() { return (dns_lookup_); }
130 131

    /// \brief Return pointer to the DNS Answer callback function
132
    isc::asiodns::DNSAnswer* getDNSAnswerProvider() { return (dns_answer_); }
133 134

    /// \brief Return pointer to the Checkin callback function
135
    isc::asiolink::SimpleCallback* getCheckinProvider() { return (checkin_); }
136

Michal Vaner's avatar
Michal Vaner committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    /**
     * \brief Specify the list of upstream servers.
     *
     * Specify the list off addresses of upstream servers to forward queries
     * to. If the list is empty, this server is set to full recursive mode.
     * If it is non-empty, it switches to forwarder.
     *
     * @param addresses The list of addresses to use (each one is the address
     * and port pair).
     */
    void setForwardAddresses(const std::vector<std::pair<std::string,
        uint16_t> >& addresses);
    /**
     * \short Get list of upstream addresses.
     *
     * \see setForwardAddresses.
     */
    std::vector<std::pair<std::string, uint16_t> > getForwardAddresses() const;
    /// Return if we are in forwarding mode (if not, we are in fully recursive)
    bool isForwarding() const;

chenzhengzhang's avatar
chenzhengzhang committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    /**
     * \brief Specify the list of root nameservers.
     *
     * Specify the list of addresses of root nameservers
     *
     * @param addresses The list of addresses to use (each one is the address
     * and port pair).
     */
    void setRootAddresses(const std::vector<std::pair<std::string,
                          uint16_t> >& addresses);

    /**
     * \short Get list of root addresses.
     *
     * \see setRootAddresses.
     */
    std::vector<std::pair<std::string, uint16_t> > getRootAddresses() const;

176 177 178 179 180 181 182
    /**
     * Set and get the addresses we listen on.
     */
    void setListenAddresses(const std::vector<std::pair<std::string,
        uint16_t> >& addresses);
    std::vector<std::pair<std::string, uint16_t> > getListenAddresses() const;

Michal Vaner's avatar
Michal Vaner committed
183 184 185 186
    /**
     * \short Set options related to timeouts.
     *
     * This sets the time of timeout and number of retries.
187 188 189 190 191
     * \param query_timeout The timeout we use for queries we send
     * \param client_timeout The timeout at which point we send back a
     * SERVFAIL (while continuing to resolve the query)
     * \param lookup_timeout The timeout at which point we give up and
     * stop.
Michal Vaner's avatar
Michal Vaner committed
192 193 194
     * \param retries The number of retries (0 means try the first time only,
     *     do not retry).
     */
195 196 197 198
    void setTimeouts(int query_timeout = 2000,
                     int client_timeout = 4000,
                     int lookup_timeout = 30000,
                     unsigned retries = 3);
Michal Vaner's avatar
Michal Vaner committed
199 200 201 202

    /**
     * \short Get info about timeouts.
     *
203
     * \return Timeout and retries (as described in setTimeouts).
Michal Vaner's avatar
Michal Vaner committed
204 205 206
     */
    std::pair<int, unsigned> getTimeouts() const;

207 208 209
    /**
     * \brief Get the timeout for outgoing queries
     *
210
     * \return Timeout for outgoing queries
211 212 213 214 215 216 217 218 219 220
     */
    int getQueryTimeout() const;

    /**
     * \brief Get the timeout for incoming client queries
     *
     * After this timeout, a SERVFAIL shall be sent back
     * (internal resolving on the query will continue, see
     * \c getLookupTimeout())
     * 
221
     * \return Timeout for outgoing queries
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
     */
    int getClientTimeout() const;

    /**
     * \brief Get the timeout for lookups
     *
     * After this timeout, internal processing shall stop
     */
    int getLookupTimeout() const;

    /**
     * \brief Get the number of retries for outgoing queries
     *
     * If a query times out (value of \c getQueryTimeout()), we
     * will retry this number of times
     */
    int getRetries() const;

240 241 242
    /// Get the query ACL.
    ///
    /// \exception None
243
    const isc::acl::dns::RequestACL& getQueryACL() const;
244

245 246 247 248 249 250 251 252 253 254 255
    /// Set the new query ACL.
    ///
    /// This method replaces the existing query ACL completely.
    /// Normally this method will be called via the configuration handler,
    /// but is publicly available for convenience of tests (and other
    /// experimental purposes).
    /// \c new_acl must not be a NULL pointer.
    ///
    /// \exception InvalidParameter The given pointer is NULL
    ///
    /// \param new_acl The new ACL to replace the existing one.
256 257
    void setQueryACL(boost::shared_ptr<const isc::acl::dns::RequestACL>
                     new_acl);
258

259
private:
260
    ResolverImpl* impl_;
261
    isc::asiodns::DNSServiceBase* dnss_;
262 263 264
    isc::asiolink::SimpleCallback* checkin_;
    isc::asiodns::DNSLookup* dns_lookup_;
    isc::asiodns::DNSAnswer* dns_answer_;
265 266
    isc::nsas::NameserverAddressStore* nsas_;
    isc::cache::ResolverCache* cache_;
267 268
};

269
#endif // RESOLVER_H
270

271
// Local Variables:
272
// mode: c++
273
// End: