| 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 "p2p/base/stun_request.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/helpers.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | #include "rtc_base/string_encode.h" |
| 21 | #include "rtc_base/time_utils.h" // For TimeMillis |
| 22 | #include "system_wrappers/include/field_trial.h" |
| 23 |
| 24 | namespace cricket { |
| 25 |
| 26 | const uint32_t MSG_STUN_SEND = 1; |
| 27 |
| 28 | // RFC 5389 says SHOULD be 500ms. |
| 29 | // For years, this was 100ms, but for networks that |
| 30 | // experience moments of high RTT (such as 2G networks), this doesn't |
| 31 | // work well. |
| 32 | const int STUN_INITIAL_RTO = 250; // milliseconds |
| 33 |
| 34 | // The timeout doubles each retransmission, up to this many times |
| 35 | // RFC 5389 says SHOULD retransmit 7 times. |
| 36 | // This has been 8 for years (not sure why). |
| 37 | const int STUN_MAX_RETRANSMISSIONS = 8; // Total sends: 9 |
| 38 |
| 39 | // We also cap the doubling, even though the standard doesn't say to. |
| 40 | // This has been 1.6 seconds for years, but for networks that |
| 41 | // experience moments of high RTT (such as 2G networks), this doesn't |
| 42 | // work well. |
| 43 | const int STUN_MAX_RTO = 8000; // milliseconds, or 5 doublings |
| 44 |
| 45 | StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {} |
| 46 |
| 47 | StunRequestManager::~StunRequestManager() { |
| 48 | while (requests_.begin() != requests_.end()) { |
| 49 | StunRequest* request = requests_.begin()->second; |
| 50 | requests_.erase(requests_.begin()); |
| 51 | delete request; |
| 52 | } |
| 53 | } |
| 54 |
| 55 | void StunRequestManager::Send(StunRequest* request) { |
| 56 | SendDelayed(request, 0); |
| 57 | } |
| 58 |
| 59 | void StunRequestManager::SendDelayed(StunRequest* request, int delay) { |
| 60 | request->set_manager(this); |
| 61 | RTC_DCHECK(requests_.find(request->id()) == requests_.end()); |
| 62 | request->Construct(); |
| 63 | requests_[request->id()] = request; |
| 64 | if (delay > 0) { |
| 65 | thread_->PostDelayed(RTC_FROM_HERE, delay, request, MSG_STUN_SEND, NULL); |
| 66 | } else { |
| 67 | thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); |
| 68 | } |
| 69 | } |
| 70 |
| 71 | void StunRequestManager::Flush(int msg_type) { |
| 72 | for (const auto& kv : requests_) { |
| 73 | StunRequest* request = kv.second; |
| 74 | if (msg_type == kAllRequests || msg_type == request->type()) { |
| 75 | thread_->Clear(request, MSG_STUN_SEND); |
| 76 | thread_->Send(RTC_FROM_HERE, request, MSG_STUN_SEND, NULL); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 |
| 81 | bool StunRequestManager::HasRequest(int msg_type) { |
| 82 | for (const auto& kv : requests_) { |
| 83 | StunRequest* request = kv.second; |
| 84 | if (msg_type == kAllRequests || msg_type == request->type()) { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 |
| 91 | void StunRequestManager::Remove(StunRequest* request) { |
| 92 | RTC_DCHECK(request->manager() == this); |
| 93 | RequestMap::iterator iter = requests_.find(request->id()); |
| 94 | if (iter != requests_.end()) { |
| 95 | RTC_DCHECK(iter->second == request); |
| 96 | requests_.erase(iter); |
| 97 | thread_->Clear(request); |
| 98 | } |
| 99 | } |
| 100 |
| 101 | void StunRequestManager::Clear() { |
| 102 | std::vector<StunRequest*> requests; |
| 103 | for (RequestMap::iterator i = requests_.begin(); i != requests_.end(); ++i) |
| 104 | requests.push_back(i->second); |
| 105 |
| 106 | for (uint32_t i = 0; i < requests.size(); ++i) { |
| 107 | // StunRequest destructor calls Remove() which deletes requests |
| 108 | // from `requests_`. |
| 109 | delete requests[i]; |
| 110 | } |
| 111 | } |
| 112 |
| 113 | bool StunRequestManager::CheckResponse(StunMessage* msg) { |
| 114 | RequestMap::iterator iter = requests_.find(msg->transaction_id()); |
| 115 | if (iter == requests_.end()) { |
| 116 | // TODO(pthatcher): Log unknown responses without being too spammy |
| 117 | // in the logs. |
| 118 | return false; |
| 119 | } |
| 120 |
| 121 | StunRequest* request = iter->second; |
| 122 |
| 123 | // Now that we know the request, we can see if the response is |
| 124 | // integrity-protected or not. |
| 125 | // For some tests, the message integrity is not set in the request. |
| 126 | // Complain, and then don't check. |
| 127 | bool skip_integrity_checking = false; |
| 128 | if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) { |
| 129 | skip_integrity_checking = true; |
| 130 | } else { |
| 131 | msg->ValidateMessageIntegrity(request->msg()->password()); |
| 132 | } |
| 133 |
| 134 | if (!msg->GetNonComprehendedAttributes().empty()) { |
| 135 | // If a response contains unknown comprehension-required attributes, it's |
| 136 | // simply discarded and the transaction is considered failed. See RFC5389 |
| 137 | // sections 7.3.3 and 7.3.4. |
| 138 | RTC_LOG(LS_ERROR) << ": Discarding response due to unknown " |
| 139 | "comprehension-required attribute."; |
| 140 | delete request; |
| 141 | return false; |
| 142 | } else if (msg->type() == GetStunSuccessResponseType(request->type())) { |
| 143 | if (!msg->IntegrityOk() && !skip_integrity_checking) { |
| 144 | return false; |
| 145 | } |
| 146 | request->OnResponse(msg); |
| 147 | } else if (msg->type() == GetStunErrorResponseType(request->type())) { |
| 148 | request->OnErrorResponse(msg); |
| 149 | } else { |
| 150 | RTC_LOG(LS_ERROR) << "Received response with wrong type: " << msg->type() |
| 151 | << " (expecting " |
| 152 | << GetStunSuccessResponseType(request->type()) << ")"; |
| 153 | return false; |
| 154 | } |
| 155 |
| 156 | delete request; |
| 157 | return true; |
| 158 | } |
| 159 |
| 160 | bool StunRequestManager::CheckResponse(const char* data, size_t size) { |
| 161 | // Check the appropriate bytes of the stream to see if they match the |
| 162 | // transaction ID of a response we are expecting. |
| 163 |
| 164 | if (size < 20) |
| 165 | return false; |
| 166 |
| 167 | std::string id; |
| 168 | id.append(data + kStunTransactionIdOffset, kStunTransactionIdLength); |
| 169 |
| 170 | RequestMap::iterator iter = requests_.find(id); |
| 171 | if (iter == requests_.end()) { |
| 172 | // TODO(pthatcher): Log unknown responses without being too spammy |
| 173 | // in the logs. |
| 174 | return false; |
| 175 | } |
| 176 |
| 177 | // Parse the STUN message and continue processing as usual. |
| 178 |
| 179 | rtc::ByteBufferReader buf(data, size); |
| 180 | std::unique_ptr<StunMessage> response(iter->second->msg_->CreateNew()); |
| 181 | if (!response->Read(&buf)) { |
| 182 | RTC_LOG(LS_WARNING) << "Failed to read STUN response " |
| 183 | << rtc::hex_encode(id); |
| 184 | return false; |
| 185 | } |
| 186 |
| 187 | return CheckResponse(response.get()); |
| 188 | } |
| 189 |
| 190 | StunRequest::StunRequest() |
| 191 | : count_(0), |
| 192 | timeout_(false), |
| 193 | manager_(0), |
| 194 | msg_(new StunMessage()), |
| 195 | tstamp_(0) { |
| 196 | msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
| 197 | } |
| 198 |
| 199 | StunRequest::StunRequest(StunMessage* request) |
| 200 | : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) { |
| 201 | msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
| 202 | } |
| 203 |
| 204 | StunRequest::~StunRequest() { |
| 205 | RTC_DCHECK(manager_ != NULL); |
| 206 | if (manager_) { |
| 207 | manager_->Remove(this); |
| 208 | manager_->thread_->Clear(this); |
| 209 | } |
| 210 | delete msg_; |
| 211 | } |
| 212 |
| 213 | void StunRequest::Construct() { |
| 214 | if (msg_->type() == 0) { |
| 215 | Prepare(msg_); |
| 216 | RTC_DCHECK(msg_->type() != 0); |
| 217 | } |
| 218 | } |
| 219 |
| 220 | int StunRequest::type() { |
| 221 | RTC_DCHECK(msg_ != NULL); |
| 222 | return msg_->type(); |
| 223 | } |
| 224 |
| 225 | const StunMessage* StunRequest::msg() const { |
| 226 | return msg_; |
| 227 | } |
| 228 |
| 229 | StunMessage* StunRequest::mutable_msg() { |
| 230 | return msg_; |
| 231 | } |
| 232 |
| 233 | int StunRequest::Elapsed() const { |
| 234 | return static_cast<int>(rtc::TimeMillis() - tstamp_); |
| 235 | } |
| 236 |
| 237 | void StunRequest::set_manager(StunRequestManager* manager) { |
| 238 | RTC_DCHECK(!manager_); |
| 239 | manager_ = manager; |
| 240 | } |
| 241 |
| 242 | void StunRequest::OnMessage(rtc::Message* pmsg) { |
| 243 | RTC_DCHECK(manager_ != NULL); |
| 244 | RTC_DCHECK(pmsg->message_id == MSG_STUN_SEND); |
| 245 |
| 246 | if (timeout_) { |
| 247 | OnTimeout(); |
| 248 | delete this; |
| 249 | return; |
| 250 | } |
| 251 |
| 252 | tstamp_ = rtc::TimeMillis(); |
| 253 |
| 254 | rtc::ByteBufferWriter buf; |
| 255 | msg_->Write(&buf); |
| 256 | manager_->SignalSendPacket(buf.Data(), buf.Length(), this); |
| 257 |
| 258 | OnSent(); |
| 259 | manager_->thread_->PostDelayed(RTC_FROM_HERE, resend_delay(), this, |
| 260 | MSG_STUN_SEND, NULL); |
| 261 | } |
| 262 |
| 263 | void StunRequest::OnSent() { |
| 264 | count_ += 1; |
| 265 | int retransmissions = (count_ - 1); |
| 266 | if (retransmissions >= STUN_MAX_RETRANSMISSIONS) { |
| 267 | timeout_ = true; |
| 268 | } |
| 269 | RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_ |
| 270 | << "; resend delay = " << resend_delay(); |
| 271 | } |
| 272 |
| 273 | int StunRequest::resend_delay() { |
| 274 | if (count_ == 0) { |
| 275 | return 0; |
| 276 | } |
| 277 | int retransmissions = (count_ - 1); |
| 278 | int rto = STUN_INITIAL_RTO << retransmissions; |
| 279 | return std::min(rto, STUN_MAX_RTO); |
| 280 | } |
| 281 |
| 282 | } // namespace cricket |
| 283 |