forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddrList.h
168 lines (131 loc) · 5.58 KB
/
AddrList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
AddrList.h - cycle through lwIP netif's ip addresses like a c++ list
Copyright (c) 2018 david gauchard. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
This class allows to explore all configured IP addresses
in lwIP netifs, with that kind of c++ loop:
for (auto a: ifList)
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
a->iface().c_str(),
a->number(),
a->addr().isLegacy(),
a->addr().isV4(),
a->addr().isLocal(),
a->hostname().c_str(),
a->addr().toString().c_str());
This loop:
while (WiFi.status() != WL_CONNECTED()) {
Serial.print('.');
delay(500);
}
can be replaced by:
for (bool configured = false; !configured; ) {
for (auto iface: ifList)
if ((configured = !iface->addr().isLocal())
break;
Serial.print('.');
delay(500);
}
waiting for an IPv6 global address:
for (bool configured = false; !configured; ) {
for (auto iface: ifList)
if ((configured = ( !iface->addr()->isV4()
&& !iface->addr().isLocal())))
break;
Serial.print('.');
delay(500);
}
waiting for an IPv6 global address, on a specific interface:
for (bool configured = false; !configured; ) {
for (auto iface: ifList)
if ((configured = ( !iface->addr()->isV4()
&& !iface->addr().isLocal()
&& iface->number() == STATION_IF)))
break;
Serial.print('.');
delay(500);
}
*/
#ifndef __ADDRLIST_H
#define __ADDRLIST_H
#include <IPAddress.h>
#include <lwip/netif.h>
#if LWIP_IPV6
#define IF_NUM_ADDRESSES (1 + LWIP_IPV6_NUM_ADDRESSES)
#else
#define IF_NUM_ADDRESSES (1)
#endif
class AddrListClass {
// no member in this class
// lwIP's global 'struct netif* netif_list' is used
// designed to be used with 'for (auto x: ifList)'
public:
class const_iterator {
public:
// iterator operations:
const_iterator (bool begin = true): _netif(begin? netif_list: nullptr), _num(-1) { ++*this; }
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 isLocal() const { return addr().isLocal(); }
IPAddress addr () const { return _ip_from_netif_num(); }
IPAddress netmask () const { return _netif->netmask; }
IPAddress gw () const { return _netif->gw; }
String iface () const { return String(_netif->name[0]) + _netif->name[1]; }
const char* hostname () const { return _netif->hostname?: emptyString.c_str(); }
const char* mac () const { return (const char*)_netif->hwaddr; }
int number () const { return _netif->num; }
protected:
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
return _num? &_netif->ip6_addr[_num - 1]: &_netif->ip_addr;
#else
return &_netif->ip_addr;
#endif
}
netif* _netif;
int _num; // address index (0 is legacy, _num-1 is ip6_addr[]'s index)
};
const const_iterator begin () const { return const_iterator(true); }
const const_iterator end () const { return const_iterator(false); }
};
extern AddrListClass addrList;
#endif // __ADDRLIST_H