From 896ca2036c35b89a7f63e1adefe5e3724bf4d40d Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 19 Jul 2011 13:40:34 +0200 Subject: tightPng: Add initial tightPng encoding support. http://wiki.qemu.org/VNC_Tight_PNG Signed-off-by: Joel Martin Signed-off-by: Christian Beier --- libvncserver/rfbserver.c | 49 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index 8f0e390..587a2f0 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -358,10 +358,12 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, rfbScreen->clientHead = cl; UNLOCK(rfbClientListMutex); -#ifdef LIBVNCSERVER_HAVE_LIBZ +#if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG) cl->tightQualityLevel = -1; -#ifdef LIBVNCSERVER_HAVE_LIBJPEG +#if defined(LIBVNCSERVER_HAVE_LIBJPEG) || defined(LIBVNCSERVER_HAVE_LIBPNG) cl->tightCompressLevel = TIGHT_DEFAULT_COMPRESSION; +#endif +#ifdef LIBVNCSERVER_HAVE_LIBJPEG { int i; for (i = 0; i < 4; i++) @@ -917,6 +919,9 @@ rfbSendSupportedEncodings(rfbClientPtr cl) #endif #ifdef LIBVNCSERVER_HAVE_LIBJPEG rfbEncodingTight, +#endif +#ifdef LIBVNCSERVER_HAVE_LIBPNG + rfbEncodingTightPng, #endif rfbEncodingUltra, rfbEncodingUltraZip, @@ -1937,6 +1942,9 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) #ifdef LIBVNCSERVER_HAVE_LIBJPEG case rfbEncodingTight: #endif +#endif +#ifdef LIBVNCSERVER_HAVE_LIBPNG + case rfbEncodingTightPng: #endif /* The first supported encoding is the 'preferred' encoding */ if (cl->preferredEncoding == -1) @@ -2026,11 +2034,11 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) } break; default: -#ifdef LIBVNCSERVER_HAVE_LIBZ +#if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG) if ( enc >= (uint32_t)rfbEncodingCompressLevel0 && enc <= (uint32_t)rfbEncodingCompressLevel9 ) { cl->zlibCompressLevel = enc & 0x0F; -#ifdef LIBVNCSERVER_HAVE_LIBJPEG +#if defined(LIBVNCSERVER_HAVE_LIBJPEG) || defined(LIBVNCSERVER_HAVE_LIBPNG) cl->tightCompressLevel = enc & 0x0F; rfbLog("Using compression level %d for client %s\n", cl->tightCompressLevel, cl->host); @@ -2754,6 +2762,28 @@ rfbSendFramebufferUpdate(rfbClientPtr cl, } sraRgnReleaseIterator(i); i=NULL; #endif +#endif +#ifdef LIBVNCSERVER_HAVE_LIBPNG + } else if (cl->preferredEncoding == rfbEncodingTightPng) { + nUpdateRegionRects = 0; + + for(i = sraRgnGetIterator(updateRegion); sraRgnIteratorNext(i,&rect);){ + int x = rect.x1; + int y = rect.y1; + int w = rect.x2 - x; + int h = rect.y2 - y; + int n; + /* We need to count the number of rects in the scaled screen */ + if (cl->screen!=cl->scaledScreen) + rfbScaledCorrection(cl->screen, cl->scaledScreen, &x, &y, &w, &h, "rfbSendFramebufferUpdate"); + n = rfbNumCodedRectsTight(cl, x, y, w, h); + if (n == 0) { + nUpdateRegionRects = 0xFFFF; + break; + } + nUpdateRegionRects += n; + } + sraRgnReleaseIterator(i); i=NULL; #endif } else { nUpdateRegionRects = sraRgnCountRects(updateRegion); @@ -2773,6 +2803,10 @@ rfbSendFramebufferUpdate(rfbClientPtr cl, /* Tight encoding counts the rectangles differently */ && cl->preferredEncoding != rfbEncodingTight #endif +#endif +#ifdef LIBVNCSERVER_HAVE_LIBPNG + /* Tight encoding counts the rectangles differently */ + && cl->preferredEncoding != rfbEncodingTightPng #endif && nUpdateRegionRects>cl->screen->maxRectsPerUpdate) { sraRegion* newUpdateRegion = sraRgnBBox(updateRegion); @@ -2868,6 +2902,13 @@ rfbSendFramebufferUpdate(rfbClientPtr cl, break; #endif #endif +#ifdef LIBVNCSERVER_HAVE_LIBPNG + case rfbEncodingTightPng: + /* TODO */ + if (!rfbSendRectEncodingTightPng(cl, x, y, w, h)) + goto updateFailed; + break; +#endif #ifdef LIBVNCSERVER_HAVE_LIBZ case rfbEncodingZRLE: case rfbEncodingZYWRLE: -- cgit v1.2.1 From 6fac22a74b5020387a6961e4cc197b5fa4743f96 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 16 Aug 2011 14:02:31 +0200 Subject: websockets: Initial WebSockets support. Has a bug: WebSocket client disconnects are not detected. rfbSendFramebufferUpdate is doing a MSG_PEEK recv to determine if enough data is available which prevents a disconnect from being detected. Otherwise it's working pretty well. [jes: moved added struct members to the end for binary compatibility with previous LibVNCServer versions, removed an unused variable] Signed-off-by: Johannes Schindelin --- libvncserver/rfbserver.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index 587a2f0..1df4fee 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -358,6 +358,14 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, rfbScreen->clientHead = cl; UNLOCK(rfbClientListMutex); +#ifdef LIBVNCSERVER_WITH_WEBSOCKETS + cl->webSockets = FALSE; + cl->webSocketsSSL = FALSE; + cl->webSocketsBase64 = FALSE; + cl->dblen= 0; + cl->carrylen = 0; +#endif + #if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG) cl->tightQualityLevel = -1; #if defined(LIBVNCSERVER_HAVE_LIBJPEG) || defined(LIBVNCSERVER_HAVE_LIBPNG) @@ -404,6 +412,20 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, cl->lastPtrX = -1; +#ifdef LIBVNCSERVER_WITH_WEBSOCKETS + /* + * Wait a few ms for the client to send one of: + * - Flash policy request + * - WebSockets connection (TLS/SSL or plain) + */ + if (!webSocketsCheck(cl)) { + /* Error reporting handled in webSocketsHandshake */ + rfbCloseClient(cl); + rfbClientConnectionGone(cl); + return NULL; + } +#endif + sprintf(pv,rfbProtocolVersionFormat,rfbScreen->protocolMajorVersion, rfbScreen->protocolMinorVersion); @@ -1817,6 +1839,16 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) char encBuf[64]; char encBuf2[64]; +#ifdef LIBVNCSERVER_WITH_WEBSOCKETS + if (cl->webSockets && cl->webSocketsBase64) { + /* With Base64 encoding we need at least 4 bytes */ + n = recv(cl->sock, encBuf, 4, MSG_PEEK); + if ((n > 0) && (n < 4)) { + return; + } + } +#endif + if ((n = rfbReadExact(cl, (char *)&msg, 1)) <= 0) { if (n != 0) rfbLogPerror("rfbProcessClientNormalMessage: read"); @@ -2904,7 +2936,6 @@ rfbSendFramebufferUpdate(rfbClientPtr cl, #endif #ifdef LIBVNCSERVER_HAVE_LIBPNG case rfbEncodingTightPng: - /* TODO */ if (!rfbSendRectEncodingTightPng(cl, x, y, w, h)) goto updateFailed; break; -- cgit v1.2.1 From 0860c4951fd1b6d90158c35ead4a4c33635802bb Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 16 Aug 2011 14:02:32 +0200 Subject: websockets: Better disconnect detection. If the only thing we are waiting on is a WebSockets terminator, then remove it from the stream early on in rfbProcessClientNormalMessage. Signed-off-by: Johannes Schindelin --- libvncserver/rfbserver.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index 1df4fee..491a438 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -1844,6 +1844,11 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) /* With Base64 encoding we need at least 4 bytes */ n = recv(cl->sock, encBuf, 4, MSG_PEEK); if ((n > 0) && (n < 4)) { + if (encBuf[0] == '\xff') { + /* Make sure we don't miss a client disconnect on an end frame + * marker */ + n = read(cl->sock, encBuf, 1); + } return; } } -- cgit v1.2.1 From 430b8f2449dee8c7128542678aa09c9f4588b939 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 16 Aug 2011 14:02:33 +0200 Subject: websockets: Add UTF-8 encoding support. This is not completely standard UTF-8 encoding. Only code points 0-255 are encoded and never encoded to more than two octets. Since '\x00' is a WebSockets framing character, it's easier for all parties to encode zero as '\xc4\x80', i.e. 194+128, i.e. UTF-8 256. This means that a random stream will be slightly more than 50% larger using this encoding scheme. But it's easy CPU-wise for client and server to decode/encode. This is especially important for clients written in languages that have weak bitops, like Javascript (i.e. the noVNC client). Signed-off-by: Johannes Schindelin --- libvncserver/rfbserver.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index 491a438..25204cd 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -1840,16 +1840,28 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) char encBuf2[64]; #ifdef LIBVNCSERVER_WITH_WEBSOCKETS - if (cl->webSockets && cl->webSocketsBase64) { - /* With Base64 encoding we need at least 4 bytes */ + if (cl->webSockets) { n = recv(cl->sock, encBuf, 4, MSG_PEEK); - if ((n > 0) && (n < 4)) { - if (encBuf[0] == '\xff') { - /* Make sure we don't miss a client disconnect on an end frame - * marker */ - n = read(cl->sock, encBuf, 1); + if (cl->webSocketsBase64) { + /* With Base64 encoding we need at least 4 bytes */ + if ((n > 0) && (n < 4)) { + if (encBuf[0] == '\xff') { + /* Make sure we don't miss a client disconnect on an end frame + * marker */ + n = read(cl->sock, encBuf, 1); + } + return; + } + } else { + /* With UTF-8 encoding we need at least 3 bytes (framing + 1) */ + if ((n == 1) || (n == 2)) { + if (encBuf[0] == '\xff') { + /* Make sure we don't miss a client disconnect on an end frame + * marker */ + n = read(cl->sock, encBuf, 1); + } + return; } - return; } } #endif -- cgit v1.2.1 From 4aa35863676335917d2a25a7952031f0fba66dfb Mon Sep 17 00:00:00 2001 From: Gernot Tenchio Date: Tue, 16 Aug 2011 14:02:35 +0200 Subject: websockets: Add encryption support [jes: moved out GnuTLS and OpenSSL support, added a dummy support, to separate changes better, and to keep things compiling] Signed-off-by: Johannes Schindelin --- libvncserver/rfbserver.c | 55 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index 25204cd..c623329 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -73,6 +73,10 @@ /* strftime() */ #include +#ifdef LIBVNCSERVER_WITH_WEBSOCKETS +#include "rfbssl.h" +#endif + #ifdef __MINGW32__ static int compat_mkdir(const char *path, int mode) { @@ -360,7 +364,6 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, #ifdef LIBVNCSERVER_WITH_WEBSOCKETS cl->webSockets = FALSE; - cl->webSocketsSSL = FALSE; cl->webSocketsBase64 = FALSE; cl->dblen= 0; cl->carrylen = 0; @@ -1841,16 +1844,50 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) #ifdef LIBVNCSERVER_WITH_WEBSOCKETS if (cl->webSockets) { - n = recv(cl->sock, encBuf, 4, MSG_PEEK); - if (cl->webSocketsBase64) { + if (cl->sslctx) + n = rfbssl_peek(cl, encBuf, 4); + else + n = recv(cl->sock, encBuf, 4, MSG_PEEK); + + if (n <= 0) { + if (n != 0) + rfbLogPerror("rfbProcessClientNormalMessage: peek"); + rfbCloseClient(cl); + return; + } + + if (cl->webSocketsBase64) { /* With Base64 encoding we need at least 4 bytes */ if ((n > 0) && (n < 4)) { if (encBuf[0] == '\xff') { + int doclose = 0; /* Make sure we don't miss a client disconnect on an end frame - * marker */ - n = read(cl->sock, encBuf, 1); + * marker. Because we use a peek buffer in some cases it is not + * applicable to wait for more data per select(). */ + switch (n) { + case 3: + if (encBuf[1] == '\xff' && encBuf[2] == '\x00') + doclose = 1; + break; + case 2: + if (encBuf[1] == '\x00') + doclose = 1; + break; + default: + ; + } + + if (cl->sslctx) + n = rfbssl_read(cl, encBuf, n); + else + n = read(cl->sock, encBuf, n); + + if (doclose) { + rfbErr("rfbProcessClientNormalMessage: websocket close frame received\n"); + rfbCloseClient(cl); + } + return; } - return; } } else { /* With UTF-8 encoding we need at least 3 bytes (framing + 1) */ @@ -1858,9 +1895,11 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) if (encBuf[0] == '\xff') { /* Make sure we don't miss a client disconnect on an end frame * marker */ - n = read(cl->sock, encBuf, 1); + if (cl->sslctx) + n = rfbssl_read(cl, encBuf, 1); + else + n = read(cl->sock, encBuf, 1); } - return; } } } -- cgit v1.2.1 From 1408866c864cac3b1bbf37eb9fdc8d303f37957d Mon Sep 17 00:00:00 2001 From: Gernot Tenchio Date: Thu, 25 Aug 2011 10:58:19 +0200 Subject: websockets: Initial HyBi support --- libvncserver/rfbserver.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index c623329..d6a5da4 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -365,8 +365,6 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, #ifdef LIBVNCSERVER_WITH_WEBSOCKETS cl->webSockets = FALSE; cl->webSocketsBase64 = FALSE; - cl->dblen= 0; - cl->carrylen = 0; #endif #if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG) -- cgit v1.2.1 From 55234a37fd0f865261c09b602b94444d42f35daa Mon Sep 17 00:00:00 2001 From: Gernot Tenchio Date: Thu, 25 Aug 2011 12:12:17 +0200 Subject: websockets: Move Hixie disconnect hack to websockets.c Move the hixie disconnect hack to websockets.c. Removed the remaining websockets vars from rfbClientPtr, so all websockets stuff is hidden behind an opaque pointer. --- libvncserver/rfbserver.c | 67 ++---------------------------------------------- 1 file changed, 2 insertions(+), 65 deletions(-) (limited to 'libvncserver/rfbserver.c') diff --git a/libvncserver/rfbserver.c b/libvncserver/rfbserver.c index d6a5da4..63f21db 100644 --- a/libvncserver/rfbserver.c +++ b/libvncserver/rfbserver.c @@ -362,11 +362,6 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen, rfbScreen->clientHead = cl; UNLOCK(rfbClientListMutex); -#ifdef LIBVNCSERVER_WITH_WEBSOCKETS - cl->webSockets = FALSE; - cl->webSocketsBase64 = FALSE; -#endif - #if defined(LIBVNCSERVER_HAVE_LIBZ) || defined(LIBVNCSERVER_HAVE_LIBPNG) cl->tightQualityLevel = -1; #if defined(LIBVNCSERVER_HAVE_LIBJPEG) || defined(LIBVNCSERVER_HAVE_LIBPNG) @@ -1841,66 +1836,8 @@ rfbProcessClientNormalMessage(rfbClientPtr cl) char encBuf2[64]; #ifdef LIBVNCSERVER_WITH_WEBSOCKETS - if (cl->webSockets) { - if (cl->sslctx) - n = rfbssl_peek(cl, encBuf, 4); - else - n = recv(cl->sock, encBuf, 4, MSG_PEEK); - - if (n <= 0) { - if (n != 0) - rfbLogPerror("rfbProcessClientNormalMessage: peek"); - rfbCloseClient(cl); - return; - } - - if (cl->webSocketsBase64) { - /* With Base64 encoding we need at least 4 bytes */ - if ((n > 0) && (n < 4)) { - if (encBuf[0] == '\xff') { - int doclose = 0; - /* Make sure we don't miss a client disconnect on an end frame - * marker. Because we use a peek buffer in some cases it is not - * applicable to wait for more data per select(). */ - switch (n) { - case 3: - if (encBuf[1] == '\xff' && encBuf[2] == '\x00') - doclose = 1; - break; - case 2: - if (encBuf[1] == '\x00') - doclose = 1; - break; - default: - ; - } - - if (cl->sslctx) - n = rfbssl_read(cl, encBuf, n); - else - n = read(cl->sock, encBuf, n); - - if (doclose) { - rfbErr("rfbProcessClientNormalMessage: websocket close frame received\n"); - rfbCloseClient(cl); - } - return; - } - } - } else { - /* With UTF-8 encoding we need at least 3 bytes (framing + 1) */ - if ((n == 1) || (n == 2)) { - if (encBuf[0] == '\xff') { - /* Make sure we don't miss a client disconnect on an end frame - * marker */ - if (cl->sslctx) - n = rfbssl_read(cl, encBuf, 1); - else - n = read(cl->sock, encBuf, 1); - } - } - } - } + if (cl->wsctx && webSocketCheckDisconnect(cl)) + return; #endif if ((n = rfbReadExact(cl, (char *)&msg, 1)) <= 0) { -- cgit v1.2.1