OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 0, false); | 248 0, false); |
249 } | 249 } |
250 ready_ = true; | 250 ready_ = true; |
251 SignalPortComplete(this); | 251 SignalPortComplete(this); |
252 } | 252 } |
253 } | 253 } |
254 | 254 |
255 const ProtocolAddress * RelayPort::ServerAddress(size_t index) const { | 255 const ProtocolAddress * RelayPort::ServerAddress(size_t index) const { |
256 if (index < server_addr_.size()) | 256 if (index < server_addr_.size()) |
257 return &server_addr_[index]; | 257 return &server_addr_[index]; |
258 return NULL; | 258 return nullptr; |
259 } | 259 } |
260 | 260 |
261 bool RelayPort::HasMagicCookie(const char* data, size_t size) { | 261 bool RelayPort::HasMagicCookie(const char* data, size_t size) { |
262 if (size < 24 + sizeof(TURN_MAGIC_COOKIE_VALUE)) { | 262 if (size < 24 + sizeof(TURN_MAGIC_COOKIE_VALUE)) { |
263 return false; | 263 return false; |
264 } else { | 264 } else { |
265 return memcmp(data + 24, | 265 return memcmp(data + 24, |
266 TURN_MAGIC_COOKIE_VALUE, | 266 TURN_MAGIC_COOKIE_VALUE, |
267 sizeof(TURN_MAGIC_COOKIE_VALUE)) == 0; | 267 sizeof(TURN_MAGIC_COOKIE_VALUE)) == 0; |
268 } | 268 } |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 | 442 |
443 int RelayConnection::Send(const void* pv, size_t cb, | 443 int RelayConnection::Send(const void* pv, size_t cb, |
444 const rtc::PacketOptions& options) { | 444 const rtc::PacketOptions& options) { |
445 return socket_->SendTo(pv, cb, GetAddress(), options); | 445 return socket_->SendTo(pv, cb, GetAddress(), options); |
446 } | 446 } |
447 | 447 |
448 void RelayConnection::SendAllocateRequest(RelayEntry* entry, int delay) { | 448 void RelayConnection::SendAllocateRequest(RelayEntry* entry, int delay) { |
449 request_manager_->SendDelayed(new AllocateRequest(entry, this), delay); | 449 request_manager_->SendDelayed(new AllocateRequest(entry, this), delay); |
450 } | 450 } |
451 | 451 |
452 RelayEntry::RelayEntry(RelayPort* port, | 452 RelayEntry::RelayEntry(RelayPort* port, const rtc::SocketAddress& ext_addr) |
453 const rtc::SocketAddress& ext_addr) | 453 : port_(port), |
454 : port_(port), ext_addr_(ext_addr), | 454 ext_addr_(ext_addr), |
455 server_index_(0), connected_(false), locked_(false), | 455 server_index_(0), |
456 current_connection_(NULL) { | 456 connected_(false), |
457 } | 457 locked_(false), |
| 458 current_connection_(nullptr) {} |
458 | 459 |
459 RelayEntry::~RelayEntry() { | 460 RelayEntry::~RelayEntry() { |
460 // Remove all RelayConnections and dispose sockets. | 461 // Remove all RelayConnections and dispose sockets. |
461 delete current_connection_; | 462 delete current_connection_; |
462 current_connection_ = NULL; | 463 current_connection_ = nullptr; |
463 } | 464 } |
464 | 465 |
465 void RelayEntry::Connect() { | 466 void RelayEntry::Connect() { |
466 // If we're already connected, return. | 467 // If we're already connected, return. |
467 if (connected_) | 468 if (connected_) |
468 return; | 469 return; |
469 | 470 |
470 // If we've exhausted all options, bail out. | 471 // If we've exhausted all options, bail out. |
471 const ProtocolAddress* ra = port()->ServerAddress(server_index_); | 472 const ProtocolAddress* ra = port()->ServerAddress(server_index_); |
472 if (!ra) { | 473 if (!ra) { |
473 LOG(LS_WARNING) << "No more relay addresses left to try"; | 474 LOG(LS_WARNING) << "No more relay addresses left to try"; |
474 return; | 475 return; |
475 } | 476 } |
476 | 477 |
477 // Remove any previous connection. | 478 // Remove any previous connection. |
478 if (current_connection_) { | 479 if (current_connection_) { |
479 port()->thread()->Dispose(current_connection_); | 480 port()->thread()->Dispose(current_connection_); |
480 current_connection_ = NULL; | 481 current_connection_ = nullptr; |
481 } | 482 } |
482 | 483 |
483 // Try to set up our new socket. | 484 // Try to set up our new socket. |
484 LOG(LS_INFO) << "Connecting to relay via " << ProtoToString(ra->proto) << | 485 LOG(LS_INFO) << "Connecting to relay via " << ProtoToString(ra->proto) << |
485 " @ " << ra->address.ToSensitiveString(); | 486 " @ " << ra->address.ToSensitiveString(); |
486 | 487 |
487 rtc::AsyncPacketSocket* socket = NULL; | 488 rtc::AsyncPacketSocket* socket = nullptr; |
488 | 489 |
489 if (ra->proto == PROTO_UDP) { | 490 if (ra->proto == PROTO_UDP) { |
490 // UDP sockets are simple. | 491 // UDP sockets are simple. |
491 socket = port_->socket_factory()->CreateUdpSocket( | 492 socket = port_->socket_factory()->CreateUdpSocket( |
492 rtc::SocketAddress(port_->ip(), 0), | 493 rtc::SocketAddress(port_->ip(), 0), |
493 port_->min_port(), port_->max_port()); | 494 port_->min_port(), port_->max_port()); |
494 } else if (ra->proto == PROTO_TCP || ra->proto == PROTO_SSLTCP) { | 495 } else if (ra->proto == PROTO_TCP || ra->proto == PROTO_SSLTCP) { |
495 int opts = (ra->proto == PROTO_SSLTCP) | 496 int opts = (ra->proto == PROTO_SSLTCP) |
496 ? rtc::PacketSocketFactory::OPT_TLS_FAKE | 497 ? rtc::PacketSocketFactory::OPT_TLS_FAKE |
497 : 0; | 498 : 0; |
(...skipping 30 matching lines...) Expand all Loading... |
528 socket->SignalClose.connect(this, &RelayEntry::OnSocketClose); | 529 socket->SignalClose.connect(this, &RelayEntry::OnSocketClose); |
529 socket->SignalConnect.connect(this, &RelayEntry::OnSocketConnect); | 530 socket->SignalConnect.connect(this, &RelayEntry::OnSocketConnect); |
530 port()->thread()->PostDelayed(RTC_FROM_HERE, kSoftConnectTimeoutMs, this, | 531 port()->thread()->PostDelayed(RTC_FROM_HERE, kSoftConnectTimeoutMs, this, |
531 kMessageConnectTimeout); | 532 kMessageConnectTimeout); |
532 } else { | 533 } else { |
533 current_connection_->SendAllocateRequest(this, 0); | 534 current_connection_->SendAllocateRequest(this, 0); |
534 } | 535 } |
535 } | 536 } |
536 | 537 |
537 int RelayEntry::GetError() { | 538 int RelayEntry::GetError() { |
538 if (current_connection_ != NULL) { | 539 if (current_connection_ != nullptr) { |
539 return current_connection_->GetError(); | 540 return current_connection_->GetError(); |
540 } | 541 } |
541 return 0; | 542 return 0; |
542 } | 543 } |
543 | 544 |
544 RelayConnection* RelayEntry::GetBestConnection(RelayConnection* conn1, | 545 RelayConnection* RelayEntry::GetBestConnection(RelayConnection* conn1, |
545 RelayConnection* conn2) { | 546 RelayConnection* conn2) { |
546 return conn1->GetProtocol() <= conn2->GetProtocol() ? conn1 : conn2; | 547 return conn1->GetProtocol() <= conn2->GetProtocol() ? conn1 : conn2; |
547 } | 548 } |
548 | 549 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 // Currently we connect to each server address in sequence. If we | 656 // Currently we connect to each server address in sequence. If we |
656 // have more addresses to try, treat this is an error and move on to | 657 // have more addresses to try, treat this is an error and move on to |
657 // the next address, otherwise give this connection more time and | 658 // the next address, otherwise give this connection more time and |
658 // await the real timeout. | 659 // await the real timeout. |
659 // | 660 // |
660 // TODO: Connect to servers in parallel to speed up connect time | 661 // TODO: Connect to servers in parallel to speed up connect time |
661 // and to avoid giving up too early. | 662 // and to avoid giving up too early. |
662 port_->SignalSoftTimeout(ra); | 663 port_->SignalSoftTimeout(ra); |
663 HandleConnectFailure(current_connection_->socket()); | 664 HandleConnectFailure(current_connection_->socket()); |
664 } else { | 665 } else { |
665 HandleConnectFailure(NULL); | 666 HandleConnectFailure(nullptr); |
666 } | 667 } |
667 } | 668 } |
668 | 669 |
669 void RelayEntry::OnSocketConnect(rtc::AsyncPacketSocket* socket) { | 670 void RelayEntry::OnSocketConnect(rtc::AsyncPacketSocket* socket) { |
670 LOG(INFO) << "relay tcp connected to " << | 671 LOG(INFO) << "relay tcp connected to " << |
671 socket->GetRemoteAddress().ToSensitiveString(); | 672 socket->GetRemoteAddress().ToSensitiveString(); |
672 if (current_connection_ != NULL) { | 673 if (current_connection_ != nullptr) { |
673 current_connection_->SendAllocateRequest(this, 0); | 674 current_connection_->SendAllocateRequest(this, 0); |
674 } | 675 } |
675 } | 676 } |
676 | 677 |
677 void RelayEntry::OnSocketClose(rtc::AsyncPacketSocket* socket, | 678 void RelayEntry::OnSocketClose(rtc::AsyncPacketSocket* socket, |
678 int error) { | 679 int error) { |
679 PLOG(LERROR, error) << "Relay connection failed: socket closed"; | 680 PLOG(LERROR, error) << "Relay connection failed: socket closed"; |
680 HandleConnectFailure(socket); | 681 HandleConnectFailure(socket); |
681 } | 682 } |
682 | 683 |
683 void RelayEntry::OnReadPacket( | 684 void RelayEntry::OnReadPacket( |
684 rtc::AsyncPacketSocket* socket, | 685 rtc::AsyncPacketSocket* socket, |
685 const char* data, size_t size, | 686 const char* data, size_t size, |
686 const rtc::SocketAddress& remote_addr, | 687 const rtc::SocketAddress& remote_addr, |
687 const rtc::PacketTime& packet_time) { | 688 const rtc::PacketTime& packet_time) { |
688 // RTC_DCHECK(remote_addr == port_->server_addr()); | 689 // RTC_DCHECK(remote_addr == port_->server_addr()); |
689 // TODO: are we worried about this? | 690 // TODO: are we worried about this? |
690 | 691 |
691 if (current_connection_ == NULL || socket != current_connection_->socket()) { | 692 if (current_connection_ == nullptr || |
| 693 socket != current_connection_->socket()) { |
692 // This packet comes from an unknown address. | 694 // This packet comes from an unknown address. |
693 LOG(WARNING) << "Dropping packet: unknown address"; | 695 LOG(WARNING) << "Dropping packet: unknown address"; |
694 return; | 696 return; |
695 } | 697 } |
696 | 698 |
697 // If the magic cookie is not present, then this is an unwrapped packet sent | 699 // If the magic cookie is not present, then this is an unwrapped packet sent |
698 // by the server, The actual remote address is the one we recorded. | 700 // by the server, The actual remote address is the one we recorded. |
699 if (!port_->HasMagicCookie(data, size)) { | 701 if (!port_->HasMagicCookie(data, size)) { |
700 if (locked_) { | 702 if (locked_) { |
701 port_->OnReadPacket(data, size, ext_addr_, PROTO_UDP, packet_time); | 703 port_->OnReadPacket(data, size, ext_addr_, PROTO_UDP, packet_time); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 if (rtc::TimeMillis() - start_time_ <= kRetryTimeout) | 841 if (rtc::TimeMillis() - start_time_ <= kRetryTimeout) |
840 entry_->ScheduleKeepAlive(); | 842 entry_->ScheduleKeepAlive(); |
841 } | 843 } |
842 | 844 |
843 void AllocateRequest::OnTimeout() { | 845 void AllocateRequest::OnTimeout() { |
844 LOG(INFO) << "Allocate request timed out"; | 846 LOG(INFO) << "Allocate request timed out"; |
845 entry_->HandleConnectFailure(connection_->socket()); | 847 entry_->HandleConnectFailure(connection_->socket()); |
846 } | 848 } |
847 | 849 |
848 } // namespace cricket | 850 } // namespace cricket |
OLD | NEW |