-
Notifications
You must be signed in to change notification settings - Fork 73
/
pconn_manager.go
246 lines (223 loc) · 5.81 KB
/
pconn_manager.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
package quic
import (
"net"
"strings"
"sync"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
// reuse "github.com/jbenet/go-reuseport"
)
type receivedRawPacket struct {
rcvPconn net.PacketConn
remoteAddr net.Addr
data []byte
rcvTime time.Time
}
type pconnManager struct {
// Two kinds of PacketConn: on specific unicast address and the "master"
// listening on any
mutex sync.Mutex
pconns map[string]net.PacketConn
pconnAny net.PacketConn
localAddrs []net.UDPAddr
perspective protocol.Perspective
rcvRawPackets chan *receivedRawPacket
changePaths chan struct{}
closeConns chan struct{}
closed chan struct{}
errorConn chan error
timer *time.Timer
}
// Setup the pconn_manager and the pconnAny connection
func (pcm *pconnManager) setup(pconnArg net.PacketConn, listenAddr net.Addr) error {
pcm.pconns = make(map[string]net.PacketConn)
pcm.localAddrs = make([]net.UDPAddr, 0)
pcm.rcvRawPackets = make(chan *receivedRawPacket)
pcm.changePaths = make(chan struct{}, 1)
pcm.closeConns = make(chan struct{}, 1)
pcm.closed = make(chan struct{}, 1)
pcm.errorConn = make(chan error, 1) // Made non-blocking for tests
pcm.timer = time.NewTimer(0)
if pconnArg == nil {
// XXX (QDC): waiting for native support of SO_REUSEADDR in go...
//var listenAddrStr string
//if listenAddr == nil {
// listenAddrStr = "[::]:0"
//} else {
// listenAddrStr = listenAddr.String()
//}
pconn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
// pconn, err := reuse.ListenPacket("udp", listenAddrStr)
if err != nil {
utils.Errorf("pconn_manager: %v", err)
// Format for expected consistency
operr := &net.OpError{Op: "listen", Net: "udp", Source: listenAddr, Addr: listenAddr, Err: err}
return operr
}
pcm.pconnAny = pconn
} else {
// FIXME Update localAddrs
pcm.pconnAny = pconnArg
}
if utils.Debug() {
utils.Debugf("Created pconn_manager, any on %s", pcm.pconnAny.LocalAddr().String())
}
// Run the pconnManager
go pcm.run()
return nil
}
func (pcm *pconnManager) listen(pconn net.PacketConn) {
var err error
listenLoop:
for {
var n int
var addr net.Addr
data := getPacketBuffer()
data = data[:protocol.MaxReceivePacketSize]
// The packet size should not exceed protocol.MaxReceivePacketSize bytes
// If it does, we only read a truncate packet, which will then end up undecryptable
n, addr, err = pconn.ReadFrom(data)
if err != nil {
// XXX (QDC): as soon as a path failed, kill the connection.
// TODO (QDC): be more resilient in the future without breaking expectations
select {
case pcm.errorConn <- err:
default:
// Don't block
}
break listenLoop
// if pconn == pconnAny {
// }
// if !strings.HasSuffix(err.Error(), "use of closed network connection") {
// TODO
// c.session.Close(err)
// }
// break
}
data = data[:n]
rcvRawPacket := &receivedRawPacket{
rcvPconn: pconn,
remoteAddr: addr,
data: data,
rcvTime: time.Now(),
}
pcm.rcvRawPackets <- rcvRawPacket
}
}
func (pcm *pconnManager) run() {
// First start to listen to the sockets
go pcm.listen(pcm.pconnAny)
// XXX (QDC): maybe wait for one handshake to complete, but maybe not needed
// FIXME Server starting on any vs. server with non-any address
if pcm.perspective == protocol.PerspectiveClient {
pcm.createPconns()
}
select {
case pcm.changePaths <- struct{}{}:
default:
}
// Start the timer for periodic interface checking (only for client)
duration, _ := time.ParseDuration("2s")
if pcm.perspective == protocol.PerspectiveClient {
pcm.timer.Reset(duration)
} else {
if !pcm.timer.Stop() {
<-pcm.timer.C
}
}
runLoop:
for {
select {
case <-pcm.closeConns:
break runLoop
case <-pcm.timer.C:
pcm.createPconns()
pcm.timer.Reset(duration)
}
}
// Close pconns
pcm.closePconns()
}
func (pcm *pconnManager) createPconn(ip net.IP) (*net.UDPAddr, error) {
// XXX (QDC): waiting for native support of SO_REUSEADDR in go...
//var listenAddrStr string
//if ip.To4() != nil {
// listenAddrStr = ip.String() + ":0"
//} else {
// listenAddrStr = "[" + ip.String() + "]:0"
//}
// pconn, err := reuse.ListenPacket("udp", listenAddrStr)
pconn, err := net.ListenUDP("udp", &net.UDPAddr{IP: ip, Port: 0})
if err != nil {
return nil, err
}
locAddr, err := net.ResolveUDPAddr("udp", pconn.LocalAddr().String())
if err != nil {
return nil, err
}
pcm.mutex.Lock()
pcm.pconns[locAddr.String()] = pconn
pcm.mutex.Unlock()
if utils.Debug() {
utils.Debugf("Created pconn on %s", pconn.LocalAddr().String())
}
// Start to listen on this new socket
go pcm.listen(pconn)
// Don't block
select {
case pcm.changePaths <- struct{}{}:
default:
}
return locAddr, nil
}
func (pcm *pconnManager) createPconns() error {
ifaces, err := net.Interfaces()
if err != nil {
return err
}
for _, i := range ifaces {
// TODO (QDC): do this in a generic way
if !strings.Contains(i.Name, "eth") && !strings.Contains(i.Name, "rmnet") && !strings.Contains(i.Name, "wlan") {
continue
}
addrs, err := i.Addrs()
if err != nil {
return err
}
for _, a := range addrs {
ip, _, err := net.ParseCIDR(a.String())
if err != nil {
return err
}
// If not Global Unicast, bypass
if !ip.IsGlobalUnicast() {
continue
}
// TODO (QDC): Clearly not optimal
found := false
lookingLoop:
for _, locAddr := range pcm.localAddrs {
if ip.Equal(locAddr.IP) {
found = true
break lookingLoop
}
}
if !found {
locAddr, err := pcm.createPconn(ip)
if err != nil {
return err
}
pcm.localAddrs = append(pcm.localAddrs, *locAddr)
}
}
}
return nil
}
func (pcm *pconnManager) closePconns() {
for _, pconn := range pcm.pconns {
pconn.Close()
}
pcm.pconnAny.Close()
close(pcm.closed)
}