• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 #ifndef DISCOVERY_MDNS_MDNS_RANDOM_H_
6 #define DISCOVERY_MDNS_MDNS_RANDOM_H_
7 
8 #include <random>
9 
10 #include "platform/api/time.h"
11 
12 namespace openscreen {
13 namespace discovery {
14 
15 class MdnsRandom {
16  public:
17   // RFC 6762 Section 5.2
18   // https://tools.ietf.org/html/rfc6762#section-5.2
19 
GetInitialQueryDelay()20   Clock::duration GetInitialQueryDelay() {
21     return std::chrono::milliseconds{initial_query_delay_(random_engine_)};
22   }
23 
GetRecordTtlVariation()24   double GetRecordTtlVariation() {
25     return record_ttl_variation_(random_engine_);
26   }
27 
28   // RFC 6762 Section 6
29   // https://tools.ietf.org/html/rfc6762#section-6
30 
GetSharedRecordResponseDelay()31   Clock::duration GetSharedRecordResponseDelay() {
32     return std::chrono::milliseconds{
33         shared_record_response_delay_(random_engine_)};
34   }
35 
GetTruncatedQueryResponseDelay()36   Clock::duration GetTruncatedQueryResponseDelay() {
37     return std::chrono::milliseconds{
38         truncated_query_response_delay_(random_engine_)};
39   }
40 
GetInitialProbeDelay()41   Clock::duration GetInitialProbeDelay() {
42     return std::chrono::milliseconds{
43         probe_initialization_delay_(random_engine_)};
44   }
45 
46  private:
47   static constexpr int64_t kMinimumInitialQueryDelayMs = 20;
48   static constexpr int64_t kMaximumInitialQueryDelayMs = 120;
49 
50   static constexpr double kMinimumTtlVariation = 0.0;
51   static constexpr double kMaximumTtlVariation = 0.02;
52 
53   static constexpr int64_t kMinimumSharedRecordResponseDelayMs = 20;
54   static constexpr int64_t kMaximumSharedRecordResponseDelayMs = 120;
55 
56   static constexpr int64_t kMinimumTruncatedQueryResponseDelayMs = 400;
57   static constexpr int64_t kMaximumTruncatedQueryResponseDelayMs = 500;
58 
59   static constexpr int64_t kMinimumProbeInitializationDelayMs = 0;
60   static constexpr int64_t kMaximumProbeInitializationDelayMs = 250;
61 
62   std::default_random_engine random_engine_{std::random_device{}()};
63   std::uniform_int_distribution<int64_t> initial_query_delay_{
64       kMinimumInitialQueryDelayMs, kMaximumInitialQueryDelayMs};
65   std::uniform_real_distribution<double> record_ttl_variation_{
66       kMinimumTtlVariation, kMaximumTtlVariation};
67   std::uniform_int_distribution<int64_t> shared_record_response_delay_{
68       kMinimumSharedRecordResponseDelayMs, kMaximumSharedRecordResponseDelayMs};
69   std::uniform_int_distribution<int64_t> truncated_query_response_delay_{
70       kMinimumTruncatedQueryResponseDelayMs,
71       kMaximumTruncatedQueryResponseDelayMs};
72   std::uniform_int_distribution<int64_t> probe_initialization_delay_{
73       kMinimumProbeInitializationDelayMs, kMaximumProbeInitializationDelayMs};
74 };
75 
76 }  // namespace discovery
77 }  // namespace openscreen
78 
79 #endif  // DISCOVERY_MDNS_MDNS_RANDOM_H_
80