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