Skip to content

Commit 1444052

Browse files
kghostpull[bot]
authored andcommitted
Clean up unusded code in SecureSessionTable (#11041)
1 parent b087ec3 commit 1444052

5 files changed

+130
-337
lines changed

src/app/tests/TestCommandInteraction.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,9 @@ void TestCommandInteraction::TestCommandSenderWithSendCommand(nlTestSuite * apSu
322322
System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
323323

324324
AddCommandDataIB(apSuite, apContext, &commandSender, false);
325-
err = commandSender.SendCommandRequest(kTestDeviceNodeId, gFabricIndex, Optional<SessionHandle>::Missing());
326-
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NOT_CONNECTED);
325+
err =
326+
commandSender.SendCommandRequest(0 /* nodeid */, 0 /* fabricindex */, Optional<SessionHandle>(ctx.GetSessionBobToAlice()));
327+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
327328

328329
GenerateReceivedCommand(apSuite, apContext, buf, true /*aNeedCommandData*/);
329330
err = commandSender.ProcessCommandMessage(std::move(buf), Command::CommandRoleId::SenderId);

src/transport/SecureSessionTable.h

+40-200
Original file line numberDiff line numberDiff line change
@@ -30,73 +30,31 @@ namespace Transport {
3030
constexpr const uint16_t kAnyKeyId = 0xffff;
3131

3232
/**
33-
* Handles a set of peer connection states.
33+
* Handles a set of sessions.
3434
*
3535
* 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.
3838
*/
39-
template <size_t kMaxConnectionCount, Time::Source kTimeSource = Time::Source::kSystem>
39+
template <size_t kMaxSessionCount, Time::Source kTimeSource = Time::Source::kSystem>
4040
class SecureSessionTable
4141
{
4242
public:
4343
/**
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.
4545
*
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
8847
* @param peerSessionId represents the encryption key ID assigned by peer node
8948
* @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.
9150
*
9251
* @note the newly created state will have an 'active' time set based on the current time source.
9352
*
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
9554
* has been reached (with CHIP_ERROR_NO_MEMORY).
9655
*/
9756
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)
10058
{
10159
CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
10260

@@ -105,20 +63,16 @@ class SecureSessionTable
10563
*state = nullptr;
10664
}
10765

108-
for (size_t i = 0; i < kMaxConnectionCount; i++)
66+
for (size_t i = 0; i < kMaxSessionCount; i++)
10967
{
11068
if (!mStates[i].IsInitialized())
11169
{
11270
mStates[i] = SecureSession();
71+
mStates[i].SetPeerNodeId(peerNode);
11372
mStates[i].SetPeerSessionId(peerSessionId);
11473
mStates[i].SetLocalSessionId(localSessionId);
11574
mStates[i].SetLastActivityTimeMs(mTimeSource.GetCurrentMonotonicTimeMs());
11675

117-
if (peerNode.ValueOr(kUndefinedNodeId) != kUndefinedNodeId)
118-
{
119-
mStates[i].SetPeerNodeId(peerNode.Value());
120-
}
121-
12276
if (state)
12377
{
12478
*state = &mStates[i];
@@ -133,56 +87,25 @@ class SecureSessionTable
13387
}
13488

13589
/**
136-
* Get a peer connection state given a Peer address.
90+
* Get a secure session given a Node Id.
13791
*
138-
* @param address is the connection to find (based on address)
92+
* @param nodeId is the session to find (based on nodeId).
13993
* @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
14094
*
14195
* @return the state found, nullptr if not found
14296
*/
14397
CHECK_RETURN_VALUE
144-
SecureSession * FindPeerConnectionState(const PeerAddress & address, SecureSession * begin)
98+
SecureSession * FindSecureSession(NodeId nodeId, SecureSession * begin)
14599
{
146100
SecureSession * state = nullptr;
147101
SecureSession * iter = &mStates[0];
148102

149-
if (begin >= iter && begin < &mStates[kMaxConnectionCount])
103+
if (begin >= iter && begin < &mStates[kMaxSessionCount])
150104
{
151105
iter = begin + 1;
152106
}
153107

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++)
186109
{
187110
if (!iter->IsInitialized())
188111
{
@@ -198,131 +121,48 @@ class SecureSessionTable
198121
}
199122

200123
/**
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.
207125
*
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.
282126
* @param localSessionId Encryption key ID used by the local node.
283127
* @param begin If a member of the pool, will start search from the next item. Can be nullptr to search from start.
284128
*
285129
* @return the state found, nullptr if not found
286130
*/
287131
CHECK_RETURN_VALUE
288-
SecureSession * FindPeerConnectionStateByLocalKey(Optional<NodeId> nodeId, uint16_t localSessionId, SecureSession * begin)
132+
SecureSession * FindSecureSessionByLocalKey(uint16_t localSessionId, SecureSession * begin)
289133
{
290134
SecureSession * state = nullptr;
291135
SecureSession * iter = &mStates[0];
292136

293-
if (begin >= iter && begin < &mStates[kMaxConnectionCount])
137+
if (begin >= iter && begin < &mStates[kMaxSessionCount])
294138
{
295139
iter = begin + 1;
296140
}
297141

298-
for (; iter < &mStates[kMaxConnectionCount]; iter++)
142+
for (; iter < &mStates[kMaxSessionCount]; iter++)
299143
{
300144
if (!iter->IsInitialized())
301145
{
302146
continue;
303147
}
304148
if (iter->GetLocalSessionId() == localSessionId)
305149
{
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;
312152
}
313153
}
314154
return state;
315155
}
316156

317157
/**
318-
* Get the first peer connection state that matches the given fabric index.
158+
* Get the first session that matches the given fabric index.
319159
*
320160
* @param fabric The fabric index to match
321161
*
322-
* @return the state found, nullptr if not found
162+
* @return the session found, nullptr if not found
323163
*/
324164
CHECK_RETURN_VALUE
325-
SecureSession * FindPeerConnectionStateByFabric(FabricIndex fabric)
165+
SecureSession * FindSecureSessionByFabric(FabricIndex fabric)
326166
{
327167
for (auto & state : mStates)
328168
{
@@ -338,51 +178,51 @@ class SecureSessionTable
338178
return nullptr;
339179
}
340180

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()); }
343183

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
345185
template <typename Callback>
346-
void MarkConnectionExpired(SecureSession * state, Callback callback)
186+
void MarkSessionExpired(SecureSession * state, Callback callback)
347187
{
348188
callback(*state);
349189
*state = SecureSession(PeerAddress::Uninitialized());
350190
}
351191

352192
/**
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
354194
* larger than the given amount.
355195
*
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.
357197
*/
358198
template <typename Callback>
359-
void ExpireInactiveConnections(uint64_t maxIdleTimeMs, Callback callback)
199+
void ExpireInactiveSessions(uint64_t maxIdleTimeMs, Callback callback)
360200
{
361201
const uint64_t currentTime = mTimeSource.GetCurrentMonotonicTimeMs();
362202

363-
for (size_t i = 0; i < kMaxConnectionCount; i++)
203+
for (size_t i = 0; i < kMaxSessionCount; i++)
364204
{
365-
if (!mStates[i].GetPeerAddress().IsInitialized())
205+
if (!mStates[i].IsInitialized())
366206
{
367-
continue; // not an active connection
207+
continue; // not an active session
368208
}
369209

370-
uint64_t connectionActiveTime = mStates[i].GetLastActivityTimeMs();
371-
if (connectionActiveTime + maxIdleTimeMs >= currentTime)
210+
uint64_t sessionActiveTime = mStates[i].GetLastActivityTimeMs();
211+
if (sessionActiveTime + maxIdleTimeMs >= currentTime)
372212
{
373213
continue; // not expired
374214
}
375215

376-
MarkConnectionExpired(&mStates[i], callback);
216+
MarkSessionExpired(&mStates[i], callback);
377217
}
378218
}
379219

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
381221
Time::TimeSource<kTimeSource> & GetTimeSource() { return mTimeSource; }
382222

383223
private:
384224
Time::TimeSource<kTimeSource> mTimeSource;
385-
SecureSession mStates[kMaxConnectionCount];
225+
SecureSession mStates[kMaxSessionCount];
386226
};
387227

388228
} // namespace Transport

0 commit comments

Comments
 (0)