quic_misc.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. //go:build !noquic
  7. // +build !noquic
  8. package connections
  9. import (
  10. "context"
  11. "crypto/tls"
  12. "net"
  13. "net/url"
  14. "sync/atomic"
  15. "time"
  16. "github.com/quic-go/quic-go"
  17. "github.com/syncthing/syncthing/lib/osutil"
  18. )
  19. var quicConfig = &quic.Config{
  20. MaxIdleTimeout: 30 * time.Second,
  21. KeepAlivePeriod: 15 * time.Second,
  22. }
  23. func quicNetwork(uri *url.URL) string {
  24. switch uri.Scheme {
  25. case "quic4":
  26. return "udp4"
  27. case "quic6":
  28. return "udp6"
  29. default:
  30. return "udp"
  31. }
  32. }
  33. type quicTlsConn struct {
  34. *quic.Conn
  35. *quic.Stream
  36. // If we created this connection, we should be the ones closing it.
  37. createdConn net.PacketConn
  38. }
  39. func (q *quicTlsConn) Close() error {
  40. sterr := q.Stream.Close()
  41. seerr := q.Conn.CloseWithError(0, "closing")
  42. var pcerr error
  43. if q.createdConn != nil {
  44. pcerr = q.createdConn.Close()
  45. }
  46. if sterr != nil {
  47. return sterr
  48. }
  49. if seerr != nil {
  50. return seerr
  51. }
  52. return pcerr
  53. }
  54. func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
  55. return q.Conn.ConnectionState().TLS
  56. }
  57. func transportConnUnspecified(conn any) bool {
  58. tran, ok := conn.(*quic.Transport)
  59. if !ok {
  60. return false
  61. }
  62. addr := tran.Conn.LocalAddr()
  63. ip, err := osutil.IPFromAddr(addr)
  64. return err == nil && ip.IsUnspecified()
  65. }
  66. // A transportPacketConn is a net.PacketConn that uses a quic.Transport.
  67. type transportPacketConn struct {
  68. tran *quic.Transport
  69. readDeadline atomic.Value // time.Time
  70. }
  71. func (t *transportPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
  72. ctx := context.Background()
  73. if deadline, ok := t.readDeadline.Load().(time.Time); ok && !deadline.IsZero() {
  74. var cancel context.CancelFunc
  75. ctx, cancel = context.WithDeadline(ctx, deadline)
  76. defer cancel()
  77. }
  78. return t.tran.ReadNonQUICPacket(ctx, p)
  79. }
  80. func (t *transportPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
  81. return t.tran.WriteTo(p, addr)
  82. }
  83. func (*transportPacketConn) Close() error {
  84. return errUnsupported
  85. }
  86. func (t *transportPacketConn) LocalAddr() net.Addr {
  87. return t.tran.Conn.LocalAddr()
  88. }
  89. func (t *transportPacketConn) SetDeadline(deadline time.Time) error {
  90. return t.SetReadDeadline(deadline)
  91. }
  92. func (t *transportPacketConn) SetReadDeadline(deadline time.Time) error {
  93. t.readDeadline.Store(deadline)
  94. return nil
  95. }
  96. func (*transportPacketConn) SetWriteDeadline(_ time.Time) error {
  97. return nil // yolo
  98. }