| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 private: | 86 private: |
| 87 Request* GetRequestByAddress(const rtc::IPAddress& ip); | 87 Request* GetRequestByAddress(const rtc::IPAddress& ip); |
| 88 | 88 |
| 89 StunProber* prober_; | 89 StunProber* prober_; |
| 90 | 90 |
| 91 // The socket for this session. | 91 // The socket for this session. |
| 92 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; | 92 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; |
| 93 | 93 |
| 94 // Temporary SocketAddress and buffer for RecvFrom. | 94 // Temporary SocketAddress and buffer for RecvFrom. |
| 95 rtc::SocketAddress addr_; | 95 rtc::SocketAddress addr_; |
| 96 rtc::scoped_ptr<rtc::ByteBuffer> response_packet_; | 96 rtc::scoped_ptr<rtc::ByteBufferWriter> response_packet_; |
| 97 | 97 |
| 98 std::vector<Request*> requests_; | 98 std::vector<Request*> requests_; |
| 99 std::vector<rtc::SocketAddress> server_ips_; | 99 std::vector<rtc::SocketAddress> server_ips_; |
| 100 int16_t num_request_sent_ = 0; | 100 int16_t num_request_sent_ = 0; |
| 101 int16_t num_response_received_ = 0; | 101 int16_t num_response_received_ = 0; |
| 102 | 102 |
| 103 rtc::ThreadChecker& thread_checker_; | 103 rtc::ThreadChecker& thread_checker_; |
| 104 | 104 |
| 105 RTC_DISALLOW_COPY_AND_ASSIGN(Requester); | 105 RTC_DISALLOW_COPY_AND_ASSIGN(Requester); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 StunProber::Requester::Requester( | 108 StunProber::Requester::Requester( |
| 109 StunProber* prober, | 109 StunProber* prober, |
| 110 rtc::AsyncPacketSocket* socket, | 110 rtc::AsyncPacketSocket* socket, |
| 111 const std::vector<rtc::SocketAddress>& server_ips) | 111 const std::vector<rtc::SocketAddress>& server_ips) |
| 112 : prober_(prober), | 112 : prober_(prober), |
| 113 socket_(socket), | 113 socket_(socket), |
| 114 response_packet_(new rtc::ByteBuffer(nullptr, kMaxUdpBufferSize)), | 114 response_packet_(new rtc::ByteBufferWriter(nullptr, kMaxUdpBufferSize)), |
| 115 server_ips_(server_ips), | 115 server_ips_(server_ips), |
| 116 thread_checker_(prober->thread_checker_) { | 116 thread_checker_(prober->thread_checker_) { |
| 117 socket_->SignalReadPacket.connect( | 117 socket_->SignalReadPacket.connect( |
| 118 this, &StunProber::Requester::OnStunResponseReceived); | 118 this, &StunProber::Requester::OnStunResponseReceived); |
| 119 } | 119 } |
| 120 | 120 |
| 121 StunProber::Requester::~Requester() { | 121 StunProber::Requester::~Requester() { |
| 122 if (socket_) { | 122 if (socket_) { |
| 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 RTC_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::ByteBufferWriter> request_packet( |
| 144 new rtc::ByteBuffer(nullptr, kMaxUdpBufferSize)); | 144 new rtc::ByteBufferWriter(nullptr, kMaxUdpBufferSize)); |
| 145 if (!message.Write(request_packet.get())) { | 145 if (!message.Write(request_packet.get())) { |
| 146 prober_->ReportOnFinished(WRITE_FAILED); | 146 prober_->ReportOnFinished(WRITE_FAILED); |
| 147 return; | 147 return; |
| 148 } | 148 } |
| 149 | 149 |
| 150 auto addr = server_ips_[num_request_sent_]; | 150 auto addr = server_ips_[num_request_sent_]; |
| 151 request.server_addr = addr.ipaddr(); | 151 request.server_addr = addr.ipaddr(); |
| 152 | 152 |
| 153 // The write must succeed immediately. Otherwise, the calculating of the STUN | 153 // The write must succeed immediately. Otherwise, the calculating of the STUN |
| 154 // request timing could become too complicated. Callback is ignored by passing | 154 // request timing could become too complicated. Callback is ignored by passing |
| 155 // empty AsyncCallback. | 155 // empty AsyncCallback. |
| 156 rtc::PacketOptions options; | 156 rtc::PacketOptions options; |
| 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_->ReportOnFinished(WRITE_FAILED); | 160 prober_->ReportOnFinished(WRITE_FAILED); |
| 161 return; | 161 return; |
| 162 } | 162 } |
| 163 | 163 |
| 164 request.sent_time_ms = rtc::Time64(); | 164 request.sent_time_ms = rtc::Time64(); |
| 165 | 165 |
| 166 num_request_sent_++; | 166 num_request_sent_++; |
| 167 RTC_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_t now = rtc::Time64(); | 172 int64_t now = rtc::Time64(); |
| 173 rtc::ByteBuffer message(buf, buf_len); | 173 rtc::ByteBufferReader 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; |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Get external address of the socket. | 181 // Get external address of the socket. |
| 182 const cricket::StunAddressAttribute* addr_attr = | 182 const cricket::StunAddressAttribute* addr_attr = |
| 183 stun_response.GetAddress(cricket::STUN_ATTR_MAPPED_ADDRESS); | 183 stun_response.GetAddress(cricket::STUN_ATTR_MAPPED_ADDRESS); |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 } | 561 } |
| 562 } | 562 } |
| 563 | 563 |
| 564 void StunProber::ReportOnFinished(StunProber::Status status) { | 564 void StunProber::ReportOnFinished(StunProber::Status status) { |
| 565 if (observer_) { | 565 if (observer_) { |
| 566 observer_->OnFinished(this, status); | 566 observer_->OnFinished(this, status); |
| 567 } | 567 } |
| 568 } | 568 } |
| 569 | 569 |
| 570 } // namespace stunprober | 570 } // namespace stunprober |
| OLD | NEW |