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 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_DNS_CLIENT_H_ |
| 6 | #define SHILL_DNS_CLIENT_H_ |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 7 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 8 | #include <memory> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/callback.h> |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 13 | #include <base/cancelable_callback.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" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 19 | #include "shill/net/ip_address.h" |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 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: |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [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 char kErrorNoData[]; |
| 36 | static const char kErrorFormErr[]; |
| 37 | static const char kErrorServerFail[]; |
| 38 | static const char kErrorNotFound[]; |
| 39 | static const char kErrorNotImp[]; |
| 40 | static const char kErrorRefused[]; |
| 41 | static const char kErrorBadQuery[]; |
| 42 | static const char kErrorNetRefused[]; |
| 43 | static const char kErrorTimedOut[]; |
| 44 | static const char kErrorUnknown[]; |
| 45 | |
| 46 | DNSClient(IPAddress::Family family, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 47 | const std::string& interface_name, |
| 48 | const std::vector<std::string>& dns_servers, |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 49 | int timeout_ms, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 50 | EventDispatcher* dispatcher, |
| 51 | const ClientCallback& callback); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 52 | virtual ~DNSClient(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 53 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 54 | // Returns true if the DNS client started successfully, false otherwise. |
| 55 | // If successful, the callback will be called with the result of the |
| 56 | // request. If Start() fails and returns false, the callback will not |
| 57 | // be called, but the error that caused the failure will be returned in |
| 58 | // |error|. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 59 | virtual bool Start(const std::string& hostname, Error* error); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 60 | |
| 61 | // Aborts any running DNS client transaction. This will cancel any callback |
| 62 | // invocation. |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 63 | virtual void Stop(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 64 | |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 65 | virtual bool IsActive() const; |
| 66 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 67 | std::string interface_name() { return interface_name_; } |
| 68 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 69 | private: |
| 70 | friend class DNSClientTest; |
| 71 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 72 | void HandleCompletion(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 73 | void HandleDNSRead(int fd); |
| 74 | void HandleDNSWrite(int fd); |
| 75 | void HandleTimeout(); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 76 | void ReceiveDNSReply(int status, struct hostent* hostent); |
| 77 | static void ReceiveDNSReplyCB(void* arg, int status, int timeouts, |
| 78 | struct hostent* hostent); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 79 | bool RefreshHandles(); |
| 80 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 81 | static const int kDefaultDNSPort; |
| 82 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 83 | Error error_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 84 | IPAddress address_; |
| 85 | std::string interface_name_; |
| 86 | std::vector<std::string> dns_servers_; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 87 | EventDispatcher* dispatcher_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 88 | ClientCallback callback_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 89 | int timeout_ms_; |
| 90 | bool running_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 91 | std::unique_ptr<DNSClientState> resolver_state_; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 92 | base::CancelableClosure timeout_closure_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 93 | base::WeakPtrFactory<DNSClient> weak_ptr_factory_; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 94 | Ares* ares_; |
| 95 | Time* time_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(DNSClient); |
| 98 | }; |
| 99 | |
| 100 | } // namespace shill |
| 101 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 102 | #endif // SHILL_DNS_CLIENT_H_ |