Skip to content

Commit

Permalink
Add ScopedNodeId and return it by Session::GetPeer() (#16000)
Browse files Browse the repository at this point in the history
* Add ScopedNodeId and return it by Session::GetPeer()

* Move ScopedNodeId to a new file

* Add guidance about PeerId class

* Update src/lib/core/ScopedNodeId.h

Co-authored-by: Jerry Johns <johnsj@google.com>

* Restyle

* Undo Progress/Error delta in logging

Co-authored-by: Andrei Litvin <andy314@gmail.com>
Co-authored-by: Jerry Johns <johnsj@google.com>
  • Loading branch information
3 people authored and pull[bot] committed Apr 6, 2022
1 parent 4313163 commit 5728580
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/core/PeerId.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ constexpr bool IsValidFabricId(FabricId aFabricId)
return aFabricId != kUndefinedFabricId;
}

/* NOTE: PeerId should be only used by mDNS, because it contains a compressed fabric id which is not unique, and the compressed
* fabric id is only used for mDNS announcement. ScopedNodeId which contains a node id and fabirc index, should be used in prefer of
* PeerId. ScopedNodeId is locally unique.
*/
// TODO: remove PeerId usage outside lib/dns, move PeerId into lib/dns
/// A peer is identified by a node id within a compressed fabric ID
class PeerId
{
Expand Down
44 changes: 44 additions & 0 deletions src/lib/core/ScopedNodeId.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <lib/core/DataModelTypes.h>
#include <lib/core/NodeId.h>

namespace chip {

/// The ScopedNodeId provides an identifier for an operational node on the network that is only valid for use within the current
/// Matter stack instance. It is not to be exchanged or directly used remotely over the network.
class ScopedNodeId
{
public:
ScopedNodeId() : mNodeId(kUndefinedNodeId), mFabricIndex(kUndefinedFabricIndex) {}
ScopedNodeId(NodeId nodeId, FabricIndex fabricIndex) : mNodeId(nodeId), mFabricIndex(fabricIndex) {}

NodeId GetNodeId() const { return mNodeId; }
FabricIndex GetFabricIndex() const { return mFabricIndex; }

bool IsOperational() const { return mFabricIndex != kUndefinedFabricIndex && IsOperationalNodeId(mNodeId); }
bool operator==(const ScopedNodeId & that) const { return (mNodeId == that.mNodeId) && (mFabricIndex == that.mFabricIndex); }
bool operator!=(const ScopedNodeId & that) const { return !(*this == that); }

private:
NodeId mNodeId;
FabricIndex mFabricIndex;
};

} // namespace chip
4 changes: 4 additions & 0 deletions src/transport/GroupSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class IncomingGroupSession : public Session
const char * GetSessionTypeString() const override { return "incoming group"; };
#endif

ScopedNodeId GetPeer() const override { return ScopedNodeId(mSourceNodeId, GetFabricIndex()); }

Access::SubjectDescriptor GetSubjectDescriptor() const override
{
Access::SubjectDescriptor subjectDescriptor;
Expand Down Expand Up @@ -131,6 +133,8 @@ class OutgoingGroupSession : public Session
const char * GetSessionTypeString() const override { return "outgoing group"; };
#endif

ScopedNodeId GetPeer() const override { return ScopedNodeId(); }

Access::SubjectDescriptor GetSubjectDescriptor() const override
{
return Access::SubjectDescriptor(); // no subject exists for outgoing group session.
Expand Down
5 changes: 5 additions & 0 deletions src/transport/SecureSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
namespace chip {
namespace Transport {

ScopedNodeId SecureSession::GetPeer() const
{
return ScopedNodeId(mPeerNodeId, GetFabricIndex());
}

Access::SubjectDescriptor SecureSession::GetSubjectDescriptor() const
{
Access::SubjectDescriptor subjectDescriptor;
Expand Down
1 change: 1 addition & 0 deletions src/transport/SecureSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class SecureSession : public Session
const char * GetSessionTypeString() const override { return "secure"; };
#endif

ScopedNodeId GetPeer() const override;
Access::SubjectDescriptor GetSubjectDescriptor() const override;

bool RequireMRP() const override { return GetPeerAddress().GetTransportType() == Transport::Type::kUdp; }
Expand Down
3 changes: 3 additions & 0 deletions src/transport/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <credentials/FabricTable.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/PeerId.h>
#include <lib/core/ScopedNodeId.h>
#include <messaging/ReliableMessageProtocolConfig.h>
#include <transport/SessionHolder.h>
#include <transport/raw/PeerAddress.h>
Expand Down Expand Up @@ -65,6 +67,7 @@ class Session
virtual void Retain() {}
virtual void Release() {}

virtual ScopedNodeId GetPeer() const = 0;
virtual Access::SubjectDescriptor GetSubjectDescriptor() const = 0;
virtual bool RequireMRP() const = 0;
virtual const ReliableMessageProtocolConfig & GetMRPConfig() const = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/transport/UnauthenticatedSessionTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class UnauthenticatedSession : public Session, public ReferenceCounted<Unauthent
void Retain() override { ReferenceCounted<UnauthenticatedSession, UnauthenticatedSessionDeleter, 0>::Retain(); }
void Release() override { ReferenceCounted<UnauthenticatedSession, UnauthenticatedSessionDeleter, 0>::Release(); }

ScopedNodeId GetPeer() const override { return ScopedNodeId(kUndefinedNodeId, GetFabricIndex()); }

Access::SubjectDescriptor GetSubjectDescriptor() const override
{
return Access::SubjectDescriptor(); // return an empty ISD for unauthenticated session.
Expand Down

0 comments on commit 5728580

Please sign in to comment.