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-09-20 12:36:11 +00:00
|
|
|
/*
|
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-09-21 12:53:45 +00:00
|
|
|
llarp::LogInfo("We used 10.x.x.x? ",
|
|
|
|
g_dns_iptracker.used_privates.ten ? "Yes" : "No");
|
2018-09-20 12:36:11 +00:00
|
|
|
*/
|
|
|
|
|
2018-09-21 12:53:45 +00:00
|
|
|
// disable all possibilities unless you setup a tunGateway
|
2018-09-22 10:24:32 +00:00
|
|
|
|
2018-10-09 11:02:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-22 10:24:32 +00:00
|
|
|
// not sure we want tunGatewayIP... we'll know when we get further
|
2018-09-20 12:36:11 +00:00
|
|
|
bool
|
2018-11-03 13:19:18 +00:00
|
|
|
dns_iptracker_setup_dotLokiLookup(dotLokiLookup *dll,
|
2018-11-07 15:30:22 +00:00
|
|
|
__attribute__((unused))
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::huint32_t tunGatewayIp)
|
2018-09-20 12:36:11 +00:00
|
|
|
{
|
2018-09-22 10:24:32 +00:00
|
|
|
dll->ip_tracker = &g_dns_iptracker;
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-20 12:36:11 +00:00
|
|
|
|
2018-09-23 16:59:13 +00:00
|
|
|
// FIXME: pass in b32addr of client
|
2018-09-22 10:24:32 +00:00
|
|
|
bool
|
2018-11-03 13:19:18 +00:00
|
|
|
dns_iptracker_setup(dns_iptracker *iptracker, llarp::huint32_t tunGatewayIp)
|
2018-09-22 10:24:32 +00:00
|
|
|
{
|
2018-10-04 14:10:09 +00:00
|
|
|
if(!iptracker)
|
|
|
|
iptracker = &g_dns_iptracker; // FIXME: god forgive I'm tired
|
2018-11-03 13:19:18 +00:00
|
|
|
// struct in_addr *addr = tunGatewayIp.addr4();
|
|
|
|
// unsigned char *ip = (unsigned char *)&(addr->s_addr);
|
|
|
|
unsigned char *ip = (unsigned char *)&(tunGatewayIp.h);
|
2018-11-06 14:06:09 +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]));
|
|
|
|
*/
|
2018-09-28 14:29:34 +00:00
|
|
|
std::unique_ptr< ip_range > range(new ip_range);
|
|
|
|
range->octet2 = ip[1]; // 2nd octet
|
|
|
|
range->octet3 = ip[2]; // 3rd octet
|
2018-09-20 12:36:11 +00:00
|
|
|
// FIXME: look up any static mappings to discount
|
|
|
|
range->left = 252;
|
|
|
|
// 4th octet, probably 1, set it
|
|
|
|
struct dns_pointer *result = new dns_pointer;
|
2018-10-03 10:52:00 +00:00
|
|
|
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
|
2018-09-20 12:36:11 +00:00
|
|
|
|
2018-09-21 12:53:45 +00:00
|
|
|
// save tun range in tracker
|
|
|
|
// FIXME: forcing one and only one range
|
2018-09-22 10:24:32 +00:00
|
|
|
if(ip[0] == 10)
|
2018-09-21 12:53:45 +00:00
|
|
|
{
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_ten_ips.push_back(std::move(range));
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_privates.ten = false;
|
2018-09-22 10:24:32 +00:00
|
|
|
}
|
|
|
|
else if(ip[0] == 172)
|
2018-09-21 12:53:45 +00:00
|
|
|
{
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_seven_ips.push_back(std::move(range));
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_privates.oneSeven = false;
|
2018-09-22 10:24:32 +00:00
|
|
|
}
|
|
|
|
else if(ip[0] == 192)
|
2018-09-21 12:53:45 +00:00
|
|
|
{
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_nine_ips.push_back(std::move(range));
|
2018-10-04 14:10:09 +00:00
|
|
|
iptracker->used_privates.oneNine = false;
|
2018-09-22 10:24:32 +00:00
|
|
|
}
|
|
|
|
else
|
2018-09-21 12:53:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-09-20 12:36:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 12:41:09 +00:00
|
|
|
inline struct dns_pointer *
|
2018-09-28 14:29:34 +00:00
|
|
|
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;
|
2018-09-22 10:24:32 +00:00
|
|
|
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);
|
2018-10-03 10:52:00 +00:00
|
|
|
// result->hostResult = new sockaddr;
|
|
|
|
// ip.CopyInto(result->hostResult);
|
2018-11-03 13:19:18 +00:00
|
|
|
result->hostResult = ip.xtohl();
|
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 *
|
2018-09-28 14:29:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-08-08 13:21:14 +00:00
|
|
|
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
|
2018-09-28 14:29:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-08-08 13:21:14 +00:00
|
|
|
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)
|
2018-08-08 13:21:14 +00:00
|
|
|
// 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-10-04 15:34:31 +00:00
|
|
|
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
|
2018-10-04 14:10:09 +00:00
|
|
|
return dns_iptracker_allocate_range(ranges[0], first);
|
2018-08-08 12:41:09 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dns_pointer *
|
|
|
|
dns_iptracker_get_free()
|
2018-09-22 10:24:32 +00:00
|
|
|
{
|
|
|
|
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-10-09 11:02:53 +00:00
|
|
|
llarp::LogInfo("Was 10.x.x.x already in-use on start? ",
|
2018-09-22 10:24:32 +00:00
|
|
|
iptracker->used_privates.ten ? "Yes" : "No");
|
2018-10-09 11:02:53 +00:00
|
|
|
if(!iptracker->used_privates.ten)
|
2018-08-08 12:41:09 +00:00
|
|
|
{
|
|
|
|
struct dns_pointer *test =
|
2018-09-22 10:24:32 +00:00
|
|
|
dns_iptracker_check_range(iptracker->used_ten_ips, 10);
|
2018-08-08 12:41:09 +00:00
|
|
|
if(test)
|
|
|
|
{
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 11:02:53 +00:00
|
|
|
llarp::LogInfo("Was 172.16.x.x already in-use on start? ",
|
2018-09-22 10:24:32 +00:00
|
|
|
iptracker->used_privates.oneSeven ? "Yes" : "No");
|
2018-10-09 11:02:53 +00:00
|
|
|
if(!iptracker->used_privates.oneSeven)
|
2018-08-08 12:41:09 +00:00
|
|
|
{
|
|
|
|
struct dns_pointer *test =
|
2018-09-22 10:24:32 +00:00
|
|
|
dns_iptracker_check_range(iptracker->used_seven_ips, 172);
|
2018-08-08 12:41:09 +00:00
|
|
|
if(test)
|
|
|
|
{
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 11:02:53 +00:00
|
|
|
llarp::LogInfo("Was 192.168.x.x already in-use on start? ",
|
2018-09-22 10:24:32 +00:00
|
|
|
iptracker->used_privates.oneNine ? "Yes" : "No");
|
2018-10-09 11:02:53 +00:00
|
|
|
if(!iptracker->used_privates.oneNine)
|
2018-08-08 12:41:09 +00:00
|
|
|
{
|
|
|
|
struct dns_pointer *test =
|
2018-09-22 10:24:32 +00:00
|
|
|
dns_iptracker_check_range(iptracker->used_nine_ips, 192);
|
2018-08-08 12:41:09 +00:00
|
|
|
if(test)
|
|
|
|
{
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|