TCPConn_bind.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. )
  30. // tcpDial is the platform-specific part of interruptibleTCPDial
  31. //
  32. // To implement socket device binding, the lower-level syscall APIs are used.
  33. // The sequence of syscalls in this implementation are taken from:
  34. // https://code.google.com/p/go/issues/detail?id=6966
  35. func tcpDial(addr string, config *DialConfig, dialResult chan error) (net.Conn, error) {
  36. // Like interruption, this timeout doesn't stop this connection goroutine,
  37. // it just unblocks the calling interruptibleTCPDial.
  38. if config.ConnectTimeout != 0 {
  39. time.AfterFunc(config.ConnectTimeout, func() {
  40. select {
  41. case dialResult <- errors.New("connect timeout"):
  42. default:
  43. }
  44. })
  45. }
  46. // Get the remote IP and port, resolving a domain name if necessary
  47. host, strPort, err := net.SplitHostPort(addr)
  48. if err != nil {
  49. return nil, ContextError(err)
  50. }
  51. port, err := strconv.Atoi(strPort)
  52. if err != nil {
  53. return nil, ContextError(err)
  54. }
  55. ipAddrs, err := LookupIP(host, config)
  56. if err != nil {
  57. return nil, ContextError(err)
  58. }
  59. if len(ipAddrs) < 1 {
  60. return nil, ContextError(errors.New("no IP address"))
  61. }
  62. // Select an IP at random from the list, so we're not always
  63. // trying the same IP (when > 1) which may be blocked.
  64. // TODO: retry all IPs until one connects? For now, this retry
  65. // will happen on subsequent TCPDial calls, when a different IP
  66. // is selected.
  67. index, err := MakeSecureRandomInt(len(ipAddrs))
  68. if err != nil {
  69. return nil, ContextError(err)
  70. }
  71. // TODO: IPv6 support
  72. var ip [4]byte
  73. copy(ip[:], ipAddrs[index].To4())
  74. // Create a socket and bind to device, when configured to do so
  75. socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
  76. if err != nil {
  77. return nil, ContextError(err)
  78. }
  79. if config.DeviceBinder != nil {
  80. // WARNING: this potentially violates the direction to not call into
  81. // external components after the Controller may have been stopped.
  82. // TODO: rework DeviceBinder as an internal 'service' which can trap
  83. // external calls when they should not be made?
  84. err = config.DeviceBinder.BindToDevice(socketFd)
  85. if err != nil {
  86. syscall.Close(socketFd)
  87. return nil, ContextError(fmt.Errorf("BindToDevice failed: %s", err))
  88. }
  89. }
  90. sockAddr := syscall.SockaddrInet4{Addr: ip, Port: port}
  91. err = syscall.Connect(socketFd, &sockAddr)
  92. if err != nil {
  93. syscall.Close(socketFd)
  94. return nil, ContextError(err)
  95. }
  96. // Convert the socket fd to a net.Conn
  97. file := os.NewFile(uintptr(socketFd), "")
  98. netConn, err := net.FileConn(file) // net.FileConn() dups socketFd
  99. file.Close() // file.Close() closes socketFd
  100. if err != nil {
  101. return nil, ContextError(err)
  102. }
  103. return netConn, nil
  104. }