24
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
- a.number (),
27
+ a.ifnumber (),
28
28
a.addr().isLegacy(),
29
29
a.addr().isV4(),
30
30
a.addr().isLocal(),
65
65
for (auto iface: addrList)
66
66
if ((configured = ( !iface.addr().isV4()
67
67
&& !iface.addr().isLocal()
68
- && iface.number () == STATION_IF)))
68
+ && iface.ifnumber () == STATION_IF)))
69
69
break;
70
70
Serial.print('.');
71
71
delay(500);
@@ -94,28 +94,40 @@ 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
- 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
+ }
101
106
102
- bool equal (const netifWrapper & o)
107
+ bool equal (const netifWrapper& o)
103
108
{
104
109
return _netif == o._netif && (!_netif || _num == o._num );
105
110
}
106
111
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
119
131
{
120
132
#if LWIP_IPV6
121
133
return _num ? &_netif->ip6_addr [_num - 1 ] : &_netif->ip_addr ;
@@ -124,44 +136,57 @@ struct netifWrapper
124
136
#endif
125
137
}
126
138
139
+ // lwIP interface
140
+ netif* _netif;
127
141
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[]
129
145
int _num;
130
146
};
131
147
132
148
133
-
134
149
class AddressListIterator
135
150
{
136
151
public:
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
+ }
139
161
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; }
142
164
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); }
145
167
146
- AddressListIterator & operator = (const AddressListIterator& o) {netIf = o.netIf ; return *this ; }
168
+ AddressListIterator& operator = (const AddressListIterator& o) { netIf = o.netIf ; return *this ; }
147
169
148
- AddressListIterator operator ++(int )
170
+ AddressListIterator operator ++ (int )
149
171
{
150
172
AddressListIterator ret = *this ;
151
- ++(* this );
173
+ ( void ) operator ++();
152
174
return ret;
153
175
}
154
176
155
- AddressListIterator & operator ++()
177
+ AddressListIterator& operator ++ ()
156
178
{
157
- while (netIf._netif )
179
+ while (netIf._netif )
158
180
{
159
- if (++netIf._num == IF_NUM_ADDRESSES)
181
+ if (++netIf._num == IF_NUM_ADDRESSES)
160
182
{
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 );
162
186
continue ;
163
187
}
164
188
if (!ip_addr_isany (netIf.ipFromNetifNum ()))
189
+ // found an initialized address
165
190
break ;
166
191
}
167
192
return *this ;
@@ -171,24 +196,23 @@ class AddressListIterator
171
196
};
172
197
173
198
174
-
175
199
class AddressList
176
200
{
177
201
public:
178
202
using const_iterator = const AddressListIterator;
179
203
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 ); }
182
206
183
207
};
184
208
209
+ inline AddressList::const_iterator begin (const AddressList& a) { return a.begin (); }
210
+ inline AddressList::const_iterator end (const AddressList& a) { return a.end (); }
185
211
186
- inline AddressList::const_iterator begin (const AddressList &a) {return a.begin ();}
187
- inline AddressList::const_iterator end (const AddressList &a) {return a.end ();}
188
212
189
- } // AddressListImplementation
213
+ } // AddressListImplementation
190
214
191
- } // esp8266
215
+ } // esp8266
192
216
193
217
extern esp8266::AddressListImplementation::AddressList addrList;
194
218
0 commit comments