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_CRYPTO_SOURCE_ADDRESS_TOKEN_H_
6 #define NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_
10 #include "base/basictypes.h"
11 #include "base/strings/string_piece.h"
12 #include "net/base/net_export.h"
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 {
21 // Describes the state of the connection during which the supplied network
22 // parameters were calculated.
23 enum PreviousConnectionState {
25 CONGESTION_AVOIDANCE = 1,
28 CachedNetworkParameters();
29 ~CachedNetworkParameters();
31 bool operator==(const CachedNetworkParameters& other) const;
32 bool operator!=(const CachedNetworkParameters& other) const;
34 std::string serving_region() const {
35 return serving_region_;
37 void set_serving_region(base::StringPiece serving_region) {
38 serving_region_ = serving_region.as_string();
41 int32 bandwidth_estimate_bytes_per_second() const {
42 return bandwidth_estimate_bytes_per_second_;
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;
49 int32 max_bandwidth_estimate_bytes_per_second() const {
50 return max_bandwidth_estimate_bytes_per_second_;
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;
58 int64 max_bandwidth_timestamp_seconds() const {
59 return max_bandwidth_timestamp_seconds_;
61 void set_max_bandwidth_timestamp_seconds(
62 int64 max_bandwidth_timestamp_seconds) {
63 max_bandwidth_timestamp_seconds_ = max_bandwidth_timestamp_seconds;
66 int32 min_rtt_ms() const {
69 void set_min_rtt_ms(int32 min_rtt_ms) {
70 min_rtt_ms_ = min_rtt_ms;
73 int32 previous_connection_state() const {
74 return previous_connection_state_;
76 void set_previous_connection_state(int32 previous_connection_state) {
77 previous_connection_state_ = previous_connection_state;
80 int64 timestamp() const { return timestamp_; }
81 void set_timestamp(int64 timestamp) { timestamp_ = timestamp; }
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
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.
101 // Encodes the PreviousConnectionState enum.
102 int32 previous_connection_state_;
103 // UNIX timestamp when this bandwidth estimate was created.
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 {
112 SourceAddressToken();
113 ~SourceAddressToken();
115 std::string SerializeAsString() const;
117 bool ParseFromArray(const char* plaintext, size_t plaintext_length);
119 std::string ip() const {
122 void set_ip(base::StringPiece ip) {
123 ip_ = ip.as_string();
126 int64 timestamp() const {
129 void set_timestamp(int64 timestamp) {
130 timestamp_ = timestamp;
133 const CachedNetworkParameters& cached_network_parameters() const {
134 return cached_network_parameters_;
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;
141 bool has_cached_network_parameters() const {
142 return has_cached_network_parameters_;
146 // ip_ contains either 4 (IPv4) or 16 (IPv6) bytes of IP address in network
149 // timestamp_ contains a UNIX timestamp value of the time when the token was
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_;
160 DISALLOW_COPY_AND_ASSIGN(SourceAddressToken);
165 #endif // NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_