mirror of https://github.com/ARMmbed/mbed-os.git
Fix ipv6 addr in SocketAddress
Correctly set and return the ipv6 address.pull/2216/head^2
parent
56e11d6709
commit
aea45c88ce
|
@ -80,11 +80,20 @@ static void ipv4_to_address(char *addr, const uint8_t *bytes)
|
||||||
|
|
||||||
static void ipv6_to_address(char *addr, const uint8_t *bytes)
|
static void ipv6_to_address(char *addr, const uint8_t *bytes)
|
||||||
{
|
{
|
||||||
|
int pos = 0;
|
||||||
for (int i = 0; i < NSAPI_IPv6_BYTES; i+=2) {
|
for (int i = 0; i < NSAPI_IPv6_BYTES; i+=2) {
|
||||||
sprintf(&addr[5*i], "%02x%02x", bytes[i], bytes[i+1]);
|
int ret = sprintf(&addr[pos], "%02x%02x", bytes[i], bytes[i+1]);
|
||||||
addr[5*i+4] = ':';
|
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)
|
SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port)
|
||||||
|
@ -136,7 +145,7 @@ void SocketAddress::set_ip_address(const char *addr)
|
||||||
address_to_ipv4(_ip_bytes, addr);
|
address_to_ipv4(_ip_bytes, addr);
|
||||||
} else if (addr && address_is_ipv6(addr)) {
|
} else if (addr && address_is_ipv6(addr)) {
|
||||||
_ip_version = NSAPI_IPv6;
|
_ip_version = NSAPI_IPv6;
|
||||||
address_to_ipv4(_ip_bytes, addr);
|
address_to_ipv6(_ip_bytes, addr);
|
||||||
} else {
|
} else {
|
||||||
_ip_version = NSAPI_IPv4;
|
_ip_version = NSAPI_IPv4;
|
||||||
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
|
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
|
||||||
|
@ -171,7 +180,7 @@ const char *SocketAddress::get_ip_address() const
|
||||||
if (!ip_address[0]) {
|
if (!ip_address[0]) {
|
||||||
if (_ip_version == NSAPI_IPv4) {
|
if (_ip_version == NSAPI_IPv4) {
|
||||||
ipv4_to_address(ip_address, _ip_bytes);
|
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);
|
ipv6_to_address(ip_address, _ip_bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue