-
-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathrfbserver.c
More file actions
4188 lines (3632 loc) · 130 KB
/
rfbserver.c
File metadata and controls
4188 lines (3632 loc) · 130 KB
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) 2011-2012 D. R. Commander
* Copyright (C) 2005 Rohit Kumar, Johannes E. Schindelin
* Copyright (C) 2002 RealVNC Ltd.
* OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
* Original Xvnc code 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef __STRICT_ANSI__
#define _BSD_SOURCE
#define _POSIX_SOURCE
#define _XOPEN_SOURCE 600
#endif
#include <stdio.h>
#include <string.h>
#include <rfb/rfb.h>
#include <rfb/rfbregion.h>
#include "private.h"
#include "rfb/rfbconfig.h"
#ifdef LIBVNCSERVER_HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef WIN32
#include <io.h>
#else
#include <pwd.h>
#endif
#include "sockets.h"
#ifdef DEBUGPROTO
#undef DEBUGPROTO
#define DEBUGPROTO(x) x
#else
#define DEBUGPROTO(x)
#endif
#include <stdarg.h>
#include "scale.h"
/* stst() */
#include <sys/types.h>
#include <sys/stat.h>
#if LIBVNCSERVER_HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef WIN32
/* readdir() */
#include <dirent.h>
#endif
/* errno */
#include <errno.h>
/* strftime() */
#include <time.h>
/* INT_MAX */
#include <limits.h>
#ifdef LIBVNCSERVER_WITH_WEBSOCKETS
#include "rfbssl.h"
#endif
#ifdef _MSC_VER
/* Prevent POSIX deprecation warnings */
#define close _close
#define strdup _strdup
#endif
#ifdef WIN32
#include <direct.h>
#ifdef __MINGW32__
#define mkdir(path, perms) mkdir(path) /* Omit the perms argument to match POSIX signature */
#else /* MSVC and other windows compilers */
#define mkdir(path, perms) _mkdir(path) /* Omit the perms argument to match POSIX signature */
#endif /* __MINGW32__ else... */
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR)
#endif
#endif
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
/*
* Map of quality levels to provide compatibility with TightVNC/TigerVNC
* clients. This emulates the behavior of the TigerVNC Server.
*/
static const int tight2turbo_qual[10] = {
15, 29, 41, 42, 62, 77, 79, 86, 92, 100
};
static const int tight2turbo_subsamp[10] = {
1, 1, 1, 2, 2, 2, 0, 0, 0, 0
};
#endif
static void rfbProcessClientProtocolVersion(rfbClientPtr cl);
static void rfbProcessClientNormalMessage(rfbClientPtr cl);
static void rfbProcessClientInitMessage(rfbClientPtr cl);
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
void rfbIncrClientRef(rfbClientPtr cl)
{
LOCK(cl->refCountMutex);
cl->refCount++;
UNLOCK(cl->refCountMutex);
}
void rfbDecrClientRef(rfbClientPtr cl)
{
LOCK(cl->refCountMutex);
cl->refCount--;
if(cl->refCount<=0) /* just to be sure also < 0 */
TSIGNAL(cl->deleteCond);
UNLOCK(cl->refCountMutex);
}
#else
void rfbIncrClientRef(rfbClientPtr cl) {}
void rfbDecrClientRef(rfbClientPtr cl) {}
#endif
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
static MUTEX(rfbClientListMutex);
#endif
struct rfbClientIterator {
rfbClientPtr next;
rfbScreenInfoPtr screen;
rfbBool closedToo;
};
void
rfbClientListInit(rfbScreenInfoPtr rfbScreen)
{
if(sizeof(rfbBool)!=1) {
/* a sanity check */
fprintf(stderr,"rfbBool's size is not 1 (%d)!\n",(int)sizeof(rfbBool));
/* we cannot continue, because rfbBool is supposed to be char everywhere */
exit(1);
}
rfbScreen->clientHead = NULL;
INIT_MUTEX(rfbClientListMutex);
}
rfbClientIteratorPtr
rfbGetClientIterator(rfbScreenInfoPtr rfbScreen)
{
rfbClientIteratorPtr i =
(rfbClientIteratorPtr)malloc(sizeof(struct rfbClientIterator));
if(i) {
i->next = NULL;
i->screen = rfbScreen;
i->closedToo = FALSE;
}
return i;
}
rfbClientIteratorPtr
rfbGetClientIteratorWithClosed(rfbScreenInfoPtr rfbScreen)
{
rfbClientIteratorPtr i =
(rfbClientIteratorPtr)malloc(sizeof(struct rfbClientIterator));
if(i) {
i->next = NULL;
i->screen = rfbScreen;
i->closedToo = TRUE;
}
return i;
}
rfbClientPtr
rfbClientIteratorHead(rfbClientIteratorPtr i)
{
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
if(i->next != 0) {
rfbDecrClientRef(i->next);
rfbIncrClientRef(i->screen->clientHead);
}
#endif
LOCK(rfbClientListMutex);
i->next = i->screen->clientHead;
UNLOCK(rfbClientListMutex);
return i->next;
}
rfbClientPtr
rfbClientIteratorNext(rfbClientIteratorPtr i)
{
if (!i)
return NULL;
if(i->next == 0) {
LOCK(rfbClientListMutex);
i->next = i->screen->clientHead;
UNLOCK(rfbClientListMutex);
} else {
rfbClientPtr cl = i->next;
i->next = i->next->next;
rfbDecrClientRef(cl);
}
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
if(!i->closedToo)
while(i->next && i->next->sock<0)
i->next = i->next->next;
if(i->next)
rfbIncrClientRef(i->next);
#endif
return i->next;
}
void
rfbReleaseClientIterator(rfbClientIteratorPtr iterator)
{
if(iterator && iterator->next) rfbDecrClientRef(iterator->next);
free(iterator);
}
/*
* rfbNewClientConnection is called from sockets.c when a new connection
* comes in.
*/
void
rfbNewClientConnection(rfbScreenInfoPtr rfbScreen,
rfbSocket sock)
{
rfbNewClient(rfbScreen,sock);
}
/*
* rfbReverseConnection is called to make an outward
* connection to a "listening" RFB client.
*/
rfbClientPtr
rfbReverseConnection(rfbScreenInfoPtr rfbScreen,
char *host,
int port)
{
rfbSocket sock;
rfbClientPtr cl;
if ((sock = rfbConnect(rfbScreen, host, port)) < 0)
return (rfbClientPtr)NULL;
cl = rfbNewClient(rfbScreen, sock);
if (cl) {
cl->reverseConnection = TRUE;
if (!cl->onHold)
rfbStartOnHoldClient(cl);
}
return cl;
}
void
rfbSetProtocolVersion(rfbScreenInfoPtr rfbScreen, int major_, int minor_)
{
/* Permit the server to set the version to report */
/* TODO: sanity checking */
if ((major_==3) && (minor_ > 2 && minor_ < 9))
{
rfbScreen->protocolMajorVersion = major_;
rfbScreen->protocolMinorVersion = minor_;
}
else
rfbLog("rfbSetProtocolVersion(%d,%d) set to invalid values\n", major_, minor_);
}
/*
* rfbNewClient is called when a new connection has been made by whatever
* means.
*/
static rfbClientPtr
rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen,
rfbSocket sock,
rfbBool isUDP)
{
rfbProtocolVersionMsg pv;
rfbClientIteratorPtr iterator;
rfbClientPtr cl,cl_;
#ifdef LIBVNCSERVER_IPv6
struct sockaddr_storage addr;
#else
struct sockaddr_in addr;
#endif
socklen_t addrlen = sizeof(addr);
rfbProtocolExtension* extension;
cl = (rfbClientPtr)calloc(sizeof(rfbClientRec),1);
if (!cl)
return NULL;
cl->screen = rfbScreen;
cl->sock = sock;
cl->viewOnly = FALSE;
/* setup pseudo scaling */
cl->scaledScreen = rfbScreen;
cl->scaledScreen->scaledScreenRefCount++;
rfbResetStats(cl);
cl->clientData = NULL;
cl->clientGoneHook = rfbDoNothingWithClient;
if(isUDP) {
rfbLog(" accepted UDP client\n");
} else {
#ifdef LIBVNCSERVER_IPv6
char host[1024];
#endif
int one=1;
size_t otherClientsCount = 0;
getpeername(sock, (struct sockaddr *)&addr, &addrlen);
#ifdef LIBVNCSERVER_IPv6
if(getnameinfo((struct sockaddr*)&addr, addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST) != 0) {
rfbLogPerror("rfbNewClient: error in getnameinfo");
cl->host = strdup("");
}
else
cl->host = strdup(host);
#else
cl->host = strdup(inet_ntoa(addr.sin_addr));
#endif
iterator = rfbGetClientIterator(rfbScreen);
while ((cl_ = rfbClientIteratorNext(iterator)) != NULL)
++otherClientsCount;
rfbReleaseClientIterator(iterator);
rfbLog(" %lu other clients\n", (unsigned long) otherClientsCount);
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if(!rfbSetNonBlocking(sock)) {
rfbCloseSocket(sock);
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
return NULL;
}
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(char *)&one, sizeof(one)) < 0) {
rfbLogPerror("setsockopt failed: can't set TCP_NODELAY flag, non TCP socket?");
}
FD_SET(sock,&(rfbScreen->allFds));
rfbScreen->maxFd = rfbMax(sock,rfbScreen->maxFd);
#endif
INIT_MUTEX(cl->outputMutex);
INIT_MUTEX(cl->refCountMutex);
INIT_MUTEX(cl->sendMutex);
INIT_COND(cl->deleteCond);
cl->state = RFB_PROTOCOL_VERSION;
cl->reverseConnection = FALSE;
cl->readyForSetColourMapEntries = FALSE;
cl->useCopyRect = FALSE;
cl->preferredEncoding = -1;
cl->correMaxWidth = 48;
cl->correMaxHeight = 48;
#ifdef LIBVNCSERVER_HAVE_LIBZ
cl->zrleData = NULL;
#endif
cl->copyRegion = sraRgnCreate();
cl->copyDX = 0;
cl->copyDY = 0;
cl->modifiedRegion =
sraRgnCreateRect(0,0,rfbScreen->width,rfbScreen->height);
INIT_MUTEX(cl->updateMutex);
INIT_COND(cl->updateCond);
cl->requestedRegion = sraRgnCreate();
cl->format = cl->screen->serverFormat;
cl->translateFn = rfbTranslateNone;
cl->translateLookupTable = NULL;
LOCK(rfbClientListMutex);
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
cl->refCount = 0;
#endif
cl->next = rfbScreen->clientHead;
cl->prev = NULL;
if (rfbScreen->clientHead)
rfbScreen->clientHead->prev = cl;
rfbScreen->clientHead = cl;
UNLOCK(rfbClientListMutex);
#if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG)
cl->tightQualityLevel = -1;
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
cl->tightCompressLevel = TIGHT_DEFAULT_COMPRESSION;
cl->turboSubsampLevel = TURBO_DEFAULT_SUBSAMP;
{
int i;
for (i = 0; i < 4; i++)
cl->zsActive[i] = FALSE;
}
#endif
#endif
cl->fileTransfer.fd = -1;
cl->enableCursorShapeUpdates = FALSE;
cl->enableCursorPosUpdates = FALSE;
cl->useRichCursorEncoding = FALSE;
cl->enableLastRectEncoding = FALSE;
cl->enableKeyboardLedState = FALSE;
cl->enableSupportedMessages = FALSE;
cl->enableSupportedEncodings = FALSE;
cl->enableServerIdentity = FALSE;
cl->lastKeyboardLedState = -1;
cl->cursorX = rfbScreen->cursorX;
cl->cursorY = rfbScreen->cursorY;
cl->useNewFBSize = FALSE;
cl->useExtDesktopSize = FALSE;
cl->requestedDesktopSizeChange = 0;
cl->lastDesktopSizeChangeError = 0;
#ifdef LIBVNCSERVER_HAVE_LIBZ
cl->compStreamInited = FALSE;
cl->compStream.total_in = 0;
cl->compStream.total_out = 0;
cl->compStream.zalloc = Z_NULL;
cl->compStream.zfree = Z_NULL;
cl->compStream.opaque = Z_NULL;
cl->zlibCompressLevel = 5;
#endif
cl->progressiveSliceY = 0;
cl->extensions = NULL;
cl->lastPtrX = -1;
#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
cl->pipe_notify_client_thread[0] = -1;
cl->pipe_notify_client_thread[1] = -1;
#endif
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#ifdef LIBVNCSERVER_WITH_WEBSOCKETS
/*
* Wait a few ms for the client to send WebSockets connection (TLS/SSL or plain)
*/
if (!webSocketsCheck(cl)) {
/* Error reporting handled in webSocketsHandshake */
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
return NULL;
}
#endif
#endif
#ifdef LIBVNCSERVER_HAVE_LIBZ
cl->enableExtendedClipboard = FALSE;
cl->extClipboardUserCap = 0x1B000007; /* text, rtf, html, request, notify, provide */
cl->extClipboardMaxUnsolicitedSize = 20 * (1 << 20); /* 20 MiB */
cl->extClipboardData = NULL;
cl->extClipboardDataSize = 0;
#endif
sprintf(pv,rfbProtocolVersionFormat,rfbScreen->protocolMajorVersion,
rfbScreen->protocolMinorVersion);
if (rfbWriteExact(cl, pv, sz_rfbProtocolVersionMsg) < 0) {
rfbLogPerror("rfbNewClient: write");
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
return NULL;
}
}
for(extension = rfbGetExtensionIterator(); extension;
extension=extension->next) {
void* data = NULL;
/* if the extension does not have a newClient method, it wants
* to be initialized later. */
if(extension->newClient && extension->newClient(cl, &data))
rfbEnableExtension(cl, extension, data);
}
rfbReleaseExtensionIterator();
switch (cl->screen->newClientHook(cl)) {
case RFB_CLIENT_ON_HOLD:
cl->onHold = TRUE;
break;
case RFB_CLIENT_ACCEPT:
cl->onHold = FALSE;
break;
case RFB_CLIENT_REFUSE:
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
cl = NULL;
break;
}
return cl;
}
rfbClientPtr
rfbNewClient(rfbScreenInfoPtr rfbScreen,
rfbSocket sock)
{
return(rfbNewTCPOrUDPClient(rfbScreen,sock,FALSE));
}
rfbClientPtr
rfbNewUDPClient(rfbScreenInfoPtr rfbScreen)
{
return((rfbScreen->udpClient=
rfbNewTCPOrUDPClient(rfbScreen,rfbScreen->udpSock,TRUE)));
}
/*
* rfbClientConnectionGone is called from sockets.c just after a connection
* has gone away.
*/
void
rfbClientConnectionGone(rfbClientPtr cl)
{
#if defined(LIBVNCSERVER_HAVE_LIBZ) && defined(LIBVNCSERVER_HAVE_LIBJPEG)
int i;
#endif
LOCK(rfbClientListMutex);
if (cl->prev)
cl->prev->next = cl->next;
else
cl->screen->clientHead = cl->next;
if (cl->next)
cl->next->prev = cl->prev;
UNLOCK(rfbClientListMutex);
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD) || defined(LIBVNCSERVER_HAVE_WIN32THREADS)
if (cl->screen->backgroundLoop) {
int i;
do {
LOCK(cl->refCountMutex);
i=cl->refCount;
if(i>0)
WAIT(cl->deleteCond,cl->refCountMutex);
UNLOCK(cl->refCountMutex);
} while(i>0);
}
#endif
if(cl->sock != RFB_INVALID_SOCKET)
rfbCloseSocket(cl->sock);
if (cl->scaledScreen!=NULL)
cl->scaledScreen->scaledScreenRefCount--;
#ifdef LIBVNCSERVER_HAVE_LIBZ
rfbFreeZrleData(cl);
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
rfbFreeTightData(cl);
#endif
#endif
rfbFreeUltraData(cl);
/* free buffers holding pixel data before and after encoding */
free(cl->beforeEncBuf);
free(cl->afterEncBuf);
if(cl->sock != RFB_INVALID_SOCKET)
FD_CLR(cl->sock,&(cl->screen->allFds));
cl->clientGoneHook(cl);
rfbLog("Client %s gone\n",cl->host);
free(cl->host);
if (cl->wsctx != NULL){
free(cl->wsctx);
cl->wsctx = NULL;
}
#ifdef LIBVNCSERVER_HAVE_LIBZ
/* Release the compression state structures if any. */
if ( cl->compStreamInited ) {
deflateEnd( &(cl->compStream) );
}
free(cl->extClipboardData);
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
for (i = 0; i < 4; i++) {
if (cl->zsActive[i])
deflateEnd(&cl->zsStruct[i]);
}
#endif
#endif
if (cl->screen->pointerClient == cl)
cl->screen->pointerClient = NULL;
sraRgnDestroy(cl->modifiedRegion);
sraRgnDestroy(cl->requestedRegion);
sraRgnDestroy(cl->copyRegion);
free(cl->translateLookupTable);
TINI_COND(cl->updateCond);
TINI_MUTEX(cl->updateMutex);
/* make sure outputMutex is unlocked before destroying */
LOCK(cl->outputMutex);
UNLOCK(cl->outputMutex);
TINI_MUTEX(cl->outputMutex);
LOCK(cl->sendMutex);
UNLOCK(cl->sendMutex);
TINI_MUTEX(cl->sendMutex);
#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
if (cl->screen->backgroundLoop) {
close(cl->pipe_notify_client_thread[0]);
close(cl->pipe_notify_client_thread[1]);
}
#endif
rfbPrintStats(cl);
rfbResetStats(cl);
free(cl);
}
/*
* rfbProcessClientMessage is called when there is data to read from a client.
*/
void
rfbProcessClientMessage(rfbClientPtr cl)
{
switch (cl->state) {
case RFB_PROTOCOL_VERSION:
rfbProcessClientProtocolVersion(cl);
return;
case RFB_SECURITY_TYPE:
rfbProcessClientSecurityType(cl);
return;
case RFB_AUTHENTICATION:
rfbAuthProcessClientMessage(cl);
return;
case RFB_INITIALISATION:
case RFB_INITIALISATION_SHARED:
rfbProcessClientInitMessage(cl);
return;
default:
rfbProcessClientNormalMessage(cl);
return;
}
}
/*
* rfbProcessClientProtocolVersion is called when the client sends its
* protocol version.
*/
static void
rfbProcessClientProtocolVersion(rfbClientPtr cl)
{
rfbProtocolVersionMsg pv;
int n, major_, minor_;
if ((n = rfbReadExact(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) {
rfbErr("rfbProcessClientProtocolVersion: not a valid RFB client: %s\n", pv);
rfbCloseClient(cl);
return;
}
rfbLog("Client Protocol Version %d.%d\n", major_, minor_);
if (major_ != rfbProtocolMajorVersion) {
rfbErr("RFB protocol version mismatch - server %d.%d, client %d.%d",
cl->screen->protocolMajorVersion, cl->screen->protocolMinorVersion,
major_,minor_);
rfbCloseClient(cl);
return;
}
/* Check for the minor version use either of the two standard version of RFB */
/*
* UltraVNC Viewer detects FileTransfer compatible servers via rfb versions
* 3.4, 3.6, 3.14, 3.16
* It's a bad method, but it is what they use to enable features...
* maintaining RFB version compatibility across multiple servers is a pain
* Should use something like ServerIdentity encoding
*/
cl->protocolMajorVersion = major_;
cl->protocolMinorVersion = minor_;
rfbLog("Protocol version sent %d.%d, using %d.%d\n",
major_, minor_, rfbProtocolMajorVersion, cl->protocolMinorVersion);
rfbAuthNewClient(cl);
}
void
rfbClientSendString(rfbClientPtr cl, const char *reason)
{
char *buf;
int len = strlen(reason);
rfbLog("rfbClientSendString(\"%s\")\n", reason);
buf = (char *)malloc(4 + len);
if (buf) {
((uint32_t *)buf)[0] = Swap32IfLE(len);
memcpy(buf + 4, reason, len);
if (rfbWriteExact(cl, buf, 4 + len) < 0)
rfbLogPerror("rfbClientSendString: write");
free(buf);
}
rfbCloseClient(cl);
}
/*
* rfbClientConnFailed is called when a client connection has failed either
* because it talks the wrong protocol or it has failed authentication.
*/
void
rfbClientConnFailed(rfbClientPtr cl,
const char *reason)
{
char *buf;
int len = strlen(reason);
rfbLog("rfbClientConnFailed(\"%s\")\n", reason);
buf = (char *)malloc(8 + len);
if (buf) {
((uint32_t *)buf)[0] = Swap32IfLE(rfbConnFailed);
((uint32_t *)buf)[1] = Swap32IfLE(len);
memcpy(buf + 8, reason, len);
if (rfbWriteExact(cl, buf, 8 + len) < 0)
rfbLogPerror("rfbClientConnFailed: write");
free(buf);
}
rfbCloseClient(cl);
}
/*
* rfbProcessClientInitMessage is called when the client sends its
* initialisation message.
*/
static void
rfbProcessClientInitMessage(rfbClientPtr cl)
{
rfbClientInitMsg ci;
union {
char buf[256];
rfbServerInitMsg si;
} u;
int len, n;
rfbClientIteratorPtr iterator;
rfbClientPtr otherCl;
rfbExtensionData* extension;
if (cl->state == RFB_INITIALISATION_SHARED) {
/* In this case behave as though an implicit ClientInit message has
* already been received with a shared-flag of true. */
ci.shared = 1;
/* Avoid the possibility of exposing the RFB_INITIALISATION_SHARED
* state to calling software. */
cl->state = RFB_INITIALISATION;
} else {
if ((n = rfbReadExact(cl, (char *)&ci,sz_rfbClientInitMsg)) <= 0) {
if (n == 0)
rfbLog("rfbProcessClientInitMessage: client gone\n");
else
rfbLogPerror("rfbProcessClientInitMessage: read");
rfbCloseClient(cl);
return;
}
}
memset(u.buf,0,sizeof(u.buf));
u.si.framebufferWidth = Swap16IfLE(cl->screen->width);
u.si.framebufferHeight = Swap16IfLE(cl->screen->height);
u.si.format = cl->screen->serverFormat;
u.si.format.redMax = Swap16IfLE(u.si.format.redMax);
u.si.format.greenMax = Swap16IfLE(u.si.format.greenMax);
u.si.format.blueMax = Swap16IfLE(u.si.format.blueMax);
strncpy(u.buf + sz_rfbServerInitMsg, cl->screen->desktopName, 127);
len = strlen(u.buf + sz_rfbServerInitMsg);
u.si.nameLength = Swap32IfLE(len);
if (rfbWriteExact(cl, u.buf, sz_rfbServerInitMsg + len) < 0) {
rfbLogPerror("rfbProcessClientInitMessage: write");
rfbCloseClient(cl);
return;
}
for(extension = cl->extensions; extension;) {
rfbExtensionData* next = extension->next;
if(extension->extension->init &&
!extension->extension->init(cl, extension->data))
/* extension requested that it be removed */
rfbDisableExtension(cl, extension->extension);
extension = next;
}
cl->state = RFB_NORMAL;
if (!cl->reverseConnection &&
(cl->screen->neverShared || (!cl->screen->alwaysShared && !ci.shared))) {
if (cl->screen->dontDisconnect) {
iterator = rfbGetClientIterator(cl->screen);
while ((otherCl = rfbClientIteratorNext(iterator)) != NULL) {
if ((otherCl != cl) && (otherCl->state == RFB_NORMAL)) {
rfbLog("-dontdisconnect: Not shared & existing client\n");
rfbLog(" refusing new client %s\n", cl->host);
rfbCloseClient(cl);
rfbReleaseClientIterator(iterator);
return;
}
}
rfbReleaseClientIterator(iterator);
} else {
iterator = rfbGetClientIterator(cl->screen);
rfbClientPtr nextCl, otherCl = rfbClientIteratorNext(iterator);
while (otherCl) {
nextCl = rfbClientIteratorNext(iterator);
if ((otherCl != cl) && (otherCl->state == RFB_NORMAL)) {
rfbLog("Not shared - closing connection to client %s\n",
otherCl->host);
rfbCloseClient(otherCl);
}
otherCl = nextCl;
}
rfbReleaseClientIterator(iterator);
}
}
}
/* The values come in based on the scaled screen, we need to convert them to
* values based on the man screen's coordinate system
*/
static rfbBool rectSwapIfLEAndClip(uint16_t* x,uint16_t* y,uint16_t* w,uint16_t* h,
rfbClientPtr cl)
{
int x1=Swap16IfLE(*x);
int y1=Swap16IfLE(*y);
int w1=Swap16IfLE(*w);
int h1=Swap16IfLE(*h);
rfbScaledCorrection(cl->scaledScreen, cl->screen, &x1, &y1, &w1, &h1, "rectSwapIfLEAndClip");
*x = x1;
*y = y1;
*w = w1;
*h = h1;
if(*w>cl->screen->width-*x)
*w=cl->screen->width-*x;
/* possible underflow */
if(*w>cl->screen->width-*x)
return FALSE;
if(*h>cl->screen->height-*y)
*h=cl->screen->height-*y;
if(*h>cl->screen->height-*y)
return FALSE;
return TRUE;
}
/*
* Send keyboard state (PointerPos pseudo-encoding).
*/
rfbBool
rfbSendKeyboardLedState(rfbClientPtr cl)
{
rfbFramebufferUpdateRectHeader rect;
if (cl->ublen + sz_rfbFramebufferUpdateRectHeader > UPDATE_BUF_SIZE) {
if (!rfbSendUpdateBuf(cl))
return FALSE;
}
rect.encoding = Swap32IfLE(rfbEncodingKeyboardLedState);
rect.r.x = Swap16IfLE(cl->lastKeyboardLedState);
rect.r.y = 0;
rect.r.w = 0;
rect.r.h = 0;
memcpy(&cl->updateBuf[cl->ublen], (char *)&rect,
sz_rfbFramebufferUpdateRectHeader);
cl->ublen += sz_rfbFramebufferUpdateRectHeader;
rfbStatRecordEncodingSent(cl, rfbEncodingKeyboardLedState, sz_rfbFramebufferUpdateRectHeader, sz_rfbFramebufferUpdateRectHeader);
if (!rfbSendUpdateBuf(cl))
return FALSE;
return TRUE;
}
#define rfbSetBit(buffer, position) (buffer[(position & 255) / 8] |= (1 << (position % 8)))
/*
* Send rfbEncodingSupportedMessages.
*/
rfbBool
rfbSendSupportedMessages(rfbClientPtr cl)
{
rfbFramebufferUpdateRectHeader rect;
rfbSupportedMessages msgs;
if (cl->ublen + sz_rfbFramebufferUpdateRectHeader
+ sz_rfbSupportedMessages > UPDATE_BUF_SIZE) {
if (!rfbSendUpdateBuf(cl))
return FALSE;
}
rect.encoding = Swap32IfLE(rfbEncodingSupportedMessages);
rect.r.x = 0;
rect.r.y = 0;
rect.r.w = Swap16IfLE(sz_rfbSupportedMessages);
rect.r.h = 0;
memcpy(&cl->updateBuf[cl->ublen], (char *)&rect,
sz_rfbFramebufferUpdateRectHeader);
cl->ublen += sz_rfbFramebufferUpdateRectHeader;
memset((char *)&msgs, 0, sz_rfbSupportedMessages);
rfbSetBit(msgs.client2server, rfbSetPixelFormat);
rfbSetBit(msgs.client2server, rfbFixColourMapEntries);
rfbSetBit(msgs.client2server, rfbSetEncodings);
rfbSetBit(msgs.client2server, rfbFramebufferUpdateRequest);
rfbSetBit(msgs.client2server, rfbKeyEvent);
rfbSetBit(msgs.client2server, rfbPointerEvent);
rfbSetBit(msgs.client2server, rfbClientCutText);
rfbSetBit(msgs.client2server, rfbFileTransfer);
rfbSetBit(msgs.client2server, rfbSetScale);
/*rfbSetBit(msgs.client2server, rfbSetServerInput); */