| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 245 } |
| 246 | 246 |
| 247 void GDig::Start() { | 247 void GDig::Start() { |
| 248 if (nameserver_.address().size() > 0) { | 248 if (nameserver_.address().size() > 0) { |
| 249 DnsConfig dns_config; | 249 DnsConfig dns_config; |
| 250 dns_config.attempts = 1; | 250 dns_config.attempts = 1; |
| 251 dns_config.nameservers.push_back(nameserver_); | 251 dns_config.nameservers.push_back(nameserver_); |
| 252 OnDnsConfig(dns_config); | 252 OnDnsConfig(dns_config); |
| 253 } else { | 253 } else { |
| 254 dns_config_service_ = DnsConfigService::CreateSystemService(); | 254 dns_config_service_ = DnsConfigService::CreateSystemService(); |
| 255 dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig, | 255 dns_config_service_->ReadConfig(base::Bind(&GDig::OnDnsConfig, |
| 256 base::Unretained(this))); | 256 base::Unretained(this))); |
| 257 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, | 257 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, |
| 258 base::Unretained(this))); | 258 base::Unretained(this))); |
| 259 MessageLoop::current()->PostDelayedTask( | 259 MessageLoop::current()->PostDelayedTask( |
| 260 FROM_HERE, | 260 FROM_HERE, |
| 261 timeout_closure_.callback(), | 261 timeout_closure_.callback(), |
| 262 config_timeout_); | 262 config_timeout_); |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 void GDig::Finish(Result result) { | 266 void GDig::Finish(Result result) { |
| 267 DCHECK_NE(RESULT_PENDING, result); | 267 DCHECK_NE(RESULT_PENDING, result); |
| 268 result_ = result; | 268 result_ = result; |
| 269 if (MessageLoop::current()) | 269 if (MessageLoop::current()) |
| 270 MessageLoop::current()->Quit(); | 270 MessageLoop::current()->Quit(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 void GDig::OnDnsConfig(const DnsConfig& dns_config_const) { | 273 void GDig::OnDnsConfig(const DnsConfig& dns_config_const) { |
| 274 timeout_closure_.Cancel(); | 274 timeout_closure_.Cancel(); |
| 275 DCHECK(dns_config_const.IsValid()); | 275 DCHECK(dns_config_const.IsValid()); |
| 276 DnsConfig dns_config = dns_config_const; | 276 DnsConfig dns_config = dns_config_const; |
| 277 | 277 |
| 278 if (timeout_.InMilliseconds() > 0) | 278 if (timeout_.InMilliseconds() > 0) |
| 279 dns_config.timeout = timeout_; | 279 dns_config.timeout = timeout_; |
| 280 if (print_config_) | 280 if (print_config_) { |
| 281 printf("# Dns Configuration\n" | 281 printf("# Dns Configuration\n" |
| 282 "%s", DnsConfigToString(dns_config).c_str()); | 282 "%s", DnsConfigToString(dns_config).c_str()); |
| 283 if (print_hosts_) | 283 } |
| 284 if (print_hosts_) { |
| 284 printf("# Host Database\n" | 285 printf("# Host Database\n" |
| 285 "%s", DnsHostsToString(dns_config.hosts).c_str()); | 286 "%s", DnsHostsToString(dns_config.hosts).c_str()); |
| 287 } |
| 286 | 288 |
| 287 // If the user didn't specify a name to resolve we can stop here. | 289 // If the user didn't specify a name to resolve we can stop here. |
| 288 if (domain_name_.length() == 0) { | 290 if (domain_name_.length() == 0) { |
| 289 Finish(RESULT_OK); | 291 Finish(RESULT_OK); |
| 290 return; | 292 return; |
| 291 } | 293 } |
| 292 | 294 |
| 293 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); | 295 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); |
| 294 dns_client->SetConfig(dns_config); | 296 dns_client->SetConfig(dns_config); |
| 295 resolver_.reset( | 297 resolver_.reset( |
| 296 new HostResolverImpl( | 298 new HostResolverImpl( |
| 297 HostCache::CreateDefaultCache(), | 299 HostCache::CreateDefaultCache(), |
| 298 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1), | 300 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1), |
| 299 HostResolverImpl::ProcTaskParams(NULL, 1), | 301 HostResolverImpl::ProcTaskParams(NULL, 1), |
| 300 scoped_ptr<DnsConfigService>(NULL), | |
| 301 dns_client.Pass(), | 302 dns_client.Pass(), |
| 302 log_.get())); | 303 log_.get())); |
| 303 | 304 |
| 304 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); | 305 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); |
| 305 | 306 |
| 306 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, | 307 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, |
| 307 base::Unretained(this)); | 308 base::Unretained(this)); |
| 308 int ret = resolver_->Resolve( | 309 int ret = resolver_->Resolve( |
| 309 info, &addrlist_, callback, NULL, | 310 info, &addrlist_, callback, NULL, |
| 310 BoundNetLog::Make(log_.get(), net::NetLog::SOURCE_NONE)); | 311 BoundNetLog::Make(log_.get(), net::NetLog::SOURCE_NONE)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 337 } | 338 } |
| 338 | 339 |
| 339 } // empty namespace | 340 } // empty namespace |
| 340 | 341 |
| 341 } // namespace net | 342 } // namespace net |
| 342 | 343 |
| 343 int main(int argc, const char* argv[]) { | 344 int main(int argc, const char* argv[]) { |
| 344 net::GDig dig; | 345 net::GDig dig; |
| 345 return dig.Main(argc, argv); | 346 return dig.Main(argc, argv); |
| 346 } | 347 } |
| OLD | NEW |