TCPConn_bind.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build !windows
  2. /*
  3. * Copyright (c) 2015, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package psiphon
  21. import (
  22. "errors"
  23. "fmt"
  24. "net"
  25. "os"
  26. "strconv"
  27. "syscall"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. )
  31. // tcpDial is the platform-specific part of interruptibleTCPDial
  32. //
  33. // To implement socket device binding, the lower-level syscall APIs are used.
  34. // The sequence of syscalls in this implementation are taken from:
  35. // https://code.google.com/p/go/issues/detail?id=6966
  36. func tcpDial(addr string, config *DialConfig, dialResult chan error) (net.Conn, error) {
  37. // Like interruption, this timeout doesn't stop this connection goroutine,
  38. // it just unblocks the calling interruptibleTCPDial.
  39. if config.ConnectTimeout != 0 {
  40. time.AfterFunc(config.ConnectTimeout, func() {
  41. select {
  42. case dialResult <- errors.New("connect timeout"):
  43. default:
  44. }
  45. })
  46. }
  47. // Get the remote IP and port, resolving a domain name if necessary
  48. host, strPort, err := net.SplitHostPort(addr)
  49. if err != nil {
  50. return nil, common.ContextError(err)
  51. }
  52. port, err := strconv.Atoi(strPort)
  53. if err != nil {
  54. return nil, common.ContextError(err)
  55. }
  56. ipAddrs, err := LookupIP(host, config)
  57. if err != nil {
  58. return nil, common.ContextError(err)
  59. }
  60. if len(ipAddrs) < 1 {
  61. return nil, common.ContextError(errors.New("no IP address"))
  62. }
  63. // Select an IP at random from the list, so we're not always
  64. // trying the same IP (when > 1) which may be blocked.
  65. // TODO: retry all IPs until one connects? For now, this retry
  66. // will happen on subsequent TCPDial calls, when a different IP
  67. // is selected.
  68. index, err := common.MakeSecureRandomInt(len(ipAddrs))
  69. if err != nil {
  70. return nil, common.ContextError(err)
  71. }
  72. // TODO: IPv6 support
  73. var ip [4]byte
  74. copy(ip[:], ipAddrs[index].To4())
  75. // Create a socket and bind to device, when configured to do so
  76. socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
  77. if err != nil {
  78. return nil, common.ContextError(err)
  79. }
  80. if config.DeviceBinder != nil {
  81. // WARNING: this potentially violates the direction to not call into
  82. // external components after the Controller may have been stopped.
  83. // TODO: rework DeviceBinder as an internal 'service' which can trap
  84. // external calls when they should not be made?
  85. err = config.DeviceBinder.BindToDevice(socketFd)
  86. if err != nil {
  87. syscall.Close(socketFd)
  88. return nil, common.ContextError(fmt.Errorf("BindToDevice failed: %s", err))
  89. }
  90. }
  91. sockAddr := syscall.SockaddrInet4{Addr: ip, Port: port}
  92. err = syscall.Connect(socketFd, &sockAddr)
  93. if err != nil {
  94. syscall.Close(socketFd)
  95. return nil, common.ContextError(err)
  96. }
  97. // Convert the socket fd to a net.Conn
  98. file := os.NewFile(uintptr(socketFd), "")
  99. netConn, err := net.FileConn(file) // net.FileConn() dups socketFd
  100. file.Close() // file.Close() closes socketFd
  101. if err != nil {
  102. return nil, common.ContextError(err)
  103. }
  104. return netConn, nil
  105. }