diff options
author | rch <rch@chromium.org> | 2015-03-21 00:42:07 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-21 07:42:32 +0000 |
commit | acfb2f91085d5291ba1c9a04dbe7aea610b13e33 (patch) | |
tree | bff79371f92c613fb569ea36711c95f38d4c171e /net/tools | |
parent | 89d44c0cfedf931e82184a12241e4ad5aa8972fc (diff) | |
download | chromium_src-acfb2f91085d5291ba1c9a04dbe7aea610b13e33.zip chromium_src-acfb2f91085d5291ba1c9a04dbe7aea610b13e33.tar.gz chromium_src-acfb2f91085d5291ba1c9a04dbe7aea610b13e33.tar.bz2 |
Make the simple QUIC client able to resolve hosts.
Implement a SynchronousHostResolver for this purpose.
Review URL: https://codereview.chromium.org/1004173004
Cr-Commit-Position: refs/heads/master@{#321688}
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/quic/quic_client_bin.cc | 17 | ||||
-rw-r--r-- | net/tools/quic/quic_simple_client_bin.cc | 17 | ||||
-rw-r--r-- | net/tools/quic/synchronous_host_resolver.cc | 109 | ||||
-rw-r--r-- | net/tools/quic/synchronous_host_resolver.h | 26 |
4 files changed, 159 insertions, 10 deletions
diff --git a/net/tools/quic/quic_client_bin.cc b/net/tools/quic/quic_client_bin.cc index 259111f..752ccd8 100644 --- a/net/tools/quic/quic_client_bin.cc +++ b/net/tools/quic/quic_client_bin.cc @@ -47,6 +47,8 @@ #include "base/strings/string_split.h" #include "base/strings/string_util.h" #include "net/base/ip_endpoint.h" +#include "net/base/net_errors.h" +#include "net/base/net_log.h" #include "net/base/privacy_mode.h" #include "net/cert/cert_verifier.h" #include "net/http/transport_security_state.h" @@ -57,6 +59,7 @@ #include "net/tools/epoll_server/epoll_server.h" #include "net/tools/quic/quic_client.h" #include "net/tools/quic/spdy_utils.h" +#include "net/tools/quic/synchronous_host_resolver.h" #include "url/gurl.h" using base::StringPiece; @@ -169,14 +172,18 @@ int main(int argc, char *argv[]) { // protocol is required in the URL. GURL url(urls[0]); string host = FLAGS_host; - // TODO(rtenneti): get ip_addr from hostname by doing host resolution. if (host.empty()) { - LOG(ERROR) << "--host must be specified\n"; - return 1; + host = url.host(); } if (!net::ParseIPLiteralToNumber(host, &ip_addr)) { - LOG(ERROR) << "--host could not be parsed as an IP address\n"; - return 1; + net::AddressList addresses; + int rv = net::tools::SynchronousHostResolver::Resolve(host, &addresses); + if (rv != net::OK) { + LOG(ERROR) << "Unable to resolve '" << host << "' : " + << net::ErrorToString(rv); + return 1; + } + ip_addr = addresses[0].address(); } string host_port = net::IPAddressToStringWithPort(ip_addr, FLAGS_port); diff --git a/net/tools/quic/quic_simple_client_bin.cc b/net/tools/quic/quic_simple_client_bin.cc index 33f3e99..3960092 100644 --- a/net/tools/quic/quic_simple_client_bin.cc +++ b/net/tools/quic/quic_simple_client_bin.cc @@ -47,6 +47,8 @@ #include "base/strings/string_split.h" #include "base/strings/string_util.h" #include "net/base/ip_endpoint.h" +#include "net/base/net_errors.h" +#include "net/base/net_log.h" #include "net/base/privacy_mode.h" #include "net/cert/cert_verifier.h" #include "net/http/http_request_info.h" @@ -57,6 +59,7 @@ #include "net/quic/quic_utils.h" #include "net/spdy/spdy_http_utils.h" #include "net/tools/quic/quic_simple_client.h" +#include "net/tools/quic/synchronous_host_resolver.h" #include "url/gurl.h" using base::StringPiece; @@ -170,14 +173,18 @@ int main(int argc, char *argv[]) { // protocol is required in the URL. GURL url(urls[0]); string host = FLAGS_host; - // TODO(rtenneti): get ip_addr from hostname by doing host resolution. if (host.empty()) { - LOG(ERROR) << "--host must be specified\n"; - return 1; + host = url.host(); } if (!net::ParseIPLiteralToNumber(host, &ip_addr)) { - LOG(ERROR) << "--host could not be parsed as an IP address\n"; - return 1; + net::AddressList addresses; + int rv = net::tools::SynchronousHostResolver::Resolve(host, &addresses); + if (rv != net::OK) { + LOG(ERROR) << "Unable to resolve '" << host << "' : " + << net::ErrorToString(rv); + return 1; + } + ip_addr = addresses[0].address(); } string host_port = net::IPAddressToStringWithPort(ip_addr, FLAGS_port); diff --git a/net/tools/quic/synchronous_host_resolver.cc b/net/tools/quic/synchronous_host_resolver.cc new file mode 100644 index 0000000..7955186 --- /dev/null +++ b/net/tools/quic/synchronous_host_resolver.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/tools/quic/synchronous_host_resolver.h" + +#include "base/at_exit.h" +#include "base/memory/weak_ptr.h" +#include "base/threading/simple_thread.h" +#include "net/base/host_port_pair.h" +#include "net/base/net_errors.h" +#include "net/dns/host_resolver_impl.h" +#include "net/dns/single_request_host_resolver.h" + +namespace net { + +namespace tools { + +namespace { + +class ResolverThread : public base::SimpleThread { + public: + ResolverThread(); + + ~ResolverThread() override; + + // Called on the main thread. + int Resolve(const std::string& host, AddressList* addresses); + + // SimpleThread methods: + void Run() override; + + private: + void OnResolutionComplete(int rv); + + AddressList* addresses_; + std::string host_; + int rv_; + + base::WaitableEvent resolved_; // Notified when the resolution is complete. + + base::WeakPtrFactory<ResolverThread> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(ResolverThread); +}; + +ResolverThread::ResolverThread() + : SimpleThread("resolver_thread"), + rv_(ERR_UNEXPECTED), + resolved_(true, false), + weak_factory_(this) {} + +ResolverThread::~ResolverThread() {} + +void ResolverThread::Run() { + base::AtExitManager exit_manager; + base::MessageLoopForIO loop; + + net::HostResolver::Options options; + options.max_concurrent_resolves = 6; + options.max_retry_attempts = 3u; + net::NetLog net_log; + scoped_ptr<net::HostResolverImpl> resolver_impl( + new net::HostResolverImpl(options, &net_log)); + SingleRequestHostResolver resolver(resolver_impl.get()); + + HostPortPair host_port_pair(host_, 80); + + rv_ = resolver.Resolve( + HostResolver::RequestInfo(host_port_pair), DEFAULT_PRIORITY, + addresses_, + base::Bind(&ResolverThread::OnResolutionComplete, + weak_factory_.GetWeakPtr()), + BoundNetLog()); + + if (rv_ != ERR_IO_PENDING) { + resolved_.Signal(); + } + + while (!resolved_.IsSignaled()) { + base::MessageLoop::current()->RunUntilIdle(); + } +} + +int ResolverThread::Resolve(const std::string& host, AddressList* addresses) { + host_ = host; + addresses_ = addresses; + this->Start(); + resolved_.Wait(); + this->Join(); + return rv_; +} + +void ResolverThread::OnResolutionComplete(int rv) { + rv_ = rv; + resolved_.Signal(); +} + +} // namespace + +// static +int SynchronousHostResolver::Resolve(const std::string& host, + AddressList* addresses) { + ResolverThread resolver; + return resolver.Resolve(host, addresses); +} + +} // namespace tools +} // namespace net diff --git a/net/tools/quic/synchronous_host_resolver.h b/net/tools/quic/synchronous_host_resolver.h new file mode 100644 index 0000000..e5373ec --- /dev/null +++ b/net/tools/quic/synchronous_host_resolver.h @@ -0,0 +1,26 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// A simple class for resolving hostname synchronously. + +#ifndef NET_TOOLS_QUIC_SYNCHRONOUS_HOST_RESOLVER_H_ +#define NET_TOOLS_QUIC_SYNCHRONOUS_HOST_RESOLVER_H_ + +#include "net/base/address_list.h" +#include "net/base/net_export.h" +#include "net/dns/host_resolver.h" + +namespace net { + +namespace tools { + +class NET_EXPORT_PRIVATE SynchronousHostResolver { + public: + static int Resolve(const std::string& host, AddressList* addresses); +}; + +} // namespace tools +} // namespace net + +#endif // NET_TOOLS_QUIC_SYNCHRONOUS_HOST_RESOLVER_H_ |