blob: 40c51288caf39ef83f5c302475bd776415a8aa8b [file] [log] [blame]
Ed Tanous7d3dba42017-04-05 13:04:39 -07001#include <crow/app.h>
Ed Tanousb4d29f42017-03-24 16:39:25 -07002#include <boost/endian/arithmetic.hpp>
3#include <string>
4
Ed Tanousb4d29f42017-03-24 16:39:25 -07005#include <video.h>
6
Ed Tanousc81ca422017-03-21 16:18:49 -07007namespace crow {
8namespace kvm {
Ed Tanousb4d29f42017-03-24 16:39:25 -07009
Ed Tanousc81ca422017-03-21 16:18:49 -070010static const std::string rfb_3_3_version_string = "RFB 003.003\n";
11static const std::string rfb_3_7_version_string = "RFB 003.007\n";
12static const std::string rfb_3_8_version_string = "RFB 003.008\n";
13
14enum class RfbAuthScheme : uint8_t {
15 connection_failed = 0,
16 no_authentication = 1,
17 vnc_authentication = 2
18};
19
20struct pixel_format_struct {
21 boost::endian::big_uint8_t bits_per_pixel;
22 boost::endian::big_uint8_t depth;
23 boost::endian::big_uint8_t is_big_endian;
24 boost::endian::big_uint8_t is_true_color;
25 boost::endian::big_uint16_t red_max;
26 boost::endian::big_uint16_t green_max;
27 boost::endian::big_uint16_t blue_max;
28 boost::endian::big_uint8_t red_shift;
29 boost::endian::big_uint8_t green_shift;
30 boost::endian::big_uint8_t blue_shift;
31 boost::endian::big_uint8_t pad1;
32 boost::endian::big_uint8_t pad2;
33 boost::endian::big_uint8_t pad3;
34};
35
Ed Tanousb4d29f42017-03-24 16:39:25 -070036struct server_initialization_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070037 boost::endian::big_uint16_t framebuffer_width;
38 boost::endian::big_uint16_t framebuffer_height;
39 pixel_format_struct pixel_format;
40 boost::endian::big_uint32_t name_length;
41};
42
Ed Tanousb4d29f42017-03-24 16:39:25 -070043enum class client_to_server_msg_type : uint8_t {
Ed Tanousc81ca422017-03-21 16:18:49 -070044 set_pixel_format = 0,
45 fix_color_map_entries = 1,
46 set_encodings = 2,
47 framebuffer_update_request = 3,
48 key_event = 4,
49 pointer_event = 5,
50 client_cut_text = 6
51};
52
Ed Tanousb4d29f42017-03-24 16:39:25 -070053struct set_pixel_format_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070054 boost::endian::big_uint8_t pad1;
55 boost::endian::big_uint8_t pad2;
56 boost::endian::big_uint8_t pad3;
57 pixel_format_struct pixel_format;
58};
59
Ed Tanousb4d29f42017-03-24 16:39:25 -070060struct frame_buffer_update_req {
Ed Tanousc81ca422017-03-21 16:18:49 -070061 boost::endian::big_uint8_t incremental;
62 boost::endian::big_uint16_t x_position;
63 boost::endian::big_uint16_t y_position;
64 boost::endian::big_uint16_t width;
65 boost::endian::big_uint16_t height;
66};
67
Ed Tanousb4d29f42017-03-24 16:39:25 -070068struct key_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070069 boost::endian::big_uint8_t down_flag;
70 boost::endian::big_uint8_t pad1;
71 boost::endian::big_uint8_t pad2;
72 boost::endian::big_uint32_t key;
73};
74
Ed Tanousb4d29f42017-03-24 16:39:25 -070075struct pointer_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070076 boost::endian::big_uint8_t button_mask;
77 boost::endian::big_uint16_t x_position;
78 boost::endian::big_uint16_t y_position;
79};
80
Ed Tanousb4d29f42017-03-24 16:39:25 -070081struct client_cut_text_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070082 std::vector<uint8_t> data;
83};
84
85enum class encoding_type : uint32_t {
86 raw = 0x00,
87 copy_rectangle = 0x01,
88 rising_rectangle = 0x02,
89 corre = 0x04,
90 hextile = 0x05,
91 zlib = 0x06,
92 tight = 0x07,
93 zlibhex = 0x08,
94 ultra = 0x09,
95 zrle = 0x10,
96 zywrle = 0x011,
97 cache_enable = 0xFFFF0001,
98 xor_enable = 0xFFFF0006,
99 server_state_ultranvc = 0xFFFF8000,
100 enable_keep_alive = 0xFFFF8001,
101 enableftp_protocol_version = 0xFFFF8002,
102 tight_compress_level_0 = 0xFFFFFF00,
103 tight_compress_level_9 = 0xFFFFFF09,
104 x_cursor = 0xFFFFFF10,
105 rich_cursor = 0xFFFFFF11,
106 pointer_pos = 0xFFFFFF18,
107 last_rect = 0xFFFFFF20,
108 new_framebuffer_size = 0xFFFFFF21,
109 tight_quality_level_0 = 0xFFFFFFE0,
110 tight_quality_level_9 = 0xFFFFFFE9
111};
112
113struct framebuffer_rectangle {
114 boost::endian::big_uint16_t x;
115 boost::endian::big_uint16_t y;
116 boost::endian::big_uint16_t width;
117 boost::endian::big_uint16_t height;
118 boost::endian::big_uint32_t encoding;
119 std::vector<uint8_t> data;
120};
121
Ed Tanousb4d29f42017-03-24 16:39:25 -0700122struct framebuffer_update_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -0700123 boost::endian::big_uint8_t message_type;
124 std::vector<framebuffer_rectangle> rectangles;
125};
126
Ed Tanousb4d29f42017-03-24 16:39:25 -0700127std::string serialize(const framebuffer_update_msg& msg) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700128 // calculate the size of the needed vector for serialization
129 size_t vector_size = 4;
130 for (const auto& rect : msg.rectangles) {
131 vector_size += 12 + rect.data.size();
132 }
133
134 std::string serialized(vector_size, 0);
135
136 size_t i = 0;
137 serialized[i++] = 0; // Type
138 serialized[i++] = 0; // Pad byte
139 boost::endian::big_uint16_t number_of_rectangles = msg.rectangles.size();
140 std::memcpy(&serialized[i], &number_of_rectangles,
141 sizeof(number_of_rectangles));
142 i += sizeof(number_of_rectangles);
143
144 for (const auto& rect : msg.rectangles) {
145 // copy the first part of the struct
146 size_t buffer_size =
147 sizeof(framebuffer_rectangle) - sizeof(std::vector<uint8_t>);
148 std::memcpy(&serialized[i], &rect, buffer_size);
149 i += buffer_size;
150
151 std::memcpy(&serialized[i], rect.data.data(), rect.data.size());
152 i += rect.data.size();
153 }
154
155 return serialized;
156}
157
158enum class VncState {
159 UNSTARTED,
160 AWAITING_CLIENT_VERSION,
161 AWAITING_CLIENT_AUTH_METHOD,
Ed Tanousb4d29f42017-03-24 16:39:25 -0700162 AWAITING_CLIENT_INIT_msg,
Ed Tanousc81ca422017-03-21 16:18:49 -0700163 MAIN_LOOP
164};
165
166class connection_metadata {
167 public:
Ed Tanousb4d29f42017-03-24 16:39:25 -0700168 connection_metadata(void) : vnc_state(VncState::UNSTARTED){};
Ed Tanousc81ca422017-03-21 16:18:49 -0700169
170 VncState vnc_state;
171};
172
173typedef std::vector<connection_metadata> meta_list;
174meta_list connection_states(10);
175
176connection_metadata meta;
177
Ed Tanous7d3dba42017-04-05 13:04:39 -0700178template <typename... Middlewares>
179void request_routes(Crow<Middlewares...>& app) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700180 CROW_ROUTE(app, "/kvmws")
181 .websocket()
182 .onopen([&](crow::websocket::connection& conn) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700183 if (meta.vnc_state == VncState::UNSTARTED) {
184 meta.vnc_state = VncState::AWAITING_CLIENT_VERSION;
185 conn.send_binary(rfb_3_8_version_string);
186 } else {
187 conn.close();
188 }
189
Ed Tanousc81ca422017-03-21 16:18:49 -0700190 })
191 .onclose(
192 [&](crow::websocket::connection& conn, const std::string& reason) {
193 meta.vnc_state = VncState::UNSTARTED;
194 })
195 .onmessage([&](crow::websocket::connection& conn, const std::string& data,
196 bool is_binary) {
197 switch (meta.vnc_state) {
198 case VncState::AWAITING_CLIENT_VERSION: {
199 LOG(DEBUG) << "Client sent: " << data;
200 if (data == rfb_3_8_version_string ||
201 data == rfb_3_7_version_string) {
202 std::string auth_types{1,
203 (uint8_t)RfbAuthScheme::no_authentication};
204 conn.send_binary(auth_types);
205 meta.vnc_state = VncState::AWAITING_CLIENT_AUTH_METHOD;
206 } else if (data == rfb_3_3_version_string) {
207 // TODO(ed) Support older protocols
208 meta.vnc_state = VncState::UNSTARTED;
209 conn.close();
210 } else {
211 // TODO(ed) Support older protocols
212 meta.vnc_state = VncState::UNSTARTED;
213 conn.close();
214 }
215 } break;
216 case VncState::AWAITING_CLIENT_AUTH_METHOD: {
217 std::string security_result{{0, 0, 0, 0}};
218 if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700219 meta.vnc_state = VncState::AWAITING_CLIENT_INIT_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700220 } else {
221 // Mark auth as failed
222 security_result[3] = 1;
223 meta.vnc_state = VncState::UNSTARTED;
224 }
225 conn.send_binary(security_result);
226 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700227 case VncState::AWAITING_CLIENT_INIT_msg: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700228 // Now send the server initialization
Ed Tanousb4d29f42017-03-24 16:39:25 -0700229 server_initialization_msg server_init_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700230 server_init_msg.framebuffer_width = 640;
231 server_init_msg.framebuffer_height = 480;
232 server_init_msg.pixel_format.bits_per_pixel = 32;
233 server_init_msg.pixel_format.is_big_endian = 0;
234 server_init_msg.pixel_format.is_true_color = 1;
235 server_init_msg.pixel_format.red_max = 255;
236 server_init_msg.pixel_format.green_max = 255;
237 server_init_msg.pixel_format.blue_max = 255;
238 server_init_msg.pixel_format.red_shift = 16;
239 server_init_msg.pixel_format.green_shift = 8;
240 server_init_msg.pixel_format.blue_shift = 0;
241 server_init_msg.name_length = 0;
242 LOG(DEBUG) << "size: " << sizeof(server_init_msg);
243 // TODO(ed) this is ugly. Crow should really have a span type
244 // interface
245 // to avoid the copy, but alas, today it does not.
246 std::string s(reinterpret_cast<char*>(&server_init_msg),
247 sizeof(server_init_msg));
248 LOG(DEBUG) << "s.size() " << s.size();
249 conn.send_binary(s);
250 meta.vnc_state = VncState::MAIN_LOOP;
251 } break;
252 case VncState::MAIN_LOOP: {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700253 if (data.size() >= sizeof(client_to_server_msg_type)) {
254 auto type = static_cast<client_to_server_msg_type>(data[0]);
255 LOG(DEBUG) << "Received client message type " << (uint32_t)type
256 << "\n";
Ed Tanousc81ca422017-03-21 16:18:49 -0700257 switch (type) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700258 case client_to_server_msg_type::set_pixel_format: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700259 } break;
260
Ed Tanousb4d29f42017-03-24 16:39:25 -0700261 case client_to_server_msg_type::fix_color_map_entries: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700262 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700263 case client_to_server_msg_type::set_encodings: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700264 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700265 case client_to_server_msg_type::framebuffer_update_request: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700266 // Make sure the buffer is long enough to handle what we're
267 // about to do
Ed Tanousb4d29f42017-03-24 16:39:25 -0700268 if (data.size() >= sizeof(frame_buffer_update_req) +
269 sizeof(client_to_server_msg_type)) {
270 auto msg = reinterpret_cast<const frame_buffer_update_req*>(
271 data.data() + sizeof(client_to_server_msg_type));
Ed Tanousc81ca422017-03-21 16:18:49 -0700272
273 if (!msg->incremental) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700274 framebuffer_update_msg buffer_update_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700275
276 // If the viewer is requesting a full update, force write
277 // of all pixels
278
279 framebuffer_rectangle this_rect;
280 this_rect.x = msg->x_position;
281 this_rect.y = msg->y_position;
282 this_rect.width = msg->width;
283 this_rect.height = msg->height;
284 this_rect.encoding =
285 static_cast<uint8_t>(encoding_type::raw);
286 LOG(DEBUG) << "Encoding is" << this_rect.encoding;
287 this_rect.data.reserve(this_rect.width *
288 this_rect.height * 4);
289
290 for (unsigned int x_index = 0; x_index < this_rect.width;
291 x_index++) {
292 for (unsigned int y_index = 0;
293 y_index < this_rect.height; y_index++) {
294 this_rect.data.push_back(
295 static_cast<uint8_t>(0)); // Blue
296 this_rect.data.push_back(
297 static_cast<uint8_t>(0)); // Green
298 this_rect.data.push_back(static_cast<uint8_t>(
299 x_index * 0xFF / msg->width)); // RED
300 this_rect.data.push_back(
301 static_cast<uint8_t>(0)); // UNUSED
302 }
303 }
304
Ed Tanousb4d29f42017-03-24 16:39:25 -0700305 buffer_update_msg.rectangles.push_back(
Ed Tanousc81ca422017-03-21 16:18:49 -0700306 std::move(this_rect));
Ed Tanousb4d29f42017-03-24 16:39:25 -0700307 auto serialized = serialize(buffer_update_msg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700308
309 conn.send_binary(serialized);
310 }
311 }
312
313 }
314
315 break;
316
Ed Tanousb4d29f42017-03-24 16:39:25 -0700317 case client_to_server_msg_type::key_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700318 } break;
319
Ed Tanousb4d29f42017-03-24 16:39:25 -0700320 case client_to_server_msg_type::pointer_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700321 } break;
322
Ed Tanousb4d29f42017-03-24 16:39:25 -0700323 case client_to_server_msg_type::client_cut_text: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700324 } break;
325
326 default:
327 break;
328 }
329 }
330
331 } break;
332 case VncState::UNSTARTED:
333 // Error? TODO
334 break;
335 }
336
337 });
338}
339}
340}