lokinet/llarp/dns_iptracker.cpp

194 lines
5.2 KiB
C++
Raw Normal View History

2018-09-24 13:09:01 +00:00
#include <llarp/dns_iptracker.hpp>
2018-08-08 12:41:09 +00:00
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;
llarp::LogInfo("We used 10.x.x.x? ",
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
*/
// disable all possibilities unless you setup a tunGateway
g_dns_iptracker.used_privates.ten = true;
g_dns_iptracker.used_privates.oneSeven = true;
g_dns_iptracker.used_privates.oneNine = true;
2018-08-08 12:41:09 +00:00
}
// not sure we want tunGatewayIP... we'll know when we get further
bool
dns_iptracker_setup_dotLokiLookup(dotLokiLookup *dll, llarp::Addr tunGatewayIp)
{
dll->ip_tracker = &g_dns_iptracker;
return true;
}
2018-09-23 16:59:13 +00:00
// FIXME: pass in b32addr of client
bool
dns_iptracker_setup(llarp::Addr tunGatewayIp)
{
struct in_addr *addr = tunGatewayIp.addr4();
unsigned char *ip = (unsigned char *)&(addr->s_addr);
2018-09-23 16:59:13 +00:00
llarp::LogInfo("iptracker setup: (", std::to_string(ip[0]), ").[",
std::to_string(ip[1]), '.', std::to_string(ip[2]), "].",
std::to_string(ip[3]));
std::unique_ptr< ip_range > range(new ip_range);
range->octet2 = ip[1]; // 2nd octet
range->octet3 = ip[2]; // 3rd octet
// FIXME: look up any static mappings to discount
range->left = 252;
// 4th octet, probably 1, set it
struct dns_pointer *result = new dns_pointer;
result->hostResult = tunGatewayIp;
// tunGatewayIp.CopyInto(result->hostResult);
2018-09-23 16:59:13 +00:00
// result->b32addr = ; // FIXME: should be our HS addr
range->used[ip[3]] = result; // claim tun IP
// save tun range in tracker
// FIXME: forcing one and only one range
if(ip[0] == 10)
{
g_dns_iptracker.used_ten_ips.push_back(std::move(range));
g_dns_iptracker.used_privates.ten = false;
}
else if(ip[0] == 172)
{
g_dns_iptracker.used_seven_ips.push_back(std::move(range));
g_dns_iptracker.used_privates.oneSeven = false;
}
else if(ip[0] == 192)
{
g_dns_iptracker.used_nine_ips.push_back(std::move(range));
g_dns_iptracker.used_privates.oneNine = false;
}
else
{
return false;
}
return true;
}
2018-08-08 12:41:09 +00:00
inline struct dns_pointer *
dns_iptracker_allocate_range(std::unique_ptr< 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); // why plus 2? to start at .2
2018-08-08 13:10:02 +00:00
llarp::LogDebug("Allocated ", ip);
// result->hostResult = new sockaddr;
// ip.CopyInto(result->hostResult);
result->hostResult = ip;
2018-08-08 13:10:02 +00:00
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< std::unique_ptr< 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
std::unique_ptr< ip_range > new_range(new ip_range);
2018-08-08 12:41:09 +00:00
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
ranges.push_back(std::move(new_range));
2018-08-08 12:41:09 +00:00
// 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()
{
return dns_iptracker_get_free(&g_dns_iptracker);
}
struct dns_pointer *
dns_iptracker_get_free(dns_iptracker *iptracker)
2018-08-08 12:41:09 +00:00
{
2018-08-09 11:28:55 +00:00
llarp::LogInfo("We used 10.x.x.x? ",
iptracker->used_privates.ten ? "Yes" : "No");
if(!iptracker->used_privates.ten)
2018-08-08 12:41:09 +00:00
{
struct dns_pointer *test =
dns_iptracker_check_range(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? ",
iptracker->used_privates.oneSeven ? "Yes" : "No");
if(!iptracker->used_privates.oneSeven)
2018-08-08 12:41:09 +00:00
{
struct dns_pointer *test =
dns_iptracker_check_range(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? ",
iptracker->used_privates.oneNine ? "Yes" : "No");
if(!iptracker->used_privates.oneNine)
2018-08-08 12:41:09 +00:00
{
struct dns_pointer *test =
dns_iptracker_check_range(iptracker->used_nine_ips, 192);
2018-08-08 12:41:09 +00:00
if(test)
{
return test;
}
}
return nullptr;
}