Skip to content

Commit 5c4db3a

Browse files
authored
IPv6 on esp8266-nonos-sdk and arduino (#5136)
1 parent a501d3c commit 5c4db3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1268
-807
lines changed

boards.txt

+348-116
Large diffs are not rendered by default.

cores/esp8266/AddrList.h

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
AddrList.h - cycle through lwIP netif's ip addresses like a c++ list
3+
Copyright (c) 2018 david gauchard. All right reserved.
4+
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.
9+
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.
14+
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+
*/
19+
20+
/*
21+
This class allows to explore all configured IP addresses
22+
in lwIP netifs, with that kind of c++ loop:
23+
24+
for (auto a: ifList)
25+
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
26+
a->iface().c_str(),
27+
a->number(),
28+
a->addr().isLegacy(),
29+
a->addr().isV4(),
30+
a->addr().isLocal(),
31+
a->hostname().c_str(),
32+
a->addr().toString().c_str());
33+
34+
This loop:
35+
36+
while (WiFi.status() != WL_CONNECTED()) {
37+
Serial.print('.');
38+
delay(500);
39+
}
40+
41+
can be replaced by:
42+
43+
for (bool configured = false; !configured; ) {
44+
for (auto iface: ifList)
45+
if ((configured = !iface->addr().isLocal())
46+
break;
47+
Serial.print('.');
48+
delay(500);
49+
}
50+
51+
waiting for an IPv6 global address:
52+
53+
for (bool configured = false; !configured; ) {
54+
for (auto iface: ifList)
55+
if ((configured = ( !iface->addr()->isV4()
56+
&& !iface->addr().isLocal())))
57+
break;
58+
Serial.print('.');
59+
delay(500);
60+
}
61+
62+
waiting for an IPv6 global address, on a specific interface:
63+
64+
for (bool configured = false; !configured; ) {
65+
for (auto iface: ifList)
66+
if ((configured = ( !iface->addr()->isV4()
67+
&& !iface->addr().isLocal()
68+
&& iface->number() == STATION_IF)))
69+
break;
70+
Serial.print('.');
71+
delay(500);
72+
}
73+
*/
74+
75+
#ifndef __ADDRLIST_H
76+
#define __ADDRLIST_H
77+
78+
#include <IPAddress.h>
79+
#include <lwip/netif.h>
80+
81+
#if LWIP_IPV6
82+
#define IF_NUM_ADDRESSES (1 + LWIP_IPV6_NUM_ADDRESSES)
83+
#else
84+
#define IF_NUM_ADDRESSES (1)
85+
#endif
86+
87+
88+
class AddrListClass {
89+
90+
// no member in this class
91+
// lwIP's global 'struct netif* netif_list' is used
92+
// designed to be used with 'for (auto x: ifList)'
93+
94+
public:
95+
96+
class const_iterator {
97+
98+
public:
99+
100+
// iterator operations:
101+
102+
const_iterator (bool begin = true): _netif(begin? netif_list: nullptr), _num(-1) { ++*this; }
103+
const_iterator (const const_iterator& o): _netif(o._netif), _num(o._num) { }
104+
const_iterator& operator= (const const_iterator& o) { _netif = o._netif; _num = o._num; return *this; }
105+
106+
bool operator!= (const const_iterator& o) { return !equal(o); }
107+
bool operator== (const const_iterator& o) { return equal(o); }
108+
109+
const_iterator operator++(int) {
110+
const_iterator ret = *this;
111+
++(*this);
112+
return ret;
113+
}
114+
115+
const_iterator& operator++() {
116+
while (_netif) {
117+
if (++_num == IF_NUM_ADDRESSES) {
118+
_num = -1;
119+
_netif = _netif->next;
120+
continue;
121+
}
122+
if (!ip_addr_isany(_ip_from_netif_num()))
123+
break;
124+
}
125+
return *this;
126+
}
127+
128+
// (*iterator) emulation:
129+
130+
const const_iterator& operator* () const { return *this; }
131+
const const_iterator* operator-> () const { return this; }
132+
133+
bool isLegacy() const { return _num == 0; }
134+
bool isLocal() const { return addr().isLocal(); }
135+
IPAddress addr () const { return _ip_from_netif_num(); }
136+
IPAddress netmask () const { return _netif->netmask; }
137+
IPAddress gw () const { return _netif->gw; }
138+
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
139+
const char* hostname () const { return _netif->hostname?: emptyString.c_str(); }
140+
const char* mac () const { return (const char*)_netif->hwaddr; }
141+
int number () const { return _netif->num; }
142+
143+
protected:
144+
145+
bool equal (const const_iterator& o) {
146+
return _netif == o._netif
147+
&& (!_netif || _num == o._num);
148+
}
149+
150+
const ip_addr_t* _ip_from_netif_num () const {
151+
#if LWIP_IPV6
152+
return _num? &_netif->ip6_addr[_num - 1]: &_netif->ip_addr;
153+
#else
154+
return &_netif->ip_addr;
155+
#endif
156+
}
157+
158+
netif* _netif;
159+
int _num; // address index (0 is legacy, _num-1 is ip6_addr[]'s index)
160+
};
161+
162+
const const_iterator begin () const { return const_iterator(true); }
163+
const const_iterator end () const { return const_iterator(false); }
164+
};
165+
166+
extern AddrListClass addrList;
167+
168+
#endif // __ADDRLIST_H

cores/esp8266/Client.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class Client: public Stream {
2727

2828
public:
29-
virtual int connect(IPAddress ip, uint16_t port) =0;
29+
virtual int connect(CONST IPAddress& ip, uint16_t port) =0;
3030
virtual int connect(const char *host, uint16_t port) =0;
3131
virtual size_t write(uint8_t) =0;
3232
virtual size_t write(const uint8_t *buf, size_t size) =0;
@@ -39,7 +39,7 @@ class Client: public Stream {
3939
virtual uint8_t connected() = 0;
4040
virtual operator bool() = 0;
4141
protected:
42-
uint8_t* rawIPAddress(IPAddress& addr) {
42+
CONST uint8_t* rawIPAddress(CONST IPAddress& addr) {
4343
return addr.raw_address();
4444
}
4545
;

cores/esp8266/Esp-version.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
#define STR(x) STRHELPER(x) // stringifier
3030

3131
static const char arduino_esp8266_git_ver [] PROGMEM = STR(ARDUINO_ESP8266_GIT_DESC);
32-
#if LWIP_VERSION_MAJOR != 1
33-
static const char lwip2_version [] PROGMEM = "/lwIP:" STR(LWIP_VERSION_MAJOR) "." STR(LWIP_VERSION_MINOR) "." STR(LWIP_VERSION_REVISION);
34-
#endif
3532
static const char bearssl_version [] PROGMEM = "/BearSSL:" STR(BEARSSL_GIT);
3633

3734
String EspClass::getFullVersion()
@@ -40,17 +37,18 @@ String EspClass::getFullVersion()
4037
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
4138
#if LWIP_VERSION_MAJOR == 1
4239
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
43-
#else
44-
+ FPSTR(lwip2_version)
45-
#endif
4640
#if LWIP_VERSION_IS_DEVELOPMENT
4741
+ F("-dev")
4842
#endif
4943
#if LWIP_VERSION_IS_RC
5044
+ F("rc") + String(LWIP_VERSION_RC)
5145
#endif
52-
#ifdef LWIP_HASH_STR
53-
+ "(" + F(LWIP_HASH_STR) + ")"
46+
#else // LWIP_VERSION_MAJOR != 1
47+
+ F("/lwIP:")
48+
#if LWIP_IPV6
49+
+ F("IPv6+")
50+
#endif
51+
+ F(LWIP_HASH_STR)
5452
#endif
5553
+ FPSTR(bearssl_version)
5654
;

0 commit comments

Comments
 (0)