From 9d3fd328693f197bef0dd7d1e321806f8660663b Mon Sep 17 00:00:00 2001 From: Dominika Maziec Date: Thu, 1 Aug 2019 16:40:04 +0200 Subject: [PATCH] TLSSocketWrapper::recvfrom sets SocketAddress output variable UNITTEST added --- .../test_TLSSocketWrapper.cpp | 1 + UNITTESTS/stubs/stoip4_stub.c | 46 ++++++++++++++++++- features/netsocket/TLSSocketWrapper.cpp | 3 ++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp b/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp index 201924e7949c..1d62f948d162 100644 --- a/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp +++ b/UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp @@ -307,6 +307,7 @@ TEST_F(TestTLSSocketWrapper, recv_from) EXPECT_EQ(wrapper->connect(a), NSAPI_ERROR_OK); SocketAddress b; EXPECT_EQ(wrapper->recvfrom(&b, dataBuf, dataSize), NSAPI_ERROR_OK); + EXPECT_EQ(a, b); } TEST_F(TestTLSSocketWrapper, recv_from_null) diff --git a/UNITTESTS/stubs/stoip4_stub.c b/UNITTESTS/stubs/stoip4_stub.c index 9cb803657774..156bedbec211 100644 --- a/UNITTESTS/stubs/stoip4_stub.c +++ b/UNITTESTS/stubs/stoip4_stub.c @@ -24,11 +24,55 @@ /** * Convert numeric IPv4 address string to a binary. * \param ip4addr IPv4 address in string format. - * \param len Length of IPv4 string, maximum of 16.. + * \param len Length of IPv4 string. * \param dest buffer for address. MUST be 4 bytes. * \return boolean set to true if conversion succeded, false if it didn't */ bool stoip4(const char *ip4addr, size_t len, void *dest) { + + if (ip4addr == NULL || dest == NULL || len <= 0) { + return true; + } + + uint8_t *addr = dest; + + uint_fast8_t stringLength = 0, byteCount = 0; + + //Iterate over each component of the IP. The exit condition is in the middle of the loop + while (true) { + + //No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z) + if (stringLength == len || ip4addr[stringLength] < '0' + || ip4addr[stringLength] > '9') { + return false; + } + + //For each component, we convert it to the raw value + uint_fast16_t byte = 0; + while (stringLength < len && ip4addr[stringLength] >= '0' + && ip4addr[stringLength] <= '9') { + byte *= 10; + byte += ip4addr[stringLength++] - '0'; + + //We go over the maximum value for an IPv4 component + if (byte > 0xff) { + return true; + } + } + + //Append the component + addr[byteCount++] = (uint8_t) byte; + + //If we're at the end, we leave the loop. It's the only way to reach the `true` output + if (byteCount == 4) { + break; + } + + //If the next character is invalid, we return false + if (stringLength == len || ip4addr[stringLength++] != '.') { + return true; + } + } return true; } diff --git a/features/netsocket/TLSSocketWrapper.cpp b/features/netsocket/TLSSocketWrapper.cpp index 605926477e22..68d24127b236 100644 --- a/features/netsocket/TLSSocketWrapper.cpp +++ b/features/netsocket/TLSSocketWrapper.cpp @@ -398,6 +398,9 @@ nsapi_size_or_error_t TLSSocketWrapper::recv(void *data, nsapi_size_t size) nsapi_size_or_error_t TLSSocketWrapper::recvfrom(SocketAddress *address, void *data, nsapi_size_t size) { + if (address) { + getpeername(address); + } return recv(data, size); }