Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <stdio.h> | |
|
szym
2012/06/03 04:49:20
Add the licence blurb.
Daniele
2012/06/04 20:01:14
Done.
| |
| 2 #include <iostream> | |
|
szym
2012/06/03 04:49:20
Don't use iostream. Use base/logging.h and LOG(INF
szym
2012/06/03 20:00:05
Also, need to include <string>
Daniele
2012/06/04 20:01:14
Done.
| |
| 3 | |
| 4 #include "base/at_exit.h" | |
|
szym
2012/06/03 20:00:05
Include "base/bind.h", "base/cancelable_callback.h
Daniele
2012/06/04 20:01:14
Done.
| |
| 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 | |
|
szym
2012/06/03 04:49:20
Unneeded blank line.
| |
| 13 #include "net/base/host_resolver_impl.h" | |
|
szym
2012/06/03 20:00:05
Include "net/base/host_cache.h".
Daniele
2012/06/04 20:01:14
Done.
| |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/base/net_util.h" | |
| 16 #include "net/base/sys_addrinfo.h" | |
|
szym
2012/06/03 20:00:05
Include "net/base/address_list.h" instead.
Daniele
2012/06/04 20:01:14
Done.
| |
| 17 | |
|
szym
2012/06/03 04:49:20
Ditto.
Daniele
2012/06/04 20:01:14
Done.
| |
| 18 #include "net/dns/dns_client.h" | |
|
szym
2012/06/03 20:00:05
Include "net/dns/dns_config_service.h".
Daniele
2012/06/04 20:01:14
Done.
| |
| 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 base::TimeDelta timeout_; | |
| 47 std::string domain_name_; | |
| 48 | |
| 49 Result result_; | |
| 50 AddressList addrlist_; | |
| 51 | |
| 52 base::CancelableClosure timeout_closure_; | |
| 53 scoped_ptr<DnsConfigService> dns_config_service_; | |
| 54 scoped_ptr<HostResolver> resolver_; | |
| 55 }; | |
| 56 | |
| 57 GDig::GDig() | |
| 58 : timeout_(base::TimeDelta::FromSeconds(5)), | |
| 59 result_(GDig::RESULT_OK) { | |
| 60 } | |
| 61 | |
| 62 GDig::Result GDig::Main(int argc, const char* argv[]) { | |
| 63 if (!ParseCommandLine(argc, argv)) { | |
| 64 std::cout << "usage: " << argv[0] << | |
| 65 " [--config_timeout=<seconds>] domain_name" << | |
| 66 std::endl; | |
| 67 return RESULT_WRONG_USAGE; | |
| 68 } | |
| 69 | |
| 70 #if defined(OS_MACOSX) | |
| 71 // Without this there will be a mem leak on osx | |
|
szym
2012/06/03 20:00:05
nit: Add "." at end of sentence.
Daniele
2012/06/04 20:01:14
Done.
| |
| 72 base::mac::ScopedNSAutoreleasePool scoped_pool; | |
| 73 #endif | |
| 74 | |
| 75 base::AtExitManager exit_manager; | |
| 76 MessageLoopForIO loop; | |
| 77 | |
| 78 Start(); | |
| 79 | |
| 80 MessageLoop::current()->Run(); | |
| 81 | |
| 82 // Destroy it while MessageLoopForIO is alive | |
|
szym
2012/06/03 20:00:05
Ditto.
Daniele
2012/06/04 20:01:14
Done.
| |
| 83 dns_config_service_.reset(); | |
| 84 return result_; | |
| 85 } | |
| 86 | |
| 87 void GDig::OnResolveComplete(int val) { | |
| 88 MessageLoop::current()->Quit(); | |
| 89 if (val != OK) { | |
| 90 std::cout << "Error trying to resolve hostname " << domain_name_ << | |
| 91 ":" << ErrorToString(val) << std::endl; | |
| 92 result_ = RESULT_NO_RESOLVE; | |
| 93 } else { | |
| 94 for (AddressList::iterator i=addrlist_.begin(); i!=addrlist_.end(); ++i) { | |
|
szym
2012/06/03 20:00:05
No strong opinion about it, but I think preference
Daniele
2012/06/04 20:01:14
std::vector<IPEndPoint>::at is not exposed in Addr
szym
2012/06/04 20:44:46
operator[] is exposed.
| |
| 95 std::cout << i->ToStringWithoutPort() << std::endl; | |
|
szym
2012/06/03 04:49:20
nit: too much indent
Daniele
2012/06/04 20:01:14
Done.
| |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void GDig::OnTimeout() { | |
| 101 MessageLoop::current()->Quit(); | |
| 102 std::cout << "Timed out waiting to load the dns config" << std::endl; | |
| 103 result_ = RESULT_NO_CONFIG; | |
| 104 } | |
| 105 | |
| 106 void GDig::Start() { | |
| 107 dns_config_service_ = DnsConfigService::CreateSystemService(); | |
| 108 dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig, | |
| 109 base::Unretained(this))); | |
| 110 | |
| 111 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this))); | |
| 112 | |
| 113 MessageLoop::current()->PostDelayedTask( | |
| 114 FROM_HERE, | |
| 115 timeout_closure_.callback(), | |
| 116 timeout_); | |
| 117 } | |
| 118 | |
| 119 void GDig::OnDnsConfig(const DnsConfig& dns_config) { | |
| 120 timeout_closure_.Cancel(); | |
| 121 DCHECK(dns_config.IsValid()); | |
| 122 | |
| 123 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); | |
| 124 dns_client->SetConfig(dns_config); | |
| 125 resolver_.reset( | |
| 126 new HostResolverImpl( | |
| 127 HostCache::CreateDefaultCache(), | |
| 128 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1), | |
| 129 HostResolverImpl::ProcTaskParams(NULL, 1), | |
| 130 scoped_ptr<DnsConfigService>(NULL), | |
| 131 dns_client.Pass(), | |
| 132 NULL)); | |
| 133 | |
| 134 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80)); | |
| 135 | |
| 136 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete, | |
| 137 base::Unretained(this)); | |
| 138 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog()); | |
| 139 DCHECK(ret == ERR_IO_PENDING); | |
| 140 | |
|
szym
2012/06/03 20:00:05
nit: Unneeded blank line.
Daniele
2012/06/04 20:01:14
Done.
| |
| 141 } | |
| 142 | |
| 143 bool GDig::ParseCommandLine(int argc, const char* argv[]) { | |
| 144 CommandLine::Init(argc, argv); | |
| 145 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
| 146 | |
| 147 if (parsed_command_line.GetArgs().size() != 1) { | |
|
szym
2012/06/03 20:00:05
No need for braces around single line body.
Daniele
2012/06/04 20:01:14
Done.
| |
| 148 return false; | |
|
szym
2012/06/03 04:49:20
nit: too much indent
Daniele
2012/06/04 20:01:14
Done.
| |
| 149 } | |
| 150 domain_name_ = parsed_command_line.GetArgs().at(0); | |
| 151 | |
| 152 if (parsed_command_line.HasSwitch("config_timeout")) { | |
| 153 int timeout_seconds = 0; | |
|
szym
2012/06/03 04:49:20
Ditto.
Daniele
2012/06/04 20:01:14
Done.
| |
| 154 base::StringToInt( | |
| 155 parsed_command_line.GetSwitchValueASCII("config_timeout"), | |
| 156 &timeout_seconds); | |
| 157 timeout_ = base::TimeDelta::FromSeconds(timeout_seconds); | |
| 158 } | |
| 159 | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 } // empty namespace | |
| 164 | |
| 165 } // namespace net | |
| 166 | |
| 167 int main(int argc, const char* argv[]) { | |
| 168 net::GDig dig; | |
| 169 return dig.Main(argc, argv); | |
| 170 } | |
| OLD | NEW |