Debian Codegraph @

modules/websocket/wsl_peer.h

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