|
20 | 20 | #include "IPAddress.h"
|
21 | 21 | #include "Print.h"
|
22 | 22 | #include "lwip/netif.h"
|
| 23 | +#include "StreamString.h" |
23 | 24 |
|
24 | 25 | IPAddress::IPAddress() : IPAddress(IPv4) {}
|
25 | 26 |
|
@@ -103,31 +104,11 @@ IPAddress::IPAddress(const IPAddress& address)
|
103 | 104 | *this = address;
|
104 | 105 | }
|
105 | 106 |
|
106 |
| -String IPAddress::toString4() const |
107 |
| -{ |
108 |
| - char szRet[16]; |
109 |
| - snprintf(szRet, sizeof(szRet), "%u.%u.%u.%u", _address.bytes[IPADDRESS_V4_BYTES_INDEX], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 1], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 2], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 3]); |
110 |
| - return String(szRet); |
111 |
| -} |
112 |
| - |
113 |
| -String IPAddress::toString6() const |
114 |
| -{ |
115 |
| - char szRet[40]; |
116 |
| - snprintf(szRet, sizeof(szRet), "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", |
117 |
| - _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], |
118 |
| - _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], |
119 |
| - _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], |
120 |
| - _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); |
121 |
| - return String(szRet); |
122 |
| -} |
123 |
| - |
124 | 107 | String IPAddress::toString() const
|
125 | 108 | {
|
126 |
| - if (_type == IPv4) { |
127 |
| - return toString4(); |
128 |
| - } else { |
129 |
| - return toString6(); |
130 |
| - } |
| 109 | + StreamString s; |
| 110 | + printTo(s); |
| 111 | + return String(s); |
131 | 112 | }
|
132 | 113 |
|
133 | 114 | bool IPAddress::fromString(const char *address) {
|
@@ -373,6 +354,13 @@ size_t IPAddress::printTo(Print& p) const
|
373 | 354 | n += p.print(':');
|
374 | 355 | }
|
375 | 356 | }
|
| 357 | + // add a zone if zone-id is non-zero |
| 358 | + if(_zone > 0){ |
| 359 | + n += p.print('%'); |
| 360 | + char if_name[NETIF_NAMESIZE]; |
| 361 | + netif_index_to_name(_zone, if_name); |
| 362 | + n += p.print(if_name); |
| 363 | + } |
376 | 364 | return n;
|
377 | 365 | }
|
378 | 366 |
|
@@ -402,5 +390,22 @@ void IPAddress::to_ip_addr_t(ip_addr_t* addr){
|
402 | 390 | }
|
403 | 391 | }
|
404 | 392 |
|
| 393 | +IPAddress& IPAddress::from_ip_addr_t(ip_addr_t* addr){ |
| 394 | + if(addr->type == IPADDR_TYPE_V6){ |
| 395 | + _type = IPv6; |
| 396 | + _address.dword[0] = addr->u_addr.ip6.addr[0]; |
| 397 | + _address.dword[1] = addr->u_addr.ip6.addr[1]; |
| 398 | + _address.dword[2] = addr->u_addr.ip6.addr[2]; |
| 399 | + _address.dword[3] = addr->u_addr.ip6.addr[3]; |
| 400 | +#if LWIP_IPV6_SCOPES |
| 401 | + _zone = addr->u_addr.ip6.zone; |
| 402 | +#endif /* LWIP_IPV6_SCOPES */ |
| 403 | + } else { |
| 404 | + _type = IPv4; |
| 405 | + _address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->u_addr.ip4.addr; |
| 406 | + } |
| 407 | + return *this; |
| 408 | +} |
| 409 | + |
405 | 410 | const IPAddress IN6ADDR_ANY(IPv6);
|
406 | 411 | const IPAddress INADDR_NONE(0,0,0,0);
|
0 commit comments