-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxio.go
66 lines (62 loc) · 1.59 KB
/
mxio.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
package mxio
import (
"errors"
"net"
"time"
)
type Mxio struct {
// The interface to use. You can use net.Interfaces() to get a list of
// interfaces in the system.
IF *net.Interface
// The device types to search for. The default value is to search for
// all device types.
DeviceType DeviceType
// The number of times to retry the search.
Retry uint8
// The number of millsecond to wait for results to come back
Timeout time.Duration
}
// Finds all devices types on the first network interface that is up and can Boradcast.
func AutoSearch() ([]Device, error) {
var m Mxio
return m.AutoSearch()
}
// init — we just want to make sure that the default values are good values.
// As the zero values don't make sense, this make the zero values acceptable values.
// If a network interface to use is not provided, we use the first device that is
// ip and able to Broadcast. If not devices are found we return an error of NoInterfaces.
func (m *Mxio) init() error {
if m == nil {
return errors.New("Mxio can not be nil!")
}
if m.DeviceType == DeviceType(0) {
m.DeviceType = ALL_DEVICES
}
if m.IF == nil {
interfaces, err := net.Interfaces()
if len(interfaces) == 0 {
return NoInterfaces
}
if err != nil {
return err
}
for _, ifc := range interfaces {
if (ifc.Flags & (net.FlagUp | net.FlagBroadcast)) != 0 {
m.IF = &ifc
break
}
}
if m.IF == nil {
return NoInterfaces
}
}
if m.Timeout == 0 {
// we want to default the timeout to 5 seconds
m.Timeout = 5 * time.Second
}
if m.Retry == 0 {
// we want the default the retry to 3
m.Retry = 3
}
return nil
}