xref: /openbmc/qemu/ui/vnc-ws.c (revision 9c4218e9)
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 "vnc.h"
22 #include "io/channel-websock.h"
23 
24 static void vncws_tls_handshake_done(Object *source,
25                                      Error *err,
26                                      gpointer user_data)
27 {
28     VncState *vs = user_data;
29 
30     if (err) {
31         VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err));
32         vnc_client_error(vs);
33     } else {
34         VNC_DEBUG("TLS handshake complete, starting websocket handshake\n");
35         vs->ioc_tag = qio_channel_add_watch(
36             QIO_CHANNEL(vs->ioc), G_IO_IN, vncws_handshake_io, vs, NULL);
37     }
38 }
39 
40 
41 gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
42                                 GIOCondition condition G_GNUC_UNUSED,
43                                 void *opaque)
44 {
45     VncState *vs = opaque;
46     QIOChannelTLS *tls;
47     Error *err = NULL;
48 
49     VNC_DEBUG("TLS Websocket connection required\n");
50     if (vs->ioc_tag) {
51         g_source_remove(vs->ioc_tag);
52         vs->ioc_tag = 0;
53     }
54 
55     tls = qio_channel_tls_new_server(
56         vs->ioc,
57         vs->vd->tlscreds,
58         vs->vd->tlsaclname,
59         &err);
60     if (!tls) {
61         VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
62         error_free(err);
63         vnc_client_error(vs);
64         return TRUE;
65     }
66 
67     VNC_DEBUG("Start TLS WS handshake process\n");
68     object_unref(OBJECT(vs->ioc));
69     vs->ioc = QIO_CHANNEL(tls);
70     vs->tls = qio_channel_tls_get_session(tls);
71 
72     qio_channel_tls_handshake(tls,
73                               vncws_tls_handshake_done,
74                               vs,
75                               NULL);
76 
77     return TRUE;
78 }
79 
80 
81 static void vncws_handshake_done(Object *source,
82                                  Error *err,
83                                  gpointer user_data)
84 {
85     VncState *vs = user_data;
86 
87     if (err) {
88         VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err));
89         vnc_client_error(vs);
90     } else {
91         VNC_DEBUG("Websock handshake complete, starting VNC protocol\n");
92         vnc_init_state(vs);
93         vs->ioc_tag = qio_channel_add_watch(
94             vs->ioc, G_IO_IN, vnc_client_io, vs, NULL);
95     }
96 }
97 
98 
99 gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
100                             GIOCondition condition G_GNUC_UNUSED,
101                             void *opaque)
102 {
103     VncState *vs = opaque;
104     QIOChannelWebsock *wioc;
105 
106     VNC_DEBUG("Websocket negotiate starting\n");
107     if (vs->ioc_tag) {
108         g_source_remove(vs->ioc_tag);
109         vs->ioc_tag = 0;
110     }
111 
112     wioc = qio_channel_websock_new_server(vs->ioc);
113 
114     object_unref(OBJECT(vs->ioc));
115     vs->ioc = QIO_CHANNEL(wioc);
116 
117     qio_channel_websock_handshake(wioc,
118                                   vncws_handshake_done,
119                                   vs,
120                                   NULL);
121 
122     return TRUE;
123 }
124