quic_misc.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }
  22. func (q *quicTlsConn) Close() error {
  23. sterr := q.Stream.Close()
  24. seerr := q.Session.Close()
  25. if sterr != nil {
  26. return sterr
  27. }
  28. return seerr
  29. }
  30. // Sort available packet connections by ip address, preferring unspecified local address.
  31. func packetConnLess(i interface{}, j interface{}) bool {
  32. iIsUnspecified := false
  33. jIsUnspecified := false
  34. iLocalAddr := i.(net.PacketConn).LocalAddr()
  35. jLocalAddr := j.(net.PacketConn).LocalAddr()
  36. if host, _, err := net.SplitHostPort(iLocalAddr.String()); err == nil {
  37. iIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  38. }
  39. if host, _, err := net.SplitHostPort(jLocalAddr.String()); err == nil {
  40. jIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  41. }
  42. if jIsUnspecified == iIsUnspecified {
  43. return len(iLocalAddr.Network()) < len(jLocalAddr.Network())
  44. }
  45. return iIsUnspecified
  46. }