11/*
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+ */
1919
2020/*
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:
2323
24- for (auto a: addrList)
24+ for (auto a: addrList)
2525 out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n",
2626 a.iface().c_str(),
2727 a.ifnumber(),
3131 a.hostname().c_str(),
3232 a.addr().toString().c_str());
3333
34- This loop:
34+ This loop:
3535
3636 while (WiFi.status() != WL_CONNECTED()) {
3737 Serial.print('.');
3838 delay(500);
3939 }
4040
41- can be replaced by:
41+ can be replaced by:
4242
4343 for (bool configured = false; !configured; ) {
4444 for (auto iface: addrList)
4848 delay(500);
4949 }
5050
51- waiting for an IPv6 global address:
51+ waiting for an IPv6 global address:
5252
5353 for (bool configured = false; !configured; ) {
5454 for (auto iface: addrList)
5959 delay(500);
6060 }
6161
62- waiting for an IPv6 global address, on a specific interface:
62+ waiting for an IPv6 global address, on a specific interface:
6363
6464 for (bool configured = false; !configured; ) {
6565 for (auto iface: addrList)
@@ -94,8 +94,8 @@ namespace AddressListImplementation
9494
9595struct netifWrapper
9696{
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) {}
9999
100100 netifWrapper& operator = (const netifWrapper& o)
101101 {
@@ -110,25 +110,64 @@ struct netifWrapper
110110 }
111111
112112 // 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+ }
119137
120138 // 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+ }
124151
125152 // 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+ }
130169
131- const ip_addr_t * ipFromNetifNum () const
170+ const ip_addr_t * ipFromNetifNum () const
132171 {
133172#if LWIP_IPV6
134173 return _num ? &_netif->ip6_addr [_num - 1 ] : &_netif->ip_addr ;
@@ -150,8 +189,8 @@ struct netifWrapper
150189class AddressListIterator
151190{
152191public:
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)
155194 {
156195 // This constructor is called with lwIP's global netif_list, or
157196 // nullptr. operator++() is designed to loop through _configured_
@@ -160,13 +199,29 @@ class AddressListIterator
160199 (void )operator ++();
161200 }
162201
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+ }
165210
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+ }
168219
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+ }
170225
171226 AddressListIterator operator ++ (int )
172227 {
@@ -188,7 +243,9 @@ class AddressListIterator
188243 }
189244 if (!ip_addr_isany (netIf.ipFromNetifNum ()))
190245 // found an initialized address
246+ {
191247 break ;
248+ }
192249 }
193250 return *this ;
194251 }
@@ -200,15 +257,27 @@ class AddressListIterator
200257class AddressList
201258{
202259public:
203- using const_iterator = const AddressListIterator;
260+ using const_iterator = const AddressListIterator;
204261
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+ }
207270
208271};
209272
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+ }
212281
213282
214283} // AddressListImplementation
0 commit comments