UDPConn.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "context"
  22. "math/rand"
  23. "net"
  24. "strconv"
  25. "syscall"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  27. )
  28. // NewUDPConn resolves addr and configures a new UDP conn. The UDP socket is
  29. // created using options in DialConfig, including DeviceBinder. The returned
  30. // UDPAddr uses DialConfig options IPv6Synthesizer and ResolvedIPCallback.
  31. //
  32. // The UDP conn is not dialed; it is intended for use with WriteTo using the
  33. // returned UDPAddr, not Write.
  34. //
  35. // The returned conn is not a Closer; the caller is expected to wrap this conn
  36. // with another higher-level conn that provides that interface.
  37. func NewUDPConn(
  38. ctx context.Context, addr string, config *DialConfig) (net.PacketConn, *net.UDPAddr, error) {
  39. host, strPort, err := net.SplitHostPort(addr)
  40. if err != nil {
  41. return nil, nil, errors.Trace(err)
  42. }
  43. port, err := strconv.Atoi(strPort)
  44. if err != nil {
  45. return nil, nil, errors.Trace(err)
  46. }
  47. if port <= 0 || port >= 65536 {
  48. return nil, nil, errors.Tracef("invalid destination port: %d", port)
  49. }
  50. if config.ResolveIP == nil {
  51. // Fail even if we don't need a resolver for this dial: this is a code
  52. // misconfiguration.
  53. return nil, nil, errors.TraceNew("missing resolver")
  54. }
  55. ipAddrs, err := config.ResolveIP(ctx, host)
  56. if err != nil {
  57. return nil, nil, errors.Trace(err)
  58. }
  59. if len(ipAddrs) < 1 {
  60. return nil, nil, errors.TraceNew("no IP address")
  61. }
  62. ipAddr := ipAddrs[rand.Intn(len(ipAddrs))]
  63. if config.IPv6Synthesizer != nil {
  64. if ipAddr.To4() != nil {
  65. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(ipAddr.String())
  66. if synthesizedIPAddress != "" {
  67. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  68. if synthesizedAddr != nil {
  69. ipAddr = synthesizedAddr
  70. }
  71. }
  72. }
  73. }
  74. var domain int
  75. if ipAddr != nil && ipAddr.To4() != nil {
  76. domain = syscall.AF_INET
  77. } else if ipAddr != nil && ipAddr.To16() != nil {
  78. domain = syscall.AF_INET6
  79. } else {
  80. return nil, nil, errors.Tracef("invalid IP address: %s", ipAddr.String())
  81. }
  82. conn, err := newUDPConn(domain, config)
  83. if err != nil {
  84. return nil, nil, errors.Trace(err)
  85. }
  86. if config.ResolvedIPCallback != nil {
  87. config.ResolvedIPCallback(ipAddr.String())
  88. }
  89. return conn, &net.UDPAddr{IP: ipAddr, Port: port}, nil
  90. }