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 serving_region()31 std::string serving_region() const { 32 return serving_region_; 33 } set_serving_region(base::StringPiece serving_region)34 void set_serving_region(base::StringPiece serving_region) { 35 serving_region_ = serving_region.as_string(); 36 } 37 bandwidth_estimate_bytes_per_second()38 int32 bandwidth_estimate_bytes_per_second() const { 39 return bandwidth_estimate_bytes_per_second_; 40 } set_bandwidth_estimate_bytes_per_second(int32 bandwidth_estimate_bytes_per_second)41 void set_bandwidth_estimate_bytes_per_second( 42 int32 bandwidth_estimate_bytes_per_second) { 43 bandwidth_estimate_bytes_per_second_ = bandwidth_estimate_bytes_per_second; 44 } 45 max_bandwidth_estimate_bytes_per_second()46 int32 max_bandwidth_estimate_bytes_per_second() const { 47 return max_bandwidth_estimate_bytes_per_second_; 48 } set_max_bandwidth_estimate_bytes_per_second(int32 max_bandwidth_estimate_bytes_per_second)49 void set_max_bandwidth_estimate_bytes_per_second( 50 int32 max_bandwidth_estimate_bytes_per_second) { 51 max_bandwidth_estimate_bytes_per_second_ = 52 max_bandwidth_estimate_bytes_per_second; 53 } 54 max_bandwidth_timestamp_seconds()55 int64 max_bandwidth_timestamp_seconds() const { 56 return max_bandwidth_timestamp_seconds_; 57 } set_max_bandwidth_timestamp_seconds(int64 max_bandwidth_timestamp_seconds)58 void set_max_bandwidth_timestamp_seconds( 59 int64 max_bandwidth_timestamp_seconds) { 60 max_bandwidth_timestamp_seconds_ = max_bandwidth_timestamp_seconds; 61 } 62 min_rtt_ms()63 int32 min_rtt_ms() const { 64 return min_rtt_ms_; 65 } set_min_rtt_ms(int32 min_rtt_ms)66 void set_min_rtt_ms(int32 min_rtt_ms) { 67 min_rtt_ms_ = min_rtt_ms; 68 } 69 previous_connection_state()70 int32 previous_connection_state() const { 71 return previous_connection_state_; 72 } set_previous_connection_state(int32 previous_connection_state)73 void set_previous_connection_state(int32 previous_connection_state) { 74 previous_connection_state_ = previous_connection_state; 75 } 76 77 private: 78 // serving_region_ is used to decide whether or not the bandwidth estimate and 79 // min RTT are reasonable and if they should be used. 80 // For example a group of geographically close servers may share the same 81 // serving_region_ string if they are expected to have similar network 82 // performance. 83 std::string serving_region_; 84 // The server can supply a bandwidth estimate (in bytes/s) which it may re-use 85 // on receipt of a source-address token with this field set. 86 int32 bandwidth_estimate_bytes_per_second_; 87 // The maximum bandwidth seen by the client, not necessarily the latest. 88 int32 max_bandwidth_estimate_bytes_per_second_; 89 // Timestamp (seconds since UNIX epoch) that indicates when the max bandwidth 90 // was seen by the server. 91 int64 max_bandwidth_timestamp_seconds_; 92 // The min RTT seen on a previous connection can be used by the server to 93 // inform initial connection parameters for new connections. 94 int32 min_rtt_ms_; 95 // Encodes the PreviousConnectionState enum. 96 int32 previous_connection_state_; 97 }; 98 99 // TODO(rtenneti): sync with server more rationally. 100 // A SourceAddressToken is serialised, encrypted and sent to clients so that 101 // they can prove ownership of an IP address. 102 class NET_EXPORT_PRIVATE SourceAddressToken { 103 public: 104 SourceAddressToken(); 105 ~SourceAddressToken(); 106 107 std::string SerializeAsString() const; 108 109 bool ParseFromArray(const char* plaintext, size_t plaintext_length); 110 ip()111 std::string ip() const { 112 return ip_; 113 } set_ip(base::StringPiece ip)114 void set_ip(base::StringPiece ip) { 115 ip_ = ip.as_string(); 116 } 117 timestamp()118 int64 timestamp() const { 119 return timestamp_; 120 } set_timestamp(int64 timestamp)121 void set_timestamp(int64 timestamp) { 122 timestamp_ = timestamp; 123 } 124 cached_network_parameters()125 const CachedNetworkParameters& cached_network_parameters() const { 126 return cached_network_parameters_; 127 } set_cached_network_parameters(const CachedNetworkParameters & cached_network_parameters)128 void set_cached_network_parameters( 129 const CachedNetworkParameters& cached_network_parameters) { 130 cached_network_parameters_ = cached_network_parameters; 131 } 132 133 private: 134 // ip_ contains either 4 (IPv4) or 16 (IPv6) bytes of IP address in network 135 // byte order. 136 std::string ip_; 137 // timestamp_ contains a UNIX timestamp value of the time when the token was 138 // created. 139 int64 timestamp_; 140 141 // The server can provide estimated network parameters to be used for 142 // initial parameter selection in future connections. 143 CachedNetworkParameters cached_network_parameters_; 144 145 DISALLOW_COPY_AND_ASSIGN(SourceAddressToken); 146 }; 147 148 } // namespace net 149 150 #endif // NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_ 151