OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 socket_->Close(); | 123 socket_->Close(); |
124 } | 124 } |
125 for (auto req : requests_) { | 125 for (auto req : requests_) { |
126 if (req) { | 126 if (req) { |
127 delete req; | 127 delete req; |
128 } | 128 } |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 void StunProber::Requester::SendStunRequest() { | 132 void StunProber::Requester::SendStunRequest() { |
133 DCHECK(thread_checker_.CalledOnValidThread()); | 133 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
134 requests_.push_back(new Request()); | 134 requests_.push_back(new Request()); |
135 Request& request = *(requests_.back()); | 135 Request& request = *(requests_.back()); |
136 cricket::StunMessage message; | 136 cricket::StunMessage message; |
137 | 137 |
138 // Random transaction ID, STUN_BINDING_REQUEST | 138 // Random transaction ID, STUN_BINDING_REQUEST |
139 message.SetTransactionID( | 139 message.SetTransactionID( |
140 rtc::CreateRandomString(cricket::kStunTransactionIdLength)); | 140 rtc::CreateRandomString(cricket::kStunTransactionIdLength)); |
141 message.SetType(cricket::STUN_BINDING_REQUEST); | 141 message.SetType(cricket::STUN_BINDING_REQUEST); |
142 | 142 |
143 rtc::scoped_ptr<rtc::ByteBuffer> request_packet( | 143 rtc::scoped_ptr<rtc::ByteBuffer> request_packet( |
(...skipping 13 matching lines...) Expand all Loading... |
157 int rv = socket_->SendTo(const_cast<char*>(request_packet->Data()), | 157 int rv = socket_->SendTo(const_cast<char*>(request_packet->Data()), |
158 request_packet->Length(), addr, options); | 158 request_packet->Length(), addr, options); |
159 if (rv < 0) { | 159 if (rv < 0) { |
160 prober_->End(WRITE_FAILED); | 160 prober_->End(WRITE_FAILED); |
161 return; | 161 return; |
162 } | 162 } |
163 | 163 |
164 request.sent_time_ms = rtc::Time(); | 164 request.sent_time_ms = rtc::Time(); |
165 | 165 |
166 num_request_sent_++; | 166 num_request_sent_++; |
167 DCHECK(static_cast<size_t>(num_request_sent_) <= server_ips_.size()); | 167 RTC_DCHECK(static_cast<size_t>(num_request_sent_) <= server_ips_.size()); |
168 } | 168 } |
169 | 169 |
170 void StunProber::Requester::Request::ProcessResponse(const char* buf, | 170 void StunProber::Requester::Request::ProcessResponse(const char* buf, |
171 size_t buf_len) { | 171 size_t buf_len) { |
172 int64 now = rtc::Time(); | 172 int64 now = rtc::Time(); |
173 rtc::ByteBuffer message(buf, buf_len); | 173 rtc::ByteBuffer message(buf, buf_len); |
174 cricket::StunMessage stun_response; | 174 cricket::StunMessage stun_response; |
175 if (!stun_response.Read(&message)) { | 175 if (!stun_response.Read(&message)) { |
176 // Invalid or incomplete STUN packet. | 176 // Invalid or incomplete STUN packet. |
177 received_time_ms = 0; | 177 received_time_ms = 0; |
(...skipping 17 matching lines...) Expand all Loading... |
195 | 195 |
196 srflx_addr = addr_attr->GetAddress(); | 196 srflx_addr = addr_attr->GetAddress(); |
197 } | 197 } |
198 | 198 |
199 void StunProber::Requester::OnStunResponseReceived( | 199 void StunProber::Requester::OnStunResponseReceived( |
200 rtc::AsyncPacketSocket* socket, | 200 rtc::AsyncPacketSocket* socket, |
201 const char* buf, | 201 const char* buf, |
202 size_t size, | 202 size_t size, |
203 const rtc::SocketAddress& addr, | 203 const rtc::SocketAddress& addr, |
204 const rtc::PacketTime& time) { | 204 const rtc::PacketTime& time) { |
205 DCHECK(thread_checker_.CalledOnValidThread()); | 205 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
206 DCHECK(socket_); | 206 RTC_DCHECK(socket_); |
207 Request* request = GetRequestByAddress(addr.ipaddr()); | 207 Request* request = GetRequestByAddress(addr.ipaddr()); |
208 if (!request) { | 208 if (!request) { |
209 // Something is wrong, finish the test. | 209 // Something is wrong, finish the test. |
210 prober_->End(GENERIC_FAILURE); | 210 prober_->End(GENERIC_FAILURE); |
211 return; | 211 return; |
212 } | 212 } |
213 | 213 |
214 num_response_received_++; | 214 num_response_received_++; |
215 request->ProcessResponse(buf, size); | 215 request->ProcessResponse(buf, size); |
216 } | 216 } |
217 | 217 |
218 StunProber::Requester::Request* StunProber::Requester::GetRequestByAddress( | 218 StunProber::Requester::Request* StunProber::Requester::GetRequestByAddress( |
219 const rtc::IPAddress& ipaddr) { | 219 const rtc::IPAddress& ipaddr) { |
220 DCHECK(thread_checker_.CalledOnValidThread()); | 220 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
221 for (auto request : requests_) { | 221 for (auto request : requests_) { |
222 if (request->server_addr == ipaddr) { | 222 if (request->server_addr == ipaddr) { |
223 return request; | 223 return request; |
224 } | 224 } |
225 } | 225 } |
226 | 226 |
227 return nullptr; | 227 return nullptr; |
228 } | 228 } |
229 | 229 |
230 StunProber::StunProber(rtc::PacketSocketFactory* socket_factory, | 230 StunProber::StunProber(rtc::PacketSocketFactory* socket_factory, |
(...skipping 17 matching lines...) Expand all Loading... |
248 } | 248 } |
249 } | 249 } |
250 } | 250 } |
251 | 251 |
252 bool StunProber::Start(const std::vector<rtc::SocketAddress>& servers, | 252 bool StunProber::Start(const std::vector<rtc::SocketAddress>& servers, |
253 bool shared_socket_mode, | 253 bool shared_socket_mode, |
254 int interval_ms, | 254 int interval_ms, |
255 int num_request_per_ip, | 255 int num_request_per_ip, |
256 int timeout_ms, | 256 int timeout_ms, |
257 const AsyncCallback callback) { | 257 const AsyncCallback callback) { |
258 DCHECK(thread_checker_.CalledOnValidThread()); | 258 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
259 interval_ms_ = interval_ms; | 259 interval_ms_ = interval_ms; |
260 shared_socket_mode_ = shared_socket_mode; | 260 shared_socket_mode_ = shared_socket_mode; |
261 | 261 |
262 requests_per_ip_ = num_request_per_ip; | 262 requests_per_ip_ = num_request_per_ip; |
263 if (requests_per_ip_ == 0 || servers.size() == 0) { | 263 if (requests_per_ip_ == 0 || servers.size() == 0) { |
264 return false; | 264 return false; |
265 } | 265 } |
266 | 266 |
267 timeout_ms_ = timeout_ms; | 267 timeout_ms_ = timeout_ms; |
268 servers_ = servers; | 268 servers_ = servers; |
(...skipping 14 matching lines...) Expand all Loading... |
283 | 283 |
284 void StunProber::OnSocketReady(rtc::AsyncPacketSocket* socket, | 284 void StunProber::OnSocketReady(rtc::AsyncPacketSocket* socket, |
285 const rtc::SocketAddress& addr) { | 285 const rtc::SocketAddress& addr) { |
286 total_ready_sockets_++; | 286 total_ready_sockets_++; |
287 if (total_ready_sockets_ == total_socket_required()) { | 287 if (total_ready_sockets_ == total_socket_required()) { |
288 MaybeScheduleStunRequests(); | 288 MaybeScheduleStunRequests(); |
289 } | 289 } |
290 } | 290 } |
291 | 291 |
292 void StunProber::OnServerResolved(rtc::AsyncResolverInterface* resolver) { | 292 void StunProber::OnServerResolved(rtc::AsyncResolverInterface* resolver) { |
293 DCHECK(thread_checker_.CalledOnValidThread()); | 293 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
294 | 294 |
295 if (resolver->GetError() == 0) { | 295 if (resolver->GetError() == 0) { |
296 rtc::SocketAddress addr(resolver->address().ipaddr(), | 296 rtc::SocketAddress addr(resolver->address().ipaddr(), |
297 resolver->address().port()); | 297 resolver->address().port()); |
298 all_servers_addrs_.push_back(addr); | 298 all_servers_addrs_.push_back(addr); |
299 } | 299 } |
300 | 300 |
301 // Deletion of AsyncResolverInterface can't be done in OnResolveResult which | 301 // Deletion of AsyncResolverInterface can't be done in OnResolveResult which |
302 // handles SignalDone. | 302 // handles SignalDone. |
303 invoker_.AsyncInvoke<void>( | 303 invoker_.AsyncInvoke<void>( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 if (socket->GetState() == rtc::AsyncPacketSocket::STATE_BINDING) { | 336 if (socket->GetState() == rtc::AsyncPacketSocket::STATE_BINDING) { |
337 socket->SignalAddressReady.connect(this, &StunProber::OnSocketReady); | 337 socket->SignalAddressReady.connect(this, &StunProber::OnSocketReady); |
338 } else { | 338 } else { |
339 OnSocketReady(socket.get(), rtc::SocketAddress(INADDR_ANY, 0)); | 339 OnSocketReady(socket.get(), rtc::SocketAddress(INADDR_ANY, 0)); |
340 } | 340 } |
341 sockets_.push_back(socket.release()); | 341 sockets_.push_back(socket.release()); |
342 } | 342 } |
343 } | 343 } |
344 | 344 |
345 StunProber::Requester* StunProber::CreateRequester() { | 345 StunProber::Requester* StunProber::CreateRequester() { |
346 DCHECK(thread_checker_.CalledOnValidThread()); | 346 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
347 if (!sockets_.size()) { | 347 if (!sockets_.size()) { |
348 return nullptr; | 348 return nullptr; |
349 } | 349 } |
350 StunProber::Requester* requester; | 350 StunProber::Requester* requester; |
351 if (shared_socket_mode_) { | 351 if (shared_socket_mode_) { |
352 requester = new Requester(this, sockets_.back(), all_servers_addrs_); | 352 requester = new Requester(this, sockets_.back(), all_servers_addrs_); |
353 } else { | 353 } else { |
354 std::vector<rtc::SocketAddress> server_ip; | 354 std::vector<rtc::SocketAddress> server_ip; |
355 server_ip.push_back( | 355 server_ip.push_back( |
356 all_servers_addrs_[(num_request_sent_ % all_servers_addrs_.size())]); | 356 all_servers_addrs_[(num_request_sent_ % all_servers_addrs_.size())]); |
(...skipping 11 matching lines...) Expand all Loading... |
368 } | 368 } |
369 if (!current_requester_) { | 369 if (!current_requester_) { |
370 return false; | 370 return false; |
371 } | 371 } |
372 current_requester_->SendStunRequest(); | 372 current_requester_->SendStunRequest(); |
373 num_request_sent_++; | 373 num_request_sent_++; |
374 return true; | 374 return true; |
375 } | 375 } |
376 | 376 |
377 void StunProber::MaybeScheduleStunRequests() { | 377 void StunProber::MaybeScheduleStunRequests() { |
378 DCHECK(thread_checker_.CalledOnValidThread()); | 378 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
379 uint32 now = rtc::Time(); | 379 uint32 now = rtc::Time(); |
380 | 380 |
381 if (Done()) { | 381 if (Done()) { |
382 invoker_.AsyncInvokeDelayed<void>( | 382 invoker_.AsyncInvokeDelayed<void>( |
383 thread_, rtc::Bind(&StunProber::End, this, SUCCESS), timeout_ms_); | 383 thread_, rtc::Bind(&StunProber::End, this, SUCCESS), timeout_ms_); |
384 return; | 384 return; |
385 } | 385 } |
386 if ((now + (thread_wake_up_interval_ms / 2)) >= next_request_time_ms_) { | 386 if ((now + (thread_wake_up_interval_ms / 2)) >= next_request_time_ms_) { |
387 if (!SendNextRequest()) { | 387 if (!SendNextRequest()) { |
388 End(GENERIC_FAILURE); | 388 End(GENERIC_FAILURE); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 // server reflexive IPs. | 453 // server reflexive IPs. |
454 if (srflx_ips.size() > 1) { | 454 if (srflx_ips.size() > 1) { |
455 return false; | 455 return false; |
456 } | 456 } |
457 | 457 |
458 int num_sent = 0; | 458 int num_sent = 0; |
459 int num_received = 0; | 459 int num_received = 0; |
460 int num_server_ip_with_response = 0; | 460 int num_server_ip_with_response = 0; |
461 | 461 |
462 for (const auto& kv : num_response_per_server) { | 462 for (const auto& kv : num_response_per_server) { |
463 DCHECK_GT(kv.second, 0); | 463 RTC_DCHECK_GT(kv.second, 0); |
464 num_server_ip_with_response++; | 464 num_server_ip_with_response++; |
465 num_received += kv.second; | 465 num_received += kv.second; |
466 num_sent += num_request_per_server[kv.first]; | 466 num_sent += num_request_per_server[kv.first]; |
467 } | 467 } |
468 | 468 |
469 // Not receiving any response, the trial is inconclusive. | 469 // Not receiving any response, the trial is inconclusive. |
470 if (!num_received) { | 470 if (!num_received) { |
471 return false; | 471 return false; |
472 } | 472 } |
473 | 473 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 | 514 |
515 if (num_received) { | 515 if (num_received) { |
516 stats.average_rtt_ms = static_cast<int>((rtt_sum / num_received)); | 516 stats.average_rtt_ms = static_cast<int>((rtt_sum / num_received)); |
517 } | 517 } |
518 | 518 |
519 *prob_stats = stats; | 519 *prob_stats = stats; |
520 return true; | 520 return true; |
521 } | 521 } |
522 | 522 |
523 void StunProber::End(StunProber::Status status) { | 523 void StunProber::End(StunProber::Status status) { |
524 DCHECK(thread_checker_.CalledOnValidThread()); | 524 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
525 if (!finished_callback_.empty()) { | 525 if (!finished_callback_.empty()) { |
526 AsyncCallback callback = finished_callback_; | 526 AsyncCallback callback = finished_callback_; |
527 finished_callback_ = AsyncCallback(); | 527 finished_callback_ = AsyncCallback(); |
528 | 528 |
529 // Callback at the last since the prober might be deleted in the callback. | 529 // Callback at the last since the prober might be deleted in the callback. |
530 callback(this, status); | 530 callback(this, status); |
531 } | 531 } |
532 } | 532 } |
533 | 533 |
534 } // namespace stunprober | 534 } // namespace stunprober |
OLD | NEW |