xref: /qemu/ui/vnc-ws.c (revision 33848cee)
1 /*
2  * QEMU VNC display driver: Websockets support
3  *
4  * Copyright (C) 2010 Joel Martin
5  * Copyright (C) 2012 Tim Hardeck
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this software; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "vnc.h"
24 #include "io/channel-websock.h"
25 #include "qemu/bswap.h"
26 
27 static void vncws_tls_handshake_done(Object *source,
28                                      Error *err,
29                                      gpointer user_data)
30 {
31     VncState *vs = user_data;
32 
33     if (err) {
34         VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err));
35         vnc_client_error(vs);
36     } else {
37         VNC_DEBUG("TLS handshake complete, starting websocket handshake\n");
38         vs->ioc_tag = qio_channel_add_watch(
39             QIO_CHANNEL(vs->ioc), G_IO_IN, vncws_handshake_io, vs, NULL);
40     }
41 }
42 
43 
44 gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
45                                 GIOCondition condition G_GNUC_UNUSED,
46                                 void *opaque)
47 {
48     VncState *vs = opaque;
49     QIOChannelTLS *tls;
50     Error *err = NULL;
51 
52     VNC_DEBUG("TLS Websocket connection required\n");
53     if (vs->ioc_tag) {
54         g_source_remove(vs->ioc_tag);
55         vs->ioc_tag = 0;
56     }
57 
58     tls = qio_channel_tls_new_server(
59         vs->ioc,
60         vs->vd->tlscreds,
61         vs->vd->tlsaclname,
62         &err);
63     if (!tls) {
64         VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
65         error_free(err);
66         vnc_client_error(vs);
67         return TRUE;
68     }
69 
70     qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls");
71 
72     VNC_DEBUG("Start TLS WS handshake process\n");
73     object_unref(OBJECT(vs->ioc));
74     vs->ioc = QIO_CHANNEL(tls);
75     vs->tls = qio_channel_tls_get_session(tls);
76 
77     qio_channel_tls_handshake(tls,
78                               vncws_tls_handshake_done,
79                               vs,
80                               NULL);
81 
82     return TRUE;
83 }
84 
85 
86 static void vncws_handshake_done(Object *source,
87                                  Error *err,
88                                  gpointer user_data)
89 {
90     VncState *vs = user_data;
91 
92     if (err) {
93         VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err));
94         vnc_client_error(vs);
95     } else {
96         VNC_DEBUG("Websock handshake complete, starting VNC protocol\n");
97         vnc_start_protocol(vs);
98         vs->ioc_tag = qio_channel_add_watch(
99             vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
100     }
101 }
102 
103 
104 gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
105                             GIOCondition condition G_GNUC_UNUSED,
106                             void *opaque)
107 {
108     VncState *vs = opaque;
109     QIOChannelWebsock *wioc;
110 
111     VNC_DEBUG("Websocket negotiate starting\n");
112     if (vs->ioc_tag) {
113         g_source_remove(vs->ioc_tag);
114         vs->ioc_tag = 0;
115     }
116 
117     wioc = qio_channel_websock_new_server(vs->ioc);
118     qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock");
119 
120     object_unref(OBJECT(vs->ioc));
121     vs->ioc = QIO_CHANNEL(wioc);
122 
123     qio_channel_websock_handshake(wioc,
124                                   vncws_handshake_done,
125                                   vs,
126                                   NULL);
127 
128     return TRUE;
129 }
130