00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef __STRICT_ANSI__
00027 #define _BSD_SOURCE
00028 #define _POSIX_SOURCE
00029 #endif
00030 #ifndef WIN32
00031 #include <unistd.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <pwd.h>
00035 #endif
00036 #include <errno.h>
00037 #include <rfb/rfbclient.h>
00038 #ifdef WIN32
00039 #undef SOCKET
00040 #undef socklen_t
00041 #endif
00042 #ifdef LIBVNCSERVER_HAVE_LIBZ
00043 #include <zlib.h>
00044 #ifdef __CHECKER__
00045 #undef Z_NULL
00046 #define Z_NULL NULL
00047 #endif
00048 #endif
00049 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
00050 #ifdef _RPCNDR_H
00051 #define HAVE_BOOLEAN
00052 #endif
00053 #include <jpeglib.h>
00054 #endif
00055 #include <stdarg.h>
00056 #include <time.h>
00057
00058 #ifdef LIBVNCSERVER_WITH_CLIENT_GCRYPT
00059 #include <gcrypt.h>
00060 #endif
00061
00062 #include "minilzo.h"
00063 #include "tls.h"
00064
00065
00066
00067
00068
00069 rfbBool rfbEnableClientLogging=TRUE;
00070
00071 static void
00072 rfbDefaultClientLog(const char *format, ...)
00073 {
00074 va_list args;
00075 char buf[256];
00076 time_t log_clock;
00077
00078 if(!rfbEnableClientLogging)
00079 return;
00080
00081 va_start(args, format);
00082
00083 time(&log_clock);
00084 strftime(buf, 255, "%d/%m/%Y %X ", localtime(&log_clock));
00085 fprintf(stderr, "%s", buf);
00086
00087 vfprintf(stderr, format, args);
00088 fflush(stderr);
00089
00090 va_end(args);
00091 }
00092
00093 rfbClientLogProc rfbClientLog=rfbDefaultClientLog;
00094 rfbClientLogProc rfbClientErr=rfbDefaultClientLog;
00095
00096
00097
00098 rfbClientProtocolExtension* rfbClientExtensions = NULL;
00099
00100 void rfbClientRegisterExtension(rfbClientProtocolExtension* e)
00101 {
00102 e->next = rfbClientExtensions;
00103 rfbClientExtensions = e;
00104 }
00105
00106
00107
00108 void rfbClientSetClientData(rfbClient* client, void* tag, void* data)
00109 {
00110 rfbClientData* clientData = client->clientData;
00111
00112 while(clientData && clientData->tag != tag)
00113 clientData = clientData->next;
00114 if(clientData == NULL) {
00115 clientData = calloc(sizeof(rfbClientData), 1);
00116 clientData->next = client->clientData;
00117 client->clientData = clientData;
00118 clientData->tag = tag;
00119 }
00120
00121 clientData->data = data;
00122 }
00123
00124 void* rfbClientGetClientData(rfbClient* client, void* tag)
00125 {
00126 rfbClientData* clientData = client->clientData;
00127
00128 while(clientData) {
00129 if(clientData->tag == tag)
00130 return clientData->data;
00131 clientData = clientData->next;
00132 }
00133
00134 return NULL;
00135 }
00136
00137
00138
00139 static void FillRectangle(rfbClient* client, int x, int y, int w, int h, uint32_t colour) {
00140 int i,j;
00141
00142 #define FILL_RECT(BPP) \
00143 for(j=y*client->width;j<(y+h)*client->width;j+=client->width) \
00144 for(i=x;i<x+w;i++) \
00145 ((uint##BPP##_t*)client->frameBuffer)[j+i]=colour;
00146
00147 switch(client->format.bitsPerPixel) {
00148 case 8: FILL_RECT(8); break;
00149 case 16: FILL_RECT(16); break;
00150 case 32: FILL_RECT(32); break;
00151 default:
00152 rfbClientLog("Unsupported bitsPerPixel: %d\n",client->format.bitsPerPixel);
00153 }
00154 }
00155
00156 static void CopyRectangle(rfbClient* client, uint8_t* buffer, int x, int y, int w, int h) {
00157 int j;
00158
00159 #define COPY_RECT(BPP) \
00160 { \
00161 int rs = w * BPP / 8, rs2 = client->width * BPP / 8; \
00162 for (j = ((x * (BPP / 8)) + (y * rs2)); j < (y + h) * rs2; j += rs2) { \
00163 memcpy(client->frameBuffer + j, buffer, rs); \
00164 buffer += rs; \
00165 } \
00166 }
00167
00168 switch(client->format.bitsPerPixel) {
00169 case 8: COPY_RECT(8); break;
00170 case 16: COPY_RECT(16); break;
00171 case 32: COPY_RECT(32); break;
00172 default:
00173 rfbClientLog("Unsupported bitsPerPixel: %d\n",client->format.bitsPerPixel);
00174 }
00175 }
00176
00177
00178 static void CopyRectangleFromRectangle(rfbClient* client, int src_x, int src_y, int w, int h, int dest_x, int dest_y) {
00179 int i,j;
00180
00181 #define COPY_RECT_FROM_RECT(BPP) \
00182 { \
00183 uint##BPP##_t* _buffer=((uint##BPP##_t*)client->frameBuffer)+(src_y-dest_y)*client->width+src_x-dest_x; \
00184 if (dest_y < src_y) { \
00185 for(j = dest_y*client->width; j < (dest_y+h)*client->width; j += client->width) { \
00186 if (dest_x < src_x) { \
00187 for(i = dest_x; i < dest_x+w; i++) { \
00188 ((uint##BPP##_t*)client->frameBuffer)[j+i]=_buffer[j+i]; \
00189 } \
00190 } else { \
00191 for(i = dest_x+w-1; i >= dest_x; i--) { \
00192 ((uint##BPP##_t*)client->frameBuffer)[j+i]=_buffer[j+i]; \
00193 } \
00194 } \
00195 } \
00196 } else { \
00197 for(j = (dest_y+h-1)*client->width; j >= dest_y*client->width; j-=client->width) { \
00198 if (dest_x < src_x) { \
00199 for(i = dest_x; i < dest_x+w; i++) { \
00200 ((uint##BPP##_t*)client->frameBuffer)[j+i]=_buffer[j+i]; \
00201 } \
00202 } else { \
00203 for(i = dest_x+w-1; i >= dest_x; i--) { \
00204 ((uint##BPP##_t*)client->frameBuffer)[j+i]=_buffer[j+i]; \
00205 } \
00206 } \
00207 } \
00208 } \
00209 }
00210
00211 switch(client->format.bitsPerPixel) {
00212 case 8: COPY_RECT_FROM_RECT(8); break;
00213 case 16: COPY_RECT_FROM_RECT(16); break;
00214 case 32: COPY_RECT_FROM_RECT(32); break;
00215 default:
00216 rfbClientLog("Unsupported bitsPerPixel: %d\n",client->format.bitsPerPixel);
00217 }
00218 }
00219
00220 static rfbBool HandleRRE8(rfbClient* client, int rx, int ry, int rw, int rh);
00221 static rfbBool HandleRRE16(rfbClient* client, int rx, int ry, int rw, int rh);
00222 static rfbBool HandleRRE32(rfbClient* client, int rx, int ry, int rw, int rh);
00223 static rfbBool HandleCoRRE8(rfbClient* client, int rx, int ry, int rw, int rh);
00224 static rfbBool HandleCoRRE16(rfbClient* client, int rx, int ry, int rw, int rh);
00225 static rfbBool HandleCoRRE32(rfbClient* client, int rx, int ry, int rw, int rh);
00226 static rfbBool HandleHextile8(rfbClient* client, int rx, int ry, int rw, int rh);
00227 static rfbBool HandleHextile16(rfbClient* client, int rx, int ry, int rw, int rh);
00228 static rfbBool HandleHextile32(rfbClient* client, int rx, int ry, int rw, int rh);
00229 static rfbBool HandleUltra8(rfbClient* client, int rx, int ry, int rw, int rh);
00230 static rfbBool HandleUltra16(rfbClient* client, int rx, int ry, int rw, int rh);
00231 static rfbBool HandleUltra32(rfbClient* client, int rx, int ry, int rw, int rh);
00232 static rfbBool HandleUltraZip8(rfbClient* client, int rx, int ry, int rw, int rh);
00233 static rfbBool HandleUltraZip16(rfbClient* client, int rx, int ry, int rw, int rh);
00234 static rfbBool HandleUltraZip32(rfbClient* client, int rx, int ry, int rw, int rh);
00235 #ifdef LIBVNCSERVER_HAVE_LIBZ
00236 static rfbBool HandleZlib8(rfbClient* client, int rx, int ry, int rw, int rh);
00237 static rfbBool HandleZlib16(rfbClient* client, int rx, int ry, int rw, int rh);
00238 static rfbBool HandleZlib32(rfbClient* client, int rx, int ry, int rw, int rh);
00239 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
00240 static rfbBool HandleTight8(rfbClient* client, int rx, int ry, int rw, int rh);
00241 static rfbBool HandleTight16(rfbClient* client, int rx, int ry, int rw, int rh);
00242 static rfbBool HandleTight32(rfbClient* client, int rx, int ry, int rw, int rh);
00243
00244 static long ReadCompactLen (rfbClient* client);
00245
00246 static void JpegInitSource(j_decompress_ptr cinfo);
00247 static boolean JpegFillInputBuffer(j_decompress_ptr cinfo);
00248 static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes);
00249 static void JpegTermSource(j_decompress_ptr cinfo);
00250 static void JpegSetSrcManager(j_decompress_ptr cinfo, uint8_t *compressedData,
00251 int compressedLen);
00252 #endif
00253 static rfbBool HandleZRLE8(rfbClient* client, int rx, int ry, int rw, int rh);
00254 static rfbBool HandleZRLE15(rfbClient* client, int rx, int ry, int rw, int rh);
00255 static rfbBool HandleZRLE16(rfbClient* client, int rx, int ry, int rw, int rh);
00256 static rfbBool HandleZRLE24(rfbClient* client, int rx, int ry, int rw, int rh);
00257 static rfbBool HandleZRLE24Up(rfbClient* client, int rx, int ry, int rw, int rh);
00258 static rfbBool HandleZRLE24Down(rfbClient* client, int rx, int ry, int rw, int rh);
00259 static rfbBool HandleZRLE32(rfbClient* client, int rx, int ry, int rw, int rh);
00260 #endif
00261
00262
00263
00264
00265 rfbBool
00266 SupportsClient2Server(rfbClient* client, int messageType)
00267 {
00268 return (client->supportedMessages.client2server[((messageType & 0xFF)/8)] & (1<<(messageType % 8)) ? TRUE : FALSE);
00269 }
00270
00271 rfbBool
00272 SupportsServer2Client(rfbClient* client, int messageType)
00273 {
00274 return (client->supportedMessages.server2client[((messageType & 0xFF)/8)] & (1<<(messageType % 8)) ? TRUE : FALSE);
00275 }
00276
00277 void
00278 SetClient2Server(rfbClient* client, int messageType)
00279 {
00280 client->supportedMessages.client2server[((messageType & 0xFF)/8)] |= (1<<(messageType % 8));
00281 }
00282
00283 void
00284 SetServer2Client(rfbClient* client, int messageType)
00285 {
00286 client->supportedMessages.server2client[((messageType & 0xFF)/8)] |= (1<<(messageType % 8));
00287 }
00288
00289 void
00290 ClearClient2Server(rfbClient* client, int messageType)
00291 {
00292 client->supportedMessages.client2server[((messageType & 0xFF)/8)] &= (!(1<<(messageType % 8)));
00293 }
00294
00295 void
00296 ClearServer2Client(rfbClient* client, int messageType)
00297 {
00298 client->supportedMessages.server2client[((messageType & 0xFF)/8)] &= (!(1<<(messageType % 8)));
00299 }
00300
00301
00302 void
00303 DefaultSupportedMessages(rfbClient* client)
00304 {
00305 memset((char *)&client->supportedMessages,0,sizeof(client->supportedMessages));
00306
00307
00308 SetClient2Server(client, rfbSetPixelFormat);
00309
00310 SetClient2Server(client, rfbSetEncodings);
00311 SetClient2Server(client, rfbFramebufferUpdateRequest);
00312 SetClient2Server(client, rfbKeyEvent);
00313 SetClient2Server(client, rfbPointerEvent);
00314 SetClient2Server(client, rfbClientCutText);
00315
00316
00317
00318 SetServer2Client(client, rfbFramebufferUpdate);
00319 SetServer2Client(client, rfbSetColourMapEntries);
00320 SetServer2Client(client, rfbBell);
00321 SetServer2Client(client, rfbServerCutText);
00322 }
00323
00324 void
00325 DefaultSupportedMessagesUltraVNC(rfbClient* client)
00326 {
00327 DefaultSupportedMessages(client);
00328 SetClient2Server(client, rfbFileTransfer);
00329 SetClient2Server(client, rfbSetScale);
00330 SetClient2Server(client, rfbSetServerInput);
00331 SetClient2Server(client, rfbSetSW);
00332 SetClient2Server(client, rfbTextChat);
00333 SetClient2Server(client, rfbPalmVNCSetScaleFactor);
00334
00335 SetServer2Client(client, rfbResizeFrameBuffer);
00336 SetServer2Client(client, rfbPalmVNCReSizeFrameBuffer);
00337 SetServer2Client(client, rfbFileTransfer);
00338 SetServer2Client(client, rfbTextChat);
00339 }
00340
00341
00342 void
00343 DefaultSupportedMessagesTightVNC(rfbClient* client)
00344 {
00345 DefaultSupportedMessages(client);
00346 SetClient2Server(client, rfbFileTransfer);
00347 SetClient2Server(client, rfbSetServerInput);
00348 SetClient2Server(client, rfbSetSW);
00349
00350
00351 SetServer2Client(client, rfbFileTransfer);
00352 SetServer2Client(client, rfbTextChat);
00353 }
00354
00355 #ifndef WIN32
00356 static rfbBool
00357 IsUnixSocket(const char *name)
00358 {
00359 struct stat sb;
00360 if(stat(name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFSOCK)
00361 return TRUE;
00362 return FALSE;
00363 }
00364 #endif
00365
00366
00367
00368
00369
00370 rfbBool
00371 ConnectToRFBServer(rfbClient* client,const char *hostname, int port)
00372 {
00373 if (client->serverPort==-1) {
00374
00375 const char* magic="vncLog0.0";
00376 char buffer[10];
00377 rfbVNCRec* rec = (rfbVNCRec*)malloc(sizeof(rfbVNCRec));
00378 client->vncRec = rec;
00379
00380 rec->file = fopen(client->serverHost,"rb");
00381 rec->tv.tv_sec = 0;
00382 rec->readTimestamp = FALSE;
00383 rec->doNotSleep = FALSE;
00384
00385 if (!rec->file) {
00386 rfbClientLog("Could not open %s.\n",client->serverHost);
00387 return FALSE;
00388 }
00389 setbuf(rec->file,NULL);
00390 fread(buffer,1,strlen(magic),rec->file);
00391 if (strncmp(buffer,magic,strlen(magic))) {
00392 rfbClientLog("File %s was not recorded by vncrec.\n",client->serverHost);
00393 fclose(rec->file);
00394 return FALSE;
00395 }
00396 client->sock = -1;
00397 return TRUE;
00398 }
00399
00400 #ifndef WIN32
00401 if(IsUnixSocket(hostname))
00402
00403 client->sock = ConnectClientToUnixSock(hostname);
00404 else
00405 #endif
00406 {
00407 #ifdef LIBVNCSERVER_IPv6
00408 client->sock = ConnectClientToTcpAddr6(hostname, port);
00409 if (client->sock == -1)
00410 #endif
00411 {
00412 unsigned int host;
00413
00414
00415 if (!StringToIPAddr(hostname, &host)) {
00416 rfbClientLog("Couldn't convert '%s' to host address\n", hostname);
00417 return FALSE;
00418 }
00419 client->sock = ConnectClientToTcpAddr(host, port);
00420 }
00421 }
00422
00423 if (client->sock < 0) {
00424 rfbClientLog("Unable to connect to VNC server\n");
00425 return FALSE;
00426 }
00427
00428 if(client->QoS_DSCP && !SetDSCP(client->sock, client->QoS_DSCP))
00429 return FALSE;
00430
00431 return SetNonBlocking(client->sock);
00432 }
00433
00434
00435
00436
00437
00438 rfbBool ConnectToRFBRepeater(rfbClient* client,const char *repeaterHost, int repeaterPort, const char *destHost, int destPort)
00439 {
00440 rfbProtocolVersionMsg pv;
00441 int major,minor;
00442 char tmphost[250];
00443
00444 #ifdef LIBVNCSERVER_IPv6
00445 client->sock = ConnectClientToTcpAddr6(repeaterHost, repeaterPort);
00446 if (client->sock == -1)
00447 #endif
00448 {
00449 unsigned int host;
00450 if (!StringToIPAddr(repeaterHost, &host)) {
00451 rfbClientLog("Couldn't convert '%s' to host address\n", repeaterHost);
00452 return FALSE;
00453 }
00454
00455 client->sock = ConnectClientToTcpAddr(host, repeaterPort);
00456 }
00457
00458 if (client->sock < 0) {
00459 rfbClientLog("Unable to connect to VNC repeater\n");
00460 return FALSE;
00461 }
00462
00463 if (!SetNonBlocking(client->sock))
00464 return FALSE;
00465
00466 if (!ReadFromRFBServer(client, pv, sz_rfbProtocolVersionMsg))
00467 return FALSE;
00468 pv[sz_rfbProtocolVersionMsg] = 0;
00469
00470
00471 if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2 || major != 0 || minor != 0) {
00472 rfbClientLog("Not a valid VNC repeater (%s)\n",pv);
00473 return FALSE;
00474 }
00475
00476 rfbClientLog("Connected to VNC repeater, using protocol version %d.%d\n", major, minor);
00477
00478 snprintf(tmphost, sizeof(tmphost), "%s:%d", destHost, destPort);
00479 if (!WriteToRFBServer(client, tmphost, sizeof(tmphost)))
00480 return FALSE;
00481
00482 return TRUE;
00483 }
00484
00485 extern void rfbClientEncryptBytes(unsigned char* bytes, char* passwd);
00486 extern void rfbClientEncryptBytes2(unsigned char *where, const int length, unsigned char *key);
00487
00488 rfbBool
00489 rfbHandleAuthResult(rfbClient* client)
00490 {
00491 uint32_t authResult=0, reasonLen=0;
00492 char *reason=NULL;
00493
00494 if (!ReadFromRFBServer(client, (char *)&authResult, 4)) return FALSE;
00495
00496 authResult = rfbClientSwap32IfLE(authResult);
00497
00498 switch (authResult) {
00499 case rfbVncAuthOK:
00500 rfbClientLog("VNC authentication succeeded\n");
00501 return TRUE;
00502 break;
00503 case rfbVncAuthFailed:
00504 if (client->major==3 && client->minor>7)
00505 {
00506
00507 if (!ReadFromRFBServer(client, (char *)&reasonLen, 4)) return FALSE;
00508 reasonLen = rfbClientSwap32IfLE(reasonLen);
00509 reason = malloc(reasonLen+1);
00510 if (!ReadFromRFBServer(client, reason, reasonLen)) { free(reason); return FALSE; }
00511 reason[reasonLen]=0;
00512 rfbClientLog("VNC connection failed: %s\n",reason);
00513 free(reason);
00514 return FALSE;
00515 }
00516 rfbClientLog("VNC authentication failed\n");
00517 return FALSE;
00518 case rfbVncAuthTooMany:
00519 rfbClientLog("VNC authentication failed - too many tries\n");
00520 return FALSE;
00521 }
00522
00523 rfbClientLog("Unknown VNC authentication result: %d\n",
00524 (int)authResult);
00525 return FALSE;
00526 }
00527
00528 static void
00529 ReadReason(rfbClient* client)
00530 {
00531 uint32_t reasonLen;
00532 char *reason;
00533
00534
00535 if (!ReadFromRFBServer(client, (char *)&reasonLen, 4)) return;
00536 reasonLen = rfbClientSwap32IfLE(reasonLen);
00537 reason = malloc(reasonLen+1);
00538 if (!ReadFromRFBServer(client, reason, reasonLen)) { free(reason); return; }
00539 reason[reasonLen]=0;
00540 rfbClientLog("VNC connection failed: %s\n",reason);
00541 free(reason);
00542 }
00543
00544 static rfbBool
00545 ReadSupportedSecurityType(rfbClient* client, uint32_t *result, rfbBool subAuth)
00546 {
00547 uint8_t count=0;
00548 uint8_t loop=0;
00549 uint8_t flag=0;
00550 uint8_t tAuth[256];
00551 char buf1[500],buf2[10];
00552 uint32_t authScheme;
00553
00554 if (!ReadFromRFBServer(client, (char *)&count, 1)) return FALSE;
00555
00556 if (count==0)
00557 {
00558 rfbClientLog("List of security types is ZERO, expecting an error to follow\n");
00559 ReadReason(client);
00560 return FALSE;
00561 }
00562
00563 rfbClientLog("We have %d security types to read\n", count);
00564 authScheme=0;
00565
00566 for (loop=0;loop<count;loop++)
00567 {
00568 if (!ReadFromRFBServer(client, (char *)&tAuth[loop], 1)) return FALSE;
00569 rfbClientLog("%d) Received security type %d\n", loop, tAuth[loop]);
00570 if (flag) continue;
00571 if (tAuth[loop]==rfbVncAuth || tAuth[loop]==rfbNoAuth ||
00572 (tAuth[loop]==rfbARD && client->GetCredential) ||
00573 (!subAuth && (tAuth[loop]==rfbTLS || (tAuth[loop]==rfbVeNCrypt && client->GetCredential))))
00574 {
00575 if (!subAuth && client->clientAuthSchemes)
00576 {
00577 int i;
00578 for (i=0;client->clientAuthSchemes[i];i++)
00579 {
00580 if (client->clientAuthSchemes[i]==(uint32_t)tAuth[loop])
00581 {
00582 flag++;
00583 authScheme=tAuth[loop];
00584 break;
00585 }
00586 }
00587 }
00588 else
00589 {
00590 flag++;
00591 authScheme=tAuth[loop];
00592 }
00593 if (flag)
00594 {
00595 rfbClientLog("Selecting security type %d (%d/%d in the list)\n", authScheme, loop, count);
00596
00597 if (!WriteToRFBServer(client, (char *)&tAuth[loop], 1)) return FALSE;
00598 }
00599 }
00600 }
00601 if (authScheme==0)
00602 {
00603 memset(buf1, 0, sizeof(buf1));
00604 for (loop=0;loop<count;loop++)
00605 {
00606 if (strlen(buf1)>=sizeof(buf1)-1) break;
00607 snprintf(buf2, sizeof(buf2), (loop>0 ? ", %d" : "%d"), (int)tAuth[loop]);
00608 strncat(buf1, buf2, sizeof(buf1)-strlen(buf1)-1);
00609 }
00610 rfbClientLog("Unknown authentication scheme from VNC server: %s\n",
00611 buf1);
00612 return FALSE;
00613 }
00614 *result = authScheme;
00615 return TRUE;
00616 }
00617
00618 static rfbBool
00619 HandleVncAuth(rfbClient *client)
00620 {
00621 uint8_t challenge[CHALLENGESIZE];
00622 char *passwd=NULL;
00623 int i;
00624
00625 if (!ReadFromRFBServer(client, (char *)challenge, CHALLENGESIZE)) return FALSE;
00626
00627 if (client->serverPort!=-1) {
00628 if (client->GetPassword)
00629 passwd = client->GetPassword(client);
00630
00631 if ((!passwd) || (strlen(passwd) == 0)) {
00632 rfbClientLog("Reading password failed\n");
00633 return FALSE;
00634 }
00635 if (strlen(passwd) > 8) {
00636 passwd[8] = '\0';
00637 }
00638
00639 rfbClientEncryptBytes(challenge, passwd);
00640
00641
00642 for (i = strlen(passwd); i >= 0; i--) {
00643 passwd[i] = '\0';
00644 }
00645 free(passwd);
00646
00647 if (!WriteToRFBServer(client, (char *)challenge, CHALLENGESIZE)) return FALSE;
00648 }
00649
00650
00651 if (!rfbHandleAuthResult(client)) return FALSE;
00652
00653 return TRUE;
00654 }
00655
00656 static void
00657 FreeUserCredential(rfbCredential *cred)
00658 {
00659 if (cred->userCredential.username) free(cred->userCredential.username);
00660 if (cred->userCredential.password) free(cred->userCredential.password);
00661 free(cred);
00662 }
00663
00664 static rfbBool
00665 HandlePlainAuth(rfbClient *client)
00666 {
00667 uint32_t ulen, ulensw;
00668 uint32_t plen, plensw;
00669 rfbCredential *cred;
00670
00671 if (!client->GetCredential)
00672 {
00673 rfbClientLog("GetCredential callback is not set.\n");
00674 return FALSE;
00675 }
00676 cred = client->GetCredential(client, rfbCredentialTypeUser);
00677 if (!cred)
00678 {
00679 rfbClientLog("Reading credential failed\n");
00680 return FALSE;
00681 }
00682
00683 ulen = (cred->userCredential.username ? strlen(cred->userCredential.username) : 0);
00684 ulensw = rfbClientSwap32IfLE(ulen);
00685 plen = (cred->userCredential.password ? strlen(cred->userCredential.password) : 0);
00686 plensw = rfbClientSwap32IfLE(plen);
00687 if (!WriteToRFBServer(client, (char *)&ulensw, 4) ||
00688 !WriteToRFBServer(client, (char *)&plensw, 4))
00689 {
00690 FreeUserCredential(cred);
00691 return FALSE;
00692 }
00693 if (ulen > 0)
00694 {
00695 if (!WriteToRFBServer(client, cred->userCredential.username, ulen))
00696 {
00697 FreeUserCredential(cred);
00698 return FALSE;
00699 }
00700 }
00701 if (plen > 0)
00702 {
00703 if (!WriteToRFBServer(client, cred->userCredential.password, plen))
00704 {
00705 FreeUserCredential(cred);
00706 return FALSE;
00707 }
00708 }
00709
00710 FreeUserCredential(cred);
00711
00712
00713 if (!rfbHandleAuthResult(client)) return FALSE;
00714
00715 return TRUE;
00716 }
00717
00718
00719
00720 #define rfbAddM64(x,y,m) ((x+y)%m+(x+y<x?(((uint64_t)-1)%m+1)%m:0))
00721
00722 static uint64_t
00723 rfbMulM64(uint64_t x, uint64_t y, uint64_t m)
00724 {
00725 uint64_t r;
00726 for(r=0;x>0;x>>=1)
00727 {
00728 if (x&1) r=rfbAddM64(r,y,m);
00729 y=rfbAddM64(y,y,m);
00730 }
00731 return r;
00732 }
00733
00734 static uint64_t
00735 rfbPowM64(uint64_t b, uint64_t e, uint64_t m)
00736 {
00737 uint64_t r;
00738 for(r=1;e>0;e>>=1)
00739 {
00740 if(e&1) r=rfbMulM64(r,b,m);
00741 b=rfbMulM64(b,b,m);
00742 }
00743 return r;
00744 }
00745
00746 static rfbBool
00747 HandleMSLogonAuth(rfbClient *client)
00748 {
00749 uint64_t gen, mod, resp, priv, pub, key;
00750 uint8_t username[256], password[64];
00751 rfbCredential *cred;
00752
00753 if (!ReadFromRFBServer(client, (char *)&gen, 8)) return FALSE;
00754 if (!ReadFromRFBServer(client, (char *)&mod, 8)) return FALSE;
00755 if (!ReadFromRFBServer(client, (char *)&resp, 8)) return FALSE;
00756 gen = rfbClientSwap64IfLE(gen);
00757 mod = rfbClientSwap64IfLE(mod);
00758 resp = rfbClientSwap64IfLE(resp);
00759
00760 if (!client->GetCredential)
00761 {
00762 rfbClientLog("GetCredential callback is not set.\n");
00763 return FALSE;
00764 }
00765 rfbClientLog("WARNING! MSLogon security type has very low password encryption! "\
00766 "Use it only with SSH tunnel or trusted network.\n");
00767 cred = client->GetCredential(client, rfbCredentialTypeUser);
00768 if (!cred)
00769 {
00770 rfbClientLog("Reading credential failed\n");
00771 return FALSE;
00772 }
00773
00774 memset(username, 0, sizeof(username));
00775 strncpy((char *)username, cred->userCredential.username, sizeof(username));
00776 memset(password, 0, sizeof(password));
00777 strncpy((char *)password, cred->userCredential.password, sizeof(password));
00778 FreeUserCredential(cred);
00779
00780 srand(time(NULL));
00781 priv = ((uint64_t)rand())<<32;
00782 priv |= (uint64_t)rand();
00783
00784 pub = rfbPowM64(gen, priv, mod);
00785 key = rfbPowM64(resp, priv, mod);
00786 pub = rfbClientSwap64IfLE(pub);
00787 key = rfbClientSwap64IfLE(key);
00788
00789 rfbClientEncryptBytes2(username, sizeof(username), (unsigned char *)&key);
00790 rfbClientEncryptBytes2(password, sizeof(password), (unsigned char *)&key);
00791
00792 if (!WriteToRFBServer(client, (char *)&pub, 8)) return FALSE;
00793 if (!WriteToRFBServer(client, (char *)username, sizeof(username))) return FALSE;
00794 if (!WriteToRFBServer(client, (char *)password, sizeof(password))) return FALSE;
00795
00796
00797 if (!rfbHandleAuthResult(client)) return FALSE;
00798
00799 return TRUE;
00800 }
00801
00802 #ifdef LIBVNCSERVER_WITH_CLIENT_GCRYPT
00803 static rfbBool
00804 rfbMpiToBytes(const gcry_mpi_t value, uint8_t *result, size_t size)
00805 {
00806 gcry_error_t error;
00807 size_t len;
00808 int i;
00809
00810 error = gcry_mpi_print(GCRYMPI_FMT_USG, result, size, &len, value);
00811 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00812 {
00813 rfbClientLog("gcry_mpi_print error: %s\n", gcry_strerror(error));
00814 return FALSE;
00815 }
00816 for (i=size-1;i>(int)size-1-(int)len;--i)
00817 result[i] = result[i-size+len];
00818 for (;i>=0;--i)
00819 result[i] = 0;
00820 return TRUE;
00821 }
00822
00823 static rfbBool
00824 HandleARDAuth(rfbClient *client)
00825 {
00826 uint8_t gen[2], len[2];
00827 size_t keylen;
00828 uint8_t *mod = NULL, *resp, *pub, *key, *shared;
00829 gcry_mpi_t genmpi = NULL, modmpi = NULL, respmpi = NULL;
00830 gcry_mpi_t privmpi = NULL, pubmpi = NULL, keympi = NULL;
00831 gcry_md_hd_t md5 = NULL;
00832 gcry_cipher_hd_t aes = NULL;
00833 gcry_error_t error;
00834 uint8_t userpass[128], ciphertext[128];
00835 int passwordLen, usernameLen;
00836 rfbCredential *cred = NULL;
00837 rfbBool result = FALSE;
00838
00839 while (1)
00840 {
00841 if (!ReadFromRFBServer(client, (char *)gen, 2))
00842 break;
00843 if (!ReadFromRFBServer(client, (char *)len, 2))
00844 break;
00845
00846 if (!client->GetCredential)
00847 {
00848 rfbClientLog("GetCredential callback is not set.\n");
00849 break;
00850 }
00851 cred = client->GetCredential(client, rfbCredentialTypeUser);
00852 if (!cred)
00853 {
00854 rfbClientLog("Reading credential failed\n");
00855 break;
00856 }
00857
00858 keylen = 256*len[0]+len[1];
00859 mod = (uint8_t*)malloc(keylen*4);
00860 if (!mod)
00861 {
00862 rfbClientLog("malloc out of memory\n");
00863 break;
00864 }
00865 resp = mod+keylen;
00866 pub = resp+keylen;
00867 key = pub+keylen;
00868
00869 if (!ReadFromRFBServer(client, (char *)mod, keylen))
00870 break;
00871 if (!ReadFromRFBServer(client, (char *)resp, keylen))
00872 break;
00873
00874 error = gcry_mpi_scan(&genmpi, GCRYMPI_FMT_USG, gen, 2, NULL);
00875 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00876 {
00877 rfbClientLog("gcry_mpi_scan error: %s\n", gcry_strerror(error));
00878 break;
00879 }
00880 error = gcry_mpi_scan(&modmpi, GCRYMPI_FMT_USG, mod, keylen, NULL);
00881 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00882 {
00883 rfbClientLog("gcry_mpi_scan error: %s\n", gcry_strerror(error));
00884 break;
00885 }
00886 error = gcry_mpi_scan(&respmpi, GCRYMPI_FMT_USG, resp, keylen, NULL);
00887 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00888 {
00889 rfbClientLog("gcry_mpi_scan error: %s\n", gcry_strerror(error));
00890 break;
00891 }
00892
00893 privmpi = gcry_mpi_new(keylen);
00894 if (!privmpi)
00895 {
00896 rfbClientLog("gcry_mpi_new out of memory\n");
00897 break;
00898 }
00899 gcry_mpi_randomize(privmpi, (keylen/8)*8, GCRY_STRONG_RANDOM);
00900
00901 pubmpi = gcry_mpi_new(keylen);
00902 if (!pubmpi)
00903 {
00904 rfbClientLog("gcry_mpi_new out of memory\n");
00905 break;
00906 }
00907 gcry_mpi_powm(pubmpi, genmpi, privmpi, modmpi);
00908
00909 keympi = gcry_mpi_new(keylen);
00910 if (!keympi)
00911 {
00912 rfbClientLog("gcry_mpi_new out of memory\n");
00913 break;
00914 }
00915 gcry_mpi_powm(keympi, respmpi, privmpi, modmpi);
00916
00917 if (!rfbMpiToBytes(pubmpi, pub, keylen))
00918 break;
00919 if (!rfbMpiToBytes(keympi, key, keylen))
00920 break;
00921
00922 error = gcry_md_open(&md5, GCRY_MD_MD5, 0);
00923 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00924 {
00925 rfbClientLog("gcry_md_open error: %s\n", gcry_strerror(error));
00926 break;
00927 }
00928 gcry_md_write(md5, key, keylen);
00929 error = gcry_md_final(md5);
00930 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00931 {
00932 rfbClientLog("gcry_md_final error: %s\n", gcry_strerror(error));
00933 break;
00934 }
00935 shared = gcry_md_read(md5, GCRY_MD_MD5);
00936
00937 passwordLen = strlen(cred->userCredential.password)+1;
00938 usernameLen = strlen(cred->userCredential.username)+1;
00939 if (passwordLen > sizeof(userpass)/2)
00940 passwordLen = sizeof(userpass)/2;
00941 if (usernameLen > sizeof(userpass)/2)
00942 usernameLen = sizeof(userpass)/2;
00943
00944 gcry_randomize(userpass, sizeof(userpass), GCRY_STRONG_RANDOM);
00945 memcpy(userpass, cred->userCredential.username, usernameLen);
00946 memcpy(userpass+sizeof(userpass)/2, cred->userCredential.password, passwordLen);
00947
00948 error = gcry_cipher_open(&aes, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0);
00949 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00950 {
00951 rfbClientLog("gcry_cipher_open error: %s\n", gcry_strerror(error));
00952 break;
00953 }
00954 error = gcry_cipher_setkey(aes, shared, 16);
00955 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00956 {
00957 rfbClientLog("gcry_cipher_setkey error: %s\n", gcry_strerror(error));
00958 break;
00959 }
00960 error = gcry_cipher_encrypt(aes, ciphertext, sizeof(ciphertext), userpass, sizeof(userpass));
00961 if (gcry_err_code(error) != GPG_ERR_NO_ERROR)
00962 {
00963 rfbClientLog("gcry_cipher_encrypt error: %s\n", gcry_strerror(error));
00964 break;
00965 }
00966
00967 if (!WriteToRFBServer(client, (char *)ciphertext, sizeof(ciphertext)))
00968 break;
00969 if (!WriteToRFBServer(client, (char *)pub, keylen))
00970 break;
00971
00972
00973 if (!rfbHandleAuthResult(client))
00974 break;
00975
00976 result = TRUE;
00977 break;
00978 }
00979
00980 if (cred)
00981 FreeUserCredential(cred);
00982 if (mod)
00983 free(mod);
00984 if (genmpi)
00985 gcry_mpi_release(genmpi);
00986 if (modmpi)
00987 gcry_mpi_release(modmpi);
00988 if (respmpi)
00989 gcry_mpi_release(respmpi);
00990 if (privmpi)
00991 gcry_mpi_release(privmpi);
00992 if (pubmpi)
00993 gcry_mpi_release(pubmpi);
00994 if (keympi)
00995 gcry_mpi_release(keympi);
00996 if (md5)
00997 gcry_md_close(md5);
00998 if (aes)
00999 gcry_cipher_close(aes);
01000 return result;
01001 }
01002 #endif
01003
01004
01005
01006
01007
01008 void
01009 SetClientAuthSchemes(rfbClient* client,const uint32_t *authSchemes, int size)
01010 {
01011 int i;
01012
01013 if (client->clientAuthSchemes)
01014 {
01015 free(client->clientAuthSchemes);
01016 client->clientAuthSchemes = NULL;
01017 }
01018 if (authSchemes)
01019 {
01020 if (size<0)
01021 {
01022
01023
01024 for (size=0;authSchemes[size];size++) ;
01025 }
01026 client->clientAuthSchemes = (uint32_t*)malloc(sizeof(uint32_t)*(size+1));
01027 for (i=0;i<size;i++)
01028 client->clientAuthSchemes[i] = authSchemes[i];
01029 client->clientAuthSchemes[size] = 0;
01030 }
01031 }
01032
01033
01034
01035
01036
01037 rfbBool
01038 InitialiseRFBConnection(rfbClient* client)
01039 {
01040 rfbProtocolVersionMsg pv;
01041 int major,minor;
01042 uint32_t authScheme;
01043 uint32_t subAuthScheme;
01044 rfbClientInitMsg ci;
01045
01046
01047
01048
01049 if (client->listenSpecified)
01050 errorMessageOnReadFailure = FALSE;
01051
01052 if (!ReadFromRFBServer(client, pv, sz_rfbProtocolVersionMsg)) return FALSE;
01053 pv[sz_rfbProtocolVersionMsg]=0;
01054
01055 errorMessageOnReadFailure = TRUE;
01056
01057 pv[sz_rfbProtocolVersionMsg] = 0;
01058
01059 if (sscanf(pv,rfbProtocolVersionFormat,&major,&minor) != 2) {
01060 rfbClientLog("Not a valid VNC server (%s)\n",pv);
01061 return FALSE;
01062 }
01063
01064
01065 DefaultSupportedMessages(client);
01066 client->major = major;
01067 client->minor = minor;
01068
01069
01070 if ((major==rfbProtocolMajorVersion) && (minor>rfbProtocolMinorVersion))
01071 client->minor = rfbProtocolMinorVersion;
01072
01073
01074 if (major==3 && (minor==4 || minor==6)) {
01075 rfbClientLog("UltraVNC server detected, enabling UltraVNC specific messages\n",pv);
01076 DefaultSupportedMessagesUltraVNC(client);
01077 }
01078
01079
01080 if (major==3 && (minor==14 || minor==16)) {
01081 minor = minor - 10;
01082 client->minor = minor;
01083 rfbClientLog("UltraVNC Single Click server detected, enabling UltraVNC specific messages\n",pv);
01084 DefaultSupportedMessagesUltraVNC(client);
01085 }
01086
01087
01088 if (major==3 && minor==5) {
01089 rfbClientLog("TightVNC server detected, enabling TightVNC specific messages\n",pv);
01090 DefaultSupportedMessagesTightVNC(client);
01091 }
01092
01093
01094 if ((major==3 && minor>8) || major>3)
01095 {
01096 client->major=3;
01097 client->minor=8;
01098 }
01099
01100 rfbClientLog("VNC server supports protocol version %d.%d (viewer %d.%d)\n",
01101 major, minor, rfbProtocolMajorVersion, rfbProtocolMinorVersion);
01102
01103 sprintf(pv,rfbProtocolVersionFormat,client->major,client->minor);
01104
01105 if (!WriteToRFBServer(client, pv, sz_rfbProtocolVersionMsg)) return FALSE;
01106
01107
01108
01109 if (client->major==3 && client->minor > 6)
01110 {
01111 if (!ReadSupportedSecurityType(client, &authScheme, FALSE)) return FALSE;
01112 }
01113 else
01114 {
01115 if (!ReadFromRFBServer(client, (char *)&authScheme, 4)) return FALSE;
01116 authScheme = rfbClientSwap32IfLE(authScheme);
01117 }
01118
01119 rfbClientLog("Selected Security Scheme %d\n", authScheme);
01120 client->authScheme = authScheme;
01121
01122 switch (authScheme) {
01123
01124 case rfbConnFailed:
01125 ReadReason(client);
01126 return FALSE;
01127
01128 case rfbNoAuth:
01129 rfbClientLog("No authentication needed\n");
01130
01131
01132 if ((client->major==3 && client->minor > 7) || client->major>3)
01133 if (!rfbHandleAuthResult(client)) return FALSE;
01134
01135 break;
01136
01137 case rfbVncAuth:
01138 if (!HandleVncAuth(client)) return FALSE;
01139 break;
01140
01141 case rfbMSLogon:
01142 if (!HandleMSLogonAuth(client)) return FALSE;
01143 break;
01144
01145 case rfbARD:
01146 #ifndef LIBVNCSERVER_WITH_CLIENT_GCRYPT
01147 rfbClientLog("GCrypt support was not compiled in\n");
01148 return FALSE;
01149 #else
01150 if (!HandleARDAuth(client)) return FALSE;
01151 #endif
01152 break;
01153
01154 case rfbTLS:
01155 if (!HandleAnonTLSAuth(client)) return FALSE;
01156
01157
01158
01159 if (!ReadSupportedSecurityType(client, &subAuthScheme, TRUE)) return FALSE;
01160 client->subAuthScheme = subAuthScheme;
01161
01162 switch (subAuthScheme) {
01163
01164 case rfbConnFailed:
01165 ReadReason(client);
01166 return FALSE;
01167
01168 case rfbNoAuth:
01169 rfbClientLog("No sub authentication needed\n");
01170
01171 if ((client->major==3 && client->minor > 7) || client->major>3)
01172 if (!rfbHandleAuthResult(client)) return FALSE;
01173 break;
01174
01175 case rfbVncAuth:
01176 if (!HandleVncAuth(client)) return FALSE;
01177 break;
01178
01179 default:
01180 rfbClientLog("Unknown sub authentication scheme from VNC server: %d\n",
01181 (int)subAuthScheme);
01182 return FALSE;
01183 }
01184
01185 break;
01186
01187 case rfbVeNCrypt:
01188 if (!HandleVeNCryptAuth(client)) return FALSE;
01189
01190 switch (client->subAuthScheme) {
01191
01192 case rfbVeNCryptTLSNone:
01193 case rfbVeNCryptX509None:
01194 rfbClientLog("No sub authentication needed\n");
01195 if (!rfbHandleAuthResult(client)) return FALSE;
01196 break;
01197
01198 case rfbVeNCryptTLSVNC:
01199 case rfbVeNCryptX509VNC:
01200 if (!HandleVncAuth(client)) return FALSE;
01201 break;
01202
01203 case rfbVeNCryptTLSPlain:
01204 case rfbVeNCryptX509Plain:
01205 if (!HandlePlainAuth(client)) return FALSE;
01206 break;
01207
01208 default:
01209 rfbClientLog("Unknown sub authentication scheme from VNC server: %d\n",
01210 client->subAuthScheme);
01211 return FALSE;
01212 }
01213
01214 break;
01215
01216 default:
01217 rfbClientLog("Unknown authentication scheme from VNC server: %d\n",
01218 (int)authScheme);
01219 return FALSE;
01220 }
01221
01222 ci.shared = (client->appData.shareDesktop ? 1 : 0);
01223
01224 if (!WriteToRFBServer(client, (char *)&ci, sz_rfbClientInitMsg)) return FALSE;
01225
01226 if (!ReadFromRFBServer(client, (char *)&client->si, sz_rfbServerInitMsg)) return FALSE;
01227
01228 client->si.framebufferWidth = rfbClientSwap16IfLE(client->si.framebufferWidth);
01229 client->si.framebufferHeight = rfbClientSwap16IfLE(client->si.framebufferHeight);
01230 client->si.format.redMax = rfbClientSwap16IfLE(client->si.format.redMax);
01231 client->si.format.greenMax = rfbClientSwap16IfLE(client->si.format.greenMax);
01232 client->si.format.blueMax = rfbClientSwap16IfLE(client->si.format.blueMax);
01233 client->si.nameLength = rfbClientSwap32IfLE(client->si.nameLength);
01234
01235 client->desktopName = malloc(client->si.nameLength + 1);
01236 if (!client->desktopName) {
01237 rfbClientLog("Error allocating memory for desktop name, %lu bytes\n",
01238 (unsigned long)client->si.nameLength);
01239 return FALSE;
01240 }
01241
01242 if (!ReadFromRFBServer(client, client->desktopName, client->si.nameLength)) return FALSE;
01243
01244 client->desktopName[client->si.nameLength] = 0;
01245
01246 rfbClientLog("Desktop name \"%s\"\n",client->desktopName);
01247
01248 rfbClientLog("Connected to VNC server, using protocol version %d.%d\n",
01249 client->major, client->minor);
01250
01251 rfbClientLog("VNC server default format:\n");
01252 PrintPixelFormat(&client->si.format);
01253
01254 return TRUE;
01255 }
01256
01257
01258
01259
01260
01261
01262 rfbBool
01263 SetFormatAndEncodings(rfbClient* client)
01264 {
01265 rfbSetPixelFormatMsg spf;
01266 char buf[sz_rfbSetEncodingsMsg + MAX_ENCODINGS * 4];
01267
01268 rfbSetEncodingsMsg *se = (rfbSetEncodingsMsg *)buf;
01269 uint32_t *encs = (uint32_t *)(&buf[sz_rfbSetEncodingsMsg]);
01270 int len = 0;
01271 rfbBool requestCompressLevel = FALSE;
01272 rfbBool requestQualityLevel = FALSE;
01273 rfbBool requestLastRectEncoding = FALSE;
01274 rfbClientProtocolExtension* e;
01275
01276 if (!SupportsClient2Server(client, rfbSetPixelFormat)) return TRUE;
01277
01278 spf.type = rfbSetPixelFormat;
01279 spf.format = client->format;
01280 spf.format.redMax = rfbClientSwap16IfLE(spf.format.redMax);
01281 spf.format.greenMax = rfbClientSwap16IfLE(spf.format.greenMax);
01282 spf.format.blueMax = rfbClientSwap16IfLE(spf.format.blueMax);
01283
01284 if (!WriteToRFBServer(client, (char *)&spf, sz_rfbSetPixelFormatMsg))
01285 return FALSE;
01286
01287
01288 if (!SupportsClient2Server(client, rfbSetEncodings)) return TRUE;
01289
01290 se->type = rfbSetEncodings;
01291 se->nEncodings = 0;
01292
01293 if (client->appData.encodingsString) {
01294 const char *encStr = client->appData.encodingsString;
01295 int encStrLen;
01296 do {
01297 const char *nextEncStr = strchr(encStr, ' ');
01298 if (nextEncStr) {
01299 encStrLen = nextEncStr - encStr;
01300 nextEncStr++;
01301 } else {
01302 encStrLen = strlen(encStr);
01303 }
01304
01305 if (strncasecmp(encStr,"raw",encStrLen) == 0) {
01306 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingRaw);
01307 } else if (strncasecmp(encStr,"copyrect",encStrLen) == 0) {
01308 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingCopyRect);
01309 #ifdef LIBVNCSERVER_HAVE_LIBZ
01310 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
01311 } else if (strncasecmp(encStr,"tight",encStrLen) == 0) {
01312 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingTight);
01313 requestLastRectEncoding = TRUE;
01314 if (client->appData.compressLevel >= 0 && client->appData.compressLevel <= 9)
01315 requestCompressLevel = TRUE;
01316 if (client->appData.enableJPEG)
01317 requestQualityLevel = TRUE;
01318 #endif
01319 #endif
01320 } else if (strncasecmp(encStr,"hextile",encStrLen) == 0) {
01321 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingHextile);
01322 #ifdef LIBVNCSERVER_HAVE_LIBZ
01323 } else if (strncasecmp(encStr,"zlib",encStrLen) == 0) {
01324 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZlib);
01325 if (client->appData.compressLevel >= 0 && client->appData.compressLevel <= 9)
01326 requestCompressLevel = TRUE;
01327 } else if (strncasecmp(encStr,"zlibhex",encStrLen) == 0) {
01328 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZlibHex);
01329 if (client->appData.compressLevel >= 0 && client->appData.compressLevel <= 9)
01330 requestCompressLevel = TRUE;
01331 } else if (strncasecmp(encStr,"zrle",encStrLen) == 0) {
01332 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZRLE);
01333 } else if (strncasecmp(encStr,"zywrle",encStrLen) == 0) {
01334 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZYWRLE);
01335 requestQualityLevel = TRUE;
01336 #endif
01337 } else if ((strncasecmp(encStr,"ultra",encStrLen) == 0) || (strncasecmp(encStr,"ultrazip",encStrLen) == 0)) {
01338
01339 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingUltra);
01340 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingUltraZip);
01341 } else if (strncasecmp(encStr,"corre",encStrLen) == 0) {
01342 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingCoRRE);
01343 } else if (strncasecmp(encStr,"rre",encStrLen) == 0) {
01344 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingRRE);
01345 } else {
01346 rfbClientLog("Unknown encoding '%.*s'\n",encStrLen,encStr);
01347 }
01348
01349 encStr = nextEncStr;
01350 } while (encStr && se->nEncodings < MAX_ENCODINGS);
01351
01352 if (se->nEncodings < MAX_ENCODINGS && requestCompressLevel) {
01353 encs[se->nEncodings++] = rfbClientSwap32IfLE(client->appData.compressLevel +
01354 rfbEncodingCompressLevel0);
01355 }
01356
01357 if (se->nEncodings < MAX_ENCODINGS && requestQualityLevel) {
01358 if (client->appData.qualityLevel < 0 || client->appData.qualityLevel > 9)
01359 client->appData.qualityLevel = 5;
01360 encs[se->nEncodings++] = rfbClientSwap32IfLE(client->appData.qualityLevel +
01361 rfbEncodingQualityLevel0);
01362 }
01363 }
01364 else {
01365 if (SameMachine(client->sock)) {
01366
01367
01368
01369 rfbClientLog("Same machine: preferring raw encoding\n");
01370 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingRaw);
01371
01372
01373
01374
01375
01376 }
01377
01378 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingCopyRect);
01379 #ifdef LIBVNCSERVER_HAVE_LIBZ
01380 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
01381 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingTight);
01382 requestLastRectEncoding = TRUE;
01383 #endif
01384 #endif
01385 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingHextile);
01386 #ifdef LIBVNCSERVER_HAVE_LIBZ
01387 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZlib);
01388 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZRLE);
01389 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingZYWRLE);
01390 #endif
01391 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingUltra);
01392 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingUltraZip);
01393 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingCoRRE);
01394 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingRRE);
01395
01396 if (client->appData.compressLevel >= 0 && client->appData.compressLevel <= 9) {
01397 encs[se->nEncodings++] = rfbClientSwap32IfLE(client->appData.compressLevel +
01398 rfbEncodingCompressLevel0);
01399 } else {
01400
01401
01402
01403
01404 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingCompressLevel1);
01405 }
01406
01407 if (client->appData.enableJPEG) {
01408 if (client->appData.qualityLevel < 0 || client->appData.qualityLevel > 9)
01409 client->appData.qualityLevel = 5;
01410 encs[se->nEncodings++] = rfbClientSwap32IfLE(client->appData.qualityLevel +
01411 rfbEncodingQualityLevel0);
01412 }
01413 }
01414
01415
01416
01417
01418 if (client->appData.useRemoteCursor) {
01419 if (se->nEncodings < MAX_ENCODINGS)
01420 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingXCursor);
01421 if (se->nEncodings < MAX_ENCODINGS)
01422 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingRichCursor);
01423 if (se->nEncodings < MAX_ENCODINGS)
01424 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingPointerPos);
01425 }
01426
01427
01428 if (se->nEncodings < MAX_ENCODINGS)
01429 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingKeyboardLedState);
01430
01431
01432 if (se->nEncodings < MAX_ENCODINGS && client->canHandleNewFBSize)
01433 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingNewFBSize);
01434
01435
01436 if (se->nEncodings < MAX_ENCODINGS && requestLastRectEncoding)
01437 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingLastRect);
01438
01439
01440 if (se->nEncodings < MAX_ENCODINGS)
01441 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingSupportedMessages);
01442 if (se->nEncodings < MAX_ENCODINGS)
01443 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingSupportedEncodings);
01444 if (se->nEncodings < MAX_ENCODINGS)
01445 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingServerIdentity);
01446
01447
01448 if (se->nEncodings < MAX_ENCODINGS)
01449 encs[se->nEncodings++] = rfbClientSwap32IfLE(rfbEncodingXvp);
01450
01451
01452 for(e = rfbClientExtensions; e; e = e->next)
01453 if(e->encodings) {
01454 int* enc;
01455 for(enc = e->encodings; *enc; enc++)
01456 encs[se->nEncodings++] = rfbClientSwap32IfLE(*enc);
01457 }
01458
01459 len = sz_rfbSetEncodingsMsg + se->nEncodings * 4;
01460
01461 se->nEncodings = rfbClientSwap16IfLE(se->nEncodings);
01462
01463 if (!WriteToRFBServer(client, buf, len)) return FALSE;
01464
01465 return TRUE;
01466 }
01467
01468
01469
01470
01471
01472
01473 rfbBool
01474 SendIncrementalFramebufferUpdateRequest(rfbClient* client)
01475 {
01476 return SendFramebufferUpdateRequest(client,
01477 client->updateRect.x, client->updateRect.y,
01478 client->updateRect.w, client->updateRect.h, TRUE);
01479 }
01480
01481
01482
01483
01484
01485
01486 rfbBool
01487 SendFramebufferUpdateRequest(rfbClient* client, int x, int y, int w, int h, rfbBool incremental)
01488 {
01489 rfbFramebufferUpdateRequestMsg fur;
01490
01491 if (!SupportsClient2Server(client, rfbFramebufferUpdateRequest)) return TRUE;
01492
01493 fur.type = rfbFramebufferUpdateRequest;
01494 fur.incremental = incremental ? 1 : 0;
01495 fur.x = rfbClientSwap16IfLE(x);
01496 fur.y = rfbClientSwap16IfLE(y);
01497 fur.w = rfbClientSwap16IfLE(w);
01498 fur.h = rfbClientSwap16IfLE(h);
01499
01500 if (!WriteToRFBServer(client, (char *)&fur, sz_rfbFramebufferUpdateRequestMsg))
01501 return FALSE;
01502
01503 return TRUE;
01504 }
01505
01506
01507
01508
01509
01510 rfbBool
01511 SendScaleSetting(rfbClient* client,int scaleSetting)
01512 {
01513 rfbSetScaleMsg ssm;
01514
01515 ssm.scale = scaleSetting;
01516 ssm.pad = 0;
01517
01518
01519 if (SupportsClient2Server(client, rfbSetScale)) {
01520 ssm.type = rfbSetScale;
01521 if (!WriteToRFBServer(client, (char *)&ssm, sz_rfbSetScaleMsg))
01522 return FALSE;
01523 }
01524
01525 if (SupportsClient2Server(client, rfbPalmVNCSetScaleFactor)) {
01526 ssm.type = rfbPalmVNCSetScaleFactor;
01527 if (!WriteToRFBServer(client, (char *)&ssm, sz_rfbSetScaleMsg))
01528 return FALSE;
01529 }
01530
01531 return TRUE;
01532 }
01533
01534
01535
01536
01537
01538
01539
01540 rfbBool TextChatSend(rfbClient* client, char *text)
01541 {
01542 rfbTextChatMsg chat;
01543 int count = strlen(text);
01544
01545 if (!SupportsClient2Server(client, rfbTextChat)) return TRUE;
01546 chat.type = rfbTextChat;
01547 chat.pad1 = 0;
01548 chat.pad2 = 0;
01549 chat.length = (uint32_t)count;
01550 chat.length = rfbClientSwap32IfLE(chat.length);
01551
01552 if (!WriteToRFBServer(client, (char *)&chat, sz_rfbTextChatMsg))
01553 return FALSE;
01554
01555 if (count>0) {
01556 if (!WriteToRFBServer(client, text, count))
01557 return FALSE;
01558 }
01559 return TRUE;
01560 }
01561
01562 rfbBool TextChatOpen(rfbClient* client)
01563 {
01564 rfbTextChatMsg chat;
01565
01566 if (!SupportsClient2Server(client, rfbTextChat)) return TRUE;
01567 chat.type = rfbTextChat;
01568 chat.pad1 = 0;
01569 chat.pad2 = 0;
01570 chat.length = rfbClientSwap32IfLE(rfbTextChatOpen);
01571 return (WriteToRFBServer(client, (char *)&chat, sz_rfbTextChatMsg) ? TRUE : FALSE);
01572 }
01573
01574 rfbBool TextChatClose(rfbClient* client)
01575 {
01576 rfbTextChatMsg chat;
01577 if (!SupportsClient2Server(client, rfbTextChat)) return TRUE;
01578 chat.type = rfbTextChat;
01579 chat.pad1 = 0;
01580 chat.pad2 = 0;
01581 chat.length = rfbClientSwap32IfLE(rfbTextChatClose);
01582 return (WriteToRFBServer(client, (char *)&chat, sz_rfbTextChatMsg) ? TRUE : FALSE);
01583 }
01584
01585 rfbBool TextChatFinish(rfbClient* client)
01586 {
01587 rfbTextChatMsg chat;
01588 if (!SupportsClient2Server(client, rfbTextChat)) return TRUE;
01589 chat.type = rfbTextChat;
01590 chat.pad1 = 0;
01591 chat.pad2 = 0;
01592 chat.length = rfbClientSwap32IfLE(rfbTextChatFinished);
01593 return (WriteToRFBServer(client, (char *)&chat, sz_rfbTextChatMsg) ? TRUE : FALSE);
01594 }
01595
01596
01597
01598
01599
01600
01601 rfbBool PermitServerInput(rfbClient* client, int enabled)
01602 {
01603 rfbSetServerInputMsg msg;
01604
01605 if (!SupportsClient2Server(client, rfbSetServerInput)) return TRUE;
01606
01607 msg.type = rfbSetServerInput;
01608 msg.status = (enabled ? 1 : 0);
01609 msg.pad = 0;
01610 return (WriteToRFBServer(client, (char *)&msg, sz_rfbSetServerInputMsg) ? TRUE : FALSE);
01611 }
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628 rfbBool SendXvpMsg(rfbClient* client, uint8_t version, uint8_t code)
01629 {
01630 rfbXvpMsg xvp;
01631
01632 if (!SupportsClient2Server(client, rfbXvp)) return TRUE;
01633 xvp.type = rfbXvp;
01634 xvp.pad = 0;
01635 xvp.version = version;
01636 xvp.code = code;
01637
01638 if (!WriteToRFBServer(client, (char *)&xvp, sz_rfbXvpMsg))
01639 return FALSE;
01640
01641 return TRUE;
01642 }
01643
01644
01645
01646
01647
01648
01649 rfbBool
01650 SendPointerEvent(rfbClient* client,int x, int y, int buttonMask)
01651 {
01652 rfbPointerEventMsg pe;
01653
01654 if (!SupportsClient2Server(client, rfbPointerEvent)) return TRUE;
01655
01656 pe.type = rfbPointerEvent;
01657 pe.buttonMask = buttonMask;
01658 if (x < 0) x = 0;
01659 if (y < 0) y = 0;
01660
01661 pe.x = rfbClientSwap16IfLE(x);
01662 pe.y = rfbClientSwap16IfLE(y);
01663 return WriteToRFBServer(client, (char *)&pe, sz_rfbPointerEventMsg);
01664 }
01665
01666
01667
01668
01669
01670
01671 rfbBool
01672 SendKeyEvent(rfbClient* client, uint32_t key, rfbBool down)
01673 {
01674 rfbKeyEventMsg ke;
01675
01676 if (!SupportsClient2Server(client, rfbKeyEvent)) return TRUE;
01677
01678 ke.type = rfbKeyEvent;
01679 ke.down = down ? 1 : 0;
01680 ke.key = rfbClientSwap32IfLE(key);
01681 return WriteToRFBServer(client, (char *)&ke, sz_rfbKeyEventMsg);
01682 }
01683
01684
01685
01686
01687
01688
01689 rfbBool
01690 SendClientCutText(rfbClient* client, char *str, int len)
01691 {
01692 rfbClientCutTextMsg cct;
01693
01694 if (!SupportsClient2Server(client, rfbClientCutText)) return TRUE;
01695
01696 cct.type = rfbClientCutText;
01697 cct.length = rfbClientSwap32IfLE(len);
01698 return (WriteToRFBServer(client, (char *)&cct, sz_rfbClientCutTextMsg) &&
01699 WriteToRFBServer(client, str, len));
01700 }
01701
01702
01703
01704
01705
01706
01707
01708 rfbBool
01709 HandleRFBServerMessage(rfbClient* client)
01710 {
01711 rfbServerToClientMsg msg;
01712
01713 if (client->serverPort==-1)
01714 client->vncRec->readTimestamp = TRUE;
01715 if (!ReadFromRFBServer(client, (char *)&msg, 1))
01716 return FALSE;
01717
01718 switch (msg.type) {
01719
01720 case rfbSetColourMapEntries:
01721 {
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746 break;
01747 }
01748
01749 case rfbFramebufferUpdate:
01750 {
01751 rfbFramebufferUpdateRectHeader rect;
01752 int linesToRead;
01753 int bytesPerLine;
01754 int i;
01755
01756 if (!ReadFromRFBServer(client, ((char *)&msg.fu) + 1,
01757 sz_rfbFramebufferUpdateMsg - 1))
01758 return FALSE;
01759
01760 msg.fu.nRects = rfbClientSwap16IfLE(msg.fu.nRects);
01761
01762 for (i = 0; i < msg.fu.nRects; i++) {
01763 if (!ReadFromRFBServer(client, (char *)&rect, sz_rfbFramebufferUpdateRectHeader))
01764 return FALSE;
01765
01766 rect.encoding = rfbClientSwap32IfLE(rect.encoding);
01767 if (rect.encoding == rfbEncodingLastRect)
01768 break;
01769
01770 rect.r.x = rfbClientSwap16IfLE(rect.r.x);
01771 rect.r.y = rfbClientSwap16IfLE(rect.r.y);
01772 rect.r.w = rfbClientSwap16IfLE(rect.r.w);
01773 rect.r.h = rfbClientSwap16IfLE(rect.r.h);
01774
01775
01776 if (rect.encoding == rfbEncodingXCursor ||
01777 rect.encoding == rfbEncodingRichCursor) {
01778
01779 if (!HandleCursorShape(client,
01780 rect.r.x, rect.r.y, rect.r.w, rect.r.h,
01781 rect.encoding)) {
01782 return FALSE;
01783 }
01784 continue;
01785 }
01786
01787 if (rect.encoding == rfbEncodingPointerPos) {
01788 if (!client->HandleCursorPos(client,rect.r.x, rect.r.y)) {
01789 return FALSE;
01790 }
01791 continue;
01792 }
01793
01794 if (rect.encoding == rfbEncodingKeyboardLedState) {
01795
01796 client->KeyboardLedStateEnabled = 1;
01797 if (client->HandleKeyboardLedState!=NULL)
01798 client->HandleKeyboardLedState(client, rect.r.x, 0);
01799
01800 client->CurrentKeyboardLedState = rect.r.x;
01801 continue;
01802 }
01803
01804 if (rect.encoding == rfbEncodingNewFBSize) {
01805 client->width = rect.r.w;
01806 client->height = rect.r.h;
01807 client->updateRect.x = client->updateRect.y = 0;
01808 client->updateRect.w = client->width;
01809 client->updateRect.h = client->height;
01810 client->MallocFrameBuffer(client);
01811 SendFramebufferUpdateRequest(client, 0, 0, rect.r.w, rect.r.h, FALSE);
01812 rfbClientLog("Got new framebuffer size: %dx%d\n", rect.r.w, rect.r.h);
01813 continue;
01814 }
01815
01816
01817 if (rect.encoding == rfbEncodingSupportedMessages) {
01818 int loop;
01819 if (!ReadFromRFBServer(client, (char *)&client->supportedMessages, sz_rfbSupportedMessages))
01820 return FALSE;
01821
01822
01823
01824
01825 rfbClientLog("client2server supported messages (bit flags)\n");
01826 for (loop=0;loop<32;loop+=8)
01827 rfbClientLog("%02X: %04x %04x %04x %04x - %04x %04x %04x %04x\n", loop,
01828 client->supportedMessages.client2server[loop], client->supportedMessages.client2server[loop+1],
01829 client->supportedMessages.client2server[loop+2], client->supportedMessages.client2server[loop+3],
01830 client->supportedMessages.client2server[loop+4], client->supportedMessages.client2server[loop+5],
01831 client->supportedMessages.client2server[loop+6], client->supportedMessages.client2server[loop+7]);
01832
01833 rfbClientLog("server2client supported messages (bit flags)\n");
01834 for (loop=0;loop<32;loop+=8)
01835 rfbClientLog("%02X: %04x %04x %04x %04x - %04x %04x %04x %04x\n", loop,
01836 client->supportedMessages.server2client[loop], client->supportedMessages.server2client[loop+1],
01837 client->supportedMessages.server2client[loop+2], client->supportedMessages.server2client[loop+3],
01838 client->supportedMessages.server2client[loop+4], client->supportedMessages.server2client[loop+5],
01839 client->supportedMessages.server2client[loop+6], client->supportedMessages.server2client[loop+7]);
01840 continue;
01841 }
01842
01843
01844 if (rect.encoding == rfbEncodingSupportedEncodings) {
01845 char *buffer;
01846 buffer = malloc(rect.r.w);
01847 if (!ReadFromRFBServer(client, buffer, rect.r.w))
01848 {
01849 free(buffer);
01850 return FALSE;
01851 }
01852
01853
01854
01855 free(buffer);
01856 continue;
01857 }
01858
01859
01860 if (rect.encoding == rfbEncodingServerIdentity) {
01861 char *buffer;
01862 buffer = malloc(rect.r.w+1);
01863 if (!ReadFromRFBServer(client, buffer, rect.r.w))
01864 {
01865 free(buffer);
01866 return FALSE;
01867 }
01868 buffer[rect.r.w]=0;
01869 rfbClientLog("Connected to Server \"%s\"\n", buffer);
01870 free(buffer);
01871 continue;
01872 }
01873
01874
01875 if (rect.encoding != rfbEncodingUltraZip)
01876 {
01877 if ((rect.r.x + rect.r.w > client->width) ||
01878 (rect.r.y + rect.r.h > client->height))
01879 {
01880 rfbClientLog("Rect too large: %dx%d at (%d, %d)\n",
01881 rect.r.w, rect.r.h, rect.r.x, rect.r.y);
01882 return FALSE;
01883 }
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897 client->SoftCursorLockArea(client, rect.r.x, rect.r.y, rect.r.w, rect.r.h);
01898 }
01899
01900 switch (rect.encoding) {
01901
01902 case rfbEncodingRaw: {
01903 int y=rect.r.y, h=rect.r.h;
01904
01905 bytesPerLine = rect.r.w * client->format.bitsPerPixel / 8;
01906 linesToRead = RFB_BUFFER_SIZE / bytesPerLine;
01907
01908 while (h > 0) {
01909 if (linesToRead > h)
01910 linesToRead = h;
01911
01912 if (!ReadFromRFBServer(client, client->buffer,bytesPerLine * linesToRead))
01913 return FALSE;
01914
01915 CopyRectangle(client, (uint8_t *)client->buffer,
01916 rect.r.x, y, rect.r.w,linesToRead);
01917
01918 h -= linesToRead;
01919 y += linesToRead;
01920
01921 }
01922 } break;
01923
01924 case rfbEncodingCopyRect:
01925 {
01926 rfbCopyRect cr;
01927
01928 if (!ReadFromRFBServer(client, (char *)&cr, sz_rfbCopyRect))
01929 return FALSE;
01930
01931 cr.srcX = rfbClientSwap16IfLE(cr.srcX);
01932 cr.srcY = rfbClientSwap16IfLE(cr.srcY);
01933
01934
01935
01936
01937 client->SoftCursorLockArea(client,
01938 cr.srcX, cr.srcY, rect.r.w, rect.r.h);
01939
01940 if (client->GotCopyRect != NULL) {
01941 client->GotCopyRect(client, cr.srcX, cr.srcY, rect.r.w, rect.r.h,
01942 rect.r.x, rect.r.y);
01943 } else
01944 CopyRectangleFromRectangle(client,
01945 cr.srcX, cr.srcY, rect.r.w, rect.r.h,
01946 rect.r.x, rect.r.y);
01947
01948 break;
01949 }
01950
01951 case rfbEncodingRRE:
01952 {
01953 switch (client->format.bitsPerPixel) {
01954 case 8:
01955 if (!HandleRRE8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01956 return FALSE;
01957 break;
01958 case 16:
01959 if (!HandleRRE16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01960 return FALSE;
01961 break;
01962 case 32:
01963 if (!HandleRRE32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01964 return FALSE;
01965 break;
01966 }
01967 break;
01968 }
01969
01970 case rfbEncodingCoRRE:
01971 {
01972 switch (client->format.bitsPerPixel) {
01973 case 8:
01974 if (!HandleCoRRE8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01975 return FALSE;
01976 break;
01977 case 16:
01978 if (!HandleCoRRE16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01979 return FALSE;
01980 break;
01981 case 32:
01982 if (!HandleCoRRE32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01983 return FALSE;
01984 break;
01985 }
01986 break;
01987 }
01988
01989 case rfbEncodingHextile:
01990 {
01991 switch (client->format.bitsPerPixel) {
01992 case 8:
01993 if (!HandleHextile8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01994 return FALSE;
01995 break;
01996 case 16:
01997 if (!HandleHextile16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
01998 return FALSE;
01999 break;
02000 case 32:
02001 if (!HandleHextile32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02002 return FALSE;
02003 break;
02004 }
02005 break;
02006 }
02007
02008 case rfbEncodingUltra:
02009 {
02010 switch (client->format.bitsPerPixel) {
02011 case 8:
02012 if (!HandleUltra8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02013 return FALSE;
02014 break;
02015 case 16:
02016 if (!HandleUltra16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02017 return FALSE;
02018 break;
02019 case 32:
02020 if (!HandleUltra32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02021 return FALSE;
02022 break;
02023 }
02024 break;
02025 }
02026 case rfbEncodingUltraZip:
02027 {
02028 switch (client->format.bitsPerPixel) {
02029 case 8:
02030 if (!HandleUltraZip8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02031 return FALSE;
02032 break;
02033 case 16:
02034 if (!HandleUltraZip16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02035 return FALSE;
02036 break;
02037 case 32:
02038 if (!HandleUltraZip32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02039 return FALSE;
02040 break;
02041 }
02042 break;
02043 }
02044
02045 #ifdef LIBVNCSERVER_HAVE_LIBZ
02046 case rfbEncodingZlib:
02047 {
02048 switch (client->format.bitsPerPixel) {
02049 case 8:
02050 if (!HandleZlib8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02051 return FALSE;
02052 break;
02053 case 16:
02054 if (!HandleZlib16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02055 return FALSE;
02056 break;
02057 case 32:
02058 if (!HandleZlib32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02059 return FALSE;
02060 break;
02061 }
02062 break;
02063 }
02064
02065 #ifdef LIBVNCSERVER_HAVE_LIBJPEG
02066 case rfbEncodingTight:
02067 {
02068 switch (client->format.bitsPerPixel) {
02069 case 8:
02070 if (!HandleTight8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02071 return FALSE;
02072 break;
02073 case 16:
02074 if (!HandleTight16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02075 return FALSE;
02076 break;
02077 case 32:
02078 if (!HandleTight32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02079 return FALSE;
02080 break;
02081 }
02082 break;
02083 }
02084 #endif
02085 case rfbEncodingZRLE:
02086
02087 client->appData.qualityLevel = 9;
02088
02089 case rfbEncodingZYWRLE:
02090 {
02091 switch (client->format.bitsPerPixel) {
02092 case 8:
02093 if (!HandleZRLE8(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02094 return FALSE;
02095 break;
02096 case 16:
02097 if (client->si.format.greenMax > 0x1F) {
02098 if (!HandleZRLE16(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02099 return FALSE;
02100 } else {
02101 if (!HandleZRLE15(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02102 return FALSE;
02103 }
02104 break;
02105 case 32:
02106 {
02107 uint32_t maxColor=(client->format.redMax<<client->format.redShift)|
02108 (client->format.greenMax<<client->format.greenShift)|
02109 (client->format.blueMax<<client->format.blueShift);
02110 if ((client->format.bigEndian && (maxColor&0xff)==0) ||
02111 (!client->format.bigEndian && (maxColor&0xff000000)==0)) {
02112 if (!HandleZRLE24(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02113 return FALSE;
02114 } else if (!client->format.bigEndian && (maxColor&0xff)==0) {
02115 if (!HandleZRLE24Up(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02116 return FALSE;
02117 } else if (client->format.bigEndian && (maxColor&0xff000000)==0) {
02118 if (!HandleZRLE24Down(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02119 return FALSE;
02120 } else if (!HandleZRLE32(client, rect.r.x,rect.r.y,rect.r.w,rect.r.h))
02121 return FALSE;
02122 break;
02123 }
02124 }
02125 break;
02126 }
02127
02128 #endif
02129
02130 default:
02131 {
02132 rfbBool handled = FALSE;
02133 rfbClientProtocolExtension* e;
02134
02135 for(e = rfbClientExtensions; !handled && e; e = e->next)
02136 if(e->handleEncoding && e->handleEncoding(client, &rect))
02137 handled = TRUE;
02138
02139 if(!handled) {
02140 rfbClientLog("Unknown rect encoding %d\n",
02141 (int)rect.encoding);
02142 return FALSE;
02143 }
02144 }
02145 }
02146
02147
02148 client->SoftCursorUnlockScreen(client);
02149
02150 client->GotFrameBufferUpdate(client, rect.r.x, rect.r.y, rect.r.w, rect.r.h);
02151 }
02152
02153 if (!SendIncrementalFramebufferUpdateRequest(client))
02154 return FALSE;
02155
02156 if (client->FinishedFrameBufferUpdate)
02157 client->FinishedFrameBufferUpdate(client);
02158
02159 break;
02160 }
02161
02162 case rfbBell:
02163 {
02164 client->Bell(client);
02165
02166 break;
02167 }
02168
02169 case rfbServerCutText:
02170 {
02171 char *buffer;
02172
02173 if (!ReadFromRFBServer(client, ((char *)&msg) + 1,
02174 sz_rfbServerCutTextMsg - 1))
02175 return FALSE;
02176
02177 msg.sct.length = rfbClientSwap32IfLE(msg.sct.length);
02178
02179 buffer = malloc(msg.sct.length+1);
02180
02181 if (!ReadFromRFBServer(client, buffer, msg.sct.length))
02182 return FALSE;
02183
02184 buffer[msg.sct.length] = 0;
02185
02186 if (client->GotXCutText)
02187 client->GotXCutText(client, buffer, msg.sct.length);
02188
02189 free(buffer);
02190
02191 break;
02192 }
02193
02194 case rfbTextChat:
02195 {
02196 char *buffer=NULL;
02197 if (!ReadFromRFBServer(client, ((char *)&msg) + 1,
02198 sz_rfbTextChatMsg- 1))
02199 return FALSE;
02200 msg.tc.length = rfbClientSwap32IfLE(msg.sct.length);
02201 switch(msg.tc.length) {
02202 case rfbTextChatOpen:
02203 rfbClientLog("Received TextChat Open\n");
02204 if (client->HandleTextChat!=NULL)
02205 client->HandleTextChat(client, (int)rfbTextChatOpen, NULL);
02206 break;
02207 case rfbTextChatClose:
02208 rfbClientLog("Received TextChat Close\n");
02209 if (client->HandleTextChat!=NULL)
02210 client->HandleTextChat(client, (int)rfbTextChatClose, NULL);
02211 break;
02212 case rfbTextChatFinished:
02213 rfbClientLog("Received TextChat Finished\n");
02214 if (client->HandleTextChat!=NULL)
02215 client->HandleTextChat(client, (int)rfbTextChatFinished, NULL);
02216 break;
02217 default:
02218 buffer=malloc(msg.tc.length+1);
02219 if (!ReadFromRFBServer(client, buffer, msg.tc.length))
02220 {
02221 free(buffer);
02222 return FALSE;
02223 }
02224
02225 buffer[msg.tc.length]=0;
02226 rfbClientLog("Received TextChat \"%s\"\n", buffer);
02227 if (client->HandleTextChat!=NULL)
02228 client->HandleTextChat(client, (int)msg.tc.length, buffer);
02229 free(buffer);
02230 break;
02231 }
02232 break;
02233 }
02234
02235 case rfbXvp:
02236 {
02237 if (!ReadFromRFBServer(client, ((char *)&msg) + 1,
02238 sz_rfbXvpMsg -1))
02239 return FALSE;
02240
02241 SetClient2Server(client, rfbXvp);
02242
02243
02244
02245 SetServer2Client(client, rfbXvp);
02246
02247 if(client->HandleXvpMsg)
02248 client->HandleXvpMsg(client, msg.xvp.version, msg.xvp.code);
02249
02250 break;
02251 }
02252
02253 case rfbResizeFrameBuffer:
02254 {
02255 if (!ReadFromRFBServer(client, ((char *)&msg) + 1,
02256 sz_rfbResizeFrameBufferMsg -1))
02257 return FALSE;
02258 client->width = rfbClientSwap16IfLE(msg.rsfb.framebufferWidth);
02259 client->height = rfbClientSwap16IfLE(msg.rsfb.framebufferHeigth);
02260 client->updateRect.x = client->updateRect.y = 0;
02261 client->updateRect.w = client->width;
02262 client->updateRect.h = client->height;
02263 client->MallocFrameBuffer(client);
02264 SendFramebufferUpdateRequest(client, 0, 0, client->width, client->height, FALSE);
02265 rfbClientLog("Got new framebuffer size: %dx%d\n", client->width, client->height);
02266 break;
02267 }
02268
02269 case rfbPalmVNCReSizeFrameBuffer:
02270 {
02271 if (!ReadFromRFBServer(client, ((char *)&msg) + 1,
02272 sz_rfbPalmVNCReSizeFrameBufferMsg -1))
02273 return FALSE;
02274 client->width = rfbClientSwap16IfLE(msg.prsfb.buffer_w);
02275 client->height = rfbClientSwap16IfLE(msg.prsfb.buffer_h);
02276 client->updateRect.x = client->updateRect.y = 0;
02277 client->updateRect.w = client->width;
02278 client->updateRect.h = client->height;
02279 client->MallocFrameBuffer(client);
02280 SendFramebufferUpdateRequest(client, 0, 0, client->width, client->height, FALSE);
02281 rfbClientLog("Got new framebuffer size: %dx%d\n", client->width, client->height);
02282 break;
02283 }
02284
02285 default:
02286 {
02287 rfbBool handled = FALSE;
02288 rfbClientProtocolExtension* e;
02289
02290 for(e = rfbClientExtensions; !handled && e; e = e->next)
02291 if(e->handleMessage && e->handleMessage(client, &msg))
02292 handled = TRUE;
02293
02294 if(!handled) {
02295 char buffer[256];
02296 rfbClientLog("Unknown message type %d from VNC server\n",msg.type);
02297 ReadFromRFBServer(client, buffer, 256);
02298 return FALSE;
02299 }
02300 }
02301 }
02302
02303 return TRUE;
02304 }
02305
02306
02307 #define GET_PIXEL8(pix, ptr) ((pix) = *(ptr)++)
02308
02309 #define GET_PIXEL16(pix, ptr) (((uint8_t*)&(pix))[0] = *(ptr)++, \
02310 ((uint8_t*)&(pix))[1] = *(ptr)++)
02311
02312 #define GET_PIXEL32(pix, ptr) (((uint8_t*)&(pix))[0] = *(ptr)++, \
02313 ((uint8_t*)&(pix))[1] = *(ptr)++, \
02314 ((uint8_t*)&(pix))[2] = *(ptr)++, \
02315 ((uint8_t*)&(pix))[3] = *(ptr)++)
02316
02317
02318
02319
02320 #define CONCAT2(a,b) a##b
02321 #define CONCAT2E(a,b) CONCAT2(a,b)
02322 #define CONCAT3(a,b,c) a##b##c
02323 #define CONCAT3E(a,b,c) CONCAT3(a,b,c)
02324
02325 #define BPP 8
02326 #include "rre.c"
02327 #include "corre.c"
02328 #include "hextile.c"
02329 #include "ultra.c"
02330 #include "zlib.c"
02331 #include "tight.c"
02332 #include "zrle.c"
02333 #undef BPP
02334 #define BPP 16
02335 #include "rre.c"
02336 #include "corre.c"
02337 #include "hextile.c"
02338 #include "ultra.c"
02339 #include "zlib.c"
02340 #include "tight.c"
02341 #include "zrle.c"
02342 #define REALBPP 15
02343 #include "zrle.c"
02344 #undef BPP
02345 #define BPP 32
02346 #include "rre.c"
02347 #include "corre.c"
02348 #include "hextile.c"
02349 #include "ultra.c"
02350 #include "zlib.c"
02351 #include "tight.c"
02352 #include "zrle.c"
02353 #define REALBPP 24
02354 #include "zrle.c"
02355 #define REALBPP 24
02356 #define UNCOMP 8
02357 #include "zrle.c"
02358 #define REALBPP 24
02359 #define UNCOMP -8
02360 #include "zrle.c"
02361 #undef BPP
02362
02363
02364
02365
02366
02367
02368 void
02369 PrintPixelFormat(rfbPixelFormat *format)
02370 {
02371 if (format->bitsPerPixel == 1) {
02372 rfbClientLog(" Single bit per pixel.\n");
02373 rfbClientLog(
02374 " %s significant bit in each byte is leftmost on the screen.\n",
02375 (format->bigEndian ? "Most" : "Least"));
02376 } else {
02377 rfbClientLog(" %d bits per pixel.\n",format->bitsPerPixel);
02378 if (format->bitsPerPixel != 8) {
02379 rfbClientLog(" %s significant byte first in each pixel.\n",
02380 (format->bigEndian ? "Most" : "Least"));
02381 }
02382 if (format->trueColour) {
02383 rfbClientLog(" TRUE colour: max red %d green %d blue %d"
02384 ", shift red %d green %d blue %d\n",
02385 format->redMax, format->greenMax, format->blueMax,
02386 format->redShift, format->greenShift, format->blueShift);
02387 } else {
02388 rfbClientLog(" Colour map (not true colour).\n");
02389 }
02390 }
02391 }
02392
02393
02394
02395 #define rfbEncryptBytes rfbClientEncryptBytes
02396 #define rfbEncryptBytes2 rfbClientEncryptBytes2
02397 #define rfbDes rfbClientDes
02398 #define rfbDesKey rfbClientDesKey
02399 #define rfbUseKey rfbClientUseKey
02400 #define rfbCPKey rfbClientCPKey
02401
02402 #include "vncauth.c"
02403 #include "d3des.c"