UDPConn.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "os"
  27. "strconv"
  28. "syscall"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. )
  31. // NewUDPConn resolves addr and configures a new UDP conn. The UDP socket is
  32. // created using options in DialConfig, including DeviceBinder. The returned
  33. // UDPAddr uses DialConfig options IPv6Synthesizer and ResolvedIPCallback.
  34. //
  35. // The UDP conn is not dialed; it is intended for use with WriteTo using the
  36. // returned UDPAddr, not Write.
  37. //
  38. // The returned conn is not a Closer; the caller is expected to wrap this conn
  39. // with another higher-level conn that provides that interface.
  40. func NewUDPConn(
  41. ctx context.Context, addr string, config *DialConfig) (net.PacketConn, *net.UDPAddr, error) {
  42. // TODO: refactor code in common with tcpDial.
  43. host, strPort, err := net.SplitHostPort(addr)
  44. if err != nil {
  45. return nil, nil, common.ContextError(err)
  46. }
  47. port, err := strconv.Atoi(strPort)
  48. if err != nil {
  49. return nil, nil, common.ContextError(err)
  50. }
  51. ipAddrs, err := LookupIP(ctx, host, config)
  52. if err != nil {
  53. return nil, nil, common.ContextError(err)
  54. }
  55. if len(ipAddrs) < 1 {
  56. return nil, nil, common.ContextError(errors.New("no IP address"))
  57. }
  58. ipAddr := ipAddrs[rand.Intn(len(ipAddrs))]
  59. if config.IPv6Synthesizer != nil {
  60. if ipAddr.To4() != nil {
  61. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(ipAddr.String())
  62. if synthesizedIPAddress != "" {
  63. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  64. if synthesizedAddr != nil {
  65. ipAddr = synthesizedAddr
  66. }
  67. }
  68. }
  69. }
  70. var domain int
  71. if ipAddr != nil && ipAddr.To4() != nil {
  72. domain = syscall.AF_INET
  73. } else if ipAddr != nil && ipAddr.To16() != nil {
  74. domain = syscall.AF_INET6
  75. } else {
  76. return nil, nil, common.ContextError(fmt.Errorf("invalid IP address: %s", ipAddr.String()))
  77. }
  78. socketFD, err := syscall.Socket(domain, syscall.SOCK_DGRAM, 0)
  79. if err != nil {
  80. return nil, nil, common.ContextError(err)
  81. }
  82. syscall.CloseOnExec(socketFD)
  83. setAdditionalSocketOptions(socketFD)
  84. if config.DeviceBinder != nil {
  85. err := bindToDeviceCallWrapper(config.DeviceBinder, socketFD)
  86. if err != nil {
  87. syscall.Close(socketFD)
  88. return nil, nil, common.ContextError(fmt.Errorf("BindToDevice failed: %s", err))
  89. }
  90. }
  91. // Convert the socket fd to a net.PacketConn
  92. // This code block is from:
  93. // https://github.com/golang/go/issues/6966
  94. file := os.NewFile(uintptr(socketFD), "")
  95. conn, err := net.FilePacketConn(file) // net.FilePackateConn() dups socketFD
  96. file.Close() // file.Close() closes socketFD
  97. if err != nil {
  98. return nil, nil, common.ContextError(err)
  99. }
  100. if config.ResolvedIPCallback != nil {
  101. config.ResolvedIPCallback(ipAddr.String())
  102. }
  103. return conn, &net.UDPAddr{IP: ipAddr, Port: port}, nil
  104. }