mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-29 11:05:43 +00:00
123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
#include "dns_iptracker.hpp" //
|
|
#include "logger.hpp"
|
|
|
|
dns_iptracker g_dns_iptracker;
|
|
|
|
void
|
|
dns_iptracker_init()
|
|
{
|
|
g_dns_iptracker.interfaces = llarp_getPrivateIfs();
|
|
llarp::LogInfo("Interface uses 10.x.x.x? ",
|
|
g_dns_iptracker.interfaces.ten ? "Yes" : "No");
|
|
g_dns_iptracker.used_privates = g_dns_iptracker.interfaces;
|
|
llarp::LogInfo("We used 10.x.x.x? ",
|
|
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
|
|
}
|
|
|
|
inline struct dns_pointer *
|
|
dns_iptracker_allocate_range(struct ip_range *range, uint8_t first)
|
|
{
|
|
// we have an IP
|
|
llarp::LogDebug("Range has ", (unsigned int)range->left, " ips left");
|
|
range->left--; // use it up
|
|
struct dns_pointer *result = new dns_pointer;
|
|
llarp::Addr ip(first, range->octet2, range->octet3, range->left + 2);
|
|
llarp::LogDebug("Allocated ", ip);
|
|
result->hostResult = new sockaddr;
|
|
ip.CopyInto(result->hostResult);
|
|
|
|
// make an address and place into this sockaddr
|
|
range->used[range->left + 2] = result;
|
|
return result;
|
|
}
|
|
|
|
struct dns_pointer *
|
|
dns_iptracker_check_range(std::vector< ip_range * > &ranges, uint8_t first)
|
|
{
|
|
// tens not all used up
|
|
if(ranges.size())
|
|
{
|
|
// FIXME: maybe find_if where left not 0
|
|
// find a range
|
|
for(auto it = ranges.begin(); it != ranges.end(); ++it)
|
|
{
|
|
if((*it)->left)
|
|
{
|
|
struct dns_pointer *result = dns_iptracker_allocate_range(*it, first);
|
|
if(!(*it)->left)
|
|
{
|
|
// all used up
|
|
// FIXME: are there any more octets available?
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// create one
|
|
auto new_range = new ip_range;
|
|
new_range->octet2 = 0;
|
|
switch(first)
|
|
{
|
|
case 172:
|
|
{
|
|
// FIXME: goes up to 31...
|
|
new_range->octet2 = 16;
|
|
break;
|
|
}
|
|
case 192:
|
|
{
|
|
new_range->octet2 = 168;
|
|
break;
|
|
}
|
|
}
|
|
new_range->octet3 = 0; // FIXME: counter (0-255)
|
|
// CHECK: planning a /24 but maybe that's too wide for broadcasts
|
|
new_range->left = 252; // 0 is net, 1 is gw, 255 is broadcast
|
|
ranges.push_back(new_range);
|
|
// don't need to check if we're out since this is fresh range
|
|
return dns_iptracker_allocate_range(new_range, first);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
struct dns_pointer *
|
|
dns_iptracker_get_free()
|
|
{
|
|
llarp::LogInfo("We used 10.x.x.x? ",
|
|
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
|
|
if(!g_dns_iptracker.used_privates.ten)
|
|
{
|
|
struct dns_pointer *test =
|
|
dns_iptracker_check_range(g_dns_iptracker.used_ten_ips, 10);
|
|
if(test)
|
|
{
|
|
return test;
|
|
}
|
|
}
|
|
llarp::LogInfo("We used 172.16.x.x? ",
|
|
g_dns_iptracker.used_privates.oneSeven ? "Yes" : "No");
|
|
if(!g_dns_iptracker.used_privates.oneSeven)
|
|
{
|
|
struct dns_pointer *test =
|
|
dns_iptracker_check_range(g_dns_iptracker.used_seven_ips, 172);
|
|
if(test)
|
|
{
|
|
return test;
|
|
}
|
|
}
|
|
llarp::LogInfo("We used 192.168.x.x? ",
|
|
g_dns_iptracker.used_privates.oneNine ? "Yes" : "No");
|
|
if(!g_dns_iptracker.used_privates.oneNine)
|
|
{
|
|
struct dns_pointer *test =
|
|
dns_iptracker_check_range(g_dns_iptracker.used_nine_ips, 192);
|
|
if(test)
|
|
{
|
|
return test;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|