Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_DNS_CLIENT_ |
| 6 | #define SHILL_DNS_CLIENT_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/callback.h> |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 12 | #include <base/cancelable_callback.h> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 14 | #include <base/memory/weak_ptr.h> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 16 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 17 | #include "shill/error.h" |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
| 19 | #include "shill/ip_address.h" |
| 20 | #include "shill/refptr_types.h" |
| 21 | |
| 22 | struct hostent; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
| 26 | class Ares; |
| 27 | class Time; |
| 28 | struct DNSClientState; |
| 29 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 30 | // Implements a DNS resolution client that can run asynchronously. |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 31 | class DNSClient { |
| 32 | public: |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 33 | typedef base::Callback<void(const Error &, const IPAddress &)> ClientCallback; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 34 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 35 | static const int kDefaultTimeoutMS; |
| 36 | static const char kErrorNoData[]; |
| 37 | static const char kErrorFormErr[]; |
| 38 | static const char kErrorServerFail[]; |
| 39 | static const char kErrorNotFound[]; |
| 40 | static const char kErrorNotImp[]; |
| 41 | static const char kErrorRefused[]; |
| 42 | static const char kErrorBadQuery[]; |
| 43 | static const char kErrorNetRefused[]; |
| 44 | static const char kErrorTimedOut[]; |
| 45 | static const char kErrorUnknown[]; |
| 46 | |
| 47 | DNSClient(IPAddress::Family family, |
| 48 | const std::string &interface_name, |
| 49 | const std::vector<std::string> &dns_servers, |
| 50 | int timeout_ms, |
| 51 | EventDispatcher *dispatcher, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 52 | const ClientCallback &callback); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 53 | virtual ~DNSClient(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 54 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 55 | // Returns true if the DNS client started successfully, false otherwise. |
| 56 | // If successful, the callback will be called with the result of the |
| 57 | // request. If Start() fails and returns false, the callback will not |
| 58 | // be called, but the error that caused the failure will be returned in |
| 59 | // |error|. |
| 60 | virtual bool Start(const std::string &hostname, Error *error); |
| 61 | |
| 62 | // Aborts any running DNS client transaction. This will cancel any callback |
| 63 | // invocation. |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 64 | virtual void Stop(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | friend class DNSClientTest; |
| 68 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 69 | void HandleCompletion(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 70 | void HandleDNSRead(int fd); |
| 71 | void HandleDNSWrite(int fd); |
| 72 | void HandleTimeout(); |
| 73 | void ReceiveDNSReply(int status, struct hostent *hostent); |
| 74 | static void ReceiveDNSReplyCB(void *arg, int status, int timeouts, |
| 75 | struct hostent *hostent); |
| 76 | bool RefreshHandles(); |
| 77 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 78 | Error error_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 79 | IPAddress address_; |
| 80 | std::string interface_name_; |
| 81 | std::vector<std::string> dns_servers_; |
| 82 | EventDispatcher *dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 83 | ClientCallback callback_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 84 | int timeout_ms_; |
| 85 | bool running_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 86 | scoped_ptr<DNSClientState> resolver_state_; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 87 | base::CancelableClosure timeout_closure_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 88 | base::WeakPtrFactory<DNSClient> weak_ptr_factory_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 89 | Ares *ares_; |
| 90 | Time *time_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(DNSClient); |
| 93 | }; |
| 94 | |
| 95 | } // namespace shill |
| 96 | |
| 97 | #endif // SHILL_DNS_CLIENT_ |