2424 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(),
27- a.number (),
27+ a.ifnumber (),
2828 a.addr().isLegacy(),
2929 a.addr().isV4(),
3030 a.addr().isLocal(),
6565 for (auto iface: addrList)
6666 if ((configured = ( !iface.addr().isV4()
6767 && !iface.addr().isLocal()
68- && iface.number () == STATION_IF)))
68+ && iface.ifnumber () == STATION_IF)))
6969 break;
7070 Serial.print('.');
7171 delay(500);
@@ -94,28 +94,40 @@ 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
100- netifWrapper& operator =(const netifWrapper & o) {_netif = o._netif ; _num = o._num ; return *this ;}
100+ netifWrapper& operator = (const netifWrapper& o)
101+ {
102+ _netif = o._netif ;
103+ _num = o._num ;
104+ return *this ;
105+ }
101106
102- bool equal (const netifWrapper & o)
107+ bool equal (const netifWrapper& o)
103108 {
104109 return _netif == o._netif && (!_netif || _num == o._num );
105110 }
106111
107-
108- bool isLegacy () const { return _num == 0 ; }
109- bool isLocal () const { return addr ().isLocal (); }
110- IPAddress addr () const { return ipFromNetifNum (); }
111- IPAddress netmask () const { return _netif->netmask ; }
112- IPAddress gw () const { return _netif->gw ; }
113- String iface () const { return String (_netif->name [0 ]) + _netif->name [1 ]; }
114- const char * hostname () const { return _netif->hostname ?: emptyString.c_str (); }
115- const char * mac () const { return (const char *)_netif->hwaddr ; }
116- int number () const { return _netif->num ; }
117-
118- const ip_addr_t * ipFromNetifNum () const
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 (); }
119+
120+ // related to legacy address (_num=0, ipv4)
121+ IPAddress netmask () const { return _netif->netmask ; }
122+ IPAddress gw () const { return _netif->gw ; }
123+
124+ // common to all addresses of this interface
125+ String ifname () const { return String (_netif->name [0 ]) + _netif->name [1 ]; }
126+ const char * ifhostname () const { return _netif->hostname ?: emptyString.c_str (); }
127+ const char * ifmac () const { return (const char *)_netif->hwaddr ; }
128+ int ifnumber () const { return _netif->num ; }
129+
130+ const ip_addr_t * ipFromNetifNum () const
119131 {
120132#if LWIP_IPV6
121133 return _num ? &_netif->ip6_addr [_num - 1 ] : &_netif->ip_addr ;
@@ -124,44 +136,57 @@ struct netifWrapper
124136#endif
125137 }
126138
139+ // lwIP interface
140+ netif* _netif;
127141
128- netif * _netif;
142+ // address index within interface
143+ // 0: legacy address (IPv4)
144+ // n>0: (_num-1) is IPv6 index for netif->ip6_addr[]
129145 int _num;
130146};
131147
132148
133-
134149class AddressListIterator
135150{
136151public:
137- AddressListIterator (const netifWrapper &o) : netIf(o) {}
138- AddressListIterator (netif * netif) : netIf(netif) {}
152+ AddressListIterator (const netifWrapper& o) : netIf(o) {}
153+ AddressListIterator (netif* netif) : netIf(netif)
154+ {
155+ // This constructor is called with lwIP's global netif_list, or
156+ // nullptr. operator++() is designed to loop through _configured_
157+ // addresses. That's why netIf's _num is initialized to -1 to allow
158+ // returning the first usable address to AddressList::begin().
159+ (void )operator ++();
160+ }
139161
140- const netifWrapper& operator * () const {return netIf;}
141- const netifWrapper* operator ->() const {return &netIf;}
162+ const netifWrapper& operator * () const { return netIf; }
163+ const netifWrapper* operator -> () const { return &netIf; }
142164
143- bool operator ==(AddressListIterator & o) {return netIf.equal (*o);}
144- bool operator !=(AddressListIterator & o) {return !netIf.equal (*o);}
165+ bool operator == (AddressListIterator& o) { return netIf.equal (*o); }
166+ bool operator != (AddressListIterator& o) { return !netIf.equal (*o); }
145167
146- AddressListIterator & operator = (const AddressListIterator& o) {netIf = o.netIf ; return *this ; }
168+ AddressListIterator& operator = (const AddressListIterator& o) { netIf = o.netIf ; return *this ; }
147169
148- AddressListIterator operator ++(int )
170+ AddressListIterator operator ++ (int )
149171 {
150172 AddressListIterator ret = *this ;
151- ++(* this );
173+ ( void ) operator ++();
152174 return ret;
153175 }
154176
155- AddressListIterator & operator ++()
177+ AddressListIterator& operator ++ ()
156178 {
157- while (netIf._netif )
179+ while (netIf._netif )
158180 {
159- if (++netIf._num == IF_NUM_ADDRESSES)
181+ if (++netIf._num == IF_NUM_ADDRESSES)
160182 {
161- netIf = netifWrapper (netIf._netif ->next ); // num is inited to -1
183+ // all addresses from current interface were iterated,
184+ // switching to next interface
185+ netIf = netifWrapper (netIf._netif ->next );
162186 continue ;
163187 }
164188 if (!ip_addr_isany (netIf.ipFromNetifNum ()))
189+ // found an initialized address
165190 break ;
166191 }
167192 return *this ;
@@ -171,24 +196,23 @@ class AddressListIterator
171196};
172197
173198
174-
175199class AddressList
176200{
177201public:
178202 using const_iterator = const AddressListIterator;
179203
180- const_iterator begin () const {return const_iterator (netif_list);}
181- const_iterator end () const {return const_iterator (nullptr );}
204+ const_iterator begin () const { return const_iterator (netif_list); }
205+ const_iterator end () const { return const_iterator (nullptr ); }
182206
183207};
184208
209+ inline AddressList::const_iterator begin (const AddressList& a) { return a.begin (); }
210+ inline AddressList::const_iterator end (const AddressList& a) { return a.end (); }
185211
186- inline AddressList::const_iterator begin (const AddressList &a) {return a.begin ();}
187- inline AddressList::const_iterator end (const AddressList &a) {return a.end ();}
188212
189- } // AddressListImplementation
213+ } // AddressListImplementation
190214
191- } // esp8266
215+ } // esp8266
192216
193217extern esp8266::AddressListImplementation::AddressList addrList;
194218
0 commit comments