]> git.proxmox.com Git - mirror_qemu.git/blame - ui/vnc-ws.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / ui / vnc-ws.c
CommitLineData
7536ee4b
TH
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
e16f4c87 21#include "qemu/osdep.h"
da34e65c 22#include "qapi/error.h"
7536ee4b 23#include "vnc.h"
d5f04223 24#include "io/channel-websock.h"
58369e22 25#include "qemu/bswap.h"
ad6374c4 26#include "trace.h"
0057a0d5 27
60e705c5 28static void vncws_tls_handshake_done(QIOTask *task,
2cc45228
DB
29 gpointer user_data)
30{
31 VncState *vs = user_data;
60e705c5 32 Error *err = NULL;
4a48aaa9 33
60e705c5 34 if (qio_task_propagate_error(task, &err)) {
2cc45228
DB
35 VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err));
36 vnc_client_error(vs);
60e705c5 37 error_free(err);
2cc45228 38 } else {
d5f04223 39 VNC_DEBUG("TLS handshake complete, starting websocket handshake\n");
a75d6f07
BC
40 if (vs->ioc_tag) {
41 g_source_remove(vs->ioc_tag);
42 }
7d5b0d68
PMD
43 vs->ioc_tag = qio_channel_add_watch(vs->ioc,
44 G_IO_IN | G_IO_HUP | G_IO_ERR,
45 vncws_handshake_io, vs, NULL);
3e305e4a 46 }
0057a0d5
TH
47}
48
2cc45228 49
04d2529d 50gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
2ddafce7 51 GIOCondition condition,
04d2529d 52 void *opaque)
0057a0d5 53{
2cc45228
DB
54 VncState *vs = opaque;
55 QIOChannelTLS *tls;
3e305e4a 56 Error *err = NULL;
0057a0d5 57
2cc45228
DB
58 if (vs->ioc_tag) {
59 g_source_remove(vs->ioc_tag);
60 vs->ioc_tag = 0;
61 }
62
2ddafce7
DH
63 if (condition & (G_IO_HUP | G_IO_ERR)) {
64 vnc_client_error(vs);
65 return TRUE;
66 }
67
2cc45228
DB
68 tls = qio_channel_tls_new_server(
69 vs->ioc,
70 vs->vd->tlscreds,
b76806d4 71 vs->vd->tlsauthzid,
2cc45228
DB
72 &err);
73 if (!tls) {
74 VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
3e305e4a
DB
75 error_free(err);
76 vnc_client_error(vs);
04d2529d 77 return TRUE;
0057a0d5 78 }
3e305e4a 79
10bcfe58
DB
80 qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls");
81
2cc45228
DB
82 object_unref(OBJECT(vs->ioc));
83 vs->ioc = QIO_CHANNEL(tls);
ad6374c4 84 trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
2cc45228
DB
85 vs->tls = qio_channel_tls_get_session(tls);
86
87 qio_channel_tls_handshake(tls,
88 vncws_tls_handshake_done,
89 vs,
1939ccda 90 NULL,
2cc45228
DB
91 NULL);
92
04d2529d 93 return TRUE;
0057a0d5 94}
0057a0d5 95
7536ee4b 96
60e705c5 97static void vncws_handshake_done(QIOTask *task,
d5f04223
DB
98 gpointer user_data)
99{
100 VncState *vs = user_data;
60e705c5 101 Error *err = NULL;
7536ee4b 102
60e705c5 103 if (qio_task_propagate_error(task, &err)) {
d5f04223
DB
104 VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err));
105 vnc_client_error(vs);
60e705c5 106 error_free(err);
d5f04223
DB
107 } else {
108 VNC_DEBUG("Websock handshake complete, starting VNC protocol\n");
dbee9897 109 vnc_start_protocol(vs);
a75d6f07
BC
110 if (vs->ioc_tag) {
111 g_source_remove(vs->ioc_tag);
112 }
04d2529d 113 vs->ioc_tag = qio_channel_add_watch(
2ddafce7
DH
114 vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR,
115 vnc_client_io, vs, NULL);
7536ee4b
TH
116 }
117}
118
119
04d2529d 120gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED,
2ddafce7 121 GIOCondition condition,
04d2529d
DB
122 void *opaque)
123{
124 VncState *vs = opaque;
d5f04223 125 QIOChannelWebsock *wioc;
7536ee4b 126
d5f04223
DB
127 if (vs->ioc_tag) {
128 g_source_remove(vs->ioc_tag);
129 vs->ioc_tag = 0;
7536ee4b
TH
130 }
131
2ddafce7
DH
132 if (condition & (G_IO_HUP | G_IO_ERR)) {
133 vnc_client_error(vs);
134 return TRUE;
135 }
136
d5f04223 137 wioc = qio_channel_websock_new_server(vs->ioc);
10bcfe58 138 qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock");
7536ee4b 139
d5f04223
DB
140 object_unref(OBJECT(vs->ioc));
141 vs->ioc = QIO_CHANNEL(wioc);
ad6374c4 142 trace_vnc_client_io_wrap(vs, vs->ioc, "websock");
7536ee4b 143
d5f04223
DB
144 qio_channel_websock_handshake(wioc,
145 vncws_handshake_done,
146 vs,
147 NULL);
7536ee4b 148
d5f04223 149 return TRUE;
7536ee4b 150}