Skip to content

feat: Support integer and byte slice value for SetNetwork() #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module pifke.org/wpasupplicant

go 1.16
39 changes: 30 additions & 9 deletions unixgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ package wpasupplicant
import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -262,7 +264,7 @@ func (uc *unixgramConn) Ping() error {
return err
}

if bytes.Compare(resp, []byte("PONG\n")) == 0 {
if bytes.Equal(resp, []byte("PONG\n")) {
return nil
}
return &ParseError{Line: string(resp)}
Expand Down Expand Up @@ -302,17 +304,36 @@ func (uc *unixgramConn) RemoveAllNetworks() error {
return uc.runCommand("REMOVE_NETWORK all")
}

func (uc *unixgramConn) SetNetwork(networkID int, variable string, value string) error {
var cmd string
func (uc *unixgramConn) SetNetwork(networkID int, variable string, value interface{}) error {
b := strings.Builder{}
b.WriteString("SET_NETWORK")
b.WriteString(" ")
b.WriteString(strconv.Itoa(networkID))
b.WriteString(" ")
b.WriteString(variable)
b.WriteString(" ")

// Since key_mgmt expects the value to not be wrapped in "" we do a little check here.
if variable == "key_mgmt" {
cmd = fmt.Sprintf("SET_NETWORK %d %s %s", networkID, variable, value)
} else {
cmd = fmt.Sprintf("SET_NETWORK %d %s \"%s\"", networkID, variable, value)
// Update: since we have to support AP mode, we need to support integer value (and hex value that just for non-ascii ssid)
switch v := value.(type) {
case string:
switch variable {
case "key_mgmt":
b.WriteString(v)
default:
b.WriteString("\"")
b.WriteString(v)
b.WriteString("\"")
}
case int:
b.WriteString(strconv.Itoa(v))
case []byte:
b.WriteString(hex.EncodeToString(v))
default:
return errors.New("unsupported value type")
}

return uc.runCommand(cmd)
return uc.runCommand(b.String())
}

func (uc *unixgramConn) SaveConfig() error {
Expand Down Expand Up @@ -370,7 +391,7 @@ func (uc *unixgramConn) runCommand(cmd string) error {
return err
}

if bytes.Compare(resp, []byte("OK\n")) == 0 {
if bytes.Equal(resp, []byte("OK\n")) {
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions unixgram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ var parseScanResultTests = []struct {
"8a:15:14:8a:46:51\t5560\t-58\t[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]\tWIP-Backoffice\n" +
"8a:15:14:8a:46:50\t5560\t-58\t[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]\tWorkInProgressMember\n",
expect: []*scanResult{
&scanResult{
{
bssid: net.HardwareAddr{0x8a, 0x15, 0x14, 0x8a, 0x46, 0x51},
frequency: 5560,
rssi: -58,
flags: []string{"WPA-PSK-CCMP+TKIP", "WPA2-PSK-CCMP+TKIP", "ESS"},
ssid: "WIP-Backoffice",
},
&scanResult{
{
bssid: net.HardwareAddr{0x8a, 0x15, 0x14, 0x8a, 0x46, 0x50},
frequency: 5560,
rssi: -58,
Expand All @@ -66,12 +66,12 @@ var parseScanResultTests = []struct {
"5560\t8a:15:14:8a:46:51\thello\tWIP-Backoffice\n" +
"5560\t8a:15:14:8a:46:50\tgoodbye\tWorkInProgressMember\n",
expect: []*scanResult{
&scanResult{
{
bssid: net.HardwareAddr{0x8a, 0x15, 0x14, 0x8a, 0x46, 0x51},
frequency: 5560,
ssid: "WIP-Backoffice",
},
&scanResult{
{
bssid: net.HardwareAddr{0x8a, 0x15, 0x14, 0x8a, 0x46, 0x50},
frequency: 5560,
ssid: "WorkInProgressMember",
Expand All @@ -93,7 +93,7 @@ func TestParseScanResults(t *testing.T) {

for i := range output {
if test.expect[i].bssid != nil {
if bytes.Compare(output[i].BSSID(), test.expect[i].bssid) != 0 {
if !bytes.Equal(output[i].BSSID(), test.expect[i].bssid) {
t.Errorf("wrong bssid (got %q, expect %q)", output[i].BSSID(), test.expect[i].bssid)
}
}
Expand Down
14 changes: 9 additions & 5 deletions wpasupplicant.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,26 @@ type Conn interface {

// SetNetwork configures a network property. Returns error if the property
// configuration failed.
SetNetwork(int, string, string) error
// Value's type must one of int, string and []byte. The int type always shown
// without double quotes. The string type always shown with double quotes except
// the variable name is key_mgmt. The []byte type only uses for ssid, maybe useful
// when it contains non-ascii encoded chars.
SetNetwork(networkID int, variable string, value interface{}) error

// EnableNetwork enables a network. Returns error if the command fails.
EnableNetwork(int) error
EnableNetwork(networkID int) error

// EnableAllNetworks enables all configured networks. Returns error if the command fails.
EnableAllNetworks() error

// SelectNetwork selects a network (and disables the others).
SelectNetwork(int) error
SelectNetwork(networkID int) error

// DisableNetwork disables a network.
DisableNetwork(int) error
DisableNetwork(networkID int) error

// RemoveNetwork removes a network from the configuration.
RemoveNetwork(int) error
RemoveNetwork(networkID int) error

// RemoveAllNetworks removes all networks (basically running `REMOVE_NETWORK all`).
// Returns error if command fails.
Expand Down