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