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,64 +110,25 @@ struct netifWrapper
110
110
}
111
111
112
112
// address properties
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
- }
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 (); }
137
119
138
120
// related to legacy address (_num=0, ipv4)
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
- }
121
+ IPAddress ipv4 () const { return _netif->ip_addr ; }
122
+ IPAddress netmask () const { return _netif->netmask ; }
123
+ IPAddress gw () const { return _netif->gw ; }
151
124
152
125
// common to all addresses of this interface
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
- }
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 ; }
169
130
170
- const ip_addr_t * ipFromNetifNum () const
131
+ const ip_addr_t * ipFromNetifNum () const
171
132
{
172
133
#if LWIP_IPV6
173
134
return _num ? &_netif->ip6_addr [_num - 1 ] : &_netif->ip_addr ;
@@ -189,8 +150,8 @@ struct netifWrapper
189
150
class AddressListIterator
190
151
{
191
152
public:
192
- AddressListIterator (const netifWrapper& o) : netIf(o) {}
193
- AddressListIterator (netif* netif) : netIf(netif)
153
+ AddressListIterator (const netifWrapper& o) : netIf(o) {}
154
+ AddressListIterator (netif* netif) : netIf(netif)
194
155
{
195
156
// This constructor is called with lwIP's global netif_list, or
196
157
// nullptr. operator++() is designed to loop through _configured_
@@ -199,29 +160,13 @@ class AddressListIterator
199
160
(void )operator ++();
200
161
}
201
162
202
- const netifWrapper& operator * () const
203
- {
204
- return netIf;
205
- }
206
- const netifWrapper* operator -> () const
207
- {
208
- return &netIf;
209
- }
163
+ const netifWrapper& operator * () const { return netIf; }
164
+ const netifWrapper* operator -> () const { return &netIf; }
210
165
211
- bool operator == (AddressListIterator& o)
212
- {
213
- return netIf.equal (*o);
214
- }
215
- bool operator != (AddressListIterator& o)
216
- {
217
- return !netIf.equal (*o);
218
- }
166
+ bool operator == (AddressListIterator& o) { return netIf.equal (*o); }
167
+ bool operator != (AddressListIterator& o) { return !netIf.equal (*o); }
219
168
220
- AddressListIterator& operator = (const AddressListIterator& o)
221
- {
222
- netIf = o.netIf ;
223
- return *this ;
224
- }
169
+ AddressListIterator& operator = (const AddressListIterator& o) { netIf = o.netIf ; return *this ; }
225
170
226
171
AddressListIterator operator ++ (int )
227
172
{
@@ -243,9 +188,7 @@ class AddressListIterator
243
188
}
244
189
if (!ip_addr_isany (netIf.ipFromNetifNum ()))
245
190
// found an initialized address
246
- {
247
191
break ;
248
- }
249
192
}
250
193
return *this ;
251
194
}
@@ -257,27 +200,15 @@ class AddressListIterator
257
200
class AddressList
258
201
{
259
202
public:
260
- using const_iterator = const AddressListIterator;
203
+ using const_iterator = const AddressListIterator;
261
204
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
- }
205
+ const_iterator begin () const { return const_iterator (netif_list); }
206
+ const_iterator end () const { return const_iterator (nullptr ); }
270
207
271
208
};
272
209
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
- }
210
+ inline AddressList::const_iterator begin (const AddressList& a) { return a.begin (); }
211
+ inline AddressList::const_iterator end (const AddressList& a) { return a.end (); }
281
212
282
213
283
214
} // AddressListImplementation
0 commit comments