1 // Copyright (c) 2013 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.
5 #ifndef NET_QUIC_QUIC_CONFIG_H_
6 #define NET_QUIC_QUIC_CONFIG_H_
10 #include "base/basictypes.h"
11 #include "net/quic/quic_protocol.h"
12 #include "net/quic/quic_time.h"
20 class CryptoHandshakeMessage
;
22 // Describes whether or not a given QuicTag is required or optional in the
24 enum QuicConfigPresence
{
25 // This negotiable value can be absent from the handshake message. Default
26 // value is selected as the negotiated value in such a case.
28 // This negotiable value is required in the handshake message otherwise the
29 // Process*Hello function returns an error.
33 // Whether the CryptoHandshakeMessage is from the client or server.
39 // An abstract base class that stores a value that can be sent in CHLO/SHLO
40 // message. These values can be OPTIONAL or REQUIRED, depending on |presence_|.
41 class NET_EXPORT_PRIVATE QuicConfigValue
{
43 QuicConfigValue(QuicTag tag
, QuicConfigPresence presence
);
44 virtual ~QuicConfigValue();
46 // Serialises tag name and value(s) to |out|.
47 virtual void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const = 0;
49 // Selects a mutually acceptable value from those offered in |peer_hello|
50 // and those defined in the subclass.
51 virtual QuicErrorCode
ProcessPeerHello(
52 const CryptoHandshakeMessage
& peer_hello
,
54 std::string
* error_details
) = 0;
58 const QuicConfigPresence presence_
;
61 class NET_EXPORT_PRIVATE QuicNegotiableValue
: public QuicConfigValue
{
63 QuicNegotiableValue(QuicTag tag
, QuicConfigPresence presence
);
64 ~QuicNegotiableValue() override
;
66 bool negotiated() const {
71 void set_negotiated(bool negotiated
) { negotiated_
= negotiated
; }
77 class NET_EXPORT_PRIVATE QuicNegotiableUint32
: public QuicNegotiableValue
{
79 // Default and max values default to 0.
80 QuicNegotiableUint32(QuicTag name
, QuicConfigPresence presence
);
81 ~QuicNegotiableUint32() override
;
83 // Sets the maximum possible value that can be achieved after negotiation and
84 // also the default values to be assumed if PRESENCE_OPTIONAL and the *HLO msg
85 // doesn't contain a value corresponding to |name_|. |max| is serialised via
86 // ToHandshakeMessage call if |negotiated_| is false.
87 void set(uint32 max
, uint32 default_value
);
89 // Returns the value negotiated if |negotiated_| is true, otherwise returns
90 // default_value_ (used to set default values before negotiation finishes).
91 uint32
GetUint32() const;
93 // Serialises |name_| and value to |out|. If |negotiated_| is true then
94 // |negotiated_value_| is serialised, otherwise |max_value_| is serialised.
95 void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const override
;
97 // Sets |negotiated_value_| to the minimum of |max_value_| and the
98 // corresponding value from |peer_hello|. If the corresponding value is
99 // missing and PRESENCE_OPTIONAL then |negotiated_value_| is set to
101 QuicErrorCode
ProcessPeerHello(const CryptoHandshakeMessage
& peer_hello
,
102 HelloType hello_type
,
103 std::string
* error_details
) override
;
107 uint32 default_value_
;
108 uint32 negotiated_value_
;
111 class NET_EXPORT_PRIVATE QuicNegotiableTag
: public QuicNegotiableValue
{
113 QuicNegotiableTag(QuicTag name
, QuicConfigPresence presence
);
114 ~QuicNegotiableTag() override
;
116 // Sets the possible values that |negotiated_tag_| can take after negotiation
117 // and the default value that |negotiated_tag_| takes if OPTIONAL and *HLO
118 // msg doesn't contain tag |name_|.
119 void set(const QuicTagVector
& possible_values
, QuicTag default_value
);
121 // Serialises |name_| and vector (either possible or negotiated) to |out|. If
122 // |negotiated_| is true then |negotiated_tag_| is serialised, otherwise
123 // |possible_values_| is serialised.
124 void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const override
;
126 // Selects the tag common to both tags in |client_hello| for |name_| and
127 // |possible_values_| with preference to tag in |possible_values_|. The
128 // selected tag is set as |negotiated_tag_|.
129 QuicErrorCode
ProcessPeerHello(const CryptoHandshakeMessage
& peer_hello
,
130 HelloType hello_type
,
131 std::string
* error_details
) override
;
134 // Reads the vector corresponding to |name_| from |msg| into |out|. If the
135 // |name_| is absent in |msg| and |presence_| is set to OPTIONAL |out| is set
136 // to |possible_values_|.
137 QuicErrorCode
ReadVector(const CryptoHandshakeMessage
& msg
,
140 std::string
* error_details
) const;
142 QuicTag negotiated_tag_
;
143 QuicTagVector possible_values_
;
144 QuicTag default_value_
;
147 // Stores uint32 from CHLO or SHLO messages that are not negotiated.
148 class NET_EXPORT_PRIVATE QuicFixedUint32
: public QuicConfigValue
{
150 QuicFixedUint32(QuicTag name
, QuicConfigPresence presence
);
151 ~QuicFixedUint32() override
;
153 bool HasSendValue() const;
155 uint32
GetSendValue() const;
157 void SetSendValue(uint32 value
);
159 bool HasReceivedValue() const;
161 uint32
GetReceivedValue() const;
163 void SetReceivedValue(uint32 value
);
165 // If has_send_value is true, serialises |tag_| and |send_value_| to |out|.
166 void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const override
;
168 // Sets |value_| to the corresponding value from |peer_hello_| if it exists.
169 QuicErrorCode
ProcessPeerHello(const CryptoHandshakeMessage
& peer_hello
,
170 HelloType hello_type
,
171 std::string
* error_details
) override
;
175 bool has_send_value_
;
176 uint32 receive_value_
;
177 bool has_receive_value_
;
180 // Stores tag from CHLO or SHLO messages that are not negotiated.
181 class NET_EXPORT_PRIVATE QuicFixedTagVector
: public QuicConfigValue
{
183 QuicFixedTagVector(QuicTag name
, QuicConfigPresence presence
);
184 ~QuicFixedTagVector() override
;
186 bool HasSendValues() const;
188 QuicTagVector
GetSendValues() const;
190 void SetSendValues(const QuicTagVector
& values
);
192 bool HasReceivedValues() const;
194 QuicTagVector
GetReceivedValues() const;
196 void SetReceivedValues(const QuicTagVector
& values
);
198 // If has_send_value is true, serialises |tag_vector_| and |send_value_| to
200 void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const override
;
202 // Sets |receive_values_| to the corresponding value from |client_hello_| if
204 QuicErrorCode
ProcessPeerHello(const CryptoHandshakeMessage
& peer_hello
,
205 HelloType hello_type
,
206 std::string
* error_details
) override
;
209 QuicTagVector send_values_
;
210 bool has_send_values_
;
211 QuicTagVector receive_values_
;
212 bool has_receive_values_
;
215 // QuicConfig contains non-crypto configuration options that are negotiated in
216 // the crypto handshake.
217 class NET_EXPORT_PRIVATE QuicConfig
{
222 void SetConnectionOptionsToSend(const QuicTagVector
& connection_options
);
224 bool HasReceivedConnectionOptions() const;
226 QuicTagVector
ReceivedConnectionOptions() const;
228 bool HasSendConnectionOptions() const;
230 QuicTagVector
SendConnectionOptions() const;
232 void SetIdleConnectionStateLifetime(
233 QuicTime::Delta max_idle_connection_state_lifetime
,
234 QuicTime::Delta default_idle_conection_state_lifetime
);
236 QuicTime::Delta
IdleConnectionStateLifetime() const;
238 void SetSilentClose(bool silent_close
);
240 bool SilentClose() const;
242 void SetMaxStreamsPerConnection(size_t max_streams
, size_t default_streams
);
244 uint32
MaxStreamsPerConnection() const;
246 void set_max_time_before_crypto_handshake(
247 QuicTime::Delta max_time_before_crypto_handshake
) {
248 max_time_before_crypto_handshake_
= max_time_before_crypto_handshake
;
251 QuicTime::Delta
max_time_before_crypto_handshake() const {
252 return max_time_before_crypto_handshake_
;
255 void set_max_idle_time_before_crypto_handshake(
256 QuicTime::Delta max_idle_time_before_crypto_handshake
) {
257 max_idle_time_before_crypto_handshake_
=
258 max_idle_time_before_crypto_handshake
;
261 QuicTime::Delta
max_idle_time_before_crypto_handshake() const {
262 return max_idle_time_before_crypto_handshake_
;
265 void set_max_undecryptable_packets(size_t max_undecryptable_packets
) {
266 max_undecryptable_packets_
= max_undecryptable_packets
;
269 size_t max_undecryptable_packets() const {
270 return max_undecryptable_packets_
;
273 bool HasSetBytesForConnectionIdToSend() const;
275 // Sets the peer's connection id length, in bytes.
276 void SetBytesForConnectionIdToSend(uint32 bytes
);
278 bool HasReceivedBytesForConnectionId() const;
280 uint32
ReceivedBytesForConnectionId() const;
282 // Sets an estimated initial round trip time in us.
283 void SetInitialRoundTripTimeUsToSend(uint32 rtt_us
);
285 bool HasReceivedInitialRoundTripTimeUs() const;
287 uint32
ReceivedInitialRoundTripTimeUs() const;
289 bool HasInitialRoundTripTimeUsToSend() const;
291 uint32
GetInitialRoundTripTimeUsToSend() const;
293 // Sets an initial stream flow control window size to transmit to the peer.
294 void SetInitialStreamFlowControlWindowToSend(uint32 window_bytes
);
296 uint32
GetInitialStreamFlowControlWindowToSend() const;
298 bool HasReceivedInitialStreamFlowControlWindowBytes() const;
300 uint32
ReceivedInitialStreamFlowControlWindowBytes() const;
302 // Sets an initial session flow control window size to transmit to the peer.
303 void SetInitialSessionFlowControlWindowToSend(uint32 window_bytes
);
305 uint32
GetInitialSessionFlowControlWindowToSend() const;
307 bool HasReceivedInitialSessionFlowControlWindowBytes() const;
309 uint32
ReceivedInitialSessionFlowControlWindowBytes() const;
311 // Sets socket receive buffer to transmit to the peer.
312 void SetSocketReceiveBufferToSend(uint32 window_bytes
);
314 bool HasReceivedSocketReceiveBuffer() const;
316 uint32
ReceivedSocketReceiveBuffer() const;
318 bool negotiated() const;
320 // ToHandshakeMessage serialises the settings in this object as a series of
321 // tags /value pairs and adds them to |out|.
322 void ToHandshakeMessage(CryptoHandshakeMessage
* out
) const;
324 // Calls ProcessPeerHello on each negotiable parameter. On failure returns
325 // the corresponding QuicErrorCode and sets detailed error in |error_details|.
326 QuicErrorCode
ProcessPeerHello(const CryptoHandshakeMessage
& peer_hello
,
327 HelloType hello_type
,
328 std::string
* error_details
);
331 friend class test::QuicConfigPeer
;
333 // SetDefaults sets the members to sensible, default values.
336 // Configurations options that are not negotiated.
337 // Maximum time the session can be alive before crypto handshake is finished.
338 QuicTime::Delta max_time_before_crypto_handshake_
;
339 // Maximum idle time before the crypto handshake has completed.
340 QuicTime::Delta max_idle_time_before_crypto_handshake_
;
341 // Maximum number of undecryptable packets stored before CHLO/SHLO.
342 size_t max_undecryptable_packets_
;
344 // Connection options.
345 QuicFixedTagVector connection_options_
;
346 // Idle connection state lifetime
347 QuicNegotiableUint32 idle_connection_state_lifetime_seconds_
;
348 // Whether to use silent close. Defaults to 0 (false) and is otherwise true.
349 QuicNegotiableUint32 silent_close_
;
350 // Maximum number of streams that the connection can support.
351 QuicNegotiableUint32 max_streams_per_connection_
;
352 // The number of bytes required for the connection ID.
353 QuicFixedUint32 bytes_for_connection_id_
;
354 // Initial round trip time estimate in microseconds.
355 QuicFixedUint32 initial_round_trip_time_us_
;
357 // Initial stream flow control receive window in bytes.
358 QuicFixedUint32 initial_stream_flow_control_window_bytes_
;
359 // Initial session flow control receive window in bytes.
360 QuicFixedUint32 initial_session_flow_control_window_bytes_
;
362 // Socket receive buffer in bytes.
363 QuicFixedUint32 socket_receive_buffer_
;
368 #endif // NET_QUIC_QUIC_CONFIG_H_