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