blob: ad4b352eb48a9b24a043489e86af6ff0a97a0914 [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 Tanous55c7b7a2018-05-22 15:27:24 -070011static const std::string rfb33VersionString = "RFB 003.003\n";
12static const std::string rfb37VersionString = "RFB 003.007\n";
13static const std::string rfb38VersionString = "RFB 003.008\n";
Ed Tanousc81ca422017-03-21 16:18:49 -070014
15enum class RfbAuthScheme : uint8_t {
16 connection_failed = 0,
17 no_authentication = 1,
18 vnc_authentication = 2
19};
20
Ed Tanous55c7b7a2018-05-22 15:27:24 -070021struct PixelFormatStruct {
22 boost::endian::big_uint8_t bitsPerPixel;
Ed Tanousc81ca422017-03-21 16:18:49 -070023 boost::endian::big_uint8_t depth;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070024 boost::endian::big_uint8_t isBigEndian;
25 boost::endian::big_uint8_t isTrueColor;
26 boost::endian::big_uint16_t redMax;
27 boost::endian::big_uint16_t greenMax;
28 boost::endian::big_uint16_t blueMax;
29 boost::endian::big_uint8_t redShift;
30 boost::endian::big_uint8_t greenShift;
31 boost::endian::big_uint8_t blueShift;
Ed Tanousc81ca422017-03-21 16:18:49 -070032 boost::endian::big_uint8_t pad1;
33 boost::endian::big_uint8_t pad2;
34 boost::endian::big_uint8_t pad3;
35};
36
Ed Tanous55c7b7a2018-05-22 15:27:24 -070037struct ServerInitializationMsg {
38 boost::endian::big_uint16_t framebufferWidth;
39 boost::endian::big_uint16_t framebufferHeight;
40 PixelFormatStruct pixelFormat;
41 boost::endian::big_uint32_t nameLength;
Ed Tanousc81ca422017-03-21 16:18:49 -070042};
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 Tanous55c7b7a2018-05-22 15:27:24 -070061struct SetPixelFormatMsg {
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;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070065 PixelFormatStruct pixelFormat;
Ed Tanousc81ca422017-03-21 16:18:49 -070066};
67
Ed Tanous55c7b7a2018-05-22 15:27:24 -070068struct FrameBufferUpdateReq {
Ed Tanousc81ca422017-03-21 16:18:49 -070069 boost::endian::big_uint8_t incremental;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070070 boost::endian::big_uint16_t xPosition;
71 boost::endian::big_uint16_t yPosition;
Ed Tanousc81ca422017-03-21 16:18:49 -070072 boost::endian::big_uint16_t width;
73 boost::endian::big_uint16_t height;
74};
75
Ed Tanous55c7b7a2018-05-22 15:27:24 -070076struct KeyEventMsg {
77 boost::endian::big_uint8_t downFlag;
Ed Tanousc81ca422017-03-21 16:18:49 -070078 boost::endian::big_uint8_t pad1;
79 boost::endian::big_uint8_t pad2;
80 boost::endian::big_uint32_t key;
81};
82
Ed Tanous55c7b7a2018-05-22 15:27:24 -070083struct PointerEventMsg {
84 boost::endian::big_uint8_t buttonMask;
85 boost::endian::big_uint16_t xPosition;
86 boost::endian::big_uint16_t yPosition;
Ed Tanousc81ca422017-03-21 16:18:49 -070087};
88
Ed Tanous55c7b7a2018-05-22 15:27:24 -070089struct ClientCutTextMsg {
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,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700108 enable_keepAlive = 0xFFFF8001,
Ed Tanousc81ca422017-03-21 16:18:49 -0700109 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
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700121struct FramebufferRectangle {
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 Tanous55c7b7a2018-05-22 15:27:24 -0700130struct FramebufferUpdateMsg {
131 boost::endian::big_uint8_t messageType{};
132 std::vector<FramebufferRectangle> rectangles;
Ed Tanousc81ca422017-03-21 16:18:49 -0700133};
134
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700135inline std::string serialize(const FramebufferUpdateMsg& msg) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700136 // calculate the size of the needed vector for serialization
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700137 size_t vectorSize = 4;
Ed Tanousc81ca422017-03-21 16:18:49 -0700138 for (const auto& rect : msg.rectangles) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700139 vectorSize += 12 + rect.data.size();
Ed Tanousc81ca422017-03-21 16:18:49 -0700140 }
141
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700142 std::string serialized(vectorSize, 0);
Ed Tanousc81ca422017-03-21 16:18:49 -0700143
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 Tanous55c7b7a2018-05-22 15:27:24 -0700148 boost::endian::big_uint16_t numberOfRectangles = msg.rectangles.size();
149 std::memcpy(&serialized[i], &numberOfRectangles, sizeof(numberOfRectangles));
150 i += sizeof(numberOfRectangles);
Ed Tanousc81ca422017-03-21 16:18:49 -0700151
152 for (const auto& rect : msg.rectangles) {
153 // copy the first part of the struct
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700154 size_t bufferSize =
155 sizeof(FramebufferRectangle) - sizeof(std::vector<uint8_t>);
156 std::memcpy(&serialized[i], &rect, bufferSize);
157 i += bufferSize;
Ed Tanousc81ca422017-03-21 16:18:49 -0700158
159 std::memcpy(&serialized[i], rect.data.data(), rect.data.size());
160 i += rect.data.size();
161 }
162
163 return serialized;
164}
165
166enum class VncState {
167 UNSTARTED,
168 AWAITING_CLIENT_VERSION,
169 AWAITING_CLIENT_AUTH_METHOD,
Ed Tanousb4d29f42017-03-24 16:39:25 -0700170 AWAITING_CLIENT_INIT_msg,
Ed Tanousc81ca422017-03-21 16:18:49 -0700171 MAIN_LOOP
172};
173
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700174class ConnectionMetadata {
Ed Tanousc81ca422017-03-21 16:18:49 -0700175 public:
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700176 ConnectionMetadata(){};
Ed Tanousc81ca422017-03-21 16:18:49 -0700177
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700178 VncState vncState{VncState::UNSTARTED};
Ed Tanousc81ca422017-03-21 16:18:49 -0700179};
180
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700181using meta_list = std::vector<ConnectionMetadata>;
182meta_list connectionStates(10);
Ed Tanousc81ca422017-03-21 16:18:49 -0700183
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700184ConnectionMetadata meta;
Ed Tanousc81ca422017-03-21 16:18:49 -0700185
Ed Tanous7d3dba42017-04-05 13:04:39 -0700186template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700187void requestRoutes(Crow<Middlewares...>& app) {
188 BMCWEB_ROUTE(app, "/kvmws")
Ed Tanousc81ca422017-03-21 16:18:49 -0700189 .websocket()
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700190 .onopen([&](crow::websocket::Connection& conn) {
191 if (meta.vncState == VncState::UNSTARTED) {
192 meta.vncState = VncState::AWAITING_CLIENT_VERSION;
193 conn.sendBinary(rfb38VersionString);
Ed Tanous01250f22017-04-18 17:49:51 -0700194 } else { // SHould never happen
Ed Tanousb4d29f42017-03-24 16:39:25 -0700195 conn.close();
196 }
197
Ed Tanousc81ca422017-03-21 16:18:49 -0700198 })
199 .onclose(
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700200 [&](crow::websocket::Connection& conn, const std::string& reason) {
201 meta.vncState = VncState::UNSTARTED;
Ed Tanousc81ca422017-03-21 16:18:49 -0700202 })
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700203 .onmessage([&](crow::websocket::Connection& conn, const std::string& data,
Ed Tanousc81ca422017-03-21 16:18:49 -0700204 bool is_binary) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700205 switch (meta.vncState) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700206 case VncState::AWAITING_CLIENT_VERSION: {
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700207 std::cout << "Client sent: " << data;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700208 if (data == rfb38VersionString || data == rfb37VersionString) {
209 std::string authTypes{1,
210 (uint8_t)RfbAuthScheme::no_authentication};
211 conn.sendBinary(authTypes);
212 meta.vncState = VncState::AWAITING_CLIENT_AUTH_METHOD;
213 } else if (data == rfb33VersionString) {
Ed Tanousc81ca422017-03-21 16:18:49 -0700214 // TODO(ed) Support older protocols
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700215 meta.vncState = VncState::UNSTARTED;
Ed Tanousc81ca422017-03-21 16:18:49 -0700216 conn.close();
217 } else {
218 // TODO(ed) Support older protocols
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700219 meta.vncState = VncState::UNSTARTED;
Ed Tanousc81ca422017-03-21 16:18:49 -0700220 conn.close();
221 }
222 } break;
223 case VncState::AWAITING_CLIENT_AUTH_METHOD: {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700224 std::string securityResult{{0, 0, 0, 0}};
Ed Tanousc81ca422017-03-21 16:18:49 -0700225 if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700226 meta.vncState = VncState::AWAITING_CLIENT_INIT_msg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700227 } else {
228 // Mark auth as failed
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700229 securityResult[3] = 1;
230 meta.vncState = VncState::UNSTARTED;
Ed Tanousc81ca422017-03-21 16:18:49 -0700231 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700232 conn.sendBinary(securityResult);
Ed Tanousc81ca422017-03-21 16:18:49 -0700233 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700234 case VncState::AWAITING_CLIENT_INIT_msg: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700235 // Now send the server initialization
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700236 ServerInitializationMsg serverInitMsg{};
237 serverInitMsg.framebufferWidth = 800;
238 serverInitMsg.framebufferHeight = 600;
239 serverInitMsg.pixelFormat.bitsPerPixel = 32;
240 serverInitMsg.pixelFormat.isBigEndian = 0;
241 serverInitMsg.pixelFormat.isTrueColor = 1;
242 serverInitMsg.pixelFormat.redMax = 255;
243 serverInitMsg.pixelFormat.greenMax = 255;
244 serverInitMsg.pixelFormat.blueMax = 255;
245 serverInitMsg.pixelFormat.redShift = 16;
246 serverInitMsg.pixelFormat.greenShift = 8;
247 serverInitMsg.pixelFormat.blueShift = 0;
248 serverInitMsg.nameLength = 0;
249 std::cout << "size: " << sizeof(serverInitMsg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700250 // TODO(ed) this is ugly. Crow should really have a span type
251 // interface
252 // to avoid the copy, but alas, today it does not.
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700253 std::string s(reinterpret_cast<char*>(&serverInitMsg),
254 sizeof(serverInitMsg));
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700255 std::cout << "s.size() " << s.size();
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700256 conn.sendBinary(s);
257 meta.vncState = VncState::MAIN_LOOP;
Ed Tanousc81ca422017-03-21 16:18:49 -0700258 } break;
259 case VncState::MAIN_LOOP: {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700260 if (data.size() >= sizeof(client_to_server_msg_type)) {
261 auto type = static_cast<client_to_server_msg_type>(data[0]);
Ed Tanous911ac312017-08-15 09:37:42 -0700262 std::cout << "Received client message type "
263 << static_cast<std::size_t>(type) << "\n";
Ed Tanousc81ca422017-03-21 16:18:49 -0700264 switch (type) {
Ed Tanousb4d29f42017-03-24 16:39:25 -0700265 case client_to_server_msg_type::set_pixel_format: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700266 } break;
267
Ed Tanousb4d29f42017-03-24 16:39:25 -0700268 case client_to_server_msg_type::fix_color_map_entries: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700269 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700270 case client_to_server_msg_type::set_encodings: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700271 } break;
Ed Tanousb4d29f42017-03-24 16:39:25 -0700272 case client_to_server_msg_type::framebuffer_update_request: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700273 // Make sure the buffer is long enough to handle what we're
274 // about to do
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700275 if (data.size() >= sizeof(FrameBufferUpdateReq) +
Ed Tanousb4d29f42017-03-24 16:39:25 -0700276 sizeof(client_to_server_msg_type)) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700277 auto msg = reinterpret_cast<const FrameBufferUpdateReq*>(
278 data.data() + // NOLINT
Ed Tanous911ac312017-08-15 09:37:42 -0700279 sizeof(client_to_server_msg_type));
280 // TODO(ed) find a better way to do this deserialization
Ed Tanousc81ca422017-03-21 16:18:49 -0700281
Ed Tanous9140a672017-04-24 17:01:32 -0700282 // Todo(ed) lifecycle of the video puller and decoder
283 // should be
284 // with the websocket, not recreated every time
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700285 ast_video::SimpleVideoPuller p;
Ed Tanous9140a672017-04-24 17:01:32 -0700286 p.initialize();
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700287 auto out = p.readVideo();
288 ast_video::AstJpegDecoder d;
Ed Tanous9140a672017-04-24 17:01:32 -0700289 d.decode(out.buffer, out.width, out.height, out.mode,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700290 out.ySelector, out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -0700291
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700292 FramebufferUpdateMsg bufferUpdateMsg;
Ed Tanousc81ca422017-03-21 16:18:49 -0700293
Ed Tanous9140a672017-04-24 17:01:32 -0700294 // If the viewer is requesting a full update, force write
295 // of all pixels
Ed Tanousc81ca422017-03-21 16:18:49 -0700296
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700297 FramebufferRectangle thisRect;
298 thisRect.x = msg->xPosition;
299 thisRect.y = msg->yPosition;
300 thisRect.width = out.width;
301 thisRect.height = out.height;
302 thisRect.encoding =
Ed Tanous9140a672017-04-24 17:01:32 -0700303 static_cast<uint8_t>(encoding_type::raw);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700304 std::cout << "Encoding is " << thisRect.encoding;
305 thisRect.data.reserve(
306 static_cast<std::size_t>(thisRect.width) *
307 static_cast<std::size_t>(thisRect.height) * 4);
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700308 std::cout << "Width " << out.width << " Height "
Ed Tanous911ac312017-08-15 09:37:42 -0700309 << out.height;
Ed Tanousc81ca422017-03-21 16:18:49 -0700310
Ed Tanous9140a672017-04-24 17:01:32 -0700311 for (int i = 0; i < out.width * out.height; i++) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700312 auto& pixel = d.outBuffer[i];
313 thisRect.data.push_back(pixel.b);
314 thisRect.data.push_back(pixel.g);
315 thisRect.data.push_back(pixel.r);
316 thisRect.data.push_back(0);
Ed Tanous9140a672017-04-24 17:01:32 -0700317 }
Ed Tanousc81ca422017-03-21 16:18:49 -0700318
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700319 bufferUpdateMsg.rectangles.push_back(std::move(thisRect));
320 auto serialized = serialize(bufferUpdateMsg);
Ed Tanousc81ca422017-03-21 16:18:49 -0700321
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700322 conn.sendBinary(serialized);
Ed Tanous01250f22017-04-18 17:49:51 -0700323
Ed Tanous93f987d2017-04-17 17:52:36 -0700324 } // TODO(Ed) handle error
Ed Tanousc81ca422017-03-21 16:18:49 -0700325
326 }
327
328 break;
329
Ed Tanousb4d29f42017-03-24 16:39:25 -0700330 case client_to_server_msg_type::key_event: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700331 } break;
332
Ed Tanousb4d29f42017-03-24 16:39:25 -0700333 case client_to_server_msg_type::pointer_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::client_cut_text: {
Ed Tanousc81ca422017-03-21 16:18:49 -0700337 } break;
338
339 default:
340 break;
341 }
342 }
343
344 } break;
345 case VncState::UNSTARTED:
346 // Error? TODO
347 break;
348 }
349
350 });
351}
Ed Tanous911ac312017-08-15 09:37:42 -0700352} // namespace kvm
353} // namespace crow