0b514fe23b60cd9c4c77c0c418dde48866e91f74
[platform/framework/web/crosswalk.git] / src / net / quic / crypto / source_address_token.cc
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 #include "net/quic/crypto/source_address_token.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_split.h"
9
10 using std::string;
11
12 namespace net {
13
14 CachedNetworkParameters::CachedNetworkParameters()
15     : bandwidth_estimate_bytes_per_second_(0),
16       max_bandwidth_estimate_bytes_per_second_(0),
17       max_bandwidth_timestamp_seconds_(0),
18       min_rtt_ms_(0),
19       previous_connection_state_(0),
20       timestamp_(0) {
21 }
22
23 CachedNetworkParameters::~CachedNetworkParameters() {
24 }
25
26 bool CachedNetworkParameters::operator==(
27     const CachedNetworkParameters& other) const {
28   return serving_region_ == other.serving_region_ &&
29       bandwidth_estimate_bytes_per_second_ ==
30           other.bandwidth_estimate_bytes_per_second_ &&
31       max_bandwidth_estimate_bytes_per_second_ ==
32           other.max_bandwidth_estimate_bytes_per_second_ &&
33       max_bandwidth_timestamp_seconds_ ==
34           other.max_bandwidth_timestamp_seconds_ &&
35       min_rtt_ms_ == other.min_rtt_ms_ &&
36       previous_connection_state_ == other.previous_connection_state_ &&
37       timestamp_ == other.timestamp_;
38 }
39
40 bool CachedNetworkParameters::operator!=(
41     const CachedNetworkParameters& other) const {
42   return !(*this == other);
43 }
44
45 SourceAddressToken::SourceAddressToken()
46     : has_cached_network_parameters_(false) {
47 }
48
49 SourceAddressToken::~SourceAddressToken() {
50 }
51
52 string SourceAddressToken::SerializeAsString() const {
53   string out;
54   out.push_back(ip_.size());
55   out.append(ip_);
56   string time_str = base::Int64ToString(timestamp_);
57   out.push_back(time_str.size());
58   out.append(time_str);
59   // TODO(rtenneti): Implement serialization of optional CachedNetworkParameters
60   // when they are used.
61   return out;
62 }
63
64 bool SourceAddressToken::ParseFromArray(const char* plaintext,
65                                         size_t plaintext_length) {
66   if (plaintext_length == 0) {
67     return false;
68   }
69   size_t ip_len = plaintext[0];
70   if (plaintext_length <= 1 + ip_len) {
71     return false;
72   }
73   size_t time_len = plaintext[1 + ip_len];
74   if (plaintext_length != 1 + ip_len + 1 + time_len) {
75     return false;
76   }
77
78   string time_str(&plaintext[1 + ip_len + 1], time_len);
79   int64 timestamp;
80   if (!base::StringToInt64(time_str, &timestamp)) {
81     return false;
82   }
83
84   ip_.assign(&plaintext[1], ip_len);
85   timestamp_ = timestamp;
86
87   // TODO(rtenneti): Implement parsing of optional CachedNetworkParameters when
88   // they are used.
89   return true;
90 }
91
92 }  // namespace net