Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 1 | #include <crow/app.h> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 2 | #include <boost/endian/arithmetic.hpp> |
| 3 | #include <string> |
| 4 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 5 | #include <ast_jpeg_decoder.hpp> |
| 6 | #include <ast_video_puller.hpp> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 7 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 8 | namespace crow { |
| 9 | namespace kvm { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 10 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 11 | static const std::string rfb_3_3_version_string = "RFB 003.003\n"; |
| 12 | static const std::string rfb_3_7_version_string = "RFB 003.007\n"; |
| 13 | static const std::string rfb_3_8_version_string = "RFB 003.008\n"; |
| 14 | |
| 15 | enum class RfbAuthScheme : uint8_t { |
| 16 | connection_failed = 0, |
| 17 | no_authentication = 1, |
| 18 | vnc_authentication = 2 |
| 19 | }; |
| 20 | |
| 21 | struct 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 37 | struct server_initialization_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 38 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 44 | enum class client_to_server_msg_type : uint8_t { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 45 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 54 | struct set_pixel_format_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 55 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 61 | struct frame_buffer_update_req { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 62 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 69 | struct key_event_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 70 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 76 | struct pointer_event_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 77 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 82 | struct client_cut_text_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 83 | std::vector<uint8_t> data; |
| 84 | }; |
| 85 | |
| 86 | enum 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 | |
| 114 | struct 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 123 | struct framebuffer_update_msg { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 124 | boost::endian::big_uint8_t message_type; |
| 125 | std::vector<framebuffer_rectangle> rectangles; |
| 126 | }; |
| 127 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 128 | std::string serialize(const framebuffer_update_msg& msg) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 129 | // 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 | |
| 159 | enum class VncState { |
| 160 | UNSTARTED, |
| 161 | AWAITING_CLIENT_VERSION, |
| 162 | AWAITING_CLIENT_AUTH_METHOD, |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 163 | AWAITING_CLIENT_INIT_msg, |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 164 | MAIN_LOOP |
| 165 | }; |
| 166 | |
| 167 | class connection_metadata { |
| 168 | public: |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 169 | connection_metadata(void) : vnc_state(VncState::UNSTARTED){}; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 170 | |
| 171 | VncState vnc_state; |
| 172 | }; |
| 173 | |
| 174 | typedef std::vector<connection_metadata> meta_list; |
| 175 | meta_list connection_states(10); |
| 176 | |
| 177 | connection_metadata meta; |
| 178 | |
Ed Tanous | 7d3dba4 | 2017-04-05 13:04:39 -0700 | [diff] [blame] | 179 | template <typename... Middlewares> |
| 180 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 181 | CROW_ROUTE(app, "/kvmws") |
| 182 | .websocket() |
| 183 | .onopen([&](crow::websocket::connection& conn) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 184 | if (meta.vnc_state == VncState::UNSTARTED) { |
| 185 | meta.vnc_state = VncState::AWAITING_CLIENT_VERSION; |
| 186 | conn.send_binary(rfb_3_8_version_string); |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame^] | 187 | } else { // SHould never happen |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 188 | conn.close(); |
| 189 | } |
| 190 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 191 | }) |
| 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 220 | meta.vnc_state = VncState::AWAITING_CLIENT_INIT_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 221 | } 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 228 | case VncState::AWAITING_CLIENT_INIT_msg: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 229 | // Now send the server initialization |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 230 | server_initialization_msg server_init_msg; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 231 | server_init_msg.framebuffer_width = 800; |
| 232 | server_init_msg.framebuffer_height = 600; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 233 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 254 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 258 | switch (type) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 259 | case client_to_server_msg_type::set_pixel_format: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 260 | } break; |
| 261 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 262 | case client_to_server_msg_type::fix_color_map_entries: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 263 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 264 | case client_to_server_msg_type::set_encodings: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 265 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 266 | case client_to_server_msg_type::framebuffer_update_request: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 267 | // Make sure the buffer is long enough to handle what we're |
| 268 | // about to do |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 269 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 273 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 274 | // 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 284 | framebuffer_update_msg buffer_update_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 285 | |
| 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 Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 292 | this_rect.width = out.width; |
| 293 | this_rect.height = out.height; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 294 | this_rect.encoding = |
| 295 | static_cast<uint8_t>(encoding_type::raw); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 296 | LOG(DEBUG) << "Encoding is " << this_rect.encoding; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 297 | this_rect.data.reserve(this_rect.width * |
| 298 | this_rect.height * 4); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 299 | LOG(DEBUG) << "Width " << out.width << " Height " |
| 300 | << out.height; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 301 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 302 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 310 | buffer_update_msg.rectangles.push_back( |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 311 | std::move(this_rect)); |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 312 | auto serialized = serialize(buffer_update_msg); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 313 | |
| 314 | conn.send_binary(serialized); |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame^] | 315 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 316 | } // TODO(Ed) handle error |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 317 | |
| 318 | } |
| 319 | |
| 320 | break; |
| 321 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 322 | case client_to_server_msg_type::key_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 323 | } break; |
| 324 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 325 | case client_to_server_msg_type::pointer_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 326 | } break; |
| 327 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 328 | case client_to_server_msg_type::client_cut_text: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 329 | } 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 | } |