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

198 lines
5.4 KiB
C++
Raw Normal View History

#ifndef LLARP_HANDLERS_TUN_HPP
#define LLARP_HANDLERS_TUN_HPP
2018-08-16 16:34:15 +02:00
#include <llarp/ev.h>
#include <llarp/codel.hpp>
#include <llarp/ip.hpp>
2018-08-16 16:34:15 +02:00
#include <llarp/service/endpoint.hpp>
#include <llarp/threading.hpp>
2018-09-24 15:09:01 +02:00
#include <llarp/dnsd.hpp> // for relay
#include <llarp/dns_dotlokilookup.hpp> // for lookup
#include <llarp/dns_iptracker.hpp> // for tracker
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";
2018-08-16 16:34:15 +02:00
struct TunEndpoint : public service::Endpoint
{
2018-08-16 16:34:15 +02:00
TunEndpoint(const std::string& nickname, llarp_router* r);
~TunEndpoint();
virtual bool
2018-08-16 16:34:15 +02:00
SetOption(const std::string& k, const std::string& v);
virtual void
2018-08-16 16:34:15 +02:00
Tick(llarp_time_t now);
void
TickTun(llarp_time_t now);
2018-08-22 17:52:10 +02:00
bool
2018-10-10 17:14:45 +02:00
MapAddress(const service::Address& remote, huint32_t ip);
2018-08-22 17:52:10 +02:00
2018-08-16 16:34:15 +02:00
bool
Start();
/// set up tun interface, blocking
bool
SetupTun();
/// overrides Endpoint
bool
SetupNetworking();
2018-09-18 19:48:26 +02:00
/// overrides Endpoint
/// handle inbound traffic
bool
2018-11-29 14:12:35 +01:00
HandleWriteIPPacket(llarp_buffer_t buf, std::function<huint32_t(void)> getFromIP) override;
2018-08-18 16:01:21 +02:00
2018-11-14 13:23:08 +01:00
/// queue outbound packet to the world
bool
QueueOutboundTraffic(llarp::net::IPv4Packet&& pkt);
/// get the local interface's address
huint32_t
GetIfAddr() const;
bool
HasLocalIP(const huint32_t& ip) const;
2018-10-19 18:54:08 +02:00
#ifndef WIN32
/// overrides Endpoint
2018-08-18 16:01:21 +02:00
bool
IsolationFailed()
{
m_TunSetupResult.set_value(false);
return false;
}
2018-08-21 20:40:42 +02:00
#endif
2018-08-18 16:01:21 +02:00
2018-08-16 16:34:15 +02:00
llarp_tun_io tunif;
/// called before writing to tun interface
2018-08-16 16:34:15 +02:00
static void
tunifBeforeWrite(llarp_tun_io* t);
2018-08-16 16:34:15 +02:00
2018-08-22 17:52:10 +02:00
/// handle user to network send buffer flush
/// called in router logic thread
static void
handleNetSend(void*);
/// called every time we wish to read a packet from the tun interface
2018-08-16 16:34:15 +02:00
static void
tunifRecvPkt(llarp_tun_io* t, const void* pkt, ssize_t sz);
/// called in the endpoint logic thread
2018-08-16 16:34:15 +02:00
static void
handleTickTun(void* u);
2018-11-14 13:23:08 +01:00
/// get a key for ip address
template < typename Addr >
Addr
ObtainAddrForIP(huint32_t ip)
{
auto itr = m_IPToAddr.find(ip);
if(itr == m_IPToAddr.end())
{
// not found
Addr addr;
addr.Zero();
return addr;
}
// found
return itr->second.data();
}
2018-10-23 20:18:00 +02:00
bool
2018-11-29 14:12:35 +01:00
HasAddress(const byte_t* addr) const override
2018-10-23 20:18:00 +02:00
{
2018-11-14 13:23:08 +01:00
return m_AddrToIP.find(addr) != m_AddrToIP.end();
2018-10-23 20:18:00 +02:00
}
2018-11-14 13:23:08 +01:00
/// get ip address for key unconditionally
2018-10-23 23:33:49 +02:00
huint32_t
2018-11-29 14:12:35 +01:00
ObtainIPForAddr(const byte_t* addr, bool serviceNode) override;
2018-10-23 23:33:49 +02:00
protected:
using PacketQueue_t = llarp::util::CoDelQueue<
net::IPv4Packet, net::IPv4Packet::GetTime, net::IPv4Packet::PutTime,
net::IPv4Packet::CompareOrder, net::IPv4Packet::GetNow >;
/// queue for sending packets over the network from us
PacketQueue_t m_UserToNetworkPktQueue;
/// queue for sending packets to user from network
PacketQueue_t m_NetworkToUserPktQueue;
/// return true if we have a remote loki address for this ip address
bool
2018-10-10 17:14:45 +02:00
HasRemoteForIP(huint32_t ipv4) const;
/// mark this address as active
void
2018-10-10 17:14:45 +02:00
MarkIPActive(huint32_t ip);
2018-09-10 13:08:09 +02:00
/// mark this address as active forever
void
2018-10-10 17:14:45 +02:00
MarkIPActiveForever(huint32_t ip);
2018-09-10 13:08:09 +02:00
/// flush ip packets
virtual void
2018-08-22 17:52:10 +02:00
FlushSend();
2018-11-14 13:23:08 +01:00
/// maps ip to key (host byte order)
std::unordered_map< huint32_t, AlignedBuffer< 32 >, huint32_t::Hash >
m_IPToAddr;
/// maps key to ip (host byte order)
std::unordered_map< AlignedBuffer< 32 >, huint32_t,
AlignedBuffer< 32 >::Hash >
m_AddrToIP;
2018-11-29 14:12:35 +01:00
/// maps key to true if key is a service node, maps key to false if key is a hidden service
std::unordered_map<AlignedBuffer<32>, bool, AlignedBuffer<32>::Hash> m_SNodes;
2018-08-16 16:34:15 +02:00
private:
2018-11-14 20:34:17 +01:00
bool
QueueInboundPacketForExit(llarp_buffer_t buf)
{
return m_NetworkToUserPktQueue.EmplaceIf(
[&](llarp::net::IPv4Packet& pkt) -> bool {
if(!pkt.Load(buf))
return false;
pkt.UpdateIPv4PacketOnDst(pkt.src(), m_OurIP);
return true;
});
}
2018-10-19 18:54:08 +02:00
#ifndef WIN32
/// handles setup, given value true on success and false on failure to set
/// up interface
2018-08-16 16:34:15 +02:00
std::promise< bool > m_TunSetupResult;
2018-08-21 20:39:18 +02:00
#endif
/// DNS server per tun
struct dnsd_context dnsd;
/// DNS loki lookup subsystem configuration (also holds optional iptracker
/// for netns)
struct dotLokiLookup dll;
/// maps ip address to timestamp last active
2018-10-10 17:50:52 +02:00
std::unordered_map< huint32_t, llarp_time_t, huint32_t::Hash >
m_IPActivity;
2018-09-23 18:47:18 +02:00
/// our ip address (host byte order)
2018-10-10 17:14:45 +02:00
huint32_t m_OurIP;
2018-09-23 18:47:18 +02:00
/// next ip address to allocate (host byte order)
2018-10-10 17:14:45 +02:00
huint32_t m_NextIP;
/// highest ip address to allocate (host byte order)
huint32_t m_MaxIP;
2018-11-11 14:14:19 +01:00
/// upstream dns
llarp::Addr m_UpstreamDNSAddr;
/// local dns
llarp::Addr m_LocalResolverAddr;
};
} // namespace handlers
} // namespace llarp
2018-08-16 16:34:15 +02:00
#endif