null_mechanism.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <stddef.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "err.hpp"
  29. #include "msg.hpp"
  30. #include "session_base.hpp"
  31. #include "null_mechanism.hpp"
  32. const char error_command_name[] = "\5ERROR";
  33. const size_t error_command_name_len = sizeof (error_command_name) - 1;
  34. const size_t error_reason_len_size = 1;
  35. const char ready_command_name[] = "\5READY";
  36. const size_t ready_command_name_len = sizeof (ready_command_name) - 1;
  37. zmq::null_mechanism_t::null_mechanism_t (session_base_t *session_,
  38. const std::string &peer_address_,
  39. const options_t &options_) :
  40. mechanism_base_t (session_, options_),
  41. zap_client_t (session_, peer_address_, options_),
  42. _ready_command_sent (false),
  43. _error_command_sent (false),
  44. _ready_command_received (false),
  45. _error_command_received (false),
  46. _zap_request_sent (false),
  47. _zap_reply_received (false)
  48. {
  49. }
  50. zmq::null_mechanism_t::~null_mechanism_t ()
  51. {
  52. }
  53. int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_)
  54. {
  55. if (_ready_command_sent || _error_command_sent) {
  56. errno = EAGAIN;
  57. return -1;
  58. }
  59. if (zap_required () && !_zap_reply_received) {
  60. if (_zap_request_sent) {
  61. errno = EAGAIN;
  62. return -1;
  63. }
  64. // Given this is a backward-incompatible change, it's behind a socket
  65. // option disabled by default.
  66. int rc = session->zap_connect ();
  67. if (rc == -1 && options.zap_enforce_domain) {
  68. session->get_socket ()->event_handshake_failed_no_detail (
  69. session->get_endpoint (), EFAULT);
  70. return -1;
  71. }
  72. if (rc == 0) {
  73. send_zap_request ();
  74. _zap_request_sent = true;
  75. // TODO actually, it is quite unlikely that we can read the ZAP
  76. // reply already, but removing this has some strange side-effect
  77. // (probably because the pipe's in_active flag is true until a read
  78. // is attempted)
  79. rc = receive_and_process_zap_reply ();
  80. if (rc != 0)
  81. return -1;
  82. _zap_reply_received = true;
  83. }
  84. }
  85. if (_zap_reply_received && status_code != "200") {
  86. _error_command_sent = true;
  87. if (status_code != "300") {
  88. const size_t status_code_len = 3;
  89. const int rc = msg_->init_size (
  90. error_command_name_len + error_reason_len_size + status_code_len);
  91. zmq_assert (rc == 0);
  92. unsigned char *msg_data =
  93. static_cast<unsigned char *> (msg_->data ());
  94. memcpy (msg_data, error_command_name, error_command_name_len);
  95. msg_data += error_command_name_len;
  96. *msg_data = status_code_len;
  97. msg_data += error_reason_len_size;
  98. memcpy (msg_data, status_code.c_str (), status_code_len);
  99. return 0;
  100. }
  101. errno = EAGAIN;
  102. return -1;
  103. }
  104. make_command_with_basic_properties (msg_, ready_command_name,
  105. ready_command_name_len);
  106. _ready_command_sent = true;
  107. return 0;
  108. }
  109. int zmq::null_mechanism_t::process_handshake_command (msg_t *msg_)
  110. {
  111. if (_ready_command_received || _error_command_received) {
  112. session->get_socket ()->event_handshake_failed_protocol (
  113. session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
  114. errno = EPROTO;
  115. return -1;
  116. }
  117. const unsigned char *cmd_data =
  118. static_cast<unsigned char *> (msg_->data ());
  119. const size_t data_size = msg_->size ();
  120. int rc = 0;
  121. if (data_size >= ready_command_name_len
  122. && !memcmp (cmd_data, ready_command_name, ready_command_name_len))
  123. rc = process_ready_command (cmd_data, data_size);
  124. else if (data_size >= error_command_name_len
  125. && !memcmp (cmd_data, error_command_name, error_command_name_len))
  126. rc = process_error_command (cmd_data, data_size);
  127. else {
  128. session->get_socket ()->event_handshake_failed_protocol (
  129. session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
  130. errno = EPROTO;
  131. rc = -1;
  132. }
  133. if (rc == 0) {
  134. rc = msg_->close ();
  135. errno_assert (rc == 0);
  136. rc = msg_->init ();
  137. errno_assert (rc == 0);
  138. }
  139. return rc;
  140. }
  141. int zmq::null_mechanism_t::process_ready_command (
  142. const unsigned char *cmd_data_, size_t data_size_)
  143. {
  144. _ready_command_received = true;
  145. return parse_metadata (cmd_data_ + ready_command_name_len,
  146. data_size_ - ready_command_name_len);
  147. }
  148. int zmq::null_mechanism_t::process_error_command (
  149. const unsigned char *cmd_data_, size_t data_size_)
  150. {
  151. const size_t fixed_prefix_size =
  152. error_command_name_len + error_reason_len_size;
  153. if (data_size_ < fixed_prefix_size) {
  154. session->get_socket ()->event_handshake_failed_protocol (
  155. session->get_endpoint (),
  156. ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR);
  157. errno = EPROTO;
  158. return -1;
  159. }
  160. const size_t error_reason_len =
  161. static_cast<size_t> (cmd_data_[error_command_name_len]);
  162. if (error_reason_len > data_size_ - fixed_prefix_size) {
  163. session->get_socket ()->event_handshake_failed_protocol (
  164. session->get_endpoint (),
  165. ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_ERROR);
  166. errno = EPROTO;
  167. return -1;
  168. }
  169. const char *error_reason =
  170. reinterpret_cast<const char *> (cmd_data_) + fixed_prefix_size;
  171. handle_error_reason (error_reason, error_reason_len);
  172. _error_command_received = true;
  173. return 0;
  174. }
  175. int zmq::null_mechanism_t::zap_msg_available ()
  176. {
  177. if (_zap_reply_received) {
  178. errno = EFSM;
  179. return -1;
  180. }
  181. const int rc = receive_and_process_zap_reply ();
  182. if (rc == 0)
  183. _zap_reply_received = true;
  184. return rc == -1 ? -1 : 0;
  185. }
  186. zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const
  187. {
  188. if (_ready_command_sent && _ready_command_received)
  189. return ready;
  190. const bool command_sent = _ready_command_sent || _error_command_sent;
  191. const bool command_received =
  192. _ready_command_received || _error_command_received;
  193. return command_sent && command_received ? error : handshaking;
  194. }
  195. void zmq::null_mechanism_t::send_zap_request ()
  196. {
  197. zap_client_t::send_zap_request ("NULL", 4, NULL, NULL, 0);
  198. }