-
Notifications
You must be signed in to change notification settings - Fork 4
/
netutil.go
40 lines (35 loc) · 886 Bytes
/
netutil.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
package kasa
import (
"net"
"strings"
)
// BroadcastAddresses - probably belongs in its own library, get all broadcast addresses
func BroadcastAddresses() ([]net.IP, error) {
var broadcasts []net.IP
ifaces, err := net.Interfaces()
if err != nil {
return broadcasts, err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return broadcasts, err
}
for _, addr := range addrs {
as := addr.String()
// ignore IPv6 and loopback since Kasa devices are v4 only (for now)
if !strings.Contains(as, ":") && !strings.HasPrefix(as, "127.") {
_, ipnet, err := net.ParseCIDR(as)
if err != nil {
return broadcasts, err
}
broadcast := net.IP(make([]byte, 4))
for i := range ipnet.IP {
broadcast[i] = ipnet.IP[i] | ^ipnet.Mask[i]
}
broadcasts = append(broadcasts, broadcast)
}
}
}
return broadcasts, nil
}