quic_misc.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 go1.15 && !noquic
  7. // +build go1.15,!noquic
  8. package connections
  9. import (
  10. "crypto/tls"
  11. "net"
  12. "net/url"
  13. "time"
  14. "github.com/quic-go/quic-go"
  15. "github.com/syncthing/syncthing/lib/osutil"
  16. )
  17. var quicConfig = &quic.Config{
  18. ConnectionIDLength: 4,
  19. MaxIdleTimeout: 30 * time.Second,
  20. KeepAlivePeriod: 15 * time.Second,
  21. }
  22. func quicNetwork(uri *url.URL) string {
  23. switch uri.Scheme {
  24. case "quic4":
  25. return "udp4"
  26. case "quic6":
  27. return "udp6"
  28. default:
  29. return "udp"
  30. }
  31. }
  32. type quicTlsConn struct {
  33. quic.Connection
  34. quic.Stream
  35. // If we created this connection, we should be the ones closing it.
  36. createdConn net.PacketConn
  37. }
  38. func (q *quicTlsConn) Close() error {
  39. sterr := q.Stream.Close()
  40. seerr := q.Connection.CloseWithError(0, "closing")
  41. var pcerr error
  42. if q.createdConn != nil {
  43. pcerr = q.createdConn.Close()
  44. }
  45. if sterr != nil {
  46. return sterr
  47. }
  48. if seerr != nil {
  49. return seerr
  50. }
  51. return pcerr
  52. }
  53. func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
  54. return q.Connection.ConnectionState().TLS.ConnectionState
  55. }
  56. func packetConnUnspecified(conn interface{}) bool {
  57. addr := conn.(net.PacketConn).LocalAddr()
  58. ip, err := osutil.IPFromAddr(addr)
  59. return err == nil && ip.IsUnspecified()
  60. }