diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 3a34b851a..686eb55be 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -71,14 +71,13 @@ namespace llarp conf.defineOption("router", "nickname", false, "", AssignmentAcceptor(m_nickname)); - conf.defineOption( - "router", "data-dir", false, GetDefaultDataDir(), [this](std::string arg) { - fs::path dir = arg; - if (not fs::exists(dir)) + conf.defineOption( + "router", "data-dir", false, params.defaultDataDir, [this](fs::path arg) { + if (not fs::exists(arg)) throw std::runtime_error( stringify("Specified [router]:data-dir ", arg, " does not exist")); - m_dataDir = std::move(dir); + m_dataDir = std::move(arg); }); conf.defineOption("router", "public-address", false, "", [this](std::string arg) { @@ -101,7 +100,6 @@ namespace llarp if (arg <= 0) throw std::invalid_argument("public-port must be > 0"); - // Not needed to flip upside-down - this is done in llarp::Addr(const AddressInfo&) m_ip4addr.sin_port = arg; m_addrInfo.port = arg; m_publicOverride = true; @@ -195,16 +193,33 @@ namespace llarp m_mapAddr = arg; }); - conf.defineOption( - "network", "ifaddr", false, "", [this](std::string arg) { m_ifaddr = arg; }); - conf.defineOption( - "network", "ifname", false, "", [this](std::string arg) { m_ifname = arg; }); + conf.defineOption("network", "ifaddr", false, "", [this](std::string arg) { + if (arg.empty()) + { + const auto maybe = llarp::FindFreeRange(); + if (not maybe.has_value()) + throw std::invalid_argument("cannot determine free ip range"); + arg = maybe.value(); + } + m_ifaddr = arg; + }); + + conf.defineOption("network", "ifname", false, "", [this](std::string arg) { + if (arg.empty()) + { + const auto maybe = llarp::FindFreeTun(); + if (not maybe.has_value()) + throw std::invalid_argument("cannot determine free interface name"); + arg = maybe.value(); + } + m_ifname = arg; + }); conf.defineOption( "network", "blacklist-snode", false, true, "", [this](std::string arg) { RouterID id; if (not id.FromString(arg)) - throw std::invalid_argument(stringify("Invalide RouterID: ", arg)); + throw std::invalid_argument(stringify("Invalid RouterID: ", arg)); auto itr = m_snodeBlacklist.emplace(std::move(id)); if (itr.second) @@ -509,6 +524,8 @@ namespace llarp addIgnoreOption("router", "threads"); addIgnoreOption("metrics", "json-metrics-path"); + + addIgnoreOption("network", "enabled"); } void diff --git a/llarp/context.cpp b/llarp/context.cpp index 43ec511a0..08caf0b66 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -215,7 +215,8 @@ namespace llarp { config = std::make_unique(); configfile = fname; - return Configure(isRelay, {}); + const fs::path filepath(fname); + return Configure(isRelay, filepath.parent_path()); } #ifdef LOKINET_HIVE @@ -305,7 +306,8 @@ extern "C" llarp_config_load_file(const char* fname, struct llarp_config** conf, bool isRelay) { llarp_config* c = new llarp_config(); - if (c->impl.Load(fname, isRelay, {})) + const fs::path filepath(fname); + if (c->impl.Load(fname, isRelay, filepath.parent_path())) { *conf = c; return true; diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index a1fdd5eca..7ed1eac56 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -109,8 +109,8 @@ namespace llarp { if (conf.m_reachable) { - m_PublishIntroSet = true; - LogInfo(Name(), " setting to be reachable by default"); + m_PublishIntroSet = true; + LogInfo(Name(), " setting to be reachable by default"); } else { @@ -252,9 +252,6 @@ namespace llarp } std::string ifname = conf.m_ifname; - if (ifname.empty()) - ifname = FindFreeTun(); - if (tunif) { if (ifname.length() >= sizeof(tunif->ifname)) @@ -267,9 +264,6 @@ namespace llarp } std::string ifaddr = conf.m_ifaddr; - if (ifaddr.empty()) - ifaddr = FindFreeRange(); - if (tunif) { std::string addr; diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index 45f87511e..73e3b3494 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -123,8 +123,19 @@ namespace llarp if (!AllInterfaces(af, m_ourAddr)) return false; } - else if (!GetIFAddr(ifname, m_ourAddr, af)) - m_ourAddr = Addr(ifname); + else + { + const auto maybe = GetIFAddr(ifname, af); + if (maybe.has_value()) + { + m_ourAddr = maybe.value(); + } + else + { + if (not m_ourAddr.FromString(ifname)) + throw std::invalid_argument(stringify("cannot parse network address: ", ifname)); + } + } m_ourAddr.port(port); return llarp_ev_add_udp(m_Loop.get(), &m_udp, m_ourAddr) != -1; } diff --git a/llarp/net/net.cpp b/llarp/net/net.cpp index 569366eb2..74f3d8a02 100644 --- a/llarp/net/net.cpp +++ b/llarp/net/net.cpp @@ -344,10 +344,13 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr) #else if (!strcmp(ifname, "lo") || !strcmp(ifname, "lo0")) { - sockaddr_in* lo = (sockaddr_in*)addr; - lo->sin_family = af; - lo->sin_port = 0; - inet_pton(af, "127.0.0.1", &lo->sin_addr); + if (addr) + { + sockaddr_in* lo = (sockaddr_in*)addr; + lo->sin_family = af; + lo->sin_port = 0; + inet_pton(af, "127.0.0.1", &lo->sin_addr); + } return true; } if (!getifaddrs(&ifa)) @@ -367,13 +370,16 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr) // if(!a.isPrivate()) //{ // llarp::LogInfo(__FILE__, "found ", ifname, " af: ", af); - memcpy(addr, i->ifa_addr, sl); - if (af == AF_INET6) + if (addr) { - // set scope id - auto* ip6addr = (sockaddr_in6*)addr; - ip6addr->sin6_scope_id = if_nametoindex(ifname); - ip6addr->sin6_flowinfo = 0; + memcpy(addr, i->ifa_addr, sl); + if (af == AF_INET6) + { + // set scope id + auto* ip6addr = (sockaddr_in6*)addr; + ip6addr->sin6_scope_id = if_nametoindex(ifname); + ip6addr->sin6_flowinfo = 0; + } } found = true; break; @@ -436,7 +442,7 @@ namespace llarp } // TODO: ipv6? - std::string + std::optional FindFreeRange() { std::vector currentRanges; @@ -505,54 +511,36 @@ namespace llarp return loaddr.ToString() + "/24"; ++oct; } - LogError( - "cannot autodetect any free ip ranges on your system for use, please " - "configure this manually"); - return ""; + return std::nullopt; } - std::string + std::optional FindFreeTun() { - uint8_t num = 0; + int num = 0; while (num < 255) { std::stringstream ifname_ss; ifname_ss << "lokitun" << num; std::string iftestname = ifname_ss.str(); - struct sockaddr addr; - bool found = llarp_getifaddr(iftestname.c_str(), AF_INET, &addr); + bool found = llarp_getifaddr(iftestname.c_str(), AF_INET, nullptr); if (!found) { - llarp::LogDebug("Detected " + iftestname + " is available for use, configuring as such"); - break; + return iftestname; } num++; } - if (num == 255) - { - llarp::LogError("Could not find any free lokitun interface names"); - return ""; - } -// include lokitun prefix to communicate result is valid -#if defined(ANDROID) || defined(RPI) - char buff[IFNAMSIZ + 1] = {0}; - snprintf(buff, sizeof(buff), "lokitun%u", num); - return buff; -#else - return "lokitun" + std::to_string(num); -#endif + return std::nullopt; } - bool - GetIFAddr(const std::string& ifname, Addr& addr, int af) + std::optional + GetIFAddr(const std::string& ifname, int af) { sockaddr_storage s; - auto* sptr = (sockaddr*)&s; + sockaddr* sptr = (sockaddr*)&s; if (!llarp_getifaddr(ifname.c_str(), af, sptr)) - return false; - addr = *sptr; - return true; + return std::nullopt; + return llarp::Addr{*sptr}; } bool @@ -590,7 +578,12 @@ namespace llarp return false; #else if (!ipv6_is_siit(addr)) + { + static in6_addr zero = {}; + if (addr == zero) + return true; return false; + } return IsIPv4Bogon( ipaddr_ipv4_bits(addr.s6_addr[12], addr.s6_addr[13], addr.s6_addr[14], addr.s6_addr[15])); #endif diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index 5192a628f..23e6ea5b9 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -168,16 +168,16 @@ namespace llarp GetBestNetIF(std::string& ifname, int af = AF_INET); /// look at adapter ranges and find a free one - std::string + std::optional FindFreeRange(); /// look at adapter names and find a free one - std::string + std::optional FindFreeTun(); /// get network interface address for network interface with ifname - bool - GetIFAddr(const std::string& ifname, Addr& addr, int af = AF_INET); + std::optional + GetIFAddr(const std::string& ifname, int af = AF_INET); } // namespace llarp diff --git a/llarp/net/net_addr.cpp b/llarp/net/net_addr.cpp index 2b862cab8..8f7f43c21 100644 --- a/llarp/net/net_addr.cpp +++ b/llarp/net/net_addr.cpp @@ -14,6 +14,8 @@ #define inet_aton(x, y) inet_pton(AF_INET, x, y) #endif +#include + namespace llarp { Addr::Addr() @@ -58,11 +60,14 @@ namespace llarp Addr::Addr(std::string_view str) : Addr() { - this->FromString(str); + if (not FromString(str)) + throw std::invalid_argument(stringify("failed to parse bullshit value: ", str)); } - Addr::Addr(std::string_view str, const uint16_t p_port) : Addr(str) + Addr::Addr(std::string_view str, const uint16_t p_port) : Addr() { + if (not FromString(str)) + throw std::invalid_argument(stringify("failed to parse bullshit value: ", str)); this->port(p_port); } @@ -271,7 +276,7 @@ namespace llarp break; // TODO : sockaddr_ll default: - break; + throw std::invalid_argument("bad address family"); } } diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 0721257f9..c31fa5667 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -421,6 +421,8 @@ namespace llarp m_isServiceNode = true; } + networkConfig = conf->network; + /// build a set of strictConnectPubkeys ( /// TODO: make this consistent with config -- do we support multiple strict connections // or not? @@ -571,8 +573,10 @@ namespace llarp // API config enableRPCServer = conf->api.m_enableRPCServer; rpcBindAddr = conf->api.m_rpcBindAddr; - - hiddenServiceContext().AddEndpoint(*conf); + if (not IsServiceNode()) + { + hiddenServiceContext().AddEndpoint(*conf); + } // Logging config LogContext::Instance().Initialize( @@ -844,14 +848,10 @@ namespace llarp rpcBindAddr = DefaultRPCBindAddr; } rpcServer = std::make_unique(this); - while (!rpcServer->Start(rpcBindAddr)) + if (not rpcServer->Start(rpcBindAddr)) { LogError("failed to bind jsonrpc to ", rpcBindAddr); -#if defined(ANDROID) || defined(RPI) - sleep(1); -#else - std::this_thread::sleep_for(std::chrono::seconds(1)); -#endif + return false; } LogInfo("Bound RPC server to ", rpcBindAddr); } @@ -869,14 +869,10 @@ namespace llarp { rpcCaller = std::make_unique(this); rpcCaller->SetAuth(lokidRPCUser, lokidRPCPassword); - while (!rpcCaller->Start(lokidRPCAddr)) + if (not rpcCaller->Start(lokidRPCAddr)) { - LogError("failed to start jsonrpc caller to ", lokidRPCAddr); -#if defined(ANDROID) || defined(RPI) - sleep(1); -#else - std::this_thread::sleep_for(std::chrono::seconds(1)); -#endif + LogError("RPC Caller to ", lokidRPCAddr, " failed to start"); + return false; } LogInfo("RPC Caller to ", lokidRPCAddr, " started"); } @@ -897,11 +893,6 @@ namespace llarp Addr publicAddr(this->addrInfo); - if (this->publicOverride) - { - LogDebug("public address:port ", publicAddr); - } - // set public signing key _rc.pubkey = seckey_topublic(identity()); // set router version if service node @@ -910,8 +901,8 @@ namespace llarp _rc.routerVersion = RouterVersion(llarp::VERSION, LLARP_PROTO_VERSION); } - AddressInfo ai; _linkManager.ForEachInboundLink([&](LinkLayer_ptr link) { + AddressInfo ai; if (link->GetOurAddressInfo(ai)) { // override ip and port @@ -922,6 +913,7 @@ namespace llarp } if (RouterContact::BlockBogons && IsBogon(ai.ip)) return; + LogInfo("adding address: ", ai); _rc.addrs.push_back(ai); if (ExitEnabled()) { diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index bda0d622c..39d9b88da 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -228,6 +228,7 @@ namespace llarp bool ExitEnabled() const { + return false; // FIXME - have to fix the FIXME because FIXME throw std::runtime_error("FIXME: this needs to be derived from config"); /* // TODO: use equal_range ?