mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Split AddrList into object-iterator-container components (#5410)
* Split AddrList into object-iterator-container components * Update AddrList.h Remove gist code
This commit is contained in:
parent
773f306ef9
commit
e5d50a6e4b
@ -21,15 +21,15 @@
|
|||||||
This class allows to explore all configured IP addresses
|
This class allows to explore all configured IP addresses
|
||||||
in lwIP netifs, with that kind of c++ loop:
|
in lwIP netifs, with that kind of c++ loop:
|
||||||
|
|
||||||
for (auto a: ifList)
|
for (auto a: addrList)
|
||||||
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
|
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
|
||||||
a->iface().c_str(),
|
a.iface().c_str(),
|
||||||
a->number(),
|
a.number(),
|
||||||
a->addr().isLegacy(),
|
a.addr().isLegacy(),
|
||||||
a->addr().isV4(),
|
a.addr().isV4(),
|
||||||
a->addr().isLocal(),
|
a.addr().isLocal(),
|
||||||
a->hostname().c_str(),
|
a.hostname().c_str(),
|
||||||
a->addr().toString().c_str());
|
a.addr().toString().c_str());
|
||||||
|
|
||||||
This loop:
|
This loop:
|
||||||
|
|
||||||
@ -41,8 +41,8 @@
|
|||||||
can be replaced by:
|
can be replaced by:
|
||||||
|
|
||||||
for (bool configured = false; !configured; ) {
|
for (bool configured = false; !configured; ) {
|
||||||
for (auto iface: ifList)
|
for (auto iface: addrList)
|
||||||
if ((configured = !iface->addr().isLocal())
|
if ((configured = !iface.addr().isLocal())
|
||||||
break;
|
break;
|
||||||
Serial.print('.');
|
Serial.print('.');
|
||||||
delay(500);
|
delay(500);
|
||||||
@ -51,9 +51,9 @@
|
|||||||
waiting for an IPv6 global address:
|
waiting for an IPv6 global address:
|
||||||
|
|
||||||
for (bool configured = false; !configured; ) {
|
for (bool configured = false; !configured; ) {
|
||||||
for (auto iface: ifList)
|
for (auto iface: addrList)
|
||||||
if ((configured = ( !iface->addr()->isV4()
|
if ((configured = ( !iface.addr().isV4()
|
||||||
&& !iface->addr().isLocal())))
|
&& !iface.addr().isLocal())))
|
||||||
break;
|
break;
|
||||||
Serial.print('.');
|
Serial.print('.');
|
||||||
delay(500);
|
delay(500);
|
||||||
@ -62,10 +62,10 @@
|
|||||||
waiting for an IPv6 global address, on a specific interface:
|
waiting for an IPv6 global address, on a specific interface:
|
||||||
|
|
||||||
for (bool configured = false; !configured; ) {
|
for (bool configured = false; !configured; ) {
|
||||||
for (auto iface: ifList)
|
for (auto iface: addrList)
|
||||||
if ((configured = ( !iface->addr()->isV4()
|
if ((configured = ( !iface.addr().isV4()
|
||||||
&& !iface->addr().isLocal()
|
&& !iface.addr().isLocal()
|
||||||
&& iface->number() == STATION_IF)))
|
&& iface.number() == STATION_IF)))
|
||||||
break;
|
break;
|
||||||
Serial.print('.');
|
Serial.print('.');
|
||||||
delay(500);
|
delay(500);
|
||||||
@ -85,54 +85,29 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
class AddrListClass {
|
namespace esp8266
|
||||||
|
{
|
||||||
|
|
||||||
// no member in this class
|
namespace AddressListImplementation
|
||||||
// lwIP's global 'struct netif* netif_list' is used
|
{
|
||||||
// designed to be used with 'for (auto x: ifList)'
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
class const_iterator {
|
struct netifWrapper
|
||||||
|
{
|
||||||
|
netifWrapper(netif * netif) : _netif(netif), _num(-1) {}
|
||||||
|
netifWrapper(const netifWrapper & o) : _netif(o._netif), _num(o._num) {}
|
||||||
|
|
||||||
public:
|
netifWrapper& operator=(const netifWrapper & o) {_netif = o._netif; _num = o._num; return *this;}
|
||||||
|
|
||||||
// iterator operations:
|
bool equal (const netifWrapper & o)
|
||||||
|
{
|
||||||
const_iterator (bool begin = true): _netif(begin? netif_list: nullptr), _num(-1) { ++*this; }
|
return _netif == o._netif && (!_netif || _num == o._num);
|
||||||
const_iterator (const const_iterator& o): _netif(o._netif), _num(o._num) { }
|
|
||||||
const_iterator& operator= (const const_iterator& o) { _netif = o._netif; _num = o._num; return *this; }
|
|
||||||
|
|
||||||
bool operator!= (const const_iterator& o) { return !equal(o); }
|
|
||||||
bool operator== (const const_iterator& o) { return equal(o); }
|
|
||||||
|
|
||||||
const_iterator operator++(int) {
|
|
||||||
const_iterator ret = *this;
|
|
||||||
++(*this);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator& operator++() {
|
|
||||||
while (_netif) {
|
|
||||||
if (++_num == IF_NUM_ADDRESSES) {
|
|
||||||
_num = -1;
|
|
||||||
_netif = _netif->next;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!ip_addr_isany(_ip_from_netif_num()))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// (*iterator) emulation:
|
|
||||||
|
|
||||||
const const_iterator& operator* () const { return *this; }
|
|
||||||
const const_iterator* operator-> () const { return this; }
|
|
||||||
|
|
||||||
bool isLegacy() const { return _num == 0; }
|
bool isLegacy() const { return _num == 0; }
|
||||||
bool isLocal() const { return addr().isLocal(); }
|
bool isLocal() const { return addr().isLocal(); }
|
||||||
IPAddress addr () const { return _ip_from_netif_num(); }
|
IPAddress addr () const { return ipFromNetifNum(); }
|
||||||
IPAddress netmask () const { return _netif->netmask; }
|
IPAddress netmask () const { return _netif->netmask; }
|
||||||
IPAddress gw () const { return _netif->gw; }
|
IPAddress gw () const { return _netif->gw; }
|
||||||
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
|
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
|
||||||
@ -140,14 +115,8 @@ class AddrListClass {
|
|||||||
const char* mac () const { return (const char*)_netif->hwaddr; }
|
const char* mac () const { return (const char*)_netif->hwaddr; }
|
||||||
int number () const { return _netif->num; }
|
int number () const { return _netif->num; }
|
||||||
|
|
||||||
protected:
|
const ip_addr_t* ipFromNetifNum () const
|
||||||
|
{
|
||||||
bool equal (const const_iterator& o) {
|
|
||||||
return _netif == o._netif
|
|
||||||
&& (!_netif || _num == o._num);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ip_addr_t* _ip_from_netif_num () const {
|
|
||||||
#if LWIP_IPV6
|
#if LWIP_IPV6
|
||||||
return _num ? &_netif->ip6_addr[_num - 1] : &_netif->ip_addr;
|
return _num ? &_netif->ip6_addr[_num - 1] : &_netif->ip_addr;
|
||||||
#else
|
#else
|
||||||
@ -155,14 +124,73 @@ class AddrListClass {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
netif * _netif;
|
netif * _netif;
|
||||||
int _num; // address index (0 is legacy, _num-1 is ip6_addr[]'s index)
|
int _num;
|
||||||
};
|
};
|
||||||
|
|
||||||
const const_iterator begin () const { return const_iterator(true); }
|
|
||||||
const const_iterator end () const { return const_iterator(false); }
|
|
||||||
|
class AddressListIterator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AddressListIterator(const netifWrapper &o) : netIf(o) {}
|
||||||
|
AddressListIterator(netif * netif) : netIf(netif) {}
|
||||||
|
|
||||||
|
const netifWrapper& operator* () const {return netIf;}
|
||||||
|
const netifWrapper* operator->() const {return &netIf;}
|
||||||
|
|
||||||
|
bool operator==(AddressListIterator & o) {return netIf.equal(*o);}
|
||||||
|
bool operator!=(AddressListIterator & o) {return !netIf.equal(*o);}
|
||||||
|
|
||||||
|
AddressListIterator & operator= (const AddressListIterator& o) {netIf = o.netIf; return *this; }
|
||||||
|
|
||||||
|
AddressListIterator operator++(int)
|
||||||
|
{
|
||||||
|
AddressListIterator ret = *this;
|
||||||
|
++(*this);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
AddressListIterator & operator++()
|
||||||
|
{
|
||||||
|
while (netIf._netif)
|
||||||
|
{
|
||||||
|
if (++netIf._num == IF_NUM_ADDRESSES)
|
||||||
|
{
|
||||||
|
netIf = netifWrapper(netIf._netif->next); //num is inited to -1
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!ip_addr_isany(netIf.ipFromNetifNum()))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
netifWrapper netIf;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern AddrListClass addrList;
|
|
||||||
|
|
||||||
#endif // __ADDRLIST_H
|
|
||||||
|
class AddressList
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using const_iterator = const AddressListIterator;
|
||||||
|
|
||||||
|
const_iterator begin() const {return const_iterator(netif_list);}
|
||||||
|
const_iterator end() const {return const_iterator(nullptr);}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
inline AddressList::const_iterator begin(const AddressList &a) {return a.begin();}
|
||||||
|
inline AddressList::const_iterator end(const AddressList &a) {return a.end();}
|
||||||
|
|
||||||
|
} //AddressListImplementation
|
||||||
|
|
||||||
|
} //esp8266
|
||||||
|
|
||||||
|
extern esp8266::AddressListImplementation::AddressList addrList;
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -66,16 +66,16 @@ void status(Print& out) {
|
|||||||
out.println(F("(with 'telnet <addr> or 'nc -u <addr> 23')"));
|
out.println(F("(with 'telnet <addr> or 'nc -u <addr> 23')"));
|
||||||
for (auto a : addrList) {
|
for (auto a : addrList) {
|
||||||
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
|
out.printf("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
|
||||||
a->iface().c_str(),
|
a.iface().c_str(),
|
||||||
!a->addr().isV4(),
|
!a.addr().isV4(),
|
||||||
a->addr().isLocal(),
|
a.addr().isLocal(),
|
||||||
a->hostname(),
|
a.hostname(),
|
||||||
a->addr().toString().c_str());
|
a.addr().toString().c_str());
|
||||||
|
|
||||||
if (a->isLegacy()) {
|
if (a.isLegacy()) {
|
||||||
out.printf(" / mask:%s / gw:%s",
|
out.printf(" / mask:%s / gw:%s",
|
||||||
a->netmask().toString().c_str(),
|
a.netmask().toString().c_str(),
|
||||||
a->gw().toString().c_str());
|
a.gw().toString().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
out.println();
|
out.println();
|
||||||
@ -121,9 +121,9 @@ void setup() {
|
|||||||
|
|
||||||
for (bool configured = false; !configured;) {
|
for (bool configured = false; !configured;) {
|
||||||
for (auto addr : addrList)
|
for (auto addr : addrList)
|
||||||
if ((configured = !addr->isLocal()
|
if ((configured = !addr.isLocal()
|
||||||
// && addr->isV6() // uncomment when IPv6 is mandatory
|
// && addr.isV6() // uncomment when IPv6 is mandatory
|
||||||
// && addr->ifnumber() == STATION_IF
|
// && addr.ifnumber() == STATION_IF
|
||||||
)) {
|
)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user