-
Notifications
You must be signed in to change notification settings - Fork 3
/
netlink.go
101 lines (81 loc) · 1.93 KB
/
netlink.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
package main
import (
"syscall"
"github.com/hkwi/nlgo"
)
type genlHuber interface {
Family(string) nlgo.GenlFamily
Sync(nlgo.GenlMessage) ([]nlgo.GenlMessage, error)
}
var newGenHub = func() (genlHuber, error) {
hub, err := nlgo.NewGenlHub()
return hub, err
}
func getLogicalInterfaces() ([]string, error) {
hub, err := newGenHub()
if err != nil {
return nil, err
}
family := hub.Family("nl80211")
resp, err := hub.Sync(family.DumpRequest(nlgo.NL80211_CMD_GET_INTERFACE))
if err != nil {
return nil, err
}
ifMap := make(map[string]struct{})
for _, msg := range resp {
switch msg.Header.Type {
case syscall.NLMSG_DONE:
// do nothing
case syscall.NLMSG_ERROR:
return nil, nlgo.NlMsgerr(msg.NetlinkMessage)
case nlgo.GENL_ID_CTRL:
// do nothing
default:
attrs, err := nlgo.Nl80211Policy.Parse(msg.Body())
if err != nil {
return nil, err
}
ifName := string(attrs.(nlgo.AttrMap).Get(nlgo.NL80211_ATTR_IFNAME).(nlgo.NulString))
ifMap[ifName] = struct{}{}
}
}
var ifList []string
for k := range ifMap {
ifList = append(ifList, k)
}
return ifList, nil
}
func getPhysicalInterfaces() ([]string, error) {
hub, err := newGenHub()
if err != nil {
panic(err)
}
family := hub.Family("nl80211")
resp, err := hub.Sync(family.DumpRequest(nlgo.NL80211_CMD_GET_WIPHY))
if err != nil {
panic(err)
}
phyMap := make(map[string]struct{})
for _, msg := range resp {
switch msg.Header.Type {
case syscall.NLMSG_DONE:
// do nothing
case syscall.NLMSG_ERROR:
return nil, nlgo.NlMsgerr(msg.NetlinkMessage)
case nlgo.GENL_ID_CTRL:
// do nothing
default:
attrs, err := nlgo.Nl80211Policy.Parse(msg.Body())
if err != nil {
return nil, err
}
phyName := string(attrs.(nlgo.AttrMap).Get(nlgo.NL80211_ATTR_WIPHY_NAME).(nlgo.NulString))
phyMap[phyName] = struct{}{}
}
}
var phyList []string
for k := range phyMap {
phyList = append(phyList, k)
}
return phyList, nil
}