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.

411 lines
13 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 <sstream>
  30. #include <string>
  31. #include <websocketpp/common/asio.hpp>
  32. #include <websocketpp/common/memory.hpp>
  33. #include <websocketpp/transport/asio/security/base.hpp>
  34. #include <websocketpp/transport/base/connection.hpp>
  35. #include <websocketpp/uri.hpp>
  36. namespace websocketpp
  37. {
  38. namespace transport
  39. {
  40. namespace asio
  41. {
  42. /// A socket policy for the asio transport that implements a plain, unencrypted
  43. /// socket
  44. namespace basic_socket
  45. {
  46. /// The signature of the socket init handler for this socket policy
  47. typedef lib::function<void(connection_hdl, lib::asio::ip::tcp::socket &)> socket_init_handler;
  48. /// Basic Asio connection socket component
  49. /**
  50. * transport::asio::basic_socket::connection implements a connection socket
  51. * component using Asio ip::tcp::socket.
  52. */
  53. class connection : public lib::enable_shared_from_this<connection>
  54. {
  55. public:
  56. /// Type of this connection socket component
  57. typedef connection type;
  58. /// Type of a shared pointer to this connection socket component
  59. typedef lib::shared_ptr<type> ptr;
  60. /// Type of a pointer to the Asio io_service being used
  61. typedef lib::asio::io_service * io_service_ptr;
  62. /// Type of a pointer to the Asio io_service strand being used
  63. typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
  64. /// Type of the ASIO socket being used
  65. typedef lib::asio::ip::tcp::socket socket_type;
  66. /// Type of a shared pointer to the socket being used.
  67. typedef lib::shared_ptr<socket_type> socket_ptr;
  68. explicit connection() :
  69. m_state(UNINITIALIZED)
  70. {
  71. // std::cout << "transport::asio::basic_socket::connection constructor"
  72. // << std::endl;
  73. }
  74. /// Get a shared pointer to this component
  75. ptr get_shared()
  76. {
  77. return shared_from_this();
  78. }
  79. /// Check whether or not this connection is secure
  80. /**
  81. * @return Whether or not this connection is secure
  82. */
  83. bool is_secure() const
  84. {
  85. return false;
  86. }
  87. /// Set the socket initialization handler
  88. /**
  89. * The socket initialization handler is called after the socket object is
  90. * created but before it is used. This gives the application a chance to
  91. * set any Asio socket options it needs.
  92. *
  93. * @param h The new socket_init_handler
  94. */
  95. void set_socket_init_handler(socket_init_handler h)
  96. {
  97. m_socket_init_handler = h;
  98. }
  99. /// Retrieve a pointer to the underlying socket
  100. /**
  101. * This is used internally. It can also be used to set socket options, etc
  102. */
  103. lib::asio::ip::tcp::socket & get_socket()
  104. {
  105. return *m_socket;
  106. }
  107. /// Retrieve a pointer to the underlying socket
  108. /**
  109. * This is used internally.
  110. */
  111. lib::asio::ip::tcp::socket & get_next_layer()
  112. {
  113. return *m_socket;
  114. }
  115. /// Retrieve a pointer to the underlying socket
  116. /**
  117. * This is used internally. It can also be used to set socket options, etc
  118. */
  119. lib::asio::ip::tcp::socket & get_raw_socket()
  120. {
  121. return *m_socket;
  122. }
  123. /// Get the remote endpoint address
  124. /**
  125. * The iostream transport has no information about the ultimate remote
  126. * endpoint. It will return the string "iostream transport". To indicate
  127. * this.
  128. *
  129. * TODO: allow user settable remote endpoint addresses if this seems useful
  130. *
  131. * @return A string identifying the address of the remote endpoint
  132. */
  133. std::string get_remote_endpoint(lib::error_code & ec) const
  134. {
  135. std::stringstream s;
  136. lib::asio::error_code aec;
  137. lib::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(aec);
  138. if (aec)
  139. {
  140. ec = error::make_error_code(error::pass_through);
  141. s << "Error getting remote endpoint: " << aec << " (" << aec.message() << ")";
  142. return s.str();
  143. }
  144. else
  145. {
  146. ec = lib::error_code();
  147. s << ep;
  148. return s.str();
  149. }
  150. }
  151. protected:
  152. /// Perform one time initializations
  153. /**
  154. * init_asio is called once immediately after construction to initialize
  155. * Asio components to the io_service
  156. *
  157. * @param service A pointer to the endpoint's io_service
  158. * @param strand A shared pointer to the connection's asio strand
  159. * @param is_server Whether or not the endpoint is a server or not.
  160. */
  161. lib::error_code init_asio(io_service_ptr service, strand_ptr, bool)
  162. {
  163. if (m_state != UNINITIALIZED)
  164. {
  165. return socket::make_error_code(socket::error::invalid_state);
  166. }
  167. m_socket = lib::make_shared<lib::asio::ip::tcp::socket>(lib::ref(*service));
  168. if (m_socket_init_handler)
  169. {
  170. m_socket_init_handler(m_hdl, *m_socket);
  171. }
  172. m_state = READY;
  173. return lib::error_code();
  174. }
  175. /// Set uri hook
  176. /**
  177. * Called by the transport as a connection is being established to provide
  178. * the uri being connected to to the security/socket layer.
  179. *
  180. * This socket policy doesn't use the uri so it is ignored.
  181. *
  182. * @since 0.6.0
  183. *
  184. * @param u The uri to set
  185. */
  186. void set_uri(uri_ptr) {}
  187. /// Pre-initialize security policy
  188. /**
  189. * Called by the transport after a new connection is created to initialize
  190. * the socket component of the connection. This method is not allowed to
  191. * write any bytes to the wire. This initialization happens before any
  192. * proxies or other intermediate wrappers are negotiated.
  193. *
  194. * @param callback Handler to call back with completion information
  195. */
  196. void pre_init(init_handler callback)
  197. {
  198. if (m_state != READY)
  199. {
  200. callback(socket::make_error_code(socket::error::invalid_state));
  201. return;
  202. }
  203. m_state = READING;
  204. callback(lib::error_code());
  205. }
  206. /// Post-initialize security policy
  207. /**
  208. * Called by the transport after all intermediate proxies have been
  209. * negotiated. This gives the security policy the chance to talk with the
  210. * real remote endpoint for a bit before the websocket handshake.
  211. *
  212. * @param callback Handler to call back with completion information
  213. */
  214. void post_init(init_handler callback)
  215. {
  216. callback(lib::error_code());
  217. }
  218. /// Sets the connection handle
  219. /**
  220. * The connection handle is passed to any handlers to identify the
  221. * connection
  222. *
  223. * @param hdl The new handle
  224. */
  225. void set_handle(connection_hdl hdl)
  226. {
  227. m_hdl = hdl;
  228. }
  229. /// Cancel all async operations on this socket
  230. /**
  231. * Attempts to cancel all async operations on this socket and reports any
  232. * failures.
  233. *
  234. * NOTE: Windows XP and earlier do not support socket cancellation.
  235. *
  236. * @return The error that occurred, if any.
  237. */
  238. lib::asio::error_code cancel_socket()
  239. {
  240. lib::asio::error_code ec;
  241. m_socket->cancel(ec);
  242. return ec;
  243. }
  244. void async_shutdown(socket::shutdown_handler h)
  245. {
  246. lib::asio::error_code ec;
  247. m_socket->shutdown(lib::asio::ip::tcp::socket::shutdown_both, ec);
  248. h(ec);
  249. }
  250. lib::error_code get_ec() const
  251. {
  252. return lib::error_code();
  253. }
  254. public:
  255. /// Translate any security policy specific information about an error code
  256. /**
  257. * Translate_ec takes an Asio error code and attempts to convert its value
  258. * to an appropriate websocketpp error code. In the case that the Asio and
  259. * Websocketpp error types are the same (such as using boost::asio and
  260. * boost::system_error or using standalone asio and std::system_error the
  261. * code will be passed through natively.
  262. *
  263. * In the case of a mismatch (boost::asio with std::system_error) a
  264. * translated code will be returned. The plain socket policy does not have
  265. * any additional information so all such errors will be reported as the
  266. * generic transport pass_through error.
  267. *
  268. * @since 0.3.0
  269. *
  270. * @param ec The error code to translate_ec
  271. * @return The translated error code
  272. */
  273. template <typename ErrorCodeType> static lib::error_code translate_ec(ErrorCodeType)
  274. {
  275. // We don't know any more information about this error so pass through
  276. return make_error_code(transport::error::pass_through);
  277. }
  278. static
  279. /// Overload of translate_ec to catch cases where lib::error_code is the
  280. /// same type as lib::asio::error_code
  281. lib::error_code
  282. translate_ec(lib::error_code ec)
  283. {
  284. // We don't know any more information about this error, but the error is
  285. // the same type as the one we are translating to, so pass through
  286. // untranslated.
  287. return ec;
  288. }
  289. private:
  290. enum state
  291. {
  292. UNINITIALIZED = 0,
  293. READY = 1,
  294. READING = 2
  295. };
  296. socket_ptr m_socket;
  297. state m_state;
  298. connection_hdl m_hdl;
  299. socket_init_handler m_socket_init_handler;
  300. };
  301. /// Basic ASIO endpoint socket component
  302. /**
  303. * transport::asio::basic_socket::endpoint implements an endpoint socket
  304. * component that uses Boost ASIO's ip::tcp::socket.
  305. */
  306. class endpoint
  307. {
  308. public:
  309. /// The type of this endpoint socket component
  310. typedef endpoint type;
  311. /// The type of the corresponding connection socket component
  312. typedef connection socket_con_type;
  313. /// The type of a shared pointer to the corresponding connection socket
  314. /// component.
  315. typedef socket_con_type::ptr socket_con_ptr;
  316. explicit endpoint() {}
  317. /// Checks whether the endpoint creates secure connections
  318. /**
  319. * @return Whether or not the endpoint creates secure connections
  320. */
  321. bool is_secure() const
  322. {
  323. return false;
  324. }
  325. /// Set socket init handler
  326. /**
  327. * The socket init handler is called after a connection's socket is created
  328. * but before it is used. This gives the end application an opportunity to
  329. * set asio socket specific parameters.
  330. *
  331. * @param h The new socket_init_handler
  332. */
  333. void set_socket_init_handler(socket_init_handler h)
  334. {
  335. m_socket_init_handler = h;
  336. }
  337. protected:
  338. /// Initialize a connection
  339. /**
  340. * Called by the transport after a new connection is created to initialize
  341. * the socket component of the connection.
  342. *
  343. * @param scon Pointer to the socket component of the connection
  344. *
  345. * @return Error code (empty on success)
  346. */
  347. lib::error_code init(socket_con_ptr scon)
  348. {
  349. scon->set_socket_init_handler(m_socket_init_handler);
  350. return lib::error_code();
  351. }
  352. private:
  353. socket_init_handler m_socket_init_handler;
  354. };
  355. } // namespace basic_socket
  356. } // namespace asio
  357. } // namespace transport
  358. } // namespace websocketpp
  359. #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP