/
rfbserver.c
2820 lines (2383 loc) · 87.4 KB
/
rfbserver.c
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
/*
* rfbserver.c - deal with server-side of the RFB protocol.
*/
/*
* Copyright (C) 2009-2019 D. R. Commander. All Rights Reserved.
* Copyright (C) 2010 University Corporation for Atmospheric Research.
* All Rights Reserved.
* Copyright (C) 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
* Copyright (C) 2004 Landmark Graphics Corporation. All Rights Reserved.
* Copyright (C) 2000-2006 Constantin Kaplinsky. All Rights Reserved.
* Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "windowstr.h"
#include "rfb.h"
#include "sprite.h"
/* #define GII_DEBUG */
char updateBuf[UPDATE_BUF_SIZE];
int ublen;
rfbClientPtr rfbClientHead = NULL;
rfbClientPtr pointerClient = NULL; /* Mutex for pointer events */
Bool rfbAlwaysShared = FALSE;
Bool rfbNeverShared = FALSE;
Bool rfbDontDisconnect = TRUE;
Bool rfbViewOnly = FALSE; /* run server in view only mode - Ehud Karni SW */
Bool rfbSyncCutBuffer = TRUE;
Bool rfbCongestionControl = TRUE;
double rfbAutoLosslessRefresh = 0.0;
int rfbALRQualityLevel = -1;
int rfbALRSubsampLevel = TVNC_1X;
int rfbCombineRect = 100;
int rfbICEBlockSize = 256;
Bool rfbInterframeDebug = FALSE;
int rfbMaxWidth = MAXSHORT, rfbMaxHeight = MAXSHORT;
int rfbMaxClipboard = MAX_CUTTEXT_LEN;
Bool rfbVirtualTablet = FALSE;
Bool rfbMT = TRUE;
int rfbNumThreads = 0;
static rfbClientPtr rfbNewClient(int sock);
static void rfbProcessClientProtocolVersion(rfbClientPtr cl);
static void rfbProcessClientInitMessage(rfbClientPtr cl);
static void rfbSendInteractionCaps(rfbClientPtr cl);
static void rfbProcessClientNormalMessage(rfbClientPtr cl);
static Bool rfbSendCopyRegion(rfbClientPtr cl, RegionPtr reg, int dx, int dy);
static Bool rfbSendLastRectMarker(rfbClientPtr cl);
Bool rfbSendDesktopSize(rfbClientPtr cl);
Bool rfbSendExtDesktopSize(rfbClientPtr cl);
/*
* Session capture
*/
char *captureFile = NULL;
static void WriteCapture(int captureFD, char *buf, int len)
{
if (write(captureFD, buf, len) < len)
rfbLogPerror("WriteCapture: Could not write to capture file");
}
/*
* Idle timeout
*/
CARD32 rfbMaxIdleTimeout = 0;
CARD32 rfbIdleTimeout = 0;
static double idleTimeout = -1.0;
void IdleTimerSet(void)
{
idleTimeout = gettime() + (double)rfbIdleTimeout;
}
static void IdleTimerCancel(void)
{
idleTimeout = -1.0;
}
void IdleTimerCheck(void)
{
if (idleTimeout >= 0.0 && gettime() >= idleTimeout)
FatalError("TurboVNC session has been idle for %u seconds. Exiting.",
(unsigned int)rfbIdleTimeout);
}
/*
* Profiling stuff
*/
static BOOL rfbProfile = FALSE;
static double tUpdate = 0., tStart = -1., tElapsed, mpixels = 0.,
idmpixels = 0.;
static unsigned long iter = 0;
unsigned long long sendBytes = 0;
double gettime(void)
{
struct timeval __tv;
gettimeofday(&__tv, (struct timezone *)NULL);
return (double)__tv.tv_sec + (double)__tv.tv_usec * 0.000001;
}
/*
* Auto Lossless Refresh
*/
static Bool putImageOnly = TRUE, alrCopyRect = TRUE;
static CARD32 alrCallback(OsTimerPtr timer, CARD32 time, pointer arg)
{
RegionRec copyRegionSave, modifiedRegionSave, requestedRegionSave,
ifRegionSave;
rfbClientPtr cl = (rfbClientPtr)arg;
int tightCompressLevelSave, tightQualityLevelSave, copyDXSave, copyDYSave,
tightSubsampLevelSave;
RegionRec tmpRegion;
REGION_INIT(pScreen, &tmpRegion, NullBox, 0);
if (putImageOnly && !cl->firstUpdate)
REGION_INTERSECT(pScreen, &tmpRegion, &cl->alrRegion, &cl->lossyRegion);
else
REGION_COPY(pScreen, &tmpRegion, &cl->lossyRegion);
if (cl->firstUpdate) cl->firstUpdate = FALSE;
if (REGION_NOTEMPTY(pScreen, &tmpRegion)) {
tightCompressLevelSave = cl->tightCompressLevel;
tightQualityLevelSave = cl->tightQualityLevel;
tightSubsampLevelSave = cl->tightSubsampLevel;
copyDXSave = cl->copyDX;
copyDYSave = cl->copyDY;
REGION_INIT(pScreen, ©RegionSave, NullBox, 0);
REGION_COPY(pScreen, ©RegionSave, &cl->copyRegion);
REGION_INIT(pScreen, &modifiedRegionSave, NullBox, 0);
REGION_COPY(pScreen, &modifiedRegionSave, &cl->modifiedRegion);
REGION_INIT(pScreen, &requestedRegionSave, NullBox, 0);
REGION_COPY(pScreen, &requestedRegionSave, &cl->requestedRegion);
REGION_INIT(pScreen, &ifRegionSave, NullBox, 0);
REGION_COPY(pScreen, &ifRegionSave, &cl->ifRegion);
cl->tightCompressLevel = 1;
cl->tightQualityLevel = rfbALRQualityLevel;
cl->tightSubsampLevel = rfbALRSubsampLevel;
cl->copyDX = cl->copyDY = 0;
REGION_EMPTY(pScreen, &cl->copyRegion);
REGION_EMPTY(pScreen, &cl->modifiedRegion);
REGION_UNION(pScreen, &cl->modifiedRegion, &cl->modifiedRegion,
&tmpRegion);
REGION_EMPTY(pScreen, &cl->requestedRegion);
REGION_UNION(pScreen, &cl->requestedRegion, &cl->requestedRegion,
&tmpRegion);
if (cl->compareFB) {
REGION_EMPTY(pScreen, &cl->ifRegion);
REGION_UNION(pScreen, &cl->ifRegion, &cl->ifRegion, &tmpRegion);
}
if (!rfbSendFramebufferUpdate(cl)) return 0;
REGION_EMPTY(pScreen, &cl->lossyRegion);
REGION_EMPTY(pScreen, &cl->alrRegion);
cl->tightCompressLevel = tightCompressLevelSave;
cl->tightQualityLevel = tightQualityLevelSave;
cl->tightSubsampLevel = tightSubsampLevelSave;
cl->copyDX = copyDXSave;
cl->copyDY = copyDYSave;
REGION_COPY(pScreen, &cl->copyRegion, ©RegionSave);
REGION_COPY(pScreen, &cl->modifiedRegion, &modifiedRegionSave);
REGION_COPY(pScreen, &cl->requestedRegion, &requestedRegionSave);
REGION_UNINIT(pScreen, ©RegionSave);
REGION_UNINIT(pScreen, &modifiedRegionSave);
REGION_UNINIT(pScreen, &requestedRegionSave);
if (cl->compareFB) {
REGION_COPY(pScreen, &cl->ifRegion, &ifRegionSave);
REGION_UNINIT(pScreen, &ifRegionSave);
}
}
REGION_UNINIT(pScreen, &tmpRegion);
return 0;
}
static CARD32 updateCallback(OsTimerPtr timer, CARD32 time, pointer arg)
{
rfbClientPtr cl = (rfbClientPtr)arg;
rfbSendFramebufferUpdate(cl);
return 0;
}
/*
* Interframe comparison
*/
int rfbInterframe = -1; /* -1 = auto (determined by compression level) */
Bool InterframeOn(rfbClientPtr cl)
{
if (!cl->compareFB) {
if (!(cl->compareFB =
(char *)malloc(rfbFB.paddedWidthInBytes * rfbFB.height))) {
rfbLogPerror("InterframeOn: couldn't allocate comparison buffer");
return FALSE;
}
memset(cl->compareFB, 0, rfbFB.paddedWidthInBytes * rfbFB.height);
REGION_INIT(pScreen, &cl->ifRegion, NullBox, 0);
cl->firstCompare = TRUE;
rfbLog("Interframe comparison enabled\n");
}
cl->fb = cl->compareFB;
return TRUE;
}
void InterframeOff(rfbClientPtr cl)
{
if (cl->compareFB) {
free(cl->compareFB);
REGION_UNINIT(pScreen, &cl->ifRegion);
rfbLog("Interframe comparison disabled\n");
}
cl->compareFB = NULL;
cl->fb = rfbFB.pfbMemory;
}
/*
* Map of quality levels to provide compatibility with TightVNC/TigerVNC
* clients
*/
static int JPEG_QUAL[10] = {
15, 29, 41, 42, 62, 77, 79, 86, 92, 100
};
static int JPEG_SUBSAMP[10] = {
1, 1, 1, 2, 2, 2, 0, 0, 0, 0
};
/*
* rfbNewClientConnection is called from sockets.c when a new connection
* comes in.
*/
void rfbNewClientConnection(int sock)
{
rfbNewClient(sock);
}
/*
* rfbReverseConnection is called to make an outward connection to a
* "listening" RFB client.
*/
rfbClientPtr rfbReverseConnection(char *host, int port, int id)
{
int sock;
rfbClientPtr cl;
if (rfbAuthDisableRevCon) {
rfbLog("Reverse connections disabled\n");
return (rfbClientPtr)NULL;
}
if ((sock = rfbConnect(host, port)) < 0)
return (rfbClientPtr)NULL;
if (id > 0) {
rfbClientRec cl;
char temps[250];
memset(temps, 0, 250);
snprintf(temps, 250, "ID:%d", id);
rfbLog("UltraVNC Repeater Mode II ID is %d\n", id);
cl.sock = sock;
if (WriteExact(&cl, temps, 250) < 0) {
rfbLogPerror("rfbReverseConnection: write");
rfbCloseSock(sock);
return NULL;
}
}
cl = rfbNewClient(sock);
if (cl)
cl->reverseConnection = TRUE;
return cl;
}
/*
* rfbNewClient is called when a new connection has been made by whatever
* means.
*/
static rfbClientPtr rfbNewClient(int sock)
{
rfbProtocolVersionMsg pv;
rfbClientPtr cl;
BoxRec box;
rfbSockAddr addr;
socklen_t addrlen = sizeof(struct sockaddr_storage);
char addrStr[INET6_ADDRSTRLEN];
char *env = NULL;
int np = sysconf(_SC_NPROCESSORS_CONF);
if (rfbClientHead == NULL)
/* no other clients - make sure we don't think any keys are pressed */
KbdReleaseAllKeys();
cl = (rfbClientPtr)rfbAlloc0(sizeof(rfbClientRec));
if (rfbClientHead == NULL && captureFile) {
cl->captureFD = open(captureFile, O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IWUSR);
if (cl->captureFD < 0)
rfbLogPerror("Could not open capture file");
else
rfbLog("Opened capture file %s\n", captureFile);
} else
cl->captureFD = -1;
cl->sock = sock;
getpeername(sock, &addr.u.sa, &addrlen);
cl->host = strdup(sockaddr_string(&addr, addrStr, INET6_ADDRSTRLEN));
/* Dispatch client input to rfbProcessClientProtocolVersion(). */
cl->state = RFB_PROTOCOL_VERSION;
cl->preferredEncoding = rfbEncodingTight;
cl->correMaxWidth = 48;
cl->correMaxHeight = 48;
REGION_INIT(pScreen, &cl->copyRegion, NullBox, 0);
box.x1 = box.y1 = 0;
box.x2 = rfbFB.width;
box.y2 = rfbFB.height;
REGION_INIT(pScreen, &cl->modifiedRegion, &box, 0);
REGION_INIT(pScreen, &cl->requestedRegion, NullBox, 0);
cl->deferredUpdateStart = gettime();
cl->format = rfbServerFormat;
cl->translateFn = rfbTranslateNone;
cl->tightCompressLevel = TIGHT_DEFAULT_COMPRESSION;
cl->tightSubsampLevel = TIGHT_DEFAULT_SUBSAMP;
cl->tightQualityLevel = -1;
cl->imageQualityLevel = -1;
cl->next = rfbClientHead;
cl->prev = NULL;
if (rfbClientHead)
rfbClientHead->prev = cl;
rfbClientHead = cl;
rfbResetStats(cl);
cl->zlibCompressLevel = 5;
sprintf(pv, rfbProtocolVersionFormat, 3, 8);
if (WriteExact(cl, pv, sz_rfbProtocolVersionMsg) < 0) {
rfbLogPerror("rfbNewClient: write");
rfbCloseClient(cl);
return NULL;
}
if ((env = getenv("TVNC_PROFILE")) != NULL && !strcmp(env, "1"))
rfbProfile = TRUE;
if ((env = getenv("TVNC_ICEDEBUG")) != NULL && !strcmp(env, "1"))
rfbInterframeDebug = TRUE;
if ((env = getenv("TVNC_ICEBLOCKSIZE")) != NULL) {
int iceBlockSize = atoi(env);
if (iceBlockSize >= 0) rfbICEBlockSize = iceBlockSize;
}
if ((env = getenv("TVNC_COMBINERECT")) != NULL) {
int combine = atoi(env);
if (combine > 0 && combine <= 65000) rfbCombineRect = combine;
}
cl->firstUpdate = TRUE;
/* The TigerVNC Viewer won't enable remote desktop resize until it receives
a desktop resize message from the server, so we give it one with the
first FBU. */
cl->reason = rfbEDSReasonServer;
cl->result = rfbEDSResultSuccess;
if (rfbAutoLosslessRefresh > 0.0) {
REGION_INIT(pScreen, &cl->lossyRegion, NullBox, 0);
if ((env = getenv("TVNC_ALRALL")) != NULL && !strcmp(env, "1"))
putImageOnly = FALSE;
if ((env = getenv("TVNC_ALRCOPYRECT")) != NULL && !strcmp(env, "0"))
alrCopyRect = FALSE;
REGION_INIT(pScreen, &cl->alrRegion, NullBox, 0);
REGION_INIT(pScreen, &cl->alrEligibleRegion, NullBox, 0);
}
if ((env = getenv("TVNC_MT")) != NULL && !strcmp(env, "0"))
rfbMT = FALSE;
if ((env = getenv("TVNC_NTHREADS")) != NULL && strlen(env) >= 1) {
int temp = atoi(env);
if (temp >= 1 && temp <= MAX_ENCODING_THREADS)
rfbNumThreads = temp;
else
rfbLog("WARNING: Invalid value of TVNC_NTHREADS (%s) ignored\n", env);
}
if (np == -1 && rfbMT) {
rfbLog("WARNING: Could not determine CPU count. Multithreaded encoding disabled.\n");
rfbMT = FALSE;
}
if (!rfbMT) rfbNumThreads = 1;
else if (rfbNumThreads < 1) rfbNumThreads = min(np, 4);
if (rfbNumThreads > np) {
rfbLog("NOTICE: Encoding thread count has been clamped to CPU count\n");
rfbNumThreads = np;
}
if (rfbIdleTimeout > 0)
IdleTimerCancel();
cl->baseRTT = cl->minRTT = (unsigned)-1;
gettimeofday(&cl->lastWrite, NULL);
REGION_INIT(pScreen, &cl->cuRegion, NullBox, 0);
if (rfbInterframe == 1) {
if (!InterframeOn(cl)) {
rfbCloseClient(cl);
return NULL;
}
} else
InterframeOff(cl);
return cl;
}
/*
* rfbClientConnectionGone is called from sockets.c just after a connection
* has gone away.
*/
void rfbClientConnectionGone(rfbClientPtr cl)
{
int i;
if (cl->prev)
cl->prev->next = cl->next;
else
rfbClientHead = cl->next;
if (cl->next)
cl->next->prev = cl->prev;
TimerFree(cl->alrTimer);
TimerFree(cl->deferredUpdateTimer);
TimerFree(cl->updateTimer);
TimerFree(cl->congestionTimer);
#ifdef XVNC_AuthPAM
rfbPAMEnd(cl);
#endif
if (cl->login != NULL) {
rfbLog("Client %s (%s) gone\n", cl->login, cl->host);
free(cl->login);
} else {
rfbLog("Client %s gone\n", cl->host);
}
free(cl->host);
ShutdownTightThreads();
if (rfbAutoLosslessRefresh > 0.0) {
REGION_UNINIT(pScreen, &cl->lossyRegion);
REGION_UNINIT(pScreen, &cl->alrRegion);
REGION_UNINIT(pScreen, &cl->alrEligibleRegion);
}
/* Release the compression state structures if any. */
if (cl->compStreamInited == TRUE)
deflateEnd(&(cl->compStream));
for (i = 0; i < 4; i++) {
if (cl->zsActive[i])
deflateEnd(&cl->zsStruct[i]);
}
if (pointerClient == cl)
pointerClient = NULL;
REGION_UNINIT(pScreen, &cl->copyRegion);
REGION_UNINIT(pScreen, &cl->modifiedRegion);
rfbPrintStats(cl);
if (cl->translateLookupTable) free(cl->translateLookupTable);
rfbFreeZrleData(cl);
if (cl->cutText)
free(cl->cutText);
InterframeOff(cl);
i = cl->numDevices;
while (i-- > 0)
RemoveExtInputDevice(cl, 0);
if (cl->captureFD >= 0)
close(cl->captureFD);
free(cl);
if (rfbClientHead == NULL && rfbIdleTimeout > 0)
IdleTimerSet();
}
/*
* rfbProcessClientMessage is called when there is data to read from a client.
*/
void rfbProcessClientMessage(rfbClientPtr cl)
{
rfbCorkSock(cl->sock);
if (cl->pendingSyncFence) {
cl->syncFence = TRUE;
cl->pendingSyncFence = FALSE;
}
switch (cl->state) {
case RFB_PROTOCOL_VERSION:
rfbProcessClientProtocolVersion(cl);
break;
case RFB_SECURITY_TYPE: /* protocol versions 3.7 and above */
rfbProcessClientSecurityType(cl);
break;
case RFB_TUNNELING_TYPE: /* protocol versions 3.7t, 3.8t */
rfbProcessClientTunnelingType(cl);
break;
case RFB_AUTH_TYPE: /* protocol versions 3.7t, 3.8t */
rfbProcessClientAuthType(cl);
break;
#if USETLS
case RFB_TLS_HANDSHAKE:
rfbAuthTLSHandshake(cl);
break;
#endif
case RFB_AUTHENTICATION:
rfbAuthProcessResponse(cl);
break;
case RFB_INITIALISATION:
rfbInitFlowControl(cl);
rfbProcessClientInitMessage(cl);
break;
default:
rfbProcessClientNormalMessage(cl);
}
CHECK_CLIENT_PTR(cl, return)
if (cl->syncFence) {
if (!rfbSendFence(cl, cl->fenceFlags, cl->fenceDataLen, cl->fenceData))
return;
cl->syncFence = FALSE;
}
rfbUncorkSock(cl->sock);
}
/*
* rfbProcessClientProtocolVersion is called when the client sends its
* protocol version.
*/
static void rfbProcessClientProtocolVersion(rfbClientPtr cl)
{
rfbProtocolVersionMsg pv;
int n, major, minor;
if ((n = ReadExact(cl, pv, sz_rfbProtocolVersionMsg)) <= 0) {
if (n == 0)
rfbLog("rfbProcessClientProtocolVersion: client gone\n");
else
rfbLogPerror("rfbProcessClientProtocolVersion: read");
rfbCloseClient(cl);
return;
}
pv[sz_rfbProtocolVersionMsg] = 0;
if (sscanf(pv, rfbProtocolVersionFormat, &major, &minor) != 2) {
rfbLog("rfbProcessClientProtocolVersion: not a valid RFB client\n");
rfbCloseClient(cl);
return;
}
if (major != 3) {
rfbLog("Unsupported protocol version %d.%d\n", major, minor);
rfbCloseClient(cl);
return;
}
/* Always use one of the three standard versions of the RFB protocol. */
cl->protocol_minor_ver = minor;
if (minor > 8) /* buggy client */
cl->protocol_minor_ver = 8;
else if (minor > 3 && minor < 7) /* non-standard client */
cl->protocol_minor_ver = 3;
else if (minor < 3) /* ancient client */
cl->protocol_minor_ver = 3;
if (cl->protocol_minor_ver != minor)
rfbLog("Non-standard protocol version 3.%d, using 3.%d instead\n", minor,
cl->protocol_minor_ver);
else
rfbLog("Using protocol version 3.%d\n", cl->protocol_minor_ver);
/* TightVNC protocol extensions are not enabled yet. */
cl->protocol_tightvnc = FALSE;
rfbAuthNewClient(cl);
}
/*
* rfbProcessClientInitMessage is called when the client sends its
* initialisation message.
*/
static void rfbProcessClientInitMessage(rfbClientPtr cl)
{
rfbClientInitMsg ci;
char buf[256];
rfbServerInitMsg *si = (rfbServerInitMsg *)buf;
int len, n;
rfbClientPtr otherCl, nextCl;
if ((n = ReadExact(cl, (char *)&ci, sz_rfbClientInitMsg)) <= 0) {
if (n == 0)
rfbLog("rfbProcessClientInitMessage: client gone\n");
else
rfbLogPerror("rfbProcessClientInitMessage: read");
rfbCloseClient(cl);
return;
}
si->framebufferWidth = Swap16IfLE(rfbFB.width);
si->framebufferHeight = Swap16IfLE(rfbFB.height);
si->format = rfbServerFormat;
si->format.redMax = Swap16IfLE(si->format.redMax);
si->format.greenMax = Swap16IfLE(si->format.greenMax);
si->format.blueMax = Swap16IfLE(si->format.blueMax);
if (strlen(desktopName) > 128) /* sanity check on desktop name len */
desktopName[128] = 0;
sprintf(buf + sz_rfbServerInitMsg, "%s", desktopName);
len = strlen(buf + sz_rfbServerInitMsg);
si->nameLength = Swap32IfLE(len);
if (WriteExact(cl, buf, sz_rfbServerInitMsg + len) < 0) {
rfbLogPerror("rfbProcessClientInitMessage: write");
rfbCloseClient(cl);
return;
}
if (cl->protocol_tightvnc)
rfbSendInteractionCaps(cl); /* protocol 3.7t */
/* Dispatch client input to rfbProcessClientNormalMessage(). */
cl->state = RFB_NORMAL;
if (!cl->reverseConnection &&
(rfbNeverShared || (!rfbAlwaysShared && !ci.shared))) {
if (rfbDontDisconnect) {
for (otherCl = rfbClientHead; otherCl; otherCl = otherCl->next) {
if ((otherCl != cl) && (otherCl->state == RFB_NORMAL)) {
rfbLog("-dontdisconnect: Not shared & existing client\n");
rfbLog(" refusing new client %s\n", cl->host);
rfbCloseClient(cl);
return;
}
}
} else {
for (otherCl = rfbClientHead; otherCl; otherCl = nextCl) {
nextCl = otherCl->next;
if ((otherCl != cl) && (otherCl->state == RFB_NORMAL)) {
rfbLog("Not shared - closing connection to client %s\n",
otherCl->host);
rfbCloseClient(otherCl);
}
}
}
}
}
/*
* rfbSendInteractionCaps is called after sending the server
* initialisation message, only if TightVNC protocol extensions were
* enabled (protocol versions 3.7t, 3.8t). In this function, we send
* the lists of supported protocol messages and encodings.
*/
/* Update these constants on changing capability lists below! */
#define N_SMSG_CAPS 0
#define N_CMSG_CAPS 0
#define N_ENC_CAPS 17
void rfbSendInteractionCaps(rfbClientPtr cl)
{
rfbInteractionCapsMsg intr_caps;
rfbCapabilityInfo enc_list[N_ENC_CAPS];
int i;
/* Fill in the header structure sent prior to capability lists. */
intr_caps.nServerMessageTypes = Swap16IfLE(N_SMSG_CAPS);
intr_caps.nClientMessageTypes = Swap16IfLE(N_CMSG_CAPS);
intr_caps.nEncodingTypes = Swap16IfLE(N_ENC_CAPS);
intr_caps.pad = 0;
/* Supported server->client message types. */
/* For future file transfer support:
i = 0;
SetCapInfo(&smsg_list[i++], rfbFileListData, rfbTightVncVendor);
SetCapInfo(&smsg_list[i++], rfbFileDownloadData, rfbTightVncVendor);
SetCapInfo(&smsg_list[i++], rfbFileUploadCancel, rfbTightVncVendor);
SetCapInfo(&smsg_list[i++], rfbFileDownloadFailed, rfbTightVncVendor);
if (i != N_SMSG_CAPS) {
rfbLog("rfbSendInteractionCaps: assertion failed, i != N_SMSG_CAPS\n");
rfbCloseClient(cl);
return;
}
*/
/* Supported client->server message types. */
/* For future file transfer support:
i = 0;
SetCapInfo(&cmsg_list[i++], rfbFileListRequest, rfbTightVncVendor);
SetCapInfo(&cmsg_list[i++], rfbFileDownloadRequest, rfbTightVncVendor);
SetCapInfo(&cmsg_list[i++], rfbFileUploadRequest, rfbTightVncVendor);
SetCapInfo(&cmsg_list[i++], rfbFileUploadData, rfbTightVncVendor);
SetCapInfo(&cmsg_list[i++], rfbFileDownloadCancel, rfbTightVncVendor);
SetCapInfo(&cmsg_list[i++], rfbFileUploadFailed, rfbTightVncVendor);
if (i != N_CMSG_CAPS) {
rfbLog("rfbSendInteractionCaps: assertion failed, i != N_CMSG_CAPS\n");
rfbCloseClient(cl);
return;
}
*/
/* Encoding types. */
i = 0;
SetCapInfo(&enc_list[i++], rfbEncodingCopyRect, rfbStandardVendor);
SetCapInfo(&enc_list[i++], rfbEncodingRRE, rfbStandardVendor);
SetCapInfo(&enc_list[i++], rfbEncodingCoRRE, rfbStandardVendor);
SetCapInfo(&enc_list[i++], rfbEncodingHextile, rfbStandardVendor);
SetCapInfo(&enc_list[i++], rfbEncodingZlib, rfbTridiaVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingZRLE, rfbTridiaVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingZYWRLE, rfbTridiaVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingTight, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingCompressLevel0, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingQualityLevel0, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingFineQualityLevel0, rfbTurboVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingSubsamp1X, rfbTurboVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingXCursor, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingRichCursor, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingPointerPos, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbEncodingLastRect, rfbTightVncVendor);
SetCapInfo(&enc_list[i++], rfbGIIServer, rfbGIIVendor);
if (i != N_ENC_CAPS) {
rfbLog("rfbSendInteractionCaps: assertion failed, i != N_ENC_CAPS\n");
rfbCloseClient(cl);
return;
}
/* Send header and capability lists */
if (WriteExact(cl, (char *)&intr_caps,
sz_rfbInteractionCapsMsg) < 0 ||
WriteExact(cl, (char *)&enc_list[0],
sz_rfbCapabilityInfo * N_ENC_CAPS) < 0) {
rfbLogPerror("rfbSendInteractionCaps: write");
rfbCloseClient(cl);
return;
}
/* Dispatch client input to rfbProcessClientNormalMessage(). */
cl->state = RFB_NORMAL;
}
/*
* rfbProcessClientNormalMessage is called when the client has sent a normal
* protocol message.
*/
#define READ(addr, numBytes) \
if ((n = ReadExact(cl, addr, numBytes)) <= 0) { \
if (n != 0) \
rfbLogPerror("rfbProcessClientNormalMessage: read"); \
rfbCloseClient(cl); \
return; \
}
#define SKIP(numBytes) \
if ((n = SkipExact(cl, numBytes)) <= 0) { \
if (n != 0) \
rfbLogPerror("rfbProcessClientNormalMessage: skip"); \
rfbCloseClient(cl); \
return; \
}
static void rfbProcessClientNormalMessage(rfbClientPtr cl)
{
int n;
rfbClientToServerMsg msg;
char *str;
READ((char *)&msg, 1)
switch (msg.type) {
case rfbSetPixelFormat:
READ(((char *)&msg) + 1, sz_rfbSetPixelFormatMsg - 1)
cl->format.bitsPerPixel = msg.spf.format.bitsPerPixel;
cl->format.depth = msg.spf.format.depth;
cl->format.bigEndian = (msg.spf.format.bigEndian ? 1 : 0);
cl->format.trueColour = (msg.spf.format.trueColour ? 1 : 0);
cl->format.redMax = Swap16IfLE(msg.spf.format.redMax);
cl->format.greenMax = Swap16IfLE(msg.spf.format.greenMax);
cl->format.blueMax = Swap16IfLE(msg.spf.format.blueMax);
cl->format.redShift = msg.spf.format.redShift;
cl->format.greenShift = msg.spf.format.greenShift;
cl->format.blueShift = msg.spf.format.blueShift;
cl->readyForSetColourMapEntries = TRUE;
rfbSetTranslateFunction(cl);
return;
case rfbFixColourMapEntries:
READ(((char *)&msg) + 1, sz_rfbFixColourMapEntriesMsg - 1)
rfbLog("rfbProcessClientNormalMessage: FixColourMapEntries unsupported\n");
rfbCloseClient(cl);
return;
case rfbSetEncodings:
{
int i;
CARD32 enc;
Bool firstFence = !cl->enableFence;
Bool firstCU = !cl->enableCU;
Bool firstGII = !cl->enableGII;
Bool logTightCompressLevel = FALSE;
READ(((char *)&msg) + 1, sz_rfbSetEncodingsMsg - 1)
msg.se.nEncodings = Swap16IfLE(msg.se.nEncodings);
cl->preferredEncoding = -1;
cl->useCopyRect = FALSE;
cl->enableCursorShapeUpdates = FALSE;
cl->enableCursorPosUpdates = FALSE;
cl->enableLastRectEncoding = FALSE;
cl->tightCompressLevel = TIGHT_DEFAULT_COMPRESSION;
cl->tightSubsampLevel = TIGHT_DEFAULT_SUBSAMP;
cl->tightQualityLevel = -1;
cl->imageQualityLevel = -1;
for (i = 0; i < msg.se.nEncodings; i++) {
READ((char *)&enc, 4)
enc = Swap32IfLE(enc);
switch (enc) {
case rfbEncodingCopyRect:
cl->useCopyRect = TRUE;
break;
case rfbEncodingRaw:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using raw encoding for client %s\n", cl->host);
}
break;
case rfbEncodingRRE:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using rre encoding for client %s\n", cl->host);
}
break;
case rfbEncodingCoRRE:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using CoRRE encoding for client %s\n", cl->host);
}
break;
case rfbEncodingHextile:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using hextile encoding for client %s\n", cl->host);
}
break;
case rfbEncodingZlib:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using zlib encoding for client %s\n", cl->host);
}
break;
case rfbEncodingZRLE:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using ZRLE encoding for client %s\n", cl->host);
}
break;
case rfbEncodingZYWRLE:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using ZYWRLE encoding for client %s\n", cl->host);
}
break;
case rfbEncodingTight:
if (cl->preferredEncoding == -1) {
cl->preferredEncoding = enc;
rfbLog("Using tight encoding for client %s\n", cl->host);
}
break;
case rfbEncodingXCursor:
if (!cl->enableCursorShapeUpdates) {
rfbLog("Enabling X-style cursor updates for client %s\n",
cl->host);
cl->enableCursorShapeUpdates = TRUE;
cl->useRichCursorEncoding = FALSE;
cl->cursorWasChanged = TRUE;
}
break;
case rfbEncodingRichCursor:
if (!cl->enableCursorShapeUpdates) {
rfbLog("Enabling full-color cursor updates for client %s\n",
cl->host);
cl->enableCursorShapeUpdates = TRUE;
cl->useRichCursorEncoding = TRUE;
cl->cursorWasChanged = TRUE;
}
break;
case rfbEncodingPointerPos:
if (!cl->enableCursorPosUpdates) {
rfbLog("Enabling cursor position updates for client %s\n",