TCPConn_windows.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "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. // interruptibleTCPDial establishes a TCP network connection. A conn is added
  40. // to config.PendingConns before blocking on network IO, which enables interruption.
  41. // The caller is responsible for removing an established conn from PendingConns.
  42. func interruptibleTCPDial(addr string, config *DialConfig) (conn *TCPConn, err error) {
  43. if config.DeviceBinder != nil {
  44. return nil, ContextError(errors.New("psiphon.interruptibleTCPDial with DeviceBinder not supported on Windows"))
  45. }
  46. // Enable interruption
  47. conn = &TCPConn{
  48. interruptible: interruptibleTCPSocket{results: make(chan *interruptibleDialResult, 2)},
  49. readTimeout: config.ReadTimeout,
  50. writeTimeout: config.WriteTimeout}
  51. if !config.PendingConns.Add(conn) {
  52. return nil, ContextError(errors.New("pending connections already closed"))
  53. }
  54. // Call the blocking Dial in a goroutine
  55. results := conn.interruptible.results
  56. go func() {
  57. // When using an upstream HTTP proxy, first connect to the proxy,
  58. // then use HTTP CONNECT to connect to the original destination.
  59. dialAddr := addr
  60. if config.UpstreamHttpProxyAddress != "" {
  61. dialAddr = config.UpstreamHttpProxyAddress
  62. }
  63. netConn, err := net.DialTimeout("tcp", dialAddr, config.ConnectTimeout)
  64. if config.UpstreamHttpProxyAddress != "" {
  65. if err == nil {
  66. err = HttpProxyConnect(netConn, addr)
  67. }
  68. if err != nil {
  69. NoticeUpstreamProxyError(err)
  70. }
  71. }
  72. if err != nil {
  73. netConn = nil
  74. }
  75. results <- &interruptibleDialResult{netConn, err}
  76. }()
  77. // Block until Dial completes (or times out) or until interrupt
  78. result := <-conn.interruptible.results
  79. if result.err != nil {
  80. return nil, ContextError(result.err)
  81. }
  82. conn.Conn = result.netConn
  83. return conn, nil
  84. }
  85. func interruptibleTCPClose(interruptible interruptibleTCPSocket) error {
  86. interruptible.results <- &interruptibleDialResult{nil, errors.New("socket interrupted")}
  87. return nil
  88. }