UDPConn.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "errors"
  23. "fmt"
  24. "math/rand"
  25. "net"
  26. "strconv"
  27. "syscall"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  29. )
  30. // NewUDPConn resolves addr and configures a new UDP conn. The UDP socket is
  31. // created using options in DialConfig, including DeviceBinder. The returned
  32. // UDPAddr uses DialConfig options IPv6Synthesizer and ResolvedIPCallback.
  33. //
  34. // The UDP conn is not dialed; it is intended for use with WriteTo using the
  35. // returned UDPAddr, not Write.
  36. //
  37. // The returned conn is not a Closer; the caller is expected to wrap this conn
  38. // with another higher-level conn that provides that interface.
  39. func NewUDPConn(
  40. ctx context.Context, addr string, config *DialConfig) (net.PacketConn, *net.UDPAddr, error) {
  41. host, strPort, err := net.SplitHostPort(addr)
  42. if err != nil {
  43. return nil, nil, common.ContextError(err)
  44. }
  45. port, err := strconv.Atoi(strPort)
  46. if err != nil {
  47. return nil, nil, common.ContextError(err)
  48. }
  49. ipAddrs, err := LookupIP(ctx, host, config)
  50. if err != nil {
  51. return nil, nil, common.ContextError(err)
  52. }
  53. if len(ipAddrs) < 1 {
  54. return nil, nil, common.ContextError(errors.New("no IP address"))
  55. }
  56. ipAddr := ipAddrs[rand.Intn(len(ipAddrs))]
  57. if config.IPv6Synthesizer != nil {
  58. if ipAddr.To4() != nil {
  59. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(ipAddr.String())
  60. if synthesizedIPAddress != "" {
  61. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  62. if synthesizedAddr != nil {
  63. ipAddr = synthesizedAddr
  64. }
  65. }
  66. }
  67. }
  68. var domain int
  69. if ipAddr != nil && ipAddr.To4() != nil {
  70. domain = syscall.AF_INET
  71. } else if ipAddr != nil && ipAddr.To16() != nil {
  72. domain = syscall.AF_INET6
  73. } else {
  74. return nil, nil, common.ContextError(fmt.Errorf("invalid IP address: %s", ipAddr.String()))
  75. }
  76. conn, err := newUDPConn(domain, config)
  77. if err != nil {
  78. return nil, nil, common.ContextError(err)
  79. }
  80. if config.ResolvedIPCallback != nil {
  81. config.ResolvedIPCallback(ipAddr.String())
  82. }
  83. return conn, &net.UDPAddr{IP: ipAddr, Port: port}, nil
  84. }