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. 4 5 #ifndef NET_QUIC_QUIC_CONFIG_H_ 6 #define NET_QUIC_QUIC_CONFIG_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "net/quic/crypto/crypto_handshake.h" 12 #include "net/quic/quic_protocol.h" 13 #include "net/quic/quic_time.h" 14 #include "net/quic/quic_utils.h" 15 16 namespace net { 17 18 class NET_EXPORT_PRIVATE QuicNegotiableValue { 19 public: 20 enum Presence { 21 // This negotiable value can be absent from the handshake message. Default 22 // value is selected as the negotiated value in such a case. 23 PRESENCE_OPTIONAL, 24 // This negotiable value is required in the handshake message otherwise the 25 // Process*Hello function returns an error. 26 PRESENCE_REQUIRED, 27 }; 28 29 QuicNegotiableValue(QuicTag tag, Presence presence); 30 negotiated()31 bool negotiated() const { 32 return negotiated_; 33 } 34 35 protected: 36 const QuicTag tag_; 37 const Presence presence_; 38 bool negotiated_; 39 }; 40 41 class NET_EXPORT_PRIVATE QuicNegotiableUint32 : public QuicNegotiableValue { 42 public: 43 // Default and max values default to 0. 44 QuicNegotiableUint32(QuicTag name, Presence presence); 45 46 // Sets the maximum possible value that can be achieved after negotiation and 47 // also the default values to be assumed if PRESENCE_OPTIONAL and the *HLO msg 48 // doesn't contain a value corresponding to |name_|. |max| is serialised via 49 // ToHandshakeMessage call if |negotiated_| is false. 50 void set(uint32 max, uint32 default_value); 51 52 // Returns the value negotiated if |negotiated_| is true, otherwise returns 53 // default_value_ (used to set default values before negotiation finishes). 54 uint32 GetUint32() const; 55 56 // Serialises |name_| and value to |out|. If |negotiated_| is true then 57 // |negotiated_value_| is serialised, otherwise |max_value_| is serialised. 58 void ToHandshakeMessage(CryptoHandshakeMessage* out) const; 59 60 // Sets |negotiated_value_| to the minimum of |max_value_| and the 61 // corresponding value from |client_hello|. If the corresponding value is 62 // missing and PRESENCE_OPTIONAL then |negotiated_value_| is set to 63 // |default_value_|. 64 QuicErrorCode ProcessClientHello(const CryptoHandshakeMessage& client_hello, 65 std::string* error_details); 66 67 // Sets the |negotiated_value_| to the corresponding value from 68 // |server_hello|. Returns error if the value received in |server_hello| is 69 // greater than |max_value_|. If the corresponding value is missing and 70 // PRESENCE_OPTIONAL then |negotiated_value_| is set to |0|, 71 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, 72 std::string* error_details); 73 74 private: 75 // Reads the value corresponding to |name_| from |msg| into |out|. If the 76 // |name_| is absent in |msg| and |presence_| is set to OPTIONAL |out| is set 77 // to |max_value_|. 78 QuicErrorCode ReadUint32(const CryptoHandshakeMessage& msg, 79 uint32* out, 80 std::string* error_details) const; 81 82 uint32 max_value_; 83 uint32 default_value_; 84 uint32 negotiated_value_; 85 }; 86 87 class NET_EXPORT_PRIVATE QuicNegotiableTag : public QuicNegotiableValue { 88 public: 89 QuicNegotiableTag(QuicTag name, Presence presence); 90 ~QuicNegotiableTag(); 91 92 // Sets the possible values that |negotiated_tag_| can take after negotiation 93 // and the default value that |negotiated_tag_| takes if OPTIONAL and *HLO 94 // msg doesn't contain tag |name_|. 95 void set(const QuicTagVector& possible_values, QuicTag default_value); 96 97 // Returns the negotiated tag if |negotiated_| is true, otherwise returns 98 // |default_value_| (used to set default values before negotiation finishes). 99 QuicTag GetTag() const; 100 101 // Serialises |name_| and vector (either possible or negotiated) to |out|. If 102 // |negotiated_| is true then |negotiated_tag_| is serialised, otherwise 103 // |possible_values_| is serialised. 104 void ToHandshakeMessage(CryptoHandshakeMessage* out) const; 105 106 // Selects the tag common to both tags in |client_hello| for |name_| and 107 // |possible_values_| with preference to tag in |possible_values_|. The 108 // selected tag is set as |negotiated_tag_|. 109 QuicErrorCode ProcessClientHello(const CryptoHandshakeMessage& client_hello, 110 std::string* error_details); 111 112 // Sets the value for |name_| tag in |server_hello| as |negotiated_value_|. 113 // Returns error if the value received in |server_hello| isn't present in 114 // |possible_values_|. 115 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, 116 std::string* error_details); 117 118 private: 119 // Reads the vector corresponding to |name_| from |msg| into |out|. If the 120 // |name_| is absent in |msg| and |presence_| is set to OPTIONAL |out| is set 121 // to |possible_values_|. 122 QuicErrorCode ReadVector(const CryptoHandshakeMessage& msg, 123 const QuicTag** out, 124 size_t* out_length, 125 std::string* error_details) const; 126 127 QuicTag negotiated_tag_; 128 QuicTagVector possible_values_; 129 QuicTag default_value_; 130 }; 131 132 // QuicConfig contains non-crypto configuration options that are negotiated in 133 // the crypto handshake. 134 class NET_EXPORT_PRIVATE QuicConfig { 135 public: 136 QuicConfig(); 137 ~QuicConfig(); 138 139 void set_congestion_control(const QuicTagVector& congestion_control, 140 QuicTag default_congestion_control); 141 142 QuicTag congestion_control() const; 143 144 void set_idle_connection_state_lifetime( 145 QuicTime::Delta max_idle_connection_state_lifetime, 146 QuicTime::Delta default_idle_conection_state_lifetime); 147 148 QuicTime::Delta idle_connection_state_lifetime() const; 149 150 QuicTime::Delta keepalive_timeout() const; 151 152 void set_max_streams_per_connection(size_t max_streams, 153 size_t default_streams); 154 155 uint32 max_streams_per_connection() const; 156 157 void set_max_time_before_crypto_handshake( 158 QuicTime::Delta max_time_before_crypto_handshake); 159 160 QuicTime::Delta max_time_before_crypto_handshake() const; 161 162 // Sets the server's TCP sender's max and default initial congestion window 163 // in packets. 164 void set_server_initial_congestion_window(size_t max_initial_window, 165 size_t default_initial_window); 166 167 uint32 server_initial_congestion_window() const; 168 169 // Sets an estimated initial round trip time in us. 170 void set_initial_round_trip_time_us(size_t max_rtt, size_t default_rtt); 171 172 uint32 initial_round_trip_time_us() const; 173 174 bool negotiated(); 175 176 // SetDefaults sets the members to sensible, default values. 177 void SetDefaults(); 178 179 // ToHandshakeMessage serializes the settings in this object as a series of 180 // tags /value pairs and adds them to |out|. 181 void ToHandshakeMessage(CryptoHandshakeMessage* out) const; 182 183 // Calls ProcessClientHello on each negotiable parameter. On failure returns 184 // the corresponding QuicErrorCode and sets detailed error in |error_details|. 185 QuicErrorCode ProcessClientHello(const CryptoHandshakeMessage& client_hello, 186 std::string* error_details); 187 188 // Calls ProcessServerHello on each negotiable parameter. On failure returns 189 // the corresponding QuicErrorCode and sets detailed error in |error_details|. 190 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, 191 std::string* error_details); 192 193 private: 194 // Congestion control feedback type. 195 QuicNegotiableTag congestion_control_; 196 // Idle connection state lifetime 197 QuicNegotiableUint32 idle_connection_state_lifetime_seconds_; 198 // Keepalive timeout, or 0 to turn off keepalive probes 199 QuicNegotiableUint32 keepalive_timeout_seconds_; 200 // Maximum number of streams that the connection can support. 201 QuicNegotiableUint32 max_streams_per_connection_; 202 // Maximum time till the session can be alive before crypto handshake is 203 // finished. (Not negotiated). 204 QuicTime::Delta max_time_before_crypto_handshake_; 205 // Initial congestion window in packets. 206 QuicNegotiableUint32 server_initial_congestion_window_; 207 // Initial round trip time estimate in microseconds. 208 QuicNegotiableUint32 initial_round_trip_time_us_; 209 }; 210 211 } // namespace net 212 213 #endif // NET_QUIC_QUIC_CONFIG_H_ 214