/
options.go
120 lines (94 loc) · 2.72 KB
/
options.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
package net
import "net"
// A UDPOption sets options such as errors parameters, etc.
type UDPOption interface {
applyUDP(*udpConnOptions)
}
type ErrorsOpt struct {
errors func(err error)
}
func (h ErrorsOpt) applyUDP(o *udpConnOptions) {
o.errors = h.errors
}
func WithErrors(v func(err error)) ErrorsOpt {
return ErrorsOpt{
errors: v,
}
}
func DefaultMulticastOptions() MulticastOptions {
return MulticastOptions{
IFaceMode: MulticastAllInterface,
HopLimit: 1,
}
}
type MulticastInterfaceMode int
const (
MulticastAllInterface MulticastInterfaceMode = 0
MulticastAnyInterface MulticastInterfaceMode = 1
MulticastSpecificInterface MulticastInterfaceMode = 2
)
type InterfaceError = func(iface *net.Interface, err error)
type MulticastOptions struct {
IFaceMode MulticastInterfaceMode
Iface *net.Interface
Source *net.IP
HopLimit int
InterfaceError InterfaceError
}
func (m *MulticastOptions) Apply(o MulticastOption) {
o.applyMC(m)
}
// A MulticastOption sets options such as hop limit, etc.
type MulticastOption interface {
applyMC(*MulticastOptions)
}
type MulticastInterfaceModeOpt struct {
mode MulticastInterfaceMode
}
func (m MulticastInterfaceModeOpt) applyMC(o *MulticastOptions) {
o.IFaceMode = m.mode
}
func WithAnyMulticastInterface() MulticastOption {
return MulticastInterfaceModeOpt{mode: MulticastAnyInterface}
}
func WithAllMulticastInterface() MulticastOption {
return MulticastInterfaceModeOpt{mode: MulticastAllInterface}
}
type MulticastInterfaceOpt struct {
iface net.Interface
}
func (m MulticastInterfaceOpt) applyMC(o *MulticastOptions) {
o.Iface = &m.iface
o.IFaceMode = MulticastSpecificInterface
}
func WithMulticastInterface(iface net.Interface) MulticastOption {
return &MulticastInterfaceOpt{iface: iface}
}
type MulticastHoplimitOpt struct {
hoplimit int
}
func (m MulticastHoplimitOpt) applyMC(o *MulticastOptions) {
o.HopLimit = m.hoplimit
}
func WithMulticastHoplimit(hoplimit int) MulticastOption {
return &MulticastHoplimitOpt{hoplimit: hoplimit}
}
type MulticastSourceOpt struct {
source net.IP
}
func (m MulticastSourceOpt) applyMC(o *MulticastOptions) {
o.Source = &m.source
}
func WithMulticastSource(source net.IP) MulticastOption {
return &MulticastSourceOpt{source: source}
}
type MulticastInterfaceErrorOpt struct {
interfaceError InterfaceError
}
func (m MulticastInterfaceErrorOpt) applyMC(o *MulticastOptions) {
o.InterfaceError = m.interfaceError
}
// WithMulticastInterfaceError sets the callback for interface errors. If it is set error is not propagated as result of WriteMulticast.
func WithMulticastInterfaceError(interfaceError InterfaceError) MulticastOption {
return &MulticastInterfaceErrorOpt{interfaceError: interfaceError}
}