This source file includes following definitions.
- previous_suggestion_
- SuggestPort
- previous_suggestion
#include "net/quic/port_suggester.h"
#include "base/logging.h"
#include "net/base/host_port_pair.h"
namespace net {
PortSuggester::PortSuggester(const HostPortPair& server, uint64 seed)
: call_count_(0),
previous_suggestion_(-1) {
unsigned char hash_bytes[base::kSHA1Length];
base::SHA1HashBytes(
reinterpret_cast<const unsigned char*>(server.host().data()),
server.host().length(), hash_bytes);
COMPILE_ASSERT(sizeof(seed_) < sizeof(hash_bytes), seed_larger_than_hash);
memcpy(&seed_, hash_bytes, sizeof(seed_));
seed_ ^= seed ^ server.port();
}
int PortSuggester::SuggestPort(int min, int max) {
if (++call_count_ > 1) {
unsigned char hash_bytes[base::kSHA1Length];
base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(&seed_),
sizeof(seed_), hash_bytes);
memcpy(&seed_, hash_bytes, sizeof(seed_));
}
DCHECK_LE(min, max);
DCHECK_GT(min, 0);
int range = max - min + 1;
previous_suggestion_ = static_cast<int>(seed_ % range) + min;
return previous_suggestion_;
}
int PortSuggester::previous_suggestion() const {
DCHECK_LT(0u, call_count_);
return previous_suggestion_;
}
}