This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
control_plane.go
241 lines (222 loc) · 5.53 KB
/
control_plane.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
package ch912x
import (
"bytes"
"context"
"net"
"time"
"github.com/mdlayher/arp"
)
const (
listenPort = 60000
controlPort = 50000
)
type ControlPlane struct {
udpClient net.PacketConn
arpClient *arp.Client
clientMAC net.HardwareAddr
pairs map[string]func(Module)
arpTable map[string]func()
discovery chan Module
Timeout time.Duration
SendTimeout time.Duration
}
func ListenCH912XByName(name string) (*ControlPlane, error) {
ifi, err := net.InterfaceByName(name)
if err != nil {
return nil, err
}
return ListenCH912X(ifi)
}
func ListenCH912X(ifi *net.Interface) (plane *ControlPlane, err error) {
if ifi == nil {
err = ErrInvalidNetworkInterface
return
}
addr := &net.UDPAddr{Port: listenPort}
plane = &ControlPlane{
discovery: make(chan Module),
pairs: make(map[string]func(Module)),
arpTable: make(map[string]func()),
clientMAC: ifi.HardwareAddr,
Timeout: 15 * time.Second,
SendTimeout: time.Second,
}
plane.udpClient, err = net.ListenUDP("udp", addr)
if err == nil {
err = bindInterfaceToUDPConn(plane.udpClient.(*net.UDPConn), ifi)
}
if err == nil {
plane.arpClient, err = arp.Dial(ifi)
}
if err == nil {
go plane.watchUDP()
go plane.watchARP()
}
return
}
func (p *ControlPlane) watchUDP() {
var data [0x200]byte
for {
n, _, err := p.udpClient.ReadFrom(data[:])
if err != nil {
break
}
go p.handleResponse(data[:n])
}
close(p.discovery)
}
func (p *ControlPlane) watchARP() {
for {
packet, _, err := p.arpClient.Read()
if err != nil {
break
}
go p.handleARP(packet)
}
return
}
func (p *ControlPlane) Discovery() <-chan Module {
return p.discovery
}
func (p *ControlPlane) SendDiscovery(product Product) (err error) {
err = ErrUnknownModuleType
switch product {
case ProductCH9120:
err = p.push(&CH9120{Kind: KindDiscoveryRequest})
case ProductCH9121:
err = p.push(&CH9121{Kind: KindDiscoveryRequest})
case ProductCH9126:
err = p.push(&CH9126{Kind: KindDiscoveryRequest})
}
return
}
func (p *ControlPlane) Pull(ctx context.Context, product Product, address net.HardwareAddr) (module Module, err error) {
err = ErrUnknownModuleType
switch product {
case ProductCH9120:
module, err = p.send(ctx, &CH9120{Kind: KindPullRequest, ModuleMAC: address})
case ProductCH9121:
module, err = p.send(ctx, &CH9121{Kind: KindPullRequest, ModuleMAC: address})
case ProductCH9126:
module, err = p.send(ctx, &CH9126{Kind: KindPullRequest, ModuleMAC: address})
}
return
}
func (p *ControlPlane) Push(ctx context.Context, module Module) (parsed Module, err error) {
ctx, cancel := context.WithTimeout(ctx, p.Timeout)
defer cancel()
kind, address := module.identity()
if kind != KindPushRequest {
err = ErrModuleKindWrong
return
}
parsed, err = p.send(ctx, module)
if err != nil {
return
}
select {
case <-ctx.Done():
return
case <-p.waitFirstARP(address):
return
}
}
func (p *ControlPlane) Reset(ctx context.Context, product Product, address net.HardwareAddr) (module Module, err error) {
ctx, cancel := context.WithTimeout(ctx, p.Timeout)
defer cancel()
err = ErrUnknownModuleType
switch product {
case ProductCH9120:
module, err = p.send(ctx, &CH9120{Kind: KindResetRequest, ModuleMAC: address})
case ProductCH9121:
module, err = p.send(ctx, &CH9121{Kind: KindResetRequest, ModuleMAC: address})
case ProductCH9126:
module, err = p.send(ctx, &CH9126{Kind: KindResetRequest, ModuleMAC: address})
}
if err != nil {
return
}
select {
case <-ctx.Done():
return
case <-p.waitFirstARP(address):
return
}
}
func (p *ControlPlane) send(ctx context.Context, module Module) (parsed Module, err error) {
ctx, cancel := context.WithTimeout(ctx, p.SendTimeout)
defer cancel()
module.setClientMAC(p.clientMAC)
_, addr := module.identity()
if addr == nil {
err = ErrModuleMustMAC
return
} else if _, ok := p.pairs[addr.String()]; ok {
err = ErrTaskRunning
return
}
returns := make(chan Module, 1)
p.pairs[addr.String()] = func(parsed Module) { returns <- parsed }
defer delete(p.pairs, addr.String())
if err = p.push(module); err != nil {
return
}
select {
case <-ctx.Done():
err = ctx.Err()
case parsed = <-returns:
err, _ = parsed.(error)
}
return
}
func (p *ControlPlane) push(module Module) (err error) {
ip := module.moduleIP()
if ip == nil {
ip = net.IPv4bcast
}
var buf bytes.Buffer
_, _ = module.WriteTo(&buf)
_, err = p.udpClient.WriteTo(buf.Bytes(), &net.UDPAddr{IP: ip, Port: controlPort})
return
}
func (p *ControlPlane) waitFirstARP(address net.HardwareAddr) chan struct{} {
returns := make(chan struct{}, 1)
p.arpTable[address.String()] = func() { returns <- struct{}{} }
defer delete(p.arpTable, address.String())
return returns
}
func (p *ControlPlane) handleResponse(data []byte) {
var module Module
switch {
case bytes.HasPrefix(data, []byte(magicCH9120)):
module = new(CH9120)
case bytes.HasPrefix(data, []byte(magicCH9121)):
module = new(CH9121)
case bytes.HasPrefix(data, []byte(magicCH9126)):
module = new(CH9126)
case bytes.HasPrefix(data, []byte(magicModule)):
return
default:
panic(ErrUnknownModuleType)
}
_, _ = module.ReadFrom(bytes.NewReader(data))
kind, addr := module.identity()
if kind == KindDiscoveryResponse {
p.discovery <- module
} else if fn, ok := p.pairs[addr.String()]; ok {
fn(module)
}
return
}
func (p *ControlPlane) handleARP(packet *arp.Packet) {
if fn, ok := p.arpTable[packet.SenderHardwareAddr.String()]; ok {
fn()
}
}
func (p *ControlPlane) Close() (err error) {
err = p.udpClient.Close()
if err == nil {
err = p.arpClient.Close()
}
return
}