Commit c902850a authored by John Chen's avatar John Chen Committed by Commit Bot

[ChromeDriver] Listen on both IPv4 & IPv6

On hosts that support both IPv4 and IPv6, ChromeDriver now listens on
both protocols.

Bug: chromedriver:779
Change-Id: I4a115cc0ca5cf0396acdad8d4c1a72f169120368
Reviewed-on: https://chromium-review.googlesource.com/1080448Reviewed-by: default avatarCaleb Rouleau <crouleau@chromium.org>
Reviewed-by: default avatarJonathon Kereliuk <kereliuk@chromium.org>
Commit-Queue: John Chen <johnchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564984}
parent b9ace247
...@@ -77,22 +77,20 @@ class HttpServer : public net::HttpServer::Delegate { ...@@ -77,22 +77,20 @@ class HttpServer : public net::HttpServer::Delegate {
~HttpServer() override {} ~HttpServer() override {}
bool Start(uint16_t port, bool allow_remote) { int Start(uint16_t port, bool allow_remote, bool use_ipv4) {
std::unique_ptr<net::ServerSocket> server_socket( std::unique_ptr<net::ServerSocket> server_socket(
new net::TCPServerSocket(NULL, net::NetLogSource())); new net::TCPServerSocket(NULL, net::NetLogSource()));
if (ListenOnIPv4(server_socket.get(), port, allow_remote) != net::OK) { int status = use_ipv4
// This will work on an IPv6-only host, but we will be IPv4-only on ? ListenOnIPv4(server_socket.get(), port, allow_remote)
// dual-stack hosts. : ListenOnIPv6(server_socket.get(), port, allow_remote);
// TODO(samuong): change this to listen on both IPv4 and IPv6. if (status != net::OK) {
VLOG(0) << "listen on IPv4 failed, trying IPv6"; VLOG(0) << "listen on " << (use_ipv4 ? "IPv4" : "IPv6")
if (ListenOnIPv6(server_socket.get(), port, allow_remote) != net::OK) { << " failed with error " << net::ErrorToShortString(status);
VLOG(1) << "listen on both IPv4 and IPv6 failed, giving up"; return status;
return false;
}
} }
server_.reset(new net::HttpServer(std::move(server_socket), this)); server_.reset(new net::HttpServer(std::move(server_socket), this));
net::IPEndPoint address; net::IPEndPoint address;
return server_->GetLocalAddress(&address) == net::OK; return server_->GetLocalAddress(&address);
} }
// Overridden from net::HttpServer::Delegate: // Overridden from net::HttpServer::Delegate:
...@@ -174,24 +172,48 @@ void HandleRequestOnIOThread( ...@@ -174,24 +172,48 @@ void HandleRequestOnIOThread(
} }
base::LazyInstance<base::ThreadLocalPointer<HttpServer>>::DestructorAtExit base::LazyInstance<base::ThreadLocalPointer<HttpServer>>::DestructorAtExit
lazy_tls_server = LAZY_INSTANCE_INITIALIZER; lazy_tls_server_ipv4 = LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<base::ThreadLocalPointer<HttpServer>>::DestructorAtExit
lazy_tls_server_ipv6 = LAZY_INSTANCE_INITIALIZER;
void StopServerOnIOThread() { void StopServerOnIOThread() {
// Note, |server| may be NULL. // Note, |server| may be NULL.
HttpServer* server = lazy_tls_server.Pointer()->Get(); HttpServer* server = lazy_tls_server_ipv4.Pointer()->Get();
lazy_tls_server.Pointer()->Set(NULL); lazy_tls_server_ipv4.Pointer()->Set(NULL);
delete server;
server = lazy_tls_server_ipv6.Pointer()->Get();
lazy_tls_server_ipv6.Pointer()->Set(NULL);
delete server; delete server;
} }
void StartServerOnIOThread(uint16_t port, void StartServerOnIOThread(uint16_t port,
bool allow_remote, bool allow_remote,
const HttpRequestHandlerFunc& handle_request_func) { const HttpRequestHandlerFunc& handle_request_func) {
std::unique_ptr<HttpServer> temp_server(new HttpServer(handle_request_func)); std::unique_ptr<HttpServer> temp_server;
if (!temp_server->Start(port, allow_remote)) {
printf("Port not available. Exiting...\n"); temp_server.reset(new HttpServer(handle_request_func));
int ipv4_status = temp_server->Start(port, allow_remote, true);
if (ipv4_status == net::OK) {
lazy_tls_server_ipv4.Pointer()->Set(temp_server.release());
} else if (ipv4_status == net::ERR_ADDRESS_IN_USE) {
printf("IPv4 port not available. Exiting...\n");
exit(1);
}
temp_server.reset(new HttpServer(handle_request_func));
int ipv6_status = temp_server->Start(port, allow_remote, false);
if (ipv6_status == net::OK) {
lazy_tls_server_ipv6.Pointer()->Set(temp_server.release());
} else if (ipv6_status == net::ERR_ADDRESS_IN_USE) {
printf("IPv6 port not available. Exiting...\n");
exit(1);
}
if (ipv4_status != net::OK && ipv6_status != net::OK) {
printf("Unable to start server with either IPv4 or IPv6. Exiting...\n");
exit(1); exit(1);
} }
lazy_tls_server.Pointer()->Set(temp_server.release());
} }
void RunServer(uint16_t port, void RunServer(uint16_t port,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment