Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
#endif

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
advertiseParameters.SetTCPSupportModes(chip::Dnssd::TCPModeAdvertise::kTCPClientServer);
advertiseParameters.SetTCPSupportModes(mTCPServerEnabled ? chip::Dnssd::TCPModeAdvertise::kTCPClientServer
: chip::Dnssd::TCPModeAdvertise::kTCPClient);
#endif
auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();

Expand Down
8 changes: 8 additions & 0 deletions src/app/server/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class DLL_EXPORT DnssdServer : public ICDStateObserver
void SetICDManager(ICDManager * manager) { mICDManager = manager; };
#endif

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void SetTCPServerEnabled(bool serverEnabled) { mTCPServerEnabled = serverEnabled; };
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

/// Start operational advertising
CHIP_ERROR AdvertiseOperational();

Expand Down Expand Up @@ -179,6 +183,10 @@ class DLL_EXPORT DnssdServer : public ICDStateObserver
ICDManager * mICDManager = nullptr;
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
bool mTCPServerEnabled = true;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

uint16_t mSecuredPort = CHIP_PORT;
uint16_t mUnsecuredPort = CHIP_UDC_PORT;
Inet::InterfaceId mInterfaceId = Inet::InterfaceId::Null();
Expand Down
17 changes: 10 additions & 7 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(mOperationalServicePort)
.SetNativeParams(initParams.endpointNativeParams)

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
TcpListenParameters(DeviceLayer::TCPEndPointManager())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some documentation explaining that the ordering here is very important and pointing to the code that depends on that ordering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to add the documentation in a subsequent PR. Had intended to do so in this one but it auto-merged before that.

.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(mOperationalServicePort)
#endif
#if INET_CONFIG_ENABLE_IPV4
,
UdpListenParameters(DeviceLayer::UDPEndPointManager())
Expand All @@ -225,12 +230,6 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
,
BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer())
#endif
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
TcpListenParameters(DeviceLayer::TCPEndPointManager())
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(mOperationalServicePort)
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAFListenParameters(DeviceLayer::ConnectivityMgr().GetWiFiPAF())
Expand Down Expand Up @@ -330,6 +329,10 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
app::DnssdServer::Instance().SetICDManager(&mICDManager);
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
app::DnssdServer::Instance().SetTCPServerEnabled(mTransports.GetTransport().GetImplAtIndex<1>().IsServerListenEnabled());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is weird. I would have expected the TcpListenParameters to tell the transport whether to enable server, not the transport deciding independently and then the Server having to get the info out somehow. And indeed the parameters have a flag for this. So what's going on here?

In particular, looks to me like Server always enables listen, so this whole IsServerListenEnabled complexity is not needed....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TcpListenParameters does carry the flag and it is set while initializing the TransportMgr. The way I have it in this PR is enabling the Server as a default setting in TcpListenParameters. The controller explicitly sets it to false.
For setting it in the DnssdServer, it is being retrieved from the Transport using the logic above because the setting in the TransportMgr is being assumed to be the source of truth.

#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

if (GetFabricTable().FabricCount() != 0)
{
#if CONFIG_NETWORK_LAYER_BLE
Expand Down
8 changes: 4 additions & 4 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ inline constexpr size_t kMaxTcpPendingPackets = CHIP_CONFIG_MAX_TCP_PENDING_PACK
// in the Server impl depends on this.
//
using ServerTransportMgr = chip::TransportMgr<chip::Transport::UDP
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>
#endif
#if INET_CONFIG_ENABLE_IPV4
,
chip::Transport::UDP
Expand All @@ -104,10 +108,6 @@ using ServerTransportMgr = chip::TransportMgr<chip::Transport::UDP
,
chip::Transport::BLE<kMaxBlePendingPackets>
#endif
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
chip::Transport::WiFiPAFBase
Expand Down
15 changes: 8 additions & 7 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,15 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
ReturnErrorOnFailure(stateParams.transportMgr->Init(Transport::UdpListenParameters(stateParams.udpEndPointManager)
.SetAddressType(Inet::IPAddressType::kIPv6)
.SetListenPort(params.listenPort)
#if INET_CONFIG_ENABLE_IPV4
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the ordering change here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And note that the ordering here is public API certainly for Server, so you have no idea whom you might be breaking with these ordering changes. Like "introduces silent security bugs" breaking.

,
Transport::TcpListenParameters(stateParams.tcpEndPointManager)
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(params.listenPort)
.SetServerListenEnabled(false) // Initialize as a TCP Client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why would we force controllers to never be TCP servers? This isn't a "by default" like the PR description misleadingly says: it's just force-disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to keep the interaction simple, I was assuming controllers(like chip-tool and commissioning clients) to be TCP clients.
The TcpListenParameters probably need to be percolated further up in order to make it a truly configurable parameter. For now, I have set the controller to be a TCP Client.

#endif
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UdpListenParameters(stateParams.udpEndPointManager)
.SetAddressType(Inet::IPAddressType::kIPv4)
.SetListenPort(params.listenPort)
Expand All @@ -177,12 +184,6 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
,
Transport::BleListenParameters(stateParams.bleLayer)
#endif
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
Transport::TcpListenParameters(stateParams.tcpEndPointManager)
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(params.listenPort)
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAFListenParameters()
Expand Down
8 changes: 4 additions & 4 deletions src/controller/CHIPDeviceControllerSystemState.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ inline constexpr size_t kMaxDeviceTransportTcpPendingPackets = CHIP_CONFIG_MAX_T

using DeviceTransportMgr =
TransportMgr<Transport::UDP /* IPv6 */
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
Transport::TCP<kMaxDeviceTransportTcpActiveConnectionCount, kMaxDeviceTransportTcpPendingPackets>
#endif
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UDP /* IPv4 */
Expand All @@ -79,10 +83,6 @@ using DeviceTransportMgr =
,
Transport::BLE<kMaxDeviceTransportBlePendingPackets> /* BLE */
#endif
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
Transport::TCP<kMaxDeviceTransportTcpActiveConnectionCount, kMaxDeviceTransportTcpPendingPackets>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAF<kMaxDeviceTransportWiFiPAFPendingPackets> /* WiFiPAF */
Expand Down
5 changes: 5 additions & 0 deletions src/transport/TransportMgrBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ void TransportMgrBase::TCPDisconnect(Transport::ActiveTCPConnectionState * conn,
{
mTransport->TCPDisconnect(conn, shouldAbort);
}

bool TransportMgrBase::IsServerListenEnabled()
{
return mTransport->IsServerListenEnabled();
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
Expand Down
2 changes: 2 additions & 0 deletions src/transport/TransportMgrBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class TransportMgrBase : public Transport::RawTransportDelegate

void TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort = 0);

bool IsServerListenEnabled();

void HandleConnectionReceived(Transport::ActiveTCPConnectionState * conn) override;

void HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr) override;
Expand Down
2 changes: 2 additions & 0 deletions src/transport/raw/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class Base
* Disconnect on the active connection that is passed in.
*/
virtual void TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort = 0) {}

virtual bool IsServerListenEnabled() { return true; }
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

/**
Expand Down
37 changes: 23 additions & 14 deletions src/transport/raw/TCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,29 @@ CHIP_ERROR TCPBase::Init(TcpListenParameters & params)

VerifyOrExit(mState == TCPState::kNotReady, err = CHIP_ERROR_INCORRECT_STATE);

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
err = params.GetEndPointManager()->NewEndPoint(&mListenSocket);
#else
err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
SuccessOrExit(err);
mEndpointType = params.GetAddressType();

err = mListenSocket->Bind(params.GetAddressType(), Inet::IPAddress::Any, params.GetListenPort(),
params.GetInterfaceId().IsPresent());
mIsServerListenEnabled = params.IsServerListenEnabled();

// Primary socket endpoint created to help get EndPointManager handle for creating multiple
// connection endpoints at runtime.
err = params.GetEndPointManager()->NewEndPoint(&mListenSocket);
SuccessOrExit(err);

mListenSocket->mAppState = reinterpret_cast<void *>(this);
mListenSocket->OnConnectionReceived = HandleIncomingConnection;
mListenSocket->OnAcceptError = HandleAcceptError;
if (mIsServerListenEnabled)
{
err = mListenSocket->Bind(params.GetAddressType(), Inet::IPAddress::Any, params.GetListenPort(),
params.GetInterfaceId().IsPresent());
SuccessOrExit(err);

mEndpointType = params.GetAddressType();
mListenSocket->mAppState = reinterpret_cast<void *>(this);
mListenSocket->OnConnectionReceived = HandleIncomingConnection;
mListenSocket->OnAcceptError = HandleAcceptError;

err = mListenSocket->Listen(kListenBacklogSize);
SuccessOrExit(err);
err = mListenSocket->Listen(kListenBacklogSize);
SuccessOrExit(err);
ChipLogProgress(Inet, "TCP server listening on port %d for incoming connections", params.GetListenPort());
}

mState = TCPState::kInitialized;

Expand Down Expand Up @@ -673,6 +677,11 @@ void TCPBase::TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool sho
}
}

bool TCPBase::IsServerListenEnabled()
{
return mIsServerListenEnabled;
}

bool TCPBase::HasActiveConnections() const
{
for (size_t i = 0; i < mActiveConnectionsSize; i++)
Expand Down
12 changes: 12 additions & 0 deletions src/transport/raw/TCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,20 @@ class TcpListenParameters
return *this;
}

bool IsServerListenEnabled() const { return mServerListenEnabled; }
TcpListenParameters & SetServerListenEnabled(bool listenEnable)
{
mServerListenEnabled = listenEnable;

return *this;
}

private:
Inet::EndPointManager<Inet::TCPEndPoint> * mEndPointManager; ///< Associated endpoint factory
Inet::IPAddressType mAddressType = Inet::IPAddressType::kIPv6; ///< type of listening socket
uint16_t mListenPort = CHIP_PORT; ///< TCP listen port
Inet::InterfaceId mInterfaceId = Inet::InterfaceId::Null(); ///< Interface to listen on
bool mServerListenEnabled = true; ///< TCP Server mode enabled
};

/**
Expand Down Expand Up @@ -178,6 +187,8 @@ class DLL_EXPORT TCPBase : public Base
// and release from the pool.
void TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort = false) override;

bool IsServerListenEnabled() override;

bool CanSendToPeer(const PeerAddress & address) override
{
return (mState == TCPState::kInitialized) && (address.GetTransportType() == Type::kTcp) &&
Expand Down Expand Up @@ -301,6 +312,7 @@ class DLL_EXPORT TCPBase : public Base
Inet::TCPEndPoint * mListenSocket = nullptr; ///< TCP socket used by the transport
Inet::IPAddressType mEndpointType = Inet::IPAddressType::kUnknown; ///< Socket listening type
TCPState mState = TCPState::kNotReady; ///< State of the TCP transport
bool mIsServerListenEnabled = true; ///< TCP Server mode enabled

// The configured timeout for the connection attempt to the peer, before
// giving up.
Expand Down
14 changes: 14 additions & 0 deletions src/transport/raw/Tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Tuple : public Base
{
return TCPDisconnectImpl<0>(conn, shouldAbort);
}

bool IsServerListenEnabled() override { return IsServerListenEnabledImpl<0>(); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? It doesn't seem to be used. The one use in Server gets something explicitly by index...

And what does this function even mean? "We have some transport that's listening as a server"?

What does it even mean for a generic transport (e.g. UDP) to be "listening as a server"?

#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

void Close() override { return CloseImpl<0>(); }
Expand Down Expand Up @@ -222,6 +224,18 @@ class Tuple : public Base
template <size_t N, typename std::enable_if<(N >= sizeof...(TransportTypes))>::type * = nullptr>
void TCPDisconnectImpl(Transport::ActiveTCPConnectionState * conn, bool shouldAbort = 0)
{}

template <size_t N, typename std::enable_if<(N < sizeof...(TransportTypes))>::type * = nullptr>
bool IsServerListenEnabledImpl()
{
return std::get<N>(mTransports).IsServerListenEnabled() || IsServerListenEnabledImpl<N + 1>();
}

template <size_t N, typename std::enable_if<(N >= sizeof...(TransportTypes))>::type * = nullptr>
bool IsServerListenEnabledImpl()
{
return false;
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

/**
Expand Down
29 changes: 29 additions & 0 deletions src/transport/raw/tests/TestTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,35 @@ TEST_F(TestTCP, CheckSimpleInitTest6)
CheckSimpleInitTest(IPAddressType::kIPv6);
}

TEST_F(TestTCP, InitializeAsTCPClient)
{
TCPImpl tcp;

CHIP_ERROR err = tcp.Init(Transport::TcpListenParameters(mIOContext->GetTCPEndPointManager())
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(gChipTCPPort)
.SetServerListenEnabled(false));

EXPECT_EQ(err, CHIP_NO_ERROR);

bool isServerEnabled = tcp.IsServerListenEnabled();
EXPECT_EQ(isServerEnabled, false);
}

TEST_F(TestTCP, InitializeAsTCPClientServer)
{
TCPImpl tcp;

CHIP_ERROR err = tcp.Init(Transport::TcpListenParameters(mIOContext->GetTCPEndPointManager())
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(gChipTCPPort));

EXPECT_EQ(err, CHIP_NO_ERROR);

bool isServerEnabled = tcp.IsServerListenEnabled();
EXPECT_EQ(isServerEnabled, true);
}

TEST_F(TestTCP, CheckMessageTest6)
{
IPAddress addr;
Expand Down
Loading