aboutsummaryrefslogtreecommitdiff
path: root/lib/connections/quic_misc.go
blob: 1ba4ed1a88bc749e65a2ecf5b3da35a63416c9df (plain)
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
// Copyright (C) 2019 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build !noquic
// +build !noquic

package connections

import (
	"context"
	"crypto/tls"
	"net"
	"net/url"
	"sync/atomic"
	"time"

	"github.com/quic-go/quic-go"
	"github.com/quic-go/quic-go/logging"

	"github.com/syncthing/syncthing/lib/osutil"
)

var quicConfig = &quic.Config{
	MaxIdleTimeout:  30 * time.Second,
	KeepAlivePeriod: 15 * time.Second,
}

func quicNetwork(uri *url.URL) string {
	switch uri.Scheme {
	case "quic4":
		return "udp4"
	case "quic6":
		return "udp6"
	default:
		return "udp"
	}
}

type quicTlsConn struct {
	quic.Connection
	quic.Stream
	// If we created this connection, we should be the ones closing it.
	createdConn net.PacketConn
}

func (q *quicTlsConn) Close() error {
	sterr := q.Stream.Close()
	seerr := q.Connection.CloseWithError(0, "closing")
	var pcerr error
	if q.createdConn != nil {
		pcerr = q.createdConn.Close()
	}
	if sterr != nil {
		return sterr
	}
	if seerr != nil {
		return seerr
	}
	return pcerr
}

func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
	return q.Connection.ConnectionState().TLS
}

func transportConnUnspecified(conn any) bool {
	tran, ok := conn.(*quic.Transport)
	if !ok {
		return false
	}
	addr := tran.Conn.LocalAddr()
	ip, err := osutil.IPFromAddr(addr)
	return err == nil && ip.IsUnspecified()
}

type writeTrackingTracer struct {
	lastWrite atomic.Int64 // unix nanos
}

func (t *writeTrackingTracer) loggingTracer() *logging.Tracer {
	return &logging.Tracer{
		SentPacket: func(net.Addr, *logging.Header, logging.ByteCount, []logging.Frame) {
			t.lastWrite.Store(time.Now().UnixNano())
		},
		SentVersionNegotiationPacket: func(net.Addr, logging.ArbitraryLenConnectionID, logging.ArbitraryLenConnectionID, []logging.VersionNumber) {
			t.lastWrite.Store(time.Now().UnixNano())
		},
	}
}

func (t *writeTrackingTracer) LastWrite() time.Time {
	return time.Unix(0, t.lastWrite.Load())
}

// A transportPacketConn is a net.PacketConn that uses a quic.Transport.
type transportPacketConn struct {
	tran         *quic.Transport
	readDeadline atomic.Value // time.Time
}

func (t *transportPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
	ctx := context.Background()
	if deadline, ok := t.readDeadline.Load().(time.Time); ok && !deadline.IsZero() {
		var cancel context.CancelFunc
		ctx, cancel = context.WithDeadline(ctx, deadline)
		defer cancel()
	}
	return t.tran.ReadNonQUICPacket(ctx, p)
}

func (t *transportPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
	return t.tran.WriteTo(p, addr)
}

func (*transportPacketConn) Close() error {
	return errUnsupported
}

func (t *transportPacketConn) LocalAddr() net.Addr {
	return t.tran.Conn.LocalAddr()
}

func (t *transportPacketConn) SetDeadline(deadline time.Time) error {
	return t.SetReadDeadline(deadline)
}

func (t *transportPacketConn) SetReadDeadline(deadline time.Time) error {
	t.readDeadline.Store(deadline)
	return nil
}

func (*transportPacketConn) SetWriteDeadline(_ time.Time) error {
	return nil // yolo
}