| OLD | NEW |
| (Empty) |
| 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 "net/quic/chromium/port_suggester.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace net { | |
| 10 | |
| 11 PortSuggester::PortSuggester(const HostPortPair& server, uint64_t seed) | |
| 12 : call_count_(0), previous_suggestion_(-1) { | |
| 13 unsigned char hash_bytes[base::kSHA1Length]; | |
| 14 base::SHA1HashBytes( | |
| 15 reinterpret_cast<const unsigned char*>(server.host().data()), | |
| 16 server.host().length(), hash_bytes); | |
| 17 static_assert(sizeof(seed_) < sizeof(hash_bytes), "seed larger than hash"); | |
| 18 memcpy(&seed_, hash_bytes, sizeof(seed_)); | |
| 19 seed_ ^= seed ^ server.port(); | |
| 20 } | |
| 21 | |
| 22 int PortSuggester::SuggestPort(int min, int max) { | |
| 23 // Sometimes our suggestion can't be used, so we ensure that if additional | |
| 24 // calls are made, then each call (probably) provides a new suggestion. | |
| 25 if (++call_count_ > 1) { | |
| 26 // Evolve the seed. | |
| 27 unsigned char hash_bytes[base::kSHA1Length]; | |
| 28 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(&seed_), | |
| 29 sizeof(seed_), hash_bytes); | |
| 30 memcpy(&seed_, hash_bytes, sizeof(seed_)); | |
| 31 } | |
| 32 DCHECK_LE(min, max); | |
| 33 DCHECK_GT(min, 0); | |
| 34 int range = max - min + 1; | |
| 35 // Ports (and hence the extent of the |range|) are generally under 2^16, so | |
| 36 // the tiny non-uniformity in the pseudo-random distribution is not | |
| 37 // significant. | |
| 38 previous_suggestion_ = static_cast<int>(seed_ % range) + min; | |
| 39 return previous_suggestion_; | |
| 40 } | |
| 41 | |
| 42 int PortSuggester::previous_suggestion() const { | |
| 43 DCHECK_LT(0u, call_count_); | |
| 44 return previous_suggestion_; | |
| 45 } | |
| 46 | |
| 47 } // namespace net | |
| OLD | NEW |