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