summaryrefslogtreecommitdiff
path: root/include/web_kvm.hpp
blob: ad4b352eb48a9b24a043489e86af6ff0a97a0914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <string>
#include <crow/app.h>
#include <boost/endian/arithmetic.hpp>

#include <ast_jpeg_decoder.hpp>
#include <ast_video_puller.hpp>

namespace crow {
namespace kvm {

static const std::string rfb33VersionString = "RFB 003.003\n";
static const std::string rfb37VersionString = "RFB 003.007\n";
static const std::string rfb38VersionString = "RFB 003.008\n";

enum class RfbAuthScheme : uint8_t {
  connection_failed = 0,
  no_authentication = 1,
  vnc_authentication = 2
};

struct PixelFormatStruct {
  boost::endian::big_uint8_t bitsPerPixel;
  boost::endian::big_uint8_t depth;
  boost::endian::big_uint8_t isBigEndian;
  boost::endian::big_uint8_t isTrueColor;
  boost::endian::big_uint16_t redMax;
  boost::endian::big_uint16_t greenMax;
  boost::endian::big_uint16_t blueMax;
  boost::endian::big_uint8_t redShift;
  boost::endian::big_uint8_t greenShift;
  boost::endian::big_uint8_t blueShift;
  boost::endian::big_uint8_t pad1;
  boost::endian::big_uint8_t pad2;
  boost::endian::big_uint8_t pad3;
};

struct ServerInitializationMsg {
  boost::endian::big_uint16_t framebufferWidth;
  boost::endian::big_uint16_t framebufferHeight;
  PixelFormatStruct pixelFormat;
  boost::endian::big_uint32_t nameLength;
};

enum class client_to_server_msg_type : uint8_t {
  set_pixel_format = 0,
  fix_color_map_entries = 1,
  set_encodings = 2,
  framebuffer_update_request = 3,
  key_event = 4,
  pointer_event = 5,
  client_cut_text = 6
};

enum class server_to_client_message_type : uint8_t {
  framebuffer_update = 0,
  set_color_map_entries = 1,
  bell_message = 2,
  server_cut_text = 3
};

struct SetPixelFormatMsg {
  boost::endian::big_uint8_t pad1;
  boost::endian::big_uint8_t pad2;
  boost::endian::big_uint8_t pad3;
  PixelFormatStruct pixelFormat;
};

struct FrameBufferUpdateReq {
  boost::endian::big_uint8_t incremental;
  boost::endian::big_uint16_t xPosition;
  boost::endian::big_uint16_t yPosition;
  boost::endian::big_uint16_t width;
  boost::endian::big_uint16_t height;
};

struct KeyEventMsg {
  boost::endian::big_uint8_t downFlag;
  boost::endian::big_uint8_t pad1;
  boost::endian::big_uint8_t pad2;
  boost::endian::big_uint32_t key;
};

struct PointerEventMsg {
  boost::endian::big_uint8_t buttonMask;
  boost::endian::big_uint16_t xPosition;
  boost::endian::big_uint16_t yPosition;
};

struct ClientCutTextMsg {
  std::vector<uint8_t> data;
};

enum class encoding_type : uint32_t {
  raw = 0x00,
  copy_rectangle = 0x01,
  rising_rectangle = 0x02,
  corre = 0x04,
  hextile = 0x05,
  zlib = 0x06,
  tight = 0x07,
  zlibhex = 0x08,
  ultra = 0x09,
  zrle = 0x10,
  zywrle = 0x011,
  cache_enable = 0xFFFF0001,
  xor_enable = 0xFFFF0006,
  server_state_ultranvc = 0xFFFF8000,
  enable_keepAlive = 0xFFFF8001,
  enableftp_protocol_version = 0xFFFF8002,
  tight_compress_level_0 = 0xFFFFFF00,
  tight_compress_level_9 = 0xFFFFFF09,
  x_cursor = 0xFFFFFF10,
  rich_cursor = 0xFFFFFF11,
  pointer_pos = 0xFFFFFF18,
  last_rect = 0xFFFFFF20,
  new_framebuffer_size = 0xFFFFFF21,
  tight_quality_level_0 = 0xFFFFFFE0,
  tight_quality_level_9 = 0xFFFFFFE9
};

struct FramebufferRectangle {
  boost::endian::big_uint16_t x{};
  boost::endian::big_uint16_t y{};
  boost::endian::big_uint16_t width{};
  boost::endian::big_uint16_t height{};
  boost::endian::big_uint32_t encoding{};
  std::vector<uint8_t> data;
};

struct FramebufferUpdateMsg {
  boost::endian::big_uint8_t messageType{};
  std::vector<FramebufferRectangle> rectangles;
};

inline std::string serialize(const FramebufferUpdateMsg& msg) {
  // calculate the size of the needed vector for serialization
  size_t vectorSize = 4;
  for (const auto& rect : msg.rectangles) {
    vectorSize += 12 + rect.data.size();
  }

  std::string serialized(vectorSize, 0);

  size_t i = 0;
  serialized[i++] = static_cast<char>(
      server_to_client_message_type::framebuffer_update);  // Type
  serialized[i++] = 0;                                     // Pad byte
  boost::endian::big_uint16_t numberOfRectangles = msg.rectangles.size();
  std::memcpy(&serialized[i], &numberOfRectangles, sizeof(numberOfRectangles));
  i += sizeof(numberOfRectangles);

  for (const auto& rect : msg.rectangles) {
    // copy the first part of the struct
    size_t bufferSize =
        sizeof(FramebufferRectangle) - sizeof(std::vector<uint8_t>);
    std::memcpy(&serialized[i], &rect, bufferSize);
    i += bufferSize;

    std::memcpy(&serialized[i], rect.data.data(), rect.data.size());
    i += rect.data.size();
  }

  return serialized;
}

enum class VncState {
  UNSTARTED,
  AWAITING_CLIENT_VERSION,
  AWAITING_CLIENT_AUTH_METHOD,
  AWAITING_CLIENT_INIT_msg,
  MAIN_LOOP
};

class ConnectionMetadata {
 public:
  ConnectionMetadata(){};

  VncState vncState{VncState::UNSTARTED};
};

using meta_list = std::vector<ConnectionMetadata>;
meta_list connectionStates(10);

ConnectionMetadata meta;

template <typename... Middlewares>
void requestRoutes(Crow<Middlewares...>& app) {
  BMCWEB_ROUTE(app, "/kvmws")
      .websocket()
      .onopen([&](crow::websocket::Connection& conn) {
        if (meta.vncState == VncState::UNSTARTED) {
          meta.vncState = VncState::AWAITING_CLIENT_VERSION;
          conn.sendBinary(rfb38VersionString);
        } else {  // SHould never happen
          conn.close();
        }

      })
      .onclose(
          [&](crow::websocket::Connection& conn, const std::string& reason) {
            meta.vncState = VncState::UNSTARTED;
          })
      .onmessage([&](crow::websocket::Connection& conn, const std::string& data,
                     bool is_binary) {
        switch (meta.vncState) {
          case VncState::AWAITING_CLIENT_VERSION: {
            std::cout << "Client sent: " << data;
            if (data == rfb38VersionString || data == rfb37VersionString) {
              std::string authTypes{1,
                                    (uint8_t)RfbAuthScheme::no_authentication};
              conn.sendBinary(authTypes);
              meta.vncState = VncState::AWAITING_CLIENT_AUTH_METHOD;
            } else if (data == rfb33VersionString) {
              // TODO(ed)  Support older protocols
              meta.vncState = VncState::UNSTARTED;
              conn.close();
            } else {
              // TODO(ed)  Support older protocols
              meta.vncState = VncState::UNSTARTED;
              conn.close();
            }
          } break;
          case VncState::AWAITING_CLIENT_AUTH_METHOD: {
            std::string securityResult{{0, 0, 0, 0}};
            if (data[0] == (uint8_t)RfbAuthScheme::no_authentication) {
              meta.vncState = VncState::AWAITING_CLIENT_INIT_msg;
            } else {
              // Mark auth as failed
              securityResult[3] = 1;
              meta.vncState = VncState::UNSTARTED;
            }
            conn.sendBinary(securityResult);
          } break;
          case VncState::AWAITING_CLIENT_INIT_msg: {
            // Now send the server initialization
            ServerInitializationMsg serverInitMsg{};
            serverInitMsg.framebufferWidth = 800;
            serverInitMsg.framebufferHeight = 600;
            serverInitMsg.pixelFormat.bitsPerPixel = 32;
            serverInitMsg.pixelFormat.isBigEndian = 0;
            serverInitMsg.pixelFormat.isTrueColor = 1;
            serverInitMsg.pixelFormat.redMax = 255;
            serverInitMsg.pixelFormat.greenMax = 255;
            serverInitMsg.pixelFormat.blueMax = 255;
            serverInitMsg.pixelFormat.redShift = 16;
            serverInitMsg.pixelFormat.greenShift = 8;
            serverInitMsg.pixelFormat.blueShift = 0;
            serverInitMsg.nameLength = 0;
            std::cout << "size: " << sizeof(serverInitMsg);
            // TODO(ed) this is ugly.  Crow should really have a span type
            // interface
            // to avoid the copy, but alas, today it does not.
            std::string s(reinterpret_cast<char*>(&serverInitMsg),
                          sizeof(serverInitMsg));
            std::cout << "s.size() " << s.size();
            conn.sendBinary(s);
            meta.vncState = VncState::MAIN_LOOP;
          } break;
          case VncState::MAIN_LOOP: {
            if (data.size() >= sizeof(client_to_server_msg_type)) {
              auto type = static_cast<client_to_server_msg_type>(data[0]);
              std::cout << "Received client message type "
                        << static_cast<std::size_t>(type) << "\n";
              switch (type) {
                case client_to_server_msg_type::set_pixel_format: {
                } break;

                case client_to_server_msg_type::fix_color_map_entries: {
                } break;
                case client_to_server_msg_type::set_encodings: {
                } break;
                case client_to_server_msg_type::framebuffer_update_request: {
                  // Make sure the buffer is long enough to handle what we're
                  // about to do
                  if (data.size() >= sizeof(FrameBufferUpdateReq) +
                                         sizeof(client_to_server_msg_type)) {
                    auto msg = reinterpret_cast<const FrameBufferUpdateReq*>(
                        data.data() +  // NOLINT
                        sizeof(client_to_server_msg_type));
                    // TODO(ed) find a better way to do this deserialization

                    // Todo(ed) lifecycle of the video puller and decoder
                    // should be
                    // with the websocket, not recreated every time
                    ast_video::SimpleVideoPuller p;
                    p.initialize();
                    auto out = p.readVideo();
                    ast_video::AstJpegDecoder d;
                    d.decode(out.buffer, out.width, out.height, out.mode,
                             out.ySelector, out.uvSelector);

                    FramebufferUpdateMsg bufferUpdateMsg;

                    // If the viewer is requesting a full update, force write
                    // of all pixels

                    FramebufferRectangle thisRect;
                    thisRect.x = msg->xPosition;
                    thisRect.y = msg->yPosition;
                    thisRect.width = out.width;
                    thisRect.height = out.height;
                    thisRect.encoding =
                        static_cast<uint8_t>(encoding_type::raw);
                    std::cout << "Encoding is " << thisRect.encoding;
                    thisRect.data.reserve(
                        static_cast<std::size_t>(thisRect.width) *
                        static_cast<std::size_t>(thisRect.height) * 4);
                    std::cout << "Width " << out.width << " Height "
                              << out.height;

                    for (int i = 0; i < out.width * out.height; i++) {
                      auto& pixel = d.outBuffer[i];
                      thisRect.data.push_back(pixel.b);
                      thisRect.data.push_back(pixel.g);
                      thisRect.data.push_back(pixel.r);
                      thisRect.data.push_back(0);
                    }

                    bufferUpdateMsg.rectangles.push_back(std::move(thisRect));
                    auto serialized = serialize(bufferUpdateMsg);

                    conn.sendBinary(serialized);

                  }  // TODO(Ed) handle error

                }

                break;

                case client_to_server_msg_type::key_event: {
                } break;

                case client_to_server_msg_type::pointer_event: {
                } break;

                case client_to_server_msg_type::client_cut_text: {
                } break;

                default:
                  break;
              }
            }

          } break;
          case VncState::UNSTARTED:
            // Error?  TODO
            break;
        }

      });
}
}  // namespace kvm
}  // namespace crow