You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
  28. #define WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
  29. #include <websocketpp/uri.hpp>
  30. #include <websocketpp/transport/base/connection.hpp>
  31. #include <websocketpp/transport/asio/security/base.hpp>
  32. #include <websocketpp/common/asio.hpp>
  33. #include <websocketpp/common/memory.hpp>
  34. #include <sstream>
  35. #include <string>
  36. namespace websocketpp {
  37. namespace transport {
  38. namespace asio {
  39. /// A socket policy for the asio transport that implements a plain, unencrypted
  40. /// socket
  41. namespace basic_socket {
  42. /// The signature of the socket init handler for this socket policy
  43. typedef lib::function<void(connection_hdl,lib::asio::ip::tcp::socket&)>
  44. socket_init_handler;
  45. /// Basic Asio connection socket component
  46. /**
  47. * transport::asio::basic_socket::connection implements a connection socket
  48. * component using Asio ip::tcp::socket.
  49. */
  50. class connection : public lib::enable_shared_from_this<connection> {
  51. public:
  52. /// Type of this connection socket component
  53. typedef connection type;
  54. /// Type of a shared pointer to this connection socket component
  55. typedef lib::shared_ptr<type> ptr;
  56. /// Type of a pointer to the Asio io_service being used
  57. typedef lib::asio::io_service* io_service_ptr;
  58. /// Type of a pointer to the Asio io_service strand being used
  59. typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
  60. /// Type of the ASIO socket being used
  61. typedef lib::asio::ip::tcp::socket socket_type;
  62. /// Type of a shared pointer to the socket being used.
  63. typedef lib::shared_ptr<socket_type> socket_ptr;
  64. explicit connection() : m_state(UNINITIALIZED) {
  65. //std::cout << "transport::asio::basic_socket::connection constructor"
  66. // << std::endl;
  67. }
  68. /// Get a shared pointer to this component
  69. ptr get_shared() {
  70. return shared_from_this();
  71. }
  72. /// Check whether or not this connection is secure
  73. /**
  74. * @return Whether or not this connection is secure
  75. */
  76. bool is_secure() const {
  77. return false;
  78. }
  79. /// Set the socket initialization handler
  80. /**
  81. * The socket initialization handler is called after the socket object is
  82. * created but before it is used. This gives the application a chance to
  83. * set any Asio socket options it needs.
  84. *
  85. * @param h The new socket_init_handler
  86. */
  87. void set_socket_init_handler(socket_init_handler h) {
  88. m_socket_init_handler = h;
  89. }
  90. /// Retrieve a pointer to the underlying socket
  91. /**
  92. * This is used internally. It can also be used to set socket options, etc
  93. */
  94. lib::asio::ip::tcp::socket & get_socket() {
  95. return *m_socket;
  96. }
  97. /// Retrieve a pointer to the underlying socket
  98. /**
  99. * This is used internally.
  100. */
  101. lib::asio::ip::tcp::socket & get_next_layer() {
  102. return *m_socket;
  103. }
  104. /// Retrieve a pointer to the underlying socket
  105. /**
  106. * This is used internally. It can also be used to set socket options, etc
  107. */
  108. lib::asio::ip::tcp::socket & get_raw_socket() {
  109. return *m_socket;
  110. }
  111. /// Get the remote endpoint address
  112. /**
  113. * The iostream transport has no information about the ultimate remote
  114. * endpoint. It will return the string "iostream transport". To indicate
  115. * this.
  116. *
  117. * TODO: allow user settable remote endpoint addresses if this seems useful
  118. *
  119. * @return A string identifying the address of the remote endpoint
  120. */
  121. std::string get_remote_endpoint(lib::error_code & ec) const {
  122. std::stringstream s;
  123. lib::asio::error_code aec;
  124. lib::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(aec);
  125. if (aec) {
  126. ec = error::make_error_code(error::pass_through);
  127. s << "Error getting remote endpoint: " << aec
  128. << " (" << aec.message() << ")";
  129. return s.str();
  130. } else {
  131. ec = lib::error_code();
  132. s << ep;
  133. return s.str();
  134. }
  135. }
  136. protected:
  137. /// Perform one time initializations
  138. /**
  139. * init_asio is called once immediately after construction to initialize
  140. * Asio components to the io_service
  141. *
  142. * @param service A pointer to the endpoint's io_service
  143. * @param strand A shared pointer to the connection's asio strand
  144. * @param is_server Whether or not the endpoint is a server or not.
  145. */
  146. lib::error_code init_asio (io_service_ptr service, strand_ptr, bool)
  147. {
  148. if (m_state != UNINITIALIZED) {
  149. return socket::make_error_code(socket::error::invalid_state);
  150. }
  151. m_socket = lib::make_shared<lib::asio::ip::tcp::socket>(
  152. lib::ref(*service));
  153. if (m_socket_init_handler) {
  154. m_socket_init_handler(m_hdl, *m_socket);
  155. }
  156. m_state = READY;
  157. return lib::error_code();
  158. }
  159. /// Set uri hook
  160. /**
  161. * Called by the transport as a connection is being established to provide
  162. * the uri being connected to to the security/socket layer.
  163. *
  164. * This socket policy doesn't use the uri so it is ignored.
  165. *
  166. * @since 0.6.0
  167. *
  168. * @param u The uri to set
  169. */
  170. void set_uri(uri_ptr) {}
  171. /// Pre-initialize security policy
  172. /**
  173. * Called by the transport after a new connection is created to initialize
  174. * the socket component of the connection. This method is not allowed to
  175. * write any bytes to the wire. This initialization happens before any
  176. * proxies or other intermediate wrappers are negotiated.
  177. *
  178. * @param callback Handler to call back with completion information
  179. */
  180. void pre_init(init_handler callback) {
  181. if (m_state != READY) {
  182. callback(socket::make_error_code(socket::error::invalid_state));
  183. return;
  184. }
  185. m_state = READING;
  186. callback(lib::error_code());
  187. }
  188. /// Post-initialize security policy
  189. /**
  190. * Called by the transport after all intermediate proxies have been
  191. * negotiated. This gives the security policy the chance to talk with the
  192. * real remote endpoint for a bit before the websocket handshake.
  193. *
  194. * @param callback Handler to call back with completion information
  195. */
  196. void post_init(init_handler callback) {
  197. callback(lib::error_code());
  198. }
  199. /// Sets the connection handle
  200. /**
  201. * The connection handle is passed to any handlers to identify the
  202. * connection
  203. *
  204. * @param hdl The new handle
  205. */
  206. void set_handle(connection_hdl hdl) {
  207. m_hdl = hdl;
  208. }
  209. /// Cancel all async operations on this socket
  210. /**
  211. * Attempts to cancel all async operations on this socket and reports any
  212. * failures.
  213. *
  214. * NOTE: Windows XP and earlier do not support socket cancellation.
  215. *
  216. * @return The error that occurred, if any.
  217. */
  218. lib::asio::error_code cancel_socket() {
  219. lib::asio::error_code ec;
  220. m_socket->cancel(ec);
  221. return ec;
  222. }
  223. void async_shutdown(socket::shutdown_handler h) {
  224. lib::asio::error_code ec;
  225. m_socket->shutdown(lib::asio::ip::tcp::socket::shutdown_both, ec);
  226. h(ec);
  227. }
  228. lib::error_code get_ec() const {
  229. return lib::error_code();
  230. }
  231. public:
  232. /// Translate any security policy specific information about an error code
  233. /**
  234. * Translate_ec takes an Asio error code and attempts to convert its value
  235. * to an appropriate websocketpp error code. In the case that the Asio and
  236. * Websocketpp error types are the same (such as using boost::asio and
  237. * boost::system_error or using standalone asio and std::system_error the
  238. * code will be passed through natively.
  239. *
  240. * In the case of a mismatch (boost::asio with std::system_error) a
  241. * translated code will be returned. The plain socket policy does not have
  242. * any additional information so all such errors will be reported as the
  243. * generic transport pass_through error.
  244. *
  245. * @since 0.3.0
  246. *
  247. * @param ec The error code to translate_ec
  248. * @return The translated error code
  249. */
  250. template <typename ErrorCodeType>
  251. static
  252. lib::error_code translate_ec(ErrorCodeType) {
  253. // We don't know any more information about this error so pass through
  254. return make_error_code(transport::error::pass_through);
  255. }
  256. static
  257. /// Overload of translate_ec to catch cases where lib::error_code is the
  258. /// same type as lib::asio::error_code
  259. lib::error_code translate_ec(lib::error_code ec) {
  260. // We don't know any more information about this error, but the error is
  261. // the same type as the one we are translating to, so pass through
  262. // untranslated.
  263. return ec;
  264. }
  265. private:
  266. enum state {
  267. UNINITIALIZED = 0,
  268. READY = 1,
  269. READING = 2
  270. };
  271. socket_ptr m_socket;
  272. state m_state;
  273. connection_hdl m_hdl;
  274. socket_init_handler m_socket_init_handler;
  275. };
  276. /// Basic ASIO endpoint socket component
  277. /**
  278. * transport::asio::basic_socket::endpoint implements an endpoint socket
  279. * component that uses Boost ASIO's ip::tcp::socket.
  280. */
  281. class endpoint {
  282. public:
  283. /// The type of this endpoint socket component
  284. typedef endpoint type;
  285. /// The type of the corresponding connection socket component
  286. typedef connection socket_con_type;
  287. /// The type of a shared pointer to the corresponding connection socket
  288. /// component.
  289. typedef socket_con_type::ptr socket_con_ptr;
  290. explicit endpoint() {}
  291. /// Checks whether the endpoint creates secure connections
  292. /**
  293. * @return Whether or not the endpoint creates secure connections
  294. */
  295. bool is_secure() const {
  296. return false;
  297. }
  298. /// Set socket init handler
  299. /**
  300. * The socket init handler is called after a connection's socket is created
  301. * but before it is used. This gives the end application an opportunity to
  302. * set asio socket specific parameters.
  303. *
  304. * @param h The new socket_init_handler
  305. */
  306. void set_socket_init_handler(socket_init_handler h) {
  307. m_socket_init_handler = h;
  308. }
  309. protected:
  310. /// Initialize a connection
  311. /**
  312. * Called by the transport after a new connection is created to initialize
  313. * the socket component of the connection.
  314. *
  315. * @param scon Pointer to the socket component of the connection
  316. *
  317. * @return Error code (empty on success)
  318. */
  319. lib::error_code init(socket_con_ptr scon) {
  320. scon->set_socket_init_handler(m_socket_init_handler);
  321. return lib::error_code();
  322. }
  323. private:
  324. socket_init_handler m_socket_init_handler;
  325. };
  326. } // namespace basic_socket
  327. } // namespace asio
  328. } // namespace transport
  329. } // namespace websocketpp
  330. #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP