| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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) | 7 #if defined(OS_WIN) |
| 8 #include <winsock2.h> | 8 #include <winsock2.h> |
| 9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
| 10 #include <arpa/inet.h> | 10 #include <arpa/inet.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cstring> | 14 #include <cstring> |
| 15 #include <cstdlib> | 15 #include <cstdlib> |
| 16 | 16 |
| 17 #include "base/basictypes.h" |
| 17 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 19 #include "net/base/address_list.h" | 20 #include "net/base/address_list.h" |
| 20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 22 #include "net/base/net_util.h" |
| 21 #include "net/base/ssl_config_service.h" | 23 #include "net/base/ssl_config_service.h" |
| 22 #include "net/base/sys_addrinfo.h" | 24 #include "net/base/sys_addrinfo.h" |
| 23 #include "net/socket/client_socket_factory.h" | 25 #include "net/socket/client_socket_factory.h" |
| 24 #include "net/socket/ssl_client_socket.h" | 26 #include "net/socket/ssl_client_socket.h" |
| 25 #include "net/socket/tcp_client_socket.h" | 27 #include "net/socket/tcp_client_socket.h" |
| 26 #include "talk/base/socketaddress.h" | 28 #include "talk/base/socketaddress.h" |
| 27 | 29 |
| 28 namespace notifier { | 30 namespace notifier { |
| 29 | 31 |
| 30 ChromeAsyncSocket::ChromeAsyncSocket( | 32 ChromeAsyncSocket::ChromeAsyncSocket( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 net_error_ = net_error; | 94 net_error_ = net_error; |
| 93 } | 95 } |
| 94 | 96 |
| 95 void ChromeAsyncSocket::DoNetErrorFromStatus(int status) { | 97 void ChromeAsyncSocket::DoNetErrorFromStatus(int status) { |
| 96 DCHECK_LT(status, net::OK); | 98 DCHECK_LT(status, net::OK); |
| 97 DoNetError(static_cast<net::Error>(status)); | 99 DoNetError(static_cast<net::Error>(status)); |
| 98 } | 100 } |
| 99 | 101 |
| 100 namespace { | 102 namespace { |
| 101 | 103 |
| 104 // Takes a 32-bit integer in host byte order and converts it to a |
| 105 // net::IPAddressNumber. |
| 106 net::IPAddressNumber Uint32ToIPAddressNumber(uint32 ip) { |
| 107 uint32 ip_nbo = htonl(ip); |
| 108 const unsigned char* const ip_start = |
| 109 reinterpret_cast<const unsigned char*>(&ip_nbo); |
| 110 return net::IPAddressNumber(ip_start, ip_start + (sizeof ip_nbo)); |
| 111 } |
| 112 |
| 102 net::AddressList SocketAddressToAddressList( | 113 net::AddressList SocketAddressToAddressList( |
| 103 const talk_base::SocketAddress& address) { | 114 const talk_base::SocketAddress& address) { |
| 104 DCHECK_NE(address.ip(), 0U); | 115 DCHECK_NE(address.ip(), 0U); |
| 105 // Use malloc() as net::AddressList uses free(). | 116 return net::AddressList(Uint32ToIPAddressNumber(address.ip()), |
| 106 addrinfo* ai = static_cast<addrinfo*>(std::malloc(sizeof *ai)); | 117 address.port(), false); |
| 107 memset(ai, 0, sizeof *ai); | |
| 108 ai->ai_family = AF_INET; | |
| 109 ai->ai_socktype = SOCK_STREAM; | |
| 110 ai->ai_addrlen = sizeof(sockaddr_in); | |
| 111 | |
| 112 sockaddr_in* addr = static_cast<sockaddr_in*>(std::malloc(sizeof *addr)); | |
| 113 memset(addr, 0, sizeof *addr); | |
| 114 addr->sin_family = AF_INET; | |
| 115 addr->sin_addr.s_addr = htonl(address.ip()); | |
| 116 addr->sin_port = htons(address.port()); | |
| 117 ai->ai_addr = reinterpret_cast<sockaddr*>(addr); | |
| 118 | |
| 119 net::AddressList address_list; | |
| 120 address_list.Adopt(ai); | |
| 121 return address_list; | |
| 122 } | 118 } |
| 123 | 119 |
| 124 } // namespace | 120 } // namespace |
| 125 | 121 |
| 126 // STATE_CLOSED -> STATE_CONNECTING | 122 // STATE_CLOSED -> STATE_CONNECTING |
| 127 | 123 |
| 128 bool ChromeAsyncSocket::Connect(const talk_base::SocketAddress& address) { | 124 bool ChromeAsyncSocket::Connect(const talk_base::SocketAddress& address) { |
| 129 if (state_ != STATE_CLOSED) { | 125 if (state_ != STATE_CLOSED) { |
| 130 LOG(DFATAL) << "Connect() called on non-closed socket"; | 126 LOG(DFATAL) << "Connect() called on non-closed socket"; |
| 131 DoNonNetError(ERROR_WRONGSTATE); | 127 DoNonNetError(ERROR_WRONGSTATE); |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 } | 467 } |
| 472 state_ = STATE_TLS_OPEN; | 468 state_ = STATE_TLS_OPEN; |
| 473 PostDoRead(); | 469 PostDoRead(); |
| 474 if (write_end_ > 0U) { | 470 if (write_end_ > 0U) { |
| 475 PostDoWrite(); | 471 PostDoWrite(); |
| 476 } | 472 } |
| 477 SignalSSLConnected(); | 473 SignalSSLConnected(); |
| 478 } | 474 } |
| 479 | 475 |
| 480 } // namespace notifier | 476 } // namespace notifier |
| OLD | NEW |