none.hpp 11 KB

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