blob: 62be097e507ed9053cf8046f558930c0a9e23d8a [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 Tanous93f987d2017-04-17 17:52:36 -07005#include <ast_jpeg_decoder.hpp>
6#include <ast_video_puller.hpp>
Ed Tanousb4d29f42017-03-24 16:39:25 -07007
Ed Tanousc81ca422017-03-21 16:18:49 -07008namespace crow {
9namespace kvm {
Ed Tanousb4d29f42017-03-24 16:39:25 -070010
Ed Tanousc81ca422017-03-21 16:18:49 -070011static const std::string rfb_3_3_version_string = "RFB 003.003\n";
12static const std::string rfb_3_7_version_string = "RFB 003.007\n";
13static const std::string rfb_3_8_version_string = "RFB 003.008\n";
14
15enum class RfbAuthScheme : uint8_t {
16 connection_failed = 0,
17 no_authentication = 1,
18 vnc_authentication = 2
19};
20
21struct pixel_format_struct {
22 boost::endian::big_uint8_t bits_per_pixel;
23 boost::endian::big_uint8_t depth;
24 boost::endian::big_uint8_t is_big_endian;
25 boost::endian::big_uint8_t is_true_color;
26 boost::endian::big_uint16_t red_max;
27 boost::endian::big_uint16_t green_max;
28 boost::endian::big_uint16_t blue_max;
29 boost::endian::big_uint8_t red_shift;
30 boost::endian::big_uint8_t green_shift;
31 boost::endian::big_uint8_t blue_shift;
32 boost::endian::big_uint8_t pad1;
33 boost::endian::big_uint8_t pad2;
34 boost::endian::big_uint8_t pad3;
35};
36
Ed Tanousb4d29f42017-03-24 16:39:25 -070037struct server_initialization_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070038 boost::endian::big_uint16_t framebuffer_width;
39 boost::endian::big_uint16_t framebuffer_height;
40 pixel_format_struct pixel_format;
41 boost::endian::big_uint32_t name_length;
42};
43
Ed Tanousb4d29f42017-03-24 16:39:25 -070044enum class client_to_server_msg_type : uint8_t {
Ed Tanousc81ca422017-03-21 16:18:49 -070045 set_pixel_format = 0,
46 fix_color_map_entries = 1,
47 set_encodings = 2,
48 framebuffer_update_request = 3,
49 key_event = 4,
50 pointer_event = 5,
51 client_cut_text = 6
52};
53
Ed Tanousb4d29f42017-03-24 16:39:25 -070054struct set_pixel_format_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070055 boost::endian::big_uint8_t pad1;
56 boost::endian::big_uint8_t pad2;
57 boost::endian::big_uint8_t pad3;
58 pixel_format_struct pixel_format;
59};
60
Ed Tanousb4d29f42017-03-24 16:39:25 -070061struct frame_buffer_update_req {
Ed Tanousc81ca422017-03-21 16:18:49 -070062 boost::endian::big_uint8_t incremental;
63 boost::endian::big_uint16_t x_position;
64 boost::endian::big_uint16_t y_position;
65 boost::endian::big_uint16_t width;
66 boost::endian::big_uint16_t height;
67};
68
Ed Tanousb4d29f42017-03-24 16:39:25 -070069struct key_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070070 boost::endian::big_uint8_t down_flag;
71 boost::endian::big_uint8_t pad1;
72 boost::endian::big_uint8_t pad2;
73 boost::endian::big_uint32_t key;
74};
75
Ed Tanousb4d29f42017-03-24 16:39:25 -070076struct pointer_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070077 boost::endian::big_uint8_t button_mask;
78 boost::endian::big_uint16_t x_position;
79 boost::endian::big_uint16_t y_position;
80};
81
Ed Tanousb4d29f42017-03-24 16:39:25 -070082struct client_cut_text_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070083 std::vector<uint8_t> data;
84};
85
86enum class encoding_type : uint32_t {
87 raw = 0x00,
88 copy_rectangle = 0x01,
89 rising_rectangle = 0x02,
90 corre = 0x04,
91 hextile = 0x05,
92 zlib = 0x06,
93 tight = 0x07,
94 zlibhex = 0x08,
95 ultra = 0x09,
96 zrle = 0x10,
97 zywrle = 0x011,
98 cache_enable = 0xFFFF0001,
99 xor_enable = 0xFFFF0006,
100 server_state_ultranvc = 0xFFFF8000,
101 enable_keep_alive = 0xFFFF8001,
102 enableftp_protocol_version = 0xFFFF8002,
103 tight_compress_level_0 = 0xFFFFFF00,
104 tight_compress_level_9 = 0xFFFFFF09,
105 x_cursor = 0xFFFFFF10,
106 rich_cursor = 0xFFFFFF11,
107 pointer_pos = 0xFFFFFF18,
108 last_rect = 0xFFFFFF20,
109 new_framebuffer_size = 0xFFFFFF21,
110 tight_quality_level_0 = 0xFFFFFFE0,
111 tight_quality_level_9 = 0xFFFFFFE9
112};
113
114struct framebuffer_rectangle {
115 boost::endian::big_uint16_t x;
116 boost::endian::big_uint16_t y;
117 boost::endian::big_uint16_t width;
118 boost::endian::big_uint16_t height;
119 boost::endian::big_uint32_t encoding;
120 std::vector<uint8_t> data;
121};
122
Ed Tanousb4d29f42017-03-24 16:39:25 -0700123struct framebuffer_update_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -0700124 boost::endian::big_uint8_t message_type;
125 std::vector<framebuffer_rectangle> rectangles;
126};
127
Ed Tanousb4d29f42017-03-24 16:39:25 -0700128std::string serialize(const framebuffer_update_msg& msg) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700129 // calculate the size of the needed vector for serialization
130 size_t vector_size = 4;
131 for (const auto& rect : msg.rectangles) {
132 vector_size += 12 + rect.data.size();
133 }
134
135 std::string serialized(vector_size, 0);
136
137 size_t i = 0;
138 serialized[i++] = 0; // Type
139 serialized[i++] = 0; // Pad byte
140 boost::endian::big_uint16_t number_of_rectangles = msg.rectangles.size();
141 std::memcpy(&serialized[i], &number_of_rectangles,
142 sizeof(number_of_rectangles));
143 i += sizeof(number_of_rectangles);
144
145 for (const auto& rect : msg.rectangles) {
146 // copy the first part of the struct
147 size_t buffer_size =
148 sizeof(framebuffer_rectangle) - sizeof(std::vector<uint8_t>);
149 std::memcpy(&serialized[i], &rect, buffer_size);
150 i += buffer_size;
151
152 std::memcpy(&serialized[i], rect.data.data(), rect.data.size());
153 i += rect.data.size();
154 }
155
156 return serialized;
157}
158
159enum class VncState {
160 UNSTARTED,
161 AWAITING_CLIENT_VERSION,
162 AWAITING_CLIENT_AUTH_METHOD,
Ed Tanousb4d29f42017-03-24 16:39:25 -0700163 AWAITING_CLIENT_INIT_msg,
Ed Tanousc81ca422017-03-21 16:18:49 -0700164 MAIN_LOOP
165};
166
167class connection_metadata {
168 public:
Ed Tanousb4d29f42017-03-24 16:39:25 -0700169 connection_metadata(void) : vnc_state(VncState::UNSTARTED){};
Ed Tanousc81ca422017-03-21 16:18:49 -0700170
171 VncState vnc_state;
172};
173
174typedef std::vector<connection_metadata> meta_list;
175meta_list connection_states(10);
176
177connection_metadata meta;
178
Ed Tanous7d3dba42017-04-05 13:04:39 -0700179template <typename... Middlewares>
180void request_routes(Crow<Middlewares...>& app) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700181 CROW_ROUTE(app, "/kvmws")
182 .websocket()
183 .onopen([&](crow::websocket::connection& conn) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700184 if (meta.vnc_state == VncState::UNSTARTED) {
185 meta.vnc_state = VncState::AWAITING_CLIENT_VERSION;
186 conn.send_binary(rfb_3_8_version_string);
Ed Tanous01250f22017-04-18 17:49:51 -0700187 } else { // SHould never happen
Ed Tanousb4d29f42017-03-24 16:39:25 -0700188 conn.close();
189 }
190
Ed Tanousc81ca422017-03-21 16:18:49 -0700191 })
192 .onclose(
193 [&](crow::websocket::connection& conn, const std::string& reason) {
194 meta.vnc_state = VncState::UNSTARTED;
195 })
196 .onmessage([&](crow::websocket::connection& conn, const std::string& data,
197 bool is_binary) {
198 switch (meta.vnc_state) {
199 case VncState::AWAITING_CLIENT_VERSION: {
200 LOG(DEBUG) << "Client sent: " << data;
201 if (data == rfb_3_8_version_string ||
202 data == rfb_3_7_version_string) {
203 std::string auth_types{1,
204 (uint8_t)RfbAuthScheme::no_authentication};
205 conn.send_binary(auth_types);
206 meta.vnc_state = VncState::AWAITING_CLIENT_AUTH_METHOD;
207 } else if (data == rfb_3_3_version_string) {
208 // TODO(ed) Support older protocols
209 meta.vnc_state = VncState::UNSTARTED;
210 conn.close();
211 } else {
212 // TODO(ed) Support older protocols
213 meta.vnc_state = VncState::UNSTARTED;
214 conn.close();
215 }
216 } break;
217 case VncState::AWAITING_CLIENT_AUTH_METHOD: {
218 std::string security_result{{0, 0, 0, 0}};
219 if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700220 meta.vnc_state = VncState::AWAITING_CLIENT_INIT_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700221 } else {
222 // Mark auth as failed
223 security_result[3] = 1;
224 meta.vnc_state = VncState::UNSTARTED;
225 }
226 conn.send_binary(security_result);
227 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700228 case VncState::AWAITING_CLIENT_INIT_msg: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700229 // Now send the server initialization
Ed Tanousb4d29f42017-03-24 16:39:25 -0700230 server_initialization_msg server_init_msg;
Ed Tanous93f987d2017-04-17 17:52:36 -0700231 server_init_msg.framebuffer_width = 800;
232 server_init_msg.framebuffer_height = 600;
Ed Tanousc81ca422017-03-21 16:18:49 -0700233 server_init_msg.pixel_format.bits_per_pixel = 32;
234 server_init_msg.pixel_format.is_big_endian = 0;
235 server_init_msg.pixel_format.is_true_color = 1;
236 server_init_msg.pixel_format.red_max = 255;
237 server_init_msg.pixel_format.green_max = 255;
238 server_init_msg.pixel_format.blue_max = 255;
239 server_init_msg.pixel_format.red_shift = 16;
240 server_init_msg.pixel_format.green_shift = 8;
241 server_init_msg.pixel_format.blue_shift = 0;
242 server_init_msg.name_length = 0;
243 LOG(DEBUG) << "size: " << sizeof(server_init_msg);
244 // TODO(ed) this is ugly. Crow should really have a span type
245 // interface
246 // to avoid the copy, but alas, today it does not.
247 std::string s(reinterpret_cast<char*>(&server_init_msg),
248 sizeof(server_init_msg));
249 LOG(DEBUG) << "s.size() " << s.size();
250 conn.send_binary(s);
251 meta.vnc_state = VncState::MAIN_LOOP;
252 } break;
253 case VncState::MAIN_LOOP: {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700254 if (data.size() >= sizeof(client_to_server_msg_type)) {
255 auto type = static_cast<client_to_server_msg_type>(data[0]);
256 LOG(DEBUG) << "Received client message type " << (uint32_t)type
257 << "\n";
Ed Tanousc81ca422017-03-21 16:18:49 -0700258 switch (type) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700259 case client_to_server_msg_type::set_pixel_format: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700260 } break;
261
Ed Tanousb4d29f42017-03-24 16:39:25 -0700262 case client_to_server_msg_type::fix_color_map_entries: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700263 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700264 case client_to_server_msg_type::set_encodings: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700265 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700266 case client_to_server_msg_type::framebuffer_update_request: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700267 // Make sure the buffer is long enough to handle what we're
268 // about to do
Ed Tanousb4d29f42017-03-24 16:39:25 -0700269 if (data.size() >= sizeof(frame_buffer_update_req) +
270 sizeof(client_to_server_msg_type)) {
271 auto msg = reinterpret_cast<const frame_buffer_update_req*>(
272 data.data() + sizeof(client_to_server_msg_type));
Ed Tanousc81ca422017-03-21 16:18:49 -0700273
Ed Tanous93f987d2017-04-17 17:52:36 -0700274 // Todo(ed) lifecycle of the video puller and decoder
275 // should be
276 // with the websocket, not recreated every time
277 AstVideo::VideoPuller p;
278 p.initialize();
279 auto out = p.read_video();
280 AstVideo::AstJpegDecoder d;
281 d.decode(out.buffer, out.width, out.height, out.mode,
282 out.y_selector, out.uv_selector);
283
Ed Tanousb4d29f42017-03-24 16:39:25 -0700284 framebuffer_update_msg buffer_update_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700285
286 // If the viewer is requesting a full update, force write
287 // of all pixels
288
289 framebuffer_rectangle this_rect;
290 this_rect.x = msg->x_position;
291 this_rect.y = msg->y_position;
Ed Tanous93f987d2017-04-17 17:52:36 -0700292 this_rect.width = out.width;
293 this_rect.height = out.height;
Ed Tanousc81ca422017-03-21 16:18:49 -0700294 this_rect.encoding =
295 static_cast<uint8_t>(encoding_type::raw);
Ed Tanous93f987d2017-04-17 17:52:36 -0700296 LOG(DEBUG) << "Encoding is " << this_rect.encoding;
Ed Tanousc81ca422017-03-21 16:18:49 -0700297 this_rect.data.reserve(this_rect.width *
298 this_rect.height * 4);
Ed Tanous93f987d2017-04-17 17:52:36 -0700299 LOG(DEBUG) << "Width " << out.width << " Height "
300 << out.height;
Ed Tanousc81ca422017-03-21 16:18:49 -0700301
Ed Tanous93f987d2017-04-17 17:52:36 -0700302 for (int i = 0; i < out.width * out.height; i++) {
303 auto& pixel = d.OutBuffer[i];
304 this_rect.data.push_back(pixel.B);
305 this_rect.data.push_back(pixel.G);
306 this_rect.data.push_back(pixel.R);
307 this_rect.data.push_back(0);
Ed Tanousc81ca422017-03-21 16:18:49 -0700308 }
309
Ed Tanousb4d29f42017-03-24 16:39:25 -0700310 buffer_update_msg.rectangles.push_back(
Ed Tanousc81ca422017-03-21 16:18:49 -0700311 std::move(this_rect));
Ed Tanousb4d29f42017-03-24 16:39:25 -0700312 auto serialized = serialize(buffer_update_msg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700313
314 conn.send_binary(serialized);
Ed Tanous01250f22017-04-18 17:49:51 -0700315
Ed Tanous93f987d2017-04-17 17:52:36 -0700316 } // TODO(Ed) handle error
Ed Tanousc81ca422017-03-21 16:18:49 -0700317
318 }
319
320 break;
321
Ed Tanousb4d29f42017-03-24 16:39:25 -0700322 case client_to_server_msg_type::key_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700323 } break;
324
Ed Tanousb4d29f42017-03-24 16:39:25 -0700325 case client_to_server_msg_type::pointer_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700326 } break;
327
Ed Tanousb4d29f42017-03-24 16:39:25 -0700328 case client_to_server_msg_type::client_cut_text: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700329 } break;
330
331 default:
332 break;
333 }
334 }
335
336 } break;
337 case VncState::UNSTARTED:
338 // Error? TODO
339 break;
340 }
341
342 });
343}
344}
345}