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