-
Notifications
You must be signed in to change notification settings - Fork 23
/
conn.go
657 lines (610 loc) · 13.8 KB
/
conn.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package irtt
import (
"bytes"
"context"
"net"
"sort"
"strings"
"time"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
// nconn (network conn) is the embedded struct in conn and lconn connections. It
// adds IPVersion, socket options and some helpers to net.UDPConn.
type nconn struct {
conn *net.UDPConn
ipVer IPVersion
ip4conn *ipv4.PacketConn
ip6conn *ipv6.PacketConn
dscp int
dscpError error
dscpSupport bool
ttl int
df DF
timeSource TimeSource
}
func (n *nconn) init(conn *net.UDPConn, ipVer IPVersion, ts TimeSource) {
n.conn = conn
n.ipVer = ipVer
n.df = DFDefault
n.timeSource = ts
// create x/net conns for socket options
if n.ipVer&IPv4 != 0 {
n.ip4conn = ipv4.NewPacketConn(n.conn)
n.dscpError = n.ip4conn.SetTOS(1)
n.ip4conn.SetTOS(0)
} else {
n.ip6conn = ipv6.NewPacketConn(n.conn)
n.dscpError = n.ip6conn.SetTrafficClass(1)
n.ip6conn.SetTrafficClass(0)
n.ip6conn.SetControlMessage(ipv6.FlagTrafficClass, true)
}
n.dscpSupport = (n.dscpError == nil)
}
func (n *nconn) setDSCP(dscp int) (err error) {
if n.dscp == dscp {
return
}
if n.ip4conn != nil {
err = n.ip4conn.SetTOS(dscp)
} else {
err = n.ip6conn.SetTrafficClass(dscp)
}
if err == nil {
n.dscp = dscp
}
return
}
func (n *nconn) setTTL(ttl int) (err error) {
if n.ttl == ttl {
return
}
if n.ip4conn != nil {
err = n.ip4conn.SetTTL(ttl)
} else {
err = n.ip6conn.SetHopLimit(ttl)
}
if err == nil {
n.ttl = ttl
}
return
}
func (n *nconn) setReceiveDstAddr(b bool) (err error) {
if n.ip4conn != nil {
err = n.ip4conn.SetControlMessage(ipv4.FlagDst, b)
} else {
err = n.ip6conn.SetControlMessage(ipv6.FlagDst, b)
}
return
}
func (n *nconn) setDF(df DF) (err error) {
if n.df == df {
return
}
err = setSockoptDF(n.conn, df)
if err == nil {
n.df = df
}
return
}
func (n *nconn) localAddr() *net.UDPAddr {
if n.conn == nil {
return nil
}
a := n.conn.LocalAddr()
if a == nil {
return nil
}
return a.(*net.UDPAddr)
}
func (n *nconn) close() error {
return n.conn.Close()
}
// cconn is used for client connections
type cconn struct {
*nconn
cfg *ClientConfig
ctoken ctoken
}
func dial(ctx context.Context, cfg *ClientConfig) (cc *cconn, err error) {
// resolve (could support trying multiple addresses in succession)
cfg.LocalAddress = addPort(cfg.LocalAddress, DefaultLocalPort)
laddr, err := net.ResolveUDPAddr(cfg.IPVersion.udpNetwork(),
cfg.LocalAddress)
if err != nil {
return
}
// add default port, if necessary, and resolve server
cfg.RemoteAddress = addPort(cfg.RemoteAddress, DefaultPort)
raddr, err := net.ResolveUDPAddr(cfg.IPVersion.udpNetwork(),
cfg.RemoteAddress)
if err != nil {
return
}
// dial, using explicit network from remote address
cfg.IPVersion = IPVersionFromUDPAddr(raddr)
conn, err := net.DialUDP(cfg.IPVersion.udpNetwork(), laddr, raddr)
if err != nil {
return
}
// set resolved local and remote addresses back to Config
cfg.LocalAddr = conn.LocalAddr()
cfg.RemoteAddr = conn.RemoteAddr()
cfg.LocalAddress = cfg.LocalAddr.String()
cfg.RemoteAddress = cfg.RemoteAddr.String()
// create cconn
cc = &cconn{nconn: &nconn{}, cfg: cfg}
cc.init(conn, cfg.IPVersion, cfg.TimeSource)
// open connection to server
err = cc.open(ctx)
if isErrorCode(ServerClosed, err) {
cc = nil
err = nil
return
}
return
}
func (c *cconn) open(ctx context.Context) (err error) {
// validate open timeouts
for _, to := range c.cfg.OpenTimeouts {
if to < minOpenTimeout {
err = Errorf(OpenTimeoutTooShort,
"open timeout %s must be >= %s", to, minOpenTimeout)
return
}
}
errC := make(chan error)
params := &c.cfg.Params
// start receiving open replies and drop anything else
go func() {
var rerr error
defer func() {
errC <- rerr
}()
orp := newPacket(0, maxHeaderLen, c.cfg.HMACKey)
for {
if rerr = c.receive(orp); rerr != nil && !isErrorCode(ServerClosed, rerr) {
return
}
if orp.flags()&flOpen == 0 {
continue
}
if rerr = orp.addFields(fopenReply, false); rerr != nil {
return
}
if orp.flags()&flClose == 0 && orp.ctoken() == 0 {
rerr = Errorf(ConnTokenZero, "received invalid zero conn token")
return
}
var sp *Params
sp, rerr = parseParams(orp.payload())
if rerr != nil {
return
}
*params = *sp
c.ctoken = orp.ctoken()
if orp.flags()&flClose != 0 {
c.close()
}
return
}
}()
// start sending open requests
sp := newPacket(0, maxHeaderLen, c.cfg.HMACKey)
defer func() {
if err != nil {
c.close()
}
}()
sp.setFlagBits(flOpen)
if c.cfg.NoTest {
sp.setFlagBits(flClose)
}
sp.setPayload(params.bytes())
sp.updateHMAC()
var received bool
for _, to := range c.cfg.OpenTimeouts {
err = c.send(sp)
if err != nil {
return
}
select {
case <-time.After(to):
case err = <-errC:
received = true
return
case <-ctx.Done():
err = ctx.Err()
return
}
}
if !received {
defer c.nconn.close()
err = Errorf(OpenTimeout, "no reply from server")
}
return
}
func (c *cconn) send(p *packet) (err error) {
if err = c.setDSCP(p.dscp); err != nil {
return
}
var n int
n, err = c.conn.Write(p.bytes())
p.tsent = c.timeSource.Now(BothClocks)
p.trcvd = Time{}
if err != nil {
return
}
if n < p.length() {
err = Errorf(ShortWrite, "only %d/%d bytes were sent", n, p.length())
}
return
}
func (c *cconn) receive(p *packet) (err error) {
var n int
if c.ip6conn != nil {
var cm *ipv6.ControlMessage
n, cm, _, err = c.ip6conn.ReadFrom(p.readTo())
if cm != nil {
//p.dscp = cm.TrafficClass >> 2
p.ecn = cm.TrafficClass & 0x3
}
} else {
n, err = c.conn.Read(p.readTo())
}
p.trcvd = c.timeSource.Now(BothClocks)
p.tsent = Time{}
p.dscp = 0
if err != nil {
return
}
if err = p.readReset(n); err != nil {
return
}
if !p.reply() {
err = Errorf(ExpectedReplyFlag, "reply flag not set")
return
}
if p.flags()&flClose != 0 {
err = Errorf(ServerClosed, "server closed connection")
c.close()
}
return
}
func (c *cconn) newPacket() *packet {
p := newPacket(0, c.cfg.Length, c.cfg.HMACKey)
p.setConnToken(c.ctoken)
p.raddr = c.conn.RemoteAddr().(*net.UDPAddr)
return p
}
func (c *cconn) remoteAddr() *net.UDPAddr {
if c.conn == nil {
return nil
}
a := c.conn.RemoteAddr()
if a == nil {
return nil
}
return a.(*net.UDPAddr)
}
func (c *cconn) close() (err error) {
defer func() {
err = c.nconn.close()
}()
// send one close packet if necessary
if c.ctoken != 0 {
cp := newPacket(0, maxHeaderLen, c.cfg.HMACKey)
if err = cp.setFields(fcloseRequest, true); err != nil {
return
}
cp.setFlagBits(flClose)
cp.setConnToken(c.ctoken)
cp.updateHMAC()
err = c.send(cp)
}
return
}
// lconn is used for server listeners
type lconn struct {
*nconn
cm4 ipv4.ControlMessage
cm6 ipv6.ControlMessage
setSrcIP bool
}
// listen creates an lconn by listening on a UDP address.
func listen(laddr *net.UDPAddr, setSrcIP bool, ts TimeSource) (l *lconn, err error) {
ipVer := IPVersionFromUDPAddr(laddr)
var conn *net.UDPConn
if conn, err = net.ListenUDP(ipVer.udpNetwork(), laddr); err != nil {
return
}
l = &lconn{nconn: &nconn{}, setSrcIP: setSrcIP && laddr.IP.IsUnspecified()}
l.init(conn, ipVer, ts)
return
}
// listenAll creates lconns on multiple addresses, with separate lconns for IPv4
// and IPv6, so that socket options can be set correctly, which is not possible
// with a dual stack conn.
func listenAll(ipVer IPVersion, addrs []string, setSrcIP bool,
ts TimeSource) (lconns []*lconn, err error) {
laddrs, err := resolveListenAddrs(addrs, ipVer)
if err != nil {
return
}
lconns = make([]*lconn, 0, 16)
for _, laddr := range laddrs {
var l *lconn
l, err = listen(laddr, setSrcIP, ts)
if err != nil {
return
}
lconns = append(lconns, l)
}
if len(lconns) == 0 {
err = Errorf(NoSuitableAddressFound, "no suitable %s address found", ipVer)
return
}
return
}
func (l *lconn) send(p *packet) (err error) {
p.updateHMAC()
if err = l.setDSCP(p.dscp); err != nil {
return
}
var n int
if !l.setSrcIP {
n, err = l.conn.WriteToUDP(p.bytes(), p.raddr)
} else if l.ip4conn != nil {
l.cm4.Src = p.srcIP
n, err = l.ip4conn.WriteTo(p.bytes(), &l.cm4, p.raddr)
} else {
l.cm6.Src = p.srcIP
n, err = l.ip6conn.WriteTo(p.bytes(), &l.cm6, p.raddr)
}
p.tsent = l.timeSource.Now(BothClocks)
p.trcvd = Time{}
if err != nil {
return
}
if n < p.length() {
err = Errorf(ShortWrite, "only %d/%d bytes were sent", n, p.length())
}
return
}
func (l *lconn) receive(p *packet) (err error) {
var n int
p.ecn = 0
if !l.setSrcIP {
n, p.raddr, err = l.conn.ReadFromUDP(p.readTo())
p.dstIP = nil
} else if l.ip4conn != nil {
var cm *ipv4.ControlMessage
var src net.Addr
n, cm, src, err = l.ip4conn.ReadFrom(p.readTo())
if src != nil {
p.raddr = src.(*net.UDPAddr)
}
if cm != nil {
p.dstIP = cm.Dst
} else {
p.dstIP = nil
}
} else {
var cm *ipv6.ControlMessage
var src net.Addr
n, cm, src, err = l.ip6conn.ReadFrom(p.readTo())
if src != nil {
p.raddr = src.(*net.UDPAddr)
}
if cm != nil {
p.dstIP = cm.Dst
//p.dscp = cm.TrafficClass >> 2
p.ecn = cm.TrafficClass // For test purposes
} else {
p.dstIP = nil
}
}
p.srcIP = nil
p.dscp = 0
p.trcvd = l.timeSource.Now(BothClocks)
p.tsent = Time{}
if err != nil {
return
}
if err = p.readReset(n); err != nil {
return
}
if p.reply() {
err = Errorf(UnexpectedReplyFlag, "unexpected reply flag set")
return
}
return
}
// parseIfaceListenAddr parses an interface listen address into an interface
// name and service. ok is false if the string does not use the syntax
// %iface:service, where :service is optional.
func parseIfaceListenAddr(addr string) (iface, service string, ok bool) {
if !strings.HasPrefix(addr, "%") {
return
}
parts := strings.Split(addr[1:], ":")
switch len(parts) {
case 2:
service = parts[1]
if len(service) == 0 {
return
}
fallthrough
case 1:
iface = parts[0]
if len(iface) == 0 {
return
}
ok = true
return
}
return
}
// resolveIfaceListenAddr resolves an interface name and service (port name
// or number) into a slice of UDP addresses.
func resolveIfaceListenAddr(ifaceName string, service string,
ipVer IPVersion) (laddrs []*net.UDPAddr, err error) {
// get interfaces
var ifaces []net.Interface
ifaces, err = net.Interfaces()
if err != nil {
return
}
// resolve service to port
var port int
if service != "" {
port, err = net.LookupPort(ipVer.udpNetwork(), service)
if err != nil {
return
}
} else {
port = DefaultPortInt
}
// helper to get IP and zone from interface address
ifaceIP := func(a net.Addr) (ip net.IP, zone string, ok bool) {
switch v := a.(type) {
case *net.IPNet:
{
ip = v.IP
ok = true
}
case *net.IPAddr:
{
ip = v.IP
zone = v.Zone
ok = true
}
}
return
}
// helper to test if IP is one we can listen on
isUsableIP := func(ip net.IP) bool {
if IPVersionFromIP(ip)&ipVer == 0 {
return false
}
if !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() && !ip.IsLoopback() {
return false
}
return true
}
// get addresses
laddrs = make([]*net.UDPAddr, 0, 16)
ifaceFound := false
ifaceUp := false
for _, iface := range ifaces {
if !glob(ifaceName, iface.Name) {
continue
}
ifaceFound = true
if iface.Flags&net.FlagUp == 0 {
continue
}
ifaceUp = true
ifaceAddrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, a := range ifaceAddrs {
ip, zone, ok := ifaceIP(a)
if ok && isUsableIP(ip) {
if ip.IsLinkLocalUnicast() && zone == "" {
zone = iface.Name
}
udpAddr := &net.UDPAddr{IP: ip, Port: port, Zone: zone}
laddrs = append(laddrs, udpAddr)
}
}
}
if !ifaceFound {
err = Errorf(NoMatchingInterfaces, "%s does not match any interfaces", ifaceName)
} else if !ifaceUp {
err = Errorf(NoMatchingInterfacesUp, "no interfaces matching %s are up", ifaceName)
}
return
}
// resolveListenAddr resolves a listen address string into a slice of UDP
// addresses.
func resolveListenAddr(addr string, ipVer IPVersion) (laddrs []*net.UDPAddr,
err error) {
laddrs = make([]*net.UDPAddr, 0, 2)
for _, v := range ipVer.Separate() {
addr = addPort(addr, DefaultPort)
laddr, err := net.ResolveUDPAddr(v.udpNetwork(), addr)
if err != nil {
continue
}
if laddr.IP == nil {
laddr.IP = v.ZeroIP()
}
laddrs = append(laddrs, laddr)
}
return
}
// resolveListenAddrs resolves a slice of listen address strings into a slice
// of UDP addresses.
func resolveListenAddrs(addrs []string, ipVer IPVersion) (laddrs []*net.UDPAddr,
err error) {
// resolve addresses
laddrs = make([]*net.UDPAddr, 0, 16)
for _, addr := range addrs {
var la []*net.UDPAddr
iface, service, ok := parseIfaceListenAddr(addr)
if ok {
la, err = resolveIfaceListenAddr(iface, service, ipVer)
} else {
la, err = resolveListenAddr(addr, ipVer)
}
if err != nil {
return
}
laddrs = append(laddrs, la...)
}
// sort addresses
sort.Slice(laddrs, func(i, j int) bool {
if bytes.Compare(laddrs[i].IP, laddrs[j].IP) < 0 {
return true
}
if laddrs[i].Port < laddrs[j].Port {
return true
}
return laddrs[i].Zone < laddrs[j].Zone
})
// remove duplicates
udpAddrsEqual := func(a *net.UDPAddr, b *net.UDPAddr) bool {
if !a.IP.Equal(b.IP) {
return false
}
if a.Port != b.Port {
return false
}
return a.Zone == b.Zone
}
for i := 1; i < len(laddrs); i++ {
if udpAddrsEqual(laddrs[i], laddrs[i-1]) {
laddrs = append(laddrs[:i], laddrs[i+1:]...)
i--
}
}
// check for combination of specified and unspecified IP addresses
m := make(map[int]int)
for _, la := range laddrs {
if la.IP.IsUnspecified() {
m[la.Port] = m[la.Port] | 1
} else {
m[la.Port] = m[la.Port] | 2
}
}
for k, v := range m {
if v > 2 {
err = Errorf(UnspecifiedWithSpecifiedAddresses,
"invalid combination of unspecified and specified IP addresses port %d", k)
break
}
}
return
}