forked from play175/wifiNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiNotifer_linux.go
331 lines (269 loc) · 8.75 KB
/
wifiNotifer_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// +build linux
package wifiNotifier
/*
// Trying to avoid using dynamic linking, therefore disabled 'iwlib'
// (wireless-tools library, which is requires to have installed correspond package).
// If you want to use original iwlib package (and do not use custom 'linux_iwlib_2.c'):
// 1) uncomment '#cgo LDFLAGS: -liw'
// 2) comment '#include "iwlib_2_linux.c"'
// 3) remove suffix '_2' from function names (in this file): iw_get_range_info_2, iw_init_event_stream_2, iw_extract_event_stream_2
// #cgo LDFLAGS: -liw
#include "iwlib_2_linux.c"
#include <stdio.h> // printf
#include <string.h> // strndup prototype
#include <stdlib.h> // free protype
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <iwlib.h> // sudo apt-get install libiw-dev
#include <ifaddrs.h>
static inline char* concatenate(char* baseString, const char* toAdd, char delimiter) {
if (toAdd == NULL)
return baseString;
int addingLen = strlen(toAdd);
if (addingLen == 0)
return baseString;
if (baseString == NULL) {
baseString = (char*)malloc(addingLen +1);
memset(baseString, 0, addingLen + 1);
strcpy(baseString, toAdd);
return baseString;
}
int newSize = strlen(baseString) + ((delimiter != 0) ? 1 : 0) + addingLen + 1;
char* newString = (char*)malloc(newSize);
if (delimiter != 0)
sprintf(newString, "%s%c%s", baseString, delimiter, toAdd);
else
sprintf(newString, "%s%s", baseString, toAdd);
free(baseString);
return newString;
}
static inline char* scanSSIDList(const char* interfaceName, int *retIsInsecure, const char* ssidToCheckSecurity) {
char *ret = NULL;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
return NULL;
//---------------------------------------------------------------------
struct iw_range range;
if ((iw_get_range_info_2(sockfd, interfaceName, &range) < 0) ||
(range.we_version_compiled < 14))
{
close(sockfd);
return NULL; // interface doesn't support scanning
}
__u8 wev = range.we_version_compiled;
//---------------------------------------------------------------------
struct iwreq request;
memset(&request, 0, sizeof(request));
request.u.param.flags = IW_SCAN_DEFAULT;
request.u.param.value = 0;
if (iw_set_ext(sockfd, interfaceName, SIOCSIWSCAN, &request) == -1)
{
close(sockfd);
return NULL;
}
//---------------------------------------------------------------------
struct timeval startTime, endTime, diffTime = { 0, 0 };
gettimeofday(&startTime, NULL);
char scanBuffer[0xFFFF];
int replyFound = 0;
while (replyFound == 0)
{
memset(scanBuffer, 0, sizeof(scanBuffer));
request.u.data.pointer = scanBuffer;
request.u.data.length = sizeof(scanBuffer);
request.u.data.flags = 0;
int result = iw_get_ext(sockfd,
interfaceName,
SIOCGIWSCAN,
&request);
if (result == -1 && errno != EAGAIN)
{
close(sockfd);
return NULL;
}
if (result == 0)
{
replyFound = 1;
break;
}
gettimeofday(&endTime, NULL);
timersub(&endTime, &startTime, &diffTime);
if (diffTime.tv_sec > 10)
break;
usleep(100000);
}
close(sockfd);
//---------------------------------------------------------------------
if (replyFound)
{
struct iw_event iwe;
struct stream_descr stream;
iw_init_event_stream_2(&stream,
scanBuffer,
request.u.data.length);
char eventBuffer[512] = {0};
char essid[IW_ESSID_MAX_SIZE+1];
unsigned short encodeFlags = -1;
while (iw_extract_event_stream_2(&stream, &iwe, wev) > 0)
{
switch (iwe.cmd)
{
case SIOCGIWESSID:
{
memset(essid, 0, sizeof(essid));
if((iwe.u.essid.pointer) && (iwe.u.essid.length))
{
memcpy(essid,
iwe.u.essid.pointer,
iwe.u.essid.length);
essid[iwe.u.essid.length] = 0;
ret = concatenate(ret, essid, '\n');
if (retIsInsecure!=NULL
&& ssidToCheckSecurity!=NULL
&& encodeFlags != -1
&& strcmp(essid, ssidToCheckSecurity)==0)
{
*retIsInsecure = ( encodeFlags & IW_ENCODE_DISABLED ) > 0; // IW_ENCODE_OPEN
encodeFlags = -1;
}
}
}
break;
case SIOCGIWENCODE:
{
encodeFlags = iwe.u.encoding.flags;
break;
}
}
}
}
return ret;
}
static inline char* get_essid (char *iface)
{
int fd;
struct iwreq w;
char essid[IW_ESSID_MAX_SIZE+1];
if (!iface) return NULL;
fd = socket(AF_INET, SOCK_DGRAM, 0);
strncpy (w.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
memset (essid, 0, IW_ESSID_MAX_SIZE);
w.u.essid.pointer = (caddr_t *) essid;
w.u.data.length = IW_ESSID_MAX_SIZE;
w.u.data.flags = 0;
int isOK = ioctl (fd, SIOCGIWESSID, &w);
close (fd);
if (isOK != 0) return NULL;
return strndup (essid, 32); // normally, the IW_ESSID_MAX_SIZE is 32 bytes (the coping with potential security flaws within the driver)
}
static inline char * getCurrentWifiInfo(int* retIsInsecure) {
char* retSSID = NULL;
// get all available network interfaces
struct ifaddrs *addrs,*tmp_addrs;
getifaddrs(&addrs);
tmp_addrs = addrs;
while (tmp_addrs)
{
if (tmp_addrs->ifa_addr && tmp_addrs->ifa_addr->sa_family == AF_PACKET)
{
retSSID = get_essid (tmp_addrs->ifa_name);
// do not forget to free 'retSSID' from memory!
if (retSSID!=NULL)
{
if (retIsInsecure!=NULL) {
char* wifiList = scanSSIDList(tmp_addrs->ifa_name, retIsInsecure, retSSID);
if (wifiList!=NULL) free(wifiList);
}
break;
}
}
tmp_addrs = tmp_addrs->ifa_next;
}
freeifaddrs(addrs);
return retSSID;
}
static inline char * getCurrentSSID(void) {
return getCurrentWifiInfo(NULL);
}
static inline int getCurrentNetworkIsInsecure() {
int retIsInecure = 0xFFFFFFFF;
char* ssid = getCurrentWifiInfo(&retIsInecure);
if (ssid!=NULL) free(ssid);
return retIsInecure;
}
static inline char* getAvailableSSIDs(void) {
char* retSSID = NULL;
// get all available network interfaces
struct ifaddrs *addrs,*tmp_addrs;
getifaddrs(&addrs);
tmp_addrs = addrs;
while (tmp_addrs)
{
if (tmp_addrs->ifa_addr && tmp_addrs->ifa_addr->sa_family == AF_PACKET)
retSSID = concatenate(retSSID, scanSSIDList(tmp_addrs->ifa_name, NULL, NULL), '\n');
tmp_addrs = tmp_addrs->ifa_next;
}
freeifaddrs(addrs);
return retSSID;
}
*/
import "C"
import (
"fmt"
"strings"
"unsafe"
"github.com/ivpn/desktop-app-daemon/oshelpers/linux/netlink"
)
// GetAvailableSSIDs returns the list of the names of available Wi-Fi networks
func GetAvailableSSIDs() []string {
ssidList := C.getAvailableSSIDs()
goSsidList := C.GoString(ssidList)
C.free(unsafe.Pointer(ssidList))
return strings.Split(goSsidList, "\n")
}
// GetCurrentSSID returns current WiFi SSID
func GetCurrentSSID() string {
ssid := C.getCurrentSSID()
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
return goSsid
}
// GetCurrentNetworkIsInsecure returns current security mode
func GetCurrentNetworkIsInsecure() bool {
// TODO: implement GetCurrentNetworkIsInsecure functionality for Linux
return false
// ret := WiFiSecurityUnknown
// if C.getCurrentNetworkIsInsecure() == 1 {
// ret = WiFiSecurityNone
// }
// return ret
}
// SetWifiNotifier initializes a handler method 'OnWifiChanged'
func SetWifiNotifier(cb func(string)) error {
if cb == nil {
return fmt.Errorf("callback function not defined")
}
l, err := netlink.CreateListener()
if err != nil {
return fmt.Errorf("Netlink listener initialization error: %w", err)
}
go func() {
for {
msgs, err := l.ReadMsgs()
if err != nil {
fmt.Println("Could not read netlink messages: %s", err)
}
for _, m := range msgs {
if netlink.IsNewAddr(&m) || netlink.IsDelAddr(&m) {
cb(GetCurrentSSID())
}
}
}
}()
return nil
}