NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
Revision 97:68232387bc75, committed 2016-04-19
- Comitter:
- Christopher Haster
- Date:
- Tue Apr 19 18:24:46 2016 -0500
- Parent:
- 96:656011e49d9f
- Child:
- 98:0f614f1d0398
- Commit message:
- Fix ipv6 addr in SocketAddress
Correctly set and return the ipv6 address.
Changed in this revision
SocketAddress.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/SocketAddress.cpp Tue Apr 19 18:24:34 2016 -0500 +++ b/SocketAddress.cpp Tue Apr 19 18:24:46 2016 -0500 @@ -80,11 +80,20 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes) { + int pos = 0; for (int i = 0; i < NSAPI_IPv6_BYTES; i+=2) { - sprintf(&addr[5*i], "%02x%02x", bytes[i], bytes[i+1]); - addr[5*i+4] = ':'; + int ret = sprintf(&addr[pos], "%02x%02x", bytes[i], bytes[i+1]); + if (ret < 0) { + memset(addr, 0, NSAPI_IPv6_SIZE + 1); + return; + } + pos += ret; + + addr[pos++] = ':'; } - addr[NSAPI_IPv6_BYTES-1] = '\0'; + pos -= 1; // Overwrite last ':' + addr[pos++] = '\0'; + MBED_ASSERT(NSAPI_IPv6_SIZE == pos); } SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) @@ -136,7 +145,7 @@ address_to_ipv4(_ip_bytes, addr); } else if (addr && address_is_ipv6(addr)) { _ip_version = NSAPI_IPv6; - address_to_ipv4(_ip_bytes, addr); + address_to_ipv6(_ip_bytes, addr); } else { _ip_version = NSAPI_IPv4; memset(_ip_bytes, 0, NSAPI_IPv4_BYTES); @@ -171,7 +180,7 @@ if (!ip_address[0]) { if (_ip_version == NSAPI_IPv4) { ipv4_to_address(ip_address, _ip_bytes); - } else if (_ip_version == NSAPI_IPv4) { + } else if (_ip_version == NSAPI_IPv6) { ipv6_to_address(ip_address, _ip_bytes); } }