-
Notifications
You must be signed in to change notification settings - Fork 2
/
rfb.h
559 lines (433 loc) · 11.7 KB
/
rfb.h
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (c) 2016 Michal Srb <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef RFB_H
#define RFB_H
#include <arpa/inet.h>
// The only version we currently support
constexpr size_t VersionStringLength = 12;
constexpr char HighestVersionString[VersionStringLength] = { 'R', 'F', 'B', ' ', '0', '0', '3', '.', '0', '0', '8', '\n' };
enum class ClientMessageType : uint8_t
{
SetPixelFormat = 0,
SetEncodings = 2,
FramebufferUpdateRequest = 3,
KeyEvent = 4,
PointerEvent = 5,
ClientCutText = 6,
SetDesktopSize = 251
};
enum class ServerMessageType : uint8_t
{
FramebufferUpdate = 0,
SetColourMapEntries = 1,
Bell = 2,
ServerCutText = 3
};
// Only the security types we support
enum class SecurityType : uint8_t
{
Invalid = 0,
None = 1,
VncAuth = 2,
VeNCrypt = 19,
};
enum class SecurityResult : uint32_t
{
OK = 0,
Failed = 1
};
// Contains only supported encodings
enum class EncodingType : int32_t
{
Raw = 0,
CopyRect = 1,
RRE = 2,
// Hextile = 5, // TODO?
Tight = 7,
// ZRLE = 16, // TODO?
JpegQualityLowest = -32,
JpegQualityHighest = -23,
DesktopSize = -223,
LastRect = -224,
Cursor = -239,
XCursor = -240,
DesktopName = -307,
ExtendedDesktopSize = -308
};
#pragma pack(push, 1)
struct ClientInitMessage {
uint8_t shared;
void ntoh() {}
void hton() {}
};
struct PixelFormat {
uint8_t bitsPerPixel;
uint8_t depth;
uint8_t bigEndianFlag;
uint8_t trueColourFlag;
uint16_t redMax;
uint16_t greenMax;
uint16_t blueMax;
uint8_t redShift;
uint8_t greenShift;
uint8_t blueShift;
uint8_t _padding[3] = { 0, 0, 0 };
void ntoh() {
if (bigEndianFlag) {
redMax = ntohs(redMax);
greenMax = ntohs(greenMax);
blueMax = ntohs(blueMax);
}
}
void hton() {
bigEndianFlag = (__BYTE_ORDER == __BIG_ENDIAN);
}
bool operator==(const PixelFormat &another) const {
return
bitsPerPixel == another.bitsPerPixel &&
depth == another.depth &&
bigEndianFlag == another.bigEndianFlag &&
trueColourFlag == another.trueColourFlag &&
redMax == another.redMax &&
greenMax == another.greenMax &&
blueMax == another.blueMax &&
redShift == another.redShift &&
greenShift == another.greenShift &&
blueShift == another.blueShift;
}
bool operator!= (const PixelFormat &another) const {
return !(*this == another);
}
bool valid() {
// Validating only things that could hurt vncmanager. If there are some other wrong values, underlying VNC server should complain.
return bitsPerPixel == 8 || bitsPerPixel == 16 || bitsPerPixel == 24 || bitsPerPixel == 32;
}
};
struct ServerInitMessage {
uint16_t framebufferWidth;
uint16_t framebufferHeight;
PixelFormat serverPixelFormat;
uint32_t nameLength;
void ntoh() {
framebufferWidth = ntohs(framebufferWidth);
framebufferHeight = ntohs(framebufferHeight);
serverPixelFormat.ntoh();
nameLength = ntohl(nameLength);
}
void hton() {
framebufferWidth = htons(framebufferWidth);
framebufferHeight = htons(framebufferHeight);
serverPixelFormat.hton();
nameLength = htonl(nameLength);
}
};
struct SetPixelFormatMessage {
ClientMessageType type = ClientMessageType::SetPixelFormat;
uint8_t _padding[3] = { 0, 0, 0 };
PixelFormat pixelFormat;
void ntoh() {
pixelFormat.ntoh();
}
void hton() {
pixelFormat.hton();
}
};
struct SetEncodingsMessage {
ClientMessageType type = ClientMessageType::SetEncodings;
uint8_t _padding = 0;
uint16_t numberOfEncodings;
// list of encodings follows
void ntoh() {
numberOfEncodings = ntohs(numberOfEncodings);
}
void hton() {
numberOfEncodings = htons(numberOfEncodings);
}
};
struct FramebufferUpdateRequestMessage {
ClientMessageType type = ClientMessageType::FramebufferUpdateRequest;
uint8_t incremental;
uint16_t xPosition;
uint16_t yPosition;
uint16_t width;
uint16_t height;
void ntoh() {
xPosition = ntohs(xPosition);
yPosition = ntohs(yPosition);
width = ntohs(width);
height = ntohs(height);
}
void hton() {
xPosition = htons(xPosition);
yPosition = htons(yPosition);
width = htons(width);
height = htons(height);
}
};
struct KeyEventMessage {
ClientMessageType type = ClientMessageType::KeyEvent;
uint8_t downFlag;
uint8_t _padding[2] = { 0, 0 };
uint32_t key;
void ntoh() {
key = ntohl(key);
}
void hton() {
key = htonl(key);
}
};
struct PointerEventMessage {
ClientMessageType type = ClientMessageType::PointerEvent;
uint8_t buttonMask;
uint16_t xPosition;
uint16_t yPosition;
void ntoh() {
xPosition = ntohs(xPosition);
yPosition = ntohs(yPosition);
}
void hton() {
xPosition = htons(xPosition);
yPosition = htons(yPosition);
}
};
struct ClientCutTextMessage {
ClientMessageType type = ClientMessageType::ClientCutText;
uint8_t _padding[3] = { 0, 0, 0 };
uint32_t length;
// text follows
void ntoh() {
length = ntohl(length);
}
void hton() {
length = htonl(length);
}
};
struct FramebufferUpdateMessage {
ServerMessageType type = ServerMessageType::FramebufferUpdate;
uint8_t _padding = 0;
uint16_t numberOfRectangles;
// rectangles follow
void ntoh() {
numberOfRectangles = ntohs(numberOfRectangles);
}
void hton() {
numberOfRectangles = htons(numberOfRectangles);
}
};
struct FramebufferUpdateRectangle {
uint16_t xPosition = 0;
uint16_t yPosition = 0;
uint16_t width = 0;
uint16_t height = 0;
EncodingType encodingType;
// data follow
void ntoh() {
xPosition = ntohs(xPosition);
yPosition = ntohs(yPosition);
width = ntohs(width);
height = ntohs(height);
encodingType =(EncodingType ) ntohl(( int32_t ) encodingType);
}
void hton() {
xPosition = htons(xPosition);
yPosition = htons(yPosition);
width = htons(width);
height = htons(height);
encodingType =(EncodingType ) htonl(( int32_t ) encodingType);
}
};
struct SetColourMapEntriesMessage {
ServerMessageType type = ServerMessageType::SetColourMapEntries;
uint8_t _padding = 0;
uint16_t firstColour;
uint16_t numberOfColours;
// colours follow
void ntoh() {
firstColour = ntohs(firstColour);
numberOfColours = ntohs(numberOfColours);
}
void hton() {
firstColour = htons(firstColour);
numberOfColours = htons(numberOfColours);
}
};
struct ColourMapEntry {
uint16_t red;
uint16_t green;
uint16_t blue;
void ntoh() {
red = ntohs(red);
green = ntohs(green);
blue = ntohs(blue);
}
void hton() {
red = htons(red);
green = htons(green);
blue = htons(blue);
}
};
struct BellMessage {
ServerMessageType type = ServerMessageType::Bell;
void ntoh() {}
void hton() {}
};
struct ServerCutTextMessage {
ServerMessageType type = ServerMessageType::ServerCutText;
uint8_t _padding[3] = { 0, 0, 0 };
uint32_t length;
// text follows
void ntoh() {
length = ntohl(length);
}
void hton() {
length = htonl(length);
}
};
struct VNCAuthMessage {
uint8_t data[16];
void ntoh() {}
void hton() {}
};
// ExtendedDesktopSize extension
struct SetDesktopSizeMessage {
ClientMessageType type = ClientMessageType::SetDesktopSize;
uint8_t _padding = 0;
uint16_t width;
uint16_t height;
uint8_t numberOfScreens;
uint8_t _padding2 = 0;
void ntoh() {
width = ntohs(width);
height = ntohs(height);
}
void hton() {
width = htons(width);
height = htons(height);
}
};
struct ExtendedDesktopSizeRectangleData {
uint8_t numberOfScreens;
uint8_t _padding[3] = { 0, 0, 0 };
void ntoh() {}
void hton() {}
};
struct SetDesktopSizeScreen {
uint32_t id;
uint16_t xPosition;
uint16_t yPosition;
uint16_t width;
uint16_t height;
uint32_t flags;
void ntoh() {
id = ntohl(id);
xPosition = ntohs(xPosition);
yPosition = ntohs(yPosition);
width = ntohs(width);
height = ntohs(height);
// flags are currently not used by vncmanager, not sure if they should be swapped
}
void hton() {
id = htonl(id);
xPosition = htons(xPosition);
yPosition = htons(yPosition);
width = htons(width);
height = htons(height);
// flags are currently not used by vncmanager, not sure if they should be swapped
}
};
enum class ExtendedDesktopSizeStatus
{
NoError = 0,
ResizeProhibited = 1,
OutOfResources = 2,
InvalidScreenLayout = 3
};
struct VeNCryptVersion {
uint8_t major;
uint8_t minor;
void ntoh() {}
void hton() {}
};
enum class VeNCryptSubtype : uint32_t // Contains only the supported subtypes
{
Invalid = (uint32_t) SecurityType::Invalid,
None = (uint32_t) SecurityType::None,
VncAuth = (uint32_t) SecurityType::VncAuth,
Plain = 256, // (used in direction towards local Xvnc)
TLSNone = 257,
X509None = 260
};
struct VeNCryptPlainMessage {
uint32_t usernameLength;
uint32_t passwordLength;
// username and password follows
void ntoh() {
usernameLength = ntohl(usernameLength);
passwordLength = ntohl(passwordLength);
}
void hton() {
usernameLength = htonl(usernameLength);
passwordLength = htonl(passwordLength);
}
};
struct TightCompressionControl {
unsigned resetStream0 : 1;
unsigned resetStream1 : 1;
unsigned resetStream2 : 1;
unsigned resetStream3 : 1;
unsigned rest : 4;
int useStream() const {
return rest & 0x3;
}
bool isBasicCompression() const {
return !(rest & 0x8);
}
bool isFillCompression() const {
return rest == 0x8;
}
bool isJpegCompression() const {
return rest == 0x9;
}
bool readFilterId() const {
return rest & 0x4;
}
void ntoh() {}
void hton() {}
};
struct TightPixel {
uint8_t red;
uint8_t green;
uint8_t blue;
};
enum class TightFilter : uint8_t
{
Copy = 0,
Palette = 1,
Gradient = 2
};
constexpr int TightMinSizeToCompress = 12;
#pragma pack(pop)
#endif