Home | History | Annotate | Download | only in quic
      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/crypto/crypto_utils.h"
     13 #include "net/quic/quic_protocol.h"
     14 #include "net/quic/quic_time.h"
     15 #include "net/quic/quic_utils.h"
     16 
     17 namespace net {
     18 
     19 class NET_EXPORT_PRIVATE QuicNegotiableValue {
     20  public:
     21   enum Presence {
     22     // This negotiable value can be absent from the handshake message. Default
     23     // value is selected as the negotiated value in such a case.
     24     PRESENCE_OPTIONAL,
     25     // This negotiable value is required in the handshake message otherwise the
     26     // Process*Hello function returns an error.
     27     PRESENCE_REQUIRED,
     28   };
     29 
     30   QuicNegotiableValue(QuicTag tag, Presence presence);
     31 
     32   bool negotiated() const {
     33     return negotiated_;
     34   }
     35 
     36  protected:
     37   const QuicTag tag_;
     38   const Presence presence_;
     39   bool negotiated_;
     40 };
     41 
     42 class NET_EXPORT_PRIVATE QuicNegotiableUint32 : public QuicNegotiableValue {
     43  public:
     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   bool negotiated();
    163 
    164   // SetDefaults sets the members to sensible, default values.
    165   void SetDefaults();
    166 
    167   // ToHandshakeMessage serializes the settings in this object as a series of
    168   // tags /value pairs and adds them to |out|.
    169   void ToHandshakeMessage(CryptoHandshakeMessage* out) const;
    170 
    171   // Calls ProcessClientHello on each negotiable parameter. On failure returns
    172   // the corresponding QuicErrorCode and sets detailed error in |error_details|.
    173   QuicErrorCode ProcessClientHello(const CryptoHandshakeMessage& client_hello,
    174                                    std::string* error_details);
    175 
    176   // Calls ProcessServerHello on each negotiable parameter. On failure returns
    177   // the corresponding QuicErrorCode and sets detailed error in |error_details|.
    178   QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello,
    179                                    std::string* error_details);
    180 
    181  private:
    182   // Congestion control feedback type.
    183   QuicNegotiableTag congestion_control_;
    184   // Idle connection state lifetime
    185   QuicNegotiableUint32 idle_connection_state_lifetime_seconds_;
    186   // Keepalive timeout, or 0 to turn off keepalive probes
    187   QuicNegotiableUint32 keepalive_timeout_seconds_;
    188   // Maximum number of streams that the connection can support.
    189   QuicNegotiableUint32 max_streams_per_connection_;
    190   // Maximum time till the session can be alive before crypto handshake is
    191   // finished. (Not negotiated).
    192   QuicTime::Delta max_time_before_crypto_handshake_;
    193 };
    194 
    195 }  // namespace net
    196 
    197 #endif  // NET_QUIC_QUIC_CONFIG_H_
    198