-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwip_linux.go
61 lines (53 loc) · 1.2 KB
/
wip_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
package main
import (
"fmt"
"net"
"os"
)
// find address of wireless interface or error
// on !Linux
// if no wlan was found
// if more than one wlan was found
// otherwise
func findWirelessIP() (string, net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", nil, err
}
var ip net.IP
var wlan string
wlanFound := false
for _, iface := range ifaces {
fi, err := os.Lstat(fmt.Sprintf("/sys/class/net/%s/wireless/", iface.Name))
if err != nil {
continue
}
if fi.Mode().IsDir() {
if wlanFound {
return "", nil, errQqget("Support for more than one wlan interface is not implemented")
}
wlanFound = true
wlan = iface.Name
// find ip address
addrs, err := iface.Addrs()
if err != nil {
return "", nil, err
}
for _, addr := range addrs {
// https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// TODO: here we assume first ip is great, but if not?
break
}
}
}
if !wlanFound {
return "", nil, errQqget("Support for non wlan interface is not implemented")
}
return wlan, ip, nil
}