blob: 4ab7e3e28ee977455e7c255313bbee3267871a01 [file] [log] [blame]
[email protected]56681582013-07-28 20:08:441// Copyright 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 "chrome/browser/net/probe_message.h"
6
avi6846aef2015-12-26 01:09:387#include <stddef.h>
8
[email protected]56681582013-07-28 20:08:449#include <string>
10
11#include "base/logging.h"
12
13namespace chrome_browser_net {
14
avi6846aef2015-12-26 01:09:3815const uint32_t ProbeMessage::kVersion = 2;
16const uint32_t ProbeMessage::kMaxNumberProbePackets = 21;
17const uint32_t ProbeMessage::kMaxProbePacketBytes = 1500;
[email protected]a94b0d72013-11-18 05:04:4418// Maximum pacing interval is 300 seconds (for testing NAT binding).
avi6846aef2015-12-26 01:09:3819const uint32_t ProbeMessage::kMaxPacingIntervalMicros = 300000000;
[email protected]56681582013-07-28 20:08:4420const char ProbeMessage::kEncodingString[] =
21 "T\xd3?\xa5h2\x9c\x8en\xf1Q6\xbc{\xc6-4\xfa$f\xb9[\xa6\xcd@6,\xdf\xb3i-\xe6"
22 "v\x9eV\x8dXd\xd9kE\xf6=\xbeO";
23
24ProbeMessage::ProbeMessage() {}
25
26bool ProbeMessage::ParseInput(const std::string& input,
27 ProbePacket* probe_packet) const {
28 // Encode is used for decoding here.
29 std::string input_decoded = Encode(input);
30
31 bool parse_result = probe_packet->ParseFromString(input_decoded);
32 if (!parse_result) {
33 DVLOG(1) << "ProtoBuffer string parsing error. Input size:" << input.size();
34 return false;
35 }
36 const ProbePacket_Header& header = probe_packet->header();
37 DVLOG(2) << "version " << header.version() << " checksum "
38 << header.checksum() << " type " << header.type();
39 if (header.version() != kVersion) {
40 DVLOG(1) << "Bad version number: " << header.version()
41 << " expected: " << kVersion;
42 return false;
43 }
44
45 // Checksum is computed on padding only.
46 if (probe_packet->has_padding()) {
47 DVLOG(3) << "received padding: " << probe_packet->padding();
avi6846aef2015-12-26 01:09:3848 uint32_t computed_checksum = Checksum(probe_packet->padding());
[email protected]56681582013-07-28 20:08:4449 if (computed_checksum != header.checksum()) {
50 DVLOG(1) << "Checksum mismatch. Got: " << header.checksum()
51 << " expected: " << computed_checksum;
52 return false;
53 }
54 }
55
56 if (header.type() != ProbePacket_Type_HELLO_REPLY &&
57 header.type() != ProbePacket_Type_PROBE_REPLY) {
58 DVLOG(1) << "Received unknown packet type:" << header.type();
59 return false;
60 }
61 return true;
62}
63
avi6846aef2015-12-26 01:09:3864uint32_t ProbeMessage::Checksum(const std::string& str) const {
65 uint32_t ret = 0;
[email protected]56681582013-07-28 20:08:4466 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) {
avi6846aef2015-12-26 01:09:3867 ret += static_cast<uint8_t>(*i);
[email protected]56681582013-07-28 20:08:4468 }
69 return ret;
70}
71
72void ProbeMessage::GenerateProbeRequest(const ProbePacket_Token& token,
avi6846aef2015-12-26 01:09:3873 uint32_t group_id,
74 uint32_t probe_size,
75 uint32_t pacing_interval_micros,
76 uint32_t number_probe_packets,
[email protected]56681582013-07-28 20:08:4477 ProbePacket* probe_packet) {
78 DCHECK_LE(number_probe_packets, kMaxNumberProbePackets);
79 DCHECK_LE(probe_size, kMaxProbePacketBytes);
80 DCHECK_LE(pacing_interval_micros, kMaxPacingIntervalMicros);
81
82 SetPacketHeader(ProbePacket_Type_PROBE_REQUEST, probe_packet);
83 *(probe_packet->mutable_token()) = token;
84 probe_packet->set_group_id(group_id);
85 probe_packet->set_probe_size_bytes(probe_size);
86 probe_packet->set_pacing_interval_micros(pacing_interval_micros);
87 probe_packet->set_number_probe_packets(number_probe_packets);
88
89 // Add padding to mitigate amplification attack.
90 std::string* padding = probe_packet->mutable_padding();
91 int padding_size = probe_size - probe_packet->ByteSize();
92 padding->append(std::string(std::max(0, padding_size), 0));
93 probe_packet->mutable_header()->set_checksum(Checksum(*padding));
94 DVLOG(3) << "Request size " << probe_packet->ByteSize() << " probe size "
95 << probe_size;
avi6846aef2015-12-26 01:09:3896 DCHECK_LE(probe_size, static_cast<uint32_t>(probe_packet->ByteSize()));
[email protected]56681582013-07-28 20:08:4497}
98
99void ProbeMessage::SetPacketHeader(ProbePacket_Type packet_type,
100 ProbePacket* probe_packet) const {
101 ProbePacket_Header* header = probe_packet->mutable_header();
102 header->set_version(kVersion);
103 header->set_type(packet_type);
104}
105
106std::string ProbeMessage::Encode(const std::string& input) const {
107
108 std::string output(input.size(), 0);
109 int key_pos = 0;
110 // kEncodingString contains a ending '\0' character, excluded for encoding.
111 int key_size = sizeof(kEncodingString) - 1;
112 for (size_t i = 0; i < input.size(); ++i) {
113 output[i] = input[i] ^ kEncodingString[key_pos];
114 ++key_pos;
115 if (key_pos >= key_size)
116 key_pos = 0;
117 }
118 return output;
119}
120
121std::string ProbeMessage::MakeEncodedPacket(
122 const ProbePacket& probe_packet) const {
123 std::string output;
124 probe_packet.SerializeToString(&output);
125 return Encode(output);
126}
127
128} // namespace chrome_browser_net