| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "jingle/notifier/base/chrome_async_socket.h" | 5 #include "jingle/notifier/base/chrome_async_socket.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | |
| 8 #include <winsock2.h> | |
| 9 #elif defined(OS_POSIX) | |
| 10 #include <arpa/inet.h> | |
| 11 #endif | |
| 12 | |
| 13 #include <algorithm> | 7 #include <algorithm> |
| 14 #include <cstring> | 8 #include <cstring> |
| 15 #include <cstdlib> | 9 #include <cstdlib> |
| 16 | 10 |
| 17 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 18 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 19 #include "base/logging.h" | 13 #include "base/logging.h" |
| 20 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "jingle/notifier/base/resolving_client_socket_factory.h" |
| 21 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
| 22 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 23 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 24 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 25 #include "net/base/ssl_config_service.h" | 20 #include "net/base/ssl_config_service.h" |
| 26 #include "net/base/sys_addrinfo.h" | 21 #include "net/base/sys_addrinfo.h" |
| 27 #include "net/socket/client_socket_factory.h" | 22 #include "net/socket/client_socket_factory.h" |
| 23 #include "net/socket/client_socket_handle.h" |
| 28 #include "net/socket/ssl_client_socket.h" | 24 #include "net/socket/ssl_client_socket.h" |
| 29 #include "net/socket/tcp_client_socket.h" | 25 #include "net/socket/tcp_client_socket.h" |
| 30 #include "talk/base/socketaddress.h" | 26 #include "talk/base/socketaddress.h" |
| 31 | 27 |
| 32 namespace notifier { | 28 namespace notifier { |
| 33 | 29 |
| 34 ChromeAsyncSocket::ChromeAsyncSocket( | 30 ChromeAsyncSocket::ChromeAsyncSocket( |
| 35 net::ClientSocketFactory* client_socket_factory, | 31 ResolvingClientSocketFactory* client_socket_factory, |
| 36 const net::SSLConfig& ssl_config, | |
| 37 net::CertVerifier* cert_verifier, | |
| 38 size_t read_buf_size, | 32 size_t read_buf_size, |
| 39 size_t write_buf_size, | 33 size_t write_buf_size, |
| 40 net::NetLog* net_log) | 34 net::NetLog* net_log) |
| 41 : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 35 : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 42 &ChromeAsyncSocket::ProcessConnectDone), | 36 &ChromeAsyncSocket::ProcessConnectDone), |
| 43 read_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 37 read_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 44 &ChromeAsyncSocket::ProcessReadDone), | 38 &ChromeAsyncSocket::ProcessReadDone), |
| 45 write_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 39 write_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 46 &ChromeAsyncSocket::ProcessWriteDone), | 40 &ChromeAsyncSocket::ProcessWriteDone), |
| 47 ssl_connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 41 ssl_connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 48 &ChromeAsyncSocket::ProcessSSLConnectDone), | 42 &ChromeAsyncSocket::ProcessSSLConnectDone), |
| 49 client_socket_factory_(client_socket_factory), | 43 client_socket_factory_(client_socket_factory), |
| 50 ssl_config_(ssl_config), | |
| 51 cert_verifier_(cert_verifier), | |
| 52 bound_net_log_( | 44 bound_net_log_( |
| 53 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), | 45 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), |
| 54 state_(STATE_CLOSED), | 46 state_(STATE_CLOSED), |
| 55 error_(ERROR_NONE), | 47 error_(ERROR_NONE), |
| 56 net_error_(net::OK), | 48 net_error_(net::OK), |
| 57 scoped_runnable_method_factory_( | 49 scoped_runnable_method_factory_( |
| 58 ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 50 ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 59 read_state_(IDLE), | 51 read_state_(IDLE), |
| 60 read_buf_(new net::IOBufferWithSize(read_buf_size)), | 52 read_buf_(new net::IOBufferWithSize(read_buf_size)), |
| 61 read_start_(0U), | 53 read_start_(0U), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 void ChromeAsyncSocket::DoNetError(net::Error net_error) { | 88 void ChromeAsyncSocket::DoNetError(net::Error net_error) { |
| 97 error_ = ERROR_WINSOCK; | 89 error_ = ERROR_WINSOCK; |
| 98 net_error_ = net_error; | 90 net_error_ = net_error; |
| 99 } | 91 } |
| 100 | 92 |
| 101 void ChromeAsyncSocket::DoNetErrorFromStatus(int status) { | 93 void ChromeAsyncSocket::DoNetErrorFromStatus(int status) { |
| 102 DCHECK_LT(status, net::OK); | 94 DCHECK_LT(status, net::OK); |
| 103 DoNetError(static_cast<net::Error>(status)); | 95 DoNetError(static_cast<net::Error>(status)); |
| 104 } | 96 } |
| 105 | 97 |
| 106 namespace { | |
| 107 | |
| 108 // Takes a 32-bit integer in host byte order and converts it to a | |
| 109 // net::IPAddressNumber. | |
| 110 net::IPAddressNumber Uint32ToIPAddressNumber(uint32 ip) { | |
| 111 uint32 ip_nbo = htonl(ip); | |
| 112 const unsigned char* const ip_start = | |
| 113 reinterpret_cast<const unsigned char*>(&ip_nbo); | |
| 114 return net::IPAddressNumber(ip_start, ip_start + (sizeof ip_nbo)); | |
| 115 } | |
| 116 | |
| 117 net::AddressList SocketAddressToAddressList( | |
| 118 const talk_base::SocketAddress& address) { | |
| 119 DCHECK_NE(address.ip(), 0U); | |
| 120 return net::AddressList(Uint32ToIPAddressNumber(address.ip()), | |
| 121 address.port(), false); | |
| 122 } | |
| 123 | |
| 124 } // namespace | |
| 125 | |
| 126 // STATE_CLOSED -> STATE_CONNECTING | 98 // STATE_CLOSED -> STATE_CONNECTING |
| 127 | 99 |
| 128 bool ChromeAsyncSocket::Connect(const talk_base::SocketAddress& address) { | 100 bool ChromeAsyncSocket::Connect(const talk_base::SocketAddress& address) { |
| 129 if (state_ != STATE_CLOSED) { | 101 if (state_ != STATE_CLOSED) { |
| 130 LOG(DFATAL) << "Connect() called on non-closed socket"; | 102 LOG(DFATAL) << "Connect() called on non-closed socket"; |
| 131 DoNonNetError(ERROR_WRONGSTATE); | 103 DoNonNetError(ERROR_WRONGSTATE); |
| 132 return false; | 104 return false; |
| 133 } | 105 } |
| 134 if (address.ip() == 0) { | 106 // We can't work with an empty hostname and IP address. |
| 107 if (address.hostname().empty() && (address.ip() == 0)) { |
| 135 DoNonNetError(ERROR_DNS); | 108 DoNonNetError(ERROR_DNS); |
| 136 return false; | 109 return false; |
| 137 } | 110 } |
| 138 | 111 |
| 139 DCHECK_EQ(state_, buzz::AsyncSocket::STATE_CLOSED); | 112 DCHECK_EQ(state_, buzz::AsyncSocket::STATE_CLOSED); |
| 140 DCHECK_EQ(read_state_, IDLE); | 113 DCHECK_EQ(read_state_, IDLE); |
| 141 DCHECK_EQ(write_state_, IDLE); | 114 DCHECK_EQ(write_state_, IDLE); |
| 142 | 115 |
| 143 state_ = STATE_CONNECTING; | 116 state_ = STATE_CONNECTING; |
| 144 | 117 |
| 145 DCHECK(scoped_runnable_method_factory_.empty()); | 118 DCHECK(scoped_runnable_method_factory_.empty()); |
| 146 scoped_runnable_method_factory_.RevokeAll(); | 119 scoped_runnable_method_factory_.RevokeAll(); |
| 147 | 120 |
| 148 net::AddressList address_list = SocketAddressToAddressList(address); | 121 net::HostPortPair dest_host_port_pair(address.IPAsString(), address.port()); |
| 122 |
| 149 transport_socket_.reset( | 123 transport_socket_.reset( |
| 150 client_socket_factory_->CreateTransportClientSocket( | 124 client_socket_factory_->CreateTransportClientSocket( |
| 151 address_list, bound_net_log_.net_log(), net::NetLog::Source())); | 125 dest_host_port_pair, bound_net_log_.net_log())); |
| 152 int status = transport_socket_->Connect(&connect_callback_); | 126 int status = transport_socket_->Connect(&connect_callback_); |
| 153 if (status != net::ERR_IO_PENDING) { | 127 if (status != net::ERR_IO_PENDING) { |
| 154 // We defer execution of ProcessConnectDone instead of calling it | 128 // We defer execution of ProcessConnectDone instead of calling it |
| 155 // directly here as the caller may not expect an error/close to | 129 // directly here as the caller may not expect an error/close to |
| 156 // happen here. This is okay, as from the caller's point of view, | 130 // happen here. This is okay, as from the caller's point of view, |
| 157 // the connect always happens asynchronously. | 131 // the connect always happens asynchronously. |
| 158 MessageLoop* message_loop = MessageLoop::current(); | 132 MessageLoop* message_loop = MessageLoop::current(); |
| 159 CHECK(message_loop); | 133 CHECK(message_loop); |
| 160 message_loop->PostTask( | 134 message_loop->PostTask( |
| 161 FROM_HERE, | 135 FROM_HERE, |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 state_ = STATE_TLS_CONNECTING; | 403 state_ = STATE_TLS_CONNECTING; |
| 430 read_state_ = IDLE; | 404 read_state_ = IDLE; |
| 431 read_start_ = 0U; | 405 read_start_ = 0U; |
| 432 read_end_ = 0U; | 406 read_end_ = 0U; |
| 433 DCHECK_EQ(write_end_, 0U); | 407 DCHECK_EQ(write_end_, 0U); |
| 434 | 408 |
| 435 // Clear out any posted DoRead() tasks. | 409 // Clear out any posted DoRead() tasks. |
| 436 scoped_runnable_method_factory_.RevokeAll(); | 410 scoped_runnable_method_factory_.RevokeAll(); |
| 437 | 411 |
| 438 DCHECK(transport_socket_.get()); | 412 DCHECK(transport_socket_.get()); |
| 413 net::ClientSocketHandle* socket_handle = new net::ClientSocketHandle(); |
| 414 socket_handle->set_socket(transport_socket_.release()); |
| 439 transport_socket_.reset( | 415 transport_socket_.reset( |
| 440 client_socket_factory_->CreateSSLClientSocket( | 416 client_socket_factory_->CreateSSLClientSocket( |
| 441 transport_socket_.release(), net::HostPortPair(domain_name, 443), | 417 socket_handle, net::HostPortPair(domain_name, 443))); |
| 442 ssl_config_, NULL /* ssl_host_info */, | |
| 443 cert_verifier_)); | |
| 444 int status = transport_socket_->Connect(&ssl_connect_callback_); | 418 int status = transport_socket_->Connect(&ssl_connect_callback_); |
| 445 if (status != net::ERR_IO_PENDING) { | 419 if (status != net::ERR_IO_PENDING) { |
| 446 MessageLoop* message_loop = MessageLoop::current(); | 420 MessageLoop* message_loop = MessageLoop::current(); |
| 447 CHECK(message_loop); | 421 CHECK(message_loop); |
| 448 message_loop->PostTask( | 422 message_loop->PostTask( |
| 449 FROM_HERE, | 423 FROM_HERE, |
| 450 scoped_runnable_method_factory_.NewRunnableMethod( | 424 scoped_runnable_method_factory_.NewRunnableMethod( |
| 451 &ChromeAsyncSocket::ProcessSSLConnectDone, status)); | 425 &ChromeAsyncSocket::ProcessSSLConnectDone, status)); |
| 452 } | 426 } |
| 453 return true; | 427 return true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 472 } | 446 } |
| 473 state_ = STATE_TLS_OPEN; | 447 state_ = STATE_TLS_OPEN; |
| 474 PostDoRead(); | 448 PostDoRead(); |
| 475 if (write_end_ > 0U) { | 449 if (write_end_ > 0U) { |
| 476 PostDoWrite(); | 450 PostDoWrite(); |
| 477 } | 451 } |
| 478 SignalSSLConnected(); | 452 SignalSSLConnected(); |
| 479 } | 453 } |
| 480 | 454 |
| 481 } // namespace notifier | 455 } // namespace notifier |
| OLD | NEW |