Skip to content

Commit

Permalink
API SecureManager::SendMessage, use PeerConnectionState instead NodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Nov 12, 2020
1 parent a3cb13d commit d747abf
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 137 deletions.
17 changes: 14 additions & 3 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ CHIP_ERROR Device::SendMessage(System::PacketBuffer * buffer)
resend = buffer;
}

err = mSessionManager->SendMessage(mDeviceId, buffer);
err = mSessionManager->SendMessage(mSecureSession, buffer);
buffer = nullptr;
ChipLogDetail(Controller, "SendMessage returned %d", err);

Expand All @@ -82,7 +82,7 @@ CHIP_ERROR Device::SendMessage(System::PacketBuffer * buffer)
err = LoadSecureSessionParameters();
SuccessOrExit(err);

err = mSessionManager->SendMessage(mDeviceId, resend);
err = mSessionManager->SendMessage(mSecureSession, resend);
resend = nullptr;
ChipLogDetail(Controller, "Re-SendMessage returned %d", err);
SuccessOrExit(err);
Expand Down Expand Up @@ -164,8 +164,19 @@ CHIP_ERROR Device::Deserialize(const SerializedDevice & input)
return error;
}

void Device::OnNewConnection(SecureSessionHandle session, SecureSessionMgrBase * mgr)
{
mState = ConnectionState::SecureConnected;
mSecureSession = session;
}

void Device::OnConnectionExpired(SecureSessionHandle session, SecureSessionMgrBase * mgr)
{
mState = ConnectionState::NotConnected;
}

void Device::OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
Transport::PeerConnectionState * state, System::PacketBuffer * msgBuf, SecureSessionMgrBase * mgr)
SecureSessionHandle session, System::PacketBuffer * msgBuf, SecureSessionMgrBase * mgr)
{
if (mState == ConnectionState::SecureConnected && mStatusDelegate != nullptr)
{
Expand Down
29 changes: 28 additions & 1 deletion src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ class DLL_EXPORT Device
**/
CHIP_ERROR Deserialize(const SerializedDevice & input);

/**
* @brief
* Called when a new pairing is being established
*
* @param state connection state
* @param mgr A pointer to the SecureSessionMgr
*/
void OnNewConnection(SecureSessionHandle session, SecureSessionMgrBase * mgr);

/**
* @brief
* Called when a connection is closing.
*
* The receiver should release all resources associated with the connection.
*
* @param state connection state
* @param mgr A pointer to the SecureSessionMgr
*/
void OnConnectionExpired(SecureSessionHandle session, SecureSessionMgrBase * mgr);

/**
* @brief
* This function is called when a message is received from the corresponding CHIP
Expand All @@ -155,7 +175,7 @@ class DLL_EXPORT Device
* @param[in] msgBuf The message buffer
* @param[in] mgr Pointer to secure session manager which received the message
*/
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader, Transport::PeerConnectionState * state,
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader, SecureSessionHandle session,
System::PacketBuffer * msgBuf, SecureSessionMgrBase * mgr);

/**
Expand All @@ -167,8 +187,12 @@ class DLL_EXPORT Device

void SetActive(bool active) { mActive = active; }

bool IsSecureConnected() const { return IsActive() && mState == ConnectionState::SecureConnected; }

NodeId GetDeviceId() const { return mDeviceId; }

SecureSessionHandle GetSecureSession() const { return mSecureSession; }

void SetAddress(const Inet::IPAddress & deviceAddr) { mDeviceAddr = deviceAddr; }

SecurePairingSessionSerializable & GetPairing() { return mPairing; }
Expand Down Expand Up @@ -204,6 +228,9 @@ class DLL_EXPORT Device

SecureSessionMgr<Transport::UDP> * mSessionManager;

// mSecureSession will be set iff mState == SecureConnected
SecureSessionHandle mSecureSession;

/**
* @brief
* This function loads the secure session object from the serialized operational
Expand Down
60 changes: 54 additions & 6 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,24 +309,58 @@ CHIP_ERROR DeviceController::ServiceEventSignal()
return err;
}

void DeviceController::OnNewConnection(Transport::PeerConnectionState * peerConnection, SecureSessionMgrBase * mgr) {}
void DeviceController::OnNewConnection(SecureSessionHandle session, SecureSessionMgrBase * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);

index = FindDeviceIndex(mgr->GetPeerConnectionState(session)->GetPeerNodeId());
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnNewConnection(session, mgr);

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to process received message: err %d", err);
}
}

void DeviceController::OnConnectionExpired(SecureSessionHandle session, SecureSessionMgrBase * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);

index = FindDeviceIndex(session);
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnConnectionExpired(session, mgr);

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to process received message: err %d", err);
}
}

void DeviceController::OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader,
Transport::PeerConnectionState * state, System::PacketBuffer * msgBuf,
SecureSessionHandle session, System::PacketBuffer * msgBuf,
SecureSessionMgrBase * mgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t index = 0;
NodeId peer;

VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(header.GetSourceNodeId().HasValue(), err = CHIP_ERROR_INVALID_ARGUMENT);

peer = header.GetSourceNodeId().Value();
index = FindDeviceIndex(peer);
index = FindDeviceIndex(session);
VerifyOrExit(index < kNumMaxActiveDevices, err = CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);

mActiveDevices[index].OnMessageReceived(header, payloadHeader, state, msgBuf, mgr);
mActiveDevices[index].OnMessageReceived(header, payloadHeader, session, msgBuf, mgr);

exit:
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -362,6 +396,20 @@ void DeviceController::ReleaseDevice(uint16_t index)
}
}

uint16_t DeviceController::FindDeviceIndex(SecureSessionHandle session)
{
uint16_t i = 0;
while (i < kNumMaxActiveDevices)
{
if (mActiveDevices[i].IsActive() && mActiveDevices[i].IsSecureConnected() && mActiveDevices[i].GetSecureSession() == session)
{
return i;
}
i++;
}
return i;
}

uint16_t DeviceController::FindDeviceIndex(NodeId id)
{
uint16_t i = 0;
Expand Down
7 changes: 5 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,19 @@ class DLL_EXPORT DeviceController : public SecureSessionMgrDelegate, public Pers
Inet::InetLayer * mInetLayer;

uint16_t GetInactiveDeviceIndex();
uint16_t FindDeviceIndex(SecureSessionHandle session);
[[deprecated("only peer node id is not sufficient to identify a device")]]
uint16_t FindDeviceIndex(NodeId id);
void ReleaseDevice(uint16_t index);
CHIP_ERROR SetPairedDeviceList(const char * pairedDeviceSerializedSet);

private:
//////////// SecureSessionMgrDelegate Implementation ///////////////
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader, Transport::PeerConnectionState * state,
void OnMessageReceived(const PacketHeader & header, const PayloadHeader & payloadHeader, SecureSessionHandle session,
System::PacketBuffer * msgBuf, SecureSessionMgrBase * mgr) override;

void OnNewConnection(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr) override;
void OnNewConnection(SecureSessionHandle session, SecureSessionMgrBase * mgr) override;
void OnConnectionExpired(SecureSessionHandle session, SecureSessionMgrBase * mgr) override;

//////////// PersistentStorageResultDelegate Implementation ///////////////
void OnValue(const char * key, const char * value) override;
Expand Down
17 changes: 10 additions & 7 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CHIP_ERROR ExchangeContext::SendMessage(uint16_t protocolId, uint8_t msgType, Pa

payloadHeader.SetInitiator(IsInitiator());

err = mExchangeMgr->GetSessionMgr()->SendMessage(payloadHeader, mPeerNodeId, msgBuf);
err = mExchangeMgr->GetSessionMgr()->SendMessage(mSecureSession, payloadHeader, msgBuf);
msgBuf = nullptr;
SuccessOrExit(err);

Expand Down Expand Up @@ -144,6 +144,7 @@ CHIP_ERROR ExchangeContext::SendMessage(uint16_t protocolId, uint8_t msgType, Pa
void ExchangeContext::DoClose(bool clearRetransTable)
{
// Clear protocol callbacks
mDelegate->OnExchangeClosing(this);
mDelegate = nullptr;

// Cancel the response timer.
Expand Down Expand Up @@ -191,16 +192,17 @@ void ExchangeContext::Reset()
*this = ExchangeContext();
}

void ExchangeContext::Alloc(ExchangeManager * em, uint16_t ExchangeId, uint64_t PeerNodeId, bool Initiator, void * AppState)
void ExchangeContext::Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator,
void * AppState)
{
VerifyOrDie(mExchangeMgr == nullptr && GetReferenceCount() == 0);

Reset();
Retain();
mExchangeMgr = em;
em->IncrementContextsInUse();
mExchangeId = ExchangeId;
mPeerNodeId = PeerNodeId;
mExchangeId = ExchangeId;
mSecureSession = session;
mFlags.Set(ExFlagValues::kFlagInitiator, Initiator);
mAppState = AppState;

Expand Down Expand Up @@ -232,16 +234,17 @@ void ExchangeContext::Free()
SYSTEM_STATS_DECREMENT(chip::System::Stats::kExchangeMgr_NumContexts);
}

bool ExchangeContext::MatchExchange(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader)
bool ExchangeContext::MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader,
const PayloadHeader & payloadHeader)
{
// A given message is part of a particular exchange if...
return

// The exchange identifier of the message matches the exchange identifier of the context.
(mExchangeId == payloadHeader.GetExchangeID())

// AND The message was received from the peer node associated with the exchange, or the peer node identifier is 'any'.
&& ((mPeerNodeId == kAnyNodeId) || (mPeerNodeId == packetHeader.GetSourceNodeId().Value()))
// AND The message was received from the peer node associated with the exchange
&& (mSecureSession == session)

// AND The message was sent by an initiator and the exchange context is a responder (IsInitiator==false)
// OR The message was sent by a responder and the exchange context is an initiator (IsInitiator==true) (for the broadcast
Expand Down
23 changes: 17 additions & 6 deletions src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class DLL_EXPORT ExchangeContextDelegate
* @param[in] ec A pointer to the ExchangeContext object.
*/
virtual void OnResponseTimeout(ExchangeContext * ec) = 0;

/**
* @brief
* This function is the protocol callback to invoke when the associated
* exchange context is being closed
*
* @param[in] ec A pointer to the ExchangeContext object.
*/
virtual void OnExchangeClosing(ExchangeContext * ec) {}
};

class ExchangeContextDeletor
Expand Down Expand Up @@ -202,7 +211,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch

ExchangeManager * GetExchangeMgr() const { return mExchangeMgr; }

uint64_t GetPeerNodeId() const { return mPeerNodeId; }
SecureSessionHandle GetSecureSession() { return mSecureSession; }

uint16_t GetExchangeId() const { return mExchangeId; }

Expand All @@ -216,7 +225,7 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
void Close();
void Abort();

void Alloc(ExchangeManager * em, uint16_t ExchangeId, uint64_t PeerNodeId, bool Initiator, void * AppState);
void Alloc(ExchangeManager * em, uint16_t ExchangeId, SecureSessionHandle session, bool Initiator, void * AppState);
void Free();
void Reset();

Expand All @@ -234,25 +243,27 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
ExchangeManager * mExchangeMgr;
void * mAppState; // Pointer to application-specific state object.

uint64_t mPeerNodeId; // Node ID of peer node.
uint16_t mExchangeId; // Assigned exchange ID.
SecureSessionHandle mSecureSession; // The connection state
uint16_t mExchangeId; // Assigned exchange ID.

BitFlags<uint16_t, ExFlagValues> mFlags; // Internal state flags

/**
* Search for an existing exchange that the message applies to.
*
* @param[in] conn The connection state of the received message.
*
* @param[in] packetHeader A reference to the PacketHeader object.
*
* @param[in] payloadHeader A reference to the PayloadHeader object.
*
* @retval true If a match is found.
* @retval false If a match is not found.
*/
bool MatchExchange(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader);
bool MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader,
const PayloadHeader & payloadHeader);

void SetInitiator(bool inInitiator);
void SetPeerNodeId(NodeId nodeId) { mPeerNodeId = nodeId; }
void SetExchangeId(uint16_t exId) { mExchangeId = exId; }
void SetExchangeMgr(ExchangeManager * exMgr) { mExchangeMgr = exMgr; }
void SetAppState(void * state) { mAppState = state; }
Expand Down
Loading

0 comments on commit d747abf

Please sign in to comment.