Skip to content

Commit 305d276

Browse files
committed
Add from ip_addr_t conversion and better toString implementation
1 parent b8cc73c commit 305d276

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

cores/esp32/IPAddress.cpp

+28-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "IPAddress.h"
2121
#include "Print.h"
2222
#include "lwip/netif.h"
23+
#include "StreamString.h"
2324

2425
IPAddress::IPAddress() : IPAddress(IPv4) {}
2526

@@ -103,31 +104,11 @@ IPAddress::IPAddress(const IPAddress& address)
103104
*this = address;
104105
}
105106

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-
124107
String IPAddress::toString() const
125108
{
126-
if (_type == IPv4) {
127-
return toString4();
128-
} else {
129-
return toString6();
130-
}
109+
StreamString s;
110+
printTo(s);
111+
return String(s);
131112
}
132113

133114
bool IPAddress::fromString(const char *address) {
@@ -373,6 +354,13 @@ size_t IPAddress::printTo(Print& p) const
373354
n += p.print(':');
374355
}
375356
}
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+
}
376364
return n;
377365
}
378366

@@ -402,5 +390,22 @@ void IPAddress::to_ip_addr_t(ip_addr_t* addr){
402390
}
403391
}
404392

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+
405410
const IPAddress IN6ADDR_ANY(IPv6);
406411
const IPAddress INADDR_NONE(0,0,0,0);

cores/esp32/IPAddress.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,19 @@ class IPAddress : public Printable {
9797

9898
IPType type() const { return _type; }
9999

100-
void to_ip_addr_t(ip_addr_t* addr);
101100
uint8_t zone() const { return (type() == IPv6)?_zone:0; }
102101

102+
// LwIP conversions
103+
void to_ip_addr_t(ip_addr_t* addr);
104+
IPAddress& from_ip_addr_t(ip_addr_t* addr);
105+
103106
friend class UDP;
104107
friend class Client;
105108
friend class Server;
106109

107110
protected:
108111
bool fromString4(const char *address);
109112
bool fromString6(const char *address);
110-
String toString4() const;
111-
String toString6() const;
112113
};
113114

114115
extern const IPAddress IN6ADDR_ANY;

cores/esp32/StreamString.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
#ifndef STREAMSTRING_H_
2323
#define STREAMSTRING_H_
24-
24+
#include "Stream.h"
25+
#include "WString.h"
2526

2627
class StreamString: public Stream, public String
2728
{

0 commit comments

Comments
 (0)