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

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