From 6505c7badbdec130c070bf4cb452945667996955 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 12 Dec 2023 13:38:31 -0400 Subject: [PATCH] Replace GetBestNetIF with quic::Address version It is now called get_best_public_address, and takes (bool, port) argument to return an optional quic::Address to make life easier: the caller now can just give the default port to set, and we keep the C sockaddr* more constrained. --- llarp/config/config.cpp | 10 +++------- llarp/net/net.hpp | 8 ++++++-- llarp/net/posix.cpp | 18 +++++++++++------- llarp/net/win32.cpp | 4 ++-- llarp/router/router.cpp | 7 ++----- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 5b92f0fe9..9b094eae9 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -961,14 +961,10 @@ namespace llarp throw std::invalid_argument{fmt::format("{} is a loopback address", arg)}; } if (not maybe) - { // infer public address - if (auto maybe_ifname = net_ptr->GetBestNetIF()) - maybe = oxen::quic::Address{*maybe_ifname}; - } - - if (maybe && maybe->port() == 0) - maybe = oxen::quic::Address{maybe->host(), DEFAULT_LISTEN_PORT}; + maybe = net_ptr->get_best_public_address(true, DEFAULT_LISTEN_PORT); + else if (maybe && maybe->port() == 0) + maybe->set_port(DEFAULT_LISTEN_PORT); return maybe; }; diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index c821c2879..0074b4594 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -10,6 +10,8 @@ #include #include +#include + #include // for itoa #include #include @@ -121,8 +123,10 @@ namespace llarp return var::visit([](auto&& ip) { return not ip.n; }, ip); } - virtual std::optional - GetBestNetIF(int af = AF_INET) const = 0; + // Attempts to guess a good default public network address from the system's public IP + // addresses; the returned Address (if set) will have its port set to the given value. + virtual std::optional + get_best_public_address(bool ipv4, uint16_t port) const = 0; virtual std::optional FindFreeRange() const = 0; diff --git a/llarp/net/posix.cpp b/llarp/net/posix.cpp index 3715e1571..f96088786 100644 --- a/llarp/net/posix.cpp +++ b/llarp/net/posix.cpp @@ -10,6 +10,8 @@ #include #endif +#include + #include namespace llarp::net @@ -50,19 +52,21 @@ namespace llarp::net return ifname; } - std::optional - GetBestNetIF(int af) const override + std::optional + get_best_public_address(bool ipv4, uint16_t port) const override { - std::optional found; + std::optional found; - iter_all([this, &found, af](ifaddrs* i) { + iter_all([&found, ipv4, port](ifaddrs* i) { if (found) return; - if (i and i->ifa_addr and i->ifa_addr->sa_family == af) + if (i and i->ifa_addr and i->ifa_addr->sa_family == (ipv4 ? AF_INET : AF_INET6)) { - if (not IsBogon(*i->ifa_addr)) + oxen::quic::Address a{i->ifa_addr}; + if (a.is_public_ip()) { - found = i->ifa_addr; + a.set_port(port); + found = std::move(a); } } }); diff --git a/llarp/net/win32.cpp b/llarp/net/win32.cpp index 88b011b20..37af7ad26 100644 --- a/llarp/net/win32.cpp +++ b/llarp/net/win32.cpp @@ -129,8 +129,8 @@ namespace llarp::net return "lokitun0"; } - std::optional - GetBestNetIF(int) const override + std::optional + get_best_public_address(bool, uint16_t) const override { // TODO: implement me ? return std::nullopt; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index c1ea59519..30604f423 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -582,11 +582,8 @@ namespace llarp if (paddr or pport) throw std::runtime_error{"Must specify [bind]:listen in config with public ip/addr!"}; - if (auto maybe_addr = net().GetBestNetIF()) - { - _listen_address = oxen::quic::Address{static_cast(*maybe_addr)}; - _listen_address.set_port(DEFAULT_LISTEN_PORT); - } + if (auto maybe_addr = net().get_best_public_address(true, DEFAULT_LISTEN_PORT)) + _listen_address = std::move(*maybe_addr); else throw std::runtime_error{"Could not find net interface on current platform!"}; }