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 | |
| 11 | #include <base/callback_old.h> |
| 12 | #include <base/memory/scoped_ptr.h> |
| 13 | #include <base/task.h> |
| 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 15 | |
| 16 | #include "shill/event_dispatcher.h" |
| 17 | #include "shill/ip_address.h" |
| 18 | #include "shill/refptr_types.h" |
| 19 | |
| 20 | struct hostent; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | class Ares; |
| 25 | class Time; |
| 26 | struct DNSClientState; |
| 27 | |
| 28 | // Implements a DNS resolution client that can run asynchronously |
| 29 | class DNSClient { |
| 30 | public: |
| 31 | static const int kDefaultTimeoutMS; |
| 32 | static const char kErrorNoData[]; |
| 33 | static const char kErrorFormErr[]; |
| 34 | static const char kErrorServerFail[]; |
| 35 | static const char kErrorNotFound[]; |
| 36 | static const char kErrorNotImp[]; |
| 37 | static const char kErrorRefused[]; |
| 38 | static const char kErrorBadQuery[]; |
| 39 | static const char kErrorNetRefused[]; |
| 40 | static const char kErrorTimedOut[]; |
| 41 | static const char kErrorUnknown[]; |
| 42 | |
| 43 | DNSClient(IPAddress::Family family, |
| 44 | const std::string &interface_name, |
| 45 | const std::vector<std::string> &dns_servers, |
| 46 | int timeout_ms, |
| 47 | EventDispatcher *dispatcher, |
| 48 | Callback1<bool>::Type *callback); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 49 | virtual ~DNSClient(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 50 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 51 | virtual bool Start(const std::string &hostname); |
| 52 | virtual void Stop(); |
| 53 | virtual const IPAddress &address() const { return address_; } |
| 54 | virtual const std::string &error() const { return error_; } |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | friend class DNSClientTest; |
| 58 | |
| 59 | void HandleDNSRead(int fd); |
| 60 | void HandleDNSWrite(int fd); |
| 61 | void HandleTimeout(); |
| 62 | void ReceiveDNSReply(int status, struct hostent *hostent); |
| 63 | static void ReceiveDNSReplyCB(void *arg, int status, int timeouts, |
| 64 | struct hostent *hostent); |
| 65 | bool RefreshHandles(); |
| 66 | |
| 67 | IPAddress address_; |
| 68 | std::string interface_name_; |
| 69 | std::vector<std::string> dns_servers_; |
| 70 | EventDispatcher *dispatcher_; |
| 71 | Callback1<bool>::Type *callback_; |
| 72 | int timeout_ms_; |
| 73 | bool running_; |
| 74 | std::string error_; |
| 75 | scoped_ptr<DNSClientState> resolver_state_; |
| 76 | scoped_ptr<Callback1<int>::Type> read_callback_; |
| 77 | scoped_ptr<Callback1<int>::Type> write_callback_; |
| 78 | ScopedRunnableMethodFactory<DNSClient> task_factory_; |
| 79 | Ares *ares_; |
| 80 | Time *time_; |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(DNSClient); |
| 83 | }; |
| 84 | |
| 85 | } // namespace shill |
| 86 | |
| 87 | #endif // SHILL_DNS_CLIENT_ |