|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package winwlanapi |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "unsafe" |
| 8 | + |
| 9 | + "golang.org/x/sys/windows" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + ClientVersion2 = 2 |
| 14 | + |
| 15 | + // InterfaceOpcode for WlanQueryInterface |
| 16 | + IntfOpcodeCurrentConnection = 7 |
| 17 | + |
| 18 | + // NotificationSource for WlanRegisterNotification |
| 19 | + NotificationSourceNone = 0 |
| 20 | + NotificationSourceACM = 0x00000008 |
| 21 | + |
| 22 | + // NotificationACM codes |
| 23 | + NotificationACMConnectionComplete = 10 |
| 24 | + NotificationACMDisconnected = 21 |
| 25 | + |
| 26 | + // InterfaceState |
| 27 | + InterfaceStateNotReady = 0 |
| 28 | + InterfaceStateConnected = 1 |
| 29 | + InterfaceStateAdHocNetworkFormed = 2 |
| 30 | + InterfaceStateDisconnecting = 3 |
| 31 | + InterfaceStateDisconnected = 4 |
| 32 | + InterfaceStateAssociating = 5 |
| 33 | + InterfaceStateDiscovering = 6 |
| 34 | + InterfaceStateAuthenticating = 7 |
| 35 | + |
| 36 | + // DOT11_SSID |
| 37 | + Dot11SSIDMaxLength = 32 |
| 38 | +) |
| 39 | + |
| 40 | +type Dot11SSID struct { |
| 41 | + Length uint32 |
| 42 | + SSID [Dot11SSIDMaxLength]byte |
| 43 | +} |
| 44 | + |
| 45 | +type Dot11MacAddress [6]byte |
| 46 | + |
| 47 | +type AssociationAttributes struct { |
| 48 | + SSID Dot11SSID |
| 49 | + BSSType uint32 |
| 50 | + BSSID Dot11MacAddress |
| 51 | + _ [2]byte // padding for 4-byte alignment |
| 52 | + PhyType uint32 |
| 53 | + PhyIndex uint32 |
| 54 | + SignalQuality uint32 |
| 55 | + RxRate uint32 |
| 56 | + TxRate uint32 |
| 57 | +} |
| 58 | + |
| 59 | +type SecurityAttributes struct { |
| 60 | + SecurityEnabled int32 // Windows BOOL is 4 bytes |
| 61 | + OneXEnabled int32 |
| 62 | + AuthAlgorithm uint32 |
| 63 | + CipherAlgorithm uint32 |
| 64 | +} |
| 65 | + |
| 66 | +type ConnectionAttributes struct { |
| 67 | + InterfaceState uint32 |
| 68 | + ConnectionMode uint32 |
| 69 | + ProfileName [256]uint16 |
| 70 | + AssociationAttributes AssociationAttributes |
| 71 | + SecurityAttributes SecurityAttributes |
| 72 | +} |
| 73 | + |
| 74 | +type InterfaceInfo struct { |
| 75 | + InterfaceGUID windows.GUID |
| 76 | + InterfaceDescription [256]uint16 |
| 77 | + InterfaceState uint32 |
| 78 | +} |
| 79 | + |
| 80 | +type InterfaceInfoList struct { |
| 81 | + NumberOfItems uint32 |
| 82 | + Index uint32 |
| 83 | + InterfaceInfo [1]InterfaceInfo |
| 84 | +} |
| 85 | + |
| 86 | +type NotificationData struct { |
| 87 | + NotificationSource uint32 |
| 88 | + NotificationCode uint32 |
| 89 | + InterfaceGUID windows.GUID |
| 90 | + DataSize uint32 |
| 91 | + Data uintptr |
| 92 | +} |
| 93 | + |
| 94 | +// NotificationCallback is the type for notification callback functions. |
| 95 | +// Use syscall.NewCallback to create a callback from a Go function. |
| 96 | +type NotificationCallback func(data *NotificationData, context uintptr) uintptr |
| 97 | + |
| 98 | +func OpenHandle() (windows.Handle, error) { |
| 99 | + var negotiatedVersion uint32 |
| 100 | + var handle windows.Handle |
| 101 | + ret := wlanOpenHandle(ClientVersion2, 0, &negotiatedVersion, &handle) |
| 102 | + if ret != 0 { |
| 103 | + return 0, os.NewSyscallError("WlanOpenHandle", windows.Errno(ret)) |
| 104 | + } |
| 105 | + return handle, nil |
| 106 | +} |
| 107 | + |
| 108 | +func CloseHandle(handle windows.Handle) error { |
| 109 | + ret := wlanCloseHandle(handle, 0) |
| 110 | + if ret != 0 { |
| 111 | + return os.NewSyscallError("WlanCloseHandle", windows.Errno(ret)) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func EnumInterfaces(handle windows.Handle) ([]InterfaceInfo, error) { |
| 117 | + var list *InterfaceInfoList |
| 118 | + ret := wlanEnumInterfaces(handle, 0, &list) |
| 119 | + if ret != 0 { |
| 120 | + return nil, os.NewSyscallError("WlanEnumInterfaces", windows.Errno(ret)) |
| 121 | + } |
| 122 | + defer wlanFreeMemory(uintptr(unsafe.Pointer(list))) |
| 123 | + |
| 124 | + if list.NumberOfItems == 0 { |
| 125 | + return nil, nil |
| 126 | + } |
| 127 | + |
| 128 | + interfaces := unsafe.Slice(&list.InterfaceInfo[0], list.NumberOfItems) |
| 129 | + result := make([]InterfaceInfo, list.NumberOfItems) |
| 130 | + copy(result, interfaces) |
| 131 | + return result, nil |
| 132 | +} |
| 133 | + |
| 134 | +func QueryCurrentConnection(handle windows.Handle, interfaceGUID *windows.GUID) (*ConnectionAttributes, error) { |
| 135 | + var dataSize uint32 |
| 136 | + var data uintptr |
| 137 | + var opcodeValueType uint32 |
| 138 | + |
| 139 | + ret := wlanQueryInterface(handle, interfaceGUID, IntfOpcodeCurrentConnection, 0, &dataSize, &data, &opcodeValueType) |
| 140 | + if ret != 0 { |
| 141 | + return nil, os.NewSyscallError("WlanQueryInterface", windows.Errno(ret)) |
| 142 | + } |
| 143 | + defer wlanFreeMemory(data) |
| 144 | + |
| 145 | + attrs := (*ConnectionAttributes)(unsafe.Pointer(data)) |
| 146 | + result := *attrs |
| 147 | + return &result, nil |
| 148 | +} |
| 149 | + |
| 150 | +func RegisterNotification(handle windows.Handle, notificationSource uint32, callback uintptr, context uintptr) error { |
| 151 | + var prevSource uint32 |
| 152 | + ret := wlanRegisterNotification(handle, notificationSource, false, callback, context, 0, &prevSource) |
| 153 | + if ret != 0 { |
| 154 | + return os.NewSyscallError("WlanRegisterNotification", windows.Errno(ret)) |
| 155 | + } |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func UnregisterNotification(handle windows.Handle) error { |
| 160 | + return RegisterNotification(handle, NotificationSourceNone, 0, 0) |
| 161 | +} |
0 commit comments