| 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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType(); | 312 P2PSocketHost::StunMessageType type = P2PSocketHost::StunMessageType(); |
| 312 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); | 313 bool stun = GetStunPacketType(&*data.begin(), data.size(), &type); |
| 313 if (!stun || type == STUN_DATA_INDICATION) { | 314 if (!stun || type == STUN_DATA_INDICATION) { |
| 314 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() | 315 LOG(ERROR) << "Page tried to send a data packet to " << to.ToString() |
| 315 << " before STUN binding is finished."; | 316 << " before STUN binding is finished."; |
| 316 OnError(); | 317 OnError(); |
| 317 return; | 318 return; |
| 318 } | 319 } |
| 319 } | 320 } |
| 320 | 321 |
| 321 DoSend(to, data); | 322 DoSend(to, data, options); |
| 322 } | 323 } |
| 323 | 324 |
| 324 void P2PSocketHostTcpBase::WriteOrQueue( | 325 void P2PSocketHostTcpBase::WriteOrQueue( |
| 325 scoped_refptr<net::DrainableIOBuffer>& buffer) { | 326 scoped_refptr<net::DrainableIOBuffer>& buffer) { |
| 326 if (write_buffer_.get()) { | 327 if (write_buffer_.get()) { |
| 327 write_queue_.push(buffer); | 328 write_queue_.push(buffer); |
| 328 return; | 329 return; |
| 329 } | 330 } |
| 330 | 331 |
| 331 write_buffer_ = buffer; | 332 write_buffer_ = buffer; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 | 444 |
| 444 int consumed = kPacketHeaderSize; | 445 int consumed = kPacketHeaderSize; |
| 445 char* cur = input + consumed; | 446 char* cur = input + consumed; |
| 446 std::vector<char> data(cur, cur + packet_size); | 447 std::vector<char> data(cur, cur + packet_size); |
| 447 OnPacket(data); | 448 OnPacket(data); |
| 448 consumed += packet_size; | 449 consumed += packet_size; |
| 449 return consumed; | 450 return consumed; |
| 450 } | 451 } |
| 451 | 452 |
| 452 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to, | 453 void P2PSocketHostTcp::DoSend(const net::IPEndPoint& to, |
| 453 const std::vector<char>& data) { | 454 const std::vector<char>& data, |
| 455 const talk_base::PacketOptions& options) { |
| 454 int size = kPacketHeaderSize + data.size(); | 456 int size = kPacketHeaderSize + data.size(); |
| 455 scoped_refptr<net::DrainableIOBuffer> buffer = | 457 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 456 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 458 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 457 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); | 459 *reinterpret_cast<uint16*>(buffer->data()) = base::HostToNet16(data.size()); |
| 458 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); | 460 memcpy(buffer->data() + kPacketHeaderSize, &data[0], data.size()); |
| 459 | 461 |
| 462 packet_processing_helpers::ApplyPacketOptions( |
| 463 buffer->data(), buffer->BytesRemaining(), options, 0); |
| 464 |
| 460 WriteOrQueue(buffer); | 465 WriteOrQueue(buffer); |
| 461 } | 466 } |
| 462 | 467 |
| 463 // P2PSocketHostStunTcp | 468 // P2PSocketHostStunTcp |
| 464 P2PSocketHostStunTcp::P2PSocketHostStunTcp( | 469 P2PSocketHostStunTcp::P2PSocketHostStunTcp( |
| 465 IPC::Sender* message_sender, int id, | 470 IPC::Sender* message_sender, int id, |
| 466 P2PSocketType type, net::URLRequestContextGetter* url_context) | 471 P2PSocketType type, net::URLRequestContextGetter* url_context) |
| 467 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { | 472 : P2PSocketHostTcpBase(message_sender, id, type, url_context) { |
| 468 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || | 473 DCHECK(type == P2P_SOCKET_STUN_TCP_CLIENT || |
| 469 type == P2P_SOCKET_STUN_SSLTCP_CLIENT || | 474 type == P2P_SOCKET_STUN_SSLTCP_CLIENT || |
| (...skipping 18 matching lines...) Expand all Loading... |
| 488 int consumed = 0; | 493 int consumed = 0; |
| 489 char* cur = input; | 494 char* cur = input; |
| 490 std::vector<char> data(cur, cur + packet_size); | 495 std::vector<char> data(cur, cur + packet_size); |
| 491 OnPacket(data); | 496 OnPacket(data); |
| 492 consumed += packet_size; | 497 consumed += packet_size; |
| 493 consumed += pad_bytes; | 498 consumed += pad_bytes; |
| 494 return consumed; | 499 return consumed; |
| 495 } | 500 } |
| 496 | 501 |
| 497 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to, | 502 void P2PSocketHostStunTcp::DoSend(const net::IPEndPoint& to, |
| 498 const std::vector<char>& data) { | 503 const std::vector<char>& data, |
| 504 const talk_base::PacketOptions& options) { |
| 499 // Each packet is expected to have header (STUN/TURN ChannelData), where | 505 // Each packet is expected to have header (STUN/TURN ChannelData), where |
| 500 // header contains message type and and length of message. | 506 // header contains message type and and length of message. |
| 501 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) { | 507 if (data.size() < kPacketHeaderSize + kPacketLengthOffset) { |
| 502 NOTREACHED(); | 508 NOTREACHED(); |
| 503 OnError(); | 509 OnError(); |
| 504 return; | 510 return; |
| 505 } | 511 } |
| 506 | 512 |
| 507 int pad_bytes; | 513 int pad_bytes; |
| 508 size_t expected_len = GetExpectedPacketSize( | 514 size_t expected_len = GetExpectedPacketSize( |
| 509 &data[0], data.size(), &pad_bytes); | 515 &data[0], data.size(), &pad_bytes); |
| 510 | 516 |
| 511 // Accepts only complete STUN/TURN packets. | 517 // Accepts only complete STUN/TURN packets. |
| 512 if (data.size() != expected_len) { | 518 if (data.size() != expected_len) { |
| 513 NOTREACHED(); | 519 NOTREACHED(); |
| 514 OnError(); | 520 OnError(); |
| 515 return; | 521 return; |
| 516 } | 522 } |
| 517 | 523 |
| 518 // Add any pad bytes to the total size. | 524 // Add any pad bytes to the total size. |
| 519 int size = data.size() + pad_bytes; | 525 int size = data.size() + pad_bytes; |
| 520 | 526 |
| 521 scoped_refptr<net::DrainableIOBuffer> buffer = | 527 scoped_refptr<net::DrainableIOBuffer> buffer = |
| 522 new net::DrainableIOBuffer(new net::IOBuffer(size), size); | 528 new net::DrainableIOBuffer(new net::IOBuffer(size), size); |
| 523 memcpy(buffer->data(), &data[0], data.size()); | 529 memcpy(buffer->data(), &data[0], data.size()); |
| 524 | 530 |
| 531 packet_processing_helpers::ApplyPacketOptions( |
| 532 buffer->data(), data.size(), options, 0); |
| 533 |
| 525 if (pad_bytes) { | 534 if (pad_bytes) { |
| 526 char padding[4] = {0}; | 535 char padding[4] = {0}; |
| 527 DCHECK_LE(pad_bytes, 4); | 536 DCHECK_LE(pad_bytes, 4); |
| 528 memcpy(buffer->data() + data.size(), padding, pad_bytes); | 537 memcpy(buffer->data() + data.size(), padding, pad_bytes); |
| 529 } | 538 } |
| 530 WriteOrQueue(buffer); | 539 WriteOrQueue(buffer); |
| 531 } | 540 } |
| 532 | 541 |
| 533 int P2PSocketHostStunTcp::GetExpectedPacketSize( | 542 int P2PSocketHostStunTcp::GetExpectedPacketSize( |
| 534 const char* data, int len, int* pad_bytes) { | 543 const char* data, int len, int* pad_bytes) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 547 } else { | 556 } else { |
| 548 packet_size += kTurnChannelDataHeaderSize; | 557 packet_size += kTurnChannelDataHeaderSize; |
| 549 // Calculate any padding if present. | 558 // Calculate any padding if present. |
| 550 if (packet_size % 4) | 559 if (packet_size % 4) |
| 551 *pad_bytes = 4 - packet_size % 4; | 560 *pad_bytes = 4 - packet_size % 4; |
| 552 } | 561 } |
| 553 return packet_size; | 562 return packet_size; |
| 554 } | 563 } |
| 555 | 564 |
| 556 } // namespace content | 565 } // namespace content |
| OLD | NEW |