socks.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef __ZMQ_SOCKS_HPP_INCLUDED__
  25. #define __ZMQ_SOCKS_HPP_INCLUDED__
  26. #include <string>
  27. #include "fd.hpp"
  28. #include "stdint.hpp"
  29. namespace zmq
  30. {
  31. struct socks_greeting_t
  32. {
  33. socks_greeting_t (uint8_t method_);
  34. socks_greeting_t (const uint8_t *methods_, uint8_t num_methods_);
  35. uint8_t methods[UINT8_MAX];
  36. const size_t num_methods;
  37. };
  38. class socks_greeting_encoder_t
  39. {
  40. public:
  41. socks_greeting_encoder_t ();
  42. void encode (const socks_greeting_t &greeting_);
  43. int output (fd_t fd_);
  44. bool has_pending_data () const;
  45. void reset ();
  46. private:
  47. size_t _bytes_encoded;
  48. size_t _bytes_written;
  49. uint8_t _buf[2 + UINT8_MAX];
  50. };
  51. struct socks_choice_t
  52. {
  53. socks_choice_t (uint8_t method_);
  54. uint8_t method;
  55. };
  56. class socks_choice_decoder_t
  57. {
  58. public:
  59. socks_choice_decoder_t ();
  60. int input (fd_t fd_);
  61. bool message_ready () const;
  62. socks_choice_t decode ();
  63. void reset ();
  64. private:
  65. unsigned char _buf[2];
  66. size_t _bytes_read;
  67. };
  68. struct socks_basic_auth_request_t
  69. {
  70. socks_basic_auth_request_t (const std::string &username_,
  71. const std::string &password_);
  72. const std::string username;
  73. const std::string password;
  74. };
  75. class socks_basic_auth_request_encoder_t
  76. {
  77. public:
  78. socks_basic_auth_request_encoder_t ();
  79. void encode (const socks_basic_auth_request_t &req_);
  80. int output (fd_t fd_);
  81. bool has_pending_data () const;
  82. void reset ();
  83. private:
  84. size_t _bytes_encoded;
  85. size_t _bytes_written;
  86. uint8_t _buf[1 + 1 + UINT8_MAX + 1 + UINT8_MAX];
  87. };
  88. struct socks_auth_response_t
  89. {
  90. socks_auth_response_t (uint8_t response_code_);
  91. uint8_t response_code;
  92. };
  93. class socks_auth_response_decoder_t
  94. {
  95. public:
  96. socks_auth_response_decoder_t ();
  97. int input (fd_t fd_);
  98. bool message_ready () const;
  99. socks_auth_response_t decode ();
  100. void reset ();
  101. private:
  102. int8_t _buf[2];
  103. size_t _bytes_read;
  104. };
  105. struct socks_request_t
  106. {
  107. socks_request_t (uint8_t command_, std::string hostname_, uint16_t port_);
  108. const uint8_t command;
  109. const std::string hostname;
  110. const uint16_t port;
  111. };
  112. class socks_request_encoder_t
  113. {
  114. public:
  115. socks_request_encoder_t ();
  116. void encode (const socks_request_t &req_);
  117. int output (fd_t fd_);
  118. bool has_pending_data () const;
  119. void reset ();
  120. private:
  121. size_t _bytes_encoded;
  122. size_t _bytes_written;
  123. uint8_t _buf[4 + UINT8_MAX + 1 + 2];
  124. };
  125. struct socks_response_t
  126. {
  127. socks_response_t (uint8_t response_code_,
  128. const std::string &address_,
  129. uint16_t port_);
  130. uint8_t response_code;
  131. std::string address;
  132. uint16_t port;
  133. };
  134. class socks_response_decoder_t
  135. {
  136. public:
  137. socks_response_decoder_t ();
  138. int input (fd_t fd_);
  139. bool message_ready () const;
  140. socks_response_t decode ();
  141. void reset ();
  142. private:
  143. int8_t _buf[4 + UINT8_MAX + 1 + 2];
  144. size_t _bytes_read;
  145. };
  146. }
  147. #endif