Skip to content

Commit

Permalink
[gui] add shared library with c interfaces for ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Jul 6, 2023
1 parent d6684e4 commit 3b57f26
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 3rd-party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ option(YAML_CPP_BUILD_CONTRIB OFF)
option(YAML_CPP_BUILD_TESTS OFF)

add_subdirectory(yaml-cpp EXCLUDE_FROM_ALL)
set_target_properties(yaml-cpp PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_library(yaml INTERFACE)

Expand Down Expand Up @@ -121,6 +122,7 @@ set(BUILD_SHARED_LIBS OFF)
add_subdirectory(semver EXCLUDE_FROM_ALL)
target_include_directories(semver INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/semver/include)
set_target_properties(semver PROPERTIES POSITION_INDEPENDENT_CODE ON)

# xz-decoder config
add_subdirectory(xz-decoder EXCLUDE_FROM_ALL)
Expand Down
16 changes: 16 additions & 0 deletions include/multipass/dart_ffi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef MULTIPASS_DART_FFI_H
#define MULTIPASS_DART_FFI_H

extern "C" const char* generate_petname();

extern "C" const char* get_server_address();

extern "C" struct KeyCertificatePair
{
const char* pem_cert;
const char* pem_priv_key;
};

extern "C" struct KeyCertificatePair get_cert_pair();

#endif // MULTIPASS_DART_FFI_H
9 changes: 9 additions & 0 deletions src/client/desktop_gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

add_library(dart_ffi SHARED ffi/dart_ffi.cpp)

target_link_libraries(dart_ffi
petname
client_common
)

install(TARGETS dart_ffi LIBRARY COMPONENT multipass)

if (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "x86_64" OR ${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "AMD64")
set(DESKTOP_GUI_ARCH x64)
else()
Expand Down
70 changes: 70 additions & 0 deletions src/client/desktop_gui/ffi/dart_ffi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "multipass/dart_ffi.h"
#include "multipass/cli/client_common.h"
#include "multipass/format.h"
#include "multipass/logging/log.h"
#include "multipass/name_generator.h"

namespace mp = multipass;
namespace mpc = multipass::client;
namespace mpl = multipass::logging;

constexpr auto category = "dart-ffi";

extern "C" const char* generate_petname()
try
{
static mp::NameGenerator::UPtr generator = mp::make_default_name_generator();
const auto name = generator->make_name();
return strdup(name.c_str());
}
catch (const std::exception& e)
{
mpl::log(mpl::Level::warning, category, fmt::format("failed generating petname: {}", e.what()));
return nullptr;
}
catch (...)
{
mpl::log(mpl::Level::warning, category, "failed generating petname");
return nullptr;
}

extern "C" const char* get_server_address()
try
{
const auto address = mpc::get_server_address();
return strdup(address.c_str());
}
catch (const std::exception& e)
{
mpl::log(mpl::Level::warning, category, fmt::format("failed retrieving server address: {}", e.what()));
return nullptr;
}
catch (...)
{
mpl::log(mpl::Level::warning, category, "failed retrieving server address");
return nullptr;
}

extern "C" struct KeyCertificatePair get_cert_pair()
try
{
const auto provider = mpc::get_cert_provider(mpc::get_server_address());
const auto cert = provider->PEM_certificate();
const auto key = provider->PEM_signing_key();
struct KeyCertificatePair pair
{
};
pair.pem_cert = strdup(cert.c_str());
pair.pem_priv_key = strdup(key.c_str());
return pair;
}
catch (const std::exception& e)
{
mpl::log(mpl::Level::warning, category, fmt::format("failed retrieving certificate key pair: {}", e.what()));
return KeyCertificatePair{nullptr, nullptr};
}
catch (...)
{
mpl::log(mpl::Level::warning, category, "failed retrieving certificate key pair");
return KeyCertificatePair{nullptr, nullptr};
}

0 comments on commit 3b57f26

Please sign in to comment.