ws_connecter.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright (c) 2007-2019 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 <new>
  26. #include <string>
  27. #include "macros.hpp"
  28. #include "ws_connecter.hpp"
  29. #include "io_thread.hpp"
  30. #include "err.hpp"
  31. #include "ip.hpp"
  32. #include "tcp.hpp"
  33. #include "address.hpp"
  34. #include "ws_address.hpp"
  35. #include "ws_engine.hpp"
  36. #include "session_base.hpp"
  37. #ifdef ZMQ_HAVE_WSS
  38. #include "wss_engine.hpp"
  39. #include "wss_address.hpp"
  40. #endif
  41. #if !defined ZMQ_HAVE_WINDOWS
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <arpa/inet.h>
  46. #include <netinet/tcp.h>
  47. #include <netinet/in.h>
  48. #include <netdb.h>
  49. #include <fcntl.h>
  50. #ifdef ZMQ_HAVE_VXWORKS
  51. #include <sockLib.h>
  52. #endif
  53. #ifdef ZMQ_HAVE_OPENVMS
  54. #include <ioctl.h>
  55. #endif
  56. #endif
  57. #ifdef __APPLE__
  58. #include <TargetConditionals.h>
  59. #endif
  60. zmq::ws_connecter_t::ws_connecter_t (class io_thread_t *io_thread_,
  61. class session_base_t *session_,
  62. const options_t &options_,
  63. address_t *addr_,
  64. bool delayed_start_,
  65. bool wss_,
  66. const std::string &tls_hostname_) :
  67. stream_connecter_base_t (
  68. io_thread_, session_, options_, addr_, delayed_start_),
  69. _connect_timer_started (false),
  70. _wss (wss_),
  71. _hostname (tls_hostname_)
  72. {
  73. }
  74. zmq::ws_connecter_t::~ws_connecter_t ()
  75. {
  76. zmq_assert (!_connect_timer_started);
  77. }
  78. void zmq::ws_connecter_t::process_term (int linger_)
  79. {
  80. if (_connect_timer_started) {
  81. cancel_timer (connect_timer_id);
  82. _connect_timer_started = false;
  83. }
  84. stream_connecter_base_t::process_term (linger_);
  85. }
  86. void zmq::ws_connecter_t::out_event ()
  87. {
  88. if (_connect_timer_started) {
  89. cancel_timer (connect_timer_id);
  90. _connect_timer_started = false;
  91. }
  92. // TODO this is still very similar to (t)ipc_connecter_t, maybe the
  93. // differences can be factored out
  94. rm_handle ();
  95. const fd_t fd = connect ();
  96. // Handle the error condition by attempt to reconnect.
  97. if (fd == retired_fd || !tune_socket (fd)) {
  98. close ();
  99. add_reconnect_timer ();
  100. return;
  101. }
  102. if (_wss)
  103. #ifdef ZMQ_HAVE_WSS
  104. create_engine (fd,
  105. get_socket_name<wss_address_t> (fd, socket_end_local));
  106. #else
  107. assert (false);
  108. #endif
  109. else
  110. create_engine (fd,
  111. get_socket_name<ws_address_t> (fd, socket_end_local));
  112. }
  113. void zmq::ws_connecter_t::timer_event (int id_)
  114. {
  115. if (id_ == connect_timer_id) {
  116. _connect_timer_started = false;
  117. rm_handle ();
  118. close ();
  119. add_reconnect_timer ();
  120. } else
  121. stream_connecter_base_t::timer_event (id_);
  122. }
  123. void zmq::ws_connecter_t::start_connecting ()
  124. {
  125. // Open the connecting socket.
  126. const int rc = open ();
  127. // Connect may succeed in synchronous manner.
  128. if (rc == 0) {
  129. _handle = add_fd (_s);
  130. out_event ();
  131. }
  132. // Connection establishment may be delayed. Poll for its completion.
  133. else if (rc == -1 && errno == EINPROGRESS) {
  134. _handle = add_fd (_s);
  135. set_pollout (_handle);
  136. _socket->event_connect_delayed (
  137. make_unconnected_connect_endpoint_pair (_endpoint), zmq_errno ());
  138. // add userspace connect timeout
  139. add_connect_timer ();
  140. }
  141. // Handle any other error condition by eventual reconnect.
  142. else {
  143. if (_s != retired_fd)
  144. close ();
  145. add_reconnect_timer ();
  146. }
  147. }
  148. void zmq::ws_connecter_t::add_connect_timer ()
  149. {
  150. if (options.connect_timeout > 0) {
  151. add_timer (options.connect_timeout, connect_timer_id);
  152. _connect_timer_started = true;
  153. }
  154. }
  155. int zmq::ws_connecter_t::open ()
  156. {
  157. zmq_assert (_s == retired_fd);
  158. tcp_address_t tcp_addr;
  159. _s = tcp_open_socket (_addr->address.c_str (), options, false, true,
  160. &tcp_addr);
  161. if (_s == retired_fd)
  162. return -1;
  163. // Set the socket to non-blocking mode so that we get async connect().
  164. unblock_socket (_s);
  165. // Connect to the remote peer.
  166. #ifdef ZMQ_HAVE_VXWORKS
  167. int rc = ::connect (_s, (sockaddr *) tcp_addr.addr (), tcp_addr.addrlen ());
  168. #else
  169. const int rc = ::connect (_s, tcp_addr.addr (), tcp_addr.addrlen ());
  170. #endif
  171. // Connect was successful immediately.
  172. if (rc == 0) {
  173. return 0;
  174. }
  175. // Translate error codes indicating asynchronous connect has been
  176. // launched to a uniform EINPROGRESS.
  177. #ifdef ZMQ_HAVE_WINDOWS
  178. const int last_error = WSAGetLastError ();
  179. if (last_error == WSAEINPROGRESS || last_error == WSAEWOULDBLOCK)
  180. errno = EINPROGRESS;
  181. else
  182. errno = wsa_error_to_errno (last_error);
  183. #else
  184. if (errno == EINTR)
  185. errno = EINPROGRESS;
  186. #endif
  187. return -1;
  188. }
  189. zmq::fd_t zmq::ws_connecter_t::connect ()
  190. {
  191. // Async connect has finished. Check whether an error occurred
  192. int err = 0;
  193. #if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_VXWORKS
  194. int len = sizeof err;
  195. #else
  196. socklen_t len = sizeof err;
  197. #endif
  198. const int rc = getsockopt (_s, SOL_SOCKET, SO_ERROR,
  199. reinterpret_cast<char *> (&err), &len);
  200. // Assert if the error was caused by 0MQ bug.
  201. // Networking problems are OK. No need to assert.
  202. #ifdef ZMQ_HAVE_WINDOWS
  203. zmq_assert (rc == 0);
  204. if (err != 0) {
  205. if (err == WSAEBADF || err == WSAENOPROTOOPT || err == WSAENOTSOCK
  206. || err == WSAENOBUFS) {
  207. wsa_assert_no (err);
  208. }
  209. return retired_fd;
  210. }
  211. #else
  212. // Following code should handle both Berkeley-derived socket
  213. // implementations and Solaris.
  214. if (rc == -1)
  215. err = errno;
  216. if (err != 0) {
  217. errno = err;
  218. #if !defined(TARGET_OS_IPHONE) || !TARGET_OS_IPHONE
  219. errno_assert (errno != EBADF && errno != ENOPROTOOPT
  220. && errno != ENOTSOCK && errno != ENOBUFS);
  221. #else
  222. errno_assert (errno != ENOPROTOOPT && errno != ENOTSOCK
  223. && errno != ENOBUFS);
  224. #endif
  225. return retired_fd;
  226. }
  227. #endif
  228. // Return the newly connected socket.
  229. const fd_t result = _s;
  230. _s = retired_fd;
  231. return result;
  232. }
  233. bool zmq::ws_connecter_t::tune_socket (const fd_t fd_)
  234. {
  235. const int rc =
  236. tune_tcp_socket (fd_) | tune_tcp_maxrt (fd_, options.tcp_maxrt);
  237. return rc == 0;
  238. }
  239. void zmq::ws_connecter_t::create_engine (fd_t fd_,
  240. const std::string &local_address_)
  241. {
  242. const endpoint_uri_pair_t endpoint_pair (local_address_, _endpoint,
  243. endpoint_type_connect);
  244. // Create the engine object for this connection.
  245. i_engine *engine = NULL;
  246. if (_wss)
  247. #ifdef ZMQ_HAVE_WSS
  248. engine = new (std::nothrow)
  249. wss_engine_t (fd_, options, endpoint_pair, *_addr->resolved.ws_addr,
  250. true, NULL, _hostname);
  251. #else
  252. assert (false);
  253. #endif
  254. else
  255. engine = new (std::nothrow) ws_engine_t (
  256. fd_, options, endpoint_pair, *_addr->resolved.ws_addr, true);
  257. alloc_assert (engine);
  258. // Attach the engine to the corresponding session object.
  259. send_attach (_session, engine);
  260. // Shut the connecter down.
  261. terminate ();
  262. _socket->event_connected (endpoint_pair, fd_);
  263. }