TCPConn_windows.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // +build windows
  2. /*
  3. * Copyright (c) 2014, 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. "net"
  24. )
  25. // interruptibleTCPSocket simulates interruptible semantics on Windows. A call
  26. // to interruptibleTCPClose doesn't actually interrupt a connect in progress,
  27. // but abandons a dial that's running in a goroutine.
  28. // Interruptible semantics are required by the controller for timely component
  29. // state changes.
  30. // TODO: implement true interruptible semantics on Windows; use syscall and
  31. // a HANDLE similar to how TCPConn_unix uses a file descriptor?
  32. type interruptibleTCPSocket struct {
  33. results chan *interruptibleDialResult
  34. }
  35. type interruptibleDialResult struct {
  36. netConn net.Conn
  37. err error
  38. }
  39. func interruptibleTCPDial(addr string, config *DialConfig) (conn *TCPConn, err error) {
  40. if config.BindToDeviceProvider != nil {
  41. return nil, ContextError(errors.New("psiphon.interruptibleTCPDial with bind not supported on Windows"))
  42. }
  43. conn = &TCPConn{
  44. interruptible: interruptibleTCPSocket{results: make(chan *interruptibleDialResult, 2)},
  45. readTimeout: config.ReadTimeout,
  46. writeTimeout: config.WriteTimeout}
  47. config.PendingConns.Add(conn)
  48. // Call the blocking Dial in a goroutine
  49. results := conn.interruptible.results
  50. go func() {
  51. // When using an upstream HTTP proxy, first connect to the proxy,
  52. // then use HTTP CONNECT to connect to the original destination.
  53. dialAddr := addr
  54. if config.UpstreamHttpProxyAddress != "" {
  55. dialAddr = config.UpstreamHttpProxyAddress
  56. }
  57. netConn, err := net.DialTimeout("tcp", dialAddr, config.ConnectTimeout)
  58. if config.UpstreamHttpProxyAddress != "" {
  59. err := HttpProxyConnect(netConn, addr)
  60. if err != nil {
  61. netConn = nil
  62. }
  63. }
  64. results <- &interruptibleDialResult{netConn, err}
  65. }()
  66. // Block until Dial completes (or times out) or until interrupt
  67. result := <-conn.interruptible.results
  68. config.PendingConns.Remove(conn)
  69. if result.err != nil {
  70. return nil, ContextError(result.err)
  71. }
  72. conn.Conn = result.netConn
  73. return conn, nil
  74. }
  75. func interruptibleTCPClose(interruptible interruptibleTCPSocket) error {
  76. interruptible.results <- &interruptibleDialResult{nil, errors.New("socket interrupted")}
  77. return nil
  78. }