Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
| 9 #include "ipc/ipc_sender.h" | 9 #include "ipc/ipc_sender.h" |
| 10 #include "jingle/glue/fake_ssl_client_socket.h" | |
| 10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 13 #include "net/socket/tcp_client_socket.h" | 14 #include "net/socket/tcp_client_socket.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 typedef uint16 PacketLength; | 18 typedef uint16 PacketLength; |
| 18 const int kPacketHeaderSize = sizeof(PacketLength); | 19 const int kPacketHeaderSize = sizeof(PacketLength); |
| 19 const int kReadBufferSize = 4096; | 20 const int kReadBufferSize = 4096; |
| 20 const int kPacketLengthOffset = 2; | 21 const int kPacketLengthOffset = 2; |
| 21 const int kTurnChannelDataHeaderSize = 4; | 22 const int kTurnChannelDataHeaderSize = 4; |
| 22 | 23 |
| 24 bool IsSslClientSocket(content::P2PSocketType type) { | |
| 25 return (type == content::P2P_SOCKET_SSLTCP_CLIENT || | |
| 26 type == content::P2P_SOCKET_STUN_SSLTCP_CLIENT); | |
| 27 } | |
| 28 | |
| 23 } // namespace | 29 } // namespace |
| 24 | 30 |
| 25 namespace content { | 31 namespace content { |
| 26 | 32 |
| 27 P2PSocketHostTcpBase::P2PSocketHostTcpBase(IPC::Sender* message_sender, | 33 P2PSocketHostTcpBase::P2PSocketHostTcpBase(IPC::Sender* message_sender, |
| 28 int id) | 34 int id, |
| 35 P2PSocketType type) | |
| 29 : P2PSocketHost(message_sender, id), | 36 : P2PSocketHost(message_sender, id), |
| 30 write_pending_(false), | 37 write_pending_(false), |
| 31 connected_(false) { | 38 connected_(false), |
| 39 type_(type) { | |
| 32 } | 40 } |
| 33 | 41 |
| 34 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { | 42 P2PSocketHostTcpBase::~P2PSocketHostTcpBase() { |
| 35 if (state_ == STATE_OPEN) { | 43 if (state_ == STATE_OPEN) { |
| 36 DCHECK(socket_.get()); | 44 DCHECK(socket_.get()); |
| 37 socket_.reset(); | 45 socket_.reset(); |
| 38 } | 46 } |
| 39 } | 47 } |
| 40 | 48 |
| 41 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, | 49 bool P2PSocketHostTcpBase::InitAccepted(const net::IPEndPoint& remote_address, |
| 42 net::StreamSocket* socket) { | 50 net::StreamSocket* socket) { |
| 43 DCHECK(socket); | 51 DCHECK(socket); |
| 44 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 52 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 45 | 53 |
| 46 remote_address_ = remote_address; | 54 remote_address_ = remote_address; |
| 55 // TODO(ronghuawu): Add FakeSSLServerSocket. | |
| 47 socket_.reset(socket); | 56 socket_.reset(socket); |
| 48 state_ = STATE_OPEN; | 57 state_ = STATE_OPEN; |
| 49 DoRead(); | 58 DoRead(); |
| 50 return state_ != STATE_ERROR; | 59 return state_ != STATE_ERROR; |
| 51 } | 60 } |
| 52 | 61 |
| 53 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, | 62 bool P2PSocketHostTcpBase::Init(const net::IPEndPoint& local_address, |
| 54 const net::IPEndPoint& remote_address) { | 63 const net::IPEndPoint& remote_address) { |
| 55 DCHECK_EQ(state_, STATE_UNINITIALIZED); | 64 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 56 | 65 |
| 57 remote_address_ = remote_address; | 66 remote_address_ = remote_address; |
| 58 state_ = STATE_CONNECTING; | 67 state_ = STATE_CONNECTING; |
| 59 scoped_ptr<net::TCPClientSocket> tcp_socket(new net::TCPClientSocket( | 68 scoped_ptr<net::TCPClientSocket> tcp_socket(new net::TCPClientSocket( |
| 60 net::AddressList(remote_address), | 69 net::AddressList(remote_address), |
| 61 NULL, net::NetLog::Source())); | 70 NULL, net::NetLog::Source())); |
| 62 if (tcp_socket->Bind(local_address) != net::OK) { | 71 if (tcp_socket->Bind(local_address) != net::OK) { |
| 63 OnError(); | 72 OnError(); |
| 64 return false; | 73 return false; |
| 65 } | 74 } |
| 66 socket_.reset(tcp_socket.release()); | 75 if (IsSslClientSocket(type_)) { |
| 76 socket_.reset(new jingle_glue::FakeSSLClientSocket(tcp_socket.release())); | |
|
piman
2013/06/20 20:53:15
Is this production code? Why are we using a "fake"
Ronghua Wu (Left Chromium)
2013/06/20 21:05:28
I would say it's the name that's misleading. This
| |
| 77 } else { | |
| 78 socket_ = tcp_socket.PassAs<net::StreamSocket>(); | |
| 79 } | |
| 67 | 80 |
| 68 int result = socket_->Connect( | 81 int result = socket_->Connect( |
| 69 base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); | 82 base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); |
| 70 if (result != net::ERR_IO_PENDING) { | 83 if (result != net::ERR_IO_PENDING) { |
| 71 OnConnected(result); | 84 OnConnected(result); |
| 72 } | 85 } |
| 73 | 86 |
| 74 return state_ != STATE_ERROR; | 87 return state_ != STATE_ERROR; |
| 75 } | 88 } |
| 76 | 89 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 pos += consumed; | 278 pos += consumed; |
| 266 } | 279 } |
| 267 // We've consumed all complete packets from the buffer; now move any remaining | 280 // We've consumed all complete packets from the buffer; now move any remaining |
| 268 // bytes to the head of the buffer and set offset to reflect this. | 281 // bytes to the head of the buffer and set offset to reflect this. |
| 269 if (pos && pos <= read_buffer_->offset()) { | 282 if (pos && pos <= read_buffer_->offset()) { |
| 270 memmove(head, head + pos, read_buffer_->offset() - pos); | 283 memmove(head, head + pos, read_buffer_->offset() - pos); |
| 271 read_buffer_->set_offset(read_buffer_->offset() - pos); | 284 read_buffer_->set_offset(read_buffer_->offset() - pos); |
| 272 } | 285 } |
| 273 } | 286 } |
| 274 | 287 |
| 275 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, int id) | 288 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, |
| 276 : P2PSocketHostTcpBase(message_sender, id) { | 289 int id, |
| 290 P2PSocketType type) | |
| 291 : P2PSocketHostTcpBase(message_sender, id, type) { | |
| 292 DCHECK(type == P2P_SOCKET_TCP_CLIENT || type == P2P_SOCKET_SSLTCP_CLIENT); | |
| 277 } | 293 } |
| 278 | 294 |
| 279 P2PSocketHostTcp::~P2PSocketHostTcp() { | 295 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 280 } | 296 } |
| 281 | 297 |
| 282 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { | 298 int P2PSocketHostTcp::ProcessInput(char* input, int input_len) { |
| 283 if (input_len < kPacketHeaderSize) | 299 if (input_len < kPacketHeaderSize) |
| 284 return 0; | 300 return 0; |
| 285 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); | 301 int packet_size = base::NetToHost16(*reinterpret_cast<uint16*>(input)); |
| 286 if (input_len < packet_size + kPacketHeaderSize) | 302 if (input_len < packet_size + kPacketHeaderSize) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 300 scoped_refptr<net::DrainableIOBuffer> buffer = | 316 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 301 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 317 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 302 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); | 318 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); |
| 303 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); | 319 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); |
| 304 | 320 |
| 305 WriteOrQueue(buffer); | 321 WriteOrQueue(buffer); |
| 306 } | 322 } |
| 307 | 323 |
| 308 // P2PSocketHostStunTcp | 324 // P2PSocketHostStunTcp |
| 309 P2PSocketHostStunTcp::P2PSocketHostStunTcp(IPC::Sender* message_sender, | 325 P2PSocketHostStunTcp::P2PSocketHostStunTcp(IPC::Sender* message_sender, |
| 310 int id) | 326 int id, |
| 311 : P2PSocketHostTcpBase(message_sender, id) { | 327 P2PSocketType type) |
| 328 : P2PSocketHostTcpBase(message_sender, id, type) { | |
| 329 DCHECK(type == P2P_SOCKET_SSLTCP_CLIENT || | |
| 330 type == P2P_SOCKET_STUN_SSLTCP_CLIENT); | |
| 312 } | 331 } |
| 313 | 332 |
| 314 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { | 333 P2PSocketHostStunTcp::~P2PSocketHostStunTcp() { |
| 315 } | 334 } |
| 316 | 335 |
| 317 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { | 336 int P2PSocketHostStunTcp::ProcessInput(char* input, int input_len) { |
| 318 if (input_len < kPacketHeaderSize + kPacketLengthOffset) | 337 if (input_len < kPacketHeaderSize + kPacketLengthOffset) |
| 319 return 0; | 338 return 0; |
| 320 | 339 |
| 321 int pad_bytes; | 340 int pad_bytes; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 } else { | 407 } else { |
| 389 packet_size += kTurnChannelDataHeaderSize; | 408 packet_size += kTurnChannelDataHeaderSize; |
| 390 // Calculate any padding if present. | 409 // Calculate any padding if present. |
| 391 if (packet_size % 4) | 410 if (packet_size % 4) |
| 392 *pad_bytes = 4 - packet_size % 4; | 411 *pad_bytes = 4 - packet_size % 4; |
| 393 } | 412 } |
| 394 return packet_size; | 413 return packet_size; |
| 395 } | 414 } |
| 396 | 415 |
| 397 } // namespace content | 416 } // namespace content |
| OLD | NEW |