Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b8cc73c

Browse files
committedDec 18, 2023
Add zone to IPAddress and update WiFiUDP and WiFiGeneric
1 parent 5987211 commit b8cc73c

File tree

5 files changed

+267
-221
lines changed

5 files changed

+267
-221
lines changed
 

‎cores/esp32/IPAddress.cpp

Lines changed: 182 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
/*
2-
IPAddress.cpp - Base class that provides IPAddress
3-
Copyright (c) 2011 Adrian McEwen. All right reserved.
2+
IPAddress.cpp - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
44
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
99
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
1414
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18-
*/
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
1919

20-
#include <Arduino.h>
21-
#include <IPAddress.h>
22-
#include <Print.h>
23-
#include <StreamString.h>
20+
#include "IPAddress.h"
21+
#include "Print.h"
22+
#include "lwip/netif.h"
2423

2524
IPAddress::IPAddress() : IPAddress(IPv4) {}
2625

2726
IPAddress::IPAddress(IPType ip_type)
2827
{
2928
_type = ip_type;
29+
_zone = IP6_NO_ZONE;
3030
memset(_address.bytes, 0, sizeof(_address.bytes));
3131
}
3232

3333
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
3434
{
3535
_type = IPv4;
36+
_zone = IP6_NO_ZONE;
3637
memset(_address.bytes, 0, sizeof(_address.bytes));
3738
_address.bytes[IPADDRESS_V4_BYTES_INDEX] = first_octet;
3839
_address.bytes[IPADDRESS_V4_BYTES_INDEX + 1] = second_octet;
3940
_address.bytes[IPADDRESS_V4_BYTES_INDEX + 2] = third_octet;
4041
_address.bytes[IPADDRESS_V4_BYTES_INDEX + 3] = fourth_octet;
4142
}
4243

43-
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) {
44+
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, uint8_t z) {
4445
_type = IPv6;
4546
_address.bytes[0] = o1;
4647
_address.bytes[1] = o2;
@@ -58,6 +59,7 @@ IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5,
5859
_address.bytes[13] = o14;
5960
_address.bytes[14] = o15;
6061
_address.bytes[15] = o16;
62+
_zone = z;
6163
}
6264

6365
IPAddress::IPAddress(uint32_t address)
@@ -78,14 +80,16 @@ IPAddress::IPAddress(uint32_t address)
7880

7981
IPAddress::IPAddress(const uint8_t *address) : IPAddress(IPv4, address) {}
8082

81-
IPAddress::IPAddress(IPType ip_type, const uint8_t *address)
83+
IPAddress::IPAddress(IPType ip_type, const uint8_t *address, uint8_t z)
8284
{
8385
_type = ip_type;
8486
if (ip_type == IPv4) {
8587
memset(_address.bytes, 0, sizeof(_address.bytes));
8688
memcpy(&_address.bytes[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t));
89+
_zone = 0;
8790
} else {
8891
memcpy(_address.bytes, address, sizeof(_address.bytes));
92+
_zone = z;
8993
}
9094
}
9195

@@ -94,121 +98,9 @@ IPAddress::IPAddress(const char *address)
9498
fromString(address);
9599
}
96100

97-
IPAddress& IPAddress::operator=(const uint8_t *address)
98-
{
99-
// IPv4 only conversion from byte pointer
100-
_type = IPv4;
101-
memset(_address.bytes, 0, sizeof(_address.bytes));
102-
memcpy(&_address.bytes[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t));
103-
return *this;
104-
}
105-
106-
IPAddress& IPAddress::operator=(const char *address)
101+
IPAddress::IPAddress(const IPAddress& address)
107102
{
108-
fromString(address);
109-
return *this;
110-
}
111-
112-
IPAddress& IPAddress::operator=(uint32_t address)
113-
{
114-
// IPv4 conversion
115-
// See note on conversion/comparison and uint32_t
116-
_type = IPv4;
117-
memset(_address.bytes, 0, sizeof(_address.bytes));
118-
_address.dword[IPADDRESS_V4_DWORD_INDEX] = address;
119-
return *this;
120-
}
121-
122-
bool IPAddress::operator==(const IPAddress& addr) const
123-
{
124-
return (addr._type == _type)
125-
&& (memcmp(addr._address.bytes, _address.bytes, sizeof(_address.bytes)) == 0);
126-
}
127-
128-
bool IPAddress::operator==(const uint8_t* addr) const
129-
{
130-
// IPv4 only comparison to byte pointer
131-
// Can't support IPv6 as we know our type, but not the length of the pointer
132-
return _type == IPv4 && memcmp(addr, &_address.bytes[IPADDRESS_V4_BYTES_INDEX], sizeof(uint32_t)) == 0;
133-
}
134-
135-
uint8_t IPAddress::operator[](int index) const {
136-
if (_type == IPv4) {
137-
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
138-
}
139-
return _address.bytes[index];
140-
}
141-
142-
uint8_t& IPAddress::operator[](int index) {
143-
if (_type == IPv4) {
144-
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
145-
}
146-
return _address.bytes[index];
147-
}
148-
149-
size_t IPAddress::printTo(Print& p) const
150-
{
151-
size_t n = 0;
152-
153-
if (_type == IPv6) {
154-
// IPv6 IETF canonical format: compress left-most longest run of two or more zero fields, lower case
155-
int8_t longest_start = -1;
156-
int8_t longest_length = 1;
157-
int8_t current_start = -1;
158-
int8_t current_length = 0;
159-
for (int8_t f = 0; f < 8; f++) {
160-
if (_address.bytes[f * 2] == 0 && _address.bytes[f * 2 + 1] == 0) {
161-
if (current_start == -1) {
162-
current_start = f;
163-
current_length = 1;
164-
} else {
165-
current_length++;
166-
}
167-
if (current_length > longest_length) {
168-
longest_start = current_start;
169-
longest_length = current_length;
170-
}
171-
} else {
172-
current_start = -1;
173-
}
174-
}
175-
for (int f = 0; f < 8; f++) {
176-
if (f < longest_start || f >= longest_start + longest_length) {
177-
uint8_t c1 = _address.bytes[f * 2] >> 4;
178-
uint8_t c2 = _address.bytes[f * 2] & 0xf;
179-
uint8_t c3 = _address.bytes[f * 2 + 1] >> 4;
180-
uint8_t c4 = _address.bytes[f * 2 + 1] & 0xf;
181-
if (c1 > 0) {
182-
n += p.print((char)(c1 < 10 ? '0' + c1 : 'a' + c1 - 10));
183-
}
184-
if (c1 > 0 || c2 > 0) {
185-
n += p.print((char)(c2 < 10 ? '0' + c2 : 'a' + c2 - 10));
186-
}
187-
if (c1 > 0 || c2 > 0 || c3 > 0) {
188-
n += p.print((char)(c3 < 10 ? '0' + c3 : 'a' + c3 - 10));
189-
}
190-
n += p.print((char)(c4 < 10 ? '0' + c4 : 'a' + c4 - 10));
191-
if (f < 7) {
192-
n += p.print(':');
193-
}
194-
} else if (f == longest_start) {
195-
if (longest_start == 0) {
196-
n += p.print(':');
197-
}
198-
n += p.print(':');
199-
}
200-
}
201-
return n;
202-
}
203-
204-
// IPv4
205-
for (int i =0; i < 3; i++)
206-
{
207-
n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + i], DEC);
208-
n += p.print('.');
209-
}
210-
n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + 3], DEC);
211-
return n;
103+
*this = address;
212104
}
213105

214106
String IPAddress::toString4() const
@@ -220,10 +112,13 @@ String IPAddress::toString4() const
220112

221113
String IPAddress::toString6() const
222114
{
223-
StreamString s;
224-
s.reserve(40);
225-
printTo(s);
226-
return s;
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);
227122
}
228123

229124
String IPAddress::toString() const
@@ -235,10 +130,8 @@ String IPAddress::toString() const
235130
}
236131
}
237132

238-
bool IPAddress::fromString(const char *address)
239-
{
240-
if (!fromString4(address))
241-
{
133+
bool IPAddress::fromString(const char *address) {
134+
if (!fromString4(address)) {
242135
return fromString6(address);
243136
}
244137
return true;
@@ -336,6 +229,12 @@ bool IPAddress::fromString6(const char *address) {
336229
colons++;
337230
acc = 0;
338231
}
232+
else if (c == '%') {
233+
_zone = netif_name_to_index(address);
234+
while(*address != '\0'){
235+
address++;
236+
}
237+
}
339238
else
340239
// Invalid char
341240
return false;
@@ -364,5 +263,144 @@ bool IPAddress::fromString6(const char *address) {
364263
return true;
365264
}
366265

367-
// declared one time - as external in IPAddress.h
368-
IPAddress INADDR_NONE(0, 0, 0, 0);
266+
IPAddress& IPAddress::operator=(const uint8_t *address)
267+
{
268+
// IPv4 only conversion from byte pointer
269+
_type = IPv4;
270+
memset(_address.bytes, 0, sizeof(_address.bytes));
271+
memcpy(&_address.bytes[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t));
272+
return *this;
273+
}
274+
275+
IPAddress& IPAddress::operator=(const char *address)
276+
{
277+
fromString(address);
278+
return *this;
279+
}
280+
281+
IPAddress& IPAddress::operator=(uint32_t address)
282+
{
283+
// IPv4 conversion
284+
// See note on conversion/comparison and uint32_t
285+
_type = IPv4;
286+
memset(_address.bytes, 0, sizeof(_address.bytes));
287+
_address.dword[IPADDRESS_V4_DWORD_INDEX] = address;
288+
return *this;
289+
}
290+
291+
IPAddress& IPAddress::operator=(const IPAddress& address){
292+
_type = address.type();
293+
_zone = address.zone();
294+
memcpy(_address.bytes, address._address.bytes, sizeof(_address.bytes));
295+
return *this;
296+
}
297+
298+
bool IPAddress::operator==(const IPAddress& addr) const {
299+
return (addr._type == _type)
300+
&& (memcmp(addr._address.bytes, _address.bytes, sizeof(_address.bytes)) == 0);
301+
}
302+
303+
bool IPAddress::operator==(const uint8_t* addr) const
304+
{
305+
// IPv4 only comparison to byte pointer
306+
// Can't support IPv6 as we know our type, but not the length of the pointer
307+
return _type == IPv4 && memcmp(addr, &_address.bytes[IPADDRESS_V4_BYTES_INDEX], sizeof(uint32_t)) == 0;
308+
}
309+
310+
uint8_t IPAddress::operator[](int index) const {
311+
if (_type == IPv4) {
312+
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
313+
}
314+
return _address.bytes[index];
315+
}
316+
317+
uint8_t& IPAddress::operator[](int index) {
318+
if (_type == IPv4) {
319+
return _address.bytes[IPADDRESS_V4_BYTES_INDEX + index];
320+
}
321+
return _address.bytes[index];
322+
}
323+
324+
size_t IPAddress::printTo(Print& p) const
325+
{
326+
size_t n = 0;
327+
328+
if (_type == IPv6) {
329+
// IPv6 IETF canonical format: compress left-most longest run of two or more zero fields, lower case
330+
int8_t longest_start = -1;
331+
int8_t longest_length = 1;
332+
int8_t current_start = -1;
333+
int8_t current_length = 0;
334+
for (int8_t f = 0; f < 8; f++) {
335+
if (_address.bytes[f * 2] == 0 && _address.bytes[f * 2 + 1] == 0) {
336+
if (current_start == -1) {
337+
current_start = f;
338+
current_length = 1;
339+
} else {
340+
current_length++;
341+
}
342+
if (current_length > longest_length) {
343+
longest_start = current_start;
344+
longest_length = current_length;
345+
}
346+
} else {
347+
current_start = -1;
348+
}
349+
}
350+
for (int f = 0; f < 8; f++) {
351+
if (f < longest_start || f >= longest_start + longest_length) {
352+
uint8_t c1 = _address.bytes[f * 2] >> 4;
353+
uint8_t c2 = _address.bytes[f * 2] & 0xf;
354+
uint8_t c3 = _address.bytes[f * 2 + 1] >> 4;
355+
uint8_t c4 = _address.bytes[f * 2 + 1] & 0xf;
356+
if (c1 > 0) {
357+
n += p.print((char)(c1 < 10 ? '0' + c1 : 'a' + c1 - 10));
358+
}
359+
if (c1 > 0 || c2 > 0) {
360+
n += p.print((char)(c2 < 10 ? '0' + c2 : 'a' + c2 - 10));
361+
}
362+
if (c1 > 0 || c2 > 0 || c3 > 0) {
363+
n += p.print((char)(c3 < 10 ? '0' + c3 : 'a' + c3 - 10));
364+
}
365+
n += p.print((char)(c4 < 10 ? '0' + c4 : 'a' + c4 - 10));
366+
if (f < 7) {
367+
n += p.print(':');
368+
}
369+
} else if (f == longest_start) {
370+
if (longest_start == 0) {
371+
n += p.print(':');
372+
}
373+
n += p.print(':');
374+
}
375+
}
376+
return n;
377+
}
378+
379+
// IPv4
380+
for (int i =0; i < 3; i++)
381+
{
382+
n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + i], DEC);
383+
n += p.print('.');
384+
}
385+
n += p.print(_address.bytes[IPADDRESS_V4_BYTES_INDEX + 3], DEC);
386+
return n;
387+
}
388+
389+
void IPAddress::to_ip_addr_t(ip_addr_t* addr){
390+
if(_type == IPv6){
391+
addr->type = IPADDR_TYPE_V6;
392+
addr->u_addr.ip6.addr[0] = _address.dword[0];
393+
addr->u_addr.ip6.addr[1] = _address.dword[1];
394+
addr->u_addr.ip6.addr[2] = _address.dword[2];
395+
addr->u_addr.ip6.addr[3] = _address.dword[3];
396+
#if LWIP_IPV6_SCOPES
397+
addr->u_addr.ip6.zone = _zone;
398+
#endif /* LWIP_IPV6_SCOPES */
399+
} else {
400+
addr->type = IPADDR_TYPE_V4;
401+
addr->u_addr.ip4.addr = _address.dword[IPADDRESS_V4_DWORD_INDEX];
402+
}
403+
}
404+
405+
const IPAddress IN6ADDR_ANY(IPv6);
406+
const IPAddress INADDR_NONE(0,0,0,0);

‎cores/esp32/IPAddress.h

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,108 @@
11
/*
2-
IPAddress.h - Base class that provides IPAddress
3-
Copyright (c) 2011 Adrian McEwen. All right reserved.
2+
IPAddress.h - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
44
5-
This library is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU Lesser General Public
7-
License as published by the Free Software Foundation; either
8-
version 2.1 of the License, or (at your option) any later version.
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
99
10-
This library is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13-
Lesser General Public License for more details.
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
1414
15-
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18-
*/
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
1919

20-
#ifndef IPAddress_h
21-
#define IPAddress_h
20+
#pragma once
2221

2322
#include <stdint.h>
24-
#include <WString.h>
25-
#include <Printable.h>
26-
27-
// A class to make it easier to handle and pass around IP addresses
23+
#include "Printable.h"
24+
#include "WString.h"
25+
#include "lwip/ip_addr.h"
2826

2927
#define IPADDRESS_V4_BYTES_INDEX 12
3028
#define IPADDRESS_V4_DWORD_INDEX 3
3129

32-
enum IPType
33-
{
30+
// A class to make it easier to handle and pass around IP addresses
31+
32+
enum IPType {
3433
IPv4,
3534
IPv6
3635
};
3736

38-
class IPAddress: public Printable
39-
{
37+
class IPAddress : public Printable {
4038
private:
4139
union {
4240
uint8_t bytes[16];
4341
uint32_t dword[4];
4442
} _address;
4543
IPType _type;
44+
uint8_t _zone;
4645

4746
// Access the raw byte array containing the address. Because this returns a pointer
4847
// to the internal structure rather than a copy of the address this function should only
4948
// be used when you know that the usage of the returned uint8_t* will be transient and not
5049
// stored.
51-
uint8_t* raw_address()
52-
{
53-
return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes;
54-
}
50+
uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; }
5551

5652
public:
5753
// Constructors
54+
55+
// Default IPv4
5856
IPAddress();
5957
IPAddress(IPType ip_type);
6058
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
61-
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);
59+
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, uint8_t z=0);
60+
// IPv4; see implementation note
6261
IPAddress(uint32_t address);
62+
// Default IPv4
6363
IPAddress(const uint8_t *address);
64-
IPAddress(IPType ip_type, const uint8_t *address);
64+
IPAddress(IPType ip_type, const uint8_t *address, uint8_t z=0);
6565
// If IPv4 fails tries IPv6 see fromString function
6666
IPAddress(const char *address);
67-
virtual ~IPAddress() {}
67+
IPAddress(const IPAddress& address);
6868

6969
bool fromString(const char *address);
7070
bool fromString(const String &address) { return fromString(address.c_str()); }
7171

72-
// Overloaded cast operator to allow IPAddress objects to be used where a
73-
// uint32_t is expected
74-
operator uint32_t() const
75-
{
76-
return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0;
77-
}
72+
// Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected
73+
// NOTE: IPv4 only; see implementation note
74+
operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; };
7875

7976
bool operator==(const IPAddress& addr) const;
77+
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };
78+
79+
// NOTE: IPv4 only; we don't know the length of the pointer
8080
bool operator==(const uint8_t* addr) const;
8181

8282
// Overloaded index operator to allow getting and setting individual octets of the address
8383
uint8_t operator[](int index) const;
8484
uint8_t& operator[](int index);
8585

8686
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
87+
// NOTE: IPv4 only
8788
IPAddress& operator=(const uint8_t *address);
89+
// NOTE: IPv4 only; see implementation note
8890
IPAddress& operator=(uint32_t address);
8991
// If IPv4 fails tries IPv6 see fromString function
9092
IPAddress& operator=(const char *address);
93+
IPAddress& operator=(const IPAddress& address);
9194

9295
virtual size_t printTo(Print& p) const;
9396
String toString() const;
9497

9598
IPType type() const { return _type; }
9699

97-
friend class EthernetClass;
100+
void to_ip_addr_t(ip_addr_t* addr);
101+
uint8_t zone() const { return (type() == IPv6)?_zone:0; }
102+
98103
friend class UDP;
99104
friend class Client;
100105
friend class Server;
101-
friend class DhcpClass;
102-
friend class DNSClient;
103106

104107
protected:
105108
bool fromString4(const char *address);
@@ -108,7 +111,5 @@ class IPAddress: public Printable
108111
String toString6() const;
109112
};
110113

111-
// changed to extern because const declaration creates copies in BSS of INADDR_NONE for each CPP unit that includes it
112-
extern IPAddress INADDR_NONE;
113-
extern IPAddress IN6ADDR_ANY;
114-
#endif
114+
extern const IPAddress IN6ADDR_ANY;
115+
extern const IPAddress INADDR_NONE;

‎libraries/WiFi/src/WiFiGeneric.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,11 @@ bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2
15461546
// ------------------------------------------------ Generic Network function ---------------------------------------------
15471547
// -----------------------------------------------------------------------------------------------------------------------
15481548

1549+
struct dns_api_msg6 {
1550+
ip_addr_t ip_addr;
1551+
int result;
1552+
};
1553+
15491554
/**
15501555
* DNS callback
15511556
* @param name
@@ -1584,7 +1589,7 @@ static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
15841589
*/
15851590
static void wifi_dns6_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
15861591
{
1587-
struct dns_api_msg *msg = (struct dns_api_msg *)callback_arg;
1592+
struct dns_api_msg6 *msg = (struct dns_api_msg6 *)callback_arg;
15881593

15891594
if(ipaddr && !msg->result) {
15901595
msg->ip_addr = *ipaddr;
@@ -1637,7 +1642,7 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
16371642
int WiFiGenericClass::hostByName6(const char* aHostname, ip_addr_t& aResult)
16381643
{
16391644
ip_addr_t addr;
1640-
struct dns_api_msg arg;
1645+
struct dns_api_msg6 arg;
16411646

16421647
memset(&arg, 0x0, sizeof(arg));
16431648
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);

‎libraries/WiFi/src/WiFiGeneric.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "esp_netif_types.h"
3333
#include "esp_eth_driver.h"
3434
#include "wifi_provisioning/manager.h"
35+
#include "lwip/ip_addr.h"
3536

3637
ESP_EVENT_DECLARE_BASE(ARDUINO_EVENTS);
3738

@@ -155,11 +156,6 @@ typedef enum {
155156
WIFI_TX_ANT_AUTO
156157
} wifi_tx_ant_t;
157158

158-
struct dns_api_msg {
159-
ip_addr_t ip_addr;
160-
int result;
161-
};
162-
163159
class WiFiGenericClass
164160
{
165161
public:

‎libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
5252
tx_buffer_len = 0;
5353

5454
#if LWIP_IPV6
55-
if ((udp_server=socket(address.isV6() ? AF_INET6 : AF_INET, SOCK_DGRAM, 0)) == -1){
55+
if ((udp_server=socket((address.type() == IPv6) ? AF_INET6 : AF_INET, SOCK_DGRAM, 0)) == -1){
5656
#else
5757
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
5858
#endif
@@ -70,19 +70,20 @@ uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
7070
struct sockaddr_storage serveraddr = {};
7171
size_t sock_size = 0;
7272
#if LWIP_IPV6
73-
if (address.isV6()) {
73+
if (address.type() == IPv6) {
7474
struct sockaddr_in6 *tmpaddr = (struct sockaddr_in6 *)&serveraddr;
75-
ip_addr_t * ip_addr = (ip_addr_t*) address;
75+
ip_addr_t addr;
76+
address.to_ip_addr_t(&addr);
7677
memset((char *) tmpaddr, 0, sizeof(struct sockaddr_in));
7778
tmpaddr->sin6_family = AF_INET6;
7879
tmpaddr->sin6_port = htons(server_port);
79-
tmpaddr->sin6_scope_id = ip_addr->u_addr.ip6.zone;
80-
inet6_addr_from_ip6addr(&tmpaddr->sin6_addr, ip_2_ip6(ip_addr));
80+
tmpaddr->sin6_scope_id = addr.u_addr.ip6.zone;
81+
inet6_addr_from_ip6addr(&tmpaddr->sin6_addr, ip_2_ip6(&addr));
8182
tmpaddr->sin6_flowinfo = 0;
8283
sock_size = sizeof(sockaddr_in6);
8384
} else
8485
#endif
85-
if (1) {
86+
{
8687
struct sockaddr_in *tmpaddr = (struct sockaddr_in *)&serveraddr;
8788
memset((char *) tmpaddr, 0, sizeof(struct sockaddr_in));
8889
tmpaddr->sin_family = AF_INET;
@@ -103,15 +104,16 @@ uint8_t WiFiUDP::begin(uint16_t p){
103104
return begin(IPAddress(), p);
104105
}
105106

106-
uint8_t WiFiUDP::beginMulticast(IPAddress a, uint16_t p){
107+
uint8_t WiFiUDP::beginMulticast(IPAddress address, uint16_t p){
107108
if(begin(IPAddress(), p)){
108-
ip_addr_t * ip_addr = (ip_addr_t*) a;
109-
if (ip_addr_ismulticast(ip_addr)) {
109+
ip_addr_t addr;
110+
address.to_ip_addr_t(&addr);
111+
if (ip_addr_ismulticast(&addr)) {
110112
#if LWIP_IPV6
111-
if (IP_IS_V6_VAL(*ip_addr)) {
113+
if (address.type() == IPv6) {
112114
struct ipv6_mreq mreq;
113115
bool joined = false;
114-
inet6_addr_from_ip6addr(&mreq.ipv6mr_multiaddr, ip_2_ip6(ip_addr));
116+
inet6_addr_from_ip6addr(&mreq.ipv6mr_multiaddr, ip_2_ip6(&addr));
115117

116118
// iterate on each interface
117119
for (netif* intf = netif_list; intf != nullptr; intf = intf->next) {
@@ -128,17 +130,17 @@ uint8_t WiFiUDP::beginMulticast(IPAddress a, uint16_t p){
128130
}
129131
} else
130132
#endif
131-
if (1) {
133+
{
132134
struct ip_mreq mreq;
133-
mreq.imr_multiaddr.s_addr = (in_addr_t)a;
135+
mreq.imr_multiaddr.s_addr = (in_addr_t)address;
134136
mreq.imr_interface.s_addr = INADDR_ANY;
135137
if (setsockopt(udp_server, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
136138
log_e("could not join igmp: %d", errno);
137139
stop();
138140
return 0;
139141
}
140142
}
141-
multicast_ip = a;
143+
multicast_ip = address;
142144
return 1;
143145
}
144146
}
@@ -158,12 +160,13 @@ void WiFiUDP::stop(){
158160
}
159161
if(udp_server == -1)
160162
return;
161-
ip_addr_t * mcast_addr = (ip_addr_t*) multicast_ip;
162-
if (!ip_addr_isany(mcast_addr)) {
163+
ip_addr_t addr;
164+
multicast_ip.to_ip_addr_t(&addr);
165+
if (!ip_addr_isany(&addr)) {
163166
#if LWIP_IPV6
164-
if (IP_IS_V6(mcast_addr)) {
167+
if (multicast_ip.type() == IPv6) {
165168
struct ipv6_mreq mreq;
166-
inet6_addr_from_ip6addr(&mreq.ipv6mr_multiaddr, ip_2_ip6(mcast_addr));
169+
inet6_addr_from_ip6addr(&mreq.ipv6mr_multiaddr, ip_2_ip6(&addr));
167170

168171
// iterate on each interface
169172
for (netif* intf = netif_list; intf != nullptr; intf = intf->next) {
@@ -174,7 +177,7 @@ void WiFiUDP::stop(){
174177
}
175178
} else
176179
#endif
177-
if (1) {
180+
{
178181
struct ip_mreq mreq;
179182
mreq.imr_multiaddr.s_addr = (in_addr_t)multicast_ip;
180183
mreq.imr_interface.s_addr = (in_addr_t)0;
@@ -240,8 +243,10 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port){
240243
}
241244

242245
int WiFiUDP::endPacket(){
246+
ip_addr_t addr;
247+
remote_ip.to_ip_addr_t(&addr);
243248

244-
if (remote_ip.isV4()) {
249+
if (remote_ip.type() == IPv4) {
245250
struct sockaddr_in recipient;
246251
recipient.sin_addr.s_addr = (uint32_t)remote_ip;
247252
recipient.sin_family = AF_INET;
@@ -254,7 +259,7 @@ int WiFiUDP::endPacket(){
254259
} else {
255260
struct sockaddr_in6 recipient;
256261
recipient.sin6_flowinfo = 0;
257-
recipient.sin6_addr = *(in6_addr*)(ip_addr_t*)remote_ip;
262+
recipient.sin6_addr = *(in6_addr*)(ip_addr_t*)(&addr);
258263
recipient.sin6_family = AF_INET6;
259264
recipient.sin6_port = htons(remote_port);
260265
recipient.sin6_scope_id = remote_ip.zone();
@@ -310,17 +315,18 @@ int WiFiUDP::parsePacket(){
310315
else if (si_other_storage.ss_family == AF_INET6) {
311316
struct sockaddr_in6 &si_other = (sockaddr_in6&) si_other_storage;
312317
remote_ip = IPAddress(IPv6, (uint8_t*)&si_other.sin6_addr, si_other.sin6_scope_id); // force IPv6
313-
ip_addr_t *ip_addr = (ip_addr_t*) remote_ip;
318+
ip_addr_t addr;
319+
remote_ip.to_ip_addr_t(&addr);
314320
/* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
315-
if (IP_IS_V6_VAL(*ip_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(ip_addr))) {
316-
unmap_ipv4_mapped_ipv6(ip_2_ip4(ip_addr), ip_2_ip6(ip_addr));
317-
IP_SET_TYPE_VAL(*ip_addr, IPADDR_TYPE_V4);
321+
if (remote_ip.type() == IPv6 && ip6_addr_isipv4mappedipv6(ip_2_ip6(&addr))) {
322+
unmap_ipv4_mapped_ipv6(ip_2_ip4(&addr), ip_2_ip6(&addr));
323+
IP_SET_TYPE_VAL(addr, IPADDR_TYPE_V4);
318324
}
319325
remote_port = ntohs(si_other.sin6_port);
320326
}
321327
#endif // LWIP_IPV6=1
322328
else {
323-
remote_ip = *IP_ADDR_ANY;
329+
remote_ip = ip_addr_any.u_addr.ip4.addr;
324330
remote_port = 0;
325331
}
326332
if (len > 0) {

0 commit comments

Comments
 (0)
Please sign in to comment.