diff --git a/src/lib/core/PeerId.h b/src/lib/core/PeerId.h index 4586ee11cda288..d7f355bb50544b 100644 --- a/src/lib/core/PeerId.h +++ b/src/lib/core/PeerId.h @@ -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 { diff --git a/src/lib/core/ScopedNodeId.h b/src/lib/core/ScopedNodeId.h new file mode 100644 index 00000000000000..04512df0c64878 --- /dev/null +++ b/src/lib/core/ScopedNodeId.h @@ -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 +#include + +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 diff --git a/src/transport/GroupSession.h b/src/transport/GroupSession.h index 88315d0527c97f..4114e77db212d1 100644 --- a/src/transport/GroupSession.h +++ b/src/transport/GroupSession.h @@ -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; @@ -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. diff --git a/src/transport/SecureSession.cpp b/src/transport/SecureSession.cpp index e2688869bc509a..2a55e03e5bbd9a 100644 --- a/src/transport/SecureSession.cpp +++ b/src/transport/SecureSession.cpp @@ -20,6 +20,11 @@ namespace chip { namespace Transport { +ScopedNodeId SecureSession::GetPeer() const +{ + return ScopedNodeId(mPeerNodeId, GetFabricIndex()); +} + Access::SubjectDescriptor SecureSession::GetSubjectDescriptor() const { Access::SubjectDescriptor subjectDescriptor; diff --git a/src/transport/SecureSession.h b/src/transport/SecureSession.h index e77fab01eb7fbf..8146884cf15373 100644 --- a/src/transport/SecureSession.h +++ b/src/transport/SecureSession.h @@ -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; } diff --git a/src/transport/Session.h b/src/transport/Session.h index 91b37de45b94a5..8c1ea3291e9bc9 100644 --- a/src/transport/Session.h +++ b/src/transport/Session.h @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include #include @@ -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; diff --git a/src/transport/UnauthenticatedSessionTable.h b/src/transport/UnauthenticatedSessionTable.h index 68c8afc66a72eb..5ac2211fd06d05 100644 --- a/src/transport/UnauthenticatedSessionTable.h +++ b/src/transport/UnauthenticatedSessionTable.h @@ -74,6 +74,8 @@ class UnauthenticatedSession : public Session, public ReferenceCounted::Retain(); } void Release() override { ReferenceCounted::Release(); } + ScopedNodeId GetPeer() const override { return ScopedNodeId(kUndefinedNodeId, GetFabricIndex()); } + Access::SubjectDescriptor GetSubjectDescriptor() const override { return Access::SubjectDescriptor(); // return an empty ISD for unauthenticated session.