diff --git a/src/inet/BUILD.gn b/src/inet/BUILD.gn index ebffb373509ae3..30c60673916893 100644 --- a/src/inet/BUILD.gn +++ b/src/inet/BUILD.gn @@ -49,8 +49,6 @@ buildconfig_header("inet_buildconfig") { defines += [ "INET_PLATFORM_CONFIG_INCLUDE=${chip_inet_platform_config_include}" ] } - - defines += [ "CHIP_INET_END_POINT_IMPL_CONFIG_FILE=" ] } source_set("inet_config_header") { @@ -69,7 +67,7 @@ static_library("inet") { sources = [ "EndPointBasis.h", - "EndPointBasisImpl${chip_system_config_inet}.h", + "EndPointState${chip_system_config_inet}.h", "IANAConstants.h", "IPAddress-StringFuncts.cpp", "IPAddress.cpp", diff --git a/src/inet/EndPointBasis.h b/src/inet/EndPointBasis.h index d7791815052732..0bee2f192770d9 100644 --- a/src/inet/EndPointBasis.h +++ b/src/inet/EndPointBasis.h @@ -39,7 +39,6 @@ class DLL_EXPORT EndPointBase { public: EndPointBase(InetLayer & aInetLayer, void * aAppState = nullptr) : mAppState(aAppState), mInetLayer(aInetLayer) {} - virtual ~EndPointBase() = default; /** * Returns a reference to the Inet layer object that owns this basis object. @@ -54,9 +53,3 @@ class DLL_EXPORT EndPointBase } // namespace Inet } // namespace chip - -#ifdef CHIP_INET_END_POINT_IMPL_CONFIG_FILE -#include CHIP_INET_END_POINT_IMPL_CONFIG_FILE -#else // CHIP_INET_END_POINT_IMPL_CONFIG_FILE -#include -#endif // CHIP_INET_END_POINT_IMPL_CONFIG_FILE diff --git a/src/inet/EndPointBasisImplLwIP.h b/src/inet/EndPointStateLwIP.h similarity index 83% rename from src/inet/EndPointBasisImplLwIP.h rename to src/inet/EndPointStateLwIP.h index 9d398c2bf182d2..ffc1c18b14c5d1 100644 --- a/src/inet/EndPointBasisImplLwIP.h +++ b/src/inet/EndPointStateLwIP.h @@ -17,7 +17,7 @@ */ /** - * LwIP implementation of EndPointBase. + * Shared state for LwIP implementations of TCPEndPoint and UDPEndPoint. */ #pragma once @@ -32,12 +32,10 @@ struct tcp_pcb; namespace chip { namespace Inet { -class DLL_EXPORT EndPointImplLwIP : public EndPointBase +class DLL_EXPORT EndPointStateLwIP { protected: - EndPointImplLwIP(InetLayer & inetLayer, void * appState = nullptr) : - EndPointBase(inetLayer, appState), mLwIPEndPointType(LwIPEndPointType::Unknown) - {} + EndPointStateLwIP() : mLwIPEndPointType(LwIPEndPointType::Unknown) {} /** Encapsulated LwIP protocol control block */ union @@ -59,7 +57,5 @@ class DLL_EXPORT EndPointImplLwIP : public EndPointBase } mLwIPEndPointType; }; -using EndPointBasis = EndPointImplLwIP; - } // namespace Inet } // namespace chip diff --git a/src/inet/EndPointBasisImplNetworkFramework.h b/src/inet/EndPointStateNetworkFramework.h similarity index 77% rename from src/inet/EndPointBasisImplNetworkFramework.h rename to src/inet/EndPointStateNetworkFramework.h index 8b72c18442fc44..5f70d77f5c7de5 100644 --- a/src/inet/EndPointBasisImplNetworkFramework.h +++ b/src/inet/EndPointStateNetworkFramework.h @@ -17,7 +17,7 @@ */ /** - * Network Framework implementation of EndPointBase. + * Shared state for Network Framework implementations of TCPEndPoint and UDPEndPoint. */ #pragma once @@ -31,16 +31,14 @@ namespace chip { namespace Inet { -class DLL_EXPORT EndPointImplNetworkFramework : public EndPointBase +class DLL_EXPORT EndPointStateNetworkFramework { protected: - EndPointImplNetworkFramework(InetLayer & inetLayer, void * appState = nullptr) : EndPointBase(inetLayer, appState) {} + EndPointStateNetworkFramework() {} nw_parameters_t mParameters; IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ }; -using EndPointBasis = EndPointImplNetworkFramework; - } // namespace Inet } // namespace chip diff --git a/src/inet/EndPointBasisImplSockets.h b/src/inet/EndPointStateSockets.h similarity index 79% rename from src/inet/EndPointBasisImplSockets.h rename to src/inet/EndPointStateSockets.h index f6c4c474541d57..c83b3a106d689b 100644 --- a/src/inet/EndPointBasisImplSockets.h +++ b/src/inet/EndPointStateSockets.h @@ -17,7 +17,7 @@ */ /** - * Sockets implementation of EndPointBase. + * Shared state for socket implementations of TCPEndPoint and UDPEndPoint. */ #pragma once @@ -30,12 +30,10 @@ namespace chip { namespace Inet { -class DLL_EXPORT EndPointImplSockets : public EndPointBase +class DLL_EXPORT EndPointStateSockets { protected: - EndPointImplSockets(InetLayer & inetLayer, void * appState = nullptr) : - EndPointBase(inetLayer, appState), mSocket(kInvalidSocketFd) - {} + EndPointStateSockets() : mSocket(kInvalidSocketFd) {} static constexpr int kInvalidSocketFd = -1; int mSocket; /**< Encapsulated socket descriptor. */ @@ -43,7 +41,5 @@ class DLL_EXPORT EndPointImplSockets : public EndPointBase System::SocketWatchToken mWatch; /**< Socket event watcher */ }; -using EndPointBasis = EndPointImplSockets; - } // namespace Inet } // namespace chip diff --git a/src/inet/InetLayer.cpp b/src/inet/InetLayer.cpp index 75e2d0d1afeb75..8b006a0f83f7b3 100644 --- a/src/inet/InetLayer.cpp +++ b/src/inet/InetLayer.cpp @@ -150,7 +150,7 @@ CHIP_ERROR InetLayer::Shutdown() #if INET_CONFIG_ENABLE_TCP_ENDPOINT // Abort all TCP endpoints owned by this instance. - TCPEndPoint::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { + TCPEndPointImpl::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { if ((lEndPoint != nullptr) && &lEndPoint->Layer() == this) { lEndPoint->Abort(); @@ -161,7 +161,7 @@ CHIP_ERROR InetLayer::Shutdown() #if INET_CONFIG_ENABLE_UDP_ENDPOINT // Close all UDP endpoints owned by this instance. - UDPEndPoint::sPool.ForEachActiveObject([&](UDPEndPoint * lEndPoint) { + UDPEndPointImpl::sPool.ForEachActiveObject([&](UDPEndPoint * lEndPoint) { if ((lEndPoint != nullptr) && &lEndPoint->Layer() == this) { lEndPoint->Close(); @@ -206,7 +206,7 @@ bool InetLayer::IsIdleTimerRunning() bool timerRunning = false; // See if there are any TCP connections with the idle timer check in use. - TCPEndPoint::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { + TCPEndPointImpl::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { if ((lEndPoint != nullptr) && (lEndPoint->mIdleTimeout != 0)) { timerRunning = true; @@ -245,7 +245,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint) VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE); - *retEndPoint = TCPEndPoint::sPool.CreateObject(*this); + *retEndPoint = TCPEndPointImpl::sPool.CreateObject(*this); if (*retEndPoint == nullptr) { ChipLogError(Inet, "%s endpoint pool FULL", "TCP"); @@ -284,7 +284,7 @@ CHIP_ERROR InetLayer::NewUDPEndPoint(UDPEndPoint ** retEndPoint) VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE); - *retEndPoint = UDPEndPoint::sPool.CreateObject(*this); + *retEndPoint = UDPEndPointImpl::sPool.CreateObject(*this); if (*retEndPoint == nullptr) { ChipLogError(Inet, "%s endpoint pool FULL", "UDP"); @@ -303,7 +303,7 @@ void InetLayer::HandleTCPInactivityTimer(chip::System::Layer * aSystemLayer, voi InetLayer & lInetLayer = *reinterpret_cast(aAppState); bool lTimerRequired = lInetLayer.IsIdleTimerRunning(); - TCPEndPoint::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { + TCPEndPointImpl::sPool.ForEachActiveObject([&](TCPEndPoint * lEndPoint) { if (&lEndPoint->Layer() != &lInetLayer) return true; if (!lEndPoint->IsConnected()) diff --git a/src/inet/TCPEndPoint.cpp b/src/inet/TCPEndPoint.cpp index 2eca44961e2718..56fe46609648a0 100644 --- a/src/inet/TCPEndPoint.cpp +++ b/src/inet/TCPEndPoint.cpp @@ -87,9 +87,10 @@ namespace chip { namespace Inet { -BitMapObjectPool TCPEndPoint::sPool; - #if CHIP_SYSTEM_CONFIG_USE_LWIP + +BitMapObjectPool TCPEndPointImplLwIP::sPool; + namespace { /* @@ -108,7 +109,7 @@ err_t start_tcp_timers(void) } // anonymous namespace -CHIP_ERROR TCPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) +CHIP_ERROR TCPEndPointImplLwIP::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) { // Lock LwIP stack LOCK_TCPIP_CORE(); @@ -170,7 +171,7 @@ CHIP_ERROR TCPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, return res; } -CHIP_ERROR TCPEndPoint::ListenImpl(uint16_t backlog) +CHIP_ERROR TCPEndPointImplLwIP::ListenImpl(uint16_t backlog) { // Start listening for incoming connections. mTCP = tcp_listen(mTCP); @@ -183,7 +184,7 @@ CHIP_ERROR TCPEndPoint::ListenImpl(uint16_t backlog) return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) +CHIP_ERROR TCPEndPointImplLwIP::ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) { CHIP_ERROR res = CHIP_NO_ERROR; IPAddressType addrType = addr.Type(); @@ -260,7 +261,7 @@ CHIP_ERROR TCPEndPoint::ConnectImpl(const IPAddress & addr, uint16_t port, Inter return res; } -CHIP_ERROR TCPEndPoint::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const +CHIP_ERROR TCPEndPointImplLwIP::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -290,7 +291,7 @@ CHIP_ERROR TCPEndPoint::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) con return res; } -CHIP_ERROR TCPEndPoint::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const +CHIP_ERROR TCPEndPointImplLwIP::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -320,7 +321,7 @@ CHIP_ERROR TCPEndPoint::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) co return res; } -CHIP_ERROR TCPEndPoint::GetInterfaceId(InterfaceId * retInterface) +CHIP_ERROR TCPEndPointImplLwIP::GetInterfaceId(InterfaceId * retInterface) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -331,7 +332,7 @@ CHIP_ERROR TCPEndPoint::GetInterfaceId(InterfaceId * retInterface) return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::SendQueuedImpl(bool queueWasEmpty) +CHIP_ERROR TCPEndPointImplLwIP::SendQueuedImpl(bool queueWasEmpty) { #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT if (!mUserTimeoutTimerRunning) @@ -344,7 +345,7 @@ CHIP_ERROR TCPEndPoint::SendQueuedImpl(bool queueWasEmpty) return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::EnableNoDelay() +CHIP_ERROR TCPEndPointImplLwIP::EnableNoDelay() { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -364,7 +365,7 @@ CHIP_ERROR TCPEndPoint::EnableNoDelay() return res; } -CHIP_ERROR TCPEndPoint::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) +CHIP_ERROR TCPEndPointImplLwIP::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); CHIP_ERROR res = CHIP_ERROR_NOT_IMPLEMENTED; @@ -402,7 +403,7 @@ CHIP_ERROR TCPEndPoint::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount return res; } -CHIP_ERROR TCPEndPoint::DisableKeepAlive() +CHIP_ERROR TCPEndPointImplLwIP::DisableKeepAlive() { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); CHIP_ERROR res = CHIP_ERROR_NOT_IMPLEMENTED; @@ -431,17 +432,12 @@ CHIP_ERROR TCPEndPoint::DisableKeepAlive() return res; } -CHIP_ERROR TCPEndPoint::SetUserTimeoutImpl(uint32_t userTimeoutMillis) +CHIP_ERROR TCPEndPointImplLwIP::SetUserTimeoutImpl(uint32_t userTimeoutMillis) { return CHIP_ERROR_NOT_IMPLEMENTED; } -void TCPEndPoint::InitImpl() -{ - mUnackedLength = 0; -} - -CHIP_ERROR TCPEndPoint::DriveSendingImpl() +CHIP_ERROR TCPEndPointImplLwIP::DriveSendingImpl() { CHIP_ERROR err = CHIP_NO_ERROR; @@ -462,7 +458,7 @@ CHIP_ERROR TCPEndPoint::DriveSendingImpl() { // Find first packet buffer with remaining data to send by skipping // all sent but un-acked data. - TCPEndPoint::BufferOffset startOfUnsent = FindStartOfUnsent(); + TCPEndPointImplLwIP::BufferOffset startOfUnsent = FindStartOfUnsent(); // While there's data to be sent and a window to send it in... do @@ -546,9 +542,9 @@ CHIP_ERROR TCPEndPoint::DriveSendingImpl() return err; } -void TCPEndPoint::HandleConnectCompleteImpl() {} +void TCPEndPointImplLwIP::HandleConnectCompleteImpl() {} -void TCPEndPoint::DoCloseImpl(CHIP_ERROR err, State oldState) +void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState) { // Lock LwIP stack LOCK_TCPIP_CORE(); @@ -621,7 +617,7 @@ void TCPEndPoint::DoCloseImpl(CHIP_ERROR err, State oldState) } } -CHIP_ERROR TCPEndPoint::AckReceive(uint16_t len) +CHIP_ERROR TCPEndPointImplLwIP::AckReceive(uint16_t len) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); CHIP_ERROR res = CHIP_NO_ERROR; @@ -641,23 +637,18 @@ CHIP_ERROR TCPEndPoint::AckReceive(uint16_t len) } #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT -void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState) +void TCPEndPointImplLwIP::TCPUserTimeoutHandler() { - TCPEndPoint * tcpEndPoint = reinterpret_cast(aAppState); - - VerifyOrDie((aSystemLayer != nullptr) && (tcpEndPoint != nullptr)); - // Set the timer running flag to false - tcpEndPoint->mUserTimeoutTimerRunning = false; + mUserTimeoutTimerRunning = false; // Close Connection as we have timed out and there is still // data not sent out successfully. - - tcpEndPoint->DoClose(INET_ERROR_TCP_USER_TIMEOUT, false); + DoClose(INET_ERROR_TCP_USER_TIMEOUT, false); } #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT -uint16_t TCPEndPoint::RemainingToSend() +uint16_t TCPEndPointImplLwIP::RemainingToSend() { if (mSendQueue.IsNull()) { @@ -673,7 +664,7 @@ uint16_t TCPEndPoint::RemainingToSend() } } -TCPEndPoint::BufferOffset TCPEndPoint::FindStartOfUnsent() +TCPEndPointImplLwIP::BufferOffset TCPEndPointImplLwIP::FindStartOfUnsent() { // Find first packet buffer with remaining data to send by skipping // all sent but un-acked data. This is necessary because of the Consume() @@ -685,7 +676,7 @@ TCPEndPoint::BufferOffset TCPEndPoint::FindStartOfUnsent() // unsent data while retaining the buffers that have un-acked data is to // traverse all sent-but-unacked data in the chain to reach the beginning // of ready-to-send data. - TCPEndPoint::BufferOffset startOfUnsent(mSendQueue.Retain()); + TCPEndPointImplLwIP::BufferOffset startOfUnsent(mSendQueue.Retain()); uint16_t leftToSkip = mUnackedLength; VerifyOrDie(leftToSkip < mSendQueue->TotalLength()); @@ -713,7 +704,7 @@ TCPEndPoint::BufferOffset TCPEndPoint::FindStartOfUnsent() return startOfUnsent; } -CHIP_ERROR TCPEndPoint::GetPCB(IPAddressType addrType) +CHIP_ERROR TCPEndPointImplLwIP::GetPCB(IPAddressType addrType) { // IMMPORTANT: This method MUST be called with the LwIP stack LOCKED! @@ -800,7 +791,7 @@ CHIP_ERROR TCPEndPoint::GetPCB(IPAddressType addrType) return CHIP_NO_ERROR; } -void TCPEndPoint::HandleDataSent(uint16_t lenSent) +void TCPEndPointImplLwIP::HandleDataSent(uint16_t lenSent) { if (IsConnected()) { @@ -867,7 +858,7 @@ void TCPEndPoint::HandleDataSent(uint16_t lenSent) } } -void TCPEndPoint::HandleDataReceived(System::PacketBufferHandle && buf) +void TCPEndPointImplLwIP::HandleDataReceived(System::PacketBufferHandle && buf) { // Only receive new data while in the Connected or SendShutdown states. if (mState == State::kConnected || mState == State::kSendShutdown) @@ -914,7 +905,7 @@ void TCPEndPoint::HandleDataReceived(System::PacketBufferHandle && buf) } } -void TCPEndPoint::HandleIncomingConnection(TCPEndPoint * conEP) +void TCPEndPointImplLwIP::HandleIncomingConnection(TCPEndPoint * conEP) { CHIP_ERROR err = CHIP_NO_ERROR; IPAddress peerAddr; @@ -946,7 +937,7 @@ void TCPEndPoint::HandleIncomingConnection(TCPEndPoint * conEP) conEP->Free(); } -void TCPEndPoint::HandleError(CHIP_ERROR err) +void TCPEndPointImplLwIP::HandleError(CHIP_ERROR err) { if (mState == State::kListening) { @@ -957,13 +948,13 @@ void TCPEndPoint::HandleError(CHIP_ERROR err) DoClose(err, false); } -err_t TCPEndPoint::LwIPHandleConnectComplete(void * arg, struct tcp_pcb * tpcb, err_t lwipErr) +err_t TCPEndPointImplLwIP::LwIPHandleConnectComplete(void * arg, struct tcp_pcb * tpcb, err_t lwipErr) { err_t res = ERR_OK; if (arg != NULL) { - TCPEndPoint * ep = static_cast(arg); + TCPEndPointImplLwIP * ep = static_cast(arg); System::Layer * lSystemLayer = ep->Layer().SystemLayer(); if (lwipErr == ERR_OK) @@ -994,15 +985,15 @@ err_t TCPEndPoint::LwIPHandleConnectComplete(void * arg, struct tcp_pcb * tpcb, return res; } -err_t TCPEndPoint::LwIPHandleIncomingConnection(void * arg, struct tcp_pcb * tpcb, err_t lwipErr) +err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_pcb * tpcb, err_t lwipErr) { CHIP_ERROR err = chip::System::MapErrorLwIP(lwipErr); if (arg != NULL) { - TCPEndPoint * listenEP = static_cast(arg); - TCPEndPoint * conEP = NULL; - System::Layer * lSystemLayer = listenEP->Layer().SystemLayer(); + TCPEndPointImplLwIP * listenEP = static_cast(arg); + TCPEndPointImplLwIP * conEP = NULL; + System::Layer * lSystemLayer = listenEP->Layer().SystemLayer(); // Tell LwIP we've accepted the connection so it can decrement the listen PCB's pending_accepts counter. tcp_accepted(listenEP->mTCP); @@ -1018,7 +1009,9 @@ err_t TCPEndPoint::LwIPHandleIncomingConnection(void * arg, struct tcp_pcb * tpc { InetLayer & lInetLayer = listenEP->Layer(); - err = lInetLayer.NewTCPEndPoint(&conEP); + TCPEndPoint * connectEndPoint = nullptr; + err = lInetLayer.NewTCPEndPoint(&connectEndPoint); + conEP = static_cast(connectEndPoint); } // Ensure that TCP timers have been started @@ -1092,13 +1085,13 @@ err_t TCPEndPoint::LwIPHandleIncomingConnection(void * arg, struct tcp_pcb * tpc } } -err_t TCPEndPoint::LwIPHandleDataReceived(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t _err) +err_t TCPEndPointImplLwIP::LwIPHandleDataReceived(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t _err) { err_t res = ERR_OK; if (arg != NULL) { - TCPEndPoint * ep = static_cast(arg); + TCPEndPointImplLwIP * ep = static_cast(arg); System::Layer * lSystemLayer = ep->Layer().SystemLayer(); // Post callback to HandleDataReceived. @@ -1128,13 +1121,13 @@ err_t TCPEndPoint::LwIPHandleDataReceived(void * arg, struct tcp_pcb * tpcb, str return res; } -err_t TCPEndPoint::LwIPHandleDataSent(void * arg, struct tcp_pcb * tpcb, u16_t len) +err_t TCPEndPointImplLwIP::LwIPHandleDataSent(void * arg, struct tcp_pcb * tpcb, u16_t len) { err_t res = ERR_OK; if (arg != NULL) { - TCPEndPoint * ep = static_cast(arg); + TCPEndPointImplLwIP * ep = static_cast(arg); System::Layer * lSystemLayer = ep->Layer().SystemLayer(); // Post callback to HandleDataReceived. @@ -1158,11 +1151,11 @@ err_t TCPEndPoint::LwIPHandleDataSent(void * arg, struct tcp_pcb * tpcb, u16_t l return res; } -void TCPEndPoint::LwIPHandleError(void * arg, err_t lwipErr) +void TCPEndPointImplLwIP::LwIPHandleError(void * arg, err_t lwipErr) { if (arg != NULL) { - TCPEndPoint * ep = static_cast(arg); + TCPEndPointImplLwIP * ep = static_cast(arg); System::LayerLwIP * lSystemLayer = static_cast(ep->Layer().SystemLayer()); // At this point LwIP has already freed the PCB. Since the thread that owns the TCPEndPoint may @@ -1188,7 +1181,9 @@ void TCPEndPoint::LwIPHandleError(void * arg, err_t lwipErr) #if CHIP_SYSTEM_CONFIG_USE_SOCKETS -CHIP_ERROR TCPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) +BitMapObjectPool TCPEndPointImplSockets::sPool; + +CHIP_ERROR TCPEndPointImplSockets::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) { CHIP_ERROR res = GetSocket(addrType); @@ -1280,7 +1275,7 @@ CHIP_ERROR TCPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, return res; } -CHIP_ERROR TCPEndPoint::ListenImpl(uint16_t backlog) +CHIP_ERROR TCPEndPointImplSockets::ListenImpl(uint16_t backlog) { if (listen(mSocket, backlog) != 0) { @@ -1302,7 +1297,7 @@ CHIP_ERROR TCPEndPoint::ListenImpl(uint16_t backlog) return res; } -CHIP_ERROR TCPEndPoint::ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) +CHIP_ERROR TCPEndPointImplSockets::ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) { IPAddressType addrType = addr.Type(); @@ -1429,17 +1424,18 @@ CHIP_ERROR TCPEndPoint::ConnectImpl(const IPAddress & addr, uint16_t port, Inter return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const +CHIP_ERROR TCPEndPointImplSockets::GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const { return GetSocketInfo(getpeername, retAddr, retPort); } -CHIP_ERROR TCPEndPoint::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const +CHIP_ERROR TCPEndPointImplSockets::GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const { return GetSocketInfo(getsockname, retAddr, retPort); } -CHIP_ERROR TCPEndPoint::GetSocketInfo(int getname(int, sockaddr *, socklen_t *), IPAddress * retAddr, uint16_t * retPort) const +CHIP_ERROR TCPEndPointImplSockets::GetSocketInfo(int getname(int, sockaddr *, socklen_t *), IPAddress * retAddr, + uint16_t * retPort) const { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1471,7 +1467,7 @@ CHIP_ERROR TCPEndPoint::GetSocketInfo(int getname(int, sockaddr *, socklen_t *), return CHIP_ERROR_INCORRECT_STATE; } -CHIP_ERROR TCPEndPoint::GetInterfaceId(InterfaceId * retInterface) +CHIP_ERROR TCPEndPointImplSockets::GetInterfaceId(InterfaceId * retInterface) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1511,7 +1507,7 @@ CHIP_ERROR TCPEndPoint::GetInterfaceId(InterfaceId * retInterface) return INET_ERROR_WRONG_ADDRESS_TYPE; } -CHIP_ERROR TCPEndPoint::SendQueuedImpl(bool queueWasEmpty) +CHIP_ERROR TCPEndPointImplSockets::SendQueuedImpl(bool queueWasEmpty) { if (queueWasEmpty) { @@ -1521,7 +1517,7 @@ CHIP_ERROR TCPEndPoint::SendQueuedImpl(bool queueWasEmpty) return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::EnableNoDelay() +CHIP_ERROR TCPEndPointImplSockets::EnableNoDelay() { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1537,7 +1533,7 @@ CHIP_ERROR TCPEndPoint::EnableNoDelay() return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) +CHIP_ERROR TCPEndPointImplSockets::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1572,7 +1568,7 @@ CHIP_ERROR TCPEndPoint::EnableKeepAlive(uint16_t interval, uint16_t timeoutCount return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::DisableKeepAlive() +CHIP_ERROR TCPEndPointImplSockets::DisableKeepAlive() { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1586,7 +1582,7 @@ CHIP_ERROR TCPEndPoint::DisableKeepAlive() return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::AckReceive(uint16_t len) +CHIP_ERROR TCPEndPointImplSockets::AckReceive(uint16_t len) { VerifyOrReturnError(IsConnected(), CHIP_ERROR_INCORRECT_STATE); @@ -1594,7 +1590,7 @@ CHIP_ERROR TCPEndPoint::AckReceive(uint16_t len) return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::SetUserTimeoutImpl(uint32_t userTimeoutMillis) +CHIP_ERROR TCPEndPointImplSockets::SetUserTimeoutImpl(uint32_t userTimeoutMillis) { #if defined(TCP_USER_TIMEOUT) // Set the user timeout @@ -1609,15 +1605,7 @@ CHIP_ERROR TCPEndPoint::SetUserTimeoutImpl(uint32_t userTimeoutMillis) #endif // defined(TCP_USER_TIMEOUT) } -void TCPEndPoint::InitImpl() -{ -#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT - mBytesWrittenSinceLastProbe = 0; - mLastTCPKernelSendQueueLen = 0; -#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT -} - -CHIP_ERROR TCPEndPoint::DriveSendingImpl() +CHIP_ERROR TCPEndPointImplSockets::DriveSendingImpl() { CHIP_ERROR err = CHIP_NO_ERROR; @@ -1738,7 +1726,7 @@ CHIP_ERROR TCPEndPoint::DriveSendingImpl() return err; } -void TCPEndPoint::HandleConnectCompleteImpl() +void TCPEndPointImplSockets::HandleConnectCompleteImpl() { // Wait for ability to read or write on this endpoint. CHIP_ERROR err = static_cast(Layer().SystemLayer())->RequestCallbackOnPendingRead(mWatch); @@ -1753,7 +1741,7 @@ void TCPEndPoint::HandleConnectCompleteImpl() } } -void TCPEndPoint::DoCloseImpl(CHIP_ERROR err, State oldState) +void TCPEndPointImplSockets::DoCloseImpl(CHIP_ERROR err, State oldState) { struct linger lingerStruct; @@ -1798,30 +1786,26 @@ void TCPEndPoint::DoCloseImpl(CHIP_ERROR err, State oldState) } #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT -void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState) +void TCPEndPointImplSockets::TCPUserTimeoutHandler() { - TCPEndPoint * tcpEndPoint = reinterpret_cast(aAppState); - - VerifyOrDie((aSystemLayer != nullptr) && (tcpEndPoint != nullptr)); - // Set the timer running flag to false - tcpEndPoint->mUserTimeoutTimerRunning = false; + mUserTimeoutTimerRunning = false; CHIP_ERROR err = CHIP_NO_ERROR; bool isProgressing = false; - err = tcpEndPoint->CheckConnectionProgress(isProgressing); + err = CheckConnectionProgress(isProgressing); SuccessOrExit(err); - if (tcpEndPoint->mLastTCPKernelSendQueueLen == 0) + if (mLastTCPKernelSendQueueLen == 0) { #if INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS // If the kernel TCP send queue as well as the TCPEndPoint // send queue have been flushed then notify application // that all data has been acknowledged. - if (tcpEndPoint->mSendQueue.IsNull()) + if (mSendQueue.IsNull()) { - tcpEndPoint->SetTCPSendIdleAndNotifyChange(true); + SetTCPSendIdleAndNotifyChange(true); } #endif // INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS } @@ -1834,7 +1818,7 @@ void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void // to shift it forward while also resetting the max // poll count. - tcpEndPoint->StartTCPUserTimeoutTimer(); + StartTCPUserTimeoutTimer(); } else { @@ -1842,13 +1826,13 @@ void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void // Data flow is not progressing. // Decrement the remaining max TCP send queue polls. - tcpEndPoint->mTCPSendQueueRemainingPollCount--; + mTCPSendQueueRemainingPollCount--; - VerifyOrExit(tcpEndPoint->mTCPSendQueueRemainingPollCount != 0, err = INET_ERROR_TCP_USER_TIMEOUT); + VerifyOrExit(mTCPSendQueueRemainingPollCount != 0, err = INET_ERROR_TCP_USER_TIMEOUT); // Restart timer to poll again - tcpEndPoint->ScheduleNextTCPUserTimeoutPoll(tcpEndPoint->mTCPSendQueuePollPeriodMillis); + ScheduleNextTCPUserTimeoutPoll(mTCPSendQueuePollPeriodMillis); #else // Close the connection as the TCP UserTimeout has expired @@ -1863,12 +1847,12 @@ void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void { // Close the connection as the TCP UserTimeout has expired - tcpEndPoint->DoClose(err, false); + DoClose(err, false); } } #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT -CHIP_ERROR TCPEndPoint::BindSrcAddrFromIntf(IPAddressType addrType, InterfaceId intfId) +CHIP_ERROR TCPEndPointImplSockets::BindSrcAddrFromIntf(IPAddressType addrType, InterfaceId intfId) { // If we are trying to make a TCP connection over a 'specified target interface', // then we bind the TCPEndPoint to an IP address on that target interface @@ -1922,7 +1906,7 @@ CHIP_ERROR TCPEndPoint::BindSrcAddrFromIntf(IPAddressType addrType, InterfaceId return CHIP_NO_ERROR; } -CHIP_ERROR TCPEndPoint::GetSocket(IPAddressType addrType) +CHIP_ERROR TCPEndPointImplSockets::GetSocket(IPAddressType addrType) { if (mSocket == kInvalidSocketFd) { @@ -1981,12 +1965,12 @@ CHIP_ERROR TCPEndPoint::GetSocket(IPAddressType addrType) } // static -void TCPEndPoint::HandlePendingIO(System::SocketEvents events, intptr_t data) +void TCPEndPointImplSockets::HandlePendingIO(System::SocketEvents events, intptr_t data) { - reinterpret_cast(data)->HandlePendingIO(events); + reinterpret_cast(data)->HandlePendingIO(events); } -void TCPEndPoint::HandlePendingIO(System::SocketEvents events) +void TCPEndPointImplSockets::HandlePendingIO(System::SocketEvents events) { // Prevent the end point from being freed while in the middle of a callback. Retain(); @@ -2048,7 +2032,7 @@ void TCPEndPoint::HandlePendingIO(System::SocketEvents events) Release(); } -void TCPEndPoint::ReceiveData() +void TCPEndPointImplSockets::ReceiveData() { System::PacketBufferHandle rcvBuf; bool isNewBuf = true; @@ -2192,10 +2176,10 @@ void TCPEndPoint::ReceiveData() DriveReceiving(); } -void TCPEndPoint::HandleIncomingConnection() +void TCPEndPointImplSockets::HandleIncomingConnection() { - CHIP_ERROR err = CHIP_NO_ERROR; - TCPEndPoint * conEP = nullptr; + CHIP_ERROR err = CHIP_NO_ERROR; + TCPEndPointImplSockets * conEP = nullptr; IPAddress peerAddr; uint16_t peerPort; @@ -2249,7 +2233,9 @@ void TCPEndPoint::HandleIncomingConnection() { InetLayer & lInetLayer = Layer(); - err = lInetLayer.NewTCPEndPoint(&conEP); + TCPEndPoint * connectEndPoint = nullptr; + err = lInetLayer.NewTCPEndPoint(&connectEndPoint); + conEP = static_cast(connectEndPoint); } // If all went well... @@ -2308,7 +2294,7 @@ void TCPEndPoint::HandleIncomingConnection() * This function probes the TCP output queue and checks if data is successfully * being transferred to the other end. */ -CHIP_ERROR TCPEndPoint::CheckConnectionProgress(bool & isProgressing) +CHIP_ERROR TCPEndPointImplSockets::CheckConnectionProgress(bool & isProgressing) { int currPendingBytesRaw = 0; uint32_t currPendingBytes; // Will be initialized once we know it's safe. @@ -2579,11 +2565,9 @@ void TCPEndPoint::StopConnectTimer() void TCPEndPoint::TCPConnectTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState) { TCPEndPoint * tcpEndPoint = reinterpret_cast(aAppState); - VerifyOrDie((aSystemLayer != nullptr) && (tcpEndPoint != nullptr)); - // Close Connection as we have timed out and Connect has not returned to - // stop this timer. + // Close Connection as we have timed out and Connect has not returned to stop this timer. tcpEndPoint->DoClose(INET_ERROR_TCP_CONNECT_TIMEOUT, false); } @@ -2789,6 +2773,14 @@ void TCPEndPoint::RestartTCPUserTimeoutTimer() StartTCPUserTimeoutTimer(); } +// static +void TCPEndPoint::TCPUserTimeoutHandler(chip::System::Layer * aSystemLayer, void * aAppState) +{ + TCPEndPoint * tcpEndPoint = reinterpret_cast(aAppState); + VerifyOrDie((aSystemLayer != nullptr) && (tcpEndPoint != nullptr)); + tcpEndPoint->TCPUserTimeoutHandler(); +} + #endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT } // namespace Inet diff --git a/src/inet/TCPEndPoint.h b/src/inet/TCPEndPoint.h index 41244c669dd5c2..a15b6cf7f7c1d5 100644 --- a/src/inet/TCPEndPoint.h +++ b/src/inet/TCPEndPoint.h @@ -37,6 +37,13 @@ #include +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS +#include +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS + #if CHIP_SYSTEM_CONFIG_USE_DISPATCH #include #endif @@ -67,15 +74,11 @@ class TCPEndPointDeletor * endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP * TCP protocol control blocks, as the system is configured accordingly. */ -class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted +class DLL_EXPORT TCPEndPoint : public EndPointBase, public ReferenceCounted { - friend class InetLayer; - friend class ::chip::Transport::TCPTest; - friend class TCPTest; - public: TCPEndPoint(InetLayer & inetLayer, void * appState = nullptr) : - EndPointBasis(inetLayer, appState), OnConnectComplete(nullptr), OnDataReceived(nullptr), OnDataSent(nullptr), + EndPointBase(inetLayer, appState), OnConnectComplete(nullptr), OnDataReceived(nullptr), OnDataSent(nullptr), OnConnectionClosed(nullptr), OnPeerClose(nullptr), OnConnectionReceived(nullptr), OnAcceptError(nullptr), mState(State::kReady), mReceiveEnabled(true), mConnectTimeoutMsecs(0) // Initialize to zero for using system defaults. #if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT @@ -89,9 +92,9 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted sPool; +protected: + friend class InetLayer; + friend class ::chip::Transport::TCPTest; + friend class TCPTest; /** * Basic dynamic state of the underlying endpoint. @@ -651,6 +655,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCountedDelete(); +} #if CHIP_SYSTEM_CONFIG_USE_LWIP + +class TCPEndPointImplLwIP : public TCPEndPoint, public EndPointStateLwIP +{ +public: + TCPEndPointImplLwIP(InetLayer & inetLayer, void * appState = nullptr) : TCPEndPoint(inetLayer, appState), mUnackedLength(0) {} + + // TCPEndPoint overrides. + CHIP_ERROR GetPeerInfo(IPAddress * retAddr, uint16_t * retPort) const override; + CHIP_ERROR GetLocalInfo(IPAddress * retAddr, uint16_t * retPort) const override; + CHIP_ERROR GetInterfaceId(InterfaceId * retInterface) override; + CHIP_ERROR EnableNoDelay() override; + CHIP_ERROR EnableKeepAlive(uint16_t interval, uint16_t timeoutCount) override; + CHIP_ERROR DisableKeepAlive() override; + CHIP_ERROR AckReceive(uint16_t len) override; +#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT + void TCPUserTimeoutHandler() override; +#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT + +private: + friend class InetLayer; + friend class BitMapObjectPool; + static BitMapObjectPool sPool; + void Delete() override { sPool.ReleaseObject(this); } + + // TCPEndPoint overrides. + CHIP_ERROR BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) override; + CHIP_ERROR ListenImpl(uint16_t backlog) override; + CHIP_ERROR ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) override; + CHIP_ERROR SendQueuedImpl(bool queueWasEmpty) override; + CHIP_ERROR SetUserTimeoutImpl(uint32_t userTimeoutMillis) override; + CHIP_ERROR DriveSendingImpl() override; + void HandleConnectCompleteImpl() override; + void DoCloseImpl(CHIP_ERROR err, State oldState) override; + struct BufferOffset { BufferOffset(System::PacketBufferHandle && aBuffer) : buffer(std::move(aBuffer)), offset(0) {} @@ -736,10 +776,53 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted; + static BitMapObjectPool sPool; + void Delete() override { sPool.ReleaseObject(this); } + + // TCPEndPoint overrides. + CHIP_ERROR BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, bool reuseAddr) override; + CHIP_ERROR ListenImpl(uint16_t backlog) override; + CHIP_ERROR ConnectImpl(const IPAddress & addr, uint16_t port, InterfaceId intfId) override; + CHIP_ERROR SendQueuedImpl(bool queueWasEmpty) override; + CHIP_ERROR SetUserTimeoutImpl(uint32_t userTimeoutMillis) override; + CHIP_ERROR DriveSendingImpl() override; + void HandleConnectCompleteImpl() override; + void DoCloseImpl(CHIP_ERROR err, State oldState) override; + CHIP_ERROR GetSocketInfo(int getname(int, sockaddr *, socklen_t *), IPAddress * retAddr, uint16_t * retPort) const; CHIP_ERROR GetSocket(IPAddressType addrType); void HandlePendingIO(System::SocketEvents events); @@ -752,13 +835,21 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted UDPEndPoint::sPool; - #if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS namespace { @@ -149,7 +147,10 @@ CHIP_ERROR CheckMulticastGroupArgs(InterfaceId aInterfaceId, const IPAddress & a #if CHIP_SYSTEM_CONFIG_USE_LWIP -CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) +BitMapObjectPool UDPEndPointImplLwIP::sPool; + +CHIP_ERROR UDPEndPointImplLwIP::BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, + InterfaceId interfaceId) { // Lock LwIP stack LOCK_TCPIP_CORE(); @@ -205,7 +206,7 @@ CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & ad return res; } -CHIP_ERROR UDPEndPoint::BindInterfaceImpl(IPAddressType addrType, InterfaceId intfId) +CHIP_ERROR UDPEndPointImplLwIP::BindInterfaceImpl(IPAddressType addrType, InterfaceId intfId) { // A lock is required because the LwIP thread may be referring to intf_filter, // while this code running in the Inet application is potentially modifying it. @@ -225,12 +226,12 @@ CHIP_ERROR UDPEndPoint::BindInterfaceImpl(IPAddressType addrType, InterfaceId in return err; } -CHIP_ERROR UDPEndPoint::LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId) +CHIP_ERROR UDPEndPointImplLwIP::LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId) { struct netif * netifp = nullptr; if (intfId.IsPresent()) { - netifp = UDPEndPoint::FindNetifFromInterfaceId(intfId); + netifp = UDPEndPointImplLwIP::FindNetifFromInterfaceId(intfId); if (netifp == nullptr) { return INET_ERROR_UNKNOWN_INTERFACE; @@ -241,7 +242,7 @@ CHIP_ERROR UDPEndPoint::LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId int return CHIP_NO_ERROR; } -InterfaceId UDPEndPoint::GetBoundInterface() const +InterfaceId UDPEndPointImplLwIP::GetBoundInterface() const { #if HAVE_LWIP_UDP_BIND_NETIF return InterfaceId(netif_get_by_index(mUDP->netif_idx)); @@ -250,12 +251,12 @@ InterfaceId UDPEndPoint::GetBoundInterface() const #endif } -uint16_t UDPEndPoint::GetBoundPort() const +uint16_t UDPEndPointImplLwIP::GetBoundPort() const { return mUDP->local_port; } -CHIP_ERROR UDPEndPoint::ListenImpl() +CHIP_ERROR UDPEndPointImplLwIP::ListenImpl() { // Lock LwIP stack LOCK_TCPIP_CORE(); @@ -279,7 +280,7 @@ CHIP_ERROR UDPEndPoint::ListenImpl() return CHIP_NO_ERROR; } -CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * pktInfo, System::PacketBufferHandle && msg) +CHIP_ERROR UDPEndPointImplLwIP::SendMsgImpl(const IPPacketInfo * pktInfo, System::PacketBufferHandle && msg) { const IPAddress & destAddr = pktInfo->DestAddress; @@ -406,7 +407,7 @@ CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * pktInfo, System::Packet return res; } -void UDPEndPoint::CloseImpl() +void UDPEndPointImplLwIP::CloseImpl() { // Lock LwIP stack @@ -425,13 +426,13 @@ void UDPEndPoint::CloseImpl() UNLOCK_TCPIP_CORE(); } -void UDPEndPoint::Free() +void UDPEndPointImplLwIP::Free() { Close(); Release(); } -void UDPEndPoint::HandleDataReceived(System::PacketBufferHandle && msg) +void UDPEndPointImplLwIP::HandleDataReceived(System::PacketBufferHandle && msg) { if ((mState == State::kListening) && (OnMessageReceived != nullptr)) { @@ -453,7 +454,7 @@ void UDPEndPoint::HandleDataReceived(System::PacketBufferHandle && msg) } } -CHIP_ERROR UDPEndPoint::GetPCB(IPAddressType addrType) +CHIP_ERROR UDPEndPointImplLwIP::GetPCB(IPAddressType addrType) { // IMPORTANT: This method MUST be called with the LwIP stack LOCKED! @@ -531,12 +532,13 @@ CHIP_ERROR UDPEndPoint::GetPCB(IPAddressType addrType) } #if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5 -void UDPEndPoint::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct pbuf * p, const ip_addr_t * addr, u16_t port) +void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct pbuf * p, const ip_addr_t * addr, + u16_t port) #else // LWIP_VERSION_MAJOR <= 1 && LWIP_VERSION_MINOR < 5 -void UDPEndPoint::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct pbuf * p, ip_addr_t * addr, u16_t port) +void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct pbuf * p, ip_addr_t * addr, u16_t port) #endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5 { - UDPEndPoint * ep = static_cast(arg); + UDPEndPointImplLwIP * ep = static_cast(arg); System::Layer * lSystemLayer = ep->Layer().SystemLayer(); IPPacketInfo * pktInfo = nullptr; System::PacketBufferHandle buf = System::PacketBufferHandle::Adopt(p); @@ -601,7 +603,7 @@ void UDPEndPoint::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct } } -CHIP_ERROR UDPEndPoint::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) +CHIP_ERROR UDPEndPointImplLwIP::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) { #if HAVE_LWIP_MULTICAST_LOOP if (mLwIPEndPointType == LwIPEndPointType::UDP) @@ -620,10 +622,8 @@ CHIP_ERROR UDPEndPoint::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopbac return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; } -void UDPEndPoint::InitImpl() {} - #if INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplLwIP::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) { #if LWIP_IPV4 && LWIP_IGMP const auto method = join ? igmp_joingroup_netif : igmp_leavegroup_netif; @@ -644,7 +644,7 @@ CHIP_ERROR UDPEndPoint::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId } #endif // INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplLwIP::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) { #ifdef HAVE_IPV6_MULTICAST const auto method = join ? mld6_joingroup_netif : mld6_leavegroup_netif; @@ -665,7 +665,7 @@ CHIP_ERROR UDPEndPoint::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId #endif // HAVE_IPV6_MULTICAST } -struct netif * UDPEndPoint::FindNetifFromInterfaceId(InterfaceId aInterfaceId) +struct netif * UDPEndPointImplLwIP::FindNetifFromInterfaceId(InterfaceId aInterfaceId) { struct netif * lRetval = nullptr; @@ -685,7 +685,7 @@ struct netif * UDPEndPoint::FindNetifFromInterfaceId(InterfaceId aInterfaceId) return (lRetval); } -IPPacketInfo * UDPEndPoint::GetPacketInfo(const System::PacketBufferHandle & aBuffer) +IPPacketInfo * UDPEndPointImplLwIP::GetPacketInfo(const System::PacketBufferHandle & aBuffer) { if (!aBuffer->EnsureReservedSize(sizeof(IPPacketInfo) + 3)) { @@ -703,6 +703,8 @@ IPPacketInfo * UDPEndPoint::GetPacketInfo(const System::PacketBufferHandle & aBu #if CHIP_SYSTEM_CONFIG_USE_SOCKETS +BitMapObjectPool UDPEndPointImplSockets::sPool; + namespace { CHIP_ERROR IPv6Bind(int socket, const IPAddress & address, uint16_t port, InterfaceId interface) @@ -784,11 +786,11 @@ CHIP_ERROR IPv4Bind(int socket, const IPAddress & address, uint16_t port) } // anonymous namespace #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API -UDPEndPoint::MulticastGroupHandler UDPEndPoint::sJoinMulticastGroupHandler; -UDPEndPoint::MulticastGroupHandler UDPEndPoint::sLeaveMulticastGroupHandler; +UDPEndPointImplSockets::MulticastGroupHandler UDPEndPointImplSockets::sJoinMulticastGroupHandler; +UDPEndPointImplSockets::MulticastGroupHandler UDPEndPointImplSockets::sLeaveMulticastGroupHandler; #endif // CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API -CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & addr, uint16_t port, InterfaceId interface) +CHIP_ERROR UDPEndPointImplSockets::BindImpl(IPAddressType addressType, const IPAddress & addr, uint16_t port, InterfaceId interface) { // Make sure we have the appropriate type of socket. ReturnErrorOnFailure(GetSocket(addressType)); @@ -849,7 +851,7 @@ CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & ad return CHIP_NO_ERROR; } -CHIP_ERROR UDPEndPoint::BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) +CHIP_ERROR UDPEndPointImplSockets::BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) { // Make sure we have the appropriate type of socket. ReturnErrorOnFailure(GetSocket(addressType)); @@ -890,17 +892,17 @@ CHIP_ERROR UDPEndPoint::BindInterfaceImpl(IPAddressType addressType, InterfaceId #endif // HAVE_SO_BINDTODEVICE } -InterfaceId UDPEndPoint::GetBoundInterface() const +InterfaceId UDPEndPointImplSockets::GetBoundInterface() const { return mBoundIntfId; } -uint16_t UDPEndPoint::GetBoundPort() const +uint16_t UDPEndPointImplSockets::GetBoundPort() const { return mBoundPort; } -CHIP_ERROR UDPEndPoint::ListenImpl() +CHIP_ERROR UDPEndPointImplSockets::ListenImpl() { // Wait for ability to read on this endpoint. auto * layer = static_cast(Layer().SystemLayer()); @@ -908,7 +910,7 @@ CHIP_ERROR UDPEndPoint::ListenImpl() return layer->RequestCallbackOnPendingRead(mWatch); } -CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * aPktInfo, System::PacketBufferHandle && msg) +CHIP_ERROR UDPEndPointImplSockets::SendMsgImpl(const IPPacketInfo * aPktInfo, System::PacketBufferHandle && msg) { // Make sure we have the appropriate type of socket based on the // destination address. @@ -1048,7 +1050,7 @@ CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * aPktInfo, System::Packe return CHIP_NO_ERROR; } -void UDPEndPoint::CloseImpl() +void UDPEndPointImplSockets::CloseImpl() { if (mSocket != kInvalidSocketFd) { @@ -1066,13 +1068,13 @@ void UDPEndPoint::CloseImpl() #endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH } -void UDPEndPoint::Free() +void UDPEndPointImplSockets::Free() { Close(); Release(); } -CHIP_ERROR UDPEndPoint::GetSocket(IPAddressType addressType) +CHIP_ERROR UDPEndPointImplSockets::GetSocket(IPAddressType addressType) { if (mSocket == kInvalidSocketFd) { @@ -1189,12 +1191,12 @@ CHIP_ERROR UDPEndPoint::GetSocket(IPAddressType addressType) } // static -void UDPEndPoint::HandlePendingIO(System::SocketEvents events, intptr_t data) +void UDPEndPointImplSockets::HandlePendingIO(System::SocketEvents events, intptr_t data) { - reinterpret_cast(data)->HandlePendingIO(events); + reinterpret_cast(data)->HandlePendingIO(events); } -void UDPEndPoint::HandlePendingIO(System::SocketEvents events) +void UDPEndPointImplSockets::HandlePendingIO(System::SocketEvents events) { if (mState != State::kListening || OnMessageReceived == nullptr || !events.Has(System::SocketEventFlags::kRead)) { @@ -1363,7 +1365,7 @@ static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, IPVersion aIPVersion, #endif // IPV6_MULTICAST_LOOP } -CHIP_ERROR UDPEndPoint::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) +CHIP_ERROR UDPEndPointImplSockets::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) { CHIP_ERROR lRetval = CHIP_ERROR_NOT_IMPLEMENTED; @@ -1374,14 +1376,9 @@ CHIP_ERROR UDPEndPoint::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopbac return (lRetval); } -void UDPEndPoint::InitImpl() -{ - mBoundIntfId = InterfaceId::Null(); -} - #if INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplSockets::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) { IPAddress lInterfaceAddress; bool lInterfaceAddressFound = false; @@ -1417,7 +1414,7 @@ CHIP_ERROR UDPEndPoint::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId #endif // INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplSockets::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) { #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API MulticastGroupHandler handler = join ? sJoinMulticastGroupHandler : sLeaveMulticastGroupHandler; @@ -1452,7 +1449,10 @@ CHIP_ERROR UDPEndPoint::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK -CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId intfId) +BitMapObjectPool UDPEndPointImplNetworkFramework::sPool; + +CHIP_ERROR UDPEndPointImplNetworkFramework::BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, + InterfaceId intfId) { nw_parameters_configure_protocol_block_t configure_tls; nw_parameters_t parameters; @@ -1493,28 +1493,28 @@ CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addressType, const IPAddress & ad return CHIP_NO_ERROR; } -CHIP_ERROR UDPEndPoint::BindInterfaceImpl(IPAddressType addrType, InterfaceId intfId) +CHIP_ERROR UDPEndPointImplNetworkFramework::BindInterfaceImpl(IPAddressType addrType, InterfaceId intfId) { return INET_ERROR_UNKNOWN_INTERFACE; } -InterfaceId UDPEndPoint::GetBoundInterface() const +InterfaceId UDPEndPointImplNetworkFramework::GetBoundInterface() const { return InterfaceId::Null(); } -uint16_t UDPEndPoint::GetBoundPort() const +uint16_t UDPEndPointImplNetworkFramework::GetBoundPort() const { nw_endpoint_t endpoint = nw_parameters_copy_local_endpoint(mParameters); return nw_endpoint_get_port(endpoint); } -CHIP_ERROR UDPEndPoint::ListenImpl() +CHIP_ERROR UDPEndPointImplNetworkFramework::ListenImpl() { return StartListener(); } -CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * pktInfo, System::PacketBufferHandle && msg) +CHIP_ERROR UDPEndPointImplNetworkFramework::SendMsgImpl(const IPPacketInfo * pktInfo, System::PacketBufferHandle && msg) { dispatch_data_t content; @@ -1554,12 +1554,12 @@ CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * pktInfo, System::Packet return res; } -void UDPEndPoint::CloseImpl() +void UDPEndPointImplNetworkFramework::CloseImpl() { ReleaseAll(); } -void UDPEndPoint::ReleaseAll() +void UDPEndPointImplNetworkFramework::ReleaseAll() { OnMessageReceived = nullptr; @@ -1607,30 +1607,32 @@ void UDPEndPoint::ReleaseAll() } } -void UDPEndPoint::Free() +void UDPEndPointImplNetworkFramework::Free() { Close(); Release(); } -CHIP_ERROR UDPEndPoint::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) +CHIP_ERROR UDPEndPointImplNetworkFramework::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) { return CHIP_ERROR_NOT_IMPLEMENTED; } #if INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplNetworkFramework::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, + bool join) { return CHIP_ERROR_NOT_IMPLEMENTED; } #endif // INET_CONFIG_ENABLE_IPV4 -CHIP_ERROR UDPEndPoint::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) +CHIP_ERROR UDPEndPointImplNetworkFramework::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, + bool join) { return CHIP_ERROR_NOT_IMPLEMENTED; } -CHIP_ERROR UDPEndPoint::ConfigureProtocol(IPAddressType aAddressType, const nw_parameters_t & aParameters) +CHIP_ERROR UDPEndPointImplNetworkFramework::ConfigureProtocol(IPAddressType aAddressType, const nw_parameters_t & aParameters) { CHIP_ERROR res = CHIP_NO_ERROR; @@ -1660,7 +1662,7 @@ CHIP_ERROR UDPEndPoint::ConfigureProtocol(IPAddressType aAddressType, const nw_p return res; } -void UDPEndPoint::GetPacketInfo(const nw_connection_t & aConnection, IPPacketInfo & aPacketInfo) +void UDPEndPointImplNetworkFramework::GetPacketInfo(const nw_connection_t & aConnection, IPPacketInfo & aPacketInfo) { nw_path_t path = nw_connection_copy_current_path(aConnection); nw_endpoint_t dest_endpoint = nw_path_copy_effective_local_endpoint(path); @@ -1673,8 +1675,8 @@ void UDPEndPoint::GetPacketInfo(const nw_connection_t & aConnection, IPPacketInf aPacketInfo.DestPort = nw_endpoint_get_port(dest_endpoint); } -CHIP_ERROR UDPEndPoint::GetEndPoint(nw_endpoint_t & aEndPoint, const IPAddressType aAddressType, const IPAddress & aAddress, - uint16_t aPort) +CHIP_ERROR UDPEndPointImplNetworkFramework::GetEndPoint(nw_endpoint_t & aEndPoint, const IPAddressType aAddressType, + const IPAddress & aAddress, uint16_t aPort) { char addrStr[INET6_ADDRSTRLEN]; char portStr[INET_PORTSTRLEN]; @@ -1699,7 +1701,7 @@ CHIP_ERROR UDPEndPoint::GetEndPoint(nw_endpoint_t & aEndPoint, const IPAddressTy return CHIP_NO_ERROR; } -CHIP_ERROR UDPEndPoint::GetConnection(const IPPacketInfo * aPktInfo) +CHIP_ERROR UDPEndPointImplNetworkFramework::GetConnection(const IPPacketInfo * aPktInfo) { VerifyOrReturnError(mParameters != nullptr, CHIP_ERROR_INCORRECT_STATE); @@ -1728,7 +1730,7 @@ CHIP_ERROR UDPEndPoint::GetConnection(const IPPacketInfo * aPktInfo) return StartConnection(connection); } -CHIP_ERROR UDPEndPoint::StartListener() +CHIP_ERROR UDPEndPointImplNetworkFramework::StartListener() { __block CHIP_ERROR res = CHIP_NO_ERROR; nw_listener_t listener; @@ -1801,7 +1803,7 @@ CHIP_ERROR UDPEndPoint::StartListener() return res; } -CHIP_ERROR UDPEndPoint::StartConnection(nw_connection_t & aConnection) +CHIP_ERROR UDPEndPointImplNetworkFramework::StartConnection(nw_connection_t & aConnection) { __block CHIP_ERROR res = CHIP_NO_ERROR; @@ -1861,7 +1863,7 @@ CHIP_ERROR UDPEndPoint::StartConnection(nw_connection_t & aConnection) return res; } -void UDPEndPoint::HandleDataReceived(const nw_connection_t & aConnection) +void UDPEndPointImplNetworkFramework::HandleDataReceived(const nw_connection_t & aConnection) { nw_connection_receive_completion_t handler = ^(dispatch_data_t content, nw_content_context_t context, bool is_complete, nw_error_t receive_error) { @@ -1909,7 +1911,7 @@ void UDPEndPoint::HandleDataReceived(const nw_connection_t & aConnection) nw_connection_receive_message(aConnection, handler); } -CHIP_ERROR UDPEndPoint::ReleaseListener() +CHIP_ERROR UDPEndPointImplNetworkFramework::ReleaseListener() { VerifyOrReturnError(mListener, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(mDispatchQueue, CHIP_ERROR_INCORRECT_STATE); @@ -1923,7 +1925,7 @@ CHIP_ERROR UDPEndPoint::ReleaseListener() return CHIP_NO_ERROR; } -CHIP_ERROR UDPEndPoint::ReleaseConnection() +CHIP_ERROR UDPEndPointImplNetworkFramework::ReleaseConnection() { VerifyOrReturnError(mConnection, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(mDispatchQueue, CHIP_ERROR_INCORRECT_STATE); diff --git a/src/inet/UDPEndPoint.h b/src/inet/UDPEndPoint.h index c8643b78d97501..97da7807b52a97 100644 --- a/src/inet/UDPEndPoint.h +++ b/src/inet/UDPEndPoint.h @@ -35,6 +35,16 @@ #include #include +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS +#include +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS +#if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK +#include +#endif // CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK + #if CHIP_SYSTEM_CONFIG_USE_DISPATCH #include #endif @@ -60,20 +70,20 @@ class UDPEndPointDeletor * endpoints (SOCK_DGRAM sockets on Linux and BSD-derived systems) or LwIP * UDP protocol control blocks, as the system is configured accordingly. */ -class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted +class DLL_EXPORT UDPEndPoint : public EndPointBase, public ReferenceCounted { public: UDPEndPoint(InetLayer & inetLayer, void * appState = nullptr) : - EndPointBasis(inetLayer, appState), mState(State::kReady), OnMessageReceived(nullptr), OnReceiveError(nullptr) - { - InitImpl(); - } + EndPointBase(inetLayer, appState), mState(State::kReady), OnMessageReceived(nullptr), OnReceiveError(nullptr) + {} UDPEndPoint(const UDPEndPoint &) = delete; UDPEndPoint(UDPEndPoint &&) = delete; UDPEndPoint & operator=(const UDPEndPoint &) = delete; UDPEndPoint & operator=(UDPEndPoint &&) = delete; + virtual ~UDPEndPoint() = default; + /** * Type of message text reception event handling function. * @@ -104,7 +114,7 @@ class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted sPool; + virtual void Delete() = 0; + + /* + * Implementation helpers for shared methods. + */ +#if INET_CONFIG_ENABLE_IPV4 + virtual CHIP_ERROR IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) = 0; +#endif // INET_CONFIG_ENABLE_IPV4 + virtual CHIP_ERROR IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) = 0; + + virtual CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) = 0; + virtual CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) = 0; + virtual CHIP_ERROR ListenImpl() = 0; + virtual CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg) = 0; + virtual void CloseImpl() = 0; +}; - CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId); - CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId); - CHIP_ERROR ListenImpl(); - CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg); - void CloseImpl(); +inline void UDPEndPointDeletor::Release(UDPEndPoint * obj) +{ + obj->Delete(); +} #if CHIP_SYSTEM_CONFIG_USE_LWIP +class UDPEndPointImplLwIP : public UDPEndPoint, public EndPointStateLwIP +{ +public: + UDPEndPointImplLwIP(InetLayer & inetLayer, void * appState = nullptr) : UDPEndPoint(inetLayer, appState) {} + + // UDPEndPoint overrides. + CHIP_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) override; + InterfaceId GetBoundInterface() const override; + uint16_t GetBoundPort() const override; + void Free() override; + +private: + friend class InetLayer; + friend class BitMapObjectPool; + static BitMapObjectPool sPool; + void Delete() override { sPool.ReleaseObject(this); } + + // UDPEndPoint overrides. +#if INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; +#endif // INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; + CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) override; + CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) override; + CHIP_ERROR ListenImpl() override; + CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg) override; + void CloseImpl() override; + static struct netif * FindNetifFromInterfaceId(InterfaceId aInterfaceId); static CHIP_ERROR LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId); @@ -335,14 +381,55 @@ class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted 1 || LWIP_VERSION_MINOR >= 5 +}; + +using UDPEndPointImpl = UDPEndPointImplLwIP; #endif // CHIP_SYSTEM_CONFIG_USE_LWIP #if CHIP_SYSTEM_CONFIG_USE_SOCKETS + +class UDPEndPointImplSockets : public UDPEndPoint, public EndPointStateSockets +{ +public: + UDPEndPointImplSockets(InetLayer & inetLayer, void * appState = nullptr) : + UDPEndPoint(inetLayer, appState), mBoundIntfId(InterfaceId::Null()) + {} + + // UDPEndPoint overrides. + CHIP_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) override; + InterfaceId GetBoundInterface() const override; + uint16_t GetBoundPort() const override; + void Free() override; + +private: + friend class InetLayer; + friend class BitMapObjectPool; + static BitMapObjectPool sPool; + void Delete() override { sPool.ReleaseObject(this); } + + // UDPEndPoint overrides. +#if INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; +#endif // INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; + CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) override; + CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) override; + CHIP_ERROR ListenImpl() override; + CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg) override; + void CloseImpl() override; + CHIP_ERROR GetSocket(IPAddressType addressType); void HandlePendingIO(System::SocketEvents events); static void HandlePendingIO(System::SocketEvents events, intptr_t data); + InterfaceId mBoundIntfId; + uint16_t mBoundPort; + +#if CHIP_SYSTEM_CONFIG_USE_DISPATCH + dispatch_source_t mReadableSource = nullptr; +#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH + #if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API public: using MulticastGroupHandler = CHIP_ERROR (*)(InterfaceId, const IPAddress &); @@ -353,16 +440,42 @@ class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted; + static BitMapObjectPool sPool; + void Delete() override { sPool.ReleaseObject(this); } + + // UDPEndPoint overrides. +#if INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; +#endif // INET_CONFIG_ENABLE_IPV4 + CHIP_ERROR IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join) override; + CHIP_ERROR BindImpl(IPAddressType addressType, const IPAddress & address, uint16_t port, InterfaceId interfaceId) override; + CHIP_ERROR BindInterfaceImpl(IPAddressType addressType, InterfaceId interfaceId) override; + CHIP_ERROR ListenImpl() override; + CHIP_ERROR SendMsgImpl(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle && msg) override; + void CloseImpl() override; + nw_listener_t mListener; dispatch_semaphore_t mListenerSemaphore; dispatch_queue_t mListenerQueue; @@ -381,13 +494,11 @@ class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted::DoInit(instance)); - UDPEndPoint::SetJoinMulticastGroupHandler([](InterfaceId, const IPAddress & address) { + UDPEndPointImplSockets::SetJoinMulticastGroupHandler([](InterfaceId, const IPAddress & address) { const otIp6Address otAddress = ToOpenThreadIP6Address(address); const auto otError = otIp6SubscribeMulticastAddress(openthread_get_default_instance(), &otAddress); return MapOpenThreadError(otError); }); - UDPEndPoint::SetLeaveMulticastGroupHandler([](InterfaceId, const IPAddress & address) { + UDPEndPointImplSockets::SetLeaveMulticastGroupHandler([](InterfaceId, const IPAddress & address) { const otIp6Address otAddress = ToOpenThreadIP6Address(address); const auto otError = otIp6UnsubscribeMulticastAddress(openthread_get_default_instance(), &otAddress); return MapOpenThreadError(otError); diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h index 9280e56a43bf0a..77a5bd3e727218 100644 --- a/src/system/SystemPacketBuffer.h +++ b/src/system/SystemPacketBuffer.h @@ -844,7 +844,7 @@ using PacketBufferWriter = PacketBufferWriterBase