83 return interface->get_first_ipv4_address();
93 std::vector<ip_address_v4> addresses;
95 auto& it = addresses.emplace_back();
97 it = interface->get_first_ipv4_address();
115 std::string output =
"Network interface configuration: ";
117 return output +
"none";
120 for (
size_t i = 0; i <
interfaces.size(); ++i) {
125 fmt::format_to(std::back_inserter(output),
"{}({})",
interfaces[i], i);
136 std::array<ip_address_v4, N> addresses {};
139 for (
size_t i = 0; i < std::min(addresses.size(), addresses_vector.size()); ++i) {
140 addresses[i] = std::move(addresses_vector[i]);
146#if RAV_HAS_BOOST_JSON
150 [[nodiscard]] boost::json::array to_boost_json()
const {
151 auto a = boost::json::array();
153 for (
size_t i = 0; i <
interfaces.size(); ++i) {
154 a.push_back(boost::json::object {{
"rank", i}, {
"identifier",
interfaces[i]}});
165 [[nodiscard]]
static tl::expected<NetworkInterfaceConfig, std::string> from_boost_json(
const boost::json::value& json) {
166 const auto json_array = json.try_as_array();
167 if (json_array.has_error()) {
168 return tl::unexpected(
"Value is not an array");
170 NetworkInterfaceConfig config;
172 for (
auto&
object : *json_array) {
173 const auto rank =
object.at(
"rank").to_number<
size_t>();
174 const auto identifier = std::string(
object.at(
"identifier").as_string());
175 config.set_interface(rank, identifier);
177 }
catch (
const std::exception& e) {
178 return tl::unexpected(fmt::format(
"Failed to parse NetworkInterfaceConfig: {}", e.what()));
185inline std::optional<NetworkInterfaceConfig>
190 for (
auto i = 0; i < 10; ++i) {
191 auto section = parser.
split(delimiter);
#define RAV_ASSERT_FALSE(message)
Asserts with false, entering the RAV_ASSERT procedure as a quick way to assert that a branch is inval...
std::array< ip_address_v4, N > get_array_of_interface_addresses() const
std::vector< NetworkInterface::Identifier > interfaces
friend bool operator==(const NetworkInterfaceConfig &lhs, const NetworkInterfaceConfig &rhs)
std::string to_string() const
std::vector< ip_address_v4 > get_interface_ipv4_addresses() const
ip_address_v4 get_interface_ipv4_address(const size_t rank) const
bool empty() const
Checks if the configuration is empty.
friend bool operator!=(const NetworkInterfaceConfig &lhs, const NetworkInterfaceConfig &rhs)
void set_interface(const size_t rank, const NetworkInterface::Identifier &identifier)
Sets a network interface by its rank.
const NetworkInterface::Identifier * get_interface_for_rank(const size_t rank) const
Gets a network interface by its rank.
std::string Identifier
The identifier of a network interface (e.g. "en0", "eth0").
static const NetworkInterfaceList & get_system_interfaces(bool force_refresh=false)
Retrieves the list of network interfaces on the system.
const NetworkInterface * find_by_string(const std::string_view &search_string) const
Finds a network interface by the given string.
A handy utility class for parsing strings.
std::optional< std::string_view > split(const char delimiter, const bool include_delimiter=false)
Reads until the given delimiter or until the end of the string.
boost::asio::ip::address_v4 ip_address_v4
std::optional< NetworkInterfaceConfig > parse_network_interface_config_from_string(const std::string &input, const char delimiter=',')