mechanism_base.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "mechanism_base.hpp"
  26. #include "session_base.hpp"
  27. zmq::mechanism_base_t::mechanism_base_t (session_base_t *const session_,
  28. const options_t &options_) :
  29. mechanism_t (options_),
  30. session (session_)
  31. {
  32. }
  33. int zmq::mechanism_base_t::check_basic_command_structure (msg_t *msg_) const
  34. {
  35. if (msg_->size () <= 1
  36. || msg_->size () <= (static_cast<uint8_t *> (msg_->data ()))[0]) {
  37. session->get_socket ()->event_handshake_failed_protocol (
  38. session->get_endpoint (),
  39. ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_UNSPECIFIED);
  40. errno = EPROTO;
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. void zmq::mechanism_base_t::handle_error_reason (const char *error_reason_,
  46. size_t error_reason_len_)
  47. {
  48. const size_t status_code_len = 3;
  49. const char zero_digit = '0';
  50. const size_t significant_digit_index = 0;
  51. const size_t first_zero_digit_index = 1;
  52. const size_t second_zero_digit_index = 2;
  53. const int factor = 100;
  54. if (error_reason_len_ == status_code_len
  55. && error_reason_[first_zero_digit_index] == zero_digit
  56. && error_reason_[second_zero_digit_index] == zero_digit
  57. && error_reason_[significant_digit_index] >= '3'
  58. && error_reason_[significant_digit_index] <= '5') {
  59. // it is a ZAP error status code (300, 400 or 500), so emit an authentication failure event
  60. session->get_socket ()->event_handshake_failed_auth (
  61. session->get_endpoint (),
  62. (error_reason_[significant_digit_index] - zero_digit) * factor);
  63. } else {
  64. // this is a violation of the ZAP protocol
  65. // TODO zmq_assert in this case?
  66. }
  67. }
  68. bool zmq::mechanism_base_t::zap_required () const
  69. {
  70. return !options.zap_domain.empty ();
  71. }