114 lines
2.4 KiB
Go
114 lines
2.4 KiB
Go
package hardware
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"github.com/BeatGlow/beatglow/internal/ioctl"
|
|
)
|
|
|
|
const (
|
|
SIOCGIWNAME ioctl.Command = 0x8B01
|
|
SIOCGIWSTATS ioctl.Command = 0x8B0F
|
|
SIOCGIWESSID ioctl.Command = 0x8B1B
|
|
)
|
|
|
|
func isWireless(iface net.Interface) bool {
|
|
i, err := os.Stat(fmt.Sprintf("/sys/class/net/%s/wireless", iface.Name))
|
|
return err == nil && i.IsDir()
|
|
}
|
|
|
|
func wirelessStatus(iface net.Interface) (ssid string, level, noise int, err error) {
|
|
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
|
|
if err != nil {
|
|
return "", 0, 0, err
|
|
}
|
|
defer syscall.Close(fd)
|
|
|
|
var b []byte
|
|
if _, err = iwIOCTL(uintptr(fd), iface.Name, SIOCGIWNAME); err != nil {
|
|
return
|
|
}
|
|
|
|
if b, err = iwIOCTL(uintptr(fd), iface.Name, SIOCGIWESSID); err != nil {
|
|
return
|
|
}
|
|
ssid = cstring(b)
|
|
|
|
if b, err = iwIOCTL(uintptr(fd), iface.Name, SIOCGIWSTATS); err != nil {
|
|
return
|
|
}
|
|
var stats iw_statistics
|
|
if err = binary.Read(bytes.NewBuffer(b), binary.BigEndian, &stats); err != nil {
|
|
return
|
|
}
|
|
|
|
level = int(stats.Qual.Level)
|
|
noise = int(stats.Qual.Noise)
|
|
|
|
return
|
|
}
|
|
|
|
func iwIOCTL(fd uintptr, iface string, call ioctl.Command) ([]byte, error) {
|
|
var buffer [32]byte
|
|
data := make([]byte, 16+8)
|
|
copy(data, []byte(iface))
|
|
binary.LittleEndian.PutUint32(data[16:], uint32(uintptr(unsafe.Pointer(&buffer[0]))))
|
|
binary.LittleEndian.PutUint32(data[20:], 32)
|
|
if err := ioctl.Do(fd, call, &data[0]); err != nil {
|
|
return nil, err
|
|
}
|
|
return buffer[:], nil
|
|
}
|
|
|
|
func cstring(cstr []byte) string {
|
|
for i, b := range cstr {
|
|
if b == 0x00 {
|
|
return string(cstr[:i-1])
|
|
}
|
|
}
|
|
return string(cstr)
|
|
}
|
|
|
|
type iwreq struct {
|
|
data [16]byte
|
|
u uintptr
|
|
}
|
|
|
|
type iw_point struct {
|
|
pointer uintptr /* Pointer to the data (in user space) */
|
|
length uint16 /* number of fields or size in bytes */
|
|
flags uint16 /* Optional params */
|
|
}
|
|
|
|
type iw_statistics struct {
|
|
Status uint16
|
|
Qual iw_quality
|
|
Discard iw_discarded
|
|
Miss iw_missed
|
|
}
|
|
|
|
type iw_quality struct {
|
|
Qual uint8
|
|
Level int8
|
|
Noise int8
|
|
Updated uint8
|
|
}
|
|
|
|
type iw_discarded struct {
|
|
NWID uint32 /* Rx : Wrong nwid/essid */
|
|
Code uint32 /* Rx : Unable to code/decode (WEP) */
|
|
Fragment uint32 /* Rx : Can't perform MAC reassembly */
|
|
Retries uint32 /* Tx : Max MAC retries num reached */
|
|
Misc uint32 /* Others cases */
|
|
}
|
|
|
|
type iw_missed struct {
|
|
Beacon uint32 /* Missed beacons/superframe */
|
|
}
|