Skip to content

Commit

Permalink
Create control socket directory if not exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltrano committed Jul 2, 2024
1 parent 97d24b3 commit 9417de0
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions tests/unit/linux/wpa-controller/detail/WpaDaemonManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,74 @@
#include <stdexcept>
#include <string_view>

#include "WpaDaemonManager.hxx"
#include <Wpa/ProtocolWpaConfig.hxx>
#include <Wpa/WpaCore.hxx>
#include <magic_enum.hpp>
#include <plog/Log.h>

#include "hostapd.conf.format.hxx"
#include "HostapdBinaryInfo.hxx"
#include "WpaDaemonManager.hxx"

namespace detail
{
/**
* @brief Format string for the default wpa_supplicant configuration file contents.
*
* There are 1 arguments expected to be substituted into the format string:
*
* 1. The control interface path.
*/
static constexpr auto WpaDaemonWpaSupplicantConfigurationFileContentsFormat = R"CONFIG(
ctrl_interface={}
)CONFIG";

/**
* @brief Format string for the default hostapd configuration file contents.
*
* There are 2 argumentd expected to be substituted into the format string:
*
* 1. The wlan interface name.
* 2. The control interface path.
*/
static constexpr auto WpaDaemonHostapdConfigurationFileContentsFormat = R"CONFIG(
interface={}
driver=nl80211
ctrl_interface={}
ssid=wificontrollertest
hw_mode=g
channel=1
auth_algs=3
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK WPA-PSK-SHA256 SAE
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
)CONFIG";

/**
* @brief Write the default configuration file contents for the specified wpa
* daemon type to the specified file stream.
*
* @param wpaType The type of wpa daemon to write the configuration file for.
* @param interfaceName The wlan interface the daemon will be managing.
* @param controlSocketPath The path to the control socket for the daemon.
* @param configurationFile The file stream to write the configuration file to.
*/
void
WriteDefaultConfigurationFileContents(Wpa::WpaType wpaType, std::string_view interfaceName, std::ofstream& configurationFile)
WriteDefaultConfigurationFileContents(Wpa::WpaType wpaType, std::string_view interfaceName, std::string_view controlSocketPath, std::ofstream& configurationFile)
{
switch (wpaType) {
case Wpa::WpaType::Hostapd: {
configurationFile << std::format(WpaDaemonHostapdConfigurationFileContentsFormat, interfaceName);
case Wpa::WpaType::Hostapd:
configurationFile << std::format(WpaDaemonHostapdConfigurationFileContentsFormat, interfaceName, controlSocketPath);
break;
}
default: {
case Wpa::WpaType::WpaSupplicant:
configurationFile << std::format(WpaDaemonWpaSupplicantConfigurationFileContentsFormat, controlSocketPath);
break;
case Wpa::WpaType::Unknown:
[[fallthrough]];
default:
throw std::runtime_error(std::format("Unsupported wpa daemon type '{}'", magic_enum::enum_name(wpaType)));
}
}
}
} // namespace detail

Expand All @@ -51,10 +88,11 @@ WpaDaemonManager::CreateAndWriteDefaultConfigurationFile(Wpa::WpaType wpaType, s
// Determine which daemon to create the configuration file for.
const auto daemon = Wpa::GetWpaTypeDaemonBinaryName(wpaType);
const auto daemonConfigurationFilePath = std::filesystem::temp_directory_path() / std::format("{}.conf", daemon);
const auto daemonControlSocketPath{ Wpa::ProtocolWpaConfig::GetControlSocketPath(wpaType) };

// Create and write default configuration file contents.
std::ofstream daemonConfigurationFile{ daemonConfigurationFilePath, std::ios::out | std::ios::trunc };
detail::WriteDefaultConfigurationFileContents(wpaType, interfaceName, daemonConfigurationFile);
detail::WriteDefaultConfigurationFileContents(wpaType, interfaceName, daemonControlSocketPath, daemonConfigurationFile);
daemonConfigurationFile.flush();
daemonConfigurationFile.close();

Expand All @@ -71,6 +109,15 @@ WpaDaemonManager::Start(Wpa::WpaType wpaType, std::string_view interfaceName, co
interfaceName = WpaDaemonManager::InterfaceNameDefault;
}

// Create the control socket path if it doesn't exist.
const std::filesystem::path controlSocketPath{ Wpa::ProtocolWpaConfig::GetControlSocketPath(wpaType) };
if (!std::filesystem::exists(controlSocketPath)) {
if (!std::filesystem::create_directories(controlSocketPath)) {
LOGE << std::format("Failed to create control socket path '{}'\n", controlSocketPath.c_str());
return std::nullopt;
}
}

// Determine which daemon to start and formulate daemon binary arguments.
const auto daemon = Wpa::GetWpaTypeDaemonBinaryName(wpaType);
const auto* configurationFileArgumentPrefix = (wpaType == Wpa::WpaType::WpaSupplicant) ? "-c" : "";
Expand Down

0 comments on commit 9417de0

Please sign in to comment.