wsl_peer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**************************************************************************/
  2. /* wsl_peer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef WSL_PEER_H
  31. #define WSL_PEER_H
  32. #ifndef WEB_ENABLED
  33. #include "packet_buffer.h"
  34. #include "websocket_peer.h"
  35. #include "core/crypto/crypto_core.h"
  36. #include "core/error/error_list.h"
  37. #include "core/io/packet_peer.h"
  38. #include "core/io/stream_peer_tcp.h"
  39. #include "core/templates/ring_buffer.h"
  40. #include <wslay/wslay.h>
  41. #define WSL_MAX_HEADER_SIZE 4096
  42. class WSLPeer : public WebSocketPeer {
  43. private:
  44. static CryptoCore::RandomGenerator *_static_rng;
  45. static WebSocketPeer *_create(bool p_notify_postinitialize) { return static_cast<WebSocketPeer *>(ClassDB::creator<WSLPeer>(p_notify_postinitialize)); }
  46. // Callbacks.
  47. static ssize_t _wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data, size_t len, int flags, void *user_data);
  48. static void _wsl_recv_start_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
  49. static void _wsl_frame_recv_chunk_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_chunk_arg *arg, void *user_data);
  50. static void _wsl_frame_recv_end_callback(wslay_event_context_ptr ctx, void *user_data);
  51. static ssize_t _wsl_send_callback(wslay_event_context_ptr ctx, const uint8_t *data, size_t len, int flags, void *user_data);
  52. static int _wsl_genmask_callback(wslay_event_context_ptr ctx, uint8_t *buf, size_t len, void *user_data);
  53. static void _wsl_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg *arg, void *user_data);
  54. static wslay_event_callbacks _wsl_callbacks;
  55. // Helpers
  56. static String _compute_key_response(String p_key);
  57. static String _generate_key();
  58. // Client IP resolver.
  59. class Resolver {
  60. Array ip_candidates;
  61. IP::ResolverID resolver_id = IP::RESOLVER_INVALID_ID;
  62. int port = 0;
  63. public:
  64. bool has_more_candidates() {
  65. return ip_candidates.size() > 0 || resolver_id != IP::RESOLVER_INVALID_ID;
  66. }
  67. void try_next_candidate(Ref<StreamPeerTCP> &p_tcp);
  68. void start(const String &p_host, int p_port);
  69. void stop();
  70. Resolver() {}
  71. };
  72. struct PendingMessage {
  73. size_t payload_size = 0;
  74. uint8_t opcode = 0;
  75. void clear() {
  76. payload_size = 0;
  77. opcode = 0;
  78. }
  79. };
  80. Resolver resolver;
  81. // WebSocket connection state.
  82. WebSocketPeer::State ready_state = WebSocketPeer::STATE_CLOSED;
  83. bool is_server = false;
  84. Ref<StreamPeerTCP> tcp;
  85. Ref<StreamPeer> connection;
  86. wslay_event_context_ptr wsl_ctx = nullptr;
  87. String requested_url;
  88. String requested_host;
  89. bool pending_request = true;
  90. Ref<StreamPeerBuffer> handshake_buffer;
  91. String selected_protocol;
  92. String session_key;
  93. int close_code = -1;
  94. String close_reason;
  95. uint8_t was_string = 0;
  96. uint64_t last_heartbeat = 0;
  97. bool heartbeat_waiting = false;
  98. PendingMessage pending_message;
  99. // WebSocket configuration.
  100. bool use_tls = true;
  101. Ref<TLSOptions> tls_options;
  102. // Packet buffers.
  103. Vector<uint8_t> packet_buffer;
  104. // Our packet info is just a boolean (is_string), using uint8_t for it.
  105. PacketBuffer<uint8_t> in_buffer;
  106. Error _send(const uint8_t *p_buffer, int p_buffer_size, wslay_opcode p_opcode);
  107. Error _do_server_handshake();
  108. bool _parse_client_request();
  109. void _do_client_handshake();
  110. bool _verify_server_response();
  111. void _clear();
  112. public:
  113. static void initialize();
  114. static void deinitialize();
  115. // PacketPeer
  116. virtual int get_available_packet_count() const override;
  117. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  118. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  119. virtual int get_max_packet_size() const override { return packet_buffer.size(); }
  120. // WebSocketPeer
  121. virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) override;
  122. virtual Error connect_to_url(const String &p_url, Ref<TLSOptions> p_options = Ref<TLSOptions>()) override;
  123. virtual Error accept_stream(Ref<StreamPeer> p_stream) override;
  124. virtual void close(int p_code = 1000, String p_reason = "") override;
  125. virtual void poll() override;
  126. virtual State get_ready_state() const override { return ready_state; }
  127. virtual int get_close_code() const override { return close_code; }
  128. virtual String get_close_reason() const override { return close_reason; }
  129. virtual int get_current_outbound_buffered_amount() const override;
  130. virtual IPAddress get_connected_host() const override;
  131. virtual uint16_t get_connected_port() const override;
  132. virtual String get_selected_protocol() const override;
  133. virtual String get_requested_url() const override;
  134. virtual bool was_string_packet() const override { return was_string; }
  135. virtual void set_no_delay(bool p_enabled) override;
  136. WSLPeer();
  137. ~WSLPeer();
  138. };
  139. #endif // WEB_ENABLED
  140. #endif // WSL_PEER_H