quic_misc.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // +build go1.12
  7. package connections
  8. import (
  9. "net"
  10. "github.com/lucas-clemente/quic-go"
  11. )
  12. var (
  13. quicConfig = &quic.Config{
  14. ConnectionIDLength: 4,
  15. KeepAlive: true,
  16. }
  17. )
  18. type quicTlsConn struct {
  19. quic.Session
  20. quic.Stream
  21. // If we created this connection, we should be the ones closing it.
  22. createdConn net.PacketConn
  23. }
  24. func (q *quicTlsConn) Close() error {
  25. sterr := q.Stream.Close()
  26. seerr := q.Session.Close()
  27. var pcerr error
  28. if q.createdConn != nil {
  29. pcerr = q.createdConn.Close()
  30. }
  31. if sterr != nil {
  32. return sterr
  33. }
  34. if seerr != nil {
  35. return seerr
  36. }
  37. return pcerr
  38. }
  39. // Sort available packet connections by ip address, preferring unspecified local address.
  40. func packetConnLess(i interface{}, j interface{}) bool {
  41. iIsUnspecified := false
  42. jIsUnspecified := false
  43. iLocalAddr := i.(net.PacketConn).LocalAddr()
  44. jLocalAddr := j.(net.PacketConn).LocalAddr()
  45. if host, _, err := net.SplitHostPort(iLocalAddr.String()); err == nil {
  46. iIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  47. }
  48. if host, _, err := net.SplitHostPort(jLocalAddr.String()); err == nil {
  49. jIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  50. }
  51. if jIsUnspecified == iIsUnspecified {
  52. return len(iLocalAddr.Network()) < len(jLocalAddr.Network())
  53. }
  54. return iIsUnspecified
  55. }