Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 1 | #include <boost/endian/arithmetic.hpp> |
| 2 | #include <string> |
| 3 | |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 4 | #include <crow/bmc_app_type.hpp> |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 5 | |
| 6 | #include <video.h> |
| 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 | |
| 179 | void request_routes(BmcAppType& app) { |
| 180 | CROW_ROUTE(app, "/kvmws") |
| 181 | .websocket() |
| 182 | .onopen([&](crow::websocket::connection& conn) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 183 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 190 | }) |
| 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 219 | meta.vnc_state = VncState::AWAITING_CLIENT_INIT_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 220 | } 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 227 | case VncState::AWAITING_CLIENT_INIT_msg: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 228 | // Now send the server initialization |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 229 | server_initialization_msg server_init_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 230 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 253 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 257 | switch (type) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 258 | case client_to_server_msg_type::set_pixel_format: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 259 | } break; |
| 260 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 261 | case client_to_server_msg_type::fix_color_map_entries: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 262 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 263 | case client_to_server_msg_type::set_encodings: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 264 | } break; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 265 | case client_to_server_msg_type::framebuffer_update_request: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 266 | // Make sure the buffer is long enough to handle what we're |
| 267 | // about to do |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 268 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 272 | |
| 273 | if (!msg->incremental) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 274 | framebuffer_update_msg buffer_update_msg; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 275 | |
| 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 305 | buffer_update_msg.rectangles.push_back( |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 306 | std::move(this_rect)); |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 307 | auto serialized = serialize(buffer_update_msg); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 308 | |
| 309 | conn.send_binary(serialized); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | } |
| 314 | |
| 315 | break; |
| 316 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 317 | case client_to_server_msg_type::key_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 318 | } break; |
| 319 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 320 | case client_to_server_msg_type::pointer_event: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 321 | } break; |
| 322 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 323 | case client_to_server_msg_type::client_cut_text: { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 324 | } 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 | } |