ipc_connecter.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "ipc_connecter.hpp"
  26. #if defined ZMQ_HAVE_IPC
  27. #include <new>
  28. #include <string>
  29. #include "io_thread.hpp"
  30. #include "random.hpp"
  31. #include "err.hpp"
  32. #include "ip.hpp"
  33. #include "address.hpp"
  34. #include "ipc_address.hpp"
  35. #include "session_base.hpp"
  36. #ifdef _MSC_VER
  37. #include <afunix.h>
  38. #else
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #endif
  44. zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
  45. class session_base_t *session_,
  46. const options_t &options_,
  47. address_t *addr_,
  48. bool delayed_start_) :
  49. stream_connecter_base_t (
  50. io_thread_, session_, options_, addr_, delayed_start_)
  51. {
  52. zmq_assert (_addr->protocol == protocol_name::ipc);
  53. }
  54. void zmq::ipc_connecter_t::out_event ()
  55. {
  56. const fd_t fd = connect ();
  57. rm_handle ();
  58. // Handle the error condition by attempt to reconnect.
  59. if (fd == retired_fd) {
  60. close ();
  61. add_reconnect_timer ();
  62. return;
  63. }
  64. create_engine (fd, get_socket_name<ipc_address_t> (fd, socket_end_local));
  65. }
  66. void zmq::ipc_connecter_t::start_connecting ()
  67. {
  68. // Open the connecting socket.
  69. const int rc = open ();
  70. // Connect may succeed in synchronous manner.
  71. if (rc == 0) {
  72. _handle = add_fd (_s);
  73. out_event ();
  74. }
  75. // Connection establishment may be delayed. Poll for its completion.
  76. else if (rc == -1 && errno == EINPROGRESS) {
  77. _handle = add_fd (_s);
  78. set_pollout (_handle);
  79. _socket->event_connect_delayed (
  80. make_unconnected_connect_endpoint_pair (_endpoint), zmq_errno ());
  81. // TODO, tcp_connecter_t adds a connect timer in this case; maybe this
  82. // should be done here as well (and then this could be pulled up to
  83. // stream_connecter_base_t).
  84. }
  85. // Handle any other error condition by eventual reconnect.
  86. else {
  87. if (_s != retired_fd)
  88. close ();
  89. add_reconnect_timer ();
  90. }
  91. }
  92. int zmq::ipc_connecter_t::open ()
  93. {
  94. zmq_assert (_s == retired_fd);
  95. // Create the socket.
  96. _s = open_socket (AF_UNIX, SOCK_STREAM, 0);
  97. if (_s == retired_fd)
  98. return -1;
  99. // Set the non-blocking flag.
  100. unblock_socket (_s);
  101. // Connect to the remote peer.
  102. const int rc = ::connect (_s, _addr->resolved.ipc_addr->addr (),
  103. _addr->resolved.ipc_addr->addrlen ());
  104. // Connect was successful immediately.
  105. if (rc == 0)
  106. return 0;
  107. // Translate other error codes indicating asynchronous connect has been
  108. // launched to a uniform EINPROGRESS.
  109. #ifdef ZMQ_HAVE_WINDOWS
  110. const int last_error = WSAGetLastError ();
  111. if (last_error == WSAEINPROGRESS || last_error == WSAEWOULDBLOCK)
  112. errno = EINPROGRESS;
  113. else
  114. errno = wsa_error_to_errno (last_error);
  115. #else
  116. if (rc == -1 && errno == EINTR) {
  117. errno = EINPROGRESS;
  118. }
  119. #endif
  120. // Forward the error.
  121. return -1;
  122. }
  123. zmq::fd_t zmq::ipc_connecter_t::connect ()
  124. {
  125. // Following code should handle both Berkeley-derived socket
  126. // implementations and Solaris.
  127. int err = 0;
  128. zmq_socklen_t len = static_cast<zmq_socklen_t> (sizeof (err));
  129. const int rc = getsockopt (_s, SOL_SOCKET, SO_ERROR,
  130. reinterpret_cast<char *> (&err), &len);
  131. if (rc == -1) {
  132. if (errno == ENOPROTOOPT)
  133. errno = 0;
  134. err = errno;
  135. }
  136. if (err != 0) {
  137. // Assert if the error was caused by 0MQ bug.
  138. // Networking problems are OK. No need to assert.
  139. errno = err;
  140. errno_assert (errno == ECONNREFUSED || errno == ECONNRESET
  141. || errno == ETIMEDOUT || errno == EHOSTUNREACH
  142. || errno == ENETUNREACH || errno == ENETDOWN);
  143. return retired_fd;
  144. }
  145. const fd_t result = _s;
  146. _s = retired_fd;
  147. return result;
  148. }
  149. #endif