Skip to content

Commit

Permalink
TLSSocketWrapper::recvfrom sets SocketAddress output variable
Browse files Browse the repository at this point in the history
UNITTEST added
  • Loading branch information
Dominika Maziec authored and Dominika Maziec committed Aug 6, 2019
1 parent 46a56fb commit 9d3fd32
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 45 additions & 1 deletion UNITTESTS/stubs/stoip4_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions features/netsocket/TLSSocketWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 9d3fd32

Please sign in to comment.