-
Notifications
You must be signed in to change notification settings - Fork 1
/
discover.go
417 lines (350 loc) · 10.2 KB
/
discover.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package hdhomerun
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/url"
"sync"
"github.com/mdlayher/hdhomerun/internal/libhdhomerun"
)
// A DeviceType is a constant indicating the type of an HDHomeRun device,
// such as a tuner or storage unit.
type DeviceType int
// Possible DeviceType values.
const (
DeviceTypeTuner = DeviceType(libhdhomerun.DeviceTypeTuner)
DeviceTypeStorage = DeviceType(libhdhomerun.DeviceTypeStorage)
// DeviceTypeWildcard is used during discovery to request that all
// types of devices reply to the request.
DeviceTypeWildcard = DeviceType(libhdhomerun.DeviceTypeWildcard)
)
// String returns the string representation of a DeviceType.
func (t DeviceType) String() string {
switch t {
case DeviceTypeTuner:
return "tuner"
case DeviceTypeStorage:
return "storage"
case DeviceTypeWildcard:
return "wildcard"
default:
return fmt.Sprintf("unknown(%d)", t)
}
}
// DeviceIDWildcard is used during discovery to request that a device
// with any ID reply to the request.
const DeviceIDWildcard = "ffffffff" // libhdhomerun.DeviceIdWildcard
// ParseDeviceID parses an eight character hexadecimal string into its
// byte representation for transmission in a Packet.
func ParseDeviceID(id string) ([]byte, error) {
idb, err := hex.DecodeString(id)
if err != nil {
return nil, err
}
if len(idb) != 4 {
return nil, fmt.Errorf("device ID must be eight hexadecimal characters: %q", id)
}
return idb, nil
}
// A Discoverer can discover HDHomeRun devices on a network.
type Discoverer struct {
deviceType DeviceType
deviceID []byte
multicastAddr *net.UDPAddr
localAddr *net.UDPAddr
c net.PacketConn
}
// A DiscovererOption is an option which modifies the behavior of a Discoverer.
type DiscovererOption func(d *Discoverer) error
// DiscoverDeviceType requests that a Discoverer only search for devices with
// the specified type.
func DiscoverDeviceType(t DeviceType) DiscovererOption {
return func(d *Discoverer) error {
d.deviceType = t
return nil
}
}
// DiscoverDeviceID requests that a Discoverer only search for devices with
// the specified ID.
func DiscoverDeviceID(id string) DiscovererOption {
return func(d *Discoverer) error {
idb, err := ParseDeviceID(id)
if err != nil {
return err
}
d.deviceID = idb
return nil
}
}
// discoverLocalUDPAddr controls the address used for the Discoverer's local
// UDP listener.
func discoverLocalUDPAddr(network, addr string) DiscovererOption {
return func(d *Discoverer) error {
udpAddr, err := net.ResolveUDPAddr(network, addr)
if err != nil {
return err
}
d.localAddr = udpAddr
return nil
}
}
// discoverMulticastUDPAddr controls the address used for the Discoverer's
// multicast UDP discovery requests.
func discoverMulticastUDPAddr(network, addr string) DiscovererOption {
return func(d *Discoverer) error {
udpAddr, err := net.ResolveUDPAddr(network, addr)
if err != nil {
return err
}
d.multicastAddr = udpAddr
return nil
}
}
// NewDiscoverer creates a Discoverer that can discover devices using a UDP
// multicast mechanism. By default, the Discoverer will look for any type
// of device with any ID.
//
// If needed, DiscovererOptions can be provided to modify the behavior of
// the Discoverer.
func NewDiscoverer(options ...DiscovererOption) (*Discoverer, error) {
d := &Discoverer{
// Search for any type of device.
deviceType: DeviceTypeWildcard,
// Find all physical HDHomeRun devices on our network.
multicastAddr: &net.UDPAddr{
IP: net.IPv4bcast,
Port: libhdhomerun.DiscoverUdpPort,
},
// Bind to any port on all interfaces.
localAddr: nil,
}
// Prepend options which are applied automatically, so user options
// can override the defaults.
options = append([]DiscovererOption{
// Search for any device ID.
DiscoverDeviceID(DeviceIDWildcard),
}, options...)
for _, o := range options {
if err := o(d); err != nil {
return nil, err
}
}
c, err := net.ListenUDP("udp", d.localAddr)
if err != nil {
return nil, err
}
// Discover devices of specified type and ID using the configured
// multicast group.
b := mustDiscoverPacket(d.deviceType, d.deviceID)
if _, err := c.WriteToUDP(b, d.multicastAddr); err != nil {
_ = c.Close()
return nil, err
}
return &Discoverer{
c: c,
}, nil
}
// A retryableError is an error returned during discovery that indicates a
// malformed reply from a device.
type retryableError struct {
err error
}
// Error implements error.
func (err *retryableError) Error() string {
return err.err.Error()
}
// Discover discovers HDHomeRun devices over a network. Discover will block
// indefinitely until a device is found, or the context is canceled. If
// the context is canceled, an io.EOF error will be returned.
func (d *Discoverer) Discover(ctx context.Context) (*DiscoveredDevice, error) {
select {
case <-ctx.Done():
// If context was already canceled before Discover was called,
// handle cancelation immediately.
_ = d.c.Close()
cerr := ctx.Err()
switch cerr {
case context.Canceled, context.DeadlineExceeded:
// Sentinel value for "done with discovery".
return nil, io.EOF
default:
return nil, cerr
}
default:
}
for {
// Keep trying to discover a device until context is canceled or a
// fatal error is returned. Malformed device replies will result
// in a retryableError.
device, err := d.discover(ctx)
if err != nil {
if _, ok := err.(*retryableError); ok {
continue
}
return nil, err
}
return device, nil
}
}
func (d *Discoverer) discover(ctx context.Context) (*DiscoveredDevice, error) {
// Blocks until a message is received from a device.
msgC := make(chan struct{})
// Ensure the cancelation goroutine exits cleanly.
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
go func() {
// When this goroutine exits, expect no additional messages to
// be sent.
defer close(msgC)
defer wg.Done()
select {
case <-ctx.Done():
// Context canceled; clean up and force io.EOF path.
_ = d.c.Close()
case <-msgC:
// Message received or listener error.
}
}()
b := make([]byte, 2048)
n, addr, err := d.c.ReadFrom(b)
if err != nil {
// Depending on whether or not the context was canceled,
// err might be caused by the goroutine closing the listener.
cerr := ctx.Err()
switch cerr {
case context.Canceled, context.DeadlineExceeded:
// Sentinel value for "done with discovery".
return nil, io.EOF
}
// We failed to receive a reply; notify the goroutine that
// it can stop waiting for context cancelation, and clean
// up the listener.
msgC <- struct{}{}
_ = d.c.Close()
switch cerr {
case nil:
// Unhandled listener errors.
return nil, err
default:
// Unhandled context errors.
return nil, cerr
}
}
// We received a reply successfully; notify the goroutine that
// it can stop waiting for context cancelation.
msgC <- struct{}{}
// There's no guarantee that the message we received is a valid discover
// reply, so any errors here result in another network read to continue
// looking for valid devices.
var p Packet
if err := (&p).UnmarshalBinary(b[:n]); err != nil {
return nil, &retryableError{err: err}
}
device, err := newDiscoveredDevice(addr.String(), p)
if err != nil {
return nil, &retryableError{err: err}
}
return device, nil
}
// A DiscoveredDevice is a device encountered during discovery. Its network
// address can be used with Dial to initiate a direct connection to a device.
type DiscoveredDevice struct {
// ID is the unique ID of this device.
ID string
// Addr is the network address of this device.
Addr string
// Type is the type of device discovered, such as a tuner or storage unit.
Type DeviceType
// URL, if available, is the URL for the device's web UI.
URL *url.URL
// Tuners is the number of TV tuners available to the device.
Tuners int
}
// newDiscoveredDevice creates a DiscoveredDevice using the data from a
// discover reply packet.
func newDiscoveredDevice(addr string, p Packet) (*DiscoveredDevice, error) {
if p.Type != libhdhomerun.TypeDiscoverRpy {
return nil, fmt.Errorf("expected discover reply, but got %#x", p.Type)
}
device := &DiscoveredDevice{
Addr: addr,
}
if err := device.parseTags(p.Tags); err != nil {
return nil, err
}
if device.Type == 0 {
return nil, errors.New("no device type found in discover reply")
}
if device.ID == "" {
return nil, errors.New("no device ID found in discover reply")
}
return device, nil
}
// parseTags parses a slice of Tags for fields that belong to a DiscoveredDevice.
func (d *DiscoveredDevice) parseTags(tags []Tag) error {
for _, t := range tags {
switch t.Type {
case libhdhomerun.TagDeviceType:
if l := len(t.Data); l != 4 {
return fmt.Errorf("unexpected device type length in discover reply: %d", l)
}
d.Type = DeviceType(binary.BigEndian.Uint32(t.Data))
case libhdhomerun.TagDeviceId:
if l := len(t.Data); l != 4 {
return fmt.Errorf("unexpected device ID length in discover reply: %d", l)
}
d.ID = hex.EncodeToString(t.Data)
case libhdhomerun.TagBaseUrl:
u, err := url.Parse(string(t.Data))
if err != nil {
return err
}
d.URL = u
case libhdhomerun.TagTunerCount:
if l := len(t.Data); l != 1 {
return fmt.Errorf("unexpected tuner count length in discover reply: %d", l)
}
d.Tuners = int(t.Data[0])
default:
// TODO(mdlayher): handle additional tags if needed
}
}
return nil
}
// mustDiscoverPacket produces the bytes for a device discovery packet,
// using the specified or wildcard device type and device ID. It panics
// if any errors occur while creating the discovery packet.
func mustDiscoverPacket(typ DeviceType, id []byte) []byte {
if len(id) != 4 {
panicf("device ID must be exactly 4 bytes: %v", id)
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(typ))
p := &Packet{
Type: libhdhomerun.TypeDiscoverReq,
Tags: []Tag{
{
Type: libhdhomerun.TagDeviceType,
Data: b,
},
{
Type: libhdhomerun.TagDeviceId,
Data: id,
},
},
}
pb, err := p.MarshalBinary()
if err != nil {
panicf("failed to marshal discover packet: %v", err)
}
return pb
}
// panicf is a convenience function for panic with fmt.Sprintf.
func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
}