DRY private range selection; add missing ranges

DRY a chunk of repeated code for finding a free private range.

Also fix it so that it will consider 10.255.0.1/16 and 192.168.255.1/24
(previously it would only check up to octet 254).
pull/2008/head
Jason Rhinelander 2 years ago
parent d10c4b9d17
commit fe0f916a09
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

@ -101,4 +101,25 @@ namespace llarp
return netmask_bits.ToString();
}
std::optional<IPRange>
IPRange::FindPrivateRange(const std::list<IPRange>& excluding)
{
auto good = [&excluding](const IPRange& range) -> bool {
for (const auto& ex : excluding)
if (ex * range)
return false;
return true;
};
for (int oct = 16; oct <= 31; ++oct)
if (auto range = IPRange::FromIPv4(172, oct, 0, 1, 16); good(range))
return range;
for (int oct = 0; oct <= 255; ++oct)
if (auto range = IPRange::FromIPv4(10, oct, 0, 1, 16); good(range))
return range;
for (int oct = 0; oct <= 255; ++oct)
if (auto range = IPRange::FromIPv4(192, 168, oct, 1, 24); good(range))
return range;
return std::nullopt;
}
} // namespace llarp

@ -1,10 +1,13 @@
#pragma once
#include <ostream>
#include "ip.hpp"
#include "net_bits.hpp"
#include <llarp/util/bits.hpp>
#include <llarp/util/buffer.hpp>
#include <llarp/util/types.hpp>
#include <list>
#include <optional>
#include <string>
namespace llarp
@ -144,6 +147,10 @@ namespace llarp
bool
BDecode(llarp_buffer_t* buf);
/// Finds a free private use range not overlapping the given ranges.
static std::optional<IPRange>
FindPrivateRange(const std::list<IPRange>& excluding);
};
template <>

@ -90,35 +90,7 @@ namespace llarp::net
}
});
auto ownsRange = [&currentRanges](const IPRange& range) -> bool {
for (const auto& ownRange : currentRanges)
{
if (ownRange * range)
return true;
}
return false;
};
// generate possible ranges to in order of attempts
std::list<IPRange> possibleRanges;
for (byte_t oct = 16; oct < 32; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(172, oct, 0, 1, 16));
}
for (byte_t oct = 0; oct < 255; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(10, oct, 0, 1, 16));
}
for (byte_t oct = 0; oct < 255; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(192, 168, oct, 1, 24));
}
// for each possible range pick the first one we don't own
for (const auto& range : possibleRanges)
{
if (not ownsRange(range))
return range;
}
return std::nullopt;
return IPRange::FindPrivateRange(currentRanges);
}
std::optional<int> GetInterfaceIndex(ipaddr_t) const override

@ -151,35 +151,7 @@ namespace llarp::net
}
});
auto ownsRange = [&currentRanges](const IPRange& range) -> bool {
for (const auto& ownRange : currentRanges)
{
if (ownRange * range)
return true;
}
return false;
};
// generate possible ranges to in order of attempts
std::list<IPRange> possibleRanges;
for (byte_t oct = 16; oct < 32; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(172, oct, 0, 1, 16));
}
for (byte_t oct = 0; oct < 255; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(10, oct, 0, 1, 16));
}
for (byte_t oct = 0; oct < 255; ++oct)
{
possibleRanges.emplace_back(IPRange::FromIPv4(192, 168, oct, 1, 24));
}
// for each possible range pick the first one we don't own
for (const auto& range : possibleRanges)
{
if (not ownsRange(range))
return range;
}
return std::nullopt;
return IPRange::FindPrivateRange(currentRanges);
}
std::string

Loading…
Cancel
Save