Skip to content

Commit

Permalink
Include interface in PeerAddress::ToString (#11176)
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost authored and pull[bot] committed Dec 2, 2021
1 parent caaba17 commit 5243155
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/transport/raw/PeerAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <inet/IPAddress.h>
#include <inet/InetInterface.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/CHIPMemString.h>

namespace chip {
namespace Transport {
Expand Down Expand Up @@ -117,11 +118,15 @@ class PeerAddress

/// Maximum size of the string outputes by ToString. Format is of the form:
/// "UDP:<ip>:<port>"
static constexpr size_t kMaxToStringSize = //
3 /* UDP/TCP/BLE */ + 1 /* : */ //
+ kInetMaxAddrLen + 1 /* : */ //
+ 5 /* 16 bit interger */ //
+ 1 /* NullTerminator */;
static constexpr size_t kMaxToStringSize = 3 // type: UDP/TCP/BLE
+ 1 // splitter :
+ 2 // brackets around address
+ kInetMaxAddrLen // address
+ 1 // splitter %
+ Inet::InterfaceId::kMaxIfNameLength // interface
+ 1 // splitter :
+ 5 // port: 16 bit interger
+ 1; // NullTerminator

template <size_t N>
inline void ToString(char (&buf)[N]) const
Expand All @@ -133,18 +138,40 @@ class PeerAddress
{
char ip_addr[kInetMaxAddrLen];

char interface[Inet::InterfaceId::kMaxIfNameLength + 1] = {}; // +1 to prepend '%'
if (mInterface.IsPresent())
{
interface[0] = '%';
interface[1] = 0;
CHIP_ERROR err = mInterface.GetInterfaceName(interface + 1, sizeof(interface) - 1);
if (err != CHIP_NO_ERROR)
{
Platform::CopyString(interface, sizeof(interface), "%(err)");
}
}

switch (mTransportType)
{
case Type::kUndefined:
snprintf(buf, bufSize, "UNDEFINED");
break;
case Type::kUdp:
mIPAddress.ToString(ip_addr);
snprintf(buf, bufSize, "UDP:%s:%d", ip_addr, mPort);
#if INET_CONFIG_ENABLE_IPV4
if (mIPAddress.IsIPv4())
snprintf(buf, bufSize, "UDP:%s%s:%d", ip_addr, interface, mPort);
else
#endif
snprintf(buf, bufSize, "UDP:[%s%s]:%d", ip_addr, interface, mPort);
break;
case Type::kTcp:
mIPAddress.ToString(ip_addr);
snprintf(buf, bufSize, "TCP:%s:%d", ip_addr, mPort);
#if INET_CONFIG_ENABLE_IPV4
if (mIPAddress.IsIPv4())
snprintf(buf, bufSize, "TCP:%s%s:%d", ip_addr, interface, mPort);
else
#endif
snprintf(buf, bufSize, "TCP:[%s%s]:%d", ip_addr, interface, mPort);
break;
case Type::kBle:
// Note that BLE does not currently use any specific address.
Expand Down

0 comments on commit 5243155

Please sign in to comment.