| 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 | #include "trace.h" |
| 27 | |
| 28 | static void vncws_tls_handshake_done(QIOTask *task, |
| 29 | gpointer user_data) |
| 30 | { |
| 31 | VncState *vs = user_data; |
| 32 | Error *err = NULL; |
| 33 | |
| 34 | if (qio_task_propagate_error(task, &err)) { |
| 35 | trace_vnc_ws_tls_handshake_fail(vs, error_get_pretty(err)); |
| 36 | vnc_client_error(vs); |
| 37 | error_free(err); |
| 38 | } else { |
| 39 | trace_vnc_ws_tls_handshake_complete(vs); |
| 40 | if (vs->ioc_tag) { |
| 41 | g_source_remove(vs->ioc_tag); |
| 42 | } |
| 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); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, |
| 51 | GIOCondition condition, |
| 52 | void *opaque) |
| 53 | { |
| 54 | VncState *vs = opaque; |
| 55 | QIOChannelTLS *tls; |
| 56 | Error *err = NULL; |
| 57 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
| 58 | |
| 59 | if (condition & (G_IO_HUP | G_IO_ERR)) { |
| 60 | vnc_client_error(vs); |
| 61 | return TRUE; |
| 62 | } |
| 63 | |
| 64 | tls = qio_channel_tls_new_server( |
| 65 | vs->ioc, |
| 66 | vs->vd->tlscreds, |
| 67 | vs->vd->tlsauthzid, |
| 68 | &err); |
| 69 | if (!tls) { |
| 70 | trace_vnc_ws_tls_setup_fail(vs, error_get_pretty(err)); |
| 71 | error_free(err); |
| 72 | vnc_client_error(vs); |
| 73 | return TRUE; |
| 74 | } |
| 75 | |
| 76 | qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls"); |
| 77 | |
| 78 | object_unref(OBJECT(vs->ioc)); |
| 79 | vs->ioc = QIO_CHANNEL(tls); |
| 80 | trace_vnc_client_io_wrap(vs, vs->ioc, "tls"); |
| 81 | vs->tls = qio_channel_tls_get_session(tls); |
| 82 | |
| 83 | qio_channel_tls_handshake(tls, |
| 84 | vncws_tls_handshake_done, |
| 85 | vs, |
| 86 | NULL, |
| 87 | NULL); |
| 88 | |
| 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void vncws_handshake_done(QIOTask *task, |
| 94 | gpointer user_data) |
| 95 | { |
| 96 | VncState *vs = user_data; |
| 97 | Error *err = NULL; |
| 98 | |
| 99 | if (qio_task_propagate_error(task, &err)) { |
| 100 | trace_vnc_ws_handshake_fail(vs, error_get_pretty(err)); |
| 101 | vnc_client_error(vs); |
| 102 | error_free(err); |
| 103 | } else { |
| 104 | trace_vnc_ws_handshake_complete(vs); |
| 105 | vnc_start_protocol(vs); |
| 106 | if (vs->ioc_tag) { |
| 107 | g_source_remove(vs->ioc_tag); |
| 108 | } |
| 109 | vs->ioc_tag = qio_channel_add_watch( |
| 110 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 111 | vnc_client_io, vs, NULL); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, |
| 117 | GIOCondition condition, |
| 118 | void *opaque) |
| 119 | { |
| 120 | VncState *vs = opaque; |
| 121 | QIOChannelWebsock *wioc; |
| 122 | g_clear_handle_id(&vs->ioc_tag, g_source_remove); |
| 123 | |
| 124 | if (condition & (G_IO_HUP | G_IO_ERR)) { |
| 125 | vnc_client_error(vs); |
| 126 | return TRUE; |
| 127 | } |
| 128 | |
| 129 | wioc = qio_channel_websock_new_server(vs->ioc); |
| 130 | qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock"); |
| 131 | |
| 132 | object_unref(OBJECT(vs->ioc)); |
| 133 | vs->ioc = QIO_CHANNEL(wioc); |
| 134 | trace_vnc_client_io_wrap(vs, vs->ioc, "websock"); |
| 135 | |
| 136 | qio_channel_websock_handshake(wioc, |
| 137 | vncws_handshake_done, |
| 138 | vs, |
| 139 | NULL); |
| 140 | |
| 141 | return TRUE; |
| 142 | } |