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

Side by Side 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: Using LOG instead of std::cout, fix includes and a few stylistic nits. Created 8 years, 6 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 unified diff | Download patch
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/cancelable_callback.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #if defined(OS_MACOSX)
13 #include "base/mac/scoped_nsautorelease_pool.h"
14 #endif
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop.h"
17 #include "base/string_number_conversions.h"
18 #include "base/time.h"
19 #include "net/base/host_cache.h"
20 #include "net/base/host_resolver_impl.h"
21 #include "net/base/net_errors.h"
22 #include "net/base/net_util.h"
23 #include "net/base/address_list.h"
24 #include "net/dns/dns_client.h"
25 #include "net/dns/dns_config_service.h"
26
27 namespace net {
28
29 namespace {
30
31 class GDig {
32 public:
33 GDig();
34
35 enum Result {
36 RESULT_NO_RESOLVE = -4,
37 RESULT_NO_CONFIG = -3,
38 RESULT_WRONG_USAGE = -2,
39 RESULT_CANNOT_INITIALIZE_LOGGING = -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 CommandLine::Init(argc, argv);
72 if (!logging::InitLogging(
73 NULL,
74 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
75 logging::DONT_LOCK_LOG_FILE,
76 logging::DELETE_OLD_LOG_FILE,
77 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS)) {
78 printf("Error: could not initialize logging. Exiting.\n");
79 return RESULT_CANNOT_INITIALIZE_LOGGING;
80 }
81 if (!ParseCommandLine(argc, argv)) {
82 LOG(INFO) << "usage: " << argv[0] <<
83 " [--config_timeout=<seconds>] domain_name";
84 return RESULT_WRONG_USAGE;
85 }
86
87 #if defined(OS_MACOSX)
88 // Without this there will be a mem leak on osx.
89 base::mac::ScopedNSAutoreleasePool scoped_pool;
90 #endif
91
92 base::AtExitManager exit_manager;
93 MessageLoopForIO loop;
94
95 Start();
96
97 MessageLoop::current()->Run();
98
99 // Destroy it while MessageLoopForIO is alive.
100 dns_config_service_.reset();
101 return result_;
102 }
103
104 void GDig::OnResolveComplete(int val) {
105 MessageLoop::current()->Quit();
106 if (val != OK) {
107 LOG(ERROR) << "Error trying to resolve hostname " << domain_name_ <<
108 ":" << ErrorToString(val);
109 result_ = RESULT_NO_RESOLVE;
110 } else {
111 for (AddressList::iterator i = addrlist_.begin(); i != addrlist_.end(); ++i)
112 printf("%s\n", i->ToStringWithoutPort().c_str());
113 }
114 }
115
116 void GDig::OnTimeout() {
117 MessageLoop::current()->Quit();
118 LOG(ERROR) << "Timed out waiting to load the dns config";
119 result_ = RESULT_NO_CONFIG;
120 }
121
122 void GDig::Start() {
123 dns_config_service_ = DnsConfigService::CreateSystemService();
124 dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig,
125 base::Unretained(this)));
126
127 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this)));
128
129 MessageLoop::current()->PostDelayedTask(
130 FROM_HERE,
131 timeout_closure_.callback(),
132 timeout_);
133 }
134
135 void GDig::OnDnsConfig(const DnsConfig& dns_config) {
136 timeout_closure_.Cancel();
137 DCHECK(dns_config.IsValid());
138
139 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
140 dns_client->SetConfig(dns_config);
141 resolver_.reset(
142 new HostResolverImpl(
143 HostCache::CreateDefaultCache(),
144 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1),
145 HostResolverImpl::ProcTaskParams(NULL, 1),
146 scoped_ptr<DnsConfigService>(NULL),
147 dns_client.Pass(),
148 NULL));
149
150 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80));
151
152 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete,
153 base::Unretained(this));
154 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog());
155 DCHECK(ret == ERR_IO_PENDING);
156 }
157
158 bool GDig::ParseCommandLine(int argc, const char* argv[]) {
159 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
160
161 if (parsed_command_line.GetArgs().size() != 1)
162 return false;
163 domain_name_ = parsed_command_line.GetArgs().at(0);
szym 2012/06/04 22:03:56 Use operator[] rather than .at() for consistency w
164
165 if (parsed_command_line.HasSwitch("config_timeout")) {
166 int timeout_seconds = 0;
167 base::StringToInt(
168 parsed_command_line.GetSwitchValueASCII("config_timeout"),
169 &timeout_seconds);
170 timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
171 }
172
173 return true;
174 }
175
176 } // empty namespace
177
178 } // namespace net
179
180 int main(int argc, const char* argv[]) {
181 net::GDig dig;
182 return dig.Main(argc, argv);
183 }
OLDNEW
« 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