wpa_supplicant

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 8 Imported by: 0

README

wpa_supplicant-go

GoDoc License

Implementation of the wpa_supplicant / hostapd control interface in Go (no CGO).

Installation
go get github.com/hdiniz/wpa_supplicant-go
Examples

For complete examples, check the examples' folder.

Scanning
package main

import (
	"context"
	"fmt"
	"os"
	"time"
	
	"github.com/hdiniz/wpa_supplicant-go"
)

func main() {
	ctrl, err := wpa_supplicant.Connect("/run/wpa_supplicant/wlan0")
	if err != nil {
		fmt.Printf("failed to connect to wpa_supplicant: %s\n", err)
		os.Exit(1)
	}
	
	ctx := context.TODO()

	// ask hostapd to start a scan
	res, err := ctrl.SendRequest(ctx, "SCAN")
	if res != "OK\n" {
		fmt.Println("failed to request scan", res)
		os.Exit(1)
	}

	time.Sleep(2 * time.Second) // give some time to scan channels

	res, err = ctrl.SendRequest(ctx, "SCAN_RESULTS")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
Listening to events
package main

import (
	"context"
	"fmt"
	"os"
	
	"github.com/hdiniz/wpa_supplicant-go"
)

func main() {
	ctrl, err := wpa_supplicant.Connect("/run/wpa_supplicant/wlan0")
	if err != nil {
		fmt.Printf("failed to connect to wpa_supplicant: %s\n", err)
		os.Exit(1)
	}
	
	ctx := context.TODO()

	err := ctrl.Listen(ctx, func(event wpa_supplicant.Event) {
        fmt.Println(event.Priority, event.Data)
	})
	if err != nil {
		fmt.Printf("failed to listen: %s\n", err)
		os.Exit(1)
	}
}
Permissions

To communicate with the wpa_supplicant/hostapd daemon, the process must have permission to access the socket path. Likewise, wpa_supplicant/hostapd must have permissions to access the socket created by this library.

For example, if hostapd is running as a less privileged user (e.g. network) and the application as root (e.g. sshed into a OpenWRT shell). The application will be able to send requests to hostapd, but hostapd will not be able to send replies. This can be fixed by running as the same user or by setting file permissions on the local socket path.

Documentation

Overview

Package wpa_supplicant provides a control interface to a wpa_supplicant/hostapd daemon.

The Connect function connects to a daemon:

ctrlIface, err := wpa_supplicant.Connect("/run/wpa_supplicant/wlan0")
if err != nil {
  // handle err
}

The SendRequest method sends request to the daemon:

res, err := ctrlIface.SendRequest(context.TODO(), "PING")
if err != nil {
  // handle err
}

fmt.Println(res)
// PONG

The control interface can listen to daemon events:

err := ctrlIface.Listen(context.TODO(), func (event wpa_supplicant.Event) {
  fmt.Println(event)
  // {Priority: wpa_supplicant.EventPriorityInfo, Data: "CTRL-EVENT-SCAN-STARTED"}
})

if err != nil {
  // handle err
}

Refer to wpa_supplicant/hostapd documentation for available requests, responses and events on this interface.

https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html

Index

Constants

View Source
const (
	EventPriorityMsgDump = EventPriority(iota)
	EventPriorityDebug
	EventPriorityInfo
	EventPriorityWarning
	EventPriorityError
)

Variables

View Source
var (
	DefaultConnectionOptions = ConnectionOptions{
		TemporaryFilePattern: "wpa_supplicant-go-ctrl",
	}
)

Functions

This section is empty.

Types

type Connection

type Connection struct {
	*net.UnixConn
}

A Connection is a connection to the wpa_supplicant/hostapd control interface socket.

func (*Connection) SendRequest

func (conn *Connection) SendRequest(ctx context.Context, cmd string) (string, error)

SendRequest sends a request to wpa_supplicant.

res, err := conn.SendRequest(ctx, "PING")
if err != nil {
  // handle err
}

fmt.Println(res)
// PONG

type ConnectionOptions

type ConnectionOptions struct {
	// TemporaryFilePattern is the file name pattern used to generate local socket addresses in os.TempDir().
	TemporaryFilePattern string
}

A ConnectionOptions holds options to construct a ControlInterface.

type ControlInterface

type ControlInterface struct {
	// contains filtered or unexported fields
}

A ControlInterface is a control interface to a wpa_supplicant/hostapd daemon.

https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html

func Connect

func Connect(controlInterfacePath string) (*ControlInterface, error)

Connect connects to wpa_supplicant/hostapd via the control interface in controlInterfacePath.

func ConnectWithOptions

func ConnectWithOptions(controlInterfacePath string, opts ConnectionOptions) (*ControlInterface, error)

ConnectWithOptions connects to wpa_supplicant/hostapd via the control interface in controlInterfacePath.

func (*ControlInterface) Close

func (ctrlIface *ControlInterface) Close() error

Close closes the connection to wpa_supplicant.

func (*ControlInterface) Listen

func (ctrlIface *ControlInterface) Listen(ctx context.Context, handler UnsolicitedEventHandler) error

Listen listens to control interface events.

The function blocks while listening to events, it returns if an error occurs while reading the remote connection or if ctx is canceled. When an event is received, handler is called on the caller goroutine.

Listen opens a separate connection to handle events only, not interfering with the solicited requests/reply exchanges occurring on this ControlInterface.

func (*ControlInterface) SendRequest

func (ctrlIface *ControlInterface) SendRequest(ctx context.Context, cmd string) (string, error)

SendRequest sends a request to wpa_supplicant.

res, err := conn.SendRequest(ctx, "PING")
if err != nil {
  // handle err
}

fmt.Println(res)
// PONG

type Event

type Event struct {
	Priority EventPriority
	Data     string
}

Event is an unsolicited message from wpa_supplicant.

https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html

type EventPriority

type EventPriority int

A EventPriority is a value indicating the priority of an event received via the control interface.

https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html

type UnsolicitedEventHandler

type UnsolicitedEventHandler func(Event)

A UnsolicitedEventHandler handles events received in via the control interface.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL