From f1edca9fa1de21aa321237d2e8d0d571997c3b12 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 4 May 2020 14:39:14 -0400 Subject: [PATCH] additional cleanup of cruft --- llarp/config/config.cpp | 1 - llarp/link/server.cpp | 15 +++++++++++++-- llarp/net/net.cpp | 11 +++++------ llarp/net/net.hpp | 4 ++-- llarp/net/net_addr.cpp | 11 ++++++++--- llarp/router/router.cpp | 26 +++++++------------------- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index d823dcb48..686eb55be 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -100,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; 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 6b2f6b53d..c813bac8d 100644 --- a/llarp/net/net.cpp +++ b/llarp/net/net.cpp @@ -533,15 +533,14 @@ namespace llarp 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 diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index 741043f5b..23e6ea5b9 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -176,8 +176,8 @@ namespace llarp 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 2829ec9cc..c31fa5667 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -848,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); } @@ -873,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"); } @@ -901,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 @@ -914,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 @@ -926,6 +913,7 @@ namespace llarp } if (RouterContact::BlockBogons && IsBogon(ai.ip)) return; + LogInfo("adding address: ", ai); _rc.addrs.push_back(ai); if (ExitEnabled()) {