Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/network/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <multipass/ip_address.h>

#include <ranges>
Comment thread
xmkg marked this conversation as resolved.
#include <sstream>
#include <stdexcept>

Expand All @@ -38,18 +39,18 @@ bool is_valid_octet(int value)

std::array<uint8_t, 4> parse(const std::string& ip)
{
char ch;
int a = -1;
int b = -1;
int c = -1;
int d = -1;
// FIXME: Use Boost.ASIO?
std::array sep = {'\0', '\0', '\0'};
std::array octets = {(int)-1, -1, -1, -1};

std::stringstream s(ip);
s >> a >> ch >> b >> ch >> c >> ch >> d;
s >> octets[0] >> sep[0] >> octets[1] >> sep[1] >> octets[2] >> sep[2] >> octets[3];

if (!is_valid_octet(a) || !is_valid_octet(b) || !is_valid_octet(c) || !is_valid_octet(d))
if (!std::ranges::all_of(octets, is_valid_octet) ||
!std::ranges::all_of(sep, [](char c) { return c == '.'; }))
Comment on lines 46 to +50
throw std::invalid_argument(fmt::format("invalid IP address {}", ip));

return {{as_octet(a), as_octet(b), as_octet(c), as_octet(d)}};
return {{as_octet(octets[0]), as_octet(octets[1]), as_octet(octets[2]), as_octet(octets[3])}};
}

std::array<uint8_t, 4> to_octets(uint32_t value)
Expand Down
7 changes: 7 additions & 0 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ HRESULT HCNAPI::HcnEnumerateEndpoints(PCWSTR Query, PWSTR* Endpoints, PWSTR* Err
{
return ::HcnEnumerateEndpoints(Query, Endpoints, ErrorRecord);
}
HRESULT HCNAPI::HcnQueryEndpointProperties(HCN_ENDPOINT Endpoint,
PCWSTR Query,
PWSTR* Properties,
PWSTR* ErrorRecord) const
{
return ::HcnQueryEndpointProperties(Endpoint, Query, Properties, ErrorRecord);
}
HRESULT HCNAPI::HcnEnumerateNetworks(PCWSTR Query, PWSTR* Networks, PWSTR* ErrorRecord) const
{
return ::HcnEnumerateNetworks(Query, Networks, ErrorRecord);
Expand Down
4 changes: 4 additions & 0 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ struct HCNAPI : public Singleton<HCNAPI>
[[nodiscard]] virtual HRESULT HcnEnumerateEndpoints(PCWSTR Query,
PWSTR* Endpoints,
PWSTR* ErrorRecord) const;
[[nodiscard]] virtual HRESULT HcnQueryEndpointProperties(HCN_ENDPOINT Endpoint,
PCWSTR Query,
PWSTR* Properties,
PWSTR* ErrorRecord) const;
[[nodiscard]] virtual HRESULT HcnEnumerateNetworks(PCWSTR Query,
PWSTR* Networks,
PWSTR* ErrorRecord) const;
Expand Down
31 changes: 31 additions & 0 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_endpoint_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <optional>
#include <string>
#include <vector>

namespace multipass::hyperv::hcn
{
struct HcnEndpointInfo
{
std::optional<std::string> mac_address;
std::vector<std::string> ip_addresses;
};
} // namespace multipass::hyperv::hcn
105 changes: 103 additions & 2 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ztd/out_ptr.hpp>

#include <cassert>
#include <optional>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -186,6 +187,52 @@ std::pair<OperationResult, UniqueHcnNetwork> open_network(const std::string& net
return std::make_pair(result, std::move(network));
}

std::pair<OperationResult, UniqueHcnEndpoint> open_endpoint(const std::string& endpoint_guid)
{
mpl::trace(log_category, "open_endpoint(...) > endpoint_guid: {}", endpoint_guid);

UniqueHcnEndpoint endpoint{};
const auto result = perform_hcn_operation([&](auto&& rmsgbuf) {
return API().HcnOpenEndpoint(guid_from_string(endpoint_guid), out_ptr(endpoint), rmsgbuf);
});

return std::make_pair(result, std::move(endpoint));
}

std::optional<std::vector<std::string>> endpoint_ip_addresses(const boost::json::object& endpoint)
{
std::vector<std::string> addresses;
const auto append_address = [&addresses](const boost::json::value& address) {
if (!address.is_string())
return false;

addresses.emplace_back(address.as_string());
return true;
};

if (const auto* configurations = endpoint.if_contains("IpConfigurations"))
{
if (!configurations->is_array())
return std::nullopt;

for (const auto& configuration : configurations->as_array())
{
if (!configuration.is_object())
return std::nullopt;

if (const auto* address = configuration.as_object().if_contains("IpAddress");
address && !append_address(*address))
return std::nullopt;
}
}

if (const auto* address = endpoint.if_contains("IPAddress");
address && !append_address(*address))
return std::nullopt;

return addresses;
}

} // namespace

// ---------------------------------------------------------
Expand Down Expand Up @@ -263,6 +310,59 @@ OperationResult HCNWrapper::delete_endpoint(const std::string& endpoint_guid) co

// ---------------------------------------------------------

OperationResult HCNWrapper::query_endpoint(const std::string& endpoint_guid,
HcnEndpointInfo& out_info) const
{
mpl::trace(log_category, "HCNWrapper::query_endpoint(...) > endpoint_guid: {}", endpoint_guid);

out_info = {};

const auto& [open_result, endpoint] = open_endpoint(endpoint_guid);
Comment thread
Copilot marked this conversation as resolved.
if (!open_result)
return open_result;

UniqueCotaskmemString properties{};
const auto result = perform_hcn_operation([&](auto&& rmsgbuf) {
return API().HcnQueryEndpointProperties(endpoint.get(),
L"{}",
out_ptr(properties),
rmsgbuf);
});
if (!result)
return result;

if (!properties)
return {E_UNEXPECTED, L"HCN returned no endpoint properties"};

const auto properties_as_str = wchar_to_utf8(properties.get());
mpl::trace(log_category, "query_endpoint result: {}", properties_as_str);

std::error_code ec;
const auto parsed = boost::json::parse(properties_as_str, ec);
if (ec || !parsed.is_object())
return {E_UNEXPECTED, L"Failed to process JSON returned from the API"};

const auto& endpoint_properties = parsed.as_object();
auto addresses = endpoint_ip_addresses(endpoint_properties);
if (!addresses)
return {E_UNEXPECTED, L"Failed to process JSON returned from the API"};

std::optional<std::string> mac_address;
if (const auto* value = endpoint_properties.if_contains("MacAddress"))
{
if (!value->is_string())
return {E_UNEXPECTED, L"Failed to process JSON returned from the API"};

mac_address = value->as_string();
}

out_info.mac_address = std::move(mac_address);
out_info.ip_addresses = std::move(*addresses);
return result;
}

// ---------------------------------------------------------

OperationResult HCNWrapper::enumerate_attached_endpoints(
const std::string& vm_guid,
std::vector<std::string>& endpoint_guids) const
Expand Down Expand Up @@ -360,8 +460,9 @@ OperationResult HCNWrapper::enumerate_networks(std::vector<std::string>& out_net
UniqueCotaskmemString enumerate_result{}, result_msgbuf{};

// List all HCN network GUIDs
const auto result =
API().HcnEnumerateNetworks(L"{}", out_ptr(enumerate_result), out_ptr(result_msgbuf));
const auto result = API().HcnEnumerateNetworks(L"{}",
out_ptr(enumerate_result),
out_ptr(result_msgbuf));
if (enumerate_result)
{
// json_output would contain the network GUIDs.
Expand Down
3 changes: 3 additions & 0 deletions src/platform/backends/hyperv_api/hcn/hyperv_hcn_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <hyperv_api/hcn/hyperv_hcn_create_endpoint_params.h>
#include <hyperv_api/hcn/hyperv_hcn_create_network_params.h>
#include <hyperv_api/hcn/hyperv_hcn_endpoint_info.h>
#include <hyperv_api/hcn/hyperv_hcn_network_info.h>
#include <hyperv_api/hyperv_api_operation_result.h>

Expand All @@ -44,6 +45,8 @@ struct HCNWrapper : public Singleton<HCNWrapper>
[[nodiscard]] virtual OperationResult create_endpoint(
const CreateEndpointParameters& params) const;
[[nodiscard]] virtual OperationResult delete_endpoint(const std::string& endpoint_guid) const;
[[nodiscard]] virtual OperationResult query_endpoint(const std::string& endpoint_guid,
HcnEndpointInfo& out_info) const;
[[nodiscard]] virtual OperationResult enumerate_attached_endpoints(
const std::string& vm_guid,
std::vector<std::string>& endpoint_guids) const;
Expand Down
Loading
Loading