OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 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 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 // Retry at most twice (i.e. three different ALLOCATE requests) on | 37 // Retry at most twice (i.e. three different ALLOCATE requests) on |
38 // STUN_ERROR_ALLOCATION_MISMATCH error per rfc5766. | 38 // STUN_ERROR_ALLOCATION_MISMATCH error per rfc5766. |
39 static const size_t MAX_ALLOCATE_MISMATCH_RETRIES = 2; | 39 static const size_t MAX_ALLOCATE_MISMATCH_RETRIES = 2; |
40 | 40 |
41 static const int TURN_SUCCESS_RESULT_CODE = 0; | 41 static const int TURN_SUCCESS_RESULT_CODE = 0; |
42 | 42 |
43 inline bool IsTurnChannelData(uint16_t msg_type) { | 43 inline bool IsTurnChannelData(uint16_t msg_type) { |
44 return ((msg_type & 0xC000) == 0x4000); // MSB are 0b01 | 44 return ((msg_type & 0xC000) == 0x4000); // MSB are 0b01 |
45 } | 45 } |
46 | 46 |
47 static int GetRelayPreference(cricket::ProtocolType proto, bool secure) { | 47 static int GetRelayPreference(cricket::ProtocolType proto) { |
48 int relay_preference = ICE_TYPE_PREFERENCE_RELAY; | 48 switch (proto) { |
49 if (proto == cricket::PROTO_TCP) { | 49 case cricket::PROTO_TCP: |
50 relay_preference -= 1; | 50 return ICE_TYPE_PREFERENCE_RELAY_TCP; |
51 if (secure) | 51 case cricket::PROTO_TLS: |
52 relay_preference -= 1; | 52 return ICE_TYPE_PREFERENCE_RELAY_TLS; |
| 53 default: |
| 54 RTC_DCHECK(proto == PROTO_UDP); |
| 55 return ICE_TYPE_PREFERENCE_RELAY_UDP; |
53 } | 56 } |
54 | |
55 ASSERT(relay_preference >= 0); | |
56 return relay_preference; | |
57 } | 57 } |
58 | 58 |
59 class TurnAllocateRequest : public StunRequest { | 59 class TurnAllocateRequest : public StunRequest { |
60 public: | 60 public: |
61 explicit TurnAllocateRequest(TurnPort* port); | 61 explicit TurnAllocateRequest(TurnPort* port); |
62 void Prepare(StunMessage* request) override; | 62 void Prepare(StunMessage* request) override; |
63 void OnSent() override; | 63 void OnSent() override; |
64 void OnResponse(StunMessage* response) override; | 64 void OnResponse(StunMessage* response) override; |
65 void OnErrorResponse(StunMessage* response) override; | 65 void OnErrorResponse(StunMessage* response) override; |
66 void OnTimeout() override; | 66 void OnTimeout() override; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 315 } |
316 } | 316 } |
317 } | 317 } |
318 | 318 |
319 bool TurnPort::CreateTurnClientSocket() { | 319 bool TurnPort::CreateTurnClientSocket() { |
320 ASSERT(!socket_ || SharedSocket()); | 320 ASSERT(!socket_ || SharedSocket()); |
321 | 321 |
322 if (server_address_.proto == PROTO_UDP && !SharedSocket()) { | 322 if (server_address_.proto == PROTO_UDP && !SharedSocket()) { |
323 socket_ = socket_factory()->CreateUdpSocket( | 323 socket_ = socket_factory()->CreateUdpSocket( |
324 rtc::SocketAddress(ip(), 0), min_port(), max_port()); | 324 rtc::SocketAddress(ip(), 0), min_port(), max_port()); |
325 } else if (server_address_.proto == PROTO_TCP) { | 325 } else if (server_address_.proto == PROTO_TCP || |
| 326 server_address_.proto == PROTO_TLS) { |
326 ASSERT(!SharedSocket()); | 327 ASSERT(!SharedSocket()); |
327 int opts = rtc::PacketSocketFactory::OPT_STUN; | 328 int opts = rtc::PacketSocketFactory::OPT_STUN; |
328 // If secure bit is enabled in server address, use TLS over TCP. | 329 |
329 if (server_address_.secure) { | 330 // Apply server address TLS and insecure bits to options. |
| 331 if (server_address_.proto == PROTO_TLS) { |
330 opts |= rtc::PacketSocketFactory::OPT_TLS; | 332 opts |= rtc::PacketSocketFactory::OPT_TLS; |
331 } | 333 } |
332 socket_ = socket_factory()->CreateClientTcpSocket( | 334 socket_ = socket_factory()->CreateClientTcpSocket( |
333 rtc::SocketAddress(ip(), 0), server_address_.address, | 335 rtc::SocketAddress(ip(), 0), server_address_.address, |
334 proxy(), user_agent(), opts); | 336 proxy(), user_agent(), opts); |
335 } | 337 } |
336 | 338 |
337 if (!socket_) { | 339 if (!socket_) { |
338 error_ = SOCKET_ERROR; | 340 error_ = SOCKET_ERROR; |
339 return false; | 341 return false; |
340 } | 342 } |
341 | 343 |
342 // Apply options if any. | 344 // Apply options if any. |
343 for (SocketOptionsMap::iterator iter = socket_options_.begin(); | 345 for (SocketOptionsMap::iterator iter = socket_options_.begin(); |
344 iter != socket_options_.end(); ++iter) { | 346 iter != socket_options_.end(); ++iter) { |
345 socket_->SetOption(iter->first, iter->second); | 347 socket_->SetOption(iter->first, iter->second); |
346 } | 348 } |
347 | 349 |
348 if (!SharedSocket()) { | 350 if (!SharedSocket()) { |
349 // If socket is shared, AllocationSequence will receive the packet. | 351 // If socket is shared, AllocationSequence will receive the packet. |
350 socket_->SignalReadPacket.connect(this, &TurnPort::OnReadPacket); | 352 socket_->SignalReadPacket.connect(this, &TurnPort::OnReadPacket); |
351 } | 353 } |
352 | 354 |
353 socket_->SignalReadyToSend.connect(this, &TurnPort::OnReadyToSend); | 355 socket_->SignalReadyToSend.connect(this, &TurnPort::OnReadyToSend); |
354 | 356 |
355 socket_->SignalSentPacket.connect(this, &TurnPort::OnSentPacket); | 357 socket_->SignalSentPacket.connect(this, &TurnPort::OnSentPacket); |
356 | 358 |
357 // TCP port is ready to send stun requests after the socket is connected, | 359 // TCP port is ready to send stun requests after the socket is connected, |
358 // while UDP port is ready to do so once the socket is created. | 360 // while UDP port is ready to do so once the socket is created. |
359 if (server_address_.proto == PROTO_TCP) { | 361 if (server_address_.proto == PROTO_TCP || |
| 362 server_address_.proto == PROTO_TLS) { |
360 socket_->SignalConnect.connect(this, &TurnPort::OnSocketConnect); | 363 socket_->SignalConnect.connect(this, &TurnPort::OnSocketConnect); |
361 socket_->SignalClose.connect(this, &TurnPort::OnSocketClose); | 364 socket_->SignalClose.connect(this, &TurnPort::OnSocketClose); |
362 } else { | 365 } else { |
363 state_ = STATE_CONNECTED; | 366 state_ = STATE_CONNECTED; |
364 } | 367 } |
365 return true; | 368 return true; |
366 } | 369 } |
367 | 370 |
368 void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { | 371 void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { |
369 ASSERT(server_address_.proto == PROTO_TCP); | 372 ASSERT(server_address_.proto == PROTO_TCP); |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 LOG_J(LS_WARNING, this) | 648 LOG_J(LS_WARNING, this) |
646 << "Blocking attempted redirect to loopback address."; | 649 << "Blocking attempted redirect to loopback address."; |
647 return false; | 650 return false; |
648 } | 651 } |
649 | 652 |
650 LOG_J(LS_INFO, this) << "Redirecting from TURN server [" | 653 LOG_J(LS_INFO, this) << "Redirecting from TURN server [" |
651 << server_address_.address.ToSensitiveString() | 654 << server_address_.address.ToSensitiveString() |
652 << "] to TURN server [" | 655 << "] to TURN server [" |
653 << address.ToSensitiveString() | 656 << address.ToSensitiveString() |
654 << "]"; | 657 << "]"; |
655 server_address_ = ProtocolAddress(address, server_address_.proto, | 658 server_address_ = ProtocolAddress(address, server_address_.proto); |
656 server_address_.secure); | |
657 | 659 |
658 // Insert the current address to prevent redirection pingpong. | 660 // Insert the current address to prevent redirection pingpong. |
659 attempted_server_addresses_.insert(server_address_.address); | 661 attempted_server_addresses_.insert(server_address_.address); |
660 return true; | 662 return true; |
661 } | 663 } |
662 | 664 |
663 void TurnPort::ResolveTurnAddress(const rtc::SocketAddress& address) { | 665 void TurnPort::ResolveTurnAddress(const rtc::SocketAddress& address) { |
664 if (resolver_) | 666 if (resolver_) |
665 return; | 667 return; |
666 | 668 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 | 731 |
730 rtc::SocketAddress related_address = stun_address; | 732 rtc::SocketAddress related_address = stun_address; |
731 | 733 |
732 // For relayed candidate, Base is the candidate itself. | 734 // For relayed candidate, Base is the candidate itself. |
733 AddAddress(address, // Candidate address. | 735 AddAddress(address, // Candidate address. |
734 address, // Base address. | 736 address, // Base address. |
735 related_address, // Related address. | 737 related_address, // Related address. |
736 UDP_PROTOCOL_NAME, | 738 UDP_PROTOCOL_NAME, |
737 ProtoToString(server_address_.proto), // The first hop protocol. | 739 ProtoToString(server_address_.proto), // The first hop protocol. |
738 "", // TCP canddiate type, empty for turn candidates. | 740 "", // TCP canddiate type, empty for turn candidates. |
739 RELAY_PORT_TYPE, | 741 RELAY_PORT_TYPE, GetRelayPreference(server_address_.proto), |
740 GetRelayPreference(server_address_.proto, server_address_.secure), | |
741 server_priority_, true); | 742 server_priority_, true); |
742 } | 743 } |
743 | 744 |
744 void TurnPort::OnAllocateError() { | 745 void TurnPort::OnAllocateError() { |
745 // We will send SignalPortError asynchronously as this can be sent during | 746 // We will send SignalPortError asynchronously as this can be sent during |
746 // port initialization. This way it will not be blocking other port | 747 // port initialization. This way it will not be blocking other port |
747 // creation. | 748 // creation. |
748 thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATE_ERROR); | 749 thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATE_ERROR); |
749 } | 750 } |
750 | 751 |
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1542 } else { | 1543 } else { |
1543 state_ = STATE_UNBOUND; | 1544 state_ = STATE_UNBOUND; |
1544 port_->FailAndPruneConnection(ext_addr_); | 1545 port_->FailAndPruneConnection(ext_addr_); |
1545 } | 1546 } |
1546 } | 1547 } |
1547 void TurnEntry::OnChannelBindTimeout() { | 1548 void TurnEntry::OnChannelBindTimeout() { |
1548 state_ = STATE_UNBOUND; | 1549 state_ = STATE_UNBOUND; |
1549 port_->FailAndPruneConnection(ext_addr_); | 1550 port_->FailAndPruneConnection(ext_addr_); |
1550 } | 1551 } |
1551 } // namespace cricket | 1552 } // namespace cricket |
OLD | NEW |