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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2009-2019 Pierre Ossman for Cendio AB
* Copyright 2018 Peter Astrand for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <network/TcpSocket.h>
#include <rfb/ComparingUpdateTracker.h>
#include <rfb/Encoder.h>
#include <rfb/Exception.h>
#include <rfb/KeyRemapper.h>
#include <rfb/KeysymStr.h>
#include <rfb/LogWriter.h>
#include <rfb/Security.h>
#include <rfb/ServerCore.h>
#include <rfb/SMsgWriter.h>
#include <rfb/VNCServerST.h>
#include <rfb/VNCSConnectionST.h>
#include <rfb/screenTypes.h>
#include <rfb/fenceTypes.h>
#include <rfb/ledStates.h>
#define XK_LATIN1
#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <rfb/keysymdef.h>
#include <rfb/util.h>
using namespace rfb;
static LogWriter vlog("VNCSConnST");
static Cursor emptyCursor(0, 0, Point(0, 0), nullptr);
VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
bool reverse, AccessRights ar)
: SConnection(ar),
sock(s), reverseConnection(reverse),
inProcessMessages(false),
pendingSyncFence(false), syncFence(false), fenceFlags(0),
fenceDataLen(0), fenceData(nullptr), congestionTimer(this),
losslessTimer(this), server(server_),
updateRenderedCursor(false), removeRenderedCursor(false),
continuousUpdates(false), encodeManager(this), idleTimer(this),
pointerEventTime(0), clientHasCursor(false)
{
setStreams(&sock->inStream(), &sock->outStream());
peerEndpoint = sock->getPeerEndpoint();
// Kick off the idle timer
if (rfb::Server::idleTimeout) {
// minimum of 15 seconds while authenticating
if (rfb::Server::idleTimeout < 15)
idleTimer.start(secsToMillis(15));
else
idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
}
}
VNCSConnectionST::~VNCSConnectionST()
{
// If we reach here then VNCServerST is deleting us!
if (!closeReason.empty())
vlog.info("closing %s: %s", peerEndpoint.c_str(),
closeReason.c_str());
// Release any mouse buttons
server->pointerEvent(this, server->getCursorPos(), 0);
// Release any keys the client still had pressed
while (!pressedKeys.empty()) {
uint32_t keysym, keycode;
keysym = pressedKeys.begin()->second;
keycode = pressedKeys.begin()->first;
pressedKeys.erase(pressedKeys.begin());
vlog.debug("Releasing key 0x%04x / XK_%s (0x%04x) on client disconnect",
keycode, KeySymName(keysym), keysym);
server->keyEvent(keysym, keycode, false);
}
delete [] fenceData;
}
// SConnection methods
bool VNCSConnectionST::accessCheck(AccessRights ar) const
{
// Reverse connections are user initiated, so they are implicitly
// allowed to bypass the query
if (reverseConnection)
ar &= ~AccessNoQuery;
return SConnection::accessCheck(ar);
}
void VNCSConnectionST::close(const char* reason)
{
SConnection::close(reason);
// Log the reason for the close
if (closeReason.empty())
closeReason = reason;
else
vlog.debug("second close: %s (%s)", peerEndpoint.c_str(), reason);
try {
if (sock->outStream().hasBufferedData()) {
sock->outStream().cork(false);
sock->outStream().flush();
if (sock->outStream().hasBufferedData())
vlog.error("Failed to flush remaining socket data on close");
}
} catch (rdr::Exception& e) {
vlog.error("Failed to flush remaining socket data on close: %s", e.str());
}
// Just shutdown the socket and mark our state as closing. Eventually the
// calling code will call VNCServerST's removeSocket() method causing us to
// be deleted.
sock->shutdown();
}
// Methods called from VNCServerST
bool VNCSConnectionST::init()
{
try {
initialiseProtocol();
} catch (rdr::Exception& e) {
close(e.str());
return false;
}
return true;
}
void VNCSConnectionST::processMessages()
{
if (state() == RFBSTATE_CLOSING) return;
try {
inProcessMessages = true;
// Get the underlying transport to build large packets if we send
// multiple small responses.
getOutStream()->cork(true);
while (true) {
if (pendingSyncFence)
syncFence = true;
if (!processMsg())
break;
if (syncFence) {
writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
syncFence = false;
pendingSyncFence = false;
}
}
// Flush out everything in case we go idle after this.
getOutStream()->cork(false);
inProcessMessages = false;
// If there were anything requiring an update, try to send it here.
// We wait until now with this to aggregate responses and to give
// higher priority to user actions such as keyboard and pointer events.
writeFramebufferUpdate();
} catch (rdr::EndOfStream&) {
close("Clean disconnection");
} catch (rdr::Exception &e) {
close(e.str());
}
}
void VNCSConnectionST::flushSocket()
{
if (state() == RFBSTATE_CLOSING) return;
try {
sock->outStream().flush();
// Flushing the socket might release an update that was previously
// delayed because of congestion.
if (!sock->outStream().hasBufferedData())
writeFramebufferUpdate();
} catch (rdr::Exception &e) {
close(e.str());
}
}
void VNCSConnectionST::pixelBufferChange()
{
try {
if (!authenticated()) return;
if (client.width() && client.height() &&
(server->getPixelBuffer()->width() != client.width() ||
server->getPixelBuffer()->height() != client.height()))
{
// We need to clip the next update to the new size, but also add any
// extra bits if it's bigger. If we wanted to do this exactly, something
// like the code below would do it, but at the moment we just update the
// entire new size. However, we do need to clip the damagedCursorRegion
// because that might be added to updates in writeFramebufferUpdate().
//updates.intersect(server->pb->getRect());
//
//if (server->pb->width() > client.width())
// updates.add_changed(Rect(client.width(), 0, server->pb->width(),
// server->pb->height()));
//if (server->pb->height() > client.height())
// updates.add_changed(Rect(0, client.height(), client.width(),
// server->pb->height()));
damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
client.setDimensions(server->getPixelBuffer()->width(),
server->getPixelBuffer()->height(),
server->getScreenLayout());
if (state() == RFBSTATE_NORMAL) {
if (!client.supportsDesktopSize()) {
close("Client does not support desktop resize");
return;
}
writer()->writeDesktopSize(reasonServer);
}
// Drop any lossy tracking that is now outside the framebuffer
encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->getRect()));
}
// Just update the whole screen at the moment because we're too lazy to
// work out what's actually changed.
updates.clear();
updates.add_changed(server->getPixelBuffer()->getRect());
writeFramebufferUpdate();
} catch(rdr::Exception &e) {
close(e.str());
}
}
void VNCSConnectionST::writeFramebufferUpdateOrClose()
{
try {
writeFramebufferUpdate();
} catch(rdr::Exception &e) {
close(e.str());
}
}
void VNCSConnectionST::screenLayoutChangeOrClose(uint16_t reason)
{
try {
screenLayoutChange(reason);
writeFramebufferUpdate();
} catch(rdr::Exception &e) {
close(e.str());
}
}
void VNCSConnectionST::bellOrClose()
{
try {
if (state() == RFBSTATE_NORMAL) writer()->writeBell();
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::setDesktopNameOrClose(const char *name)
{
try {
setDesktopName(name);
writeFramebufferUpdate();
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::setCursorOrClose()
{
try {
setCursor();
writeFramebufferUpdate();
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
{
try {
setLEDState(state);
writeFramebufferUpdate();
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::requestClipboardOrClose()
{
try {
if (state() != RFBSTATE_NORMAL) return;
if (!accessCheck(AccessCutText)) return;
if (!rfb::Server::acceptCutText) return;
requestClipboard();
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::announceClipboardOrClose(bool available)
{
try {
if (state() != RFBSTATE_NORMAL) return;
if (!accessCheck(AccessCutText)) return;
if (!rfb::Server::sendCutText) return;
announceClipboard(available);
} catch(rdr::Exception& e) {
close(e.str());
}
}
void VNCSConnectionST::sendClipboardDataOrClose(const char* data)
{
try {
if (state() != RFBSTATE_NORMAL) return;
if (!accessCheck(AccessCutText)) return;
if (!rfb::Server::sendCutText) return;
sendClipboardData(data);
} catch(rdr::Exception& e) {
close(e.str());
}
}
bool VNCSConnectionST::getComparerState()
{
// We interpret a low compression level as an indication that the client
// wants to prioritise CPU usage over bandwidth, and hence disable the
// comparing update tracker.
return (client.compressLevel == -1) || (client.compressLevel > 1);
}
// renderedCursorChange() is called whenever the server-side rendered cursor
// changes shape or position. It ensures that the next update will clean up
// the old rendered cursor and if necessary draw the new rendered cursor.
void VNCSConnectionST::renderedCursorChange()
{
if (state() != RFBSTATE_NORMAL) return;
// Are we switching between client-side and server-side cursor?
if (clientHasCursor == needRenderedCursor())
setCursorOrClose();
bool hasRenderedCursor = !damagedCursorRegion.is_empty();
if (hasRenderedCursor)
removeRenderedCursor = true;
if (needRenderedCursor()) {
updateRenderedCursor = true;
writeFramebufferUpdateOrClose();
}
}
// cursorPositionChange() is called whenever the cursor has changed position by
// the server. If the client supports being informed about these changes then
// it will arrange for the new cursor position to be sent to the client.
void VNCSConnectionST::cursorPositionChange()
{
setCursorPos();
}
// needRenderedCursor() returns true if this client needs the server-side
// rendered cursor. This may be because it does not support local cursor or
// because the current cursor position has not been set by this client.
// Unfortunately we can't know for sure when the current cursor position has
// been set by this client. We guess that this is the case when the current
// cursor position is the same as the last pointer event from this client, or
// if it is a very short time since this client's last pointer event (up to a
// second). [ Ideally we should do finer-grained timing here and make the time
// configurable, but I don't think it's that important. ]
bool VNCSConnectionST::needRenderedCursor()
{
if (state() != RFBSTATE_NORMAL)
return false;
if (!client.supportsLocalCursor())
return true;
if ((server->getCursorPos() != pointerEventPos) &&
(time(nullptr) - pointerEventTime) > 0)
return true;
return false;
}
void VNCSConnectionST::approveConnectionOrClose(bool accept,
const char* reason)
{
try {
approveConnection(accept, reason);
} catch (rdr::Exception& e) {
close(e.str());
}
}
// -=- Callbacks from SConnection
void VNCSConnectionST::authSuccess()
{
if (rfb::Server::idleTimeout)
idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
// - Set the connection parameters appropriately
client.setDimensions(server->getPixelBuffer()->width(),
server->getPixelBuffer()->height(),
server->getScreenLayout());
client.setName(server->getName());
client.setLEDState(server->getLEDState());
// - Set the default pixel format
client.setPF(server->getPixelBuffer()->getPF());
char buffer[256];
client.pf().print(buffer, 256);
vlog.info("Server default pixel format %s", buffer);
// - Mark the entire display as "dirty"
updates.add_changed(server->getPixelBuffer()->getRect());
}
void VNCSConnectionST::queryConnection(const char* userName)
{
server->queryConnection(this, userName);
}
void VNCSConnectionST::clientInit(bool shared)
{
if (rfb::Server::idleTimeout)
idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
if (rfb::Server::alwaysShared || reverseConnection) shared = true;
if (!accessCheck(AccessNonShared)) shared = true;
if (rfb::Server::neverShared) shared = false;
SConnection::clientInit(shared);
server->clientReady(this, shared);
}
void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
{
SConnection::setPixelFormat(pf);
char buffer[256];
pf.print(buffer, 256);
vlog.info("Client pixel format %s", buffer);
setCursor();
}
void VNCSConnectionST::pointerEvent(const Point& pos, uint8_t buttonMask)
{
if (rfb::Server::idleTimeout)
idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
pointerEventTime = time(nullptr);
if (!accessCheck(AccessPtrEvents)) return;
if (!rfb::Server::acceptPointerEvents) return;
pointerEventPos = pos;
server->pointerEvent(this, pointerEventPos, buttonMask);
}
class VNCSConnectionSTShiftPresser {
public:
VNCSConnectionSTShiftPresser(VNCServerST* server_)
: server(server_), pressed(false) {}
~VNCSConnectionSTShiftPresser() {
if (pressed) {
vlog.debug("Releasing fake Shift_L");
server->keyEvent(XK_Shift_L, 0x2a, false);
}
}
void press() {
vlog.debug("Pressing fake Shift_L");
server->keyEvent(XK_Shift_L, 0x2a, true);
pressed = true;
}
VNCServerST* server;
bool pressed;
};
// keyEvent() - record in the pressedKeys which keys were pressed. Allow
// multiple down events (for autorepeat), but only allow a single up event.
void VNCSConnectionST::keyEvent(uint32_t keysym, uint32_t keycode, bool down) {
uint32_t lookup;
if (rfb::Server::idleTimeout)
idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
if (!accessCheck(AccessKeyEvents)) return;
if (!rfb::Server::acceptKeyEvents) return;
if (down)
vlog.debug("Key pressed: 0x%04x / XK_%s (0x%04x)",
keycode, KeySymName(keysym), keysym);
else
vlog.debug("Key released: 0x%04x / XK_%s (0x%04x)",
keycode, KeySymName(keysym), keysym);
// Avoid lock keys if we don't know the server state
if ((server->getLEDState() == ledUnknown) &&
((keysym == XK_Caps_Lock) ||
(keysym == XK_Num_Lock))) {
vlog.debug("Ignoring lock key (e.g. caps lock)");
return;
}
// Lock key heuristics
// (only for clients that do not support the LED state extension)
if (!client.supportsLEDState()) {
if (down && (server->getLEDState() != ledUnknown)) {
// CapsLock synchronisation heuristic
// (this assumes standard interaction between CapsLock the Shift
// keys and normal characters)
if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
((keysym >= XK_a) && (keysym <= XK_z))) {
bool uppercase, shift, lock;
uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
shift = isShiftPressed();
lock = server->getLEDState() & ledCapsLock;
if (lock == (uppercase == shift)) {
vlog.debug("Inserting fake CapsLock to get in sync with client");
server->keyEvent(XK_Caps_Lock, 0x3a, true);
server->keyEvent(XK_Caps_Lock, 0x3a, false);
}
}
// NumLock synchronisation heuristic
// (this is more cautious because of the differences between Unix,
// Windows and macOS)
if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
(keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
bool number, shift, lock;
number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
(keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
shift = isShiftPressed();
lock = server->getLEDState() & ledNumLock;
if (shift) {
// We don't know the appropriate NumLock state for when Shift
// is pressed as it could be one of:
//
// a) A Unix client where Shift negates NumLock
//
// b) A Windows client where Shift only cancels NumLock
//
// c) A macOS client where Shift doesn't have any effect
//
} else if (lock == (number == shift)) {
vlog.debug("Inserting fake NumLock to get in sync with client");
server->keyEvent(XK_Num_Lock, 0x45, true);
server->keyEvent(XK_Num_Lock, 0x45, false);
}
}
}
}
// Turn ISO_Left_Tab into shifted Tab.
VNCSConnectionSTShiftPresser shiftPresser(server);
if (keysym == XK_ISO_Left_Tab) {
if (!isShiftPressed())
shiftPresser.press();
keysym = XK_Tab;
}
// We need to be able to track keys, so generate a fake index when we
// aren't given a keycode
if (keycode == 0)
lookup = 0x80000000 | keysym;
else
lookup = keycode;
// We force the same keysym for an already down key for the
// sake of sanity
if (pressedKeys.find(lookup) != pressedKeys.end())
keysym = pressedKeys[lookup];
if (down) {
pressedKeys[lookup] = keysym;
} else {
if (!pressedKeys.erase(lookup))
return;
}
server->keyEvent(keysym, keycode, down);
}
void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
{
Rect safeRect;
if (!accessCheck(AccessView)) return;
SConnection::framebufferUpdateRequest(r, incremental);
// Check that the client isn't sending crappy requests
if (!r.enclosed_by(Rect(0, 0, client.width(), client.height()))) {
vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
r.width(), r.height(), r.tl.x, r.tl.y,
client.width(), client.height());
safeRect = r.intersect(Rect(0, 0, client.width(), client.height()));
} else {
safeRect = r;
}
// Just update the requested region.
// Framebuffer update will be sent a bit later, see processMessages().
Region reqRgn(safeRect);
if (!incremental || !continuousUpdates)
requested.assign_union(reqRgn);
if (!incremental) {
// Non-incremental update - treat as if area requested has changed
updates.add_changed(reqRgn);
// And send the screen layout to the client (which, unlike the
// framebuffer dimensions, the client doesn't get during init)
if (client.supportsEncoding(pseudoEncodingExtendedDesktopSize))
writer()->writeDesktopSize(reasonServer);
// We do not send a DesktopSize since it only contains the
// framebuffer size (which the client already should know) and
// because some clients don't handle extra DesktopSize events
// very well.
}
}
void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
const ScreenSet& layout)
{
unsigned int result;
char buffer[2048];
vlog.debug("Got request for framebuffer resize to %dx%d",
fb_width, fb_height);
layout.print(buffer, sizeof(buffer));
vlog.debug("%s", buffer);
if (!accessCheck(AccessSetDesktopSize) ||
!rfb::Server::acceptSetDesktopSize) {
vlog.debug("Rejecting unauthorized framebuffer resize request");
result = resultProhibited;
} else {
result = server->setDesktopSize(this, fb_width, fb_height, layout);
}
writer()->writeDesktopSize(reasonClient, result);
}
void VNCSConnectionST::fence(uint32_t flags, unsigned len, const uint8_t data[])
{
uint8_t type;
if (flags & fenceFlagRequest) {
if (flags & fenceFlagSyncNext) {
pendingSyncFence = true;
fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
fenceDataLen = len;
delete [] fenceData;
fenceData = nullptr;
if (len > 0) {
fenceData = new uint8_t[len];
memcpy(fenceData, data, len);
}
return;
}
// We handle everything synchronously so we trivially honor these modes
flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
writer()->writeFence(flags, len, data);
return;
}
if (len < 1) {
vlog.error("Fence response of unexpected size received");
return;
}
type = data[0];
switch (type) {
case 0:
// Initial dummy fence;
break;
case 1:
congestion.gotPong();
break;
default:
vlog.error("Fence response of unexpected type received");
}
}
void VNCSConnectionST::enableContinuousUpdates(bool enable,
int x, int y, int w, int h)
{
Rect rect;
if (!client.supportsFence() || !client.supportsContinuousUpdates())
throw Exception("Client tried to enable continuous updates when not allowed");
continuousUpdates = enable;
rect.setXYWH(x, y, w, h);
cuRegion.reset(rect);
if (enable) {
requested.clear();
} else {
writer()->writeEndOfContinuousUpdates();
}
}
void VNCSConnectionST::handleClipboardRequest()
{
if (!accessCheck(AccessCutText)) return;
server->handleClipboardRequest(this);
}
void VNCSConnectionST::handleClipboardAnnounce(bool available)
{
if (!accessCheck(AccessCutText)) return;
if (!rfb::Server::acceptCutText) return;
server->handleClipboardAnnounce(this, available);
}
void VNCSConnectionST::handleClipboardData(const char* data)
{
if (!accessCheck(AccessCutText)) return;
if (!rfb::Server::acceptCutText) return;
server->handleClipboardData(this, data);
}
// supportsLocalCursor() is called whenever the status of
// client.supportsLocalCursor() has changed. If the client does now support local
// cursor, we make sure that the old server-side rendered cursor is cleaned up
// and the cursor is sent to the client.
void VNCSConnectionST::supportsLocalCursor()
{
bool hasRenderedCursor = !damagedCursorRegion.is_empty();
if (hasRenderedCursor && !needRenderedCursor())
removeRenderedCursor = true;
setCursor();
}
void VNCSConnectionST::supportsFence()
{
uint8_t type = 0;
writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
}
void VNCSConnectionST::supportsContinuousUpdates()
{
// We refuse to use continuous updates if we cannot monitor the buffer
// usage using fences.
if (!client.supportsFence())
return;
writer()->writeEndOfContinuousUpdates();
}
void VNCSConnectionST::supportsLEDState()
{
if (client.ledState() == ledUnknown)
return;
writer()->writeLEDState();
}
void VNCSConnectionST::handleTimeout(Timer* t)
{
try {
if ((t == &congestionTimer) ||
(t == &losslessTimer))
writeFramebufferUpdate();
} catch (rdr::Exception& e) {
close(e.str());
}
if (t == &idleTimer)
close("Idle timeout");
}
bool VNCSConnectionST::isShiftPressed()
{
std::map<uint32_t, uint32_t>::const_iterator iter;
for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
if (iter->second == XK_Shift_L)
return true;
if (iter->second == XK_Shift_R)
return true;
}
return false;
}
void VNCSConnectionST::writeRTTPing()
{
uint8_t type;
if (!client.supportsFence())
return;
congestion.updatePosition(sock->outStream().length());
// We need to make sure any old update are already processed by the
// time we get the response back. This allows us to reliably throttle
// back on client overload, as well as network overload.
type = 1;
writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
sizeof(type), &type);
congestion.sentPing();
}
bool VNCSConnectionST::isCongested()
{
int eta;
congestionTimer.stop();
// Stuff still waiting in the send buffer?
sock->outStream().flush();
congestion.debugTrace("congestion-trace.csv", sock->getFd());
if (sock->outStream().hasBufferedData())
return true;
if (!client.supportsFence())
return false;
congestion.updatePosition(sock->outStream().length());
if (!congestion.isCongested())
return false;
eta = congestion.getUncongestedETA();
if (eta >= 0)
congestionTimer.start(eta);
return true;
}
void VNCSConnectionST::writeFramebufferUpdate()
{
congestion.updatePosition(sock->outStream().length());
// We're in the middle of processing a command that's supposed to be
// synchronised. Allowing an update to slip out right now might violate
// that synchronisation.
if (syncFence)
return;
// We try to aggregate responses, so don't send out anything whilst we
// still have incoming messages. processMessages() will give us another
// chance to run once things are idle.
if (inProcessMessages)
return;
if (state() != RFBSTATE_NORMAL)
return;
if (requested.is_empty() && !continuousUpdates)
return;
// Check that we actually have some space on the link and retry in a
// bit if things are congested.
if (isCongested())
return;
// Updates often consists of many small writes, and in continuous
// mode, we will also have small fence messages around the update. We
// need to aggregate these in order to not clog up TCP's congestion
// window.
getOutStream()->cork(true);
// First take care of any updates that cannot contain framebuffer data
// changes.
writeNoDataUpdate();
// Then real data (if possible)
writeDataUpdate();
getOutStream()->cork(false);
congestion.updatePosition(sock->outStream().length());
}
void VNCSConnectionST::writeNoDataUpdate()
{
if (!writer()->needNoDataUpdate())
return;
writer()->writeNoDataUpdate();
// Make sure no data update is sent until next request
requested.clear();
}
void VNCSConnectionST::writeDataUpdate()
{
Region req;
UpdateInfo ui;
bool needNewUpdateInfo;
const RenderedCursor *cursor;
// See what the client has requested (if anything)
if (continuousUpdates)
req = cuRegion.union_(requested);
else
req = requested;
if (req.is_empty())
return;
// Get the lists of updates. Prior to exporting the data to the `ui' object,
// getUpdateInfo() will normalize the `updates' object such way that its
// `changed' and `copied' regions would not intersect.
updates.getUpdateInfo(&ui, req);
needNewUpdateInfo = false;
// If the previous position of the rendered cursor overlaps the source of the
// copy, then when the copy happens the corresponding rectangle in the
// destination will be wrong, so add it to the changed region.
if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
Region bogusCopiedCursor;
bogusCopiedCursor = damagedCursorRegion;
bogusCopiedCursor.translate(ui.copy_delta);
bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
updates.add_changed(bogusCopiedCursor);
needNewUpdateInfo = true;
}
}
// If we need to remove the old rendered cursor, just add the region to
// the changed region.
if (removeRenderedCursor) {
updates.add_changed(damagedCursorRegion);
needNewUpdateInfo = true;
damagedCursorRegion.clear();
removeRenderedCursor = false;
}
// If we need a full cursor update then make sure its entire region
// is marked as changed.
if (updateRenderedCursor) {
updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
needNewUpdateInfo = true;
updateRenderedCursor = false;
}
// The `updates' object could change, make sure we have valid update info.
if (needNewUpdateInfo)
updates.getUpdateInfo(&ui, req);
// If there are queued updates then we cannot safely send an update
// without risking a partially updated screen
if (!server->getPendingRegion().is_empty()) {
req.clear();
ui.changed.clear();
ui.copied.clear();
}
// Does the client need a server-side rendered cursor?
cursor = nullptr;
if (needRenderedCursor()) {
Rect renderedCursorRect;
cursor = server->getRenderedCursor();
renderedCursorRect = cursor->getEffectiveRect();
// Check that we don't try to copy over the cursor area, and
// if that happens we need to treat it as changed so that we can
// re-render it
if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
ui.copied.assign_subtract(renderedCursorRect);
}
// Track where we've rendered the cursor
damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
}
// If we don't have a normal update, then try a lossless refresh
if (ui.is_empty() && !writer()->needFakeUpdate()) {
writeLosslessRefresh();
return;
}
// We have something to send, so let's get to it
writeRTTPing();
encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
writeRTTPing();
// The request might be for just part of the screen, so we cannot
// just clear the entire update tracker.
updates.subtract(req);
requested.clear();
}
void VNCSConnectionST::writeLosslessRefresh()
{
Region req, pending;
const RenderedCursor *cursor;
int nextRefresh, nextUpdate;
size_t bandwidth, maxUpdateSize;
if (continuousUpdates)
req = cuRegion.union_(requested);
else
req = requested;
// If there are queued updates then we could not safely send an
// update without risking a partially updated screen, however we
// might still be able to send a lossless refresh
pending = server->getPendingRegion();
if (!pending.is_empty()) {
UpdateInfo ui;
// Don't touch the updates pending in the server core
req.assign_subtract(pending);
// Or any updates pending just for this connection
updates.getUpdateInfo(&ui, req);
req.assign_subtract(ui.changed);
req.assign_subtract(ui.copied);
}
// Any lossy area we can refresh?
if (!encodeManager.needsLosslessRefresh(req))
return;
// Right away? Or later?
nextRefresh = encodeManager.getNextLosslessRefresh(req);
if (nextRefresh > 0) {
losslessTimer.start(nextRefresh);
return;
}
// Prepare the cursor in case it overlaps with a region getting
// refreshed
cursor = nullptr;
if (needRenderedCursor())
cursor = server->getRenderedCursor();
// FIXME: If continuous updates aren't used then the client might
// be slower than frameRate in its requests and we could
// afford a larger update size
nextUpdate = server->msToNextUpdate();
// Don't bother if we're about to send a real update
if (nextUpdate == 0)
return;
// FIXME: Bandwidth estimation without congestion control
bandwidth = congestion.getBandwidth();
// FIXME: Hard coded value for maximum CPU throughput
if (bandwidth > 5000000)
bandwidth = 5000000;
maxUpdateSize = bandwidth * nextUpdate / 1000;
writeRTTPing();
encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
cursor, maxUpdateSize);
writeRTTPing();
requested.clear();
}
void VNCSConnectionST::screenLayoutChange(uint16_t reason)
{
if (!authenticated())
return;
client.setDimensions(client.width(), client.height(),
server->getScreenLayout());
if (state() != RFBSTATE_NORMAL)
return;
writer()->writeDesktopSize(reason);
}
// setCursor() is called whenever the cursor has changed shape or pixel format.
// If the client supports local cursor then it will arrange for the cursor to
// be sent to the client.
void VNCSConnectionST::setCursor()
{
if (state() != RFBSTATE_NORMAL)
return;
// We need to blank out the client's cursor or there will be two
if (needRenderedCursor()) {
client.setCursor(emptyCursor);
clientHasCursor = false;
} else {
client.setCursor(*server->getCursor());
clientHasCursor = true;
}
if (client.supportsLocalCursor())
writer()->writeCursor();
}
// setCursorPos() is called whenever the cursor has changed position by the
// server. If the client supports being informed about these changes then it
// will arrange for the new cursor position to be sent to the client.
void VNCSConnectionST::setCursorPos()
{
if (state() != RFBSTATE_NORMAL)
return;
if (client.supportsCursorPosition()) {
client.setCursorPos(server->getCursorPos());
writer()->writeCursorPos();
}
}
void VNCSConnectionST::setDesktopName(const char *name)
{
client.setName(name);
if (state() != RFBSTATE_NORMAL)
return;
if (client.supportsEncoding(pseudoEncodingDesktopName))
writer()->writeSetDesktopName();
}
void VNCSConnectionST::setLEDState(unsigned int ledstate)
{
if (state() != RFBSTATE_NORMAL)
return;
client.setLEDState(ledstate);
if (client.supportsLEDState())
writer()->writeLEDState();
}
|