diff --git a/src/lib/message/BUILD.gn b/src/lib/message/BUILD.gn index cfbdd91b2c9c3b..be2b7ef9c10854 100644 --- a/src/lib/message/BUILD.gn +++ b/src/lib/message/BUILD.gn @@ -41,8 +41,6 @@ static_library("message") { "CHIPServerBase.cpp", "CHIPServerBase.h", "ExchangeContext.cpp", - "HostPortList.cpp", - "HostPortList.h", ] public_deps = [ diff --git a/src/lib/message/CHIPConnection.cpp b/src/lib/message/CHIPConnection.cpp index 57218a06c0ec57..f5b5e8fced465f 100644 --- a/src/lib/message/CHIPConnection.cpp +++ b/src/lib/message/CHIPConnection.cpp @@ -424,106 +424,6 @@ CHIP_ERROR ChipConnection::Connect(uint64_t peerNodeId, ChipAuthMode authMode, c return err; } -/** - * Connect to a CHIP node using a node identifier and/or a list of hostname and ports. - * - * @param[in] peerNodeId The node identifier of the peer. - * - * @param[in] authMode The authentication mode used for the connection. - * - * @param[in] hostPortList The list of hostnames and ports. - * - * @param[in] intf The optional interface to use to connect to the peer node, - * default to #INET_NULL_INTERFACEID. - * - * @retval #CHIP_NO_ERROR on successful initiation of the connection to the peer. - * @retval #CHIP_ERROR_INCORRECT_STATE if the ChipConnection state is incorrect. - * - * @retval #CHIP_ERROR_UNSUPPORTED_AUTH_MODE if the requested authentication mode is not supported. - * - * @retval #CHIP_ERROR_INVALID_ADDRESS if the destination address cannot be deduced from the node id. - * - * @retval other Inet layer errors generated by the TCPEndPoint connect operations. - * - */ -CHIP_ERROR ChipConnection::Connect(uint64_t peerNodeId, ChipAuthMode authMode, HostPortList hostPortList, InterfaceId intf) -{ -#if CHIP_CONFIG_ENABLE_DNS_RESOLVER - const uint8_t dnsOptions = ::chip::Inet::kDNSOption_Default; -#else - const uint8_t dnsOptions = 0; -#endif - return Connect(peerNodeId, authMode, hostPortList, dnsOptions, intf); -} - -/** - * Connect to a CHIP node using a node identifier and/or a list of hostname and ports. - * - * @param[in] peerNodeId The node identifier of the peer. - * - * @param[in] authMode The authentication mode used for the connection. - * - * @param[in] hostPortList The list of hostnames and ports. - * - * @param[in] dnsOptions An integer value controlling how host name resolution is performed. - * Value should be the OR of one or more values from from the - * #::chip::Inet::DNSOptions enumeration. - * - * @param[in] intf The optional interface to use to connect to the peer node, - * default to #INET_NULL_INTERFACEID. - * - * @retval #CHIP_NO_ERROR on successful initiation of the connection to the peer. - * @retval #CHIP_ERROR_INCORRECT_STATE if the ChipConnection state is incorrect. - * - * @retval #CHIP_ERROR_UNSUPPORTED_AUTH_MODE if the requested authentication mode is not supported. - * - * @retval #CHIP_ERROR_INVALID_ADDRESS if the destination address cannot be deduced from the node id. - * - * @retval other Inet layer errors generated by the TCPEndPoint connect operations. - * - */ -CHIP_ERROR ChipConnection::Connect(uint64_t peerNodeId, ChipAuthMode authMode, HostPortList hostPortList, uint8_t dnsOptions, - InterfaceId intf) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - - VerifyOrExit(State == kState_ReadyToConnect, err = CHIP_ERROR_INCORRECT_STATE); - - VerifyOrExit(authMode == kChipAuthMode_Unauthenticated || IsCASEAuthMode(authMode) || IsPASEAuthMode(authMode), - err = CHIP_ERROR_INVALID_ARGUMENT); - - // Can't request authentication if the security manager is not initialized. - VerifyOrExit(authMode == kChipAuthMode_Unauthenticated || MessageLayer->SecurityMgr != NULL, - err = CHIP_ERROR_UNSUPPORTED_AUTH_MODE); - - // Application has made this an IP-based ChipConnection. - NetworkType = kNetworkType_IP; - - // Clear the list of resolved peer addresses in preparation for resolving the first host name. - memset(mPeerAddrs, 0, sizeof(mPeerAddrs)); - - PeerNodeId = peerNodeId; - AuthMode = authMode; - mPeerHostPortList = hostPortList; - mTargetInterface = intf; -#if CHIP_CONFIG_ENABLE_DNS_RESOLVER - mDNSOptions = dnsOptions; -#endif - - // Bump the reference count when we start the connection process. The corresponding decrement happens when the - // DoClose() method is called. This ensures the object stays live while there's the possibility of a callback - // happening from an underlying layer (e.g. TCPEndPoint or DNS resolution). - mRefCount++; - - ChipLogProgress(MessageLayer, "Con start %04X %016llX %04X", LogId(), peerNodeId, authMode); - - err = TryNextPeerAddress(CHIP_ERROR_HOST_PORT_LIST_EMPTY); - SuccessOrExit(err); - -exit: - return err; -} - /** * @brief Set timeout for Connect to succeed or return an error. * @@ -1133,6 +1033,7 @@ CHIP_ERROR ChipConnection::TryNextPeerAddress(CHIP_ERROR lastErr) // Search the list of peer addresses for one we haven't tried yet... for (int i = 0; i < CHIP_CONFIG_CONNECT_IP_ADDRS; i++) + { if (mPeerAddrs[i] != IPAddress::Any) { // Select the next address, removing it from the list so it won't get tried again. @@ -1143,32 +1044,6 @@ CHIP_ERROR ChipConnection::TryNextPeerAddress(CHIP_ERROR lastErr) err = StartConnect(); ExitNow(); } - - // If Connect() was called with a host/port list and there are additional entries in the list, then... - if (!mPeerHostPortList.IsEmpty()) - { - char hostName[256]; // Per spec, max DNS name length is 253. - - // Pop the next host/port pair from the list. - err = mPeerHostPortList.Pop(hostName, sizeof(hostName), PeerPort); - SuccessOrExit(err); - -#if CHIP_CONFIG_ENABLE_DNS_RESOLVER - // Initiate name resolution for the new host name. - // - // NOTE: there was a moment, maybe it's passed now, when the - // log statement below was very useful since in showed exactly - // which host we thought we were getting a particular service - // from. for now i'm leaving it in place, protected by an - // ifdef 0 at the top of the file. - // - ChipLogProgress(MessageLayer, "Con DNS start %04" PRIX16 " %s %02" PRIX8, LogId(), hostName, mDNSOptions); - State = kState_Resolving; - err = MessageLayer->Inet->ResolveHostAddress(hostName, strlen(hostName), mDNSOptions, CHIP_CONFIG_CONNECT_IP_ADDRS, - mPeerAddrs, HandleResolveComplete, this); -#else // !CHIP_CONFIG_ENABLE_DNS_RESOLVER - err = StartConnectToAddressLiteral(hostName, strlen(hostName)); -#endif // !CHIP_CONFIG_ENABLE_DNS_RESOLVER } exit: @@ -1579,7 +1454,6 @@ void ChipConnection::Init(ChipMessageLayer * msgLayer) #if CONFIG_NETWORK_LAYER_BLE mBleEndPoint = NULL; #endif - mPeerHostPortList.Clear(); mTargetInterface = INET_NULL_INTERFACEID; mRefCount = 1; SendSourceNodeId = false; diff --git a/src/lib/message/CHIPMessageLayer.h b/src/lib/message/CHIPMessageLayer.h index 914997f36ca41c..272526cd518f86 100644 --- a/src/lib/message/CHIPMessageLayer.h +++ b/src/lib/message/CHIPMessageLayer.h @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -254,10 +253,6 @@ class ChipConnection uint16_t defaultPort = 0); CHIP_ERROR Connect(uint64_t peerNodeId, ChipAuthMode authMode, const char * peerAddr, uint16_t peerAddrLen, uint8_t dnsOptions, uint16_t defaultPort); - CHIP_ERROR Connect(uint64_t peerNodeId, ChipAuthMode authMode, HostPortList hostPortList, - InterfaceId intf = INET_NULL_INTERFACEID); - CHIP_ERROR Connect(uint64_t peerNodeId, ChipAuthMode authMode, HostPortList hostPortList, uint8_t dnsOptions, InterfaceId intf); - CHIP_ERROR GetPeerAddressInfo(IPPacketInfo & addrInfo); enum @@ -355,7 +350,6 @@ class ChipConnection IPAddress mPeerAddrs[CHIP_CONFIG_CONNECT_IP_ADDRS]; TCPEndPoint * mTcpEndPoint; - HostPortList mPeerHostPortList; InterfaceId mTargetInterface; uint32_t mConnectTimeout; uint8_t mRefCount; diff --git a/src/lib/message/HostPortList.cpp b/src/lib/message/HostPortList.cpp deleted file mode 100644 index 074942168053f5..00000000000000 --- a/src/lib/message/HostPortList.cpp +++ /dev/null @@ -1,288 +0,0 @@ -/* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2013-2017 Nest Labs, Inc. - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file implements a class for managing and manipulating a Host - * Port List, a compact, binary-encoded collection of host and port - * identifier tuples associated with the CHIP Service Directory. - * - */ - -#include "HostPortList.h" - -#include -#include - -namespace chip { - -using namespace chip::Encoding; - -/** - * Class default (void) constructor. - * - */ -HostPortList::HostPortList(void) -{ - Clear(); -} - -/** - * Reset the list to empty. - * - */ -void HostPortList::Clear(void) -{ - mElements = mSuffixTable = NULL; - mCount = mSuffixCount = 0; -} - -/** - * Get the host name and port at the specified index from the list. - * - * @param[in] index The position in the list to return the host - * name and port. - * - * @param[inout] hostBuf A pointer to storage into which to copy the - * host name. - * - * @param[in] hostBufSize The amount of storage available in hostBuf. - * - * @param[out] port The port number. - * - * @retval #CHIP_ERROR_INVALID_ARGUMENT The requested index - * exceeds the size of - * the list. - * @retval #CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT The Host Port element - * requested was an - * unsupported type. - * @retval #CHIP_ERROR_BUFFER_TOO_SMALL The Host Port host - * name length - * is longer - * than the - * provided - * buffer. - * @retval #CHIP_NO_ERROR On success. - */ -CHIP_ERROR HostPortList::Get(uint8_t index, char * hostBuf, uint32_t hostBufSize, uint16_t & port) const -{ - CHIP_ERROR err = CHIP_NO_ERROR; - const uint8_t * elem; - - VerifyOrExit(index < mCount, err = CHIP_ERROR_INVALID_ARGUMENT); - - elem = mElements; - for (; index > 0; index--) - { - err = Skip(elem); - SuccessOrExit(err); - } - - err = Get(elem, hostBuf, hostBufSize, port); - -exit: - return err; -} - -/** - * Get and remove the first host name and port from the list. - * - * @param[inout] hostBuf A pointer to storage into which to copy the - * host name. - * - * @param[in] hostBufSize The amount of storage available in hostBuf. - * - * @param[out] port The port number. - * - * @retval #CHIP_ERROR_HOST_PORT_LIST_EMPTY There are no entries - * in the list. - * @retval #CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT The Host Port element - * encountered was an - * unsupported type. - * @retval #CHIP_ERROR_BUFFER_TOO_SMALL The Host Port host - * name length - * is longer - * than the - * provided - * buffer. - * @retval #CHIP_NO_ERROR On success. - */ -CHIP_ERROR HostPortList::Pop(char * hostBuf, uint32_t hostBufSize, uint16_t & port) -{ - CHIP_ERROR err = CHIP_NO_ERROR; - - VerifyOrExit(mCount > 0, err = CHIP_ERROR_HOST_PORT_LIST_EMPTY); - - err = Get(mElements, hostBuf, hostBufSize, port); - if (err == CHIP_NO_ERROR) - mCount--; - -exit: - return err; -} - -/** - * Get the host name and port at the specified element. - * - * @param[inout] elem The host name and port element to get. The - * pointer will be advanced to the next - * element on success and may be left in - * an indeterminate state on failure. - * - * @param[inout] hostBuf A pointer to storage into which to copy the - * host name. - * - * @param[in] hostBufSize The amount of storage available in hostBuf. - * - * @param[out] port The port number. - * - * @retval #CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT The Host Port element - * encountered was an - * unsupported type. - * @retval #CHIP_ERROR_BUFFER_TOO_SMALL The Host Port host - * name length - * is longer - * than the - * provided - * buffer. - * @retval #CHIP_NO_ERROR On success. - */ -CHIP_ERROR HostPortList::Get(const uint8_t *& elem, char * hostBuf, uint32_t hostBufSize, uint16_t & port) const -{ - CHIP_ERROR err = CHIP_NO_ERROR; - uint8_t control, type, hostLen; - - control = *elem++; - type = control & kHostPortControl_TypeMask; - VerifyOrExit(type == kHostPortType_FullyQualified || type == kHostPortType_Indexed, - err = CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT); - - hostLen = *elem++; - VerifyOrExit(hostLen < hostBufSize, err = CHIP_ERROR_BUFFER_TOO_SMALL); - - memcpy(hostBuf, elem, hostLen); - elem += hostLen; - - if ((control & kHostPortControl_HasSuffixIndex) != 0) - { - if (type == kHostPortType_Indexed) - { - uint8_t suffixLen; - err = GetSuffix(*elem, hostBuf + hostLen, hostBufSize - hostLen, suffixLen); - SuccessOrExit(err); - - hostLen += suffixLen; - } - - elem++; - } - - hostBuf[hostLen] = 0; - - if ((control & kHostPortControl_HasPort) != 0) - port = LittleEndian::Read16(elem); - else - port = CHIP_PORT; - -exit: - return err; -} - -/** - * Skip the host name and port at the specified element. - * - * @param[inout] elem The host name and port element to skip. The pointer - * will be advanced to the next element on success and - * may be left in an indeterminate state on failure. - * - * @retval #CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT The Host Port element - * encountered was an - * unsupported type. - * @retval #CHIP_NO_ERROR On success. - */ -CHIP_ERROR HostPortList::Skip(const uint8_t *& elem) const -{ - CHIP_ERROR err = CHIP_NO_ERROR; - uint8_t control, type, hostLen; - - control = *elem++; - type = control & kHostPortControl_TypeMask; - VerifyOrExit(type == kHostPortType_FullyQualified || type == kHostPortType_Indexed, - err = CHIP_ERROR_UNSUPPORTED_HOST_PORT_ELEMENT); - - hostLen = *elem++; - elem += hostLen; - - if ((control & kHostPortControl_HasSuffixIndex) != 0) - elem++; - - if ((control & kHostPortControl_HasPort) != 0) - elem += 2; - -exit: - return err; -} - -/** - * Get the Host Port element host name suffix (e.g. ".acme.com") at the - * specified position in the list. - * - * @param[in] index The position in the list to return the host - * name suffix. - * - * @param[inout] buf A pointer to storage into which to copy the - * host name suffix. - * - * @param[in] bufSize The amount of storage available in buf. - * - * @param[out] suffixLen The length, in characters, of the host name - * suffix. - * - * @retval #CHIP_ERROR_INVALID_HOST_SUFFIX_INDEX The requested index - * exceeds the size of the - * list. - * @retval #CHIP_ERROR_BUFFER_TOO_SMALL The Host Port host - * suffix name length is - * longer than the provided - * buffer. - * @retval #CHIP_NO_ERROR On success. - * - */ -CHIP_ERROR HostPortList::GetSuffix(uint8_t index, char * buf, uint32_t bufSize, uint8_t & suffixLen) const -{ - CHIP_ERROR err = CHIP_NO_ERROR; - const uint8_t * elem; - - VerifyOrExit(index < mSuffixCount, err = CHIP_ERROR_INVALID_HOST_SUFFIX_INDEX); - - elem = mSuffixTable; - for (; index > 0; index--) - elem += 1 + elem[0]; - - suffixLen = *elem++; - VerifyOrExit(suffixLen < bufSize, err = CHIP_ERROR_BUFFER_TOO_SMALL); - - memcpy(buf, elem, suffixLen); - buf[suffixLen] = 0; - -exit: - return err; -} - -} /* namespace chip */ diff --git a/src/lib/message/HostPortList.h b/src/lib/message/HostPortList.h deleted file mode 100644 index b4552555bce049..00000000000000 --- a/src/lib/message/HostPortList.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2013-2017 Nest Labs, Inc. - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file defines a class for managing and manipulating a Host - * Port List, a compact, binary-encoded collection of host and port - * identifier tuples associated with the CHIP Service Directory. - * - */ - -#ifndef HOSTPORTLIST_H_ -#define HOSTPORTLIST_H_ - -#include - -#include - -namespace chip { - -/** - * @class HostPortList - * - * @brief - * For managing and manipulating a Host Port List, a compact, - * binary-encoded collection of host and port identifier tuples - * associated with the CHIP Service Directory. - * - */ -class HostPortList -{ -public: - HostPortList(void); - inline HostPortList(const uint8_t * hostPortList, uint8_t hostPortCount, const uint8_t * suffixList, const uint8_t suffixCount) - { - mElements = hostPortList; - mCount = hostPortCount; - mSuffixTable = suffixList; - mSuffixCount = suffixCount; - } - - CHIP_ERROR Get(uint8_t index, char * hostBuf, uint32_t hostBufSize, uint16_t & port) const; - CHIP_ERROR Pop(char * hostBuf, uint32_t hostBufSize, uint16_t & port); - - void Clear(void); - - inline bool IsEmpty(void) const { return (mCount == 0); } - -private: - enum - { - kHostPortControl_TypeMask = 0x03, ///< Mask for the type of entry flags. - kHostPortControl_HasSuffixIndex = 0x04, ///< Flag indicating whether the host port tuple has an associated suffix index. - kHostPortControl_HasPort = 0x08, ///< Flag indicating whether the host port tuple has a port number. - - kHostPortType_FullyQualified = 0x00, ///< Flag indicating whether the host name is fully-qualified. - kHostPortType_Indexed = 0x01, ///< Flag indicating whether the host name is index (i.e. has a suffix). - }; - - CHIP_ERROR Get(const uint8_t *& elem, char * hostBuf, uint32_t hostBufSize, uint16_t & port) const; - CHIP_ERROR Skip(const uint8_t *& elem) const; - CHIP_ERROR GetSuffix(uint8_t index, char * buf, uint32_t bufSize, uint8_t & suffixLen) const; - -private: - const uint8_t * mElements; ///< A read-only pointer to the host and port tuples list. - const uint8_t * mSuffixTable; ///< A read-only pointer to the host suffix list. - uint8_t mCount; ///< The number of host and port tuples in the list. - uint8_t mSuffixCount; ///< The number of host suffixes in the list. -}; - -} /* namespace chip */ - -#endif /* HOSTPORTLIST_H_ */