Skip to content

Commit 17dc50c

Browse files
committed
Apply IPAddress fixes from ESP8266
esp8266/Arduino#8818
1 parent 5e62273 commit 17dc50c

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

cores/esp32/IPAddress.cpp

+18-21
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ IPAddress::IPAddress(const IPAddress& from)
3737
}
3838

3939
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
40-
setV4();
41-
(*this)[0] = first_octet;
42-
(*this)[1] = second_octet;
43-
(*this)[2] = third_octet;
44-
(*this)[3] = fourth_octet;
40+
uint8_t addr[] {
41+
first_octet,
42+
second_octet,
43+
third_octet,
44+
fourth_octet,
45+
};
46+
*this = &addr[0];
4547
}
4648

4749
IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16) {
@@ -86,19 +88,6 @@ IPAddress::IPAddress(IPType type, const uint8_t *address, uint8_t zone) {
8688

8789
}
8890

89-
void IPAddress::ctor32(uint32_t address) {
90-
setV4();
91-
v4() = address;
92-
}
93-
94-
IPAddress::IPAddress(const uint8_t *address) {
95-
setV4();
96-
(*this)[0] = address[0];
97-
(*this)[1] = address[1];
98-
(*this)[2] = address[2];
99-
(*this)[3] = address[3];
100-
}
101-
10291
bool IPAddress::fromString(const char *address) {
10392
if (!fromString4(address)) {
10493
#if LWIP_IPV6
@@ -154,8 +143,9 @@ bool IPAddress::fromString4(const char *address) {
154143
}
155144

156145
IPAddress& IPAddress::operator=(const uint8_t *address) {
157-
setV4();
158-
v4() = *reinterpret_cast<const uint32_t*>(address);
146+
uint32_t value;
147+
memcpy_P(&value, address, sizeof(value));
148+
*this = value;
159149
return *this;
160150
}
161151

@@ -166,7 +156,14 @@ IPAddress& IPAddress::operator=(uint32_t address) {
166156
}
167157

168158
bool IPAddress::operator==(const uint8_t* addr) const {
169-
return isV4() && v4() == *reinterpret_cast<const uint32_t*>(addr);
159+
if (!isV4()) {
160+
return false;
161+
}
162+
163+
uint32_t value;
164+
memcpy_P(&value, addr, sizeof(value));
165+
166+
return v4() == value;
170167
}
171168

172169
size_t IPAddress::printTo(Print& p) const {

cores/esp32/IPAddress.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class IPAddress: public Printable
5454
IPAddress(const IPAddress& from);
5555
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
5656
IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16);
57-
IPAddress(uint32_t address) { ctor32(address); }
58-
IPAddress(const uint8_t *address); // v4 only
57+
IPAddress(uint32_t address) { *this = address; }
58+
IPAddress(unsigned long address) { *this = address; }
59+
IPAddress(int address) { *this = address; }
60+
IPAddress(const uint8_t *address) { *this = address; }
5961
IPAddress(IPType type, const uint8_t *address);
6062
IPAddress(IPType type, const uint8_t *address, uint8_t zone);
6163

@@ -68,7 +70,7 @@ class IPAddress: public Printable
6870
operator uint32_t() { return isV4()? v4(): (uint32_t)0; }
6971

7072
// generic IPv4 wrapper to uint32-view like arduino loves to see it
71-
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; } // for raw_address(const)
73+
const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; }
7274
uint32_t& v4() { return ip_2_ip4(&_ip)->addr; }
7375

7476
bool operator==(const IPAddress& addr) const {
@@ -97,11 +99,16 @@ class IPAddress: public Printable
9799

98100
// Overloaded index operator to allow getting and setting individual octets of the address
99101
uint8_t operator[](int index) const {
100-
return isV4()? *(raw_address() + index): 0;
102+
if (!isV4()) {
103+
return 0;
104+
}
105+
106+
return ip4_addr_get_byte_val(*ip_2_ip4(&_ip), index);
101107
}
102108
uint8_t& operator[](int index) {
103109
setV4();
104-
return *(raw_address() + index);
110+
uint8_t* ptr = reinterpret_cast<uint8_t*>(&v4());
111+
return *(ptr + index);
105112
}
106113

107114
// Overloaded copy operators to allow initialisation of IPAddress objects from other types

0 commit comments

Comments
 (0)