2018-08-17 19:49:58 +00:00
|
|
|
#include <llarp/handlers/tun.hpp>
|
2018-07-11 16:11:19 +00:00
|
|
|
#include <llarp/service/context.hpp>
|
2018-09-21 12:53:20 +00:00
|
|
|
#include <llarp/service/endpoint.hpp>
|
2018-10-29 16:48:36 +00:00
|
|
|
#include "router.hpp"
|
2018-07-11 16:11:19 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
Context::Context(llarp_router *r) : m_Router(r)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
{
|
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
itr = m_Endpoints.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Tick()
|
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
auto now = m_Router->Now();
|
2018-07-11 16:11:19 +00:00
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
itr->second->Tick(now);
|
2018-07-11 16:11:19 +00:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 12:35:29 +00:00
|
|
|
|
2018-10-03 10:44:58 +00:00
|
|
|
bool
|
|
|
|
Context::hasEndpoints()
|
|
|
|
{
|
|
|
|
return m_Endpoints.size() ? true : false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
llarp::service::Endpoint *
|
|
|
|
Context::getFirstEndpoint()
|
2018-09-20 12:35:29 +00:00
|
|
|
{
|
2018-09-22 10:23:23 +00:00
|
|
|
if(!m_Endpoints.size())
|
2018-09-20 12:35:29 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No endpoints found");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-09-22 10:23:23 +00:00
|
|
|
auto firstEndpoint = m_Endpoints.begin();
|
2018-09-20 12:35:29 +00:00
|
|
|
auto *uniqueEndpoint = &firstEndpoint->second;
|
2018-09-22 10:23:23 +00:00
|
|
|
return uniqueEndpoint->get();
|
|
|
|
}
|
|
|
|
|
2018-10-03 10:44:58 +00:00
|
|
|
bool
|
|
|
|
Context::iterate(struct endpoint_iter &i)
|
|
|
|
{
|
|
|
|
if(!m_Endpoints.size())
|
|
|
|
{
|
|
|
|
llarp::LogError("No endpoints found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i.index = 0;
|
|
|
|
// llarp::util::Lock lock(access);
|
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
i.endpoint = itr->second.get();
|
2018-10-18 12:14:26 +00:00
|
|
|
if(!i.visit(&i))
|
|
|
|
return false;
|
2018-10-03 10:44:58 +00:00
|
|
|
// advance
|
|
|
|
i.index++;
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-22 10:23:23 +00:00
|
|
|
llarp::handlers::TunEndpoint *
|
|
|
|
Context::getFirstTun()
|
|
|
|
{
|
|
|
|
llarp::service::Endpoint *endpointer = this->getFirstEndpoint();
|
|
|
|
if(!endpointer)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint =
|
2018-11-06 14:27:25 +00:00
|
|
|
static_cast< llarp::handlers::TunEndpoint * >(endpointer);
|
2018-09-20 12:35:29 +00:00
|
|
|
return tunEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_tun_io *
|
|
|
|
Context::getRange()
|
|
|
|
{
|
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint = this->getFirstTun();
|
2018-09-22 10:23:23 +00:00
|
|
|
if(!tunEndpoint)
|
2018-09-20 12:35:29 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No tunnel endpoint found");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &tunEndpoint->tunif;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:28:01 +00:00
|
|
|
bool
|
|
|
|
Context::FindBestAddressFor(const llarp::service::Address &addr,
|
|
|
|
huint32_t &ip)
|
2018-10-23 18:00:55 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
2018-10-23 18:18:00 +00:00
|
|
|
if(itr->second->HasAddress(addr))
|
2018-10-23 18:00:55 +00:00
|
|
|
{
|
2018-10-23 21:28:01 +00:00
|
|
|
ip = itr->second->ObtainIPForAddr(addr);
|
|
|
|
return true;
|
2018-10-23 18:00:55 +00:00
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
2018-10-23 21:28:01 +00:00
|
|
|
itr = m_Endpoints.find("default");
|
|
|
|
if(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
ip = itr->second->ObtainIPForAddr(addr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-10-23 18:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 12:53:20 +00:00
|
|
|
bool
|
|
|
|
Context::Prefetch(const llarp::service::Address &addr)
|
|
|
|
{
|
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint = this->getFirstTun();
|
2018-09-22 10:23:23 +00:00
|
|
|
if(!tunEndpoint)
|
2018-09-21 12:53:20 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No tunnel endpoint found");
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-22 10:23:23 +00:00
|
|
|
// HiddenServiceAddresslookup *lookup = new
|
|
|
|
// HiddenServiceEndpoint(tunEndpoint, callback, addr,
|
|
|
|
// tunEndpoint->GenTXID());
|
|
|
|
return tunEndpoint->EnsurePathToService(
|
2018-11-07 15:30:22 +00:00
|
|
|
addr,
|
|
|
|
[](__attribute__((unused)) Address addr,
|
|
|
|
__attribute__((unused)) void *ctx) {},
|
|
|
|
10000);
|
2018-09-21 12:53:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
huint32_t
|
|
|
|
Context::GetIpForAddr(const llarp::service::Address &addr)
|
|
|
|
{
|
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint = this->getFirstTun();
|
|
|
|
if(!tunEndpoint)
|
|
|
|
{
|
|
|
|
huint32_t zero;
|
|
|
|
zero.h = 0;
|
|
|
|
llarp::LogError("No tunnel endpoint found");
|
|
|
|
return zero;
|
|
|
|
}
|
|
|
|
return tunEndpoint->ObtainIPForAddr(addr);
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:05:42 +00:00
|
|
|
bool
|
2018-10-10 15:14:45 +00:00
|
|
|
Context::MapAddress(const llarp::service::Address &addr, huint32_t ip)
|
2018-09-20 10:05:42 +00:00
|
|
|
{
|
2018-09-22 10:23:23 +00:00
|
|
|
if(!m_Endpoints.size())
|
2018-09-20 10:05:42 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No endpoints found");
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-22 10:23:23 +00:00
|
|
|
auto firstEndpoint = m_Endpoints.begin();
|
|
|
|
auto *uniqueEndpoint = &firstEndpoint->second;
|
2018-09-20 10:05:42 +00:00
|
|
|
llarp::service::Endpoint *endpointer = uniqueEndpoint->get();
|
2018-09-22 10:23:23 +00:00
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint =
|
2018-11-06 14:27:25 +00:00
|
|
|
static_cast< llarp::handlers::TunEndpoint * >(endpointer);
|
2018-09-22 10:23:23 +00:00
|
|
|
if(!tunEndpoint)
|
2018-09-20 10:05:42 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No tunnel endpoint found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return tunEndpoint->MapAddress(addr, ip);
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2018-10-02 17:35:51 +00:00
|
|
|
bool
|
2018-10-18 12:14:26 +00:00
|
|
|
MapAddressAllIter(struct Context::endpoint_iter *endpointCfg)
|
2018-10-02 17:35:51 +00:00
|
|
|
{
|
2018-10-03 10:44:58 +00:00
|
|
|
Context::mapAddressAll_context *context =
|
|
|
|
(Context::mapAddressAll_context *)endpointCfg->user;
|
|
|
|
llarp::handlers::TunEndpoint *tunEndpoint =
|
|
|
|
(llarp::handlers::TunEndpoint *)endpointCfg->endpoint;
|
|
|
|
if(!tunEndpoint)
|
|
|
|
{
|
|
|
|
llarp::LogError("No tunnel endpoint found");
|
2018-10-18 12:14:26 +00:00
|
|
|
return true; // still continue
|
2018-10-03 10:44:58 +00:00
|
|
|
}
|
|
|
|
return tunEndpoint->MapAddress(context->serviceAddr,
|
2018-10-10 15:14:45 +00:00
|
|
|
context->localPrivateIpAddr.xtohl());
|
2018-10-02 17:35:51 +00:00
|
|
|
}
|
2018-10-03 10:44:58 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Context::MapAddressAll(const llarp::service::Address &addr,
|
|
|
|
llarp::Addr &localPrivateIpAddr)
|
|
|
|
{
|
|
|
|
struct Context::mapAddressAll_context context;
|
|
|
|
context.serviceAddr = addr;
|
|
|
|
context.localPrivateIpAddr = localPrivateIpAddr;
|
|
|
|
|
|
|
|
struct Context::endpoint_iter i;
|
|
|
|
i.user = &context;
|
|
|
|
i.index = 0;
|
2018-10-18 12:14:26 +00:00
|
|
|
i.visit = &MapAddressAllIter;
|
2018-10-03 10:44:58 +00:00
|
|
|
return this->iterate(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::AddDefaultEndpoint(const std::string &ifaddr,
|
2018-11-11 13:22:02 +00:00
|
|
|
const std::string &ifname,
|
|
|
|
const std::string &remoteResolver,
|
|
|
|
const std::string &localResolver)
|
2018-10-03 10:44:58 +00:00
|
|
|
{
|
2018-11-11 13:22:02 +00:00
|
|
|
return AddEndpoint({"default",
|
|
|
|
{{"type", "tun"},
|
|
|
|
{"ifaddr", ifaddr},
|
|
|
|
{"ifname", ifname},
|
|
|
|
{"local-dns", localResolver},
|
|
|
|
{"upstream-dns", remoteResolver}}},
|
|
|
|
true);
|
2018-10-03 10:44:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
2018-11-11 13:14:19 +00:00
|
|
|
Context::StartAll()
|
|
|
|
{
|
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
if(!itr->second->Start())
|
|
|
|
{
|
|
|
|
llarp::LogError(itr->first, " failed to start");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::LogInfo(itr->first, " started");
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::AddEndpoint(const Config::section_t &conf, bool autostart)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
|
|
|
{
|
2018-08-23 14:07:53 +00:00
|
|
|
auto itr = m_Endpoints.find(conf.first);
|
|
|
|
if(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
llarp::LogError("cannot add hidden service with duplicate name: ",
|
|
|
|
conf.first);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2018-08-23 14:07:53 +00:00
|
|
|
// extract type
|
|
|
|
std::string endpointType = "tun";
|
|
|
|
for(const auto &option : conf.second)
|
|
|
|
{
|
|
|
|
if(option.first == "type")
|
|
|
|
endpointType = option.second;
|
|
|
|
}
|
|
|
|
std::unique_ptr< llarp::service::Endpoint > service;
|
|
|
|
|
|
|
|
static std::map< std::string,
|
|
|
|
std::function< llarp::service::Endpoint *(
|
|
|
|
const std::string &, llarp_router *) > >
|
|
|
|
endpointConstructors = {
|
|
|
|
{"tun",
|
|
|
|
[](const std::string &nick,
|
|
|
|
llarp_router *r) -> llarp::service::Endpoint * {
|
|
|
|
return new llarp::handlers::TunEndpoint(nick, r);
|
|
|
|
}},
|
|
|
|
{"null",
|
|
|
|
[](const std::string &nick,
|
|
|
|
llarp_router *r) -> llarp::service::Endpoint * {
|
|
|
|
return new llarp::service::Endpoint(nick, r);
|
|
|
|
}}};
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2018-08-23 14:07:53 +00:00
|
|
|
{
|
|
|
|
// detect type
|
|
|
|
auto itr = endpointConstructors.find(endpointType);
|
|
|
|
if(itr == endpointConstructors.end())
|
|
|
|
{
|
|
|
|
llarp::LogError("no such endpoint type: ", endpointType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct
|
2018-11-02 12:35:20 +00:00
|
|
|
service.reset(itr->second(conf.first, m_Router));
|
2018-08-23 14:07:53 +00:00
|
|
|
}
|
|
|
|
// configure
|
2018-07-11 16:11:19 +00:00
|
|
|
for(const auto &option : conf.second)
|
|
|
|
{
|
|
|
|
auto &k = option.first;
|
2018-08-23 14:07:53 +00:00
|
|
|
if(k == "type")
|
|
|
|
continue;
|
2018-07-11 16:11:19 +00:00
|
|
|
auto &v = option.second;
|
|
|
|
if(!service->SetOption(k, v))
|
|
|
|
{
|
2018-07-12 13:43:37 +00:00
|
|
|
llarp::LogError("failed to set ", k, "=", v,
|
|
|
|
" for hidden service endpoint ", conf.first);
|
2018-07-11 16:11:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 13:14:19 +00:00
|
|
|
if(autostart)
|
|
|
|
{
|
|
|
|
// start
|
|
|
|
if(service->Start())
|
|
|
|
{
|
|
|
|
llarp::LogInfo("autostarting hidden service endpoint ",
|
|
|
|
service->Name());
|
|
|
|
m_Endpoints.insert(std::make_pair(conf.first, std::move(service)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llarp::LogError("failed to start hidden service endpoint ", conf.first);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
2018-07-29 23:29:36 +00:00
|
|
|
llarp::LogInfo("added hidden service endpoint ", service->Name());
|
2018-08-17 19:49:58 +00:00
|
|
|
m_Endpoints.insert(std::make_pair(conf.first, std::move(service)));
|
2018-07-11 16:11:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
} // namespace service
|
2018-08-17 19:49:58 +00:00
|
|
|
} // namespace llarp
|