From 2b2e0007faa41d104b3bc16561d7d8b2ee6d7d2c Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sat, 22 Sep 2018 03:25:16 -0700 Subject: [PATCH] active DNS relay (server/client) in tun interface --- include/llarp/handlers/tun.hpp | 9 +++++++++ llarp/handlers/tun.cpp | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/include/llarp/handlers/tun.hpp b/include/llarp/handlers/tun.hpp index 795b0509d..8d3a9ed42 100644 --- a/include/llarp/handlers/tun.hpp +++ b/include/llarp/handlers/tun.hpp @@ -5,6 +5,9 @@ #include #include #include +#include "dnsd.hpp" +#include "dns_dotlokilookup.hpp" +#include "dns_iptracker.hpp" namespace llarp { @@ -110,6 +113,12 @@ namespace llarp /// up interface std::promise< bool > m_TunSetupResult; #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 to service address std::unordered_map< uint32_t, service::Address > m_IPToAddr; /// maps service address to ip diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index ee6a13b98..fda84b15a 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -2,6 +2,8 @@ #define __USE_MINGW_ANSI_STDIO 1 #include #include "router.hpp" +#include "dns_iptracker.hpp" +#include "dns_dotlokilookup.hpp" namespace llarp { @@ -16,9 +18,12 @@ namespace llarp tunif.netmask = DefaultTunNetmask; strncpy(tunif.ifaddr, DefaultTunSrcAddr, sizeof(tunif.ifaddr) - 1); strncpy(tunif.ifname, DefaultTunIfname, sizeof(tunif.ifname) - 1); - tunif.tick = nullptr; - tunif.before_write = &tunifBeforeWrite; - tunif.recvpkt = &tunifRecvPkt; + tunif.tick = nullptr; + tunif.before_write = &tunifBeforeWrite; + tunif.recvpkt = &tunifRecvPkt; + this->dll.ip_tracker = nullptr; + this->dll.user = this; + // this->dll.callback = std::bind(&TunEndpoint::MapAddress, this); } bool @@ -82,6 +87,10 @@ namespace llarp llarp::LogInfo(Name() + " set ifaddr to ", addr, " with netmask ", tunif.netmask); strncpy(tunif.ifaddr, addr.c_str(), sizeof(tunif.ifaddr) - 1); + + // set up address in dotLokiLookup + // llarp::Addr tunIp; + // dns_iptracker_setup_dotLokiLookup(&this->dll, tunIp); return true; } return Endpoint::SetOption(k, v); @@ -119,6 +128,15 @@ namespace llarp // set up networking in currrent thread if we are not isolated if(!SetupNetworking()) return false; + + llarp::LogInfo("Setting up global DNS IP tracker"); + llarp::Addr tunIp; + dns_iptracker_setup_dotLokiLookup(&this->dll, tunIp); + } + else + { + llarp::LogInfo("Setting up per netns DNS IP tracker"); + this->dll.ip_tracker = new dns_iptracker; } // wait for result for network setup llarp::LogInfo("waiting for tun interface..."); @@ -165,6 +183,15 @@ namespace llarp #ifndef _WIN32 m_TunSetupResult.set_value(result); #endif + if(!llarp_dnsd_init(&this->dnsd, EndpointLogic(), EndpointNetLoop(), + tunif.ifname, 53, "8.8.8.8", 53)) + { + llarp::LogError("Couldnt init dns daemon"); + } + // configure hook + dnsd.intercept = &llarp_dotlokilookup_handler; + // set dotLokiLookup (this->dll) + dnsd.user = &this->dll; return result; }