Skip to contentSkip to conversation

vnc: added initial websocket protocol support

PATCH Superseded Tim Hardeck 2 participants 7 messages Dec 7, 2012 – Dec 12, 2012

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover vnc: added initial websocket protocol support

This patch set adds basic Websocket Protocol version 13 - RFC 6455 - support to QEMU VNC. Binary encoding support on the client side is mandatory.

Because of the GnuTLS requirement the Websockets implementation is optional (--enable-vnc-ws).

To activate Websocket support the VNC option "websocket"is used, for example "-vnc :0,websocket".
The listen port for Websocket connections is (5700 + display) so if QEMU VNC is started with :0 the Websocket port would be 5700. As an alternative the Websocket port could be manually specified by using ",websocket=<port>" instead.

Changes v2
* removed automatic websocket recognition
* added new lwebsock socket on port 5700 + display when the vnc option "websocket" is passed on
* adapted vnc_connect vnc_listen_read to differ between websocket
* added separate event handler to read the Websocket handshake

Changes v3
* added manual port specification by using ",websocket=<port>"
* switched from memmem() to g_strstr_len()
* removed masked_size from vncws_decode_frame()
* resetted vnc_tls variable to default in the configure script

Changes v4
* incorporated suggestions from Stefan Hajnoczi
* moved websockets encoding from vnc_write to its own client_write function
* moved websockets decoding to its own client_read function
* added initialization checks to vnc_disconnect to prevent crashes if a regular client connects to the websocket port

Tim Hardeck (3):
vnc: added buffer_advance function
vnc: added initial websocket protocol support
vnc: fix possible uninitialized removals

 configure        |   27 +++++-
 qemu-options.hx  |    8 ++
 ui/Makefile.objs |    1 +
 ui/vnc-ws.c      |  282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui/vnc-ws.h      |   92 ++++++++++++++++++
 ui/vnc.c         |  223 ++++++++++++++++++++++++++++++++++++------
 ui/vnc.h         |   20 ++++
 7 files changed, 620 insertions(+), 33 deletions(-)
 create mode 100644 ui/vnc-ws.c
 create mode 100644 ui/vnc-ws.h
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v4 1/3 vnc: added buffer_advance function

Following Anthony Liguori's Websocket implementation I have added the buffer_advance function to VNC and replaced all related buffer memmove operations with it.

---
 ui/vnc.c |   13 +++++++++----
 ui/vnc.h |    1 +
 2 files changed, 10 insertions(+), 4 deletions(-)
ui/vnc.c +9 -4
index ba30362..87bdd5f 100644
@@ -510,6 +510,13 @@ void buffer_append(Buffer *buffer, const void *data, size_t len)
     buffer->offset += len;
 }
 
+void buffer_advance(Buffer *buf, size_t len)
+{
+    memmove(buf->buffer, buf->buffer + len,
+            (buf->offset - len));
+    buf->offset -= len;
+}
+
 static void vnc_desktop_resize(VncState *vs)
 {
     DisplayState *ds = vs->ds;
@@ -1166,8 +1173,7 @@ static long vnc_client_write_plain(VncState *vs)
     if (!ret)
         return 0;
 
-    memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
-    vs->output.offset -= ret;
+    buffer_advance(&vs->output, ret);
 
     if (vs->output.offset == 0) {
         qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
@@ -1313,8 +1319,7 @@ void vnc_client_read(void *opaque)
         }
 
         if (!ret) {
-            memmove(vs->input.buffer, vs->input.buffer + len, (vs->input.offset - len));
-            vs->input.offset -= len;
+            buffer_advance(&vs->input, len);
         } else {
             vs->read_handler_expect = ret;
         }
ui/vnc.h +1 -0
index 6141e88..ea82875 100644
@@ -510,6 +510,7 @@ void buffer_reserve(Buffer *buffer, size_t len);
 void buffer_reset(Buffer *buffer);
 void buffer_free(Buffer *buffer);
 void buffer_append(Buffer *buffer, const void *data, size_t len);
+void buffer_advance(Buffer *buf, size_t len);
 
 
 /* Misc helpers */
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v4 2/3 vnc: added initial websocket protocol support

In reply to Tim Hardeck

This patch adds basic Websocket Protocol version 13 - RFC 6455 - support to QEMU VNC. Binary encoding support on the client side is mandatory.

Because of the GnuTLS requirement the Websockets implementation is optional (--enable-vnc-ws).

To activate Websocket support the VNC option "websocket"is used, for example "-vnc :0,websocket".
The listen port for Websocket connections is (5700 + display) so if QEMU VNC is started with :0 the Websocket port would be 5700. As an alternative the Websocket port could be manually specified by using ",websocket=<port>" instead.

Parts of the implementation base on Anthony Liguori's QEMU Websocket patch from 2010 and on Joel Martin's LibVNC Websocket implementation.

---
 configure        |   27 +++++-
 qemu-options.hx  |    8 ++
 ui/Makefile.objs |    1 +
 ui/vnc-ws.c      |  282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui/vnc-ws.h      |   92 ++++++++++++++++++
 ui/vnc.c         |  198 +++++++++++++++++++++++++++++++++-----
 ui/vnc.h         |   19 ++++
 7 files changed, 601 insertions(+), 26 deletions(-)
 create mode 100644 ui/vnc-ws.c
 create mode 100644 ui/vnc-ws.h
configure +24 -3
index 994f731..ca519c6 100755
@@ -158,6 +158,7 @@ vnc_tls=""
 vnc_sasl=""
 vnc_jpeg=""
 vnc_png=""
+vnc_ws=""
 xen=""
 xen_ctrl_version=""
 xen_pci_passthrough=""
@@ -703,6 +704,10 @@ for opt do
   ;;
   --enable-vnc-png) vnc_png="yes"
   ;;
+  --disable-vnc-ws) vnc_ws="no"
+  ;;
+  --enable-vnc-ws) vnc_ws="yes"
+  ;;
   --disable-slirp) slirp="no"
   ;;
   --disable-uuid) uuid="no"
@@ -1048,6 +1053,8 @@ echo "  --disable-vnc-jpeg       disable JPEG lossy compression for VNC server"
 echo "  --enable-vnc-jpeg        enable JPEG lossy compression for VNC server"
 echo "  --disable-vnc-png        disable PNG compression for VNC server (default)"
 echo "  --enable-vnc-png         enable PNG compression for VNC server"
+echo "  --disable-vnc-ws         disable Websockets support for VNC server"
+echo "  --enable-vnc-ws          enable Websockets support for VNC server"
 echo "  --disable-curses         disable curses output"
 echo "  --enable-curses          enable curses output"
 echo "  --disable-curl           disable curl connectivity"
@@ -1692,8 +1699,8 @@ EOF
 fi
 
 ##########################################
-# VNC TLS detection
-if test "$vnc" = "yes" -a "$vnc_tls" != "no" ; then
+# VNC TLS/WS detection
+if test "$vnc" = "yes" -a \( "$vnc_tls" != "no" -o "$vnc_ws" != "no" \) ; then
   cat > $TMPC <<EOF
 #include <gnutls/gnutls.h>
 int main(void) { gnutls_session_t s; gnutls_init(&s, GNUTLS_SERVER); return 0; }
@@ -1701,13 +1708,22 @@ EOF
   vnc_tls_cflags=`$pkg_config --cflags gnutls 2> /dev/null`
   vnc_tls_libs=`$pkg_config --libs gnutls 2> /dev/null`
   if compile_prog "$vnc_tls_cflags" "$vnc_tls_libs" ; then
-    vnc_tls=yes
+    if test "$vnc_tls" != "no" ; then
+      vnc_tls=yes
+    fi
+    if test "$vnc_ws" != "no" ; then
+      vnc_ws=yes
+    fi
     libs_softmmu="$vnc_tls_libs $libs_softmmu"
   else
     if test "$vnc_tls" = "yes" ; then
       feature_not_found "vnc-tls"
     fi
+    if test "$vnc_ws" = "yes" ; then
+      feature_not_found "vnc-ws"
+    fi
     vnc_tls=no
+    vnc_ws=no
   fi
 fi
 
@@ -3209,6 +3225,7 @@ if test "$vnc" = "yes" ; then
     echo "VNC SASL support  $vnc_sasl"
     echo "VNC JPEG support  $vnc_jpeg"
     echo "VNC PNG support   $vnc_png"
+    echo "VNC WS support    $vnc_ws"
 fi
 if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
@@ -3385,6 +3402,10 @@ if test "$vnc_png" = "yes" ; then
   echo "CONFIG_VNC_PNG=y" >> $config_host_mak
   echo "VNC_PNG_CFLAGS=$vnc_png_cflags" >> $config_host_mak
 fi
+if test "$vnc_ws" = "yes" ; then
+  echo "CONFIG_VNC_WS=y" >> $config_host_mak
+  echo "VNC_WS_CFLAGS=$vnc_ws_cflags" >> $config_host_mak
+fi
 if test "$fnmatch" = "yes" ; then
   echo "CONFIG_FNMATCH=y" >> $config_host_mak
 fi
qemu-options.hx +8 -0
index de43b1b..d7c4d81 100644
@@ -1096,6 +1096,14 @@ client is specified by the @var{display}. For reverse network
 connections (@var{host}:@var{d},@code{reverse}), the @var{d} argument
 is a TCP port number, not a display number.
 
+@item websocket
+
+Opens an additional TCP listening port dedicated to VNC Websocket connections.
+By defintion the Websocket port is 5700+@var{display}. If @var{host} is
+specified connections will only be allowed from this host.
+As an alternative the Websocket port could be specified by using
+@code{websocket}=@var{port}.
+
 @item password
 
 Require that password based authentication is used for client connections.
ui/Makefile.objs +1 -0
index adc07be..58e191b 100644
@@ -4,6 +4,7 @@ vnc-obj-y += vnc-enc-tight.o vnc-palette.o
 vnc-obj-y += vnc-enc-zrle.o
 vnc-obj-$(CONFIG_VNC_TLS) += vnc-tls.o vnc-auth-vencrypt.o
 vnc-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o
+vnc-obj-$(CONFIG_VNC_WS) += vnc-ws.o
 vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o
ui/vnc-ws.c new file +282 -0

282 lines not on this page · raw message

ui/vnc-ws.h new file +92 -0

92 lines not on this page · raw message

ui/vnc.c +175 -23

340 lines not on this page · raw message

ui/vnc.h +19 -0
index ea82875..9a7b38c 100644
@@ -99,6 +99,9 @@ typedef struct VncDisplay VncDisplay;
 #ifdef CONFIG_VNC_SASL
 #include "vnc-auth-sasl.h"
 #endif
+#ifdef CONFIG_VNC_WS
+#include "vnc-ws.h"
+#endif
 
 struct VncRectStat
 {
@@ -142,6 +145,11 @@ struct VncDisplay
     QEMUTimer *timer;
     int timer_interval;
     int lsock;
+#ifdef CONFIG_VNC_WS
+    int lwebsock;
+    bool websocket;
+    char *ws_display;
+#endif
     DisplayState *ds;
     kbd_layout_t *kbd_layout;
     int lock_key_sync;
@@ -269,11 +277,19 @@ struct VncState
 #ifdef CONFIG_VNC_SASL
     VncStateSASL sasl;
 #endif
+#ifdef CONFIG_VNC_WS
+    bool encode_ws;
+    bool websocket;
+#endif
 
     QObject *info;
 
     Buffer output;
     Buffer input;
+#ifdef CONFIG_VNC_WS
+    Buffer ws_input;
+    Buffer ws_output;
+#endif
     /* current output mode information */
     VncWritePixels *write_pixels;
     PixelFormat client_pf;
@@ -493,6 +509,8 @@ void vnc_write_u16(VncState *vs, uint16_t value);
 void vnc_write_u8(VncState *vs, uint8_t value);
 void vnc_flush(VncState *vs);
 void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting);
+void vnc_disconnect_finish(VncState *vs);
+void vnc_init_state(VncState *vs);
 
 
 /* Buffer I/O functions */
@@ -511,6 +529,7 @@ void buffer_reset(Buffer *buffer);
 void buffer_free(Buffer *buffer);
 void buffer_append(Buffer *buffer, const void *data, size_t len);
 void buffer_advance(Buffer *buf, size_t len);
+uint8_t *buffer_end(Buffer *buffer);
 
 
 /* Misc helpers */
Signature · 1 line
1.7.10.4
#3 lore raw

On Fri, Dec 07, 2012 at 03:56:34PM +0100, Tim Hardeck wrote:

Thanks for addressing my review comments.

Quoted 7 lines
ui/vnc.c +1 -1
@@ -1328,13 +1358,14 @@ void vnc_client_read(void *opaque)
 
 void vnc_write(VncState *vs, const void *data, size_t len)
 {
-    buffer_reserve(&vs->output, len);
+        buffer_reserve(&vs->output, len);
 

ui/vnc.c void vnc_client_read(void *opaque) · line 1368

Quoted 10 lines
ui/vnc.c +5 -4
-    if (vs->csock != -1 && buffer_empty(&vs->output)) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
-    }
+        if (vs->csock != -1 && buffer_empty(&vs->output)) {
+            qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
+                    vnc_client_write, vs);
+        }
 
-    buffer_append(&vs->output, data, len);
+        buffer_append(&vs->output, data, len);

QEMU uses 4 spaces for indentation.

Stefan

#4 lore raw
Quoted 19 lines · Stefan Hajnoczi

On Fri, Dec 07, 2012 at 03:56:34PM +0100, Tim Hardeck wrote:

Thanks for addressing my review comments.

Quoted 15 lines
ui/vnc.c +5 -4
@@ -1328,13 +1358,14 @@ void vnc_client_read(void *opaque)
 
 void vnc_write(VncState *vs, const void *data, size_t len)
 {
-    buffer_reserve(&vs->output, len);
+        buffer_reserve(&vs->output, len);
 
-    if (vs->csock != -1 && buffer_empty(&vs->output)) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
-    }
+        if (vs->csock != -1 && buffer_empty(&vs->output)) {
+            qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
+                    vnc_client_write, vs);
+        }
 
Quoted 4 lines

ui/vnc.c void vnc_client_read(void *opaque) · line 1368

Quoted 2 lines
ui/vnc.c +1 -1
-    buffer_append(&vs->output, data, len);
+        buffer_append(&vs->output, data, len);

QEMU uses 4 spaces for indentation.

I don't know how I missed that, probably during the removal of the previous parts, but scripts/checkpatch.pl didn't complain either.

Is the rest of the patchset fine?

Regards
Tim

Signature · 5 lines
SUSE LINUX Products GmbH, GF: Jeff Hawn, Jennifer Guild, Felix
Imendörffer, HRB 16746 (AG Nürnberg)
Maxfeldstr. 5, 90409 Nürnberg, Germany
T: +49 (0) 911 74053-0  F: +49 (0) 911 74053-483
http://www.suse.de/
Reviewed-by
#5 lore raw
Quoted 20 lines · Tim Hardeck
Quoted 19 lines · Stefan Hajnoczi

On Fri, Dec 07, 2012 at 03:56:34PM +0100, Tim Hardeck wrote:

Thanks for addressing my review comments.

Quoted 15 lines
ui/vnc.c +5 -4
@@ -1328,13 +1358,14 @@ void vnc_client_read(void *opaque)
 
 void vnc_write(VncState *vs, const void *data, size_t len)
 {
-    buffer_reserve(&vs->output, len);
+        buffer_reserve(&vs->output, len);
 
-    if (vs->csock != -1 && buffer_empty(&vs->output)) {
-        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
-    }
+        if (vs->csock != -1 && buffer_empty(&vs->output)) {
+            qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read,
+                    vnc_client_write, vs);
+        }
 
Quoted 8 lines
Quoted 4 lines

ui/vnc.c void vnc_client_read(void *opaque) · line 1368

Quoted 2 lines
ui/vnc.c +1 -1
-    buffer_append(&vs->output, data, len);
+        buffer_append(&vs->output, data, len);

QEMU uses 4 spaces for indentation.

I don't know how I missed that, probably during the removal of the previous parts, but scripts/checkpatch.pl didn't complain either.

Is the rest of the patchset fine?

A VNC expert needs to review it. I only looked at coding style, logic bugs, etc but didn't review VNC server or WebSocket protocol specifics.

Corentin or Anthony?

#6 lore raw

PATCH v4 3/3 vnc: fix possible uninitialized removals

In reply to Tim Hardeck

Some VncState values are not initialized before the Websocket handshake. If it fails QEMU segfaults during the cleanup. To prevent this behavior intialization checks are added.

---
 ui/vnc.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
ui/vnc.c +9 -3
index a5c16e0..3af1840 100644
@@ -1053,20 +1053,26 @@ void vnc_disconnect_finish(VncState *vs)
     audio_del(vs);
     vnc_release_modifiers(vs);
 
-    QTAILQ_REMOVE(&vs->vd->clients, vs, next);
+    if (!QTAILQ_EMPTY(&vs->vd->clients)) {
+        QTAILQ_REMOVE(&vs->vd->clients, vs, next);
+    }
 
     if (QTAILQ_EMPTY(&vs->vd->clients)) {
         dcl->idle = 1;
     }
 
-    qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
+    if (vs->mouse_mode_notifier.notify != NULL) {
+        qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
+    }
     vnc_remove_timer(vs->vd);
     if (vs->vd->lock_key_sync)
         qemu_remove_led_event_handler(vs->led);
     vnc_unlock_output(vs);
 
     qemu_mutex_destroy(&vs->output_mutex);
-    qemu_bh_delete(vs->bh);
+    if (vs->bh != NULL) {
+        qemu_bh_delete(vs->bh);
+    }
     buffer_free(&vs->jobs_buffer);
 
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
Signature · 1 line
1.7.10.4