OLD | NEW |
1 // TODO(pthatcher): Remove this file once Chrome's build files no | 1 /* |
2 // longer refer to it. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/p2p/base/rawtransportchannel.h" |
| 12 |
| 13 #include <string> |
| 14 #include <vector> |
| 15 #include "webrtc/p2p/base/constants.h" |
| 16 #include "webrtc/p2p/base/portallocator.h" |
| 17 #include "webrtc/p2p/base/portinterface.h" |
| 18 #include "webrtc/p2p/base/rawtransport.h" |
| 19 #include "webrtc/p2p/base/relayport.h" |
| 20 #include "webrtc/p2p/base/stunport.h" |
| 21 #include "webrtc/base/common.h" |
| 22 |
| 23 #if defined(FEATURE_ENABLE_PSTN) |
| 24 |
| 25 namespace { |
| 26 |
| 27 const uint32 MSG_DESTROY_RTC_UNUSED_PORTS = 1; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace cricket { |
| 32 |
| 33 RawTransportChannel::RawTransportChannel(const std::string& content_name, |
| 34 int component, |
| 35 RawTransport* transport, |
| 36 rtc::Thread *worker_thread, |
| 37 PortAllocator *allocator) |
| 38 : TransportChannelImpl(content_name, component), |
| 39 raw_transport_(transport), |
| 40 allocator_(allocator), |
| 41 allocator_session_(NULL), |
| 42 stun_port_(NULL), |
| 43 relay_port_(NULL), |
| 44 port_(NULL), |
| 45 use_relay_(false) { |
| 46 if (worker_thread == NULL) |
| 47 worker_thread_ = raw_transport_->worker_thread(); |
| 48 else |
| 49 worker_thread_ = worker_thread; |
| 50 } |
| 51 |
| 52 RawTransportChannel::~RawTransportChannel() { |
| 53 delete allocator_session_; |
| 54 } |
| 55 |
| 56 int RawTransportChannel::SendPacket(const char *data, size_t size, |
| 57 const rtc::PacketOptions& options, |
| 58 int flags) { |
| 59 if (port_ == NULL) |
| 60 return -1; |
| 61 if (remote_address_.IsNil()) |
| 62 return -1; |
| 63 if (flags != 0) |
| 64 return -1; |
| 65 return port_->SendTo(data, size, remote_address_, options, true); |
| 66 } |
| 67 |
| 68 int RawTransportChannel::SetOption(rtc::Socket::Option opt, int value) { |
| 69 // TODO: allow these to be set before we have a port |
| 70 if (port_ == NULL) |
| 71 return -1; |
| 72 return port_->SetOption(opt, value); |
| 73 } |
| 74 |
| 75 bool RawTransportChannel::GetOption(rtc::Socket::Option opt, int* value) { |
| 76 return false; |
| 77 } |
| 78 |
| 79 int RawTransportChannel::GetError() { |
| 80 return (port_ != NULL) ? port_->GetError() : 0; |
| 81 } |
| 82 |
| 83 void RawTransportChannel::Connect() { |
| 84 // Create an allocator that only returns stun and relay ports. |
| 85 // Use empty string for ufrag and pwd here. There won't be any STUN or relay |
| 86 // interactions when using RawTC. |
| 87 // TODO: Change raw to only use local udp ports. |
| 88 allocator_session_ = allocator_->CreateSession( |
| 89 SessionId(), content_name(), component(), "", ""); |
| 90 |
| 91 uint32 flags = PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP; |
| 92 |
| 93 #if !defined(FEATURE_ENABLE_STUN_CLASSIFICATION) |
| 94 flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 95 #endif |
| 96 allocator_session_->set_flags(flags); |
| 97 allocator_session_->SignalPortReady.connect( |
| 98 this, &RawTransportChannel::OnPortReady); |
| 99 allocator_session_->SignalCandidatesReady.connect( |
| 100 this, &RawTransportChannel::OnCandidatesReady); |
| 101 |
| 102 // The initial ports will include stun. |
| 103 allocator_session_->StartGettingPorts(); |
| 104 } |
| 105 |
| 106 void RawTransportChannel::Reset() { |
| 107 set_readable(false); |
| 108 set_writable(false); |
| 109 |
| 110 delete allocator_session_; |
| 111 |
| 112 allocator_session_ = NULL; |
| 113 stun_port_ = NULL; |
| 114 relay_port_ = NULL; |
| 115 port_ = NULL; |
| 116 remote_address_ = rtc::SocketAddress(); |
| 117 } |
| 118 |
| 119 void RawTransportChannel::OnCandidate(const Candidate& candidate) { |
| 120 remote_address_ = candidate.address(); |
| 121 ASSERT(!remote_address_.IsNil()); |
| 122 set_readable(true); |
| 123 |
| 124 // We can write once we have a port and a remote address. |
| 125 if (port_ != NULL) |
| 126 SetWritable(); |
| 127 } |
| 128 |
| 129 void RawTransportChannel::OnRemoteAddress( |
| 130 const rtc::SocketAddress& remote_address) { |
| 131 remote_address_ = remote_address; |
| 132 set_readable(true); |
| 133 |
| 134 if (port_ != NULL) |
| 135 SetWritable(); |
| 136 } |
| 137 |
| 138 // Note about stun classification |
| 139 // Code to classify our NAT type and use the relay port if we are behind an |
| 140 // asymmetric NAT is under a FEATURE_ENABLE_STUN_CLASSIFICATION #define. |
| 141 // To turn this one we will have to enable a second stun address and make sure |
| 142 // that the relay server works for raw UDP. |
| 143 // |
| 144 // Another option is to classify the NAT type early and not offer the raw |
| 145 // transport type at all if we can't support it. |
| 146 |
| 147 void RawTransportChannel::OnPortReady( |
| 148 PortAllocatorSession* session, PortInterface* port) { |
| 149 ASSERT(session == allocator_session_); |
| 150 |
| 151 if (port->Type() == STUN_PORT_TYPE) { |
| 152 stun_port_ = static_cast<StunPort*>(port); |
| 153 } else if (port->Type() == RELAY_PORT_TYPE) { |
| 154 relay_port_ = static_cast<RelayPort*>(port); |
| 155 } else { |
| 156 ASSERT(false); |
| 157 } |
| 158 } |
| 159 |
| 160 void RawTransportChannel::OnCandidatesReady( |
| 161 PortAllocatorSession *session, const std::vector<Candidate>& candidates) { |
| 162 ASSERT(session == allocator_session_); |
| 163 ASSERT(candidates.size() >= 1); |
| 164 |
| 165 // The most recent candidate is the one we haven't seen yet. |
| 166 Candidate c = candidates[candidates.size() - 1]; |
| 167 |
| 168 if (c.type() == STUN_PORT_TYPE) { |
| 169 ASSERT(stun_port_ != NULL); |
| 170 |
| 171 #if defined(FEATURE_ENABLE_STUN_CLASSIFICATION) |
| 172 // We need to wait until we have two addresses. |
| 173 if (stun_port_->candidates().size() < 2) |
| 174 return; |
| 175 |
| 176 // This is the second address. If these addresses are the same, then we |
| 177 // are not behind a symmetric NAT. Hence, a stun port should be sufficient. |
| 178 if (stun_port_->candidates()[0].address() == |
| 179 stun_port_->candidates()[1].address()) { |
| 180 SetPort(stun_port_); |
| 181 return; |
| 182 } |
| 183 |
| 184 // We will need to use relay. |
| 185 use_relay_ = true; |
| 186 |
| 187 // If we already have a relay address, we're good. Otherwise, we will need |
| 188 // to wait until one arrives. |
| 189 if (relay_port_->candidates().size() > 0) |
| 190 SetPort(relay_port_); |
| 191 #else // defined(FEATURE_ENABLE_STUN_CLASSIFICATION) |
| 192 // Always use the stun port. We don't classify right now so just assume it |
| 193 // will work fine. |
| 194 SetPort(stun_port_); |
| 195 #endif |
| 196 } else if (c.type() == RELAY_PORT_TYPE) { |
| 197 if (use_relay_) |
| 198 SetPort(relay_port_); |
| 199 } else { |
| 200 ASSERT(false); |
| 201 } |
| 202 } |
| 203 |
| 204 void RawTransportChannel::SetPort(PortInterface* port) { |
| 205 ASSERT(port_ == NULL); |
| 206 port_ = port; |
| 207 |
| 208 // We don't need any ports other than the one we picked. |
| 209 allocator_session_->StopGettingPorts(); |
| 210 worker_thread_->Post( |
| 211 this, MSG_DESTROY_RTC_UNUSED_PORTS, NULL); |
| 212 |
| 213 // Send a message to the other client containing our address. |
| 214 |
| 215 ASSERT(port_->Candidates().size() >= 1); |
| 216 ASSERT(port_->Candidates()[0].protocol() == "udp"); |
| 217 SignalCandidateReady(this, port_->Candidates()[0]); |
| 218 |
| 219 // Read all packets from this port. |
| 220 port_->EnablePortPackets(); |
| 221 port_->SignalReadPacket.connect(this, &RawTransportChannel::OnReadPacket); |
| 222 |
| 223 // We can write once we have a port and a remote address. |
| 224 if (!remote_address_.IsAny()) |
| 225 SetWritable(); |
| 226 } |
| 227 |
| 228 void RawTransportChannel::SetWritable() { |
| 229 ASSERT(port_ != NULL); |
| 230 ASSERT(!remote_address_.IsAny()); |
| 231 |
| 232 set_writable(true); |
| 233 |
| 234 Candidate remote_candidate; |
| 235 remote_candidate.set_address(remote_address_); |
| 236 SignalRouteChange(this, remote_candidate); |
| 237 } |
| 238 |
| 239 void RawTransportChannel::OnReadPacket( |
| 240 PortInterface* port, const char* data, size_t size, |
| 241 const rtc::SocketAddress& addr) { |
| 242 ASSERT(port_ == port); |
| 243 SignalReadPacket(this, data, size, rtc::CreatePacketTime(0), 0); |
| 244 } |
| 245 |
| 246 void RawTransportChannel::OnMessage(rtc::Message* msg) { |
| 247 ASSERT(msg->message_id == MSG_DESTROY_RTC_UNUSED_PORTS); |
| 248 ASSERT(port_ != NULL); |
| 249 if (port_ != stun_port_) { |
| 250 stun_port_->Destroy(); |
| 251 stun_port_ = NULL; |
| 252 } |
| 253 if (port_ != relay_port_ && relay_port_ != NULL) { |
| 254 relay_port_->Destroy(); |
| 255 relay_port_ = NULL; |
| 256 } |
| 257 } |
| 258 |
| 259 } // namespace cricket |
| 260 #endif // defined(FEATURE_ENABLE_PSTN) |
OLD | NEW |