From 0eb6431eb1c593e9a9f17a92076a12e5f450bdde Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 8 Jul 2019 11:26:06 -0400 Subject: [PATCH] initialize tun with 0 and set defaults in correct places --- llarp/config/ini.cpp | 1 + llarp/ev/ev.cpp | 43 ++++++------------------------------- llarp/handlers/tun.cpp | 9 ++++---- llarp/handlers/tun.hpp | 5 ----- llarp/router/router.cpp | 45 +++++++++++++++++++++++---------------- llarp/service/context.cpp | 17 --------------- 6 files changed, 40 insertions(+), 80 deletions(-) diff --git a/llarp/config/ini.cpp b/llarp/config/ini.cpp index 70f8df1ae..8b626837b 100644 --- a/llarp/config/ini.cpp +++ b/llarp/config/ini.cpp @@ -132,6 +132,7 @@ namespace llarp return false; } Section_t& sect = m_Config[sectName]; + LogDebug(m_FileName, ": ", sectName, ".", k, "=", v); sect.emplace(k, v); } else // malformed? diff --git a/llarp/ev/ev.cpp b/llarp/ev/ev.cpp index ead48e102..7b5b5eb04 100644 --- a/llarp/ev/ev.cpp +++ b/llarp/ev/ev.cpp @@ -94,46 +94,17 @@ llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to, bool llarp_ev_add_tun(struct llarp_ev_loop *loop, struct llarp_tun_io *tun) { - // llarp::LogInfo("ev creating tunnel ", tun->ifaddr, " on ", tun->ifname); - if(strcmp(tun->ifaddr, "") == 0 || strcmp(tun->ifaddr, "auto") == 0) +#if !defined(_WIN32) + if(tun->ifaddr[0] == 0 || strcmp(tun->ifaddr, "auto") == 0) { - std::string ifaddr = llarp::FindFreeRange(); - auto pos = ifaddr.find("/"); - if(pos == std::string::npos) - { - llarp::LogWarn("Auto ifaddr didn't return a netmask: ", ifaddr); - return false; - } - int num; - std::string part = ifaddr.substr(pos + 1); -#if defined(ANDROID) || defined(RPI) - num = atoi(part.c_str()); -#else - num = std::stoi(part); -#endif - if(num <= 0) - { - llarp::LogError("bad ifaddr netmask value: ", ifaddr); - return false; - } - tun->netmask = num; - const std::string addr = ifaddr.substr(0, pos); - std::copy_n(addr.begin(), std::min(sizeof(tun->ifaddr), addr.size()), - tun->ifaddr); - llarp::LogInfo("IfAddr autodetect: ", tun->ifaddr, "/", tun->netmask); + LogError("invalid ifaddr on tun: ", tun->ifaddr); + return false; } - if(strcmp(tun->ifname, "") == 0 || strcmp(tun->ifname, "auto") == 0) + if(tun->ifname[0] == 0 || strcmp(tun->ifname, "auto") == 0) { - std::string ifname = llarp::FindFreeTun(); - std::copy_n(ifname.begin(), std::min(sizeof(tun->ifname), ifname.size()), - tun->ifname); - llarp::LogInfo("IfName autodetect: ", tun->ifname); + LogError("invalid ifname on tun: ", tun->ifname); + return false; } - llarp::LogDebug("Tun Interface will use the following settings:"); - llarp::LogDebug("IfAddr: ", tun->ifaddr); - llarp::LogDebug("IfName: ", tun->ifname); - llarp::LogDebug("IfNMsk: ", tun->netmask); -#ifndef _WIN32 return loop->tun_listen(tun); #else UNREFERENCED_PARAMETER(loop); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index fff248f25..6d2244268 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -45,18 +45,19 @@ namespace llarp , m_Resolver(std::make_shared< dns::Proxy >( r->netloop(), r->logic(), r->netloop(), r->logic(), this)) { + std::fill(tunif.ifaddr, tunif.ifaddr + sizeof(tunif.ifaddr), 0); + std::fill(tunif.ifname, tunif.ifname + sizeof(tunif.ifname), 0); + tunif.netmask = 0; + #ifdef ANDROID tunif.get_fd_promise = &get_tun_fd_promise; Promise.reset(new llarp_fd_promise(&m_VPNPromise)); #else tunif.get_fd_promise = nullptr; #endif - tunif.user = this; - tunif.netmask = DefaultTunNetmask; + tunif.user = this; // eh this shouldn't do anything on windows anyway - strncpy(tunif.ifaddr, DefaultTunSrcAddr, sizeof(tunif.ifaddr) - 1); - strncpy(tunif.ifname, DefaultTunIfname, sizeof(tunif.ifname) - 1); tunif.tick = &tunifTick; tunif.before_write = &tunifBeforeWrite; tunif.recvpkt = &tunifRecvPkt; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 176aadaff..98d47daf7 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -15,11 +15,6 @@ 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"; - struct TunEndpoint : public service::Endpoint, public dns::IQueryHandler, public std::enable_shared_from_this< TunEndpoint > diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 1a69d8219..21df833bf 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #if defined(RPI) || defined(ANDROID) #include #endif @@ -1426,6 +1427,30 @@ namespace llarp " routers"); } + /// this function ensure there are sane defualts in a net config + static void + EnsureNetConfigDefaultsSane( + std::unordered_multimap< std::string, std::string > &netConfig) + { + static const std::unordered_map< std::string, + std::function< std::string(void) > > + netConfigDefaults = { + {"ifname", llarp::FindFreeTun}, + {"ifaddr", llarp::FindFreeRange}, + {"local-dns", []() -> std::string { return "127.0.0.1:53"; }}}; + // populate with fallback defaults if values not present + auto itr = netConfigDefaults.begin(); + while(itr != netConfigDefaults.end()) + { + auto found = netConfig.find(itr->first); + if(found == netConfig.end() || found->second.empty()) + { + netConfig.emplace(itr->first, itr->second()); + } + ++itr; + } + } + bool Router::Run(struct llarp_nodedb *nodedb) { @@ -1566,6 +1591,8 @@ namespace llarp LogWarn("Link ", link->Name(), " failed to start"); } + EnsureNetConfigDefaultsSane(netConfig); + if(IBLinksStarted > 0) { // initialize as service node @@ -1763,24 +1790,6 @@ namespace llarp bool Router::CreateDefaultHiddenService() { - // fallback defaults - static const std::unordered_map< std::string, - std::function< std::string(void) > > - netConfigDefaults = { - {"ifname", llarp::FindFreeTun}, - {"ifaddr", llarp::FindFreeRange}, - {"local-dns", []() -> std::string { return "127.0.0.1:53"; }}}; - // populate with fallback defaults if values not present - auto itr = netConfigDefaults.begin(); - while(itr != netConfigDefaults.end()) - { - auto found = netConfig.find(itr->first); - if(found == netConfig.end() || found->second.empty()) - { - netConfig.emplace(itr->first, itr->second()); - } - ++itr; - } // add endpoint return hiddenServiceContext().AddDefaultEndpoint(netConfig); } diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index b99e1b271..d1802fd89 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -127,23 +127,6 @@ namespace llarp { item.second->Tick(now); } - /* - std::vector< RouterID > expired; - m_Router->nodedb()->visit([&](const RouterContact &rc) -> bool { - if(rc.IsExpired(now)) - expired.emplace_back(rc.pubkey); - return true; - }); - ForEachService([&](const std::string &, - const std::shared_ptr< Endpoint > &ep) -> bool { - // TODO: we need to stop looking up service nodes that are gone forever - // how do? - for(const auto &k : expired) - if(!ep->LookupRouterAnon(k, nullptr)) - return false; - return true; - }); - */ } bool