Skip to content

TierOne-Software/wpasupplicant-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wpasupplicant-cpp

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.

Features

  • Modern C++20 with std::expected for 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

Requirements

  • C++20 compatible compiler (GCC 11+, Clang 13+)
  • CMake 3.20 or later
  • wpa_supplicant installed and configured

Building

mkdir build
cd build
cmake ..
make

Build Options

  • BUILD_TESTS - Build unit tests (default: ON)
  • BUILD_EXAMPLES - Build example programs (default: ON)

Usage

Basic Example

#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;
}

Error Handling

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";
}

API Overview

Connection Management

  • Connection::connect(path) - Connect to wpa_supplicant socket
  • ping() - Test if wpa_supplicant is responding
  • save_config() - Save current configuration to file

Network Configuration

  • add_network() - Add a new network
  • remove_network(id) - Remove a network
  • remove_all_networks() - Remove all networks
  • set_network(id, field, value) - Set network parameter
  • set_network_quoted(id, field, value) - Set network parameter (quoted)
  • set_network_wep_keys(id, format, keys) - Set WEP keys
  • get_network(id, field) - Get network parameter

Network Control

  • select_network(id) - Select a network (disable others)
  • enable_network(id) - Enable a network
  • disable_network(id) - Disable a network
  • enable_all_networks() - Enable all networks
  • disable_all_networks() - Disable all networks
  • reassociate() - Force reassociation
  • reconnect() - Attempt to reconnect

Status and Information

  • status() - Get current connection status
  • status_verbose() - Get verbose status
  • list_networks() - List configured networks
  • num_of_networks() - Get number of networks
  • interfaces() - List available interfaces
  • ifname() - Get current interface name
  • bss(id) - Get BSS scan results

Global Configuration

  • set_global_parameter(field, value) - Set global parameter
  • reconfigure() - Reload configuration file

Running Tests

cd build
ctest --output-on-failure

Or run the test binary directly:

./tests/wpasupplicant_test

wpa_supplicant Configuration

Your 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:

License

See LICENSE file for copyright and license details.

Related Projects

About

A modern C++20 library for controlling wpa_supplicant via Unix domain sockets.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published