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.
pull/2232/head
Jason Rhinelander 7 months ago committed by dr7ana
parent 90a530a114
commit 6505c7badb

@ -961,14 +961,10 @@ namespace llarp
throw std::invalid_argument{fmt::format("{} is a loopback address", arg)}; throw std::invalid_argument{fmt::format("{} is a loopback address", arg)};
} }
if (not maybe) if (not maybe)
{
// infer public address // infer public address
if (auto maybe_ifname = net_ptr->GetBestNetIF()) maybe = net_ptr->get_best_public_address(true, DEFAULT_LISTEN_PORT);
maybe = oxen::quic::Address{*maybe_ifname}; else if (maybe && maybe->port() == 0)
} maybe->set_port(DEFAULT_LISTEN_PORT);
if (maybe && maybe->port() == 0)
maybe = oxen::quic::Address{maybe->host(), DEFAULT_LISTEN_PORT};
return maybe; return maybe;
}; };

@ -10,6 +10,8 @@
#include <llarp/util/bits.hpp> #include <llarp/util/bits.hpp>
#include <llarp/util/mem.hpp> #include <llarp/util/mem.hpp>
#include <quic/address.hpp>
#include <cstdlib> // for itoa #include <cstdlib> // for itoa
#include <functional> #include <functional>
#include <vector> #include <vector>
@ -121,8 +123,10 @@ namespace llarp
return var::visit([](auto&& ip) { return not ip.n; }, ip); return var::visit([](auto&& ip) { return not ip.n; }, ip);
} }
virtual std::optional<sockaddr*> // Attempts to guess a good default public network address from the system's public IP
GetBestNetIF(int af = AF_INET) const = 0; // addresses; the returned Address (if set) will have its port set to the given value.
virtual std::optional<oxen::quic::Address>
get_best_public_address(bool ipv4, uint16_t port) const = 0;
virtual std::optional<IPRange> virtual std::optional<IPRange>
FindFreeRange() const = 0; FindFreeRange() const = 0;

@ -10,6 +10,8 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#endif #endif
#include <quic/address.hpp>
#include <list> #include <list>
namespace llarp::net namespace llarp::net
@ -50,19 +52,21 @@ namespace llarp::net
return ifname; return ifname;
} }
std::optional<sockaddr*> std::optional<oxen::quic::Address>
GetBestNetIF(int af) const override get_best_public_address(bool ipv4, uint16_t port) const override
{ {
std::optional<sockaddr*> found; std::optional<oxen::quic::Address> found;
iter_all([this, &found, af](ifaddrs* i) { iter_all([&found, ipv4, port](ifaddrs* i) {
if (found) if (found)
return; 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);
} }
} }
}); });

@ -129,8 +129,8 @@ namespace llarp::net
return "lokitun0"; return "lokitun0";
} }
std::optional<sockaddr*> std::optional<oxen::quic::Address>
GetBestNetIF(int) const override get_best_public_address(bool, uint16_t) const override
{ {
// TODO: implement me ? // TODO: implement me ?
return std::nullopt; return std::nullopt;

@ -582,11 +582,8 @@ namespace llarp
if (paddr or pport) if (paddr or pport)
throw std::runtime_error{"Must specify [bind]:listen in config with public ip/addr!"}; throw std::runtime_error{"Must specify [bind]:listen in config with public ip/addr!"};
if (auto maybe_addr = net().GetBestNetIF()) if (auto maybe_addr = net().get_best_public_address(true, DEFAULT_LISTEN_PORT))
{ _listen_address = std::move(*maybe_addr);
_listen_address = oxen::quic::Address{static_cast<const sockaddr*>(*maybe_addr)};
_listen_address.set_port(DEFAULT_LISTEN_PORT);
}
else else
throw std::runtime_error{"Could not find net interface on current platform!"}; throw std::runtime_error{"Could not find net interface on current platform!"};
} }

Loading…
Cancel
Save