diff --git a/features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp b/features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp index 76e02aa12b7..9ed53f03cf4 100644 --- a/features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp +++ b/features/cellular/UNITTESTS/stubs/SocketAddress_stub.cpp @@ -22,40 +22,16 @@ #include "mbed.h" -static bool ipv4_is_valid(const char *addr) -{ - return false; -} - static bool ipv6_is_valid(const char *addr) { return false; } -static void ipv4_from_address(uint8_t *bytes, const char *addr) -{ - -} - static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk) { return 0; } -static void ipv6_from_address(uint8_t *bytes, const char *addr) -{ - -} - -static void ipv4_to_address(char *addr, const uint8_t *bytes) -{ - -} - -static void ipv6_to_address(char *addr, const uint8_t *bytes) -{ -} - SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port) { diff --git a/features/netsocket/SocketAddress.cpp b/features/netsocket/SocketAddress.cpp index b83c690fca0..3e42619a5e7 100644 --- a/features/netsocket/SocketAddress.cpp +++ b/features/netsocket/SocketAddress.cpp @@ -19,29 +19,11 @@ #include "NetworkStack.h" #include #include +#include "ip4string.h" #include "ip6string.h" -static bool ipv4_is_valid(const char *addr) -{ - int i = 0; - - // Check each digit for [0-9.] - for (; addr[i]; i++) { - if (!(addr[i] >= '0' && addr[i] <= '9') && addr[i] != '.') { - return false; - } - } - - // Ending with '.' garuntees host - if (i > 0 && addr[i - 1] == '.') { - return false; - } - - return true; -} - static bool ipv6_is_valid(const char *addr) { // Check each digit for [0-9a-fA-F:] @@ -62,48 +44,6 @@ static bool ipv6_is_valid(const char *addr) return colons >= 2; } -static void ipv4_from_address(uint8_t *bytes, const char *addr) -{ - int count = 0; - int i = 0; - - for (; count < NSAPI_IPv4_BYTES; count++) { - unsigned d; - // Not using %hh, since it might be missing in newlib-based toolchains. - // See also: https://git.io/vxiw5 - int scanned = sscanf(&addr[i], "%u", &d); - if (scanned < 1) { - return; - } - - bytes[count] = static_cast(d); - - for (; addr[i] != '.'; i++) { - if (!addr[i]) { - return; - } - } - - i++; - } -} - -static void ipv6_from_address(uint8_t *bytes, const char *addr) -{ - stoip6(addr, strlen(addr), bytes); -} - -static void ipv4_to_address(char *addr, const uint8_t *bytes) -{ - sprintf(addr, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); -} - -static void ipv6_to_address(char *addr, const uint8_t *bytes) -{ - ip6tos(bytes, addr); -} - - SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port) { _ip_address = NULL; @@ -137,13 +77,12 @@ bool SocketAddress::set_ip_address(const char *addr) delete[] _ip_address; _ip_address = NULL; - if (addr && ipv4_is_valid(addr)) { + if (addr && stoip4(addr, strlen(addr), _addr.bytes)) { _addr.version = NSAPI_IPv4; - ipv4_from_address(_addr.bytes, addr); return true; } else if (addr && ipv6_is_valid(addr)) { _addr.version = NSAPI_IPv6; - ipv6_from_address(_addr.bytes, addr); + stoip6(addr, strlen(addr), _addr.bytes); return true; } else { _addr = nsapi_addr_t(); @@ -186,9 +125,9 @@ const char *SocketAddress::get_ip_address() const if (!_ip_address) { _ip_address = new char[NSAPI_IP_SIZE]; if (_addr.version == NSAPI_IPv4) { - ipv4_to_address(_ip_address, _addr.bytes); + ip4tos(_addr.bytes, _ip_address); } else if (_addr.version == NSAPI_IPv6) { - ipv6_to_address(_ip_address, _addr.bytes); + ip6tos(_addr.bytes, _ip_address); } }