Skip to content

Commit

Permalink
Fix format for peer addresses, update code for nRF
Browse files Browse the repository at this point in the history
  • Loading branch information
andreilitvin committed Jun 22, 2020
1 parent 9835892 commit d382f8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions examples/lock-app/nrf5/main/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ static void OnRecv(const MessageHeader & header, const IPPacketInfo & packet_inf
SecureSessionMgr * transport)
{
const size_t data_len = buffer->DataLength();
char src_addr[128];
char src_addr[Transport::PeerAddress::kMaxToStringSize];

// as soon as a client connects, assume it is connected
VerifyOrExit(transport != NULL && buffer != NULL, NRF_LOG_INFO("Received data but couldn't process it..."));
VerifyOrExit(header.GetSourceNodeId().HasValue(), NRF_LOG_INFO("Unknown source for received message"));

VerifyOrExit(state->GetPeerNodeId() != kUndefinedNodeId, ESP_LOGE(TAG, "Unknown source for received message"));
VerifyOrExit(state->GetPeerNodeId() != kUndefinedNodeId, NRF_LOG_INFO("Unknown source for received message"));

state->GetPeerAddress().ToString(src_addr, sizeof(src_addr));

NRF_LOG_INFO("Packet received from %s (%zu bytes)", src_addr, static_cast<size_t>(data_len));
NRF_LOG_INFO("Packet received from %s: %zu bytes", src_addr, static_cast<size_t>(data_len));

HandleDataModelMessage(buffer);
buffer = NULL;
Expand Down
2 changes: 1 addition & 1 deletion examples/wifi-echo/server/esp32/main/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void echo(const MessageHeader & header, Transport::PeerConnectionState * state,
VerifyOrExit(state->GetPeerNodeId() != kUndefinedNodeId, ESP_LOGE(TAG, "Unknown source for received message"));

{
char src_addr[128];
char src_addr[Transport::PeerAddress::kMaxToStringSize];

state->GetPeerAddress().ToString(src_addr, sizeof(src_addr));

Expand Down
15 changes: 15 additions & 0 deletions src/transport/PeerAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ class PeerAddress
return (mTransportType == other.mTransportType) && (mIPAddress == other.mIPAddress) && (mPort == other.mPort);
}

/// Maximum size of an Inet address ToString format, that can hold both IPV6 and IPV4 addresses.
#ifdef INET6_ADDRSTRLEN
static constexpr size_t kInetMaxAddrLen = INET6_ADDRSTRLEN;
#else
static constexpr size_t kInetMaxAddrLen = INET_ADDRSTRLEN;
#endif

/// 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 */;

void ToString(char * buf, size_t bufSize)
{
char ip_addr[INET_ADDRSTRLEN];
Expand Down

0 comments on commit d382f8f

Please sign in to comment.