• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 // A server specific QuicSession subclass.
6 
7 #ifndef QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
8 #define QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
9 
10 #include <cstdint>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "quiche/quic/core/crypto/quic_compressed_certs_cache.h"
17 #include "quiche/quic/core/http/quic_spdy_session.h"
18 #include "quiche/quic/core/quic_crypto_server_stream_base.h"
19 #include "quiche/quic/core/quic_packets.h"
20 #include "quiche/quic/platform/api/quic_export.h"
21 
22 namespace quic {
23 
24 class QuicConfig;
25 class QuicConnection;
26 class QuicCryptoServerConfig;
27 
28 namespace test {
29 class QuicServerSessionBasePeer;
30 class QuicSimpleServerSessionPeer;
31 }  // namespace test
32 
33 class QUIC_EXPORT_PRIVATE QuicServerSessionBase : public QuicSpdySession {
34  public:
35   // Does not take ownership of |connection|. |crypto_config| must outlive the
36   // session. |helper| must outlive any created crypto streams.
37   QuicServerSessionBase(const QuicConfig& config,
38                         const ParsedQuicVersionVector& supported_versions,
39                         QuicConnection* connection,
40                         QuicSession::Visitor* visitor,
41                         QuicCryptoServerStreamBase::Helper* helper,
42                         const QuicCryptoServerConfig* crypto_config,
43                         QuicCompressedCertsCache* compressed_certs_cache);
44   QuicServerSessionBase(const QuicServerSessionBase&) = delete;
45   QuicServerSessionBase& operator=(const QuicServerSessionBase&) = delete;
46 
47   // Override the base class to cancel any ongoing asychronous crypto.
48   void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
49                           ConnectionCloseSource source) override;
50 
51   // Override to send bandwidth estimate.
52   void OnBandwidthUpdateTimeout() override;
53 
54   // Sends a server config update to the client, containing new bandwidth
55   // estimate.
56   void OnCongestionWindowChange(QuicTime now) override;
57 
58   ~QuicServerSessionBase() override;
59 
60   void Initialize() override;
61 
crypto_stream()62   const QuicCryptoServerStreamBase* crypto_stream() const {
63     return crypto_stream_.get();
64   }
65 
66   // Override base class to process bandwidth related config received from
67   // client.
68   void OnConfigNegotiated() override;
69 
set_serving_region(const std::string & serving_region)70   void set_serving_region(const std::string& serving_region) {
71     serving_region_ = serving_region;
72   }
73 
serving_region()74   const std::string& serving_region() const { return serving_region_; }
75 
76   QuicSSLConfig GetSSLConfig() const override;
77 
enable_sending_bandwidth_estimate_when_network_idle()78   bool enable_sending_bandwidth_estimate_when_network_idle() const {
79     return enable_sending_bandwidth_estimate_when_network_idle_;
80   }
81 
82  protected:
83   // QuicSession methods(override them with return type of QuicSpdyStream*):
84   QuicCryptoServerStreamBase* GetMutableCryptoStream() override;
85 
86   const QuicCryptoServerStreamBase* GetCryptoStream() const override;
87 
88   absl::optional<CachedNetworkParameters> GenerateCachedNetworkParameters()
89       const override;
90 
91   // If an outgoing stream can be created, return true.
92   // Return false when connection is closed or forward secure encryption hasn't
93   // established yet or number of server initiated streams already reaches the
94   // upper limit.
95   bool ShouldCreateOutgoingBidirectionalStream() override;
96   bool ShouldCreateOutgoingUnidirectionalStream() override;
97 
98   // If we should create an incoming stream, returns true. Otherwise
99   // does error handling, including communicating the error to the client and
100   // possibly closing the connection, and returns false.
101   bool ShouldCreateIncomingStream(QuicStreamId id) override;
102 
103   virtual std::unique_ptr<QuicCryptoServerStreamBase>
104   CreateQuicCryptoServerStream(
105       const QuicCryptoServerConfig* crypto_config,
106       QuicCompressedCertsCache* compressed_certs_cache) = 0;
107 
crypto_config()108   const QuicCryptoServerConfig* crypto_config() { return crypto_config_; }
109 
stream_helper()110   QuicCryptoServerStreamBase::Helper* stream_helper() { return helper_; }
111 
112  private:
113   friend class test::QuicServerSessionBasePeer;
114   friend class test::QuicSimpleServerSessionPeer;
115 
116   // Informs the QuicCryptoStream of the SETTINGS that will be used on this
117   // connection, so that the server crypto stream knows whether to accept 0-RTT
118   // data.
119   void SendSettingsToCryptoStream();
120 
121   const QuicCryptoServerConfig* crypto_config_;
122 
123   // The cache which contains most recently compressed certs.
124   // Owned by QuicDispatcher.
125   QuicCompressedCertsCache* compressed_certs_cache_;
126 
127   std::unique_ptr<QuicCryptoServerStreamBase> crypto_stream_;
128 
129   // Pointer to the helper used to create crypto server streams. Must outlive
130   // streams created via CreateQuicCryptoServerStream.
131   QuicCryptoServerStreamBase::Helper* helper_;
132 
133   // Whether bandwidth resumption is enabled for this connection.
134   bool bandwidth_resumption_enabled_;
135 
136   // The most recent bandwidth estimate sent to the client.
137   QuicBandwidth bandwidth_estimate_sent_to_client_;
138 
139   // Text describing server location. Sent to the client as part of the
140   // bandwidth estimate in the source-address token. Optional, can be left
141   // empty.
142   std::string serving_region_;
143 
144   // Time at which we send the last SCUP to the client.
145   QuicTime last_scup_time_;
146 
147   // Number of packets sent to the peer, at the time we last sent a SCUP.
148   QuicPacketNumber last_scup_packet_number_;
149 
150   // Converts QuicBandwidth to an int32 bytes/second that can be
151   // stored in CachedNetworkParameters.  TODO(jokulik): This function
152   // should go away once we fix http://b//27897982
153   int32_t BandwidthToCachedParameterBytesPerSecond(
154       const QuicBandwidth& bandwidth) const;
155 
156   bool enable_sending_bandwidth_estimate_when_network_idle_ = false;
157 };
158 
159 }  // namespace quic
160 
161 #endif  // QUICHE_QUIC_CORE_HTTP_QUIC_SERVER_SESSION_BASE_H_
162