more tun code

pull/13/head
Jeff Becker 6 years ago
parent 1c916e9d0c
commit 08d6121a5a
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -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

@ -26,10 +26,10 @@ namespace llarp
void
SetHandler(IDataHandler* h);
bool
virtual bool
SetOption(const std::string& k, const std::string& v);
void
virtual void
Tick(llarp_time_t now);
/// router's logic
@ -52,9 +52,16 @@ namespace llarp
return m_Router;
}
bool
virtual bool
Start();
virtual bool
Stop()
{
// TODO: implement me
return false;
}
std::string
Name() const;
@ -230,6 +237,9 @@ namespace llarp
bool
IsolateNetwork();
bool
NetworkIsIsolated() const;
private:
bool
OnOutboundLookup(const IntroSet* i); /* */
@ -240,6 +250,13 @@ namespace llarp
bool
DoNetworkIsolation();
virtual bool
SetupNetworking()
{
// XXX: override me
return true;
}
uint64_t
GenTXID();
@ -378,4 +395,4 @@ namespace llarp
} // namespace service
} // 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

@ -9,7 +9,7 @@
#include <condition_variable>
#include <thread>
#endif
#include <future>
#include <memory>
namespace llarp
@ -86,4 +86,4 @@ namespace llarp
} // namespace util
} // namespace llarp
#endif
#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

@ -11,10 +11,10 @@
#include <unordered_map>
#include <llarp/dht.hpp>
#include <llarp/handlers/tun.hpp>
#include <llarp/link_message.hpp>
#include <llarp/routing/handler.hpp>
#include <llarp/service.hpp>
#include <llarp/service/tun.hpp>
#include "llarp/iwp/establish_job.hpp"
#include "crypto.hpp"
@ -97,7 +97,7 @@ struct llarp_router
llarp::service::Context hiddenServiceContext;
llarp::service::TunEndpoint *tunEndpoint = nullptr;
llarp::handlers::TunEndpoint *tunEndpoint = nullptr;
llarp_link *outboundLink = nullptr;
std::list< llarp_link * > inboundLinks;

@ -55,6 +55,12 @@ namespace llarp
return true;
}
bool
Endpoint::NetworkIsIsolated() const
{
return m_IsolatedLogic && m_IsolatedWorker;
}
struct PathAlignJob
{
void
@ -399,6 +405,7 @@ namespace llarp
{
m_DataHandler = this;
}
// this does network isolation
while(m_OnInit.size())
{
if(m_OnInit.front()())

@ -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…
Cancel
Save