Skip to content

Commit

Permalink
fix(network): map SSID to .Name
Browse files Browse the repository at this point in the history
resolves #5742
  • Loading branch information
JanDeDobbeleer committed Oct 14, 2024
1 parent 49ea489 commit 2557ee2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/runtime/networks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type DOT11_SSID struct {
ucSSID [DOT11_SSID_MAX_LENGTH]uint8
}

func (env *Terminal) getConnections() []*Connection {
func (term *Terminal) getConnections() []*Connection {
var pIFTable2 *MIN_IF_TABLE2
_, _, _ = hGetIfTable2.Call(uintptr(unsafe.Pointer(&pIFTable2)))

Expand All @@ -153,7 +153,6 @@ func (env *Terminal) getConnections() []*Connection {
for i := 0; i < int(pIFTable2.NumEntries); i++ {
networkInterface := pIFTable2.Table[i]
alias := strings.TrimRight(syscall.UTF16ToString(networkInterface.Alias[:]), "\x00")
description := strings.TrimRight(syscall.UTF16ToString(networkInterface.Description[:]), "\x00")

if networkInterface.OperStatus != 1 || // not connected or functional
!networkInterface.InterfaceAndOperStatusFlags.HardwareInterface || // rule out software interfaces
Expand Down Expand Up @@ -181,9 +180,11 @@ func (env *Terminal) getConnections() []*Connection {
continue
}

term.DebugF("Found network interface: %s", alias)

network := &Connection{
Type: connectionType,
Name: description, // we want a relatable name, alias isn't that
Name: alias,
TransmitRate: networkInterface.TransmitLinkSpeed,
ReceiveRate: networkInterface.ReceiveLinkSpeed,
SSID: ssid,
Expand All @@ -192,15 +193,15 @@ func (env *Terminal) getConnections() []*Connection {
networks = append(networks, network)
}

if wifi, err := env.wifiNetwork(); err == nil {
if wifi, err := term.wifiNetwork(); err == nil {
networks = append(networks, wifi)
}

return networks
}

func (env *Terminal) wifiNetwork() (*Connection, error) {
env.Trace(time.Now())
func (term *Terminal) wifiNetwork() (*Connection, error) {
term.Trace(time.Now())
// Open handle
var pdwNegotiatedVersion uint32
var phClientHandle uint32
Expand All @@ -209,7 +210,6 @@ func (env *Terminal) wifiNetwork() (*Connection, error) {
return nil, err
}

// defer closing handle
defer func() {
_, _, _ = hWlanCloseHandle.Call(uintptr(phClientHandle), uintptr(unsafe.Pointer(nil)))
}()
Expand All @@ -229,12 +229,14 @@ func (env *Terminal) wifiNetwork() (*Connection, error) {
if network.isState != 1 {
continue
}
return env.parseNetworkInterface(network, phClientHandle)

return term.parseNetworkInterface(network, phClientHandle)
}

return nil, errors.New("Not connected")
}

func (env *Terminal) parseNetworkInterface(network *WLAN_INTERFACE_INFO, clientHandle uint32) (*Connection, error) {
func (term *Terminal) parseNetworkInterface(network *WLAN_INTERFACE_INFO, clientHandle uint32) (*Connection, error) {
info := Connection{
Type: WIFI,
}
Expand All @@ -250,14 +252,16 @@ func (env *Terminal) parseNetworkInterface(network *WLAN_INTERFACE_INFO, clientH
uintptr(unsafe.Pointer(&wlanAttr)),
uintptr(unsafe.Pointer(nil)))
if e != 0 {
env.Error(err)
term.Error(err)
return &info, err
}

// SSID
ssid := wlanAttr.wlanAssociationAttributes.dot11Ssid
if ssid.uSSIDLength > 0 {
info.SSID = string(ssid.ucSSID[0:ssid.uSSIDLength])
info.Name = info.SSID
term.DebugF("Found wifi interface: %s", info.SSID)
}

info.TransmitRate = uint64(wlanAttr.wlanAssociationAttributes.ulTxRate / 1024)
Expand Down
1 change: 1 addition & 0 deletions src/runtime/terminal_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (term *Terminal) Connection(_ ConnectionType) (*Connection, error) {
if len(term.networks) == 0 {
return nil, &NotImplemented{}
}

return nil, &NotImplemented{}
}

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/terminal_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,13 @@ func (term *Terminal) Connection(connectionType ConnectionType) (*Connection, er
}
term.networks = networks
}

for _, network := range term.networks {
if network.Type == connectionType {
return network, nil
}
}

term.Error(fmt.Errorf("Network type '%s' not found", connectionType))
return nil, &NotImplemented{}
}
Expand Down
1 change: 0 additions & 1 deletion website/docs/segments/system/connection.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ import Config from '@site/src/components/Config.js';
| ------- | -------- | ------------------------------------------------------- |
| `.Type` | `string` | the connection type type. Single values of `type` above |
| `.Name` | `string` | the name of the connection |
| `.SSID` | `string` | the SSID of the current wifi network |

[templates]: /docs/configuration/templates

0 comments on commit 2557ee2

Please sign in to comment.