wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/qbone/qbone_server_session.h" |
| 6 | |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 10 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/qbone/qbone_constants.h" |
dmcardle | d70b99e | 2019-12-12 09:52:39 -0800 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | bool QboneCryptoServerStreamHelper::CanAcceptClientHello( |
| 18 | const CryptoHandshakeMessage& chlo, |
| 19 | const QuicSocketAddress& client_address, |
| 20 | const QuicSocketAddress& peer_address, |
| 21 | const QuicSocketAddress& self_address, |
| 22 | string* error_details) const { |
dschinazi | b3b51de | 2019-12-19 16:52:04 -0800 | [diff] [blame] | 23 | quiche::QuicheStringPiece alpn; |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 24 | chlo.GetStringPiece(quic::kALPN, &alpn); |
| 25 | if (alpn != QboneConstants::kQboneAlpn) { |
| 26 | *error_details = "ALPN-indicated protocol is not qbone"; |
| 27 | return false; |
| 28 | } |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | QboneServerSession::QboneServerSession( |
| 33 | const quic::ParsedQuicVersionVector& supported_versions, |
| 34 | QuicConnection* connection, |
| 35 | Visitor* owner, |
| 36 | const QuicConfig& config, |
| 37 | const QuicCryptoServerConfig* quic_crypto_server_config, |
| 38 | QuicCompressedCertsCache* compressed_certs_cache, |
| 39 | QbonePacketWriter* writer, |
| 40 | QuicIpAddress self_ip, |
| 41 | QuicIpAddress client_ip, |
| 42 | size_t client_ip_subnet_length, |
| 43 | QboneServerControlStream::Handler* handler) |
| 44 | : QboneSessionBase(connection, owner, config, supported_versions, writer), |
| 45 | processor_(self_ip, client_ip, client_ip_subnet_length, this, this), |
| 46 | quic_crypto_server_config_(quic_crypto_server_config), |
| 47 | compressed_certs_cache_(compressed_certs_cache), |
| 48 | handler_(handler) {} |
| 49 | |
| 50 | QboneServerSession::~QboneServerSession() {} |
| 51 | |
| 52 | std::unique_ptr<QuicCryptoStream> QboneServerSession::CreateCryptoStream() { |
nharper | e5e28f9 | 2020-01-03 14:10:07 -0800 | [diff] [blame] | 53 | return CreateCryptoServerStream(quic_crypto_server_config_, |
| 54 | compressed_certs_cache_, this, |
| 55 | &stream_helper_); |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void QboneServerSession::Initialize() { |
| 59 | QboneSessionBase::Initialize(); |
| 60 | // Register the reserved control stream. |
| 61 | auto control_stream = |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 62 | std::make_unique<QboneServerControlStream>(this, handler_); |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 63 | control_stream_ = control_stream.get(); |
renjietang | 5c729f0 | 2019-09-06 12:43:48 -0700 | [diff] [blame] | 64 | ActivateStream(std::move(control_stream)); |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool QboneServerSession::SendClientRequest(const QboneClientRequest& request) { |
| 68 | if (!control_stream_) { |
| 69 | QUIC_BUG << "Cannot send client request before control stream is created."; |
| 70 | return false; |
| 71 | } |
| 72 | return control_stream_->SendRequest(request); |
| 73 | } |
| 74 | |
dmcardle | d70b99e | 2019-12-12 09:52:39 -0800 | [diff] [blame] | 75 | void QboneServerSession::ProcessPacketFromNetwork( |
| 76 | quiche::QuicheStringPiece packet) { |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 77 | string buffer = string(packet); |
| 78 | processor_.ProcessPacket(&buffer, |
| 79 | QbonePacketProcessor::Direction::FROM_NETWORK); |
| 80 | } |
| 81 | |
dmcardle | d70b99e | 2019-12-12 09:52:39 -0800 | [diff] [blame] | 82 | void QboneServerSession::ProcessPacketFromPeer( |
| 83 | quiche::QuicheStringPiece packet) { |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 84 | string buffer = string(packet); |
| 85 | processor_.ProcessPacket(&buffer, |
QUICHE team | 234c877 | 2019-12-06 14:14:12 -0800 | [diff] [blame] | 86 | QbonePacketProcessor::Direction::FROM_OFF_NETWORK); |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 87 | } |
| 88 | |
dmcardle | d70b99e | 2019-12-12 09:52:39 -0800 | [diff] [blame] | 89 | void QboneServerSession::SendPacketToClient(quiche::QuicheStringPiece packet) { |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 90 | SendPacketToPeer(packet); |
| 91 | } |
| 92 | |
dmcardle | d70b99e | 2019-12-12 09:52:39 -0800 | [diff] [blame] | 93 | void QboneServerSession::SendPacketToNetwork(quiche::QuicheStringPiece packet) { |
wub | f975eac | 2019-08-19 19:41:01 -0700 | [diff] [blame] | 94 | DCHECK(writer_ != nullptr); |
| 95 | writer_->WritePacketToNetwork(packet.data(), packet.size()); |
| 96 | } |
| 97 | |
| 98 | } // namespace quic |