OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 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 16 matching lines...) Expand all Loading... |
27 | 27 |
28 // We don't pull the RTP constants from rtputils.h, to avoid a layer violation. | 28 // We don't pull the RTP constants from rtputils.h, to avoid a layer violation. |
29 static const size_t kDtlsRecordHeaderLen = 13; | 29 static const size_t kDtlsRecordHeaderLen = 13; |
30 static const size_t kMaxDtlsPacketLen = 2048; | 30 static const size_t kMaxDtlsPacketLen = 2048; |
31 static const size_t kMinRtpPacketLen = 12; | 31 static const size_t kMinRtpPacketLen = 12; |
32 | 32 |
33 // Maximum number of pending packets in the queue. Packets are read immediately | 33 // Maximum number of pending packets in the queue. Packets are read immediately |
34 // after they have been written, so a capacity of "1" is sufficient. | 34 // after they have been written, so a capacity of "1" is sufficient. |
35 static const size_t kMaxPendingPackets = 1; | 35 static const size_t kMaxPendingPackets = 1; |
36 | 36 |
37 // Minimum and maximum values for the initial DTLS handshake timeout. We'll pick | |
38 // an initial timeout based on ICE RTT estimates, but clamp it to this range. | |
39 static const int kMinHandshakeTimeout = 50; | |
40 static const int kMaxHandshakeTimeout = 3000; | |
41 | |
42 static bool IsDtlsPacket(const char* data, size_t len) { | 37 static bool IsDtlsPacket(const char* data, size_t len) { |
43 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); | 38 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); |
44 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64)); | 39 return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64)); |
45 } | 40 } |
46 static bool IsDtlsClientHelloPacket(const char* data, size_t len) { | 41 static bool IsDtlsClientHelloPacket(const char* data, size_t len) { |
47 if (!IsDtlsPacket(data, len)) { | 42 if (!IsDtlsPacket(data, len)) { |
48 return false; | 43 return false; |
49 } | 44 } |
50 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); | 45 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); |
51 return len > 17 && u[0] == 22 && u[13] == 1; | 46 return len > 17 && u[0] == 22 && u[13] == 1; |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 set_dtls_state(DTLS_TRANSPORT_CLOSED); | 596 set_dtls_state(DTLS_TRANSPORT_CLOSED); |
602 } else { | 597 } else { |
603 LOG_J(LS_INFO, this) << "DTLS transport error, code=" << err; | 598 LOG_J(LS_INFO, this) << "DTLS transport error, code=" << err; |
604 set_dtls_state(DTLS_TRANSPORT_FAILED); | 599 set_dtls_state(DTLS_TRANSPORT_FAILED); |
605 } | 600 } |
606 } | 601 } |
607 } | 602 } |
608 | 603 |
609 void DtlsTransport::MaybeStartDtls() { | 604 void DtlsTransport::MaybeStartDtls() { |
610 if (dtls_ && ice_transport_->writable()) { | 605 if (dtls_ && ice_transport_->writable()) { |
611 ConfigureHandshakeTimeout(); | |
612 | |
613 if (dtls_->StartSSL()) { | 606 if (dtls_->StartSSL()) { |
614 // This should never fail: | 607 // This should never fail: |
615 // Because we are operating in a nonblocking mode and all | 608 // Because we are operating in a nonblocking mode and all |
616 // incoming packets come in via OnReadPacket(), which rejects | 609 // incoming packets come in via OnReadPacket(), which rejects |
617 // packets in this state, the incoming queue must be empty. We | 610 // packets in this state, the incoming queue must be empty. We |
618 // ignore write errors, thus any errors must be because of | 611 // ignore write errors, thus any errors must be because of |
619 // configuration and therefore are our fault. | 612 // configuration and therefore are our fault. |
620 RTC_NOTREACHED() << "StartSSL failed."; | 613 RTC_NOTREACHED() << "StartSSL failed."; |
621 LOG_J(LS_ERROR, this) << "Couldn't start DTLS handshake"; | 614 LOG_J(LS_ERROR, this) << "Couldn't start DTLS handshake"; |
622 set_dtls_state(DTLS_TRANSPORT_FAILED); | 615 set_dtls_state(DTLS_TRANSPORT_FAILED); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 LOG_J(LS_VERBOSE, this) << "set_dtls_state from:" << dtls_state_ << " to " | 686 LOG_J(LS_VERBOSE, this) << "set_dtls_state from:" << dtls_state_ << " to " |
694 << state; | 687 << state; |
695 dtls_state_ = state; | 688 dtls_state_ = state; |
696 SignalDtlsState(this, state); | 689 SignalDtlsState(this, state); |
697 } | 690 } |
698 | 691 |
699 void DtlsTransport::OnDtlsHandshakeError(rtc::SSLHandshakeError error) { | 692 void DtlsTransport::OnDtlsHandshakeError(rtc::SSLHandshakeError error) { |
700 SignalDtlsHandshakeError(error); | 693 SignalDtlsHandshakeError(error); |
701 } | 694 } |
702 | 695 |
703 void DtlsTransport::ConfigureHandshakeTimeout() { | |
704 RTC_DCHECK(dtls_); | |
705 rtc::Optional<int> rtt = ice_transport_->GetRttEstimate(); | |
706 if (rtt) { | |
707 // Limit the timeout to a reasonable range in case the ICE RTT takes | |
708 // extreme values. | |
709 int initial_timeout = std::max(kMinHandshakeTimeout, | |
710 std::min(kMaxHandshakeTimeout, | |
711 2 * (*rtt))); | |
712 LOG_J(LS_INFO, this) << "configuring DTLS handshake timeout " | |
713 << initial_timeout << " based on ICE RTT " << *rtt; | |
714 | |
715 dtls_->SetInitialRetransmissionTimeout(initial_timeout); | |
716 } else { | |
717 LOG_J(LS_INFO, this) | |
718 << "no RTT estimate - using default DTLS handshake timeout"; | |
719 } | |
720 } | |
721 | |
722 | |
723 } // namespace cricket | 696 } // namespace cricket |
OLD | NEW |