2018-10-10 12:06:28 +00:00
|
|
|
#include <algorithm>
|
2020-05-01 16:13:49 +00:00
|
|
|
#include "net/net.hpp"
|
2018-08-22 07:35:49 +00:00
|
|
|
// harmless on other platforms
|
|
|
|
#define __USE_MINGW_ANSI_STDIO 1
|
2018-12-12 01:12:59 +00:00
|
|
|
#include <handlers/tun.hpp>
|
2018-09-23 16:48:43 +00:00
|
|
|
#include <sys/types.h>
|
2018-09-25 08:31:29 +00:00
|
|
|
#ifndef _WIN32
|
2018-09-23 16:48:43 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2018-09-25 08:31:29 +00:00
|
|
|
#endif
|
2018-08-15 15:36:34 +00:00
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
#include <dns/dns.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
#include <service/context.hpp>
|
2020-08-31 20:07:17 +00:00
|
|
|
#include <service/outbound_context.hpp>
|
2020-05-21 14:18:23 +00:00
|
|
|
#include <service/endpoint_state.hpp>
|
2020-08-25 18:57:15 +00:00
|
|
|
#include <service/outbound_context.hpp>
|
2019-09-01 12:38:03 +00:00
|
|
|
#include <util/meta/memfn.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-05-06 12:42:21 +00:00
|
|
|
#include <nodedb.hpp>
|
2020-06-02 21:10:42 +00:00
|
|
|
#include <rpc/endpoint_rpc.hpp>
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2019-05-22 16:20:03 +00:00
|
|
|
#include <util/str.hpp>
|
|
|
|
|
2020-08-31 20:07:17 +00:00
|
|
|
#include <dns/srv_data.hpp>
|
|
|
|
|
2018-08-15 15:36:34 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-08-16 14:34:15 +00:00
|
|
|
namespace handlers
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::FlushToUser(std::function<bool(net::IPPacket&)> send)
|
2018-11-26 22:46:22 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
// flush network to user
|
|
|
|
m_NetworkToUserPktQueue.Process(send);
|
2018-11-26 22:46:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 18:37:19 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::ShouldFlushNow(llarp_time_t now) const
|
|
|
|
{
|
2020-02-24 19:40:45 +00:00
|
|
|
static constexpr auto FlushInterval = 25ms;
|
2019-11-29 18:37:19 +00:00
|
|
|
return now >= m_LastFlushAt + FlushInterval;
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:45:23 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::tunifTick(llarp_tun_io* tun)
|
2018-12-15 16:56:35 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* self = static_cast<TunEndpoint*>(tun->user);
|
2020-05-21 14:18:23 +00:00
|
|
|
self->Flush();
|
2018-12-15 16:56:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 19:40:20 +00:00
|
|
|
TunEndpoint::TunEndpoint(AbstractRouter* r, service::Context* parent, bool lazyVPN)
|
|
|
|
: service::Endpoint(r, parent)
|
|
|
|
, m_UserToNetworkPktQueue("endpoint_sendq", r->netloop(), r->netloop())
|
|
|
|
, m_NetworkToUserPktQueue("endpoint_recvq", r->netloop(), r->netloop())
|
2020-04-07 18:38:56 +00:00
|
|
|
, m_Resolver(std::make_shared<dns::Proxy>(
|
2019-05-23 12:22:48 +00:00
|
|
|
r->netloop(), r->logic(), r->netloop(), r->logic(), this))
|
2018-08-15 15:36:34 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not lazyVPN)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
tunif.reset(new llarp_tun_io());
|
|
|
|
std::fill(tunif->ifaddr, tunif->ifaddr + sizeof(tunif->ifaddr), 0);
|
|
|
|
std::fill(tunif->ifname, tunif->ifname + sizeof(tunif->ifname), 0);
|
2020-04-07 18:38:56 +00:00
|
|
|
tunif->netmask = 0;
|
2019-10-04 18:10:58 +00:00
|
|
|
tunif->get_fd_promise = nullptr;
|
2020-04-07 18:38:56 +00:00
|
|
|
tunif->user = this;
|
2019-10-04 18:10:58 +00:00
|
|
|
// eh this shouldn't do anything on windows anyway
|
2020-04-07 18:38:56 +00:00
|
|
|
tunif->tick = &tunifTick;
|
2019-10-04 18:10:58 +00:00
|
|
|
tunif->before_write = &tunifBeforeWrite;
|
2020-04-07 18:38:56 +00:00
|
|
|
tunif->recvpkt = &tunifRecvPkt;
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
TunEndpoint::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto obj = service::Endpoint::ExtractStatus();
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["ifaddr"] = m_OurRange.ToString();
|
2020-06-24 13:24:07 +00:00
|
|
|
if (tunif)
|
|
|
|
{
|
|
|
|
obj["ifname"] = tunif->ifname;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<std::string> resolvers;
|
|
|
|
for (const auto& addr : m_UpstreamResolvers)
|
2020-05-06 20:38:44 +00:00
|
|
|
resolvers.emplace_back(addr.toString());
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["ustreamResolvers"] = resolvers;
|
2020-05-06 20:38:44 +00:00
|
|
|
obj["localResolver"] = m_LocalResolverAddr.toString();
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject ips{};
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& item : m_IPActivity)
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2020-02-25 17:05:13 +00:00
|
|
|
util::StatusObject ipObj{{"lastActive", to_json(item.second)}};
|
2019-02-08 19:43:25 +00:00
|
|
|
std::string remoteStr;
|
2020-04-07 18:38:56 +00:00
|
|
|
AlignedBuffer<32> addr = m_IPToAddr.at(item.first);
|
|
|
|
if (m_SNodes.at(addr))
|
2019-02-08 19:43:25 +00:00
|
|
|
remoteStr = RouterID(addr.as_array()).ToString();
|
|
|
|
else
|
|
|
|
remoteStr = service::Address(addr.as_array()).ToString();
|
2020-04-07 18:38:56 +00:00
|
|
|
ipObj["remote"] = remoteStr;
|
2019-02-08 19:43:25 +00:00
|
|
|
std::string ipaddr = item.first.ToString();
|
2020-04-07 18:38:56 +00:00
|
|
|
ips[ipaddr] = ipObj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
obj["addrs"] = ips;
|
|
|
|
obj["ourIP"] = m_OurIP.ToString();
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["nextIP"] = m_NextIP.ToString();
|
2020-04-07 18:38:56 +00:00
|
|
|
obj["maxIP"] = m_MaxIP.ToString();
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
2020-05-01 16:13:49 +00:00
|
|
|
TunEndpoint::Configure(const NetworkConfig& conf, const DnsConfig& dnsConf)
|
2018-08-16 14:34:15 +00:00
|
|
|
{
|
2020-05-01 16:13:49 +00:00
|
|
|
if (conf.m_reachable)
|
2020-02-11 21:48:36 +00:00
|
|
|
{
|
2020-05-04 16:51:57 +00:00
|
|
|
m_PublishIntroSet = true;
|
|
|
|
LogInfo(Name(), " setting to be reachable by default");
|
2020-02-11 21:48:36 +00:00
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_PublishIntroSet = false;
|
|
|
|
LogInfo(Name(), " setting to be not reachable by default");
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:02:29 +00:00
|
|
|
if (conf.m_AuthType != service::AuthType::eAuthTypeNone)
|
2020-06-02 21:10:42 +00:00
|
|
|
{
|
2020-06-30 16:02:29 +00:00
|
|
|
std::string url, method;
|
|
|
|
if (conf.m_AuthUrl.has_value() and conf.m_AuthMethod.has_value())
|
|
|
|
{
|
|
|
|
url = *conf.m_AuthUrl;
|
|
|
|
method = *conf.m_AuthMethod;
|
|
|
|
}
|
2020-06-02 21:10:42 +00:00
|
|
|
auto auth = std::make_shared<rpc::EndpointAuthRPC>(
|
2020-06-30 16:02:29 +00:00
|
|
|
url, method, conf.m_AuthWhitelist, Router()->lmq(), shared_from_this());
|
2020-06-02 21:10:42 +00:00
|
|
|
auth->Start();
|
|
|
|
m_AuthPolicy = std::move(auth);
|
|
|
|
}
|
|
|
|
|
2020-05-01 16:13:49 +00:00
|
|
|
/*
|
|
|
|
* TODO: reinstate this option (it's not even clear what section this came from...)
|
|
|
|
*
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "isolate-network" && IsTrueValue(v.c_str()))
|
2019-05-22 16:20:03 +00:00
|
|
|
{
|
2019-05-22 16:20:50 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
LogInfo(Name(), " isolating network...");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!SpawnIsolatedNetwork())
|
2019-05-22 16:20:50 +00:00
|
|
|
{
|
|
|
|
LogError(Name(), " failed to spawn isolated network");
|
2019-05-22 16:20:03 +00:00
|
|
|
return false;
|
2019-05-22 16:20:50 +00:00
|
|
|
}
|
|
|
|
LogInfo(Name(), " booyeah network isolation succeeded");
|
|
|
|
return true;
|
|
|
|
#else
|
2020-04-07 18:38:56 +00:00
|
|
|
LogError(Name(), " network isolation is not supported on your platform");
|
2019-05-22 16:20:50 +00:00
|
|
|
return false;
|
|
|
|
#endif
|
2019-05-22 16:20:03 +00:00
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: this is currently defined for [router] / RouterConfig, but is clearly an [endpoint]
|
|
|
|
* option. either move it to [endpoint] or plumb RouterConfig through
|
|
|
|
*
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "strict-connect")
|
2019-05-06 12:42:21 +00:00
|
|
|
{
|
|
|
|
RouterID connect;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!connect.FromString(v))
|
2019-05-06 12:42:21 +00:00
|
|
|
{
|
|
|
|
LogError(Name(), " invalid snode for strict-connect: ", v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RouterContact rc;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!m_router->nodedb()->Get(connect, rc))
|
2019-05-06 12:42:21 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogError(
|
|
|
|
Name(), " we don't have the RC for ", v, " so we can't use it in strict-connect");
|
2019-05-06 12:42:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& ai : rc.addrs)
|
2019-05-06 12:42:21 +00:00
|
|
|
{
|
|
|
|
m_StrictConnectAddrs.emplace_back(ai);
|
2020-04-07 18:38:56 +00:00
|
|
|
LogInfo(Name(), " added ", m_StrictConnectAddrs.back(), " to strict connect");
|
2019-05-06 12:42:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
m_LocalResolverAddr = dnsConf.m_bind;
|
|
|
|
m_UpstreamResolvers = dnsConf.m_upstreamDNS;
|
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
for (const auto& item : conf.m_mapAddrs)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
if (not MapAddress(item.second, item.first, false))
|
2020-05-01 16:13:49 +00:00
|
|
|
return false;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
|
|
|
|
std::string ifname = conf.m_ifname;
|
|
|
|
if (tunif)
|
2018-08-16 14:34:15 +00:00
|
|
|
{
|
2020-05-01 16:13:49 +00:00
|
|
|
if (ifname.length() >= sizeof(tunif->ifname))
|
2018-12-28 15:10:05 +00:00
|
|
|
{
|
2020-05-01 16:13:49 +00:00
|
|
|
llarp::LogError(Name() + " ifname '", ifname, "' is too long");
|
2018-12-28 15:10:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
strncpy(tunif->ifname, ifname.c_str(), sizeof(tunif->ifname) - 1);
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp::LogInfo(Name() + " setting ifname to ", tunif->ifname);
|
2020-05-01 16:13:49 +00:00
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
m_OurRange = conf.m_ifaddr;
|
|
|
|
m_UseV6 = not m_OurRange.IsV4();
|
|
|
|
tunif->netmask = m_OurRange.HostmaskBits();
|
|
|
|
const auto addr = m_OurRange.BaseAddressString();
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogInfo(Name() + " set ifaddr to ", addr, " with netmask ", tunif->netmask);
|
2019-10-04 18:10:58 +00:00
|
|
|
strncpy(tunif->ifaddr, addr.c_str(), sizeof(tunif->ifaddr) - 1);
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
2020-05-01 16:13:49 +00:00
|
|
|
|
|
|
|
return Endpoint::Configure(conf, dnsConf);
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::HasLocalIP(const huint128_t& ip) const
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
return m_IPToAddr.find(ip) != m_IPToAddr.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::QueueOutboundTraffic(llarp::net::IPPacket&& pkt)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
return m_NetworkToUserPktQueue.EmplaceIf(
|
2020-04-07 18:38:56 +00:00
|
|
|
[](llarp::net::IPPacket&) -> bool { return true; }, std::move(pkt));
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 16:56:35 +00:00
|
|
|
void
|
|
|
|
TunEndpoint::Flush()
|
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
FlushSend();
|
|
|
|
Pump(Now());
|
2018-12-15 16:56:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:48:23 +00:00
|
|
|
static bool
|
2020-04-07 18:38:56 +00:00
|
|
|
is_random_snode(const dns::Message& msg)
|
2019-03-20 15:48:23 +00:00
|
|
|
{
|
2019-04-26 12:11:34 +00:00
|
|
|
return msg.questions[0].IsName("random.snode");
|
2019-03-20 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-04-07 18:38:56 +00:00
|
|
|
is_localhost_loki(const dns::Message& msg)
|
2019-03-20 15:48:23 +00:00
|
|
|
{
|
2020-08-31 23:25:58 +00:00
|
|
|
return msg.questions[0].IsLocalhost();
|
2019-03-20 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 00:37:58 +00:00
|
|
|
template <>
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::FindAddrForIP(service::Address& addr, huint128_t ip)
|
2019-11-29 00:37:58 +00:00
|
|
|
{
|
|
|
|
auto itr = m_IPToAddr.find(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_IPToAddr.end() and not m_SNodes[itr->second])
|
2019-11-29 00:37:58 +00:00
|
|
|
{
|
|
|
|
addr = service::Address(itr->second.as_array());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::FindAddrForIP(RouterID& addr, huint128_t ip)
|
2019-11-29 00:37:58 +00:00
|
|
|
{
|
|
|
|
auto itr = m_IPToAddr.find(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_IPToAddr.end() and m_SNodes[itr->second])
|
2019-11-29 00:37:58 +00:00
|
|
|
{
|
|
|
|
addr = RouterID(itr->second.as_array());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
static dns::Message&
|
|
|
|
clear_dns_message(dns::Message& msg)
|
2020-02-13 15:44:43 +00:00
|
|
|
{
|
|
|
|
msg.authorities.resize(0);
|
|
|
|
msg.additional.resize(0);
|
|
|
|
msg.answers.resize(0);
|
|
|
|
msg.hdr_fields &= ~dns::flags_RCODENameError;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)> reply)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-03-12 12:19:37 +00:00
|
|
|
auto ReplyToSNodeDNSWhenReady = [self = this, reply = reply](
|
2020-04-07 18:38:56 +00:00
|
|
|
RouterID snode, auto msg, bool isV6) -> bool {
|
|
|
|
return self->EnsurePathToSNode(snode, [=](const RouterID&, exit::BaseSession_ptr s) {
|
|
|
|
self->SendDNSReply(snode, s, msg, reply, true, isV6);
|
|
|
|
});
|
2020-03-12 12:19:37 +00:00
|
|
|
};
|
|
|
|
auto ReplyToLokiDNSWhenReady = [self = this, reply = reply](
|
2020-04-07 18:38:56 +00:00
|
|
|
service::Address addr, auto msg, bool isV6) -> bool {
|
2020-03-12 12:19:37 +00:00
|
|
|
using service::Address;
|
|
|
|
using service::OutboundContext;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (self->HasAddress(addr))
|
2020-03-28 20:26:16 +00:00
|
|
|
{
|
|
|
|
const auto ip = self->ObtainIPForAddr(addr, false);
|
|
|
|
msg->AddINReply(ip, isV6);
|
|
|
|
reply(*msg);
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-12 12:19:37 +00:00
|
|
|
return self->EnsurePathToService(
|
|
|
|
addr,
|
2020-04-07 18:38:56 +00:00
|
|
|
[=](const Address&, OutboundContext* ctx) {
|
2020-03-12 12:19:37 +00:00
|
|
|
self->SendDNSReply(addr, ctx, msg, reply, false, isV6);
|
|
|
|
},
|
|
|
|
2s);
|
|
|
|
};
|
2020-08-31 20:07:17 +00:00
|
|
|
|
|
|
|
auto ReplyToLokiSRVWhenReady = [self = this, reply = reply](
|
|
|
|
service::Address addr, auto msg) -> bool {
|
|
|
|
using service::Address;
|
|
|
|
using service::OutboundContext;
|
|
|
|
|
|
|
|
return self->EnsurePathToService(
|
|
|
|
addr,
|
|
|
|
[=](const Address&, OutboundContext* ctx) {
|
|
|
|
if (ctx == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& introset = ctx->GetCurrentIntroSet();
|
2020-08-31 23:25:58 +00:00
|
|
|
msg->AddSRVReply(introset.GetMatchingSRVRecords(addr.subdomain));
|
2020-08-31 20:07:17 +00:00
|
|
|
reply(*msg);
|
|
|
|
},
|
|
|
|
2s);
|
|
|
|
};
|
|
|
|
|
2020-02-12 20:43:37 +00:00
|
|
|
std::string qname;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.answers.size() > 0)
|
2020-02-12 20:43:37 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto& answer = msg.answers[0];
|
|
|
|
if (answer.HasCNameForTLD(".snode"))
|
2020-02-13 15:44:43 +00:00
|
|
|
{
|
|
|
|
dns::Name_t qname;
|
|
|
|
llarp_buffer_t buf(answer.rData);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not dns::DecodeName(&buf, qname, true))
|
2020-02-13 15:44:43 +00:00
|
|
|
return false;
|
|
|
|
RouterID addr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not addr.FromString(qname))
|
2020-02-13 15:44:43 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto replyMsg = std::make_shared<dns::Message>(clear_dns_message(msg));
|
2020-03-12 12:19:37 +00:00
|
|
|
return ReplyToSNodeDNSWhenReady(addr, std::move(replyMsg), false);
|
2020-02-13 15:44:43 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (answer.HasCNameForTLD(".loki"))
|
2020-02-12 20:43:37 +00:00
|
|
|
{
|
|
|
|
dns::Name_t qname;
|
|
|
|
llarp_buffer_t buf(answer.rData);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not dns::DecodeName(&buf, qname, true))
|
2020-02-12 20:43:37 +00:00
|
|
|
return false;
|
|
|
|
service::Address addr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not addr.FromString(qname))
|
2020-02-12 20:43:37 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto replyMsg = std::make_shared<dns::Message>(clear_dns_message(msg));
|
2020-03-12 12:19:37 +00:00
|
|
|
return ReplyToLokiDNSWhenReady(addr, replyMsg, false);
|
2020-02-12 20:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions.size() != 1)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("bad number of dns questions: ", msg.questions.size());
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-12 20:43:37 +00:00
|
|
|
qname = msg.questions[0].Name();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].qtype == dns::qTypeMX)
|
2018-12-07 21:52:19 +00:00
|
|
|
{
|
|
|
|
// mx record
|
2019-02-05 14:23:51 +00:00
|
|
|
service::Address addr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (addr.FromString(qname, ".loki") || addr.FromString(qname, ".snode")
|
|
|
|
|| is_random_snode(msg) || is_localhost_loki(msg))
|
2018-12-07 21:52:19 +00:00
|
|
|
msg.AddMXReply(qname, 1);
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
2018-12-07 22:08:23 +00:00
|
|
|
reply(msg);
|
2018-12-07 21:52:19 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypeCNAME)
|
2019-01-10 15:49:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (is_random_snode(msg))
|
2019-01-10 15:49:08 +00:00
|
|
|
{
|
|
|
|
RouterID random;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (Router()->GetRandomGoodRouter(random))
|
2020-03-12 12:19:37 +00:00
|
|
|
{
|
2019-01-10 15:49:08 +00:00
|
|
|
msg.AddCNAMEReply(random.ToString(), 1);
|
2020-03-12 12:19:37 +00:00
|
|
|
}
|
2019-01-10 15:49:08 +00:00
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (is_localhost_loki(msg))
|
2019-01-28 15:26:35 +00:00
|
|
|
{
|
|
|
|
size_t counter = 0;
|
|
|
|
context->ForEachService(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&](const std::string&, const std::shared_ptr<service::Endpoint>& service) -> bool {
|
2019-07-05 14:41:26 +00:00
|
|
|
const service::Address addr = service->GetIdentity().pub.Addr();
|
2019-01-28 15:26:35 +00:00
|
|
|
msg.AddCNAMEReply(addr.ToString(), 1);
|
|
|
|
++counter;
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 18:38:56 +00:00
|
|
|
if (counter == 0)
|
2019-01-28 15:26:35 +00:00
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
2019-01-10 15:49:08 +00:00
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
2019-02-05 14:03:38 +00:00
|
|
|
reply(msg);
|
2019-01-10 15:49:08 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeAAAA)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-03-09 19:40:43 +00:00
|
|
|
const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA;
|
2019-06-11 16:44:05 +00:00
|
|
|
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
|
2018-12-03 22:22:59 +00:00
|
|
|
llarp::service::Address addr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isV6 && !SupportsV6())
|
2020-03-09 20:03:44 +00:00
|
|
|
{ // empty reply but not a NXDOMAIN so that client can retry IPv4
|
|
|
|
msg.AddNSReply("localhost.loki.");
|
2020-03-09 19:40:43 +00:00
|
|
|
}
|
2019-02-05 03:19:06 +00:00
|
|
|
// on MacOS this is a typeA query
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (is_random_snode(msg))
|
2019-02-05 03:19:06 +00:00
|
|
|
{
|
2019-02-05 21:04:30 +00:00
|
|
|
RouterID random;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (Router()->GetRandomGoodRouter(random))
|
2020-03-12 12:19:37 +00:00
|
|
|
{
|
2019-02-05 21:04:30 +00:00
|
|
|
msg.AddCNAMEReply(random.ToString(), 1);
|
2020-04-07 18:38:56 +00:00
|
|
|
return ReplyToSNodeDNSWhenReady(random, std::make_shared<dns::Message>(msg), isV6);
|
2020-03-12 12:19:37 +00:00
|
|
|
}
|
2019-02-05 21:04:30 +00:00
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
2019-02-05 03:19:06 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (is_localhost_loki(msg))
|
2018-12-13 00:03:19 +00:00
|
|
|
{
|
2019-01-28 15:26:35 +00:00
|
|
|
size_t counter = 0;
|
|
|
|
context->ForEachService(
|
2020-04-07 18:38:56 +00:00
|
|
|
[&](const std::string&, const std::shared_ptr<service::Endpoint>& service) -> bool {
|
|
|
|
if (!service->HasIfAddr())
|
2019-07-01 13:44:25 +00:00
|
|
|
return true;
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t ip = service->GetIfAddr();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ip.h)
|
2019-01-28 15:26:35 +00:00
|
|
|
{
|
2019-03-27 13:36:11 +00:00
|
|
|
msg.AddINReply(ip, isV6);
|
2019-01-28 15:26:35 +00:00
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 18:38:56 +00:00
|
|
|
if (counter == 0)
|
2018-12-13 00:03:19 +00:00
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (addr.FromString(qname, ".loki"))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isV4 && SupportsV6())
|
2018-12-07 20:56:01 +00:00
|
|
|
{
|
2019-06-11 18:23:53 +00:00
|
|
|
msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA;
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
2018-12-07 20:56:01 +00:00
|
|
|
else
|
2019-01-07 22:15:31 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return ReplyToLokiDNSWhenReady(addr, std::make_shared<dns::Message>(msg), isV6);
|
2019-01-07 22:15:31 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (addr.FromString(qname, ".snode"))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isV4 && SupportsV6())
|
2019-06-12 13:48:14 +00:00
|
|
|
{
|
|
|
|
msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-12 12:19:37 +00:00
|
|
|
return ReplyToSNodeDNSWhenReady(
|
2020-04-07 18:38:56 +00:00
|
|
|
addr.as_array(), std::make_shared<dns::Message>(msg), isV6);
|
2019-06-12 13:48:14 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
else
|
2018-12-04 16:16:43 +00:00
|
|
|
msg.AddNXReply();
|
|
|
|
|
|
|
|
reply(msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypePTR)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
// reverse dns
|
2019-06-12 13:48:14 +00:00
|
|
|
huint128_t ip = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!dns::DecodePTR(msg.questions[0].qname, ip))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
msg.AddNXReply();
|
|
|
|
reply(msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-29 00:37:58 +00:00
|
|
|
RouterID snodeAddr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (FindAddrForIP(snodeAddr, ip))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-11-29 00:37:58 +00:00
|
|
|
msg.AddAReply(snodeAddr.ToString());
|
2018-12-04 16:16:43 +00:00
|
|
|
reply(msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-29 00:37:58 +00:00
|
|
|
service::Address lokiAddr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (FindAddrForIP(lokiAddr, ip))
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-11-29 00:37:58 +00:00
|
|
|
msg.AddAReply(lokiAddr.ToString());
|
2018-12-04 16:16:43 +00:00
|
|
|
reply(msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-04 16:16:43 +00:00
|
|
|
msg.AddNXReply();
|
|
|
|
reply(msg);
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-31 20:07:17 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypeSRV)
|
|
|
|
{
|
|
|
|
llarp::service::Address addr;
|
|
|
|
|
|
|
|
if (is_localhost_loki(msg))
|
|
|
|
{
|
2020-08-31 23:25:58 +00:00
|
|
|
msg.AddSRVReply(introSet().GetMatchingSRVRecords(msg.questions[0].Subdomains()));
|
2020-08-31 20:07:17 +00:00
|
|
|
reply(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (addr.FromString(qname, ".loki"))
|
|
|
|
{
|
2020-08-31 23:25:58 +00:00
|
|
|
llarp::LogDebug("SRV request for: ", qname);
|
|
|
|
|
2020-08-31 20:07:17 +00:00
|
|
|
return ReplyToLokiSRVWhenReady(addr, std::make_shared<dns::Message>(msg));
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
else
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
msg.AddNXReply();
|
|
|
|
reply(msg);
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:46:38 +00:00
|
|
|
void
|
|
|
|
TunEndpoint::ResetInternalState()
|
|
|
|
{
|
|
|
|
service::Endpoint::ResetInternalState();
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::SupportsV6() const
|
|
|
|
{
|
|
|
|
return m_UseV6;
|
|
|
|
}
|
|
|
|
|
2019-03-20 03:18:38 +00:00
|
|
|
// FIXME: pass in which question it should be addressing
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::ShouldHookDNSMessage(const dns::Message& msg) const
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
|
|
|
llarp::service::Address addr;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions.size() == 1)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-05-01 13:40:10 +00:00
|
|
|
/// hook every .loki
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].HasTLD(".loki"))
|
2018-12-13 00:03:19 +00:00
|
|
|
return true;
|
2019-05-01 13:40:10 +00:00
|
|
|
/// hook every .snode
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].HasTLD(".snode"))
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
2019-01-10 15:49:08 +00:00
|
|
|
// hook any ranges we own
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].qtype == llarp::dns::qTypePTR)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2019-06-12 13:48:14 +00:00
|
|
|
huint128_t ip = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!dns::DecodePTR(msg.questions[0].qname, ip))
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2019-06-12 13:48:14 +00:00
|
|
|
return m_OurRange.Contains(ip);
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& answer : msg.answers)
|
2020-02-12 20:43:37 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (answer.HasCNameForTLD(".loki"))
|
2020-02-12 20:43:37 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (answer.HasCNameForTLD(".snode"))
|
2020-02-12 20:43:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::MapAddress(const service::Address& addr, huint128_t ip, bool SNode)
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
|
|
|
auto itr = m_IPToAddr.find(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_IPToAddr.end())
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn(
|
|
|
|
ip, " already mapped to ", service::Address(itr->second.as_array()).ToString());
|
2018-08-22 15:52:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::LogInfo(Name() + " map ", addr.ToString(), " to ", ip);
|
2018-10-19 15:04:14 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
m_IPToAddr[ip] = addr;
|
2019-01-02 01:04:08 +00:00
|
|
|
m_AddrToIP[addr] = ip;
|
2020-04-07 18:38:56 +00:00
|
|
|
m_SNodes[addr] = SNode;
|
2018-09-10 11:08:09 +00:00
|
|
|
MarkIPActiveForever(ip);
|
2018-08-22 15:52:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-21 15:07:37 +00:00
|
|
|
std::string
|
|
|
|
TunEndpoint::GetIfName() const
|
|
|
|
{
|
|
|
|
return m_IfName;
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::Start()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!Endpoint::Start())
|
2019-01-16 21:08:00 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Couldn't start endpoint");
|
2018-08-16 14:34:15 +00:00
|
|
|
return false;
|
2019-01-16 21:08:00 +00:00
|
|
|
}
|
2018-08-21 18:39:18 +00:00
|
|
|
return SetupNetworking();
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::IsSNode() const
|
2018-11-30 14:14:30 +00:00
|
|
|
{
|
|
|
|
// TODO : implement me
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::SetupTun()
|
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
lazy_vpn vpn;
|
|
|
|
huint32_t ip;
|
2019-04-08 12:01:52 +00:00
|
|
|
auto loop = EndpointNetLoop();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tunif == nullptr)
|
2018-09-23 16:48:43 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp::LogInfo(Name(), " waiting for vpn to start");
|
2020-04-07 18:38:56 +00:00
|
|
|
vpn = m_LazyVPNPromise.get_future().get();
|
2019-10-04 18:10:58 +00:00
|
|
|
vpnif = vpn.io;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (vpnif == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(Name(), " failed to recieve vpn interface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::LogInfo(Name(), " got vpn interface");
|
|
|
|
auto self = shared_from_this();
|
|
|
|
// function to queue a packet to send to vpn interface
|
2020-04-07 18:38:56 +00:00
|
|
|
auto sendpkt = [self](net::IPPacket& pkt) -> bool {
|
2019-10-04 18:10:58 +00:00
|
|
|
// drop if no endpoint
|
|
|
|
auto impl = self->GetVPNImpl();
|
|
|
|
// drop if no vpn interface
|
2020-04-07 18:38:56 +00:00
|
|
|
if (impl == nullptr)
|
2019-10-04 18:10:58 +00:00
|
|
|
return true;
|
|
|
|
// drop if queue to vpn not enabled
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not impl->reader.queue.enabled())
|
2019-10-04 18:10:58 +00:00
|
|
|
return true;
|
|
|
|
// drop if queue to vpn full
|
2020-04-07 18:38:56 +00:00
|
|
|
if (impl->reader.queue.full())
|
2019-10-04 18:10:58 +00:00
|
|
|
return true;
|
|
|
|
// queue to reader
|
|
|
|
impl->reader.queue.pushBack(pkt);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
// event loop ticker
|
|
|
|
auto ticker = [self, sendpkt]() {
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint* ep = self.get();
|
2019-10-04 18:10:58 +00:00
|
|
|
const bool running = not ep->IsStopped();
|
2020-04-07 18:38:56 +00:00
|
|
|
auto impl = ep->GetVPNImpl();
|
|
|
|
if (impl)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
/// get packets from vpn
|
2020-04-07 18:38:56 +00:00
|
|
|
while (not impl->writer.queue.empty())
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
// queue it to be sent over lokinet
|
|
|
|
auto pkt = impl->writer.queue.popFront();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (running)
|
2019-10-04 18:10:58 +00:00
|
|
|
ep->m_UserToNetworkPktQueue.Emplace(pkt);
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 16:48:43 +00:00
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
// process packets queued from vpn
|
2020-04-07 18:38:56 +00:00
|
|
|
if (running)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
ep->Flush();
|
|
|
|
ep->FlushToUser(sendpkt);
|
|
|
|
}
|
|
|
|
// if impl has a tick function call it
|
2020-04-07 18:38:56 +00:00
|
|
|
if (impl && impl->parent && impl->parent->tick)
|
2019-10-04 18:10:58 +00:00
|
|
|
impl->parent->tick(impl->parent);
|
|
|
|
};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not loop->add_ticker(ticker))
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(Name(), " failed to add vpn to event loop");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (vpnif->injected)
|
2019-10-04 18:10:58 +00:00
|
|
|
vpnif->injected(vpnif, false);
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!llarp_ev_add_tun(loop.get(), tunif.get()))
|
2019-06-11 16:44:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogError(
|
|
|
|
Name(), " failed to set up tun interface: ", tunif->ifaddr, " on ", tunif->ifname);
|
2019-06-11 16:44:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
const char* ifname;
|
|
|
|
const char* ifaddr;
|
2019-10-04 18:10:58 +00:00
|
|
|
unsigned char netmask;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tunif)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
ifname = tunif->ifname;
|
|
|
|
ifaddr = tunif->ifaddr;
|
2019-10-04 18:10:58 +00:00
|
|
|
netmask = tunif->netmask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
ifname = vpn.info.ifname;
|
|
|
|
ifaddr = vpn.info.ifaddr;
|
2019-10-04 18:10:58 +00:00
|
|
|
netmask = vpn.info.netmask;
|
|
|
|
}
|
2020-08-21 15:07:37 +00:00
|
|
|
m_IfName = ifname;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ip.FromString(ifaddr))
|
2019-06-11 16:44:05 +00:00
|
|
|
{
|
2020-05-20 19:14:05 +00:00
|
|
|
m_OurIP = net::ExpandV4(ip);
|
2019-10-04 18:10:58 +00:00
|
|
|
m_OurRange.netmask_bits = netmask_ipv6_bits(netmask + 96);
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (m_OurIP.FromString(ifaddr))
|
2019-06-11 16:44:05 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
m_OurRange.netmask_bits = netmask_ipv6_bits(netmask);
|
2020-04-07 18:38:56 +00:00
|
|
|
m_UseV6 = true;
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError(Name(), " invalid interface address given, ifaddr=", ifaddr);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (vpnif && vpnif->injected)
|
2019-10-04 18:10:58 +00:00
|
|
|
vpnif->injected(vpnif, false);
|
|
|
|
return false;
|
2018-09-23 16:48:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
m_NextIP = m_OurIP;
|
2019-06-11 16:44:05 +00:00
|
|
|
m_OurRange.addr = m_OurIP;
|
2020-04-07 18:38:56 +00:00
|
|
|
m_MaxIP = m_OurRange.HighestAddr();
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp::LogInfo(Name(), " set ", ifname, " to have address ", m_OurIP);
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogInfo(Name(), " allocated up to ", m_MaxIP, " on range ", m_OurRange);
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2019-11-29 00:37:58 +00:00
|
|
|
const service::Address ourAddr = m_Identity.pub.Addr();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not MapAddress(ourAddr, GetIfAddr(), false))
|
2019-11-29 00:37:58 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_OnUp)
|
2019-04-22 12:25:25 +00:00
|
|
|
{
|
2019-04-22 14:00:59 +00:00
|
|
|
m_OnUp->NotifyAsync(NotifyParams());
|
2019-04-22 12:25:25 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (vpnif && vpnif->injected)
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
|
|
|
vpnif->injected(vpnif, true);
|
|
|
|
}
|
2019-11-29 00:37:58 +00:00
|
|
|
|
|
|
|
return HasAddress(ourAddr);
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<std::string, std::string>
|
2019-04-22 14:00:59 +00:00
|
|
|
TunEndpoint::NotifyParams() const
|
|
|
|
{
|
|
|
|
auto env = Endpoint::NotifyParams();
|
|
|
|
env.emplace("IP_ADDR", m_OurIP.ToString());
|
|
|
|
env.emplace("IF_ADDR", m_OurRange.ToString());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tunif)
|
2019-10-04 18:10:58 +00:00
|
|
|
env.emplace("IF_NAME", tunif->ifname);
|
2019-05-06 12:42:21 +00:00
|
|
|
std::string strictConnect;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& addr : m_StrictConnectAddrs)
|
2020-05-06 20:38:44 +00:00
|
|
|
strictConnect += addr.toString() + " ";
|
2019-05-06 12:42:21 +00:00
|
|
|
env.emplace("STRICT_CONNECT_ADDRS", strictConnect);
|
2019-04-22 14:00:59 +00:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::SetupNetworking()
|
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
llarp::LogInfo("Set Up networking for ", Name());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!SetupTun())
|
2018-10-04 13:42:22 +00:00
|
|
|
{
|
2018-12-03 22:22:59 +00:00
|
|
|
llarp::LogError(Name(), " failed to set up network interface");
|
|
|
|
return false;
|
2018-10-04 13:42:22 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!m_Resolver->Start(m_LocalResolverAddr, m_UpstreamResolvers))
|
2018-09-22 10:25:16 +00:00
|
|
|
{
|
2019-01-29 11:17:21 +00:00
|
|
|
// downgrade DNS server failure to a warning
|
|
|
|
llarp::LogWarn(Name(), " failed to start dns server");
|
|
|
|
// return false;
|
2018-09-22 10:25:16 +00:00
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TunEndpoint::Tick(llarp_time_t now)
|
|
|
|
{
|
2019-11-20 19:45:23 +00:00
|
|
|
Endpoint::Tick(now);
|
2018-08-16 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
TunEndpoint::Stop()
|
|
|
|
{
|
|
|
|
return llarp::service::Endpoint::Stop();
|
|
|
|
}
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
void
|
|
|
|
TunEndpoint::FlushSend()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_UserToNetworkPktQueue.Process([&](net::IPPacket& pkt) {
|
|
|
|
std::function<bool(const llarp_buffer_t&)> sendFunc;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
huint128_t dst, src;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pkt.IsV4())
|
2020-05-21 14:18:23 +00:00
|
|
|
{
|
|
|
|
dst = pkt.dst4to6();
|
|
|
|
src = pkt.src4to6();
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
else
|
2020-05-21 14:18:23 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
dst = pkt.dstv6();
|
2020-05-21 14:18:23 +00:00
|
|
|
src = pkt.srcv6();
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
auto itr = m_IPToAddr.find(dst);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == m_IPToAddr.end())
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2020-06-24 13:24:07 +00:00
|
|
|
const auto exits = m_ExitMap.FindAll(dst);
|
|
|
|
if (IsBogon(dst) or exits.empty())
|
2019-06-11 19:48:21 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
// send icmp unreachable
|
|
|
|
const auto icmp = pkt.MakeICMPUnreachable();
|
|
|
|
if (icmp.has_value())
|
2019-09-03 15:56:56 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
HandleWriteIPPacket(icmp->ConstBuffer(), dst, src);
|
2019-09-03 15:56:56 +00:00
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-24 13:24:07 +00:00
|
|
|
const auto addr = *exits.begin();
|
2020-05-21 14:18:23 +00:00
|
|
|
pkt.ZeroSourceAddress();
|
2020-06-24 13:24:07 +00:00
|
|
|
MarkAddressOutbound(addr);
|
2020-05-21 14:18:23 +00:00
|
|
|
EnsurePathToService(
|
2020-06-24 13:24:07 +00:00
|
|
|
addr,
|
2020-08-25 18:57:15 +00:00
|
|
|
[addr, pkt, self = this](service::Address, service::OutboundContext* ctx) {
|
|
|
|
if (ctx)
|
|
|
|
{
|
|
|
|
ctx->sendTimeout = 5s;
|
|
|
|
}
|
2020-06-24 13:24:07 +00:00
|
|
|
self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::eProtocolExit);
|
2020-05-21 14:18:23 +00:00
|
|
|
},
|
|
|
|
1s);
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
return;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_SNodes.at(itr->second))
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
sendFunc = std::bind(
|
|
|
|
&TunEndpoint::SendToSNodeOrQueue,
|
|
|
|
this,
|
|
|
|
itr->second.as_array(),
|
|
|
|
std::placeholders::_1);
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
else if (m_state->m_ExitEnabled)
|
|
|
|
{
|
|
|
|
sendFunc = std::bind(
|
|
|
|
&TunEndpoint::SendToServiceOrQueue,
|
|
|
|
this,
|
|
|
|
service::Address(itr->second.as_array()),
|
|
|
|
std::placeholders::_1,
|
|
|
|
service::eProtocolExit);
|
|
|
|
}
|
2018-12-02 18:07:07 +00:00
|
|
|
else
|
2018-11-29 13:12:35 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
sendFunc = std::bind(
|
|
|
|
&TunEndpoint::SendToServiceOrQueue,
|
|
|
|
this,
|
|
|
|
service::Address(itr->second.as_array()),
|
|
|
|
std::placeholders::_1,
|
|
|
|
pkt.ServiceProtocol());
|
2018-11-29 13:12:35 +00:00
|
|
|
}
|
2018-10-10 00:28:53 +00:00
|
|
|
// prepare packet for insertion into network
|
2018-10-10 21:29:49 +00:00
|
|
|
// this includes clearing IP addresses, recalculating checksums, etc
|
2020-05-21 14:18:23 +00:00
|
|
|
if (not m_state->m_ExitEnabled)
|
|
|
|
{
|
|
|
|
if (pkt.IsV4())
|
|
|
|
pkt.UpdateIPv4Address({0}, {0});
|
|
|
|
else
|
|
|
|
pkt.UpdateIPv6Address({0}, {0});
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sendFunc && sendFunc(pkt.Buffer()))
|
2019-08-01 12:20:51 +00:00
|
|
|
{
|
|
|
|
MarkIPActive(dst);
|
2019-06-11 16:44:05 +00:00
|
|
|
return;
|
2019-08-01 12:20:51 +00:00
|
|
|
}
|
2018-11-29 13:12:35 +00:00
|
|
|
llarp::LogWarn(Name(), " did not flush packets");
|
2018-08-22 15:52:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:08:47 +00:00
|
|
|
bool
|
2020-05-21 14:18:23 +00:00
|
|
|
TunEndpoint::HandleInboundPacket(
|
|
|
|
const service::ConvoTag tag, const llarp_buffer_t& buf, service::ProtocolType t)
|
|
|
|
{
|
|
|
|
if (t != service::eProtocolTrafficV4 && t != service::eProtocolTrafficV6
|
|
|
|
&& t != service::eProtocolExit)
|
|
|
|
return false;
|
|
|
|
AlignedBuffer<32> addr;
|
|
|
|
bool snode = false;
|
|
|
|
if (!GetEndpointWithConvoTag(tag, addr, snode))
|
|
|
|
return false;
|
|
|
|
huint128_t src, dst;
|
|
|
|
|
|
|
|
net::IPPacket pkt;
|
|
|
|
if (not pkt.Load(buf))
|
|
|
|
return false;
|
2020-06-24 13:24:07 +00:00
|
|
|
if (m_state->m_ExitEnabled)
|
2020-05-21 14:18:23 +00:00
|
|
|
{
|
|
|
|
// exit side from exit
|
|
|
|
src = ObtainIPForAddr(addr, snode);
|
|
|
|
if (pkt.IsV4())
|
|
|
|
dst = pkt.dst4to6();
|
|
|
|
else if (pkt.IsV6())
|
|
|
|
dst = pkt.dstv6();
|
|
|
|
}
|
2020-06-24 13:24:07 +00:00
|
|
|
else if (t == service::eProtocolExit)
|
|
|
|
{
|
|
|
|
// client side exit traffic from exit
|
|
|
|
if (pkt.IsV4())
|
|
|
|
src = pkt.src4to6();
|
|
|
|
else if (pkt.IsV6())
|
|
|
|
src = pkt.srcv6();
|
|
|
|
dst = m_OurIP;
|
|
|
|
// find what exit we think this should be for
|
|
|
|
const auto mapped = m_ExitMap.FindAll(src);
|
|
|
|
if (mapped.count(service::Address{addr}) == 0 or IsBogon(src))
|
|
|
|
{
|
|
|
|
// we got exit traffic from someone who we should not have gotten it from
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// snapp traffic
|
|
|
|
src = ObtainIPForAddr(addr, snode);
|
|
|
|
dst = m_OurIP;
|
|
|
|
}
|
|
|
|
HandleWriteIPPacket(buf, src, dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TunEndpoint::HandleWriteIPPacket(const llarp_buffer_t& b, huint128_t src, huint128_t dst)
|
2018-08-18 14:01:21 +00:00
|
|
|
{
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buf(b);
|
2020-05-21 14:18:23 +00:00
|
|
|
return m_NetworkToUserPktQueue.EmplaceIf([buf, src, dst](net::IPPacket& pkt) -> bool {
|
2020-04-07 18:38:56 +00:00
|
|
|
// load
|
|
|
|
if (!pkt.Load(buf))
|
|
|
|
return false;
|
|
|
|
if (pkt.IsV4())
|
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
pkt.UpdateIPv4Address(xhtonl(net::TruncateV6(src)), xhtonl(net::TruncateV6(dst)));
|
2020-04-07 18:38:56 +00:00
|
|
|
}
|
|
|
|
else if (pkt.IsV6())
|
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
pkt.UpdateIPv6Address(src, dst);
|
2020-04-07 18:38:56 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2018-08-20 19:12:12 +00:00
|
|
|
}
|
2018-10-19 15:04:14 +00:00
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2018-11-14 12:23:08 +00:00
|
|
|
TunEndpoint::GetIfAddr() const
|
2018-10-19 14:53:06 +00:00
|
|
|
{
|
2018-11-14 12:23:08 +00:00
|
|
|
return m_OurIP;
|
2018-10-19 14:53:06 +00:00
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::ObtainIPForAddr(const AlignedBuffer<32>& ident, bool snode)
|
2018-08-20 19:12:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_time_t now = Now();
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t nextIP = {0};
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
|
|
|
// previously allocated address
|
2018-11-14 20:56:54 +00:00
|
|
|
auto itr = m_AddrToIP.find(ident);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_AddrToIP.end())
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
|
|
|
// mark ip active
|
2018-09-16 12:25:17 +00:00
|
|
|
MarkIPActive(itr->second);
|
2018-08-21 18:17:16 +00:00
|
|
|
return itr->second;
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
2018-09-13 16:41:53 +00:00
|
|
|
// allocate new address
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_NextIP < m_MaxIP)
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
2018-09-19 14:49:42 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
nextIP = ++m_NextIP;
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (m_IPToAddr.find(nextIP) != m_IPToAddr.end() && m_NextIP < m_MaxIP);
|
|
|
|
if (nextIP < m_MaxIP)
|
2018-09-19 14:49:42 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_AddrToIP[ident] = nextIP;
|
2018-11-14 21:40:44 +00:00
|
|
|
m_IPToAddr[nextIP] = ident;
|
2020-04-07 18:38:56 +00:00
|
|
|
m_SNodes[ident] = snode;
|
2018-11-14 20:56:54 +00:00
|
|
|
llarp::LogInfo(Name(), " mapped ", ident, " to ", nextIP);
|
2018-09-19 14:49:42 +00:00
|
|
|
MarkIPActive(nextIP);
|
|
|
|
return nextIP;
|
|
|
|
}
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 14:49:42 +00:00
|
|
|
// we are full
|
|
|
|
// expire least active ip
|
|
|
|
// TODO: prevent DoS
|
2020-04-07 18:38:56 +00:00
|
|
|
std::pair<huint128_t, llarp_time_t> oldest = {huint128_t{0}, 0s};
|
2018-09-19 14:49:42 +00:00
|
|
|
|
|
|
|
// find oldest entry
|
|
|
|
auto itr = m_IPActivity.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_IPActivity.end())
|
2018-09-19 14:49:42 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second <= now)
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((now - itr->second) > oldest.second)
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
oldest.first = itr->first;
|
2018-09-19 14:49:42 +00:00
|
|
|
oldest.second = itr->second;
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 14:49:42 +00:00
|
|
|
++itr;
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
2018-09-19 14:49:42 +00:00
|
|
|
// remap address
|
2018-11-14 21:47:58 +00:00
|
|
|
m_IPToAddr[oldest.first] = ident;
|
2020-04-07 18:38:56 +00:00
|
|
|
m_AddrToIP[ident] = oldest.first;
|
|
|
|
m_SNodes[ident] = snode;
|
|
|
|
nextIP = oldest.first;
|
2018-08-21 18:17:16 +00:00
|
|
|
|
|
|
|
// mark ip active
|
2018-09-10 16:36:36 +00:00
|
|
|
m_IPActivity[nextIP] = std::max(m_IPActivity[nextIP], now);
|
2018-08-20 19:12:12 +00:00
|
|
|
|
|
|
|
return nextIP;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
TunEndpoint::HasRemoteForIP(huint128_t ip) const
|
2018-08-20 19:12:12 +00:00
|
|
|
{
|
|
|
|
return m_IPToAddr.find(ip) != m_IPToAddr.end();
|
2018-08-18 14:01:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
TunEndpoint::MarkIPActive(huint128_t ip)
|
2018-08-21 18:17:16 +00:00
|
|
|
{
|
2019-06-11 21:28:55 +00:00
|
|
|
llarp::LogDebug(Name(), " address ", ip, " is active");
|
2018-10-29 16:48:36 +00:00
|
|
|
m_IPActivity[ip] = std::max(Now(), m_IPActivity[ip]);
|
2018-08-21 18:17:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 11:08:09 +00:00
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
TunEndpoint::MarkIPActiveForever(huint128_t ip)
|
2018-09-10 11:08:09 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_IPActivity[ip] = std::numeric_limits<llarp_time_t>::max();
|
2018-09-10 11:08:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
2018-11-07 15:30:22 +00:00
|
|
|
TunEndpoint::TickTun(__attribute__((unused)) llarp_time_t now)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
|
|
|
// called in the isolated thread
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::tunifBeforeWrite(llarp_tun_io* tun)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-08-21 18:17:16 +00:00
|
|
|
// called in the isolated network thread
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* self = static_cast<TunEndpoint*>(tun->user);
|
2020-06-04 21:25:34 +00:00
|
|
|
self->Flush();
|
2020-05-21 14:18:23 +00:00
|
|
|
self->FlushToUser([self, tun](net::IPPacket& pkt) -> bool {
|
|
|
|
if (not llarp_ev_tun_async_write(tun, pkt.Buffer()))
|
2019-10-04 18:10:58 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
llarp::LogWarn(self->Name(), " packet dropped");
|
2019-10-04 18:10:58 +00:00
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
return false;
|
2019-11-25 13:18:24 +00:00
|
|
|
});
|
2020-05-21 14:18:23 +00:00
|
|
|
} // namespace handlers
|
2018-08-22 15:52:10 +00:00
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
TunEndpoint::tunifRecvPkt(llarp_tun_io* tun, const llarp_buffer_t& b)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
|
|
|
// called for every packet read from user in isolated network thread
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* self = static_cast<TunEndpoint*>(tun->user);
|
2020-05-21 14:18:23 +00:00
|
|
|
self->m_UserToNetworkPktQueue.EmplaceIf([&](net::IPPacket& pkt) { return pkt.Load(b); });
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
TunEndpoint::~TunEndpoint() = default;
|
2018-08-15 15:36:34 +00:00
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|