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

Side by Side Diff: net/quic/crypto/strike_register.h

Issue 15937012: Land Recent QUIC changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Small bug fixes Created 7 years, 6 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_QUIC_CRYPTO_STRIKE_REGISTER_H_ 5 #ifndef NET_QUIC_CRYPTO_STRIKE_REGISTER_H_
6 #define NET_QUIC_CRYPTO_STRIKE_REGISTER_H_ 6 #define NET_QUIC_CRYPTO_STRIKE_REGISTER_H_
7 7
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 12 matching lines...) Expand all
23 // 2) We can write it to use a fixed block of memory: avoiding fragmentation 23 // 2) We can write it to use a fixed block of memory: avoiding fragmentation
24 // issues and so forth. (We might be able to do that with the STL 24 // issues and so forth. (We might be able to do that with the STL
25 // algorithms and a custom allocator, but I don't want to go there.) 25 // algorithms and a custom allocator, but I don't want to go there.)
26 // 3) It's simple (compared to balanced binary trees) and doesn't involve 26 // 3) It's simple (compared to balanced binary trees) and doesn't involve
27 // bouncing nearly as many cache lines around. 27 // bouncing nearly as many cache lines around.
28 // 4) It allows us to query for the oldest element in log(n) time. 28 // 4) It allows us to query for the oldest element in log(n) time.
29 // 29 //
30 // This code is based on djb's public domain critbit tree from qhasm. 30 // This code is based on djb's public domain critbit tree from qhasm.
31 // 31 //
32 // A critbit tree has external and internal nodes. External nodes are just the 32 // A critbit tree has external and internal nodes. External nodes are just the
33 // nonce values (which are stored without the orbit values included). Internal 33 // nonce values (which are stored with internal times, see below, and without
34 // nodes contain the bit number at which the tree is branching and exactly two 34 // the orbit values included). Internal nodes contain the bit number at which
35 // children. The critical bit is stored as a byte number and a byte 35 // the tree is branching and exactly two children. The critical bit is stored
36 // (|otherbits|) which has all the bits set /except/ the one in question. 36 // as a byte number and a byte (|otherbits|) which has all the bits set
37 // /except/ the one in question.
37 // 38 //
38 // Internal nodes have exactly two children: an internal node with only a 39 // Internal nodes have exactly two children: an internal node with only a
39 // single child would be useless. 40 // single child would be useless.
40 // 41 //
41 // The branching bit number (considering the MSB to be the 1st bit) is 42 // The branching bit number (considering the MSB to be the 1st bit) is
42 // monotonically increasing as you go down the tree. 43 // monotonically increasing as you go down the tree.
44 //
45 // There are two distinct time representations used. External times are those
46 // which are exposed to the users of this class. They are expected to be a
47 // count of the number of seconds since the UNIX epoch. Internal times are a
48 // count of the number of seconds since a point in time a couple of years
49 // before the creation time given to the constructor. (See
50 // |ExternalTimeToInternal|) This avoids having to worry about overflow since
51 // we assume that no process will run for 130 years.
43 class NET_EXPORT_PRIVATE StrikeRegister { 52 class NET_EXPORT_PRIVATE StrikeRegister {
44 public: 53 public:
54 enum StartupType {
55 // DENY_REQUESTS_AT_STARTUP is the typical mode for a strike register.
56 // Because servers can crash and the strike-register memory-based, the
57 // state of the strike-register may be lost at any time. Thus the previous
58 // instance of the server may have accepted an nonce with time
59 // now+window_secs, which was forgotten in the crash. Therefore
60 // DENY_REQUESTS_AT_STARTUP causes the strike-register to reject all
61 // requests timestampped before window_secs + the creation time (the
62 // quiescent period).
63 DENY_REQUESTS_AT_STARTUP,
64 // NO_STARTUP_PERIOD_NEEDED indicates that no quiescent period is required.
65 // This may be because the strike-register is using an orbit randomly
66 // generated at startup and therefore nonces accepted by the previous
67 // instance of the strike-register are invalid for that reason.
68 NO_STARTUP_PERIOD_NEEDED,
69 };
70
45 // An external node takes 24 bytes as we don't record the orbit. 71 // An external node takes 24 bytes as we don't record the orbit.
46 static const uint32 kExternalNodeSize; 72 static const uint32 kExternalNodeSize;
47 73
48 // We address the nodes by their index in the array. This means that 0 is a 74 // We address the nodes by their index in the array. This means that 0 is a
49 // valid index. Therefore this is our invalid index. It also has a one bit 75 // valid index. Therefore this is our invalid index. It also has a one bit
50 // in the LSB position because we tend to store indexes shifted up 8 bits 76 // in the LSB position because we tend to store indexes shifted up 8 bits
51 // and this distinguishes kNil from (kExternalFlag | 0) << 8. 77 // and this distinguishes kNil from (kExternalFlag | 0) << 8.
52 static const uint32 kNil; 78 static const uint32 kNil;
53 79
54 // Our pointers from internal nodes can either point to an internal or 80 // Our pointers from internal nodes can either point to an internal or
55 // external node. We flag the 24th bit to mark a pointer as external. 81 // external node. We flag the 24th bit to mark a pointer as external.
56 static const uint32 kExternalFlag; 82 static const uint32 kExternalFlag;
57 83
58 // Construct a new set which can hold, at most, |max_entries| (which must be 84 // Construct a new set which can hold, at most, |max_entries| (which must be
59 // less than 2**23). Once constructed, all nonces created before 85 // less than 2**23). See the comments around StartupType about initial
60 // |current_time| (in seconds) will be rejected. In the future, all nonces 86 // behaviour. Otherwise, all nonces that are outside +/- |window_secs| from
61 // that are outside +/- |window_secs| from the current time will be rejected. 87 // the current time will be rejected. Additionally, all nonces that have an
62 // Additionally, all nonces that have an orbit value other than |orbit| will 88 // orbit value other than |orbit| will be rejected.
63 // be rejected.
64 // 89 //
65 // (Note that this code is independent of the actual units of time used, but 90 // (Note that this code is independent of the actual units of time used, but
66 // you should use seconds.) 91 // you should use seconds.)
67 StrikeRegister(unsigned max_entries, 92 StrikeRegister(unsigned max_entries,
68 uint32 current_time, 93 uint32 current_time_external,
69 uint32 window_secs, 94 uint32 window_secs,
70 const uint8 orbit[8]); 95 const uint8 orbit[8],
96 StartupType startup);
71 97
72 ~StrikeRegister(); 98 ~StrikeRegister();
73 99
74 void Reset(); 100 void Reset();
75 101
76 // |Insert| queries to see if |nonce| is 102 // |Insert| queries to see if |nonce| is
77 // a) for the wrong orbit 103 // a) for the wrong orbit
78 // b) before the current horizon 104 // b) before the current horizon
79 // c) outside of the valid time window 105 // c) outside of the valid time window
80 // d) already in the set of observed nonces 106 // d) already in the set of observed nonces
(...skipping 10 matching lines...) Expand all
91 117
92 // This is a debugging aid which checks the tree for sanity. 118 // This is a debugging aid which checks the tree for sanity.
93 void Validate(); 119 void Validate();
94 120
95 private: 121 private:
96 class InternalNode; 122 class InternalNode;
97 123
98 // TimeFromBytes returns a big-endian uint32 from |d|. 124 // TimeFromBytes returns a big-endian uint32 from |d|.
99 static uint32 TimeFromBytes(const uint8 d[4]); 125 static uint32 TimeFromBytes(const uint8 d[4]);
100 126
127 // ExternalTimeToInternal converts an external time value into an internal
128 // time value using |creation_time_external_|.
129 uint32 ExternalTimeToInternal(uint32 external_time);
130
101 // BestMatch returns either kNil, or an external node index which could 131 // BestMatch returns either kNil, or an external node index which could
102 // possibly match |v|. 132 // possibly match |v|.
103 uint32 BestMatch(const uint8 v[24]) const; 133 uint32 BestMatch(const uint8 v[24]) const;
104 134
105 // external_node_next_ptr returns the 'next' pointer embedded in external 135 // external_node_next_ptr returns the 'next' pointer embedded in external
106 // node |i|. This is used to thread a free list through the external nodes. 136 // node |i|. This is used to thread a free list through the external nodes.
107 uint32& external_node_next_ptr(unsigned i); 137 uint32& external_node_next_ptr(unsigned i);
108 138
109 uint8* external_node(unsigned i); 139 uint8* external_node(unsigned i);
110 140
(...skipping 12 matching lines...) Expand all
123 void ValidateTree(uint32 internal_node, 153 void ValidateTree(uint32 internal_node,
124 int last_bit, 154 int last_bit,
125 const std::vector<std::pair<unsigned, bool> >& bits, 155 const std::vector<std::pair<unsigned, bool> >& bits,
126 const std::set<uint32>& free_internal_nodes, 156 const std::set<uint32>& free_internal_nodes,
127 const std::set<uint32>& free_external_nodes, 157 const std::set<uint32>& free_external_nodes,
128 std::set<uint32>* used_internal_nodes, 158 std::set<uint32>* used_internal_nodes,
129 std::set<uint32>* used_external_nodes); 159 std::set<uint32>* used_external_nodes);
130 160
131 const uint32 max_entries_; 161 const uint32 max_entries_;
132 const uint32 window_secs_; 162 const uint32 window_secs_;
163 // creation_time_external_ contains the uint32, external time when this
164 // object was created (i.e. the value passed to the constructor). This is
165 // used to translate external times to internal times.
166 const uint32 creation_time_external_;
167 // internal_epoch_ contains the external time value of the start of internal
168 // time.
169 const uint32 internal_epoch_;
133 uint8 orbit_[8]; 170 uint8 orbit_[8];
134 uint32 horizon_; 171 uint32 horizon_;
172 bool horizon_valid_;
135 173
136 uint32 internal_node_free_head_; 174 uint32 internal_node_free_head_;
137 uint32 external_node_free_head_; 175 uint32 external_node_free_head_;
138 uint32 internal_node_head_; 176 uint32 internal_node_head_;
139 // internal_nodes_ can't be a scoped_ptr because the type isn't defined in 177 // internal_nodes_ can't be a scoped_ptr because the type isn't defined in
140 // this header. 178 // this header.
141 InternalNode* internal_nodes_; 179 InternalNode* internal_nodes_;
142 scoped_ptr<uint8[]> external_nodes_; 180 scoped_ptr<uint8[]> external_nodes_;
143 }; 181 };
144 182
145 } // namespace net 183 } // namespace net
146 184
147 #endif // NET_QUIC_CRYPTO_STRIKE_REGISTER_H_ 185 #endif // NET_QUIC_CRYPTO_STRIKE_REGISTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698