|
| 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 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + This class allows to explore all configured IP addresses |
| 22 | + in lwIP netifs, with that kind of c++ loop: |
| 23 | +
|
| 24 | + for (auto a: ifList) |
| 25 | + out.printf("IF='%s' index=%d legacy=%d IPv4=%d local=%d hostname='%s' addr= %s\n", |
| 26 | + a->iface().c_str(), |
| 27 | + a->number(), |
| 28 | + a->addr().isLegacy(), |
| 29 | + a->addr().isV4(), |
| 30 | + a->addr().isLocal(), |
| 31 | + a->hostname().c_str(), |
| 32 | + a->addr().toString().c_str()); |
| 33 | +
|
| 34 | + This loop: |
| 35 | +
|
| 36 | + while (WiFi.status() != WL_CONNECTED()) { |
| 37 | + Serial.print('.'); |
| 38 | + delay(500); |
| 39 | + } |
| 40 | +
|
| 41 | + can be replaced by: |
| 42 | +
|
| 43 | + for (bool configured = false; !configured; ) { |
| 44 | + for (auto iface: ifList) |
| 45 | + if ((configured = !iface->addr().isLocal()) |
| 46 | + break; |
| 47 | + Serial.print('.'); |
| 48 | + delay(500); |
| 49 | + } |
| 50 | +
|
| 51 | + waiting for an IPv6 global address: |
| 52 | +
|
| 53 | + for (bool configured = false; !configured; ) { |
| 54 | + for (auto iface: ifList) |
| 55 | + if ((configured = ( !iface->addr()->isV4() |
| 56 | + && !iface->addr().isLocal()))) |
| 57 | + break; |
| 58 | + Serial.print('.'); |
| 59 | + delay(500); |
| 60 | + } |
| 61 | +
|
| 62 | + waiting for an IPv6 global address, on a specific interface: |
| 63 | +
|
| 64 | + for (bool configured = false; !configured; ) { |
| 65 | + for (auto iface: ifList) |
| 66 | + if ((configured = ( !iface->addr()->isV4() |
| 67 | + && !iface->addr().isLocal() |
| 68 | + && iface->number() == STATION_IF))) |
| 69 | + break; |
| 70 | + Serial.print('.'); |
| 71 | + delay(500); |
| 72 | + } |
| 73 | +*/ |
| 74 | + |
| 75 | +#ifndef __ADDRLIST_H |
| 76 | +#define __ADDRLIST_H |
| 77 | + |
| 78 | +#include <IPAddress.h> |
| 79 | +#include <lwip/netif.h> |
| 80 | + |
| 81 | +#if LWIP_IPV6 |
| 82 | +#define IF_NUM_ADDRESSES (1 + LWIP_IPV6_NUM_ADDRESSES) |
| 83 | +#else |
| 84 | +#define IF_NUM_ADDRESSES (1) |
| 85 | +#endif |
| 86 | + |
| 87 | + |
| 88 | +class AddrListClass { |
| 89 | + |
| 90 | + // no member in this class |
| 91 | + // lwIP's global 'struct netif* netif_list' is used |
| 92 | + // designed to be used with 'for (auto x: ifList)' |
| 93 | + |
| 94 | + public: |
| 95 | + |
| 96 | + class const_iterator { |
| 97 | + |
| 98 | + public: |
| 99 | + |
| 100 | + // iterator operations: |
| 101 | + |
| 102 | + const_iterator (bool begin = true): _netif(begin? netif_list: nullptr), _num(-1) { ++*this; } |
| 103 | + const_iterator (const const_iterator& o): _netif(o._netif), _num(o._num) { } |
| 104 | + const_iterator& operator= (const const_iterator& o) { _netif = o._netif; _num = o._num; return *this; } |
| 105 | + |
| 106 | + bool operator!= (const const_iterator& o) { return !equal(o); } |
| 107 | + bool operator== (const const_iterator& o) { return equal(o); } |
| 108 | + |
| 109 | + const_iterator operator++(int) { |
| 110 | + const_iterator ret = *this; |
| 111 | + ++(*this); |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + const_iterator& operator++() { |
| 116 | + while (_netif) { |
| 117 | + if (++_num == IF_NUM_ADDRESSES) { |
| 118 | + _num = -1; |
| 119 | + _netif = _netif->next; |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (!ip_addr_isany(_ip_from_netif_num())) |
| 123 | + break; |
| 124 | + } |
| 125 | + return *this; |
| 126 | + } |
| 127 | + |
| 128 | + // (*iterator) emulation: |
| 129 | + |
| 130 | + const const_iterator& operator* () const { return *this; } |
| 131 | + const const_iterator* operator-> () const { return this; } |
| 132 | + |
| 133 | + bool isLegacy() const { return _num == 0; } |
| 134 | + bool isLocal() const { return addr().isLocal(); } |
| 135 | + IPAddress addr () const { return _ip_from_netif_num(); } |
| 136 | + IPAddress netmask () const { return _netif->netmask; } |
| 137 | + IPAddress gw () const { return _netif->gw; } |
| 138 | + String iface () const { return String(_netif->name[0]) + _netif->name[1]; } |
| 139 | + const char* hostname () const { return _netif->hostname?: emptyString.c_str(); } |
| 140 | + const char* mac () const { return (const char*)_netif->hwaddr; } |
| 141 | + int number () const { return _netif->num; } |
| 142 | + |
| 143 | + protected: |
| 144 | + |
| 145 | + bool equal (const const_iterator& o) { |
| 146 | + return _netif == o._netif |
| 147 | + && (!_netif || _num == o._num); |
| 148 | + } |
| 149 | + |
| 150 | + const ip_addr_t* _ip_from_netif_num () const { |
| 151 | +#if LWIP_IPV6 |
| 152 | + return _num? &_netif->ip6_addr[_num - 1]: &_netif->ip_addr; |
| 153 | +#else |
| 154 | + return &_netif->ip_addr; |
| 155 | +#endif |
| 156 | + } |
| 157 | + |
| 158 | + netif* _netif; |
| 159 | + int _num; // address index (0 is legacy, _num-1 is ip6_addr[]'s index) |
| 160 | + }; |
| 161 | + |
| 162 | + const const_iterator begin () const { return const_iterator(true); } |
| 163 | + const const_iterator end () const { return const_iterator(false); } |
| 164 | +}; |
| 165 | + |
| 166 | +extern AddrListClass addrList; |
| 167 | + |
| 168 | +#endif // __ADDRLIST_H |
0 commit comments