A modern C++20 library for controlling wpa_supplicant via Unix domain sockets.
This is a C++ port of the wpasupplicant Go library, providing a type-safe interface to configure and manage wireless networks through wpa_supplicant's control interface.
- Modern C++20 with
std::expectedfor error handling - Unix domain socket communication (SOCK_DGRAM)
- Complete wpa_supplicant control interface support
- Comprehensive unit tests with Google Test
- CMake build system
- Move semantics for efficient resource management
- C++20 compatible compiler (GCC 11+, Clang 13+)
- CMake 3.20 or later
- wpa_supplicant installed and configured
mkdir build
cd build
cmake ..
makeBUILD_TESTS- Build unit tests (default: ON)BUILD_EXAMPLES- Build example programs (default: ON)
#include <wpasupplicant.hpp>
int main() {
// Connect to wpa_supplicant
auto conn = wpasupplicant::Connection::connect("/var/run/wpa_supplicant");
if (!conn) {
std::cerr << "Failed to connect: " << conn.error().message() << "\n";
return 1;
}
// Add a new network
auto network_id = conn->add_network();
if (!network_id) {
std::cerr << "Failed to add network\n";
return 1;
}
// Configure the network
conn->set_network_quoted(*network_id, "ssid", "my_wifi");
conn->set_network_quoted(*network_id, "psk", "my_password");
conn->set_network(*network_id, "key_mgmt", "WPA-PSK");
// Enable and select the network
conn->enable_network(*network_id);
conn->select_network(*network_id);
return 0;
}The library uses std::expected for error handling:
auto result = conn->status();
if (result) {
std::cout << "Status: " << *result << "\n";
} else {
std::cerr << "Error: " << result.error().message() << "\n";
}Connection::connect(path)- Connect to wpa_supplicant socketping()- Test if wpa_supplicant is respondingsave_config()- Save current configuration to file
add_network()- Add a new networkremove_network(id)- Remove a networkremove_all_networks()- Remove all networksset_network(id, field, value)- Set network parameterset_network_quoted(id, field, value)- Set network parameter (quoted)set_network_wep_keys(id, format, keys)- Set WEP keysget_network(id, field)- Get network parameter
select_network(id)- Select a network (disable others)enable_network(id)- Enable a networkdisable_network(id)- Disable a networkenable_all_networks()- Enable all networksdisable_all_networks()- Disable all networksreassociate()- Force reassociationreconnect()- Attempt to reconnect
status()- Get current connection statusstatus_verbose()- Get verbose statuslist_networks()- List configured networksnum_of_networks()- Get number of networksinterfaces()- List available interfacesifname()- Get current interface namebss(id)- Get BSS scan results
set_global_parameter(field, value)- Set global parameterreconfigure()- Reload configuration file
cd build
ctest --output-on-failureOr run the test binary directly:
./tests/wpasupplicant_testYour wpa_supplicant must be configured with a control interface. Add this to your wpa_supplicant.conf:
ctrl_interface=/var/run/wpa_supplicant
For more information on wpa_supplicant configuration, see:
See LICENSE file for copyright and license details.
- wpasupplicant (Go) - Original Go implementation