Skip to content

Commit

Permalink
Address comments from review
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Sep 15, 2020
1 parent 5754565 commit 1141420
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 410 deletions.
7 changes: 4 additions & 3 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
* #CHIP_CONFIG_USE_MICRO_ECC.
*/
#ifndef CHIP_CONFIG_USE_OPENSSL_ECC
#define CHIP_CONFIG_USE_OPENSSL_ECC 1
#define CHIP_CONFIG_USE_OPENSSL_ECC 0
#endif // CHIP_CONFIG_USE_OPENSSL_ECC

/**
Expand Down Expand Up @@ -876,7 +876,7 @@
*
*/
#ifndef CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL
#define CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL 1
#define CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL 0
#endif // CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL

/**
Expand All @@ -902,7 +902,8 @@
CHIP_CONFIG_HASH_IMPLEMENTATION_MINCRYPT + \
CHIP_CONFIG_HASH_IMPLEMENTATION_OPENSSL + \
CHIP_CONFIG_HASH_IMPLEMENTATION_MBEDTLS) != 1)
#error "Please assert exactly one CHIP_CONFIG_HASH_IMPLEMENTATION_... option."
// TODO(#2093): Need to reimplement with CHIP defined crypto primitives
// #error "Please assert exactly one CHIP_CONFIG_HASH_IMPLEMENTATION_... option."
#endif


Expand Down
33 changes: 31 additions & 2 deletions src/lib/message/CHIPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ CHIP_ERROR ChipConnection::SendMessage(ChipMessageInfo * msgInfo, PacketBuffer *
else
#endif
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
res = mTcpEndPoint->Send(msgBuf, true);
#endif
}
msgBuf = NULL;

Expand Down Expand Up @@ -604,7 +606,9 @@ CHIP_ERROR ChipConnection::Shutdown()
if (State == kState_Connected)
{
State = kState_SendShutdown;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mTcpEndPoint->Shutdown();
#endif
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -794,7 +798,11 @@ CHIP_ERROR ChipConnection::EnableKeepAlive(uint16_t interval, uint16_t timeoutCo
if (!StateAllowsSend())
return CHIP_ERROR_INCORRECT_STATE;

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
return mTcpEndPoint->EnableKeepAlive(interval, timeoutCount);
#endif

return CHIP_ERROR_NOT_IMPLEMENTED;
}

/**
Expand Down Expand Up @@ -830,7 +838,11 @@ CHIP_ERROR ChipConnection::DisableKeepAlive()
if (!StateAllowsSend())
return CHIP_ERROR_INCORRECT_STATE;

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
return mTcpEndPoint->DisableKeepAlive();
#endif

return CHIP_ERROR_NOT_IMPLEMENTED;
}

/**
Expand Down Expand Up @@ -876,7 +888,11 @@ CHIP_ERROR ChipConnection::SetUserTimeout(uint32_t userTimeoutMillis)
if (!StateAllowsSend())
return CHIP_ERROR_INCORRECT_STATE;

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
return mTcpEndPoint->SetUserTimeout(userTimeoutMillis);
#endif

return CHIP_ERROR_NOT_IMPLEMENTED;
}

/**
Expand Down Expand Up @@ -930,7 +946,9 @@ CHIP_ERROR ChipConnection::SetIdleTimeout(uint32_t timeoutMS)
else
#endif
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mTcpEndPoint->SetIdleTimeout(timeoutMS);
#endif
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -960,6 +978,7 @@ void ChipConnection::DoClose(CHIP_ERROR err, uint8_t flags)
else
#endif
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (mTcpEndPoint != NULL)
{
if (err == CHIP_NO_ERROR)
Expand All @@ -969,7 +988,7 @@ void ChipConnection::DoClose(CHIP_ERROR err, uint8_t flags)
mTcpEndPoint->Free();
mTcpEndPoint = NULL;
}

#endif
#if CHIP_CONFIG_ENABLE_DNS_RESOLVER
// Cancel any outstanding DNS query that may still be active. (This situation can
// arise if the application initiates a connection to a peer using a DNS name and
Expand Down Expand Up @@ -1085,6 +1104,7 @@ void ChipConnection::StartSession()

CHIP_ERROR ChipConnection::StartConnect()
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
CHIP_ERROR err;

// TODO: this is wrong. PeerNodeId should only be set once we have a successful connection (including security).
Expand Down Expand Up @@ -1139,8 +1159,13 @@ CHIP_ERROR ChipConnection::StartConnect()
#endif
// Initiate the TCP connection.
return mTcpEndPoint->Connect(PeerAddr, PeerPort, mTargetInterface);

#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif //INET_CONFIG_ENABLE_TCP_ENDPOINT
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void ChipConnection::HandleConnectComplete(TCPEndPoint * endPoint, INET_ERROR conRes)
{
ChipConnection * con = (ChipConnection *) endPoint->AppState;
Expand Down Expand Up @@ -1405,6 +1430,7 @@ void ChipConnection::HandleTcpConnectionClosed(TCPEndPoint * endPoint, INET_ERRO
err = CHIP_ERROR_CONNECTION_CLOSED_UNEXPECTEDLY;
con->DoClose(err, 0);
}
#endif // #if INET_CONFIG_ENABLE_TCP_ENDPOINT

void ChipConnection::HandleSecureSessionEstablished(ChipSecurityManager * sm, ChipConnection * con, void * reqState,
uint16_t sessionKeyId, uint64_t peerNodeId, uint8_t encType)
Expand Down Expand Up @@ -1450,7 +1476,9 @@ void ChipConnection::Init(ChipMessageLayer * msgLayer)
OnConnectionClosed = DefaultConnectionClosedHandler;
OnReceiveError = NULL;
memset(&mPeerAddrs, 0, sizeof(mPeerAddrs));
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mTcpEndPoint = NULL;
#endif
#if CONFIG_NETWORK_LAYER_BLE
mBleEndPoint = NULL;
#endif
Expand Down Expand Up @@ -1482,6 +1510,7 @@ void ChipConnection::DefaultConnectionClosedHandler(ChipConnection * con, CHIP_E
con->Close();
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void ChipConnection::MakeConnectedTcp(TCPEndPoint * endPoint, const IPAddress & localAddr, const IPAddress & peerAddr)
{
mTcpEndPoint = endPoint;
Expand Down Expand Up @@ -1518,6 +1547,7 @@ void ChipConnection::MakeConnectedTcp(TCPEndPoint * endPoint, const IPAddress &

State = kState_Connected;
}
#endif

#if CONFIG_NETWORK_LAYER_BLE

Expand Down Expand Up @@ -1708,7 +1738,6 @@ void ChipConnection::MakeConnectedBle(BLEEndPoint * endPoint)

mRefCount++;
}

#endif // CONFIG_NETWORK_LAYER_BLE

void ChipConnection::DisconnectOnError(CHIP_ERROR err)
Expand Down
20 changes: 15 additions & 5 deletions src/lib/message/CHIPMessageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ CHIP_ERROR ChipMessageLayer::Init(InitContext * context)
SetEphemeralUDPPortEnabled(context->enableEphemeralUDPPort);
#endif

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mIPv6TCPListen = NULL;
#endif

mIPv6UDP = NULL;

#if INET_CONFIG_ENABLE_IPV4
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
mIPv4TCPListen = NULL;
#endif
mIPv4UDP = NULL;
#endif // INET_CONFIG_ENABLE_IPV4

Expand Down Expand Up @@ -1618,6 +1623,7 @@ void ChipMessageLayer::HandleIncomingBleConnection(BLEEndPoint * bleEP)
}
#endif /* CONFIG_NETWORK_LAYER_BLE */

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void ChipMessageLayer::HandleIncomingTcpConnection(TCPEndPoint * listeningEP, TCPEndPoint * conEP, const IPAddress & peerAddr,
uint16_t peerPort)
{
Expand Down Expand Up @@ -1705,6 +1711,7 @@ void ChipMessageLayer::HandleAcceptError(TCPEndPoint * ep, INET_ERROR err)
if (msgLayer->OnAcceptError != NULL)
msgLayer->OnAcceptError(msgLayer, err);
}
#endif /* INET_CONFIG_ENABLE_TCP_ENDPOINT */

/**
* Refresh the InetLayer endpoints based on the current state of the system's network interfaces.
Expand Down Expand Up @@ -1754,6 +1761,7 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()

#endif // CHIP_CONFIG_ENABLE_TARGETED_LISTEN

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
// ================================================================================
// Enable / disable TCP listening endpoints...
// ================================================================================
Expand All @@ -1763,7 +1771,6 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()
const bool listenIPv6TCP = (listenTCP && listenIPv6);

#if INET_CONFIG_ENABLE_IPV4

const bool listenIPv4TCP = (listenTCP && listenIPv4);

// Enable / disable the CHIP IPv4 TCP listening endpoint
Expand All @@ -1774,7 +1781,6 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()
err =
RefreshEndpoint(mIPv4TCPListen, listenIPv4TCP, "CHIP IPv4 TCP listen", kIPAddressType_IPv4, listenIPv4Addr, CHIP_PORT);
SuccessOrExit(err);

#endif // INET_CONFIG_ENABLE_IPV4

// Enable / disable the CHIP IPv6 TCP listening endpoint
Expand All @@ -1787,7 +1793,6 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()
SuccessOrExit(err);

#if CHIP_CONFIG_ENABLE_UNSECURED_TCP_LISTEN

// Enable / disable the Unsecured IPv6 TCP listening endpoint
//
// The Unsecured IPv6 TCP listening endpoint is use to listen for incoming IPv6 TCP connections
Expand All @@ -1798,9 +1803,9 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()
err = RefreshEndpoint(mUnsecuredIPv6TCPListen, listenUnsecuredIPv6TCP, "unsecured IPv6 TCP listen", kIPAddressType_IPv6,
listenIPv6Addr, CHIP_UNSECURED_PORT);
SuccessOrExit(err);

#endif // CHIP_CONFIG_ENABLE_UNSECURED_TCP_LISTEN
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

// ================================================================================
// Enable / disable UDP endpoints...
Expand Down Expand Up @@ -1907,6 +1912,7 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoints()
return err;
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
CHIP_ERROR ChipMessageLayer::RefreshEndpoint(TCPEndPoint *& endPoint, bool enable, const char * name, IPAddressType addrType,
IPAddress addr, uint16_t port)
{
Expand Down Expand Up @@ -1958,6 +1964,7 @@ CHIP_ERROR ChipMessageLayer::RefreshEndpoint(TCPEndPoint *& endPoint, bool enabl
}
return err;
}
#endif /* INET_CONFIG_ENABLE_TCP_ENDPOINT */

CHIP_ERROR ChipMessageLayer::RefreshEndpoint(UDPEndPoint *& endPoint, bool enable, const char * name, IPAddressType addrType,
IPAddress addr, uint16_t port, InterfaceId intfId)
Expand Down Expand Up @@ -2097,11 +2104,13 @@ void ChipMessageLayer::CloseListeningEndpoints(void)
{
ChipBindLog("Closing endpoints");

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (mIPv6TCPListen != NULL)
{
mIPv6TCPListen->Free();
mIPv6TCPListen = NULL;
}
#endif

if (mIPv6UDP != NULL)
{
Expand Down Expand Up @@ -2140,12 +2149,13 @@ void ChipMessageLayer::CloseListeningEndpoints(void)
#endif // CHIP_CONFIG_ENABLE_UNSECURED_TCP_LISTEN

#if INET_CONFIG_ENABLE_IPV4

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (mIPv4TCPListen != NULL)
{
mIPv4TCPListen->Free();
mIPv4TCPListen = NULL;
}
#endif

if (mIPv4UDP != NULL)
{
Expand Down
17 changes: 16 additions & 1 deletion src/lib/message/CHIPMessageLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stdint.h>
#include <string.h>

#include <inet/InetLayer.h>
#include <message/CHIPFabricState.h>
#include <support/DLLUtil.h>
#include <system/SystemStats.h>
Expand Down Expand Up @@ -288,7 +289,9 @@ class ChipConnection
CHIP_ERROR ResetUserTimeout(void);
uint16_t LogId(void) const { return static_cast<uint16_t>(reinterpret_cast<intptr_t>(this)); }

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * GetTCPEndPoint(void) const { return mTcpEndPoint; }
#endif

/**
* This function is the application callback that is invoked when a connection setup is complete.
Expand Down Expand Up @@ -349,7 +352,9 @@ class ChipConnection
} DoCloseFlags;

IPAddress mPeerAddrs[CHIP_CONFIG_CONNECT_IP_ADDRS];
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * mTcpEndPoint;
#endif
InterfaceId mTargetInterface;
uint32_t mConnectTimeout;
uint8_t mRefCount;
Expand All @@ -365,7 +370,6 @@ class ChipConnection
uint8_t mFlags; /**< Various flags associated with the connection. */

void Init(ChipMessageLayer * msgLayer);
void MakeConnectedTcp(TCPEndPoint * endPoint, const IPAddress & localAddr, const IPAddress & peerAddr);
CHIP_ERROR StartConnect(void);
void DoClose(CHIP_ERROR err, uint8_t flags);
CHIP_ERROR TryNextPeerAddress(CHIP_ERROR lastErr);
Expand All @@ -379,9 +383,12 @@ class ChipConnection
CHIP_ERROR StartConnectToAddressLiteral(const char * peerAddr, size_t peerAddrLen);

static void HandleResolveComplete(void * appState, INET_ERROR err, uint8_t addrCount, IPAddress * addrArray);
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void MakeConnectedTcp(TCPEndPoint * endPoint, const IPAddress & localAddr, const IPAddress & peerAddr);
static void HandleConnectComplete(TCPEndPoint * endPoint, INET_ERROR conRes);
static void HandleDataReceived(TCPEndPoint * endPoint, PacketBuffer * data);
static void HandleTcpConnectionClosed(TCPEndPoint * endPoint, INET_ERROR err);
#endif
static void HandleSecureSessionEstablished(ChipSecurityManager * sm, ChipConnection * con, void * reqState,
uint16_t sessionKeyId, uint64_t peerNodeId, uint8_t encType);
static void HandleSecureSessionError(ChipSecurityManager * sm, ChipConnection * con, void * reqState, CHIP_ERROR localErr,
Expand Down Expand Up @@ -638,7 +645,9 @@ class DLL_EXPORT ChipMessageLayer
kFlag_ForceRefreshUDPEndPoints = 0x10,
};

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * mIPv6TCPListen;
#endif
UDPEndPoint * mIPv6UDP;
ChipConnection mConPool[CHIP_CONFIG_MAX_CONNECTIONS];
uint8_t mFlags;
Expand All @@ -656,7 +665,9 @@ class DLL_EXPORT ChipMessageLayer

#if INET_CONFIG_ENABLE_IPV4
UDPEndPoint * mIPv4UDP;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * mIPv4TCPListen;
#endif
#endif // INET_CONFIG_ENABLE_IPV4

#if CHIP_CONFIG_ENABLE_EPHEMERAL_UDP_PORT
Expand All @@ -679,8 +690,10 @@ class DLL_EXPORT ChipMessageLayer

void CloseListeningEndpoints(void);

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
CHIP_ERROR RefreshEndpoint(TCPEndPoint *& endPoint, bool enable, const char * name, IPAddressType addrType, IPAddress addr,
uint16_t port);
#endif
CHIP_ERROR RefreshEndpoint(UDPEndPoint *& endPoint, bool enable, const char * name, IPAddressType addrType, IPAddress addr,
uint16_t port, InterfaceId intfId);

Expand All @@ -699,9 +712,11 @@ class DLL_EXPORT ChipMessageLayer

static void HandleUDPMessage(UDPEndPoint * endPoint, PacketBuffer * msg, const IPPacketInfo * pktInfo);
static void HandleUDPReceiveError(UDPEndPoint * endPoint, INET_ERROR err, const IPPacketInfo * pktInfo);
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
static void HandleIncomingTcpConnection(TCPEndPoint * listeningEndPoint, TCPEndPoint * conEndPoint, const IPAddress & peerAddr,
uint16_t peerPort);
static void HandleAcceptError(TCPEndPoint * endPoint, INET_ERROR err);
#endif
static void Encrypt_AES128CTRSHA1(const ChipMessageInfo * msgInfo, const uint8_t * key, const uint8_t * inData, uint16_t inLen,
uint8_t * outBuf);
static void ComputeIntegrityCheck_AES128CTRSHA1(const ChipMessageInfo * msgInfo, const uint8_t * key, const uint8_t * inData,
Expand Down
Loading

0 comments on commit 1141420

Please sign in to comment.