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 5 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)};
}
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;
};

@ -10,6 +10,8 @@
#include <llarp/util/bits.hpp>
#include <llarp/util/mem.hpp>
#include <quic/address.hpp>
#include <cstdlib> // for itoa
#include <functional>
#include <vector>
@ -121,8 +123,10 @@ namespace llarp
return var::visit([](auto&& ip) { return not ip.n; }, ip);
}
virtual std::optional<sockaddr*>
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<oxen::quic::Address>
get_best_public_address(bool ipv4, uint16_t port) const = 0;
virtual std::optional<IPRange>
FindFreeRange() const = 0;

@ -10,6 +10,8 @@
#include <ifaddrs.h>
#endif
#include <quic/address.hpp>
#include <list>
namespace llarp::net
@ -50,19 +52,21 @@ namespace llarp::net
return ifname;
}
std::optional<sockaddr*>
GetBestNetIF(int af) const override
std::optional<oxen::quic::Address>
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)
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";
}
std::optional<sockaddr*>
GetBestNetIF(int) const override
std::optional<oxen::quic::Address>
get_best_public_address(bool, uint16_t) const override
{
// TODO: implement me ?
return std::nullopt;

@ -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<const sockaddr*>(*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!"};
}

Loading…
Cancel
Save