UDPConn.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ipAddrs, err := LookupIP(ctx, host, config)
  51. if err != nil {
  52. return nil, nil, errors.Trace(err)
  53. }
  54. if len(ipAddrs) < 1 {
  55. return nil, nil, errors.TraceNew("no IP address")
  56. }
  57. ipAddr := ipAddrs[rand.Intn(len(ipAddrs))]
  58. if config.IPv6Synthesizer != nil {
  59. if ipAddr.To4() != nil {
  60. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(ipAddr.String())
  61. if synthesizedIPAddress != "" {
  62. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  63. if synthesizedAddr != nil {
  64. ipAddr = synthesizedAddr
  65. }
  66. }
  67. }
  68. }
  69. var domain int
  70. if ipAddr != nil && ipAddr.To4() != nil {
  71. domain = syscall.AF_INET
  72. } else if ipAddr != nil && ipAddr.To16() != nil {
  73. domain = syscall.AF_INET6
  74. } else {
  75. return nil, nil, errors.Tracef("invalid IP address: %s", ipAddr.String())
  76. }
  77. conn, err := newUDPConn(domain, config)
  78. if err != nil {
  79. return nil, nil, errors.Trace(err)
  80. }
  81. if config.ResolvedIPCallback != nil {
  82. config.ResolvedIPCallback(ipAddr.String())
  83. }
  84. return conn, &net.UDPAddr{IP: ipAddr, Port: port}, nil
  85. }