Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / crypto / source_address_token.cc
blobf20c343f987e297e301cdd796529f72cb03368b8
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 #include "net/quic/crypto/source_address_token.h"
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
12 using std::string;
13 using std::vector;
15 namespace net {
17 CachedNetworkParameters::CachedNetworkParameters() {
20 CachedNetworkParameters::~CachedNetworkParameters() {
23 SourceAddressToken::SourceAddressToken() {
26 SourceAddressToken::~SourceAddressToken() {
29 string SourceAddressToken::SerializeAsString() const {
30 string out;
31 out.push_back(ip_.size());
32 out.append(ip_);
33 string time_str = base::Int64ToString(timestamp_);
34 out.push_back(time_str.size());
35 out.append(time_str);
36 // TODO(rtenneti): Implement serialization of optional CachedNetworkParameters
37 // when they are used.
38 return out;
41 bool SourceAddressToken::ParseFromArray(const char* plaintext,
42 size_t plaintext_length) {
43 if (plaintext_length == 0) {
44 return false;
46 size_t ip_len = plaintext[0];
47 if (plaintext_length <= 1 + ip_len) {
48 return false;
50 size_t time_len = plaintext[1 + ip_len];
51 if (plaintext_length != 1 + ip_len + 1 + time_len) {
52 return false;
55 string time_str(&plaintext[1 + ip_len + 1], time_len);
56 int64 timestamp;
57 if (!base::StringToInt64(time_str, &timestamp)) {
58 return false;
61 ip_.assign(&plaintext[1], ip_len);
62 timestamp_ = timestamp;
64 // TODO(rtenneti): Implement parsing of optional CachedNetworkParameters when
65 // they are used.
66 return true;
69 } // namespace net