blob: 65fe812e6974cb43bab861b6a6d126d29bb213dd [file] [log] [blame]
Ed Tanousb4d29f42017-03-24 16:39:25 -07001#include <boost/endian/arithmetic.hpp>
2#include <string>
3
Ed Tanous8041f312017-04-03 09:47:01 -07004#include <crow/bmc_app_type.hpp>
Ed Tanousb4d29f42017-03-24 16:39:25 -07005
6#include <video.h>
7
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
179void request_routes(BmcAppType& app) {
180 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}