henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/turnserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
Taylor Brandstetter | 734262c | 2016-08-01 16:37:14 -0700 | [diff] [blame] | 13 | #include <tuple> // for std::tie |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 14 | #include <utility> |
Taylor Brandstetter | 734262c | 2016-08-01 16:37:14 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "p2p/base/asyncstuntcpsocket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "p2p/base/packetsocketfactory.h" |
| 18 | #include "p2p/base/stun.h" |
| 19 | #include "rtc_base/bind.h" |
| 20 | #include "rtc_base/bytebuffer.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/helpers.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/messagedigest.h" |
| 25 | #include "rtc_base/ptr_util.h" |
| 26 | #include "rtc_base/socketadapters.h" |
| 27 | #include "rtc_base/stringencode.h" |
| 28 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 29 | |
| 30 | namespace cricket { |
| 31 | |
| 32 | // TODO(juberti): Move this all to a future turnmessage.h |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 33 | // static const int IPPROTO_UDP = 17; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 34 | static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes |
| 35 | static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes |
| 36 | static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes |
| 37 | static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes |
| 38 | |
| 39 | static const int kMinChannelNumber = 0x4000; |
| 40 | static const int kMaxChannelNumber = 0x7FFF; |
| 41 | |
| 42 | static const size_t kNonceKeySize = 16; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 43 | static const size_t kNonceSize = 48; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | |
| 45 | static const size_t TURN_CHANNEL_HEADER_SIZE = 4U; |
| 46 | |
| 47 | // TODO(mallinath) - Move these to a common place. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 48 | inline bool IsTurnChannelData(uint16_t msg_type) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | // The first two bits of a channel data message are 0b01. |
| 50 | return ((msg_type & 0xC000) == 0x4000); |
| 51 | } |
| 52 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 53 | // IDs used for posted messages for TurnServerAllocation. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | enum { |
| 55 | MSG_ALLOCATION_TIMEOUT, |
| 56 | }; |
| 57 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | // Encapsulates a TURN permission. |
| 59 | // The object is created when a create permission request is received by an |
| 60 | // allocation, and self-deletes when its lifetime timer expires. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 61 | class TurnServerAllocation::Permission : public rtc::MessageHandler { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | public: |
| 63 | Permission(rtc::Thread* thread, const rtc::IPAddress& peer); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 64 | ~Permission() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | |
| 66 | const rtc::IPAddress& peer() const { return peer_; } |
| 67 | void Refresh(); |
| 68 | |
| 69 | sigslot::signal1<Permission*> SignalDestroyed; |
| 70 | |
| 71 | private: |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 72 | void OnMessage(rtc::Message* msg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | |
| 74 | rtc::Thread* thread_; |
| 75 | rtc::IPAddress peer_; |
| 76 | }; |
| 77 | |
| 78 | // Encapsulates a TURN channel binding. |
| 79 | // The object is created when a channel bind request is received by an |
| 80 | // allocation, and self-deletes when its lifetime timer expires. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 81 | class TurnServerAllocation::Channel : public rtc::MessageHandler { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | public: |
| 83 | Channel(rtc::Thread* thread, int id, |
| 84 | const rtc::SocketAddress& peer); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 85 | ~Channel() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 86 | |
| 87 | int id() const { return id_; } |
| 88 | const rtc::SocketAddress& peer() const { return peer_; } |
| 89 | void Refresh(); |
| 90 | |
| 91 | sigslot::signal1<Channel*> SignalDestroyed; |
| 92 | |
| 93 | private: |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 94 | void OnMessage(rtc::Message* msg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 95 | |
| 96 | rtc::Thread* thread_; |
| 97 | int id_; |
| 98 | rtc::SocketAddress peer_; |
| 99 | }; |
| 100 | |
| 101 | static bool InitResponse(const StunMessage* req, StunMessage* resp) { |
| 102 | int resp_type = (req) ? GetStunSuccessResponseType(req->type()) : -1; |
| 103 | if (resp_type == -1) |
| 104 | return false; |
| 105 | resp->SetType(resp_type); |
| 106 | resp->SetTransactionID(req->transaction_id()); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | static bool InitErrorResponse(const StunMessage* req, int code, |
| 111 | const std::string& reason, StunMessage* resp) { |
| 112 | int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1; |
| 113 | if (resp_type == -1) |
| 114 | return false; |
| 115 | resp->SetType(resp_type); |
| 116 | resp->SetTransactionID(req->transaction_id()); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 117 | resp->AddAttribute(rtc::MakeUnique<cricket::StunErrorCodeAttribute>( |
nisse | cc99bc2 | 2017-02-02 01:31:30 -0800 | [diff] [blame] | 118 | STUN_ATTR_ERROR_CODE, code, reason)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 122 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | TurnServer::TurnServer(rtc::Thread* thread) |
| 124 | : thread_(thread), |
| 125 | nonce_key_(rtc::CreateRandomString(kNonceKeySize)), |
| 126 | auth_hook_(NULL), |
| 127 | redirect_hook_(NULL), |
| 128 | enable_otu_nonce_(false) { |
| 129 | } |
| 130 | |
| 131 | TurnServer::~TurnServer() { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 132 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | for (InternalSocketMap::iterator it = server_sockets_.begin(); |
| 134 | it != server_sockets_.end(); ++it) { |
| 135 | rtc::AsyncPacketSocket* socket = it->first; |
| 136 | delete socket; |
| 137 | } |
| 138 | |
| 139 | for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); |
| 140 | it != server_listen_sockets_.end(); ++it) { |
| 141 | rtc::AsyncSocket* socket = it->first; |
| 142 | delete socket; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void TurnServer::AddInternalSocket(rtc::AsyncPacketSocket* socket, |
| 147 | ProtocolType proto) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 148 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 149 | RTC_DCHECK(server_sockets_.end() == server_sockets_.find(socket)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 150 | server_sockets_[socket] = proto; |
| 151 | socket->SignalReadPacket.connect(this, &TurnServer::OnInternalPacket); |
| 152 | } |
| 153 | |
| 154 | void TurnServer::AddInternalServerSocket(rtc::AsyncSocket* socket, |
| 155 | ProtocolType proto) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 156 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 157 | RTC_DCHECK(server_listen_sockets_.end() == |
| 158 | server_listen_sockets_.find(socket)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | server_listen_sockets_[socket] = proto; |
| 160 | socket->SignalReadEvent.connect(this, &TurnServer::OnNewInternalConnection); |
| 161 | } |
| 162 | |
| 163 | void TurnServer::SetExternalSocketFactory( |
| 164 | rtc::PacketSocketFactory* factory, |
| 165 | const rtc::SocketAddress& external_addr) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 166 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 167 | external_socket_factory_.reset(factory); |
| 168 | external_addr_ = external_addr; |
| 169 | } |
| 170 | |
| 171 | void TurnServer::OnNewInternalConnection(rtc::AsyncSocket* socket) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 172 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 173 | RTC_DCHECK(server_listen_sockets_.find(socket) != |
| 174 | server_listen_sockets_.end()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 175 | AcceptConnection(socket); |
| 176 | } |
| 177 | |
| 178 | void TurnServer::AcceptConnection(rtc::AsyncSocket* server_socket) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 179 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 180 | // Check if someone is trying to connect to us. |
| 181 | rtc::SocketAddress accept_addr; |
| 182 | rtc::AsyncSocket* accepted_socket = server_socket->Accept(&accept_addr); |
| 183 | if (accepted_socket != NULL) { |
| 184 | ProtocolType proto = server_listen_sockets_[server_socket]; |
| 185 | cricket::AsyncStunTCPSocket* tcp_socket = |
| 186 | new cricket::AsyncStunTCPSocket(accepted_socket, false); |
| 187 | |
| 188 | tcp_socket->SignalClose.connect(this, &TurnServer::OnInternalSocketClose); |
| 189 | // Finally add the socket so it can start communicating with the client. |
| 190 | AddInternalSocket(tcp_socket, proto); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void TurnServer::OnInternalSocketClose(rtc::AsyncPacketSocket* socket, |
| 195 | int err) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 196 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 197 | DestroyInternalSocket(socket); |
| 198 | } |
| 199 | |
| 200 | void TurnServer::OnInternalPacket(rtc::AsyncPacketSocket* socket, |
| 201 | const char* data, size_t size, |
| 202 | const rtc::SocketAddress& addr, |
| 203 | const rtc::PacketTime& packet_time) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 204 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 205 | // Fail if the packet is too small to even contain a channel header. |
| 206 | if (size < TURN_CHANNEL_HEADER_SIZE) { |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 207 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 208 | } |
| 209 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 210 | RTC_DCHECK(iter != server_sockets_.end()); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 211 | TurnServerConnection conn(addr, iter->second, socket); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 212 | uint16_t msg_type = rtc::GetBE16(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | if (!IsTurnChannelData(msg_type)) { |
| 214 | // This is a STUN message. |
| 215 | HandleStunMessage(&conn, data, size); |
| 216 | } else { |
| 217 | // This is a channel message; let the allocation handle it. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 218 | TurnServerAllocation* allocation = FindAllocation(&conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 219 | if (allocation) { |
| 220 | allocation->HandleChannelData(data, size); |
| 221 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 222 | if (stun_message_observer_ != nullptr) { |
| 223 | stun_message_observer_->ReceivedChannelData(data, size); |
| 224 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 228 | void TurnServer::HandleStunMessage(TurnServerConnection* conn, const char* data, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | size_t size) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 230 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | TurnMessage msg; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 232 | rtc::ByteBufferReader buf(data, size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 233 | if (!msg.Read(&buf) || (buf.Length() > 0)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 234 | RTC_LOG(LS_WARNING) << "Received invalid STUN message"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 238 | if (stun_message_observer_ != nullptr) { |
| 239 | stun_message_observer_->ReceivedMessage(&msg); |
| 240 | } |
| 241 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 242 | // If it's a STUN binding request, handle that specially. |
| 243 | if (msg.type() == STUN_BINDING_REQUEST) { |
| 244 | HandleBindingRequest(conn, &msg); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (redirect_hook_ != NULL && msg.type() == STUN_ALLOCATE_REQUEST) { |
| 249 | rtc::SocketAddress address; |
| 250 | if (redirect_hook_->ShouldRedirect(conn->src(), &address)) { |
| 251 | SendErrorResponseWithAlternateServer( |
| 252 | conn, &msg, address); |
| 253 | return; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Look up the key that we'll use to validate the M-I. If we have an |
| 258 | // existing allocation, the key will already be cached. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 259 | TurnServerAllocation* allocation = FindAllocation(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 260 | std::string key; |
| 261 | if (!allocation) { |
| 262 | GetKey(&msg, &key); |
| 263 | } else { |
| 264 | key = allocation->key(); |
| 265 | } |
| 266 | |
| 267 | // Ensure the message is authorized; only needed for requests. |
| 268 | if (IsStunRequestType(msg.type())) { |
| 269 | if (!CheckAuthorization(conn, &msg, data, size, key)) { |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (!allocation && msg.type() == STUN_ALLOCATE_REQUEST) { |
| 275 | HandleAllocateRequest(conn, &msg, key); |
| 276 | } else if (allocation && |
| 277 | (msg.type() != STUN_ALLOCATE_REQUEST || |
| 278 | msg.transaction_id() == allocation->transaction_id())) { |
| 279 | // This is a non-allocate request, or a retransmit of an allocate. |
| 280 | // Check that the username matches the previous username used. |
| 281 | if (IsStunRequestType(msg.type()) && |
| 282 | msg.GetByteString(STUN_ATTR_USERNAME)->GetString() != |
| 283 | allocation->username()) { |
| 284 | SendErrorResponse(conn, &msg, STUN_ERROR_WRONG_CREDENTIALS, |
| 285 | STUN_ERROR_REASON_WRONG_CREDENTIALS); |
| 286 | return; |
| 287 | } |
| 288 | allocation->HandleTurnMessage(&msg); |
| 289 | } else { |
| 290 | // Allocation mismatch. |
| 291 | SendErrorResponse(conn, &msg, STUN_ERROR_ALLOCATION_MISMATCH, |
| 292 | STUN_ERROR_REASON_ALLOCATION_MISMATCH); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 297 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 298 | const StunByteStringAttribute* username_attr = |
| 299 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 300 | if (!username_attr) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | std::string username = username_attr->GetString(); |
| 305 | return (auth_hook_ != NULL && auth_hook_->GetKey(username, realm_, key)); |
| 306 | } |
| 307 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 308 | bool TurnServer::CheckAuthorization(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 309 | const StunMessage* msg, |
| 310 | const char* data, size_t size, |
| 311 | const std::string& key) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 312 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 313 | // RFC 5389, 10.2.2. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 314 | RTC_DCHECK(IsStunRequestType(msg->type())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | const StunByteStringAttribute* mi_attr = |
| 316 | msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 317 | const StunByteStringAttribute* username_attr = |
| 318 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 319 | const StunByteStringAttribute* realm_attr = |
| 320 | msg->GetByteString(STUN_ATTR_REALM); |
| 321 | const StunByteStringAttribute* nonce_attr = |
| 322 | msg->GetByteString(STUN_ATTR_NONCE); |
| 323 | |
| 324 | // Fail if no M-I. |
| 325 | if (!mi_attr) { |
| 326 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_UNAUTHORIZED, |
| 327 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | // Fail if there is M-I but no username, nonce, or realm. |
| 332 | if (!username_attr || !realm_attr || !nonce_attr) { |
| 333 | SendErrorResponse(conn, msg, STUN_ERROR_BAD_REQUEST, |
| 334 | STUN_ERROR_REASON_BAD_REQUEST); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | // Fail if bad nonce. |
| 339 | if (!ValidateNonce(nonce_attr->GetString())) { |
| 340 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_STALE_NONCE, |
| 341 | STUN_ERROR_REASON_STALE_NONCE); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | // Fail if bad username or M-I. |
| 346 | // We need |data| and |size| for the call to ValidateMessageIntegrity. |
| 347 | if (key.empty() || !StunMessage::ValidateMessageIntegrity(data, size, key)) { |
| 348 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_UNAUTHORIZED, |
| 349 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | // Fail if one-time-use nonce feature is enabled. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 354 | TurnServerAllocation* allocation = FindAllocation(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 355 | if (enable_otu_nonce_ && allocation && |
| 356 | allocation->last_nonce() == nonce_attr->GetString()) { |
| 357 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_STALE_NONCE, |
| 358 | STUN_ERROR_REASON_STALE_NONCE); |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | if (allocation) { |
| 363 | allocation->set_last_nonce(nonce_attr->GetString()); |
| 364 | } |
| 365 | // Success. |
| 366 | return true; |
| 367 | } |
| 368 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 369 | void TurnServer::HandleBindingRequest(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 370 | const StunMessage* req) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 371 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 372 | StunMessage response; |
| 373 | InitResponse(req, &response); |
| 374 | |
| 375 | // Tell the user the address that we received their request from. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 376 | auto mapped_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 377 | STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src()); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 378 | response.AddAttribute(std::move(mapped_addr_attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 379 | |
| 380 | SendStun(conn, &response); |
| 381 | } |
| 382 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 383 | void TurnServer::HandleAllocateRequest(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | const TurnMessage* msg, |
| 385 | const std::string& key) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 386 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 387 | // Check the parameters in the request. |
| 388 | const StunUInt32Attribute* transport_attr = |
| 389 | msg->GetUInt32(STUN_ATTR_REQUESTED_TRANSPORT); |
| 390 | if (!transport_attr) { |
| 391 | SendErrorResponse(conn, msg, STUN_ERROR_BAD_REQUEST, |
| 392 | STUN_ERROR_REASON_BAD_REQUEST); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // Only UDP is supported right now. |
| 397 | int proto = transport_attr->value() >> 24; |
| 398 | if (proto != IPPROTO_UDP) { |
| 399 | SendErrorResponse(conn, msg, STUN_ERROR_UNSUPPORTED_PROTOCOL, |
| 400 | STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | // Create the allocation and let it send the success response. |
| 405 | // If the actual socket allocation fails, send an internal error. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 406 | TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 407 | if (alloc) { |
| 408 | alloc->HandleTurnMessage(msg); |
| 409 | } else { |
| 410 | SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, |
| 411 | "Failed to allocate socket"); |
| 412 | } |
| 413 | } |
| 414 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 415 | std::string TurnServer::GenerateNonce(int64_t now) const { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 416 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 417 | // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 418 | std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); |
| 419 | std::string nonce = rtc::hex_encode(input.c_str(), input.size()); |
| 420 | nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 421 | RTC_DCHECK(nonce.size() == kNonceSize); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 422 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 423 | return nonce; |
| 424 | } |
| 425 | |
| 426 | bool TurnServer::ValidateNonce(const std::string& nonce) const { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 427 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 428 | // Check the size. |
| 429 | if (nonce.size() != kNonceSize) { |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | // Decode the timestamp. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 434 | int64_t then; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 435 | char* p = reinterpret_cast<char*>(&then); |
| 436 | size_t len = rtc::hex_decode(p, sizeof(then), |
| 437 | nonce.substr(0, sizeof(then) * 2)); |
| 438 | if (len != sizeof(then)) { |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // Verify the HMAC. |
| 443 | if (nonce.substr(sizeof(then) * 2) != rtc::ComputeHmac( |
| 444 | rtc::DIGEST_MD5, nonce_key_, std::string(p, sizeof(then)))) { |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Validate the timestamp. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 449 | return rtc::TimeMillis() - then < kNonceTimeout; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 450 | } |
| 451 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 452 | TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 453 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 454 | AllocationMap::const_iterator it = allocations_.find(*conn); |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 455 | return (it != allocations_.end()) ? it->second.get() : nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 456 | } |
| 457 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 458 | TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, |
| 459 | int proto, |
| 460 | const std::string& key) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 461 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 462 | rtc::AsyncPacketSocket* external_socket = (external_socket_factory_) ? |
| 463 | external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) : NULL; |
| 464 | if (!external_socket) { |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | // The Allocation takes ownership of the socket. |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 469 | TurnServerAllocation* allocation = new TurnServerAllocation(this, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 470 | thread_, *conn, external_socket, key); |
| 471 | allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 472 | allocations_[*conn].reset(allocation); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 473 | return allocation; |
| 474 | } |
| 475 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 476 | void TurnServer::SendErrorResponse(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 477 | const StunMessage* req, |
| 478 | int code, const std::string& reason) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 479 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 480 | TurnMessage resp; |
| 481 | InitErrorResponse(req, code, reason, &resp); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 482 | RTC_LOG(LS_INFO) << "Sending error response, type=" << resp.type() |
| 483 | << ", code=" << code << ", reason=" << reason; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 484 | SendStun(conn, &resp); |
| 485 | } |
| 486 | |
| 487 | void TurnServer::SendErrorResponseWithRealmAndNonce( |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 488 | TurnServerConnection* conn, const StunMessage* msg, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 489 | int code, const std::string& reason) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 490 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 491 | TurnMessage resp; |
| 492 | InitErrorResponse(msg, code, reason, &resp); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 493 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 494 | int64_t timestamp = rtc::TimeMillis(); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 495 | if (ts_for_next_nonce_) { |
| 496 | timestamp = ts_for_next_nonce_; |
| 497 | ts_for_next_nonce_ = 0; |
| 498 | } |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 499 | resp.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 500 | STUN_ATTR_NONCE, GenerateNonce(timestamp))); |
nisse | cc99bc2 | 2017-02-02 01:31:30 -0800 | [diff] [blame] | 501 | resp.AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 502 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_REALM, realm_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 503 | SendStun(conn, &resp); |
| 504 | } |
| 505 | |
| 506 | void TurnServer::SendErrorResponseWithAlternateServer( |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 507 | TurnServerConnection* conn, const StunMessage* msg, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 508 | const rtc::SocketAddress& addr) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 509 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 510 | TurnMessage resp; |
| 511 | InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, |
| 512 | STUN_ERROR_REASON_TRY_ALTERNATE_SERVER, &resp); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 513 | resp.AddAttribute( |
| 514 | rtc::MakeUnique<StunAddressAttribute>(STUN_ATTR_ALTERNATE_SERVER, addr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 515 | SendStun(conn, &resp); |
| 516 | } |
| 517 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 518 | void TurnServer::SendStun(TurnServerConnection* conn, StunMessage* msg) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 519 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 520 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 521 | // Add a SOFTWARE attribute if one is set. |
| 522 | if (!software_.empty()) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 523 | msg->AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 524 | STUN_ATTR_SOFTWARE, software_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 525 | } |
| 526 | msg->Write(&buf); |
| 527 | Send(conn, buf); |
| 528 | } |
| 529 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 530 | void TurnServer::Send(TurnServerConnection* conn, |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 531 | const rtc::ByteBufferWriter& buf) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 532 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 533 | rtc::PacketOptions options; |
| 534 | conn->socket()->SendTo(buf.Data(), buf.Length(), conn->src(), options); |
| 535 | } |
| 536 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 537 | void TurnServer::OnAllocationDestroyed(TurnServerAllocation* allocation) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 538 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 539 | // Removing the internal socket if the connection is not udp. |
| 540 | rtc::AsyncPacketSocket* socket = allocation->conn()->socket(); |
| 541 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 542 | // Skip if the socket serving this allocation is UDP, as this will be shared |
| 543 | // by all allocations. |
Taylor Brandstetter | 716d07a | 2016-06-27 14:07:41 -0700 | [diff] [blame] | 544 | // Note: We may not find a socket if it's a TCP socket that was closed, and |
| 545 | // the allocation is only now timing out. |
| 546 | if (iter != server_sockets_.end() && iter->second != cricket::PROTO_UDP) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 547 | DestroyInternalSocket(socket); |
| 548 | } |
| 549 | |
| 550 | AllocationMap::iterator it = allocations_.find(*(allocation->conn())); |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 551 | if (it != allocations_.end()) { |
| 552 | it->second.release(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 553 | allocations_.erase(it); |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 554 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 558 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 559 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 560 | if (iter != server_sockets_.end()) { |
| 561 | rtc::AsyncPacketSocket* socket = iter->first; |
Qingsi Wang | 0ea7515 | 2018-07-02 10:48:25 -0700 | [diff] [blame^] | 562 | socket->SignalReadPacket.disconnect(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 563 | server_sockets_.erase(iter); |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 564 | // We must destroy the socket async to avoid invalidating the sigslot |
| 565 | // callback list iterator inside a sigslot callback. (In other words, |
| 566 | // deleting an object from within a callback from that object). |
| 567 | sockets_to_delete_.push_back( |
| 568 | std::unique_ptr<rtc::AsyncPacketSocket>(socket)); |
| 569 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, rtc::Thread::Current(), |
| 570 | rtc::Bind(&TurnServer::FreeSockets, this)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 574 | void TurnServer::FreeSockets() { |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 575 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 576 | sockets_to_delete_.clear(); |
| 577 | } |
| 578 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 579 | TurnServerConnection::TurnServerConnection(const rtc::SocketAddress& src, |
| 580 | ProtocolType proto, |
| 581 | rtc::AsyncPacketSocket* socket) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 582 | : src_(src), |
| 583 | dst_(socket->GetRemoteAddress()), |
| 584 | proto_(proto), |
| 585 | socket_(socket) { |
| 586 | } |
| 587 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 588 | bool TurnServerConnection::operator==(const TurnServerConnection& c) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 589 | return src_ == c.src_ && dst_ == c.dst_ && proto_ == c.proto_; |
| 590 | } |
| 591 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 592 | bool TurnServerConnection::operator<(const TurnServerConnection& c) const { |
Taylor Brandstetter | 734262c | 2016-08-01 16:37:14 -0700 | [diff] [blame] | 593 | return std::tie(src_, dst_, proto_) < std::tie(c.src_, c.dst_, c.proto_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 594 | } |
| 595 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 596 | std::string TurnServerConnection::ToString() const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 597 | const char* const kProtos[] = { |
| 598 | "unknown", "udp", "tcp", "ssltcp" |
| 599 | }; |
| 600 | std::ostringstream ost; |
| 601 | ost << src_.ToString() << "-" << dst_.ToString() << ":"<< kProtos[proto_]; |
| 602 | return ost.str(); |
| 603 | } |
| 604 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 605 | TurnServerAllocation::TurnServerAllocation(TurnServer* server, |
| 606 | rtc::Thread* thread, |
| 607 | const TurnServerConnection& conn, |
| 608 | rtc::AsyncPacketSocket* socket, |
| 609 | const std::string& key) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 610 | : server_(server), |
| 611 | thread_(thread), |
| 612 | conn_(conn), |
| 613 | external_socket_(socket), |
| 614 | key_(key) { |
| 615 | external_socket_->SignalReadPacket.connect( |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 616 | this, &TurnServerAllocation::OnExternalPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 617 | } |
| 618 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 619 | TurnServerAllocation::~TurnServerAllocation() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 620 | for (ChannelList::iterator it = channels_.begin(); |
| 621 | it != channels_.end(); ++it) { |
| 622 | delete *it; |
| 623 | } |
| 624 | for (PermissionList::iterator it = perms_.begin(); |
| 625 | it != perms_.end(); ++it) { |
| 626 | delete *it; |
| 627 | } |
| 628 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 629 | RTC_LOG(LS_INFO) << ToString() << ": Allocation destroyed"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 630 | } |
| 631 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 632 | std::string TurnServerAllocation::ToString() const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 633 | std::ostringstream ost; |
| 634 | ost << "Alloc[" << conn_.ToString() << "]"; |
| 635 | return ost.str(); |
| 636 | } |
| 637 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 638 | void TurnServerAllocation::HandleTurnMessage(const TurnMessage* msg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 639 | RTC_DCHECK(msg != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 640 | switch (msg->type()) { |
| 641 | case STUN_ALLOCATE_REQUEST: |
| 642 | HandleAllocateRequest(msg); |
| 643 | break; |
| 644 | case TURN_REFRESH_REQUEST: |
| 645 | HandleRefreshRequest(msg); |
| 646 | break; |
| 647 | case TURN_SEND_INDICATION: |
| 648 | HandleSendIndication(msg); |
| 649 | break; |
| 650 | case TURN_CREATE_PERMISSION_REQUEST: |
| 651 | HandleCreatePermissionRequest(msg); |
| 652 | break; |
| 653 | case TURN_CHANNEL_BIND_REQUEST: |
| 654 | HandleChannelBindRequest(msg); |
| 655 | break; |
| 656 | default: |
| 657 | // Not sure what to do with this, just eat it. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 658 | RTC_LOG(LS_WARNING) << ToString() |
| 659 | << ": Invalid TURN message type received: " |
| 660 | << msg->type(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 664 | void TurnServerAllocation::HandleAllocateRequest(const TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 665 | // Copy the important info from the allocate request. |
| 666 | transaction_id_ = msg->transaction_id(); |
| 667 | const StunByteStringAttribute* username_attr = |
| 668 | msg->GetByteString(STUN_ATTR_USERNAME); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 669 | RTC_DCHECK(username_attr != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 670 | username_ = username_attr->GetString(); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 671 | const StunByteStringAttribute* origin_attr = |
| 672 | msg->GetByteString(STUN_ATTR_ORIGIN); |
| 673 | if (origin_attr) { |
| 674 | origin_ = origin_attr->GetString(); |
| 675 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 676 | |
| 677 | // Figure out the lifetime and start the allocation timer. |
| 678 | int lifetime_secs = ComputeLifetime(msg); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 679 | thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| 680 | MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 681 | |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 682 | RTC_LOG(LS_INFO) << ToString() |
| 683 | << ": Created allocation with lifetime=" << lifetime_secs; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 684 | |
| 685 | // We've already validated all the important bits; just send a response here. |
| 686 | TurnMessage response; |
| 687 | InitResponse(msg, &response); |
| 688 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 689 | auto mapped_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>( |
| 690 | STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src()); |
| 691 | auto relayed_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>( |
| 692 | STUN_ATTR_XOR_RELAYED_ADDRESS, external_socket_->GetLocalAddress()); |
| 693 | auto lifetime_attr = |
| 694 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs); |
| 695 | response.AddAttribute(std::move(mapped_addr_attr)); |
| 696 | response.AddAttribute(std::move(relayed_addr_attr)); |
| 697 | response.AddAttribute(std::move(lifetime_attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 698 | |
| 699 | SendResponse(&response); |
| 700 | } |
| 701 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 702 | void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 703 | // Figure out the new lifetime. |
| 704 | int lifetime_secs = ComputeLifetime(msg); |
| 705 | |
| 706 | // Reset the expiration timer. |
| 707 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 708 | thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| 709 | MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 710 | |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 711 | RTC_LOG(LS_INFO) << ToString() |
| 712 | << ": Refreshed allocation, lifetime=" << lifetime_secs; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 713 | |
| 714 | // Send a success response with a LIFETIME attribute. |
| 715 | TurnMessage response; |
| 716 | InitResponse(msg, &response); |
| 717 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 718 | auto lifetime_attr = |
| 719 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs); |
| 720 | response.AddAttribute(std::move(lifetime_attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 721 | |
| 722 | SendResponse(&response); |
| 723 | } |
| 724 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 725 | void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 726 | // Check mandatory attributes. |
| 727 | const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA); |
| 728 | const StunAddressAttribute* peer_attr = |
| 729 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 730 | if (!data_attr || !peer_attr) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 731 | RTC_LOG(LS_WARNING) << ToString() |
| 732 | << ": Received invalid send indication"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
| 736 | // If a permission exists, send the data on to the peer. |
| 737 | if (HasPermission(peer_attr->GetAddress().ipaddr())) { |
| 738 | SendExternal(data_attr->bytes(), data_attr->length(), |
| 739 | peer_attr->GetAddress()); |
| 740 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 741 | RTC_LOG(LS_WARNING) << ToString() |
| 742 | << ": Received send indication without permission" |
| 743 | " peer=" |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 744 | << peer_attr->GetAddress().ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 748 | void TurnServerAllocation::HandleCreatePermissionRequest( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 749 | const TurnMessage* msg) { |
| 750 | // Check mandatory attributes. |
| 751 | const StunAddressAttribute* peer_attr = |
| 752 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 753 | if (!peer_attr) { |
| 754 | SendBadRequestResponse(msg); |
| 755 | return; |
| 756 | } |
| 757 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 758 | if (server_->reject_private_addresses_ && |
| 759 | rtc::IPIsPrivate(peer_attr->GetAddress().ipaddr())) { |
| 760 | SendErrorResponse(msg, STUN_ERROR_FORBIDDEN, STUN_ERROR_REASON_FORBIDDEN); |
| 761 | return; |
| 762 | } |
| 763 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 764 | // Add this permission. |
| 765 | AddPermission(peer_attr->GetAddress().ipaddr()); |
| 766 | |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 767 | RTC_LOG(LS_INFO) << ToString() |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 768 | << ": Created permission, peer=" |
| 769 | << peer_attr->GetAddress().ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 770 | |
| 771 | // Send a success response. |
| 772 | TurnMessage response; |
| 773 | InitResponse(msg, &response); |
| 774 | SendResponse(&response); |
| 775 | } |
| 776 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 777 | void TurnServerAllocation::HandleChannelBindRequest(const TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 778 | // Check mandatory attributes. |
| 779 | const StunUInt32Attribute* channel_attr = |
| 780 | msg->GetUInt32(STUN_ATTR_CHANNEL_NUMBER); |
| 781 | const StunAddressAttribute* peer_attr = |
| 782 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 783 | if (!channel_attr || !peer_attr) { |
| 784 | SendBadRequestResponse(msg); |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | // Check that channel id is valid. |
| 789 | int channel_id = channel_attr->value() >> 16; |
| 790 | if (channel_id < kMinChannelNumber || channel_id > kMaxChannelNumber) { |
| 791 | SendBadRequestResponse(msg); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | // Check that this channel id isn't bound to another transport address, and |
| 796 | // that this transport address isn't bound to another channel id. |
| 797 | Channel* channel1 = FindChannel(channel_id); |
| 798 | Channel* channel2 = FindChannel(peer_attr->GetAddress()); |
| 799 | if (channel1 != channel2) { |
| 800 | SendBadRequestResponse(msg); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | // Add or refresh this channel. |
| 805 | if (!channel1) { |
| 806 | channel1 = new Channel(thread_, channel_id, peer_attr->GetAddress()); |
| 807 | channel1->SignalDestroyed.connect(this, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 808 | &TurnServerAllocation::OnChannelDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 809 | channels_.push_back(channel1); |
| 810 | } else { |
| 811 | channel1->Refresh(); |
| 812 | } |
| 813 | |
| 814 | // Channel binds also refresh permissions. |
| 815 | AddPermission(peer_attr->GetAddress().ipaddr()); |
| 816 | |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 817 | RTC_LOG(LS_INFO) << ToString() |
| 818 | << ": Bound channel, id=" << channel_id |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 819 | << ", peer=" << peer_attr->GetAddress().ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 820 | |
| 821 | // Send a success response. |
| 822 | TurnMessage response; |
| 823 | InitResponse(msg, &response); |
| 824 | SendResponse(&response); |
| 825 | } |
| 826 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 827 | void TurnServerAllocation::HandleChannelData(const char* data, size_t size) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 828 | // Extract the channel number from the data. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 829 | uint16_t channel_id = rtc::GetBE16(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 830 | Channel* channel = FindChannel(channel_id); |
| 831 | if (channel) { |
| 832 | // Send the data to the peer address. |
| 833 | SendExternal(data + TURN_CHANNEL_HEADER_SIZE, |
| 834 | size - TURN_CHANNEL_HEADER_SIZE, channel->peer()); |
| 835 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 836 | RTC_LOG(LS_WARNING) << ToString() |
| 837 | << ": Received channel data for invalid channel, id=" |
| 838 | << channel_id; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 842 | void TurnServerAllocation::OnExternalPacket( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 843 | rtc::AsyncPacketSocket* socket, |
| 844 | const char* data, size_t size, |
| 845 | const rtc::SocketAddress& addr, |
| 846 | const rtc::PacketTime& packet_time) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 847 | RTC_DCHECK(external_socket_.get() == socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 848 | Channel* channel = FindChannel(addr); |
| 849 | if (channel) { |
| 850 | // There is a channel bound to this address. Send as a channel message. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 851 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 852 | buf.WriteUInt16(channel->id()); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 853 | buf.WriteUInt16(static_cast<uint16_t>(size)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 854 | buf.WriteBytes(data, size); |
| 855 | server_->Send(&conn_, buf); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 856 | } else if (!server_->enable_permission_checks_ || |
| 857 | HasPermission(addr.ipaddr())) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 858 | // No channel, but a permission exists. Send as a data indication. |
| 859 | TurnMessage msg; |
| 860 | msg.SetType(TURN_DATA_INDICATION); |
| 861 | msg.SetTransactionID( |
| 862 | rtc::CreateRandomString(kStunTransactionIdLength)); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 863 | msg.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
nisse | cc99bc2 | 2017-02-02 01:31:30 -0800 | [diff] [blame] | 864 | STUN_ATTR_XOR_PEER_ADDRESS, addr)); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 865 | msg.AddAttribute( |
| 866 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_DATA, data, size)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 867 | server_->SendStun(&conn_, &msg); |
| 868 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 869 | RTC_LOG(LS_WARNING) |
| 870 | << ToString() |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 871 | << ": Received external packet without permission, peer=" |
| 872 | << addr.ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 876 | int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 877 | // Return the smaller of our default lifetime and the requested lifetime. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 878 | int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 879 | const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 880 | if (lifetime_attr && static_cast<int>(lifetime_attr->value()) < lifetime) { |
| 881 | lifetime = static_cast<int>(lifetime_attr->value()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 882 | } |
| 883 | return lifetime; |
| 884 | } |
| 885 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 886 | bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 887 | return (FindPermission(addr) != NULL); |
| 888 | } |
| 889 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 890 | void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 891 | Permission* perm = FindPermission(addr); |
| 892 | if (!perm) { |
| 893 | perm = new Permission(thread_, addr); |
| 894 | perm->SignalDestroyed.connect( |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 895 | this, &TurnServerAllocation::OnPermissionDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 896 | perms_.push_back(perm); |
| 897 | } else { |
| 898 | perm->Refresh(); |
| 899 | } |
| 900 | } |
| 901 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 902 | TurnServerAllocation::Permission* TurnServerAllocation::FindPermission( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 903 | const rtc::IPAddress& addr) const { |
| 904 | for (PermissionList::const_iterator it = perms_.begin(); |
| 905 | it != perms_.end(); ++it) { |
| 906 | if ((*it)->peer() == addr) |
| 907 | return *it; |
| 908 | } |
| 909 | return NULL; |
| 910 | } |
| 911 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 912 | TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
| 913 | int channel_id) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 914 | for (ChannelList::const_iterator it = channels_.begin(); |
| 915 | it != channels_.end(); ++it) { |
| 916 | if ((*it)->id() == channel_id) |
| 917 | return *it; |
| 918 | } |
| 919 | return NULL; |
| 920 | } |
| 921 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 922 | TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 923 | const rtc::SocketAddress& addr) const { |
| 924 | for (ChannelList::const_iterator it = channels_.begin(); |
| 925 | it != channels_.end(); ++it) { |
| 926 | if ((*it)->peer() == addr) |
| 927 | return *it; |
| 928 | } |
| 929 | return NULL; |
| 930 | } |
| 931 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 932 | void TurnServerAllocation::SendResponse(TurnMessage* msg) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 933 | // Success responses always have M-I. |
| 934 | msg->AddMessageIntegrity(key_); |
| 935 | server_->SendStun(&conn_, msg); |
| 936 | } |
| 937 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 938 | void TurnServerAllocation::SendBadRequestResponse(const TurnMessage* req) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 939 | SendErrorResponse(req, STUN_ERROR_BAD_REQUEST, STUN_ERROR_REASON_BAD_REQUEST); |
| 940 | } |
| 941 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 942 | void TurnServerAllocation::SendErrorResponse(const TurnMessage* req, int code, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 943 | const std::string& reason) { |
| 944 | server_->SendErrorResponse(&conn_, req, code, reason); |
| 945 | } |
| 946 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 947 | void TurnServerAllocation::SendExternal(const void* data, size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 948 | const rtc::SocketAddress& peer) { |
| 949 | rtc::PacketOptions options; |
| 950 | external_socket_->SendTo(data, size, peer, options); |
| 951 | } |
| 952 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 953 | void TurnServerAllocation::OnMessage(rtc::Message* msg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 954 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 955 | SignalDestroyed(this); |
| 956 | delete this; |
| 957 | } |
| 958 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 959 | void TurnServerAllocation::OnPermissionDestroyed(Permission* perm) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 960 | PermissionList::iterator it = std::find(perms_.begin(), perms_.end(), perm); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 961 | RTC_DCHECK(it != perms_.end()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 962 | perms_.erase(it); |
| 963 | } |
| 964 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 965 | void TurnServerAllocation::OnChannelDestroyed(Channel* channel) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 966 | ChannelList::iterator it = |
| 967 | std::find(channels_.begin(), channels_.end(), channel); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 968 | RTC_DCHECK(it != channels_.end()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 969 | channels_.erase(it); |
| 970 | } |
| 971 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 972 | TurnServerAllocation::Permission::Permission(rtc::Thread* thread, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 973 | const rtc::IPAddress& peer) |
| 974 | : thread_(thread), peer_(peer) { |
| 975 | Refresh(); |
| 976 | } |
| 977 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 978 | TurnServerAllocation::Permission::~Permission() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 979 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 980 | } |
| 981 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 982 | void TurnServerAllocation::Permission::Refresh() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 983 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 984 | thread_->PostDelayed(RTC_FROM_HERE, kPermissionTimeout, this, |
| 985 | MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 986 | } |
| 987 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 988 | void TurnServerAllocation::Permission::OnMessage(rtc::Message* msg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 989 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 990 | SignalDestroyed(this); |
| 991 | delete this; |
| 992 | } |
| 993 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 994 | TurnServerAllocation::Channel::Channel(rtc::Thread* thread, int id, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 995 | const rtc::SocketAddress& peer) |
| 996 | : thread_(thread), id_(id), peer_(peer) { |
| 997 | Refresh(); |
| 998 | } |
| 999 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1000 | TurnServerAllocation::Channel::~Channel() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1001 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 1002 | } |
| 1003 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1004 | void TurnServerAllocation::Channel::Refresh() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1005 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1006 | thread_->PostDelayed(RTC_FROM_HERE, kChannelTimeout, this, |
| 1007 | MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1010 | void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1011 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1012 | SignalDestroyed(this); |
| 1013 | delete this; |
| 1014 | } |
| 1015 | |
| 1016 | } // namespace cricket |