Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Unified Diff: net/tools/gdig/gdig.cc

Issue 10386120: Utility to resolve an hostname using Chromium's code in net/dns (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Moved dns_config_service_.release() in a single place Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/tools/gdig/gdig.cc
diff --git a/net/tools/gdig/gdig.cc b/net/tools/gdig/gdig.cc
new file mode 100644
index 0000000000000000000000000000000000000000..596ee4bbb73689f6f9fa2d5fd249366220fb6888
--- /dev/null
+++ b/net/tools/gdig/gdig.cc
@@ -0,0 +1,171 @@
+#include <stdio.h>
+#include <iostream>
+
+#include "base/at_exit.h"
+#include "base/command_line.h"
+#if defined(OS_MACOSX)
+#include "base/mac/scoped_nsautorelease_pool.h"
+#endif
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/string_number_conversions.h"
+
+#include "net/base/host_resolver_impl.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_util.h"
+#include "net/base/sys_addrinfo.h"
+
+#include "net/dns/dns_client.h"
+
+namespace net {
+
+namespace {
+
+class GDig {
+ public:
+ GDig();
+
+ enum Result {
+ RESULT_NO_RESOLVE = -3,
+ RESULT_NO_CONFIG = -2,
+ RESULT_WRONG_USAGE = -1,
+ RESULT_OK = 0,
+ };
+
+ Result Main(int argc, const char* argv[]);
+
+ private:
+ bool ParseCommandLine(int argc, const char* argv[]);
+
+ void Start();
+
+ void OnDnsConfig(const DnsConfig& dns_config);
+ void OnResolveComplete(int val);
+ void OnTimeout();
+
+ Result result_;
+
+ AddressList addrlist_;
+
+ base::CancelableClosure timeout_closure_;
+
+ scoped_ptr<DnsConfigService> dns_config_service_;
+
+ base::TimeDelta timeout_;
+ std::string domain_name_;
+
+ scoped_ptr<HostResolver> resolver_;
+};
+
+GDig::GDig()
+ : result_(GDig::RESULT_OK),
+ timeout_(base::TimeDelta::FromSeconds(5)) {
+}
+
+GDig::Result GDig::Main(int argc, const char* argv[]) {
+ if (!ParseCommandLine(argc, argv)) {
+ std::cout << "usage: " << argv[0] <<
+ " [--config_timeout=<seconds>] domain_name" <<
+ std::endl;
+ return RESULT_WRONG_USAGE;
+ }
+
+#if defined(OS_MACOSX)
+ // Without this there will be a mem leak on osx
+ base::mac::ScopedNSAutoreleasePool scoped_pool;
+#endif
+
+ base::AtExitManager exit_manager;
+ MessageLoop loop(MessageLoop::TYPE_IO);
+
+ Start();
+
+ MessageLoop::current()->Run();
+ 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.
+ return result_;
+}
+
+void GDig::OnResolveComplete(int val) {
+ MessageLoop::current()->Quit();
+ if (val != OK) {
+ std::cout << "Error trying to resolve hostname " << domain_name_ <<
+ ":" << ErrorToString(val) << std::endl;
+ result_ = RESULT_NO_RESOLVE;
+ } else {
+ for (AddressList::iterator i=addrlist_.begin(); i!=addrlist_.end(); ++i) {
+ std::cout << i->ToStringWithoutPort() << std::endl;
+ }
+ }
+}
+
+void GDig::OnTimeout() {
+ MessageLoop::current()->Quit();
+ std::cout << "Timed out waiting to load the dns config" << std::endl;
+ result_ = RESULT_NO_CONFIG;
+}
+
+void GDig::Start() {
+ dns_config_service_ = DnsConfigService::CreateSystemService();
+ dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig,
+ base::Unretained(this)));
+
+ timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this)));
+
+ MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ timeout_closure_.callback(),
+ timeout_);
+}
+
+void GDig::OnDnsConfig(const DnsConfig& dns_config) {
+ timeout_closure_.Cancel();
+ DCHECK(dns_config.IsValid());
+
+ scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
+ dns_client->SetConfig(dns_config);
+ resolver_.reset(
+ new HostResolverImpl(
+ HostCache::CreateDefaultCache(),
+ PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1),
+ HostResolverImpl::ProcTaskParams(NULL, 1),
+ scoped_ptr<DnsConfigService>(NULL),
+ dns_client.Pass(),
+ NULL));
+
+ HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80));
+
+ CompletionCallback callback = base::Bind(&GDig::OnResolveComplete,
+ base::Unretained(this));
+ int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog());
+ DCHECK(ret == ERR_IO_PENDING);
+
+}
+
+bool GDig::ParseCommandLine(int argc, const char* argv[]) {
+ CommandLine::Init(argc, argv);
+ const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
+
+ if (parsed_command_line.GetArgs().size() != 1) {
+ return false;
+ }
+ domain_name_ = parsed_command_line.GetArgs().at(0);
+
+ if (parsed_command_line.HasSwitch("config_timeout")) {
+ int timeout_seconds = 0;
+ base::StringToInt(
+ parsed_command_line.GetSwitchValueASCII("config_timeout"),
+ &timeout_seconds);
+ timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
+ }
+
+ return true;
+}
+
+} // empty namespace
+
+} // namespace net
+
+int main(int argc, const char* argv[]) {
+ net::GDig dig;
+ return dig.Main(argc, argv);
+}
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698