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

Unified 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: Repond to wtc review 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 side-by-side diff with in-line comments
Download patch
Index: net/quic/port_suggester.cc
diff --git a/net/quic/port_suggester.cc b/net/quic/port_suggester.cc
new file mode 100644
index 0000000000000000000000000000000000000000..340a72c36bdd65e89e1826903c632d0efed00dd8
--- /dev/null
+++ b/net/quic/port_suggester.cc
@@ -0,0 +1,45 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/quic/port_suggester.h"
+
+#include "base/metrics/histogram.h"
+#include "net/base/host_port_pair.h"
+
+namespace net {
+
+PortSuggester::PortSuggester(const HostPortPair& server,
+ uint64 per_profile_randomness)
+ : count_(0) {
+ base::SHA1HashBytes(
+ reinterpret_cast<const unsigned char*>(server.host().data()),
+ server.host().length(), seed_.hash_bytes);
+
+ DCHECK_LE(sizeof(seed_.first_uint64), sizeof(seed_.hash_bytes));
Ryan Hamilton 2013/12/07 01:46:14 Can you use COMPILE_ASSERT here?
jar (doing other things) 2013/12/07 03:12:18 Done.
Ryan Hamilton 2013/12/07 23:21:54 It looks like this check has been removed. I'm to
jar (doing other things) 2013/12/08 01:38:36 Done.
+ seed_.first_uint64 ^= per_profile_randomness ^ server.port();
Ryan Hamilton 2013/12/07 01:46:14 Since you've already gone through the trouble of u
jar (doing other things) 2013/12/07 03:12:18 The only item that should not be (close to) unifor
Ryan Hamilton 2013/12/07 04:52:22 Ah, I see. Makes sense.
+}
+
+PortSuggester::~PortSuggester() {
+ UMA_HISTOGRAM_COUNTS("Net.Quic.EphemeralPortsSuggested", count_);
+}
+
+int PortSuggester::SuggestPort(int min, int max) {
+ // Sometimes our suggestion can't be used, so we ensure that if a additional
wtc 2013/12/07 01:03:38 Nit: remove "a"
jar (doing other things) 2013/12/07 03:12:18 Done.
+ // calls are made, that each call (probably) provides a new suggestion.
wtc 2013/12/07 01:03:38 Nit: I think "that" can be removed
jar (doing other things) 2013/12/07 03:12:18 Done.
+ if (++count_ > 1) {
+ // Evolve the seed.
+ base::SHA1HashBytes(seed_.hash_bytes, sizeof(seed_.hash_bytes),
+ seed_.hash_bytes);
+ }
+ int rand = std::abs(seed_.first_int);
Ryan Hamilton 2013/12/07 01:46:14 I notice that you're using first_int here, and fir
jar (doing other things) 2013/12/07 03:12:18 Seed_ is currently large enough to hold all the re
Ryan Hamilton 2013/12/07 04:52:22 Understood. However, in my opinion the extra comp
+ DCHECK_LE(min, max);
+ DCHECK_GT(min, 0);
+ int range = max - min + 1;
+ // Ports (and hence the extent of the |range|) are generally under 2^16, while
+ // ints are bigger than 2^31, so the tiny non-uniformity in the pseudo-random
+ // distribution is not significant.
+ return (rand % range) + min;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698