1
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
- */
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
19
20
20
/*
21
- This class allows to explore all configured IP addresses
22
- in lwIP netifs, with that kind of c++ loop:
21
+ This class allows to explore all configured IP addresses
22
+ in lwIP netifs, with that kind of c++ loop:
23
23
24
- for (auto a: addrList)
24
+ for (auto a: addrList)
25
25
out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
26
26
a.iface().c_str(),
27
27
a.ifnumber(),
31
31
a.hostname().c_str(),
32
32
a.addr().toString().c_str());
33
33
34
- This loop:
34
+ This loop:
35
35
36
36
while (WiFi.status() != WL_CONNECTED()) {
37
37
Serial.print('.');
38
38
delay(500);
39
39
}
40
40
41
- can be replaced by:
41
+ can be replaced by:
42
42
43
43
for (bool configured = false; !configured; ) {
44
44
for (auto iface: addrList)
48
48
delay(500);
49
49
}
50
50
51
- waiting for an IPv6 global address:
51
+ waiting for an IPv6 global address:
52
52
53
53
for (bool configured = false; !configured; ) {
54
54
for (auto iface: addrList)
59
59
delay(500);
60
60
}
61
61
62
- waiting for an IPv6 global address, on a specific interface:
62
+ waiting for an IPv6 global address, on a specific interface:
63
63
64
64
for (bool configured = false; !configured; ) {
65
65
for (auto iface: addrList)
@@ -94,8 +94,8 @@ namespace AddressListImplementation
94
94
95
95
struct netifWrapper
96
96
{
97
- netifWrapper (netif* netif) : _netif(netif), _num(-1 ) {}
98
- netifWrapper (const netifWrapper& o) : _netif(o._netif), _num(o._num) {}
97
+ netifWrapper (netif* netif) : _netif(netif), _num(-1 ) {}
98
+ netifWrapper (const netifWrapper& o) : _netif(o._netif), _num(o._num) {}
99
99
100
100
netifWrapper& operator = (const netifWrapper& o)
101
101
{
@@ -110,25 +110,64 @@ struct netifWrapper
110
110
}
111
111
112
112
// address properties
113
- IPAddress addr () const { return ipFromNetifNum (); }
114
- bool isLegacy () const { return _num == 0 ; }
115
- bool isLocal () const { return addr ().isLocal (); }
116
- bool isV4 () const { return addr ().isV4 (); }
117
- bool isV6 () const { return !addr ().isV4 (); }
118
- String toString () const { return addr ().toString (); }
113
+ IPAddress addr () const
114
+ {
115
+ return ipFromNetifNum ();
116
+ }
117
+ bool isLegacy () const
118
+ {
119
+ return _num == 0 ;
120
+ }
121
+ bool isLocal () const
122
+ {
123
+ return addr ().isLocal ();
124
+ }
125
+ bool isV4 () const
126
+ {
127
+ return addr ().isV4 ();
128
+ }
129
+ bool isV6 () const
130
+ {
131
+ return !addr ().isV4 ();
132
+ }
133
+ String toString () const
134
+ {
135
+ return addr ().toString ();
136
+ }
119
137
120
138
// related to legacy address (_num=0, ipv4)
121
- IPAddress ipv4 () const { return _netif->ip_addr ; }
122
- IPAddress netmask () const { return _netif->netmask ; }
123
- IPAddress gw () const { return _netif->gw ; }
139
+ IPAddress ipv4 () const
140
+ {
141
+ return _netif->ip_addr ;
142
+ }
143
+ IPAddress netmask () const
144
+ {
145
+ return _netif->netmask ;
146
+ }
147
+ IPAddress gw () const
148
+ {
149
+ return _netif->gw ;
150
+ }
124
151
125
152
// common to all addresses of this interface
126
- String ifname () const { return String (_netif->name [0 ]) + _netif->name [1 ]; }
127
- const char * ifhostname () const { return _netif->hostname ?: emptyString.c_str (); }
128
- const char * ifmac () const { return (const char *)_netif->hwaddr ; }
129
- int ifnumber () const { return _netif->num ; }
153
+ String ifname () const
154
+ {
155
+ return String (_netif->name [0 ]) + _netif->name [1 ];
156
+ }
157
+ const char * ifhostname () const
158
+ {
159
+ return _netif->hostname ? : emptyString.c_str ();
160
+ }
161
+ const char * ifmac () const
162
+ {
163
+ return (const char *)_netif->hwaddr ;
164
+ }
165
+ int ifnumber () const
166
+ {
167
+ return _netif->num ;
168
+ }
130
169
131
- const ip_addr_t * ipFromNetifNum () const
170
+ const ip_addr_t * ipFromNetifNum () const
132
171
{
133
172
#if LWIP_IPV6
134
173
return _num ? &_netif->ip6_addr [_num - 1 ] : &_netif->ip_addr ;
@@ -150,8 +189,8 @@ struct netifWrapper
150
189
class AddressListIterator
151
190
{
152
191
public:
153
- AddressListIterator (const netifWrapper& o) : netIf(o) {}
154
- AddressListIterator (netif* netif) : netIf(netif)
192
+ AddressListIterator (const netifWrapper& o) : netIf(o) {}
193
+ AddressListIterator (netif* netif) : netIf(netif)
155
194
{
156
195
// This constructor is called with lwIP's global netif_list, or
157
196
// nullptr. operator++() is designed to loop through _configured_
@@ -160,13 +199,29 @@ class AddressListIterator
160
199
(void )operator ++();
161
200
}
162
201
163
- const netifWrapper& operator * () const { return netIf; }
164
- const netifWrapper* operator -> () const { return &netIf; }
202
+ const netifWrapper& operator * () const
203
+ {
204
+ return netIf;
205
+ }
206
+ const netifWrapper* operator -> () const
207
+ {
208
+ return &netIf;
209
+ }
165
210
166
- bool operator == (AddressListIterator& o) { return netIf.equal (*o); }
167
- bool operator != (AddressListIterator& o) { return !netIf.equal (*o); }
211
+ bool operator == (AddressListIterator& o)
212
+ {
213
+ return netIf.equal (*o);
214
+ }
215
+ bool operator != (AddressListIterator& o)
216
+ {
217
+ return !netIf.equal (*o);
218
+ }
168
219
169
- AddressListIterator& operator = (const AddressListIterator& o) { netIf = o.netIf ; return *this ; }
220
+ AddressListIterator& operator = (const AddressListIterator& o)
221
+ {
222
+ netIf = o.netIf ;
223
+ return *this ;
224
+ }
170
225
171
226
AddressListIterator operator ++ (int )
172
227
{
@@ -188,7 +243,9 @@ class AddressListIterator
188
243
}
189
244
if (!ip_addr_isany (netIf.ipFromNetifNum ()))
190
245
// found an initialized address
246
+ {
191
247
break ;
248
+ }
192
249
}
193
250
return *this ;
194
251
}
@@ -200,15 +257,27 @@ class AddressListIterator
200
257
class AddressList
201
258
{
202
259
public:
203
- using const_iterator = const AddressListIterator;
260
+ using const_iterator = const AddressListIterator;
204
261
205
- const_iterator begin () const { return const_iterator (netif_list); }
206
- const_iterator end () const { return const_iterator (nullptr ); }
262
+ const_iterator begin () const
263
+ {
264
+ return const_iterator (netif_list);
265
+ }
266
+ const_iterator end () const
267
+ {
268
+ return const_iterator (nullptr );
269
+ }
207
270
208
271
};
209
272
210
- inline AddressList::const_iterator begin (const AddressList& a) { return a.begin (); }
211
- inline AddressList::const_iterator end (const AddressList& a) { return a.end (); }
273
+ inline AddressList::const_iterator begin (const AddressList& a)
274
+ {
275
+ return a.begin ();
276
+ }
277
+ inline AddressList::const_iterator end (const AddressList& a)
278
+ {
279
+ return a.end ();
280
+ }
212
281
213
282
214
283
} // AddressListImplementation
0 commit comments