blob: 82cb488c5fcb1b57e77241abc371b3c6d262625f [file] [log] [blame]
Ed Tanouseb1b8622017-04-25 14:11:46 -07001#include <string>
Ed Tanous7d3dba42017-04-05 13:04:39 -07002#include <crow/app.h>
Ed Tanousb4d29f42017-03-24 16:39:25 -07003#include <boost/endian/arithmetic.hpp>
Ed Tanousb4d29f42017-03-24 16:39:25 -07004
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 Tanouseb1b8622017-04-25 14:11:46 -070054enum class server_to_client_message_type : uint8_t {
55 framebuffer_update = 0,
56 set_color_map_entries = 1,
57 bell_message = 2,
58 server_cut_text = 3
Ed Tanous9140a672017-04-24 17:01:32 -070059};
60
Ed Tanousb4d29f42017-03-24 16:39:25 -070061struct set_pixel_format_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070062 boost::endian::big_uint8_t pad1;
63 boost::endian::big_uint8_t pad2;
64 boost::endian::big_uint8_t pad3;
65 pixel_format_struct pixel_format;
66};
67
Ed Tanousb4d29f42017-03-24 16:39:25 -070068struct frame_buffer_update_req {
Ed Tanousc81ca422017-03-21 16:18:49 -070069 boost::endian::big_uint8_t incremental;
70 boost::endian::big_uint16_t x_position;
71 boost::endian::big_uint16_t y_position;
72 boost::endian::big_uint16_t width;
73 boost::endian::big_uint16_t height;
74};
75
Ed Tanousb4d29f42017-03-24 16:39:25 -070076struct key_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070077 boost::endian::big_uint8_t down_flag;
78 boost::endian::big_uint8_t pad1;
79 boost::endian::big_uint8_t pad2;
80 boost::endian::big_uint32_t key;
81};
82
Ed Tanousb4d29f42017-03-24 16:39:25 -070083struct pointer_event_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070084 boost::endian::big_uint8_t button_mask;
85 boost::endian::big_uint16_t x_position;
86 boost::endian::big_uint16_t y_position;
87};
88
Ed Tanousb4d29f42017-03-24 16:39:25 -070089struct client_cut_text_msg {
Ed Tanousc81ca422017-03-21 16:18:49 -070090 std::vector<uint8_t> data;
91};
92
93enum class encoding_type : uint32_t {
94 raw = 0x00,
95 copy_rectangle = 0x01,
96 rising_rectangle = 0x02,
97 corre = 0x04,
98 hextile = 0x05,
99 zlib = 0x06,
100 tight = 0x07,
101 zlibhex = 0x08,
102 ultra = 0x09,
103 zrle = 0x10,
104 zywrle = 0x011,
105 cache_enable = 0xFFFF0001,
106 xor_enable = 0xFFFF0006,
107 server_state_ultranvc = 0xFFFF8000,
108 enable_keep_alive = 0xFFFF8001,
109 enableftp_protocol_version = 0xFFFF8002,
110 tight_compress_level_0 = 0xFFFFFF00,
111 tight_compress_level_9 = 0xFFFFFF09,
112 x_cursor = 0xFFFFFF10,
113 rich_cursor = 0xFFFFFF11,
114 pointer_pos = 0xFFFFFF18,
115 last_rect = 0xFFFFFF20,
116 new_framebuffer_size = 0xFFFFFF21,
117 tight_quality_level_0 = 0xFFFFFFE0,
118 tight_quality_level_9 = 0xFFFFFFE9
119};
120
121struct framebuffer_rectangle {
Ed Tanous911ac312017-08-15 09:37:42 -0700122 boost::endian::big_uint16_t x{};
123 boost::endian::big_uint16_t y{};
124 boost::endian::big_uint16_t width{};
125 boost::endian::big_uint16_t height{};
126 boost::endian::big_uint32_t encoding{};
Ed Tanousc81ca422017-03-21 16:18:49 -0700127 std::vector<uint8_t> data;
128};
129
Ed Tanousb4d29f42017-03-24 16:39:25 -0700130struct framebuffer_update_msg {
Ed Tanous911ac312017-08-15 09:37:42 -0700131 boost::endian::big_uint8_t message_type{};
Ed Tanousc81ca422017-03-21 16:18:49 -0700132 std::vector<framebuffer_rectangle> rectangles;
133};
134
Ed Tanous911ac312017-08-15 09:37:42 -0700135inline std::string serialize(const framebuffer_update_msg& msg) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700136 // calculate the size of the needed vector for serialization
137 size_t vector_size = 4;
138 for (const auto& rect : msg.rectangles) {
139 vector_size += 12 + rect.data.size();
140 }
141
142 std::string serialized(vector_size, 0);
143
144 size_t i = 0;
Ed Tanouseb1b8622017-04-25 14:11:46 -0700145 serialized[i++] = static_cast<char>(
146 server_to_client_message_type::framebuffer_update); // Type
147 serialized[i++] = 0; // Pad byte
Ed Tanousc81ca422017-03-21 16:18:49 -0700148 boost::endian::big_uint16_t number_of_rectangles = msg.rectangles.size();
149 std::memcpy(&serialized[i], &number_of_rectangles,
150 sizeof(number_of_rectangles));
151 i += sizeof(number_of_rectangles);
152
153 for (const auto& rect : msg.rectangles) {
154 // copy the first part of the struct
155 size_t buffer_size =
156 sizeof(framebuffer_rectangle) - sizeof(std::vector<uint8_t>);
157 std::memcpy(&serialized[i], &rect, buffer_size);
158 i += buffer_size;
159
160 std::memcpy(&serialized[i], rect.data.data(), rect.data.size());
161 i += rect.data.size();
162 }
163
164 return serialized;
165}
166
167enum class VncState {
168 UNSTARTED,
169 AWAITING_CLIENT_VERSION,
170 AWAITING_CLIENT_AUTH_METHOD,
Ed Tanousb4d29f42017-03-24 16:39:25 -0700171 AWAITING_CLIENT_INIT_msg,
Ed Tanousc81ca422017-03-21 16:18:49 -0700172 MAIN_LOOP
173};
174
175class connection_metadata {
176 public:
Ed Tanous911ac312017-08-15 09:37:42 -0700177 connection_metadata() {};
Ed Tanousc81ca422017-03-21 16:18:49 -0700178
Ed Tanous911ac312017-08-15 09:37:42 -0700179 VncState vnc_state{VncState::UNSTARTED};
Ed Tanousc81ca422017-03-21 16:18:49 -0700180};
181
Ed Tanous911ac312017-08-15 09:37:42 -0700182using meta_list = std::vector<connection_metadata>;
Ed Tanousc81ca422017-03-21 16:18:49 -0700183meta_list connection_states(10);
184
185connection_metadata meta;
186
Ed Tanous7d3dba42017-04-05 13:04:39 -0700187template <typename... Middlewares>
188void request_routes(Crow<Middlewares...>& app) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700189 CROW_ROUTE(app, "/kvmws")
190 .websocket()
191 .onopen([&](crow::websocket::connection& conn) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700192 if (meta.vnc_state == VncState::UNSTARTED) {
193 meta.vnc_state = VncState::AWAITING_CLIENT_VERSION;
194 conn.send_binary(rfb_3_8_version_string);
Ed Tanous01250f22017-04-18 17:49:51 -0700195 } else { // SHould never happen
Ed Tanousb4d29f42017-03-24 16:39:25 -0700196 conn.close();
197 }
198
Ed Tanousc81ca422017-03-21 16:18:49 -0700199 })
200 .onclose(
201 [&](crow::websocket::connection& conn, const std::string& reason) {
202 meta.vnc_state = VncState::UNSTARTED;
203 })
204 .onmessage([&](crow::websocket::connection& conn, const std::string& data,
205 bool is_binary) {
206 switch (meta.vnc_state) {
207 case VncState::AWAITING_CLIENT_VERSION: {
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700208 std::cout << "Client sent: " << data;
Ed Tanousc81ca422017-03-21 16:18:49 -0700209 if (data == rfb_3_8_version_string ||
210 data == rfb_3_7_version_string) {
211 std::string auth_types{1,
212 (uint8_t)RfbAuthScheme::no_authentication};
213 conn.send_binary(auth_types);
214 meta.vnc_state = VncState::AWAITING_CLIENT_AUTH_METHOD;
215 } else if (data == rfb_3_3_version_string) {
216 // TODO(ed) Support older protocols
217 meta.vnc_state = VncState::UNSTARTED;
218 conn.close();
219 } else {
220 // TODO(ed) Support older protocols
221 meta.vnc_state = VncState::UNSTARTED;
222 conn.close();
223 }
224 } break;
225 case VncState::AWAITING_CLIENT_AUTH_METHOD: {
226 std::string security_result{{0, 0, 0, 0}};
227 if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700228 meta.vnc_state = VncState::AWAITING_CLIENT_INIT_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700229 } else {
230 // Mark auth as failed
231 security_result[3] = 1;
232 meta.vnc_state = VncState::UNSTARTED;
233 }
234 conn.send_binary(security_result);
235 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700236 case VncState::AWAITING_CLIENT_INIT_msg: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700237 // Now send the server initialization
Ed Tanous911ac312017-08-15 09:37:42 -0700238 server_initialization_msg server_init_msg{};
Ed Tanous93f987d2017-04-17 17:52:36 -0700239 server_init_msg.framebuffer_width = 800;
240 server_init_msg.framebuffer_height = 600;
Ed Tanousc81ca422017-03-21 16:18:49 -0700241 server_init_msg.pixel_format.bits_per_pixel = 32;
242 server_init_msg.pixel_format.is_big_endian = 0;
243 server_init_msg.pixel_format.is_true_color = 1;
244 server_init_msg.pixel_format.red_max = 255;
245 server_init_msg.pixel_format.green_max = 255;
246 server_init_msg.pixel_format.blue_max = 255;
247 server_init_msg.pixel_format.red_shift = 16;
248 server_init_msg.pixel_format.green_shift = 8;
249 server_init_msg.pixel_format.blue_shift = 0;
250 server_init_msg.name_length = 0;
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700251 std::cout << "size: " << sizeof(server_init_msg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700252 // TODO(ed) this is ugly. Crow should really have a span type
253 // interface
254 // to avoid the copy, but alas, today it does not.
255 std::string s(reinterpret_cast<char*>(&server_init_msg),
256 sizeof(server_init_msg));
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700257 std::cout << "s.size() " << s.size();
Ed Tanousc81ca422017-03-21 16:18:49 -0700258 conn.send_binary(s);
259 meta.vnc_state = VncState::MAIN_LOOP;
260 } break;
261 case VncState::MAIN_LOOP: {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700262 if (data.size() >= sizeof(client_to_server_msg_type)) {
263 auto type = static_cast<client_to_server_msg_type>(data[0]);
Ed Tanous911ac312017-08-15 09:37:42 -0700264 std::cout << "Received client message type "
265 << static_cast<std::size_t>(type) << "\n";
Ed Tanousc81ca422017-03-21 16:18:49 -0700266 switch (type) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700267 case client_to_server_msg_type::set_pixel_format: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700268 } break;
269
Ed Tanousb4d29f42017-03-24 16:39:25 -0700270 case client_to_server_msg_type::fix_color_map_entries: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700271 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700272 case client_to_server_msg_type::set_encodings: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700273 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700274 case client_to_server_msg_type::framebuffer_update_request: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700275 // Make sure the buffer is long enough to handle what we're
276 // about to do
Ed Tanousb4d29f42017-03-24 16:39:25 -0700277 if (data.size() >= sizeof(frame_buffer_update_req) +
278 sizeof(client_to_server_msg_type)) {
279 auto msg = reinterpret_cast<const frame_buffer_update_req*>(
Ed Tanous911ac312017-08-15 09:37:42 -0700280 data.data() + // NOLINT
281 sizeof(client_to_server_msg_type));
282 // TODO(ed) find a better way to do this deserialization
Ed Tanousc81ca422017-03-21 16:18:49 -0700283
Ed Tanous9140a672017-04-24 17:01:32 -0700284 // Todo(ed) lifecycle of the video puller and decoder
285 // should be
286 // with the websocket, not recreated every time
Ed Tanouse2fc45a2017-04-26 09:19:10 -0700287 AstVideo::SimpleVideoPuller p;
Ed Tanous9140a672017-04-24 17:01:32 -0700288 p.initialize();
289 auto out = p.read_video();
290 AstVideo::AstJpegDecoder d;
291 d.decode(out.buffer, out.width, out.height, out.mode,
292 out.y_selector, out.uv_selector);
Ed Tanous93f987d2017-04-17 17:52:36 -0700293
Ed Tanous9140a672017-04-24 17:01:32 -0700294 framebuffer_update_msg buffer_update_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700295
Ed Tanous9140a672017-04-24 17:01:32 -0700296 // If the viewer is requesting a full update, force write
297 // of all pixels
Ed Tanousc81ca422017-03-21 16:18:49 -0700298
Ed Tanous9140a672017-04-24 17:01:32 -0700299 framebuffer_rectangle this_rect;
300 this_rect.x = msg->x_position;
301 this_rect.y = msg->y_position;
302 this_rect.width = out.width;
303 this_rect.height = out.height;
304 this_rect.encoding =
305 static_cast<uint8_t>(encoding_type::raw);
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700306 std::cout << "Encoding is " << this_rect.encoding;
Ed Tanous911ac312017-08-15 09:37:42 -0700307 this_rect.data.reserve(
308 static_cast<std::size_t>(this_rect.width) *
309 static_cast<std::size_t>(this_rect.height) * 4);
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700310 std::cout << "Width " << out.width << " Height "
Ed Tanous911ac312017-08-15 09:37:42 -0700311 << out.height;
Ed Tanousc81ca422017-03-21 16:18:49 -0700312
Ed Tanous9140a672017-04-24 17:01:32 -0700313 for (int i = 0; i < out.width * out.height; i++) {
314 auto& pixel = d.OutBuffer[i];
315 this_rect.data.push_back(pixel.B);
316 this_rect.data.push_back(pixel.G);
317 this_rect.data.push_back(pixel.R);
318 this_rect.data.push_back(0);
319 }
Ed Tanousc81ca422017-03-21 16:18:49 -0700320
Ed Tanous9140a672017-04-24 17:01:32 -0700321 buffer_update_msg.rectangles.push_back(
322 std::move(this_rect));
323 auto serialized = serialize(buffer_update_msg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700324
Ed Tanous9140a672017-04-24 17:01:32 -0700325 conn.send_binary(serialized);
Ed Tanous01250f22017-04-18 17:49:51 -0700326
Ed Tanous93f987d2017-04-17 17:52:36 -0700327 } // TODO(Ed) handle error
Ed Tanousc81ca422017-03-21 16:18:49 -0700328
329 }
330
331 break;
332
Ed Tanousb4d29f42017-03-24 16:39:25 -0700333 case client_to_server_msg_type::key_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700334 } break;
335
Ed Tanousb4d29f42017-03-24 16:39:25 -0700336 case client_to_server_msg_type::pointer_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700337 } break;
338
Ed Tanousb4d29f42017-03-24 16:39:25 -0700339 case client_to_server_msg_type::client_cut_text: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700340 } break;
341
342 default:
343 break;
344 }
345 }
346
347 } break;
348 case VncState::UNSTARTED:
349 // Error? TODO
350 break;
351 }
352
353 });
354}
Ed Tanous911ac312017-08-15 09:37:42 -0700355} // namespace kvm
356} // namespace crow