lokinet/llarp/dns_iptracker.cpp

123 lines
3.2 KiB
C++
Raw Normal View History

2018-08-08 12:41:09 +00:00
#include "dns_iptracker.hpp" //
#include "logger.hpp"
dns_iptracker g_dns_iptracker;
void
dns_iptracker_init()
{
2018-08-09 11:28:55 +00:00
g_dns_iptracker.interfaces = llarp_getPrivateIfs();
llarp::LogInfo("Interface uses 10.x.x.x? ",
g_dns_iptracker.interfaces.ten ? "Yes" : "No");
2018-08-08 12:41:09 +00:00
g_dns_iptracker.used_privates = g_dns_iptracker.interfaces;
2018-08-09 11:28:55 +00:00
llarp::LogInfo("We used 10.x.x.x? ",
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
2018-08-08 12:41:09 +00:00
}
inline struct dns_pointer *
dns_iptracker_allocate_range(struct ip_range *range, uint8_t first)
2018-08-08 12:41:09 +00:00
{
// we have an IP
2018-08-08 13:10:02 +00:00
llarp::LogDebug("Range has ", (unsigned int)range->left, " ips left");
2018-08-08 12:41:09 +00:00
range->left--; // use it up
struct dns_pointer *result = new dns_pointer;
llarp::Addr ip(first, range->octet2, range->octet3, range->left + 2);
2018-08-08 13:10:02 +00:00
llarp::LogDebug("Allocated ", ip);
2018-08-08 12:41:09 +00:00
result->hostResult = new sockaddr;
2018-08-08 13:10:02 +00:00
ip.CopyInto(result->hostResult);
2018-08-08 12:41:09 +00:00
// 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)
2018-08-08 12:41:09 +00:00
{
// 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);
2018-08-08 12:41:09 +00:00
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;
2018-08-09 11:28:55 +00:00
switch(first)
{
case 172:
{
// FIXME: goes up to 31...
new_range->octet2 = 16;
break;
}
case 192:
{
new_range->octet2 = 168;
break;
}
}
2018-08-09 11:28:55 +00:00
new_range->octet3 = 0; // FIXME: counter (0-255)
// CHECK: planning a /24 but maybe that's too wide for broadcasts
2018-08-09 11:28:55 +00:00
new_range->left = 252; // 0 is net, 1 is gw, 255 is broadcast
2018-08-08 12:41:09 +00:00
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);
2018-08-08 12:41:09 +00:00
}
return nullptr;
}
struct dns_pointer *
dns_iptracker_get_free()
{
2018-08-09 11:28:55 +00:00
llarp::LogInfo("We used 10.x.x.x? ",
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
2018-08-08 12:41:09 +00:00
if(!g_dns_iptracker.used_privates.ten)
{
struct dns_pointer *test =
dns_iptracker_check_range(g_dns_iptracker.used_ten_ips, 10);
2018-08-08 12:41:09 +00:00
if(test)
{
return test;
}
}
2018-08-09 11:28:55 +00:00
llarp::LogInfo("We used 172.16.x.x? ",
g_dns_iptracker.used_privates.oneSeven ? "Yes" : "No");
2018-08-08 12:41:09 +00:00
if(!g_dns_iptracker.used_privates.oneSeven)
{
struct dns_pointer *test =
dns_iptracker_check_range(g_dns_iptracker.used_seven_ips, 172);
2018-08-08 12:41:09 +00:00
if(test)
{
return test;
}
}
2018-08-09 11:28:55 +00:00
llarp::LogInfo("We used 192.168.x.x? ",
g_dns_iptracker.used_privates.oneNine ? "Yes" : "No");
2018-08-08 12:41:09 +00:00
if(!g_dns_iptracker.used_privates.oneNine)
{
struct dns_pointer *test =
dns_iptracker_check_range(g_dns_iptracker.used_nine_ips, 192);
2018-08-08 12:41:09 +00:00
if(test)
{
return test;
}
}
return nullptr;
}