1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

initialize tun with 0 and set defaults in correct places

This commit is contained in:
Jeff Becker 2019-07-08 11:26:06 -04:00
parent 134d36eb02
commit 0eb6431eb1
No known key found for this signature in database
GPG key ID: F357B3B42F6F9B05
6 changed files with 40 additions and 80 deletions

View file

@ -132,6 +132,7 @@ namespace llarp
return false;
}
Section_t& sect = m_Config[sectName];
LogDebug(m_FileName, ": ", sectName, ".", k, "=", v);
sect.emplace(k, v);
}
else // malformed?

View file

@ -94,46 +94,17 @@ llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to,
bool
llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun)
{
// llarp::LogInfo("ev creating tunnel ", tun->ifaddr, " on ", tun->ifname);
if(strcmp(tun->ifaddr, "") == 0 || strcmp(tun->ifaddr, "auto") == 0)
#if !defined(_WIN32)
if(tun->ifaddr[0] == 0 || strcmp(tun->ifaddr, "auto") == 0)
{
std::string ifaddr = llarp::FindFreeRange();
auto pos = ifaddr.find("/");
if(pos == std::string::npos)
{
llarp::LogWarn("Auto ifaddr didn't return a netmask: ", ifaddr);
return false;
}
int num;
std::string part = ifaddr.substr(pos + 1);
#if defined(ANDROID) || defined(RPI)
num = atoi(part.c_str());
#else
num = std::stoi(part);
#endif
if(num <= 0)
{
llarp::LogError("bad ifaddr netmask value: ", ifaddr);
return false;
}
tun->netmask = num;
const std::string addr = ifaddr.substr(0, pos);
std::copy_n(addr.begin(), std::min(sizeof(tun->ifaddr), addr.size()),
tun->ifaddr);
llarp::LogInfo("IfAddr autodetect: ", tun->ifaddr, "/", tun->netmask);
LogError("invalid ifaddr on tun: ", tun->ifaddr);
return false;
}
if(strcmp(tun->ifname, "") == 0 || strcmp(tun->ifname, "auto") == 0)
if(tun->ifname[0] == 0 || strcmp(tun->ifname, "auto") == 0)
{
std::string ifname = llarp::FindFreeTun();
std::copy_n(ifname.begin(), std::min(sizeof(tun->ifname), ifname.size()),
tun->ifname);
llarp::LogInfo("IfName autodetect: ", tun->ifname);
LogError("invalid ifname on tun: ", tun->ifname);
return false;
}
llarp::LogDebug("Tun Interface will use the following settings:");
llarp::LogDebug("IfAddr: ", tun->ifaddr);
llarp::LogDebug("IfName: ", tun->ifname);
llarp::LogDebug("IfNMsk: ", tun->netmask);
#ifndef _WIN32
return loop->tun_listen(tun);
#else
UNREFERENCED_PARAMETER(loop);

View file

@ -45,18 +45,19 @@ namespace llarp
, m_Resolver(std::make_shared< dns::Proxy >(
r->netloop(), r->logic(), r->netloop(), r->logic(), this))
{
std::fill(tunif.ifaddr, tunif.ifaddr + sizeof(tunif.ifaddr), 0);
std::fill(tunif.ifname, tunif.ifname + sizeof(tunif.ifname), 0);
tunif.netmask = 0;
#ifdef ANDROID
tunif.get_fd_promise = &get_tun_fd_promise;
Promise.reset(new llarp_fd_promise(&m_VPNPromise));
#else
tunif.get_fd_promise = nullptr;
#endif
tunif.user = this;
tunif.netmask = DefaultTunNetmask;
tunif.user = this;
// eh this shouldn't do anything on windows anyway
strncpy(tunif.ifaddr, DefaultTunSrcAddr, sizeof(tunif.ifaddr) - 1);
strncpy(tunif.ifname, DefaultTunIfname, sizeof(tunif.ifname) - 1);
tunif.tick = &tunifTick;
tunif.before_write = &tunifBeforeWrite;
tunif.recvpkt = &tunifRecvPkt;

View file

@ -15,11 +15,6 @@ namespace llarp
{
namespace handlers
{
static const int DefaultTunNetmask = 16;
static const char DefaultTunIfname[] = "lokinet0";
static const char DefaultTunDstAddr[] = "10.10.0.1";
static const char DefaultTunSrcAddr[] = "10.10.0.2";
struct TunEndpoint : public service::Endpoint,
public dns::IQueryHandler,
public std::enable_shared_from_this< TunEndpoint >

View file

@ -25,6 +25,7 @@
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <unordered_map>
#if defined(RPI) || defined(ANDROID)
#include <unistd.h>
#endif
@ -1426,6 +1427,30 @@ namespace llarp
" routers");
}
/// this function ensure there are sane defualts in a net config
static void
EnsureNetConfigDefaultsSane(
std::unordered_multimap< std::string, std::string > &netConfig)
{
static const std::unordered_map< std::string,
std::function< std::string(void) > >
netConfigDefaults = {
{"ifname", llarp::FindFreeTun},
{"ifaddr", llarp::FindFreeRange},
{"local-dns", []() -> std::string { return "127.0.0.1:53"; }}};
// populate with fallback defaults if values not present
auto itr = netConfigDefaults.begin();
while(itr != netConfigDefaults.end())
{
auto found = netConfig.find(itr->first);
if(found == netConfig.end() || found->second.empty())
{
netConfig.emplace(itr->first, itr->second());
}
++itr;
}
}
bool
Router::Run(struct llarp_nodedb *nodedb)
{
@ -1566,6 +1591,8 @@ namespace llarp
LogWarn("Link ", link->Name(), " failed to start");
}
EnsureNetConfigDefaultsSane(netConfig);
if(IBLinksStarted > 0)
{
// initialize as service node
@ -1763,24 +1790,6 @@ namespace llarp
bool
Router::CreateDefaultHiddenService()
{
// fallback defaults
static const std::unordered_map< std::string,
std::function< std::string(void) > >
netConfigDefaults = {
{"ifname", llarp::FindFreeTun},
{"ifaddr", llarp::FindFreeRange},
{"local-dns", []() -> std::string { return "127.0.0.1:53"; }}};
// populate with fallback defaults if values not present
auto itr = netConfigDefaults.begin();
while(itr != netConfigDefaults.end())
{
auto found = netConfig.find(itr->first);
if(found == netConfig.end() || found->second.empty())
{
netConfig.emplace(itr->first, itr->second());
}
++itr;
}
// add endpoint
return hiddenServiceContext().AddDefaultEndpoint(netConfig);
}

View file

@ -127,23 +127,6 @@ namespace llarp
{
item.second->Tick(now);
}
/*
std::vector< RouterID > expired;
m_Router->nodedb()->visit([&](const RouterContact &rc) -> bool {
if(rc.IsExpired(now))
expired.emplace_back(rc.pubkey);
return true;
});
ForEachService([&](const std::string &,
const std::shared_ptr< Endpoint > &ep) -> bool {
// TODO: we need to stop looking up service nodes that are gone forever
// how do?
for(const auto &k : expired)
if(!ep->LookupRouterAnon(k, nullptr))
return false;
return true;
});
*/
}
bool