diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f616ecf --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module pifke.org/wpasupplicant + +go 1.16 diff --git a/unixgram.go b/unixgram.go index 0cee532..786e0b4 100644 --- a/unixgram.go +++ b/unixgram.go @@ -31,6 +31,8 @@ package wpasupplicant import ( "bufio" "bytes" + "encoding/hex" + "errors" "fmt" "io" "io/ioutil" @@ -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)} @@ -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 { @@ -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 } diff --git a/unixgram_test.go b/unixgram_test.go index 64006f3..1f7d33c 100644 --- a/unixgram_test.go +++ b/unixgram_test.go @@ -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, @@ -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", @@ -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) } } diff --git a/wpasupplicant.go b/wpasupplicant.go index ab3b2d3..cae83ba 100644 --- a/wpasupplicant.go +++ b/wpasupplicant.go @@ -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.