@@ -30,73 +30,31 @@ namespace Transport {
30
30
constexpr const uint16_t kAnyKeyId = 0xffff ;
31
31
32
32
/* *
33
- * Handles a set of peer connection states .
33
+ * Handles a set of sessions .
34
34
*
35
35
* Intended for:
36
- * - handle connection active time and expiration
37
- * - allocate and free space for connection states .
36
+ * - handle session active time and expiration
37
+ * - allocate and free space for sessions .
38
38
*/
39
- template <size_t kMaxConnectionCount , Time::Source kTimeSource = Time::Source::kSystem >
39
+ template <size_t kMaxSessionCount , Time::Source kTimeSource = Time::Source::kSystem >
40
40
class SecureSessionTable
41
41
{
42
42
public:
43
43
/* *
44
- * Allocates a new peer connection state state object out of the internal resource pool.
44
+ * Allocates a new secure session out of the internal resource pool.
45
45
*
46
- * @param address represents the connection state address
47
- * @param state [out] will contain the connection state if one was available. May be null if no return value is desired.
48
- *
49
- * @note the newly created state will have an 'active' time set based on the current time source.
50
- *
51
- * @returns CHIP_NO_ERROR if state could be initialized. May fail if maximum connection count
52
- * has been reached (with CHIP_ERROR_NO_MEMORY).
53
- */
54
- CHECK_RETURN_VALUE
55
- CHIP_ERROR CreateNewPeerConnectionState (const PeerAddress & address, SecureSession ** state)
56
- {
57
- CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
58
-
59
- if (state)
60
- {
61
- *state = nullptr ;
62
- }
63
-
64
- for (size_t i = 0 ; i < kMaxConnectionCount ; i++)
65
- {
66
- if (!mStates [i].IsInitialized ())
67
- {
68
- mStates [i] = SecureSession (address);
69
- mStates [i].SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ());
70
-
71
- if (state)
72
- {
73
- *state = &mStates [i];
74
- }
75
-
76
- err = CHIP_NO_ERROR;
77
- break ;
78
- }
79
- }
80
-
81
- return err;
82
- }
83
-
84
- /* *
85
- * Allocates a new peer connection state state object out of the internal resource pool.
86
- *
87
- * @param peerNode represents optional peer Node's ID
46
+ * @param peerNode represents peer Node's ID
88
47
* @param peerSessionId represents the encryption key ID assigned by peer node
89
48
* @param localSessionId represents the encryption key ID assigned by local node
90
- * @param state [out] will contain the connection state if one was available. May be null if no return value is desired.
49
+ * @param state [out] will contain the session if one was available. May be null if no return value is desired.
91
50
*
92
51
* @note the newly created state will have an 'active' time set based on the current time source.
93
52
*
94
- * @returns CHIP_NO_ERROR if state could be initialized. May fail if maximum connection count
53
+ * @returns CHIP_NO_ERROR if state could be initialized. May fail if maximum session count
95
54
* has been reached (with CHIP_ERROR_NO_MEMORY).
96
55
*/
97
56
CHECK_RETURN_VALUE
98
- CHIP_ERROR CreateNewPeerConnectionState (const Optional<NodeId> & peerNode, uint16_t peerSessionId, uint16_t localSessionId,
99
- SecureSession ** state)
57
+ CHIP_ERROR CreateNewSecureSession (NodeId peerNode, uint16_t peerSessionId, uint16_t localSessionId, SecureSession ** state)
100
58
{
101
59
CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
102
60
@@ -105,20 +63,16 @@ class SecureSessionTable
105
63
*state = nullptr ;
106
64
}
107
65
108
- for (size_t i = 0 ; i < kMaxConnectionCount ; i++)
66
+ for (size_t i = 0 ; i < kMaxSessionCount ; i++)
109
67
{
110
68
if (!mStates [i].IsInitialized ())
111
69
{
112
70
mStates [i] = SecureSession ();
71
+ mStates [i].SetPeerNodeId (peerNode);
113
72
mStates [i].SetPeerSessionId (peerSessionId);
114
73
mStates [i].SetLocalSessionId (localSessionId);
115
74
mStates [i].SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ());
116
75
117
- if (peerNode.ValueOr (kUndefinedNodeId ) != kUndefinedNodeId )
118
- {
119
- mStates [i].SetPeerNodeId (peerNode.Value ());
120
- }
121
-
122
76
if (state)
123
77
{
124
78
*state = &mStates [i];
@@ -133,56 +87,25 @@ class SecureSessionTable
133
87
}
134
88
135
89
/* *
136
- * Get a peer connection state given a Peer address .
90
+ * Get a secure session given a Node Id .
137
91
*
138
- * @param address is the connection to find (based on address)
92
+ * @param nodeId is the session to find (based on nodeId).
139
93
* @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
140
94
*
141
95
* @return the state found, nullptr if not found
142
96
*/
143
97
CHECK_RETURN_VALUE
144
- SecureSession * FindPeerConnectionState ( const PeerAddress & address , SecureSession * begin)
98
+ SecureSession * FindSecureSession (NodeId nodeId , SecureSession * begin)
145
99
{
146
100
SecureSession * state = nullptr ;
147
101
SecureSession * iter = &mStates [0 ];
148
102
149
- if (begin >= iter && begin < &mStates [kMaxConnectionCount ])
103
+ if (begin >= iter && begin < &mStates [kMaxSessionCount ])
150
104
{
151
105
iter = begin + 1 ;
152
106
}
153
107
154
- for (; iter < &mStates [kMaxConnectionCount ]; iter++)
155
- {
156
- if (iter->GetPeerAddress () == address)
157
- {
158
- state = iter;
159
- break ;
160
- }
161
- }
162
- return state;
163
- }
164
-
165
- /* *
166
- * Get a peer connection state given a Node Id.
167
- *
168
- * @param nodeId is the connection to find (based on nodeId). Note that initial connections
169
- * do not have a node id set. Use this if you know the node id should be set.
170
- * @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
171
- *
172
- * @return the state found, nullptr if not found
173
- */
174
- CHECK_RETURN_VALUE
175
- SecureSession * FindPeerConnectionState (NodeId nodeId, SecureSession * begin)
176
- {
177
- SecureSession * state = nullptr ;
178
- SecureSession * iter = &mStates [0 ];
179
-
180
- if (begin >= iter && begin < &mStates [kMaxConnectionCount ])
181
- {
182
- iter = begin + 1 ;
183
- }
184
-
185
- for (; iter < &mStates [kMaxConnectionCount ]; iter++)
108
+ for (; iter < &mStates [kMaxSessionCount ]; iter++)
186
109
{
187
110
if (!iter->IsInitialized ())
188
111
{
@@ -198,131 +121,48 @@ class SecureSessionTable
198
121
}
199
122
200
123
/* *
201
- * Get a peer connection state given a Node Id and Peer's Encryption Key Id.
202
- *
203
- * @param nodeId is the connection to find (based on nodeId). Note that initial connections
204
- * do not have a node id set. Use this if you know the node id should be set.
205
- * @param peerSessionId Encryption key ID used by the peer node.
206
- * @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
124
+ * Get a secure session given a Node Id and Peer's Encryption Key Id.
207
125
*
208
- * @return the state found, nullptr if not found
209
- */
210
- CHECK_RETURN_VALUE
211
- SecureSession * FindPeerConnectionState (Optional<NodeId> nodeId, uint16_t peerSessionId, SecureSession * begin)
212
- {
213
- SecureSession * state = nullptr ;
214
- SecureSession * iter = &mStates [0 ];
215
-
216
- if (begin >= iter && begin < &mStates [kMaxConnectionCount ])
217
- {
218
- iter = begin + 1 ;
219
- }
220
-
221
- for (; iter < &mStates [kMaxConnectionCount ]; iter++)
222
- {
223
- if (!iter->IsInitialized ())
224
- {
225
- continue ;
226
- }
227
- if (peerSessionId == kAnyKeyId || iter->GetPeerSessionId () == peerSessionId)
228
- {
229
- if (nodeId.ValueOr (kUndefinedNodeId ) == kUndefinedNodeId || iter->GetPeerNodeId () == kUndefinedNodeId ||
230
- iter->GetPeerNodeId () == nodeId.Value ())
231
- {
232
- state = iter;
233
- break ;
234
- }
235
- }
236
- }
237
- return state;
238
- }
239
-
240
- /* *
241
- * Get a peer connection state given the local Encryption Key Id.
242
- *
243
- * @param keyId Encryption key ID assigned by the local node.
244
- * @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
245
- *
246
- * @return the state found, nullptr if not found
247
- */
248
- CHECK_RETURN_VALUE
249
- SecureSession * FindPeerConnectionState (uint16_t keyId, SecureSession * begin)
250
- {
251
- SecureSession * state = nullptr ;
252
- SecureSession * iter = &mStates [0 ];
253
-
254
- VerifyOrDie (begin == nullptr || (begin >= iter && begin < &mStates [kMaxConnectionCount ]));
255
-
256
- if (begin != nullptr )
257
- {
258
- iter = begin + 1 ;
259
- }
260
-
261
- for (; iter < &mStates [kMaxConnectionCount ]; iter++)
262
- {
263
- if (!iter->IsInitialized ())
264
- {
265
- continue ;
266
- }
267
-
268
- if (iter->GetLocalSessionId () == keyId)
269
- {
270
- state = iter;
271
- break ;
272
- }
273
- }
274
- return state;
275
- }
276
-
277
- /* *
278
- * Get a peer connection state given a Node Id and Peer's Encryption Key Id.
279
- *
280
- * @param nodeId is the connection to find (based on peer nodeId). Note that initial connections
281
- * do not have a node id set. Use this if you know the node id should be set.
282
126
* @param localSessionId Encryption key ID used by the local node.
283
127
* @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
284
128
*
285
129
* @return the state found, nullptr if not found
286
130
*/
287
131
CHECK_RETURN_VALUE
288
- SecureSession * FindPeerConnectionStateByLocalKey (Optional<NodeId> nodeId, uint16_t localSessionId, SecureSession * begin)
132
+ SecureSession * FindSecureSessionByLocalKey ( uint16_t localSessionId, SecureSession * begin)
289
133
{
290
134
SecureSession * state = nullptr ;
291
135
SecureSession * iter = &mStates [0 ];
292
136
293
- if (begin >= iter && begin < &mStates [kMaxConnectionCount ])
137
+ if (begin >= iter && begin < &mStates [kMaxSessionCount ])
294
138
{
295
139
iter = begin + 1 ;
296
140
}
297
141
298
- for (; iter < &mStates [kMaxConnectionCount ]; iter++)
142
+ for (; iter < &mStates [kMaxSessionCount ]; iter++)
299
143
{
300
144
if (!iter->IsInitialized ())
301
145
{
302
146
continue ;
303
147
}
304
148
if (iter->GetLocalSessionId () == localSessionId)
305
149
{
306
- if (nodeId.ValueOr (kUndefinedNodeId ) == kUndefinedNodeId || iter->GetPeerNodeId () == kUndefinedNodeId ||
307
- iter->GetPeerNodeId () == nodeId.Value ())
308
- {
309
- state = iter;
310
- break ;
311
- }
150
+ state = iter;
151
+ break ;
312
152
}
313
153
}
314
154
return state;
315
155
}
316
156
317
157
/* *
318
- * Get the first peer connection state that matches the given fabric index.
158
+ * Get the first session that matches the given fabric index.
319
159
*
320
160
* @param fabric The fabric index to match
321
161
*
322
- * @return the state found, nullptr if not found
162
+ * @return the session found, nullptr if not found
323
163
*/
324
164
CHECK_RETURN_VALUE
325
- SecureSession * FindPeerConnectionStateByFabric (FabricIndex fabric)
165
+ SecureSession * FindSecureSessionByFabric (FabricIndex fabric)
326
166
{
327
167
for (auto & state : mStates )
328
168
{
@@ -338,51 +178,51 @@ class SecureSessionTable
338
178
return nullptr ;
339
179
}
340
180
341
- // / Convenience method to mark a peer connection state as active
342
- void MarkConnectionActive (SecureSession * state) { state->SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ()); }
181
+ // / Convenience method to mark a session as active
182
+ void MarkSessionActive (SecureSession * state) { state->SetLastActivityTimeMs (mTimeSource .GetCurrentMonotonicTimeMs ()); }
343
183
344
- // / Convenience method to expired a peer connection state and fired the related callback
184
+ // / Convenience method to expired a session and fired the related callback
345
185
template <typename Callback>
346
- void MarkConnectionExpired (SecureSession * state, Callback callback)
186
+ void MarkSessionExpired (SecureSession * state, Callback callback)
347
187
{
348
188
callback (*state);
349
189
*state = SecureSession (PeerAddress::Uninitialized ());
350
190
}
351
191
352
192
/* *
353
- * Iterates through all active connections and expires any connection with an idle time
193
+ * Iterates through all active sessions and expires any sessions with an idle time
354
194
* larger than the given amount.
355
195
*
356
- * Expiring a connection involves callback execution and then clearing the internal state.
196
+ * Expiring a session involves callback execution and then clearing the internal state.
357
197
*/
358
198
template <typename Callback>
359
- void ExpireInactiveConnections (uint64_t maxIdleTimeMs, Callback callback)
199
+ void ExpireInactiveSessions (uint64_t maxIdleTimeMs, Callback callback)
360
200
{
361
201
const uint64_t currentTime = mTimeSource .GetCurrentMonotonicTimeMs ();
362
202
363
- for (size_t i = 0 ; i < kMaxConnectionCount ; i++)
203
+ for (size_t i = 0 ; i < kMaxSessionCount ; i++)
364
204
{
365
- if (!mStates [i].GetPeerAddress (). IsInitialized ())
205
+ if (!mStates [i].IsInitialized ())
366
206
{
367
- continue ; // not an active connection
207
+ continue ; // not an active session
368
208
}
369
209
370
- uint64_t connectionActiveTime = mStates [i].GetLastActivityTimeMs ();
371
- if (connectionActiveTime + maxIdleTimeMs >= currentTime)
210
+ uint64_t sessionActiveTime = mStates [i].GetLastActivityTimeMs ();
211
+ if (sessionActiveTime + maxIdleTimeMs >= currentTime)
372
212
{
373
213
continue ; // not expired
374
214
}
375
215
376
- MarkConnectionExpired (&mStates [i], callback);
216
+ MarkSessionExpired (&mStates [i], callback);
377
217
}
378
218
}
379
219
380
- // / Allows access to the underlying time source used for keeping track of connection active time
220
+ // / Allows access to the underlying time source used for keeping track of session active time
381
221
Time::TimeSource<kTimeSource > & GetTimeSource () { return mTimeSource ; }
382
222
383
223
private:
384
224
Time::TimeSource<kTimeSource > mTimeSource ;
385
- SecureSession mStates [kMaxConnectionCount ];
225
+ SecureSession mStates [kMaxSessionCount ];
386
226
};
387
227
388
228
} // namespace Transport
0 commit comments