OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/crypto/source_address_token.h" | 5 #include "net/quic/crypto/source_address_token.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 | 11 |
12 using std::string; | 12 using std::string; |
13 using std::vector; | 13 using std::vector; |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 SourceAddressToken::SourceAddressToken() { | 17 SourceAddressToken::SourceAddressToken() { |
18 } | 18 } |
19 | 19 |
20 SourceAddressToken::~SourceAddressToken() { | 20 SourceAddressToken::~SourceAddressToken() { |
21 } | 21 } |
22 | 22 |
23 string SourceAddressToken::SerializeAsString() const { | 23 string SourceAddressToken::SerializeAsString() const { |
24 return ip_ + " " + base::Int64ToString(timestamp_); | 24 string out; |
| 25 out.push_back(ip_.size()); |
| 26 out.append(ip_); |
| 27 string time_str = base::Int64ToString(timestamp_); |
| 28 out.push_back(time_str.size()); |
| 29 out.append(time_str); |
| 30 return out; |
25 } | 31 } |
26 | 32 |
27 bool SourceAddressToken::ParseFromArray(const char* plaintext, | 33 bool SourceAddressToken::ParseFromArray(const char* plaintext, |
28 size_t plaintext_length) { | 34 size_t plaintext_length) { |
29 string data(plaintext, plaintext_length); | 35 if (plaintext_length == 0) { |
30 vector<string> results; | 36 return false; |
31 base::SplitString(data, ' ', &results); | 37 } |
32 if (results.size() < 2) { | 38 size_t ip_len = plaintext[0]; |
| 39 if (plaintext_length <= 1 + ip_len) { |
| 40 return false; |
| 41 } |
| 42 size_t time_len = plaintext[1 + ip_len]; |
| 43 if (plaintext_length != 1 + ip_len + 1 + time_len) { |
33 return false; | 44 return false; |
34 } | 45 } |
35 | 46 |
| 47 string time_str(&plaintext[1 + ip_len + 1], time_len); |
36 int64 timestamp; | 48 int64 timestamp; |
37 if (!base::StringToInt64(results[1], ×tamp)) { | 49 if (!base::StringToInt64(time_str, ×tamp)) { |
38 return false; | 50 return false; |
39 } | 51 } |
40 | 52 |
41 ip_ = results[0]; | 53 ip_.assign(&plaintext[1], ip_len); |
42 timestamp_ = timestamp; | 54 timestamp_ = timestamp; |
43 return true; | 55 return true; |
44 } | 56 } |
45 | 57 |
46 } // namespace net | 58 } // namespace net |
OLD | NEW |