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