Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <stdio.h> | |
| 2 #include <iostream> | |
| 3 | |
| 4 #include "base/at_exit.h" | |
| 5 #include "base/command_line.h" | |
| 6 #if defined(OS_MACOSX) | |
| 7 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 8 #endif | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/string_number_conversions.h" | |
| 12 | |
| 13 #include "net/base/host_resolver_impl.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/base/net_util.h" | |
| 16 #include "net/base/sys_addrinfo.h" | |
| 17 | |
| 18 #include "net/dns/dns_client.h" | |
| 19 | |
| 20 namespace net { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class GDig { | |
| 25 public: | |
| 26 GDig(); | |
| 27 | |
| 28 enum Result { | |
| 29 RESULT_NO_RESOLVE = -3, | |
| 30 RESULT_NO_CONFIG = -2, | |
| 31 RESULT_WRONG_USAGE = -1, | |
| 32 RESULT_OK = 0, | |
| 33 }; | |
| 34 | |
| 35 Result Main(int argc, const char* argv[]); | |
| 36 | |
| 37 private: | |
| 38 bool ParseCommandLine(int argc, const char* argv[]); | |
| 39 | |
| 40 void Start(); | |
| 41 | |
| 42 void OnDnsConfig(const DnsConfig& dns_config); | |
| 43 void OnResolveComplete(int val); | |
| 44 void OnTimeout(); | |
| 45 | |
| 46 Result result_; | |
| 47 | |
| 48 AddressList addrlist_; | |
| 49 | |
| 50 base::CancelableClosure timeout_closure_; | |
| 51 | |
| 52 scoped_ptr<DnsConfigService> dns_config_service_; | |
| 53 | |
| 54 base::TimeDelta timeout_; | |
| 55 std::string domain_name_; | |
| 56 | |
| 57 scoped_ptr<HostResolver> resolver_; | |
| 58 }; | |
| 59 | |
| 60 GDig::GDig() | |
| 61 : result_(GDig::RESULT_OK), | |
| 62 timeout_(base::TimeDelta::FromSeconds(5)) { | |
| 63 } | |
| 64 | |
| 65 GDig::Result GDig::Main(int argc, const char* argv[]) { | |
| 66 if (!ParseCommandLine(argc, argv)) { | |
| 67 std::cout << "usage: " << argv[0] << | |
| 68 " [--config_timeout=<seconds>] domain_name" << | |
| 69 std::endl; | |
| 70 return RESULT_WRONG_USAGE; | |
| 71 } | |
| 72 | |
| 73 #if defined(OS_MACOSX) | |
| 74 // Without this there will be a mem leak on osx | |
| 75 base::mac::ScopedNSAutoreleasePool scoped_pool; | |
| 76 #endif | |
| 77 | |
| 78 base::AtExitManager exit_manager; | |
| 79 MessageLoop loop(MessageLoop::TYPE_IO); | |
| 80 | |
| 81 Start(); | |
| 82 | |
| 83 MessageLoop::current()->Run(); | |
| 84 dns_config_service_.reset(); | |
|
szym
2012/06/01 22:10:23
Maybe add a comment: "Destroy it while MessageLoop
Daniele
2012/06/01 22:29:27
Done.
| |
| 85 return result_; | |
| 86 } | |
| 87 | |
| 88 void GDig::OnResolveComplete(int val) { | |
| 89 MessageLoop::current()->Quit(); | |
| 90 if (val != OK) { | |
| 91 std::cout << "Error trying to resolve hostname " << domain_name_ << | |
| 92 ":" << ErrorToString(val) << std::endl; | |
| 93 result_ = RESULT_NO_RESOLVE; | |
| 94 } else { | |
| 95 for (AddressList::iterator i=addrlist_.begin(); i!=addrlist_.end(); ++i) { | |
| 96 std::cout << i->ToStringWithoutPort() << std::endl; | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 void GDig::OnTimeout() { | |
| 102 MessageLoop::current()->Quit(); | |
| 103 std::cout << "Timed out waiting to load the dns config" << std::endl; | |
| 104 result_ = RESULT_NO_CONFIG; | |
| 105 } | |
| 106 | |
| 107 void GDig::Start() { | |
| 108 dns_config_service_ = DnsConfigService::CreateSystemService(); | |
| 109 dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig, | |
| 110 base::Unretained(this))); | |
| 111 | |
| 112 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this))); | |
| 113 | |
| 114 MessageLoop::current()->PostDelayedTask( | |
| 115 FROM_HERE, | |
| 116 timeout_closure_.callback(), | |
| 117 timeout_); | |
| 118 } | |
| 119 | |
| 120 void GDig::OnDnsConfig(const DnsConfig& dns_config) { | |
| 121 timeout_closure_.Cancel(); | |
| 122 DCHECK(dns_config.IsValid()); | |
| 123 | |
| 124 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); | |
| 125 dns_client->SetConfig(dns_config); | |
| 126 resolver_.reset( | |
| 127 new HostResolverImpl( | |
| 128 HostCache::CreateDefaultCache(), | |
| 129 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1), | |
| 130 HostResolverImpl::ProcTaskParams(NULL, 1), | |
| 131 scoped_ptr<DnsConfigService>(NULL), | |
| 132 dns_client.Pass(), | |
| 133 NULL)); | |
| 134 | |
| 135 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); | |
| 136 | |
| 137 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, | |
| 138 base::Unretained(this)); | |
| 139 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog()); | |
| 140 DCHECK(ret == ERR_IO_PENDING); | |
| 141 | |
| 142 } | |
| 143 | |
| 144 bool GDig::ParseCommandLine(int argc, const char* argv[]) { | |
| 145 CommandLine::Init(argc, argv); | |
| 146 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
| 147 | |
| 148 if (parsed_command_line.GetArgs().size() != 1) { | |
| 149 return false; | |
| 150 } | |
| 151 domain_name_ = parsed_command_line.GetArgs().at(0); | |
| 152 | |
| 153 if (parsed_command_line.HasSwitch("config_timeout")) { | |
| 154 int timeout_seconds = 0; | |
| 155 base::StringToInt( | |
| 156 parsed_command_line.GetSwitchValueASCII("config_timeout"), | |
| 157 &timeout_seconds); | |
| 158 timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); | |
| 159 } | |
| 160 | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 } // empty namespace | |
| 165 | |
| 166 } // namespace net | |
| 167 | |
| 168 int main(int argc, const char* argv[]) { | |
| 169 net::GDig dig; | |
| 170 return dig.Main(argc, argv); | |
| 171 } | |
| OLD | NEW |