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 |
11 #include "webrtc/p2p/base/turnport.h" | 11 #include "webrtc/p2p/base/turnport.h" |
12 | 12 |
13 #include <functional> | 13 #include <functional> |
14 | 14 |
15 #include "webrtc/p2p/base/common.h" | |
16 #include "webrtc/p2p/base/stun.h" | |
17 #include "webrtc/base/asyncpacketsocket.h" | 15 #include "webrtc/base/asyncpacketsocket.h" |
18 #include "webrtc/base/byteorder.h" | 16 #include "webrtc/base/byteorder.h" |
19 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/nethelpers.h" | 19 #include "webrtc/base/nethelpers.h" |
| 20 #include "webrtc/base/ptr_util.h" |
22 #include "webrtc/base/socketaddress.h" | 21 #include "webrtc/base/socketaddress.h" |
23 #include "webrtc/base/stringencode.h" | 22 #include "webrtc/base/stringencode.h" |
| 23 #include "webrtc/p2p/base/common.h" |
| 24 #include "webrtc/p2p/base/stun.h" |
24 | 25 |
25 namespace cricket { | 26 namespace cricket { |
26 | 27 |
27 // TODO(juberti): Move to stun.h when relay messages have been renamed. | 28 // TODO(juberti): Move to stun.h when relay messages have been renamed. |
28 static const int TURN_ALLOCATE_REQUEST = STUN_ALLOCATE_REQUEST; | 29 static const int TURN_ALLOCATE_REQUEST = STUN_ALLOCATE_REQUEST; |
29 | 30 |
30 // TODO(juberti): Extract to turnmessage.h | 31 // TODO(juberti): Extract to turnmessage.h |
31 static const int TURN_DEFAULT_PORT = 3478; | 32 static const int TURN_DEFAULT_PORT = 3478; |
32 static const int TURN_CHANNEL_NUMBER_START = 0x4000; | 33 static const int TURN_CHANNEL_NUMBER_START = 0x4000; |
33 static const int TURN_PERMISSION_TIMEOUT = 5 * 60 * 1000; // 5 minutes | 34 static const int TURN_PERMISSION_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
(...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 return true; | 922 return true; |
922 } | 923 } |
923 | 924 |
924 void TurnPort::SendRequest(StunRequest* req, int delay) { | 925 void TurnPort::SendRequest(StunRequest* req, int delay) { |
925 request_manager_.SendDelayed(req, delay); | 926 request_manager_.SendDelayed(req, delay); |
926 } | 927 } |
927 | 928 |
928 void TurnPort::AddRequestAuthInfo(StunMessage* msg) { | 929 void TurnPort::AddRequestAuthInfo(StunMessage* msg) { |
929 // If we've gotten the necessary data from the server, add it to our request. | 930 // If we've gotten the necessary data from the server, add it to our request. |
930 RTC_DCHECK(!hash_.empty()); | 931 RTC_DCHECK(!hash_.empty()); |
931 msg->AddAttribute(new StunByteStringAttribute( | 932 msg->AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
932 STUN_ATTR_USERNAME, credentials_.username)); | 933 STUN_ATTR_USERNAME, credentials_.username)); |
933 msg->AddAttribute(new StunByteStringAttribute( | 934 msg->AddAttribute( |
934 STUN_ATTR_REALM, realm_)); | 935 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_REALM, realm_)); |
935 msg->AddAttribute(new StunByteStringAttribute( | 936 msg->AddAttribute( |
936 STUN_ATTR_NONCE, nonce_)); | 937 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_NONCE, nonce_)); |
937 const bool success = msg->AddMessageIntegrity(hash()); | 938 const bool success = msg->AddMessageIntegrity(hash()); |
938 RTC_DCHECK(success); | 939 RTC_DCHECK(success); |
939 } | 940 } |
940 | 941 |
941 int TurnPort::Send(const void* data, size_t len, | 942 int TurnPort::Send(const void* data, size_t len, |
942 const rtc::PacketOptions& options) { | 943 const rtc::PacketOptions& options) { |
943 return socket_->SendTo(data, len, server_address_.address, options); | 944 return socket_->SendTo(data, len, server_address_.address, options); |
944 } | 945 } |
945 | 946 |
946 void TurnPort::UpdateHash() { | 947 void TurnPort::UpdateHash() { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1102 } | 1103 } |
1103 | 1104 |
1104 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) | 1105 TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) |
1105 : StunRequest(new TurnMessage()), | 1106 : StunRequest(new TurnMessage()), |
1106 port_(port) { | 1107 port_(port) { |
1107 } | 1108 } |
1108 | 1109 |
1109 void TurnAllocateRequest::Prepare(StunMessage* request) { | 1110 void TurnAllocateRequest::Prepare(StunMessage* request) { |
1110 // Create the request as indicated in RFC 5766, Section 6.1. | 1111 // Create the request as indicated in RFC 5766, Section 6.1. |
1111 request->SetType(TURN_ALLOCATE_REQUEST); | 1112 request->SetType(TURN_ALLOCATE_REQUEST); |
1112 StunUInt32Attribute* transport_attr = StunAttribute::CreateUInt32( | 1113 auto transport_attr = |
1113 STUN_ATTR_REQUESTED_TRANSPORT); | 1114 StunAttribute::CreateUInt32(STUN_ATTR_REQUESTED_TRANSPORT); |
1114 transport_attr->SetValue(IPPROTO_UDP << 24); | 1115 transport_attr->SetValue(IPPROTO_UDP << 24); |
1115 request->AddAttribute(transport_attr); | 1116 request->AddAttribute(std::move(transport_attr)); |
1116 if (!port_->hash().empty()) { | 1117 if (!port_->hash().empty()) { |
1117 port_->AddRequestAuthInfo(request); | 1118 port_->AddRequestAuthInfo(request); |
1118 } | 1119 } |
1119 } | 1120 } |
1120 | 1121 |
1121 void TurnAllocateRequest::OnSent() { | 1122 void TurnAllocateRequest::OnSent() { |
1122 LOG_J(LS_INFO, port_) << "TURN allocate request sent" | 1123 LOG_J(LS_INFO, port_) << "TURN allocate request sent" |
1123 << ", id=" << rtc::hex_encode(id()); | 1124 << ", id=" << rtc::hex_encode(id()); |
1124 StunRequest::OnSent(); | 1125 StunRequest::OnSent(); |
1125 } | 1126 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1279 : StunRequest(new TurnMessage()), | 1280 : StunRequest(new TurnMessage()), |
1280 port_(port), | 1281 port_(port), |
1281 lifetime_(-1) { | 1282 lifetime_(-1) { |
1282 } | 1283 } |
1283 | 1284 |
1284 void TurnRefreshRequest::Prepare(StunMessage* request) { | 1285 void TurnRefreshRequest::Prepare(StunMessage* request) { |
1285 // Create the request as indicated in RFC 5766, Section 7.1. | 1286 // Create the request as indicated in RFC 5766, Section 7.1. |
1286 // No attributes need to be included. | 1287 // No attributes need to be included. |
1287 request->SetType(TURN_REFRESH_REQUEST); | 1288 request->SetType(TURN_REFRESH_REQUEST); |
1288 if (lifetime_ > -1) { | 1289 if (lifetime_ > -1) { |
1289 request->AddAttribute(new StunUInt32Attribute( | 1290 request->AddAttribute( |
1290 STUN_ATTR_LIFETIME, lifetime_)); | 1291 rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_)); |
1291 } | 1292 } |
1292 | 1293 |
1293 port_->AddRequestAuthInfo(request); | 1294 port_->AddRequestAuthInfo(request); |
1294 } | 1295 } |
1295 | 1296 |
1296 void TurnRefreshRequest::OnSent() { | 1297 void TurnRefreshRequest::OnSent() { |
1297 LOG_J(LS_INFO, port_) << "TURN refresh request sent" | 1298 LOG_J(LS_INFO, port_) << "TURN refresh request sent" |
1298 << ", id=" << rtc::hex_encode(id()); | 1299 << ", id=" << rtc::hex_encode(id()); |
1299 StunRequest::OnSent(); | 1300 StunRequest::OnSent(); |
1300 } | 1301 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1349 port_(port), | 1350 port_(port), |
1350 entry_(entry), | 1351 entry_(entry), |
1351 ext_addr_(ext_addr) { | 1352 ext_addr_(ext_addr) { |
1352 entry_->SignalDestroyed.connect( | 1353 entry_->SignalDestroyed.connect( |
1353 this, &TurnCreatePermissionRequest::OnEntryDestroyed); | 1354 this, &TurnCreatePermissionRequest::OnEntryDestroyed); |
1354 } | 1355 } |
1355 | 1356 |
1356 void TurnCreatePermissionRequest::Prepare(StunMessage* request) { | 1357 void TurnCreatePermissionRequest::Prepare(StunMessage* request) { |
1357 // Create the request as indicated in RFC5766, Section 9.1. | 1358 // Create the request as indicated in RFC5766, Section 9.1. |
1358 request->SetType(TURN_CREATE_PERMISSION_REQUEST); | 1359 request->SetType(TURN_CREATE_PERMISSION_REQUEST); |
1359 request->AddAttribute(new StunXorAddressAttribute( | 1360 request->AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
1360 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); | 1361 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); |
1361 port_->AddRequestAuthInfo(request); | 1362 port_->AddRequestAuthInfo(request); |
1362 } | 1363 } |
1363 | 1364 |
1364 void TurnCreatePermissionRequest::OnSent() { | 1365 void TurnCreatePermissionRequest::OnSent() { |
1365 LOG_J(LS_INFO, port_) << "TURN create permission request sent" | 1366 LOG_J(LS_INFO, port_) << "TURN create permission request sent" |
1366 << ", id=" << rtc::hex_encode(id()); | 1367 << ", id=" << rtc::hex_encode(id()); |
1367 StunRequest::OnSent(); | 1368 StunRequest::OnSent(); |
1368 } | 1369 } |
1369 | 1370 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1410 entry_(entry), | 1411 entry_(entry), |
1411 channel_id_(channel_id), | 1412 channel_id_(channel_id), |
1412 ext_addr_(ext_addr) { | 1413 ext_addr_(ext_addr) { |
1413 entry_->SignalDestroyed.connect( | 1414 entry_->SignalDestroyed.connect( |
1414 this, &TurnChannelBindRequest::OnEntryDestroyed); | 1415 this, &TurnChannelBindRequest::OnEntryDestroyed); |
1415 } | 1416 } |
1416 | 1417 |
1417 void TurnChannelBindRequest::Prepare(StunMessage* request) { | 1418 void TurnChannelBindRequest::Prepare(StunMessage* request) { |
1418 // Create the request as indicated in RFC5766, Section 11.1. | 1419 // Create the request as indicated in RFC5766, Section 11.1. |
1419 request->SetType(TURN_CHANNEL_BIND_REQUEST); | 1420 request->SetType(TURN_CHANNEL_BIND_REQUEST); |
1420 request->AddAttribute(new StunUInt32Attribute( | 1421 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
1421 STUN_ATTR_CHANNEL_NUMBER, channel_id_ << 16)); | 1422 STUN_ATTR_CHANNEL_NUMBER, channel_id_ << 16)); |
1422 request->AddAttribute(new StunXorAddressAttribute( | 1423 request->AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
1423 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); | 1424 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); |
1424 port_->AddRequestAuthInfo(request); | 1425 port_->AddRequestAuthInfo(request); |
1425 } | 1426 } |
1426 | 1427 |
1427 void TurnChannelBindRequest::OnSent() { | 1428 void TurnChannelBindRequest::OnSent() { |
1428 LOG_J(LS_INFO, port_) << "TURN channel bind request sent" | 1429 LOG_J(LS_INFO, port_) << "TURN channel bind request sent" |
1429 << ", id=" << rtc::hex_encode(id()); | 1430 << ", id=" << rtc::hex_encode(id()); |
1430 StunRequest::OnSent(); | 1431 StunRequest::OnSent(); |
1431 } | 1432 } |
1432 | 1433 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1494 | 1495 |
1495 int TurnEntry::Send(const void* data, size_t size, bool payload, | 1496 int TurnEntry::Send(const void* data, size_t size, bool payload, |
1496 const rtc::PacketOptions& options) { | 1497 const rtc::PacketOptions& options) { |
1497 rtc::ByteBufferWriter buf; | 1498 rtc::ByteBufferWriter buf; |
1498 if (state_ != STATE_BOUND) { | 1499 if (state_ != STATE_BOUND) { |
1499 // If we haven't bound the channel yet, we have to use a Send Indication. | 1500 // If we haven't bound the channel yet, we have to use a Send Indication. |
1500 TurnMessage msg; | 1501 TurnMessage msg; |
1501 msg.SetType(TURN_SEND_INDICATION); | 1502 msg.SetType(TURN_SEND_INDICATION); |
1502 msg.SetTransactionID( | 1503 msg.SetTransactionID( |
1503 rtc::CreateRandomString(kStunTransactionIdLength)); | 1504 rtc::CreateRandomString(kStunTransactionIdLength)); |
1504 msg.AddAttribute(new StunXorAddressAttribute( | 1505 msg.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
1505 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); | 1506 STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_)); |
1506 msg.AddAttribute(new StunByteStringAttribute( | 1507 msg.AddAttribute( |
1507 STUN_ATTR_DATA, data, size)); | 1508 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_DATA, data, size)); |
1508 const bool success = msg.Write(&buf); | 1509 const bool success = msg.Write(&buf); |
1509 RTC_DCHECK(success); | 1510 RTC_DCHECK(success); |
1510 | 1511 |
1511 // If we're sending real data, request a channel bind that we can use later. | 1512 // If we're sending real data, request a channel bind that we can use later. |
1512 if (state_ == STATE_UNBOUND && payload) { | 1513 if (state_ == STATE_UNBOUND && payload) { |
1513 SendChannelBindRequest(0); | 1514 SendChannelBindRequest(0); |
1514 state_ = STATE_BINDING; | 1515 state_ = STATE_BINDING; |
1515 } | 1516 } |
1516 } else { | 1517 } else { |
1517 // If the channel is bound, we can send the data as a Channel Message. | 1518 // If the channel is bound, we can send the data as a Channel Message. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1580 } else { | 1581 } else { |
1581 state_ = STATE_UNBOUND; | 1582 state_ = STATE_UNBOUND; |
1582 port_->FailAndPruneConnection(ext_addr_); | 1583 port_->FailAndPruneConnection(ext_addr_); |
1583 } | 1584 } |
1584 } | 1585 } |
1585 void TurnEntry::OnChannelBindTimeout() { | 1586 void TurnEntry::OnChannelBindTimeout() { |
1586 state_ = STATE_UNBOUND; | 1587 state_ = STATE_UNBOUND; |
1587 port_->FailAndPruneConnection(ext_addr_); | 1588 port_->FailAndPruneConnection(ext_addr_); |
1588 } | 1589 } |
1589 } // namespace cricket | 1590 } // namespace cricket |
OLD | NEW |