Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: net/quic/port_suggester.cc

Issue 107803002: Consistently suggest ephemeral port for QUIC client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/port_suggester.h"
6
7 #include "base/metrics/histogram.h"
8
9 namespace net {
10
11 PortSuggester::PortSuggester(const HostPortPair& hostport,
12 uint64 per_profile_randomness)
13 : count_(0) {
14 base::SHA1HashBytes(
15 reinterpret_cast<const unsigned char*>(hostport.host().data()),
16 hostport.host().length(), seed_);
17
18 uint64* start_of_seed = reinterpret_cast<uint64*>(seed_);
wtc 2013/12/06 23:36:44 This cast is not portable because not all processo
jar (doing other things) 2013/12/07 00:47:58 Done.
19 DCHECK_LE(sizeof(*start_of_seed), sizeof(seed_)); // Safe memory access.
20 *start_of_seed ^= per_profile_randomness ^ hostport.port();
21 }
22
23 PortSuggester::~PortSuggester() {
24 UMA_HISTOGRAM_COUNTS("Net.Quic.EphemeralPortsSuggested", count_);
25 }
26
27 int PortSuggester::SuggestPort(int min, int max) {
28 // Sometimes our suggestion can't be used, so we ensure that if a 2nd call
29 // is made, that each call (probably) provides a new suggestion.
wtc 2013/12/06 23:36:44 Nit: that each call => that call ?
jar (doing other things) 2013/12/07 00:47:58 Adjusted sentence.
30 if (++count_ > 1)
31 base::SHA1HashBytes(seed_, sizeof(seed_), seed_); // Evolve the seed.
32 int rand = std::abs(*reinterpret_cast<int*>(seed_));
33 DCHECK_LE(sizeof(rand), sizeof(seed_));
34 DCHECK_LE(min, max);
35 DCHECK_GT(min, 0);
36 int range = max - min + 1;
37 return (rand % range) + min;
wtc 2013/12/06 23:36:44 If |range| is not a power of 2, this results in a
jar (doing other things) 2013/12/07 00:47:58 Added comment.
38 }
39
40 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698