wsl_peer.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/stream_peer_tcp.h"
  38. #include <wslay/wslay.h>
  39. #define WSL_MAX_HEADER_SIZE 4096
  40. class WSLPeer : public WebSocketPeer {
  41. private:
  42. static CryptoCore::RandomGenerator *_static_rng;
  43. static WebSocketPeer *_create(bool p_notify_postinitialize) { return static_cast<WebSocketPeer *>(ClassDB::creator<WSLPeer>(p_notify_postinitialize)); }
  44. // Callbacks.
  45. static ssize_t _wsl_recv_callback(wslay_event_context_ptr ctx, uint8_t *data, size_t len, int flags, void *user_data);
  46. static void _wsl_recv_start_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
  47. 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);
  48. static ssize_t _wsl_send_callback(wslay_event_context_ptr ctx, const uint8_t *data, size_t len, int flags, void *user_data);
  49. static int _wsl_genmask_callback(wslay_event_context_ptr ctx, uint8_t *buf, size_t len, void *user_data);
  50. static void _wsl_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg *arg, void *user_data);
  51. static wslay_event_callbacks _wsl_callbacks;
  52. // Helpers
  53. static String _compute_key_response(String p_key);
  54. static String _generate_key();
  55. // Client IP resolver.
  56. class Resolver {
  57. Array ip_candidates;
  58. IP::ResolverID resolver_id = IP::RESOLVER_INVALID_ID;
  59. int port = 0;
  60. public:
  61. bool has_more_candidates() {
  62. return ip_candidates.size() > 0 || resolver_id != IP::RESOLVER_INVALID_ID;
  63. }
  64. void try_next_candidate(Ref<StreamPeerTCP> &p_tcp);
  65. void start(const String &p_host, int p_port);
  66. void stop();
  67. Resolver() {}
  68. };
  69. struct PendingMessage {
  70. size_t payload_size = 0;
  71. uint8_t opcode = 0;
  72. void clear() {
  73. payload_size = 0;
  74. opcode = 0;
  75. }
  76. };
  77. Resolver resolver;
  78. // WebSocket connection state.
  79. WebSocketPeer::State ready_state = WebSocketPeer::STATE_CLOSED;
  80. bool is_server = false;
  81. Ref<StreamPeerTCP> tcp;
  82. Ref<StreamPeer> connection;
  83. wslay_event_context_ptr wsl_ctx = nullptr;
  84. String requested_url;
  85. String requested_host;
  86. bool pending_request = true;
  87. Ref<StreamPeerBuffer> handshake_buffer;
  88. String selected_protocol;
  89. String session_key;
  90. int close_code = -1;
  91. String close_reason;
  92. uint8_t was_string = 0;
  93. uint64_t last_heartbeat = 0;
  94. bool heartbeat_waiting = false;
  95. PendingMessage pending_message;
  96. // WebSocket configuration.
  97. bool use_tls = true;
  98. Ref<TLSOptions> tls_options;
  99. // Packet buffers.
  100. Vector<uint8_t> packet_buffer;
  101. // Our packet info is just a boolean (is_string), using uint8_t for it.
  102. PacketBuffer<uint8_t> in_buffer;
  103. Error _send(const uint8_t *p_buffer, int p_buffer_size, wslay_opcode p_opcode);
  104. Error _do_server_handshake();
  105. bool _parse_client_request();
  106. void _do_client_handshake();
  107. bool _verify_server_response();
  108. void _clear();
  109. public:
  110. static void initialize();
  111. static void deinitialize();
  112. // PacketPeer
  113. virtual int get_available_packet_count() const override;
  114. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  115. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  116. virtual int get_max_packet_size() const override { return packet_buffer.size(); }
  117. // WebSocketPeer
  118. virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) override;
  119. virtual Error connect_to_url(const String &p_url, Ref<TLSOptions> p_options = Ref<TLSOptions>()) override;
  120. virtual Error accept_stream(Ref<StreamPeer> p_stream) override;
  121. virtual void close(int p_code = 1000, String p_reason = "") override;
  122. virtual void poll() override;
  123. virtual State get_ready_state() const override { return ready_state; }
  124. virtual int get_close_code() const override { return close_code; }
  125. virtual String get_close_reason() const override { return close_reason; }
  126. virtual int get_current_outbound_buffered_amount() const override;
  127. virtual IPAddress get_connected_host() const override;
  128. virtual uint16_t get_connected_port() const override;
  129. virtual String get_selected_protocol() const override;
  130. virtual String get_requested_url() const override;
  131. virtual bool was_string_packet() const override { return was_string; }
  132. virtual void set_no_delay(bool p_enabled) override;
  133. WSLPeer();
  134. ~WSLPeer();
  135. };
  136. #endif // WEB_ENABLED
  137. #endif // WSL_PEER_H