blob: 6397570bd329df97656233c8ca6fcd92eaa05fe7 [file] [log] [blame]
Paul Stewartc2350ee2011-10-19 12:28:40 -07001// 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 Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_DNS_CLIENT_H_
6#define SHILL_DNS_CLIENT_H_
Paul Stewartc2350ee2011-10-19 12:28:40 -07007
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Paul Stewartc2350ee2011-10-19 12:28:40 -07009#include <string>
10#include <vector>
11
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/callback.h>
Paul Stewartf582b502012-04-04 21:39:22 -070013#include <base/cancelable_callback.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050014#include <base/memory/weak_ptr.h>
Paul Stewartc2350ee2011-10-19 12:28:40 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
16
Paul Stewartbdb02e62012-02-22 16:24:33 -080017#include "shill/error.h"
Paul Stewartc2350ee2011-10-19 12:28:40 -070018#include "shill/event_dispatcher.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070019#include "shill/net/ip_address.h"
Paul Stewartc2350ee2011-10-19 12:28:40 -070020#include "shill/refptr_types.h"
21
22struct hostent;
23
24namespace shill {
25
26class Ares;
27class Time;
28struct DNSClientState;
29
Paul Stewartbdb02e62012-02-22 16:24:33 -080030// Implements a DNS resolution client that can run asynchronously.
Paul Stewartc2350ee2011-10-19 12:28:40 -070031class DNSClient {
32 public:
Paul Stewarta794cd62015-06-16 13:13:10 -070033 typedef base::Callback<void(const Error&, const IPAddress&)> ClientCallback;
Paul Stewartbdb02e62012-02-22 16:24:33 -080034
Paul Stewartc2350ee2011-10-19 12:28:40 -070035 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 Stewarta794cd62015-06-16 13:13:10 -070047 const std::string& interface_name,
48 const std::vector<std::string>& dns_servers,
Paul Stewartc2350ee2011-10-19 12:28:40 -070049 int timeout_ms,
Paul Stewarta794cd62015-06-16 13:13:10 -070050 EventDispatcher* dispatcher,
51 const ClientCallback& callback);
Paul Stewartf65320c2011-10-13 14:34:52 -070052 virtual ~DNSClient();
Paul Stewartc2350ee2011-10-19 12:28:40 -070053
Paul Stewartbdb02e62012-02-22 16:24:33 -080054 // 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 Stewarta794cd62015-06-16 13:13:10 -070059 virtual bool Start(const std::string& hostname, Error* error);
Paul Stewartbdb02e62012-02-22 16:24:33 -080060
61 // Aborts any running DNS client transaction. This will cancel any callback
62 // invocation.
Paul Stewartf65320c2011-10-13 14:34:52 -070063 virtual void Stop();
Paul Stewartc2350ee2011-10-19 12:28:40 -070064
Prathmesh Prabhu3e452f82013-04-10 16:31:44 -070065 virtual bool IsActive() const;
66
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070067 std::string interface_name() { return interface_name_; }
68
Paul Stewartc2350ee2011-10-19 12:28:40 -070069 private:
70 friend class DNSClientTest;
71
Paul Stewartbdb02e62012-02-22 16:24:33 -080072 void HandleCompletion();
Paul Stewartc2350ee2011-10-19 12:28:40 -070073 void HandleDNSRead(int fd);
74 void HandleDNSWrite(int fd);
75 void HandleTimeout();
Paul Stewarta794cd62015-06-16 13:13:10 -070076 void ReceiveDNSReply(int status, struct hostent* hostent);
77 static void ReceiveDNSReplyCB(void* arg, int status, int timeouts,
78 struct hostent* hostent);
Paul Stewartc2350ee2011-10-19 12:28:40 -070079 bool RefreshHandles();
80
Peter Qiuf3a8f902014-08-20 10:05:42 -070081 static const int kDefaultDNSPort;
82
Paul Stewartbdb02e62012-02-22 16:24:33 -080083 Error error_;
Paul Stewartc2350ee2011-10-19 12:28:40 -070084 IPAddress address_;
85 std::string interface_name_;
86 std::vector<std::string> dns_servers_;
Paul Stewarta794cd62015-06-16 13:13:10 -070087 EventDispatcher* dispatcher_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050088 ClientCallback callback_;
Paul Stewartc2350ee2011-10-19 12:28:40 -070089 int timeout_ms_;
90 bool running_;
Ben Chancd477322014-10-17 14:19:30 -070091 std::unique_ptr<DNSClientState> resolver_state_;
Paul Stewartf582b502012-04-04 21:39:22 -070092 base::CancelableClosure timeout_closure_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050093 base::WeakPtrFactory<DNSClient> weak_ptr_factory_;
Paul Stewarta794cd62015-06-16 13:13:10 -070094 Ares* ares_;
95 Time* time_;
Paul Stewartc2350ee2011-10-19 12:28:40 -070096
97 DISALLOW_COPY_AND_ASSIGN(DNSClient);
98};
99
100} // namespace shill
101
Ben Chanc45688b2014-07-02 23:50:45 -0700102#endif // SHILL_DNS_CLIENT_H_