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

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: 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 unified diff | Download patch
« net/base/host_resolver_impl.cc ('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 #include <stdio.h>
2 #include <iostream>
3
4 #include "base/memory/scoped_ptr.h"
5 #include "base/at_exit.h"
6 #include "base/message_loop.h"
7 #include "base/command_line.h"
8 #include "base/string_number_conversions.h"
9
10 #include "net/base/host_resolver_impl.h"
11 #include "net/base/net_util.h"
12 #include "net/base/sys_addrinfo.h"
13
14 #include "net/base/net_errors.h"
szym 2012/05/14 18:48:32 The lint tool will automatically point most style
15
16 #if defined(OS_MACOSX)
17 #include "base/mac/scoped_nsautorelease_pool.h"
18 #endif
19
20 namespace net {
21
22 namespace {
23
24 void usage(const char* program_name) {
szym 2012/05/14 18:48:32 Suggested function name: PrintUsage, probably shou
Daniele 2012/05/17 23:04:34 Ok, even if I don't really see the necessity to ma
25 std::cout << "usage: " << program_name <<
26 " [--max_retry=<retry>] [--concurrent_resolves=<n>] domain_name" <<
27 std::endl;
28 }
29
30 // This class is used to mock the DnsConfigService when an HostResolverImpl
31 // is created and to set to it a custom dns_config.
32 class DnsConfigServiceMock : public DnsConfigService {
33 public:
34 DnsConfigServiceMock(const DnsConfig& dns_config) :
35 dns_config_(dns_config) {};
36
37 virtual ~DnsConfigServiceMock() {};
38
39 virtual void Watch(const CallbackType& callback) OVERRIDE{
40 DCHECK(CalledOnValidThread());
41 DCHECK(!callback.is_null());
42 set_callback(callback);
43 callback.Run(dns_config_);
szym 2012/05/14 18:48:32 This is strongly discouraged. Rule-of-thumb: avoid
Daniele 2012/05/17 23:04:34 Quite clear, it solved other problems I had to wor
44 }
45 private:
46 const DnsConfig& dns_config_;
47 };
48
49 class GDig {
50
51 public:
52 GDig() :
53 resolve_name_ret_(ERR_TIMED_OUT),
54 max_retry_(1),
55 concurrent_resolves_(1){ }
56
57 int Main(int argc, const char* argv[]) {
58
59 if( !ParseCommandLine(argc, argv) ){
60 usage(argv[0]);
61 return -1;
szym 2012/05/14 18:48:32 I suggest defining in GDig: enum { RESULT_NO_RES
Daniele 2012/05/17 23:04:34 Done, but the name you used seems ok to me, so I k
62 }
63
64 #if defined(OS_MACOSX)
65 //without this there will be a mem leak on osx
66 base::mac::ScopedNSAutoreleasePool scoped_pool;
67 #endif
68
69 base::AtExitManager exit_manager;
70 MessageLoop loop(MessageLoop::TYPE_IO);
71
72 LoadDnsConfig();
szym 2012/05/14 18:48:32 I suggest: void Start() { resolver_ = new HostR
Daniele 2012/05/17 23:04:34 That's definitely nicer than what I did. On 2012/
73
74 MessageLoop::current()->Run();
75
76 if (!dns_config_.IsValid()){
77 std::cout << "Dns configuration is not valid." << std::endl;
78 return -2;
79 }
80 if (resolve_name_ret_ < 0){
81 std::cout << "Error trying to resolve hostname " << domain_name_ <<
82 ":" << ErrorToString(resolve_name_ret_) << std::endl;
83 return -3;
84 }
85
86 return 0;
87 }
88
89 private:
90 void ResolverCallback(int val){
szym 2012/05/14 18:48:32 better name: OnResolveComplete(int rv)
Daniele 2012/05/17 23:04:34 Done.
91 timeout_.Cancel();
92 resolve_name_ret_ = val;
93
94 if (resolve_name_ret_ < 0 ){
95 MessageLoop::current()->Quit();
96 }
97
98 const struct addrinfo* addrinf = addrlist_.head();
99 while (addrinf){
100 std::string ip = NetAddressToStringWithPort(addrinf);
101 std::cout << ip << std::endl;
102 addrinf = addrinf->ai_next;
103 }
104
105 MessageLoop::current()->Quit();
106 }
107
108 void ReleaseDnsConfigService(){
109 config_service_.reset();
110 }
111
112 void ResolveName(const std::string& domain_name){
113
114 scoped_ptr<DnsConfigService>
115 dns_config_service(new DnsConfigServiceMock(dns_config_));
116
117 resolver_.reset(CreateHostResolver(concurrent_resolves_,
118 max_retry_,
119 HostCache::CreateDefaultCache(),
120 dns_config_service.Pass(),
121 NULL));
122
123 HostResolver::RequestInfo info(HostPortPair(domain_name.c_str(), 80));
124
125 CompletionCallback callback = base::Bind(&GDig::ResolverCallback,
126 base::Unretained(this));
127 int ret = resolver_->Resolve(info, &addrlist_, callback, NULL,
128 BoundNetLog());
129 DCHECK(ret == ERR_IO_PENDING);
130
131 timeout_.Reset(MessageLoop::QuitClosure());
132 MessageLoop::current()->PostDelayedTask(FROM_HERE,
133 timeout_.callback(),
134 base::TimeDelta::FromSeconds(5));
szym 2012/05/14 18:48:32 Ideally, make timeout value a command-line argumen
Daniele 2012/05/17 23:04:34 Done.
135 }
136
137 void OnDnsConfigChanged(const DnsConfig& dns_config) {
138 timeout_.Cancel();
139 dns_config_ = dns_config;
140 MessageLoop::current()->PostTask(FROM_HERE,
szym 2012/05/14 18:48:32 No need to post it on MessageLoop. Just let go of
Daniele 2012/05/17 23:04:34 Releasing the pointer here, caused a DCHECK to com
141 base::Bind(&GDig::ReleaseDnsConfigService,
142 base::Unretained(this)));
143
144 ResolveName(domain_name_);
145 }
146
147 bool ParseCommandLine(int argc, const char* argv[]){
148 CommandLine::Init(argc, argv);
149 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
150
151 if (parsed_command_line.GetArgs().size() != 1){
152 return false;
153 }
154 domain_name_ = parsed_command_line.GetArgs().at(0);
155
156 if (parsed_command_line.HasSwitch("max_retry")) {
szym 2012/05/14 18:48:32 These values are not going to be used with DnsConf
Daniele 2012/05/17 23:04:34 Done.
157 base::StringToInt(parsed_command_line.GetSwitchValueASCII("max_retry"),
158 &max_retry_);
159 }
160
161 if (parsed_command_line.HasSwitch("concurrent_resolves")) {
162 base::StringToInt(parsed_command_line.
163 GetSwitchValueASCII("concurrent_resolves"),
164 &concurrent_resolves_);
165 }
166
167 return true;
168 }
169
170 void LoadDnsConfig(){
171
172 timeout_.Reset(MessageLoop::QuitClosure());
173
174 // Is there a better way of doing this?
175 config_service_.reset(DnsConfigService::CreateSystemService().release());
szym 2012/05/14 18:48:32 scoped_ptr::operator= has the semantics you want:
176 config_service_->Watch(base::Bind(&GDig::OnDnsConfigChanged,
177 base::Unretained(this)));
178
179 MessageLoop::current()->PostDelayedTask(FROM_HERE,
180 timeout_.callback(),
181 base::TimeDelta::FromSeconds(5));
182
183 }
184
185 int resolve_name_ret_;
186 AddressList addrlist_;
187
188 base::CancelableClosure timeout_;
189 DnsConfig dns_config_;
190
191 int max_retry_;
192 int concurrent_resolves_;
193
194 std::string domain_name_;
195
196 scoped_ptr<DnsConfigService> config_service_;
197 scoped_ptr<HostResolver> resolver_;
198
199 };
200
201 } // empty namespace
202
203 } // namespace net
204
205 int main(int argc, const char* argv[]) {
206 net::GDig dig;
207 return dig.Main(argc, argv);
208 }
OLDNEW
« net/base/host_resolver_impl.cc ('K') | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698