syncthing/lib/connections/quic_misc.go
greatroar 7c292cc812
lib/connections: Fix and optimize registry (#7996)
Registry.Get used a full sort to get the minimum of a list, and the sort
was broken because util.AddressUnspecifiedLess assumed it could find out
whether an address is IPv4 or IPv6 from its Network method. However,
net.(TCP|UDP)Addr.Network always returns "tcp"/"udp".
2021-10-06 10:52:51 +02:00

72 lines
1.5 KiB
Go

// 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 go1.15 && !noquic
// +build go1.15,!noquic
package connections
import (
"crypto/tls"
"net"
"net/url"
"github.com/lucas-clemente/quic-go"
)
var (
quicConfig = &quic.Config{
ConnectionIDLength: 4,
KeepAlive: true,
}
)
func quicNetwork(uri *url.URL) string {
switch uri.Scheme {
case "quic4":
return "udp4"
case "quic6":
return "udp6"
default:
return "udp"
}
}
type quicTlsConn struct {
quic.Session
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.Session.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.Session.ConnectionState().TLS.ConnectionState
}
func packetConnUnspecified(conn interface{}) bool {
// Since QUIC connections are wrapped, we can't do a simple typecheck
// on *net.UDPAddr here.
addr := conn.(net.PacketConn).LocalAddr()
host, _, err := net.SplitHostPort(addr.String())
return err == nil && net.ParseIP(host).IsUnspecified()
}