| 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 "jingle/glue/fake_ssl_client_socket.h" |
| 11 #include "jingle/glue/proxy_resolving_client_socket.h" | 11 #include "jingle/glue/proxy_resolving_client_socket.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 15 #include "net/socket/client_socket_factory.h" | 15 #include "net/socket/client_socket_factory.h" |
| 16 #include "net/socket/client_socket_handle.h" | 16 #include "net/socket/client_socket_handle.h" |
| 17 #include "net/socket/ssl_client_socket.h" | 17 #include "net/socket/ssl_client_socket.h" |
| 18 #include "net/socket/tcp_client_socket.h" | 18 #include "net/socket/tcp_client_socket.h" |
| 19 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 typedef uint16 PacketLength; | 25 typedef uint16 PacketLength; |
| 25 const int kPacketHeaderSize = sizeof(PacketLength); | 26 const int kPacketHeaderSize = sizeof(PacketLength); |
| 26 const int kReadBufferSize = 4096; | 27 const int kReadBufferSize = 4096; |
| 27 const int kPacketLengthOffset = 2; | 28 const int kPacketLengthOffset = 2; |
| 28 const int kTurnChannelDataHeaderSize = 4; | 29 const int kTurnChannelDataHeaderSize = 4; |
| 29 const int kRecvSocketBufferSize = 128 * 1024; | 30 const int kRecvSocketBufferSize = 128 * 1024; |
| 30 const int kSendSocketBufferSize = 128 * 1024; | 31 const int kSendSocketBufferSize = 128 * 1024; |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType(); | 309 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType(); |
| 309 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); | 310 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); |
| 310 if (!stun || type == STUN_DATA_INDICATION) { | 311 if (!stun || type == STUN_DATA_INDICATION) { |
| 311 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() | 312 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() |
| 312 << " before STUN binding is finished."; | 313 << " before STUN binding is finished."; |
| 313 OnError(); | 314 OnError(); |
| 314 return; | 315 return; |
| 315 } | 316 } |
| 316 } | 317 } |
| 317 | 318 |
| 318 DoSend(to, data); | 319 DoSend(to, data, options); |
| 319 } | 320 } |
| 320 | 321 |
| 321 void P2PSocketHostTcpBase::WriteOrQueue( | 322 void P2PSocketHostTcpBase::WriteOrQueue( |
| 322 scoped_refptr<net::DrainableIOBuffer>& buffer) { | 323 scoped_refptr<net::DrainableIOBuffer>& buffer) { |
| 323 if (write_buffer_.get()) { | 324 if (write_buffer_.get()) { |
| 324 write_queue_.push(buffer); | 325 write_queue_.push(buffer); |
| 325 return; | 326 return; |
| 326 } | 327 } |
| 327 | 328 |
| 328 write_buffer_ = buffer; | 329 write_buffer_ = buffer; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 441 |
| 441 int consumed = kPacketHeaderSize; | 442 int consumed = kPacketHeaderSize; |
| 442 char* cur = input + consumed; | 443 char* cur = input + consumed; |
| 443 std::vector<char> data(cur, cur + packet_size); | 444 std::vector<char> data(cur, cur + packet_size); |
| 444 OnPacket(data); | 445 OnPacket(data); |
| 445 consumed += packet_size; | 446 consumed += packet_size; |
| 446 return consumed; | 447 return consumed; |
| 447 } | 448 } |
| 448 | 449 |
| 449 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to, | 450 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to, |
| 450 const std::vector<char>& data) { | 451 const std::vector<char>& data, |
| 452 const talk_base::PacketOptions& options) { |
| 451 int size = kPacketHeaderSize + data.size(); | 453 int size = kPacketHeaderSize + data.size(); |
| 452 scoped_refptr<net::DrainableIOBuffer> buffer = | 454 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 453 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 455 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 454 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); | 456 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); |
| 455 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); | 457 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); |
| 456 | 458 |
| 459 packet_processing_helpers::ApplyPacketOptions( |
| 460 buffer->data(), buffer->BytesRemaining(), options, 0); |
| 461 |
| 457 WriteOrQueue(buffer); | 462 WriteOrQueue(buffer); |
| 458 } | 463 } |
| 459 | 464 |
| 460 // P2PSocketHostStunTcp | 465 // P2PSocketHostStunTcp |
| 461 P2PSocketHostStunTcp::P2PSocketHostStunTcp( | 466 P2PSocketHostStunTcp::P2PSocketHostStunTcp( |
| 462 IPC::Sender* message_sender, int id, | 467 IPC::Sender* message_sender, int id, |
| 463 P2PSocketType type, net::URLRequestContextGetter* url_context) | 468 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 464 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { | 469 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 465 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || | 470 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || |
| 466 type == P2P_SOCKET_STUN_SSLTCP_CLIENT || | 471 type == P2P_SOCKET_STUN_SSLTCP_CLIENT || |
| (...skipping 18 matching lines...) Expand all Loading... |
| 485 int consumed = 0; | 490 int consumed = 0; |
| 486 char* cur = input; | 491 char* cur = input; |
| 487 std::vector<char> data(cur, cur + packet_size); | 492 std::vector<char> data(cur, cur + packet_size); |
| 488 OnPacket(data); | 493 OnPacket(data); |
| 489 consumed += packet_size; | 494 consumed += packet_size; |
| 490 consumed += pad_bytes; | 495 consumed += pad_bytes; |
| 491 return consumed; | 496 return consumed; |
| 492 } | 497 } |
| 493 | 498 |
| 494 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to, | 499 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to, |
| 495 const std::vector<char>& data) { | 500 const std::vector<char>& data, |
| 501 const talk_base::PacketOptions& options) { |
| 496 // Each packet is expected to have header (STUN/TURN ChannelData), where | 502 // Each packet is expected to have header (STUN/TURN ChannelData), where |
| 497 // header contains message type and and length of message. | 503 // header contains message type and and length of message. |
| 498 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) { | 504 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) { |
| 499 NOTREACHED(); | 505 NOTREACHED(); |
| 500 OnError(); | 506 OnError(); |
| 501 return; | 507 return; |
| 502 } | 508 } |
| 503 | 509 |
| 504 int pad_bytes; | 510 int pad_bytes; |
| 505 size_t expected_len = GetExpectedPacketSize( | 511 size_t expected_len = GetExpectedPacketSize( |
| 506 &data[0], data.size(), &pad_bytes); | 512 &data[0], data.size(), &pad_bytes); |
| 507 | 513 |
| 508 // Accepts only complete STUN/TURN packets. | 514 // Accepts only complete STUN/TURN packets. |
| 509 if (data.size() != expected_len) { | 515 if (data.size() != expected_len) { |
| 510 NOTREACHED(); | 516 NOTREACHED(); |
| 511 OnError(); | 517 OnError(); |
| 512 return; | 518 return; |
| 513 } | 519 } |
| 514 | 520 |
| 515 // Add any pad bytes to the total size. | 521 // Add any pad bytes to the total size. |
| 516 int size = data.size() + pad_bytes; | 522 int size = data.size() + pad_bytes; |
| 517 | 523 |
| 518 scoped_refptr<net::DrainableIOBuffer> buffer = | 524 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 519 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 525 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 520 memcpy(buffer->data(), &data[0], data.size()); | 526 memcpy(buffer->data(), &data[0], data.size()); |
| 521 | 527 |
| 528 packet_processing_helpers::ApplyPacketOptions( |
| 529 buffer->data(), data.size(), options, 0); |
| 530 |
| 522 if (pad_bytes) { | 531 if (pad_bytes) { |
| 523 char padding[4] = {0}; | 532 char padding[4] = {0}; |
| 524 DCHECK_LE(pad_bytes, 4); | 533 DCHECK_LE(pad_bytes, 4); |
| 525 memcpy(buffer->data() + data.size(), padding, pad_bytes); | 534 memcpy(buffer->data() + data.size(), padding, pad_bytes); |
| 526 } | 535 } |
| 527 WriteOrQueue(buffer); | 536 WriteOrQueue(buffer); |
| 528 } | 537 } |
| 529 | 538 |
| 530 int P2PSocketHostStunTcp::GetExpectedPacketSize( | 539 int P2PSocketHostStunTcp::GetExpectedPacketSize( |
| 531 const char* data, int len, int* pad_bytes) { | 540 const char* data, int len, int* pad_bytes) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 544 } else { | 553 } else { |
| 545 packet_size += kTurnChannelDataHeaderSize; | 554 packet_size += kTurnChannelDataHeaderSize; |
| 546 // Calculate any padding if present. | 555 // Calculate any padding if present. |
| 547 if (packet_size % 4) | 556 if (packet_size % 4) |
| 548 *pad_bytes = 4 - packet_size % 4; | 557 *pad_bytes = 4 - packet_size % 4; |
| 549 } | 558 } |
| 550 return packet_size; | 559 return packet_size; |
| 551 } | 560 } |
| 552 | 561 |
| 553 } // namespace content | 562 } // namespace content |
| OLD | NEW |