Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / quic / crypto / source_address_token.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_CRYPTO_SOURCE_ADDRESS_TOKEN_H_
6 #define NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/strings/string_piece.h"
12 #include "net/base/net_export.h"
13
14 namespace net {
15
16 // TODO(rtenneti): sync with server more rationally.
17 // CachedNetworkParameters contains data that can be used to choose appropriate
18 // connection parameters (initial RTT, initial CWND, etc.) in new connections.
19 class NET_EXPORT_PRIVATE CachedNetworkParameters {
20  public:
21   // Describes the state of the connection during which the supplied network
22   // parameters were calculated.
23   enum PreviousConnectionState {
24     SLOW_START = 0,
25     CONGESTION_AVOIDANCE = 1,
26   };
27
28   CachedNetworkParameters();
29   ~CachedNetworkParameters();
30
31   bool operator==(const CachedNetworkParameters& other) const;
32   bool operator!=(const CachedNetworkParameters& other) const;
33
34   std::string serving_region() const {
35     return serving_region_;
36   }
37   void set_serving_region(base::StringPiece serving_region) {
38     serving_region_ = serving_region.as_string();
39   }
40
41   int32 bandwidth_estimate_bytes_per_second() const {
42     return bandwidth_estimate_bytes_per_second_;
43   }
44   void set_bandwidth_estimate_bytes_per_second(
45       int32 bandwidth_estimate_bytes_per_second) {
46     bandwidth_estimate_bytes_per_second_ = bandwidth_estimate_bytes_per_second;
47   }
48
49   int32 max_bandwidth_estimate_bytes_per_second() const {
50     return max_bandwidth_estimate_bytes_per_second_;
51   }
52   void set_max_bandwidth_estimate_bytes_per_second(
53       int32 max_bandwidth_estimate_bytes_per_second) {
54     max_bandwidth_estimate_bytes_per_second_ =
55         max_bandwidth_estimate_bytes_per_second;
56   }
57
58   int64 max_bandwidth_timestamp_seconds() const {
59     return max_bandwidth_timestamp_seconds_;
60   }
61   void set_max_bandwidth_timestamp_seconds(
62       int64 max_bandwidth_timestamp_seconds) {
63     max_bandwidth_timestamp_seconds_ = max_bandwidth_timestamp_seconds;
64   }
65
66   int32 min_rtt_ms() const {
67     return min_rtt_ms_;
68   }
69   void set_min_rtt_ms(int32 min_rtt_ms) {
70     min_rtt_ms_ = min_rtt_ms;
71   }
72
73   int32 previous_connection_state() const {
74     return previous_connection_state_;
75   }
76   void set_previous_connection_state(int32 previous_connection_state) {
77     previous_connection_state_ = previous_connection_state;
78   }
79
80   int64 timestamp() const { return timestamp_; }
81   void set_timestamp(int64 timestamp) { timestamp_ = timestamp; }
82
83  private:
84   // serving_region_ is used to decide whether or not the bandwidth estimate and
85   // min RTT are reasonable and if they should be used.
86   // For example a group of geographically close servers may share the same
87   // serving_region_ string if they are expected to have similar network
88   // performance.
89   std::string serving_region_;
90   // The server can supply a bandwidth estimate (in bytes/s) which it may re-use
91   // on receipt of a source-address token with this field set.
92   int32 bandwidth_estimate_bytes_per_second_;
93   // The maximum bandwidth seen by the client, not necessarily the latest.
94   int32 max_bandwidth_estimate_bytes_per_second_;
95   // Timestamp (seconds since UNIX epoch) that indicates when the max bandwidth
96   // was seen by the server.
97   int64 max_bandwidth_timestamp_seconds_;
98   // The min RTT seen on a previous connection can be used by the server to
99   // inform initial connection parameters for new connections.
100   int32 min_rtt_ms_;
101   // Encodes the PreviousConnectionState enum.
102   int32 previous_connection_state_;
103   // UNIX timestamp when this bandwidth estimate was created.
104   int64 timestamp_;
105 };
106
107 // TODO(rtenneti): sync with server more rationally.
108 // A SourceAddressToken is serialised, encrypted and sent to clients so that
109 // they can prove ownership of an IP address.
110 class NET_EXPORT_PRIVATE SourceAddressToken {
111  public:
112   SourceAddressToken();
113   ~SourceAddressToken();
114
115   std::string SerializeAsString() const;
116
117   bool ParseFromArray(const char* plaintext, size_t plaintext_length);
118
119   std::string ip() const {
120     return ip_;
121   }
122   void set_ip(base::StringPiece ip) {
123     ip_ = ip.as_string();
124   }
125
126   int64 timestamp() const {
127     return timestamp_;
128   }
129   void set_timestamp(int64 timestamp) {
130     timestamp_ = timestamp;
131   }
132
133   const CachedNetworkParameters& cached_network_parameters() const {
134     return cached_network_parameters_;
135   }
136   void set_cached_network_parameters(
137       const CachedNetworkParameters& cached_network_parameters) {
138     cached_network_parameters_ = cached_network_parameters;
139     has_cached_network_parameters_ = true;
140   }
141   bool has_cached_network_parameters() const {
142     return has_cached_network_parameters_;
143   }
144
145  private:
146   // ip_ contains either 4 (IPv4) or 16 (IPv6) bytes of IP address in network
147   // byte order.
148   std::string ip_;
149   // timestamp_ contains a UNIX timestamp value of the time when the token was
150   // created.
151   int64 timestamp_;
152
153   // The server can provide estimated network parameters to be used for
154   // initial parameter selection in future connections.
155   CachedNetworkParameters cached_network_parameters_;
156   // TODO(rtenneti): Delete |has_cached_network_parameters_| after we convert
157   // SourceAddressToken to protobuf.
158   bool has_cached_network_parameters_;
159
160   DISALLOW_COPY_AND_ASSIGN(SourceAddressToken);
161 };
162
163 }  // namespace net
164
165 #endif  // NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_