TCPConn_windows.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. netConn, err := net.DialTimeout("tcp", addr, config.ConnectTimeout)
  58. results <- &interruptibleDialResult{netConn, err}
  59. }()
  60. // Block until Dial completes (or times out) or until interrupt
  61. result := <-conn.interruptible.results
  62. if result.err != nil {
  63. return nil, ContextError(result.err)
  64. }
  65. conn.Conn = result.netConn
  66. return conn, nil
  67. }
  68. func interruptibleTCPClose(interruptible interruptibleTCPSocket) error {
  69. interruptible.results <- &interruptibleDialResult{nil, errors.New("socket interrupted")}
  70. return nil
  71. }