Skip to contentSkip to conversation

vnc: added initial websockets support

PATCH In review Tim Hardeck 2 participants 4 messages Nov 20, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vnc: added initial websockets support

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

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

To activate Websockets during runtime the VNC option "websocket" is used, for example "-vnc :0,websocket" would activate Websockets. The port for Websockets connections is (5700 + display) so if QEMU VNC is started with :0 the websockets port would be 5700.

SHA1 is required for the handshake which is generated by GnuTLS. Since using GnuTLS does automatically activate VNC-TLS, which has warnings about deprecated parts, I have changed the configure script to disable VNC-TLS if not explicitly enabled.

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

---

Changes to v1
* 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

Would it be Ok to use a public domain SHA1 implementation like tests/tcg/sha1.c and if so where should the sha1.c be stored? Without the GnuTLS dependency would it be Ok to make Websockets not optional because this would clean up the patch/code quite a bit?

QEMU does segfault if a regular VNC client connects to the Websocket port and then disconnects because of several unitialized lists since vnc_init_state wasn't run before.
The segfault could be fixed by applying my previously sent patches "[PATCH 0/2] fix segfaults triggered by failed vnc handshakes".

I have used parts of the LibVNC websockets implementation that's why I have added the GPL header to the new files.

---
 configure        |   34 ++++++++-
 qemu-options.hx  |    6 ++
 ui/Makefile.objs |    1 +
 ui/vnc-ws.c      |  211 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui/vnc-ws.h      |  101 ++++++++++++++++++++++++++
 ui/vnc.c         |  185 +++++++++++++++++++++++++++++++++++++++++------
 ui/vnc.h         |   20 ++++++
 7 files changed, 537 insertions(+), 21 deletions(-)
 create mode 100644 ui/vnc-ws.c
 create mode 100644 ui/vnc-ws.h
configure +33 -1
index 780b19a..ac9da73 100755
@@ -154,10 +154,11 @@ vnc="yes"
 sparse="no"
 uuid=""
 vde=""
-vnc_tls=""
+vnc_tls="no"
 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"
@@ -1772,6 +1779,26 @@ EOF
 fi
 
 ##########################################
+# VNC WS detection
+if test "$vnc" = "yes" -a "$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; }
+EOF
+  vnc_ws_cflags=`$pkg_config --cflags gnutls 2> /dev/null`
+  vnc_ws_libs=`$pkg_config --libs gnutls 2> /dev/null`
+  if compile_prog "$vnc_ws_cflags" "$vnc_ws_libs" ; then
+    vnc_ws=yes
+    libs_softmmu="$vnc_ws_libs $libs_softmmu"
+  else
+    if test "$vnc_ws" = "yes" ; then
+      feature_not_found "vnc-ws"
+    fi
+    vnc_ws=no
+  fi
+fi
+
+##########################################
 # fnmatch() probe, used for ACL routines
 fnmatch="no"
 cat > $TMPC << EOF
@@ -3194,6 +3221,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"
@@ -3370,6 +3398,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 +6 -0
index 9bb29d3..3b3fff8 100644
@@ -1096,6 +1096,12 @@ 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.
+
 @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 +211 -0

211 lines not on this page · raw message

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

101 lines not on this page · raw message

ui/vnc.c +165 -20

325 lines not on this page · raw message

ui/vnc.h +20 -0
index 6141e88..22d57f0 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,10 @@ struct VncDisplay
     QEMUTimer *timer;
     int timer_interval;
     int lsock;
+#ifdef CONFIG_VNC_WS
+    int lwebsock;
+    int websocket;
+#endif
     DisplayState *ds;
     kbd_layout_t *kbd_layout;
     int lock_key_sync;
@@ -269,11 +276,18 @@ struct VncState
 #ifdef CONFIG_VNC_SASL
     VncStateSASL sasl;
 #endif
+#ifdef CONFIG_VNC_WS
+    int encode_ws;
+    int websocket;
+#endif
 
     QObject *info;
 
     Buffer output;
     Buffer input;
+#ifdef CONFIG_VNC_WS
+    Buffer ws_input;
+#endif
     /* current output mode information */
     VncWritePixels *write_pixels;
     PixelFormat client_pf;
@@ -505,11 +519,17 @@ int vnc_client_io_error(VncState *vs, int ret, int last_errno);
 void start_client_init(VncState *vs);
 void start_auth_vnc(VncState *vs);
 
+#ifdef CONFIG_VNC_WS
+void vncws_handshake_read(void *opaque);
+void vnc_init_state(VncState *vs);
+#endif
+
 /* Buffer management */
 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
#1 lore raw
Quoted 10 lines · Tim Hardeck

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

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

To activate Websockets during runtime the VNC option "websocket" is used, for example "-vnc :0,websocket" would activate Websockets. The port for Websockets connections is (5700 + display) so if QEMU VNC is started with :0 the websockets port would be 5700.

We need to be able to specify an explicit port number for the websockets listen address, separately from the main VNC port number. This automatic pick of websockets port might be nice for a user starting QEMU manually, but for management apps we need full direct control.

Quoted 11 lines

Changes to v1
* 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

Would it be Ok to use a public domain SHA1 implementation like tests/tcg/sha1.c and if so where should the sha1.c be stored? Without the GnuTLS dependency would it be Ok to make Websockets not optional because this would clean up the patch/code quite a bit?

IMHO using gcrypt/GNUTLS is a good thing. Creating our own copies of encryption algorithms in source tree causes significant complications getting QEMU security certified. GNUTLS is a common enough crypto library that I don't think it is unreasonably onerous to expect it to be used for WebSockets. It even works fine on Windows.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#2 lore raw
Quoted 12 lines · Daniel P. Berrange
Quoted 10 lines · Tim Hardeck

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

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

To activate Websockets during runtime the VNC option "websocket" is used, for example "-vnc :0,websocket" would activate Websockets. The port for Websockets connections is (5700 + display) so if QEMU VNC is started with :0 the websockets port would be 5700.

Quoted 4 lines

We need to be able to specify an explicit port number for the websockets listen address, separately from the main VNC port number. This automatic pick of websockets port might be nice for a user starting QEMU manually, but for management apps we need full direct control.

Ok, this should be no problem to add something like websocket=<port> but I just thought that a correlation between the vnc and websocket port would be quite useful especially when several QEMU instances are run on one machine.
Would it be Ok to keep the correlation if no port is specified?

I am going to add the websocket port option after some more feedback.

Quoted 12 lines
Quoted 11 lines

Changes to v1
* 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

Would it be Ok to use a public domain SHA1 implementation like tests/tcg/sha1.c and if so where should the sha1.c be stored? Without the GnuTLS dependency would it be Ok to make Websockets not optional because this would clean up the patch/code quite a bit?

Quoted 5 lines

IMHO using gcrypt/GNUTLS is a good thing. Creating our own copies of encryption algorithms in source tree causes significant complications getting QEMU security certified. GNUTLS is a common enough crypto library that I don't think it is unreasonably onerous to expect it to be used for WebSockets. It even works fine on Windows.

That's one of the reasons why I have used GnuTLS but it is quite a huge dependency for just one algorithm and the many ifdefs don't really help the code quality.

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/
#3 lore raw
Quoted 17 lines · Tim Hardeck
Quoted 12 lines · Daniel P. Berrange
Quoted 10 lines · Tim Hardeck

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

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

To activate Websockets during runtime the VNC option "websocket" is used, for example "-vnc :0,websocket" would activate Websockets. The port for Websockets connections is (5700 + display) so if QEMU VNC is started with :0 the websockets port would be 5700.

Quoted 4 lines

We need to be able to specify an explicit port number for the websockets listen address, separately from the main VNC port number. This automatic pick of websockets port might be nice for a user starting QEMU manually, but for management apps we need full direct control.

Quoted 5 lines

Ok, this should be no problem to add something like websocket=<port> but I just thought that a correlation between the vnc and websocket port would be quite useful especially when several QEMU instances are run on one machine.
Would it be Ok to keep the correlation if no port is specified?

Yep, like I said, its useful for users starting QEMU directly. We just need to make sure there's an override.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Changes to v1
* 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

Would it be Ok to use a public domain SHA1 implementation like tests/tcg/sha1.c and if so where should the sha1.c be stored? Without the GnuTLS dependency would it be Ok to make Websockets not optional because this would clean up the patch/code quite a bit?

Quoted 8 lines
Quoted 5 lines

IMHO using gcrypt/GNUTLS is a good thing. Creating our own copies of encryption algorithms in source tree causes significant complications getting QEMU security certified. GNUTLS is a common enough crypto library that I don't think it is unreasonably onerous to expect it to be used for WebSockets. It even works fine on Windows.

That's one of the reasons why I have used GnuTLS but it is quite a huge dependency for just one algorithm and the many ifdefs don't really help the code quality.

One of the things I'd like to try todo for QEMU is to have an centralized internal interface for crypto functions. eg, so you could say

    h = crypt_hash_init("sha1");

    ...

and the actual impl of crypt_hash_init() whould then be backed by GNUTLS, OpenSSL, or the Linux kernel (via socket AF_ALG family). This would let all the #ifdef code be centralized in one place, making the VNC code clean again. So while I agree with you that its ugly, I think that's OK in the short term

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|