Skip to content

Commit 1c3af37

Browse files
committed
net: create GetNetworkNames()
1 parent b45eae4 commit 1c3af37

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/netaddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static constexpr int ADDRV2_FORMAT = 0x20000000;
3636
* @note An address may belong to more than one network, for example `10.0.0.1`
3737
* belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
3838
* Keep these sequential starting from 0 and `NET_MAX` as the last entry.
39-
* We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate
39+
* We have loops like `for (int i = 0; i < NET_MAX; ++i)` that expect to iterate
4040
* over all enum values and also `GetExtNetwork()` "extends" this enum by
4141
* introducing standalone constants starting from `NET_MAX`.
4242
*/

src/netbase.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ std::string GetNetworkName(enum Network net)
6868
assert(false);
6969
}
7070

71+
std::vector<std::string> GetNetworkNames(bool append_unroutable)
72+
{
73+
std::vector<std::string> names;
74+
for (int n = 0; n < NET_MAX; ++n) {
75+
const enum Network network{static_cast<Network>(n)};
76+
if (network == NET_UNROUTABLE || network == NET_I2P || network == NET_CJDNS || network == NET_INTERNAL) continue;
77+
names.emplace_back(GetNetworkName(network));
78+
}
79+
if (append_unroutable) {
80+
names.emplace_back(GetNetworkName(NET_UNROUTABLE));
81+
}
82+
return names;
83+
}
84+
7185
bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
7286
{
7387
vIP.clear();

src/netbase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class proxyType
3939

4040
enum Network ParseNetwork(const std::string& net);
4141
std::string GetNetworkName(enum Network net);
42+
/** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
43+
std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
4244
bool SetProxy(enum Network net, const proxyType &addrProxy);
4345
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
4446
bool IsProxy(const CNetAddr &addr);

0 commit comments

Comments
 (0)