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

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: Include files in alphabetical order 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
« net/net.gyp ('K') | « 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>
Ryan Sleevi 2012/06/06 15:54:58 nit: Line breaks between each of C headers, C++ he
Daniele 2012/06/06 17:43:42 Done.
7 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/cancelable_callback.h"
10 #include "base/command_line.h"
11 #if defined(OS_MACOSX)
12 #include "base/mac/scoped_nsautorelease_pool.h"
13 #endif
Ryan Sleevi 2012/06/06 15:54:58 nit: platform-specific headers appear at the end o
Daniele 2012/06/06 17:43:42 Now cpplint complains that it's not in alphabetic
Ryan Sleevi 2012/06/06 23:20:29 Yes. The rules in cpplint.py currently reflect Goo
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h"
16 #include "base/string_number_conversions.h"
17 #include "base/string_util.h"
18 #include "base/time.h"
19 #include "net/base/address_list.h"
20 #include "net/base/host_cache.h"
21 #include "net/base/host_resolver_impl.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.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 = -3,
37 RESULT_NO_CONFIG = -2,
38 RESULT_WRONG_USAGE = -1,
39 RESULT_OK = 0,
40 };
41
42 Result Main(int argc, const char* argv[]);
43
44 private:
45 bool ParseCommandLine(int argc, const char* argv[]);
46
47 void Start();
48
49 void OnDnsConfig(const DnsConfig& dns_config);
50 void OnResolveComplete(int val);
51 void OnTimeout();
52
53 base::TimeDelta timeout_;
54 std::string domain_name_;
55
56 Result result_;
57 AddressList addrlist_;
58
59 base::CancelableClosure timeout_closure_;
60 scoped_ptr<DnsConfigService> dns_config_service_;
61 scoped_ptr<HostResolver> resolver_;
62 };
63
64 GDig::GDig()
65 : timeout_(base::TimeDelta::FromSeconds(5)),
66 result_(GDig::RESULT_OK) {
67 }
68
69 GDig::Result GDig::Main(int argc, const char* argv[]) {
70 if (!ParseCommandLine(argc, argv)) {
71 fprintf(stderr,
72 "usage: %s [--config_timeout=<seconds>] domain_name\n",
73 argv[0]);
74 return RESULT_WRONG_USAGE;
75 }
76
77 #if defined(OS_MACOSX)
78 // Without this there will be a mem leak on osx.
79 base::mac::ScopedNSAutoreleasePool scoped_pool;
80 #endif
81
82 base::AtExitManager exit_manager;
83 MessageLoopForIO loop;
84
85 Start();
86
87 MessageLoop::current()->Run();
88
89 // Destroy it while MessageLoopForIO is alive.
90 dns_config_service_.reset();
91 return result_;
92 }
93
94 void GDig::OnResolveComplete(int val) {
95 MessageLoop::current()->Quit();
96 if (val != OK) {
97 fprintf(stderr, "Error trying to resolve hostname %s: %s\n",
98 domain_name_.c_str(), ErrorToString(val));
99 result_ = RESULT_NO_RESOLVE;
100 } else {
101 for (size_t i = 0; i < addrlist_.size(); ++i)
102 printf("%s\n", addrlist_[i].ToStringWithoutPort().c_str());
103 }
104 }
105
106 void GDig::OnTimeout() {
107 MessageLoop::current()->Quit();
108 fprintf(stderr, "Timed out waiting to load the dns config\n");
109 result_ = RESULT_NO_CONFIG;
110 }
111
112 void GDig::Start() {
113 dns_config_service_ = DnsConfigService::CreateSystemService();
114 dns_config_service_->Read(base::Bind(&GDig::OnDnsConfig,
115 base::Unretained(this)));
116
117 timeout_closure_.Reset(base::Bind(&GDig::OnTimeout, base::Unretained(this)));
118
119 MessageLoop::current()->PostDelayedTask(
120 FROM_HERE,
121 timeout_closure_.callback(),
122 timeout_);
123 }
124
125 void GDig::OnDnsConfig(const DnsConfig& dns_config) {
126 timeout_closure_.Cancel();
127 DCHECK(dns_config.IsValid());
128
129 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
130 dns_client->SetConfig(dns_config);
131 resolver_.reset(
132 new HostResolverImpl(
133 HostCache::CreateDefaultCache(),
134 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1),
135 HostResolverImpl::ProcTaskParams(NULL, 1),
136 scoped_ptr<DnsConfigService>(NULL),
137 dns_client.Pass(),
138 NULL));
139
140 HostResolver::RequestInfo info(HostPortPair(domain_name_.c_str(), 80));
141
142 CompletionCallback callback = base::Bind(&GDig::OnResolveComplete,
143 base::Unretained(this));
144 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL, BoundNetLog());
145 DCHECK(ret == ERR_IO_PENDING);
146 }
147
148 bool GDig::ParseCommandLine(int argc, const char* argv[]) {
149 CommandLine::Init(argc, argv);
150 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
151
152 if (parsed_command_line.GetArgs().size() != 1)
153 return false;
154
155 #if defined(OS_WIN)
156 domain_name_ = WideToASCII(parsed_command_line.GetArgs()[0]);
157 #else
158 domain_name_ = parsed_command_line.GetArgs()[0];
159 #endif
160
161 if (parsed_command_line.HasSwitch("config_timeout")) {
162 int timeout_seconds = 0;
163 base::StringToInt(
164 parsed_command_line.GetSwitchValueASCII("config_timeout"),
165 &timeout_seconds);
Ryan Sleevi 2012/06/06 15:54:58 nit: This returns a bool - check the result? Also
Daniele 2012/06/06 17:43:42 Done.
166 timeout_ = base::TimeDelta::FromSeconds(timeout_seconds);
167 }
168
169 return true;
170 }
171
172 } // empty namespace
173
174 } // namespace net
175
176 int main(int argc, const char* argv[]) {
177 net::GDig dig;
178 return dig.Main(argc, argv);
179 }
OLDNEW
« net/net.gyp ('K') | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698