mirror of https://github.com/oxen-io/lokinet
more tun code
parent
1c916e9d0c
commit
08d6121a5a
@ -1,15 +1,58 @@
|
||||
#ifndef LLARP_HANDLERS_TUN_HPP
|
||||
#define LLARP_HANDLERS_TUN_HPP
|
||||
#include <llarp/service/handler.hpp>
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/service/endpoint.hpp>
|
||||
#include <llarp/threading.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace handlers
|
||||
{
|
||||
struct TunHandler : public service::IDataHandler
|
||||
struct TunEndpoint : public service::Endpoint
|
||||
{
|
||||
static constexpr int DefaultNetmask = 16;
|
||||
static constexpr char DefaultIfname[] = "lokinet0";
|
||||
static constexpr char DefaultDstAddr[] = "10.10.0.1";
|
||||
static constexpr char DefaultSrcAddr[] = "10.10.0.2";
|
||||
|
||||
TunEndpoint(const std::string& nickname, llarp_router* r);
|
||||
~TunEndpoint();
|
||||
|
||||
bool
|
||||
SetOption(const std::string& k, const std::string& v);
|
||||
|
||||
void
|
||||
Tick(llarp_time_t now);
|
||||
|
||||
void
|
||||
TickTun(llarp_time_t now);
|
||||
|
||||
bool
|
||||
Start();
|
||||
|
||||
/// set up tun interface, blocking
|
||||
bool
|
||||
SetupTun();
|
||||
|
||||
/// overrides Endpoint
|
||||
bool
|
||||
SetupNetworking();
|
||||
|
||||
llarp_tun_io tunif;
|
||||
|
||||
static void
|
||||
tunifTick(llarp_tun_io* t);
|
||||
|
||||
static void
|
||||
tunifRecvPkt(llarp_tun_io* t, const void* pkt, ssize_t sz);
|
||||
|
||||
static void
|
||||
handleTickTun(void* u);
|
||||
|
||||
private:
|
||||
std::promise< bool > m_TunSetupResult;
|
||||
};
|
||||
} // namespace handlers
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -1,21 +0,0 @@
|
||||
#ifndef LLARP_SERVICE_TUN_HPP
|
||||
#define LLARP_SERVICE_TUN_HPP
|
||||
#include <tuntap.h>
|
||||
#include <llarp/service/endpoint.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace service
|
||||
{
|
||||
struct TunEndpoint : public Endpoint
|
||||
{
|
||||
TunEndpoint(const std::string& ifname, llarp_router* r);
|
||||
~TunEndpoint();
|
||||
|
||||
device* m_tunif;
|
||||
std::string m_IfName;
|
||||
};
|
||||
} // namespace service
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -1,20 +1,112 @@
|
||||
#include <llarp/service/tun.hpp>
|
||||
#include <llarp/handlers/tun.hpp>
|
||||
#include "router.hpp"
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace service
|
||||
namespace handlers
|
||||
{
|
||||
TunEndpoint::TunEndpoint(const std::string &ifname, llarp_router *r)
|
||||
: Endpoint("tunif-" + ifname, r)
|
||||
, m_tunif(tuntap_init())
|
||||
, m_IfName(ifname)
|
||||
TunEndpoint::TunEndpoint(const std::string &nickname, llarp_router *r)
|
||||
: service::Endpoint(nickname, r)
|
||||
{
|
||||
tunif.user = this;
|
||||
tunif.netmask = TunEndpoint::DefaultNetmask;
|
||||
strncpy(tunif.ifaddr, TunEndpoint::DefaultSrcAddr,
|
||||
sizeof(tunif.ifaddr) - 1);
|
||||
strncpy(tunif.ifname, TunEndpoint::DefaultIfname,
|
||||
sizeof(tunif.ifname) - 1);
|
||||
tunif.tick = &tunifTick;
|
||||
tunif.recvpkt = &tunifRecvPkt;
|
||||
}
|
||||
|
||||
bool
|
||||
TunEndpoint::SetOption(const std::string &k, const std::string &v)
|
||||
{
|
||||
if(k == "ifname")
|
||||
{
|
||||
strncpy(tunif.ifname, v.c_str(), sizeof(tunif.ifname) - 1);
|
||||
return true;
|
||||
}
|
||||
if(k == "ifaddr")
|
||||
{
|
||||
std::string addr;
|
||||
auto pos = v.find("/");
|
||||
if(pos != std::string::npos)
|
||||
{
|
||||
auto num = std::stoi(v.substr(pos + 1));
|
||||
if(num > 0)
|
||||
{
|
||||
tunif.netmask = num;
|
||||
addr = v.substr(0, pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
llarp::LogError("bad ifaddr value: ", v);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tunif.netmask = 32;
|
||||
addr = v;
|
||||
}
|
||||
strncpy(tunif.ifaddr, addr.c_str(), sizeof(tunif.ifaddr) - 1);
|
||||
return true;
|
||||
}
|
||||
return Endpoint::SetOption(k, v);
|
||||
}
|
||||
|
||||
bool
|
||||
TunEndpoint::Start()
|
||||
{
|
||||
// do network isolation first
|
||||
if(!Endpoint::Start())
|
||||
return false;
|
||||
|
||||
if(!NetworkIsIsolated())
|
||||
{
|
||||
// set up networking in currrent thread if we are not isolated
|
||||
if(!SetupNetworking())
|
||||
return false;
|
||||
}
|
||||
// wait for result for network setup
|
||||
llarp::LogInfo("waiting for tun interface...");
|
||||
return m_TunSetupResult.get_future().get();
|
||||
}
|
||||
|
||||
bool
|
||||
TunEndpoint::SetupTun()
|
||||
{
|
||||
auto evloop = Router()->netloop;
|
||||
return llarp_ev_add_tun(evloop, &tunif);
|
||||
}
|
||||
|
||||
bool
|
||||
TunEndpoint::SetupNetworking()
|
||||
{
|
||||
bool result = SetupTun();
|
||||
m_TunSetupResult.set_value(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
TunEndpoint::Tick(llarp_time_t now)
|
||||
{
|
||||
// call tun code in endpoint logic in case of network isolation
|
||||
llarp_logic_queue_job(EndpointLogic(), {this, handleTickTun});
|
||||
Endpoint::Tick(now);
|
||||
}
|
||||
|
||||
void
|
||||
TunEndpoint::handleTickTun(void *u)
|
||||
{
|
||||
auto now = llarp_time_now_ms();
|
||||
TunEndpoint *self = static_cast< TunEndpoint * >(u);
|
||||
self->TickTun(now);
|
||||
}
|
||||
|
||||
TunEndpoint::~TunEndpoint()
|
||||
{
|
||||
tuntap_destroy(m_tunif);
|
||||
}
|
||||
|
||||
} // namespace service
|
||||
} // namespace llarp
|
||||
} // namespace handlers
|
||||
} // namespace llarp
|
||||
|
@ -1,14 +0,0 @@
|
||||
#include <llarp/service/tun.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace service
|
||||
{
|
||||
TunEndpoint::TunEndpoint(const std::string& ifname, llarp_router* r)
|
||||
: Endpoint("tun-" + ifname, r)
|
||||
{
|
||||
m_tunif = tuntap_init();
|
||||
tuntap_set_ifname(m_tunif, ifname.c_str());
|
||||
}
|
||||
} // namespace service
|
||||
} // namespace llarp
|
Loading…
Reference in New Issue