13e230dd2SCorentin Chary /* 23e230dd2SCorentin Chary * QEMU VNC display driver 33e230dd2SCorentin Chary * 43e230dd2SCorentin Chary * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> 53e230dd2SCorentin Chary * Copyright (C) 2006 Fabrice Bellard 63e230dd2SCorentin Chary * Copyright (C) 2009 Red Hat, Inc 73e230dd2SCorentin Chary * 83e230dd2SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy 93e230dd2SCorentin Chary * of this software and associated documentation files (the "Software"), to deal 103e230dd2SCorentin Chary * in the Software without restriction, including without limitation the rights 113e230dd2SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 123e230dd2SCorentin Chary * copies of the Software, and to permit persons to whom the Software is 133e230dd2SCorentin Chary * furnished to do so, subject to the following conditions: 143e230dd2SCorentin Chary * 153e230dd2SCorentin Chary * The above copyright notice and this permission notice shall be included in 163e230dd2SCorentin Chary * all copies or substantial portions of the Software. 173e230dd2SCorentin Chary * 183e230dd2SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193e230dd2SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203e230dd2SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 213e230dd2SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 223e230dd2SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 233e230dd2SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 243e230dd2SCorentin Chary * THE SOFTWARE. 253e230dd2SCorentin Chary */ 263e230dd2SCorentin Chary 27e16f4c87SPeter Maydell #include "qemu/osdep.h" 283e230dd2SCorentin Chary #include "vnc.h" 29bd023f95SCorentin Chary #include "vnc-jobs.h" 3040066175SGerd Hoffmann #include "trace.h" 319c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 32d49b6836SMarkus Armbruster #include "qemu/error-report.h" 331de7afc9SPaolo Bonzini #include "qemu/sockets.h" 341de7afc9SPaolo Bonzini #include "qemu/timer.h" 351de7afc9SPaolo Bonzini #include "qemu/acl.h" 364db14629SGerd Hoffmann #include "qemu/config-file.h" 37cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 387b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 392b54aa87SLuiz Capitulino #include "qmp-commands.h" 408d447d10SGerd Hoffmann #include "ui/input.h" 41fb6ba0d5SWenchao Xia #include "qapi-event.h" 428e9b0d24SDaniel P. Berrange #include "crypto/hash.h" 433e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h" 443e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h" 453e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h" 46f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 4757a6d6d5SDaniel P. Berrange #include "io/dns-resolver.h" 483e230dd2SCorentin Chary 490f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 503e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 510f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 52999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 53999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 543e230dd2SCorentin Chary 553e230dd2SCorentin Chary #include "vnc_keysym.h" 56800567a6SDaniel P. Berrange #include "crypto/cipher.h" 573e230dd2SCorentin Chary 58d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays = 59d616ccc5SGerd Hoffmann QTAILQ_HEAD_INITIALIZER(vnc_displays); 603e230dd2SCorentin Chary 613e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 627bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs); 633e230dd2SCorentin Chary 648cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 658cf36489SGerd Hoffmann { 668cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 678cf36489SGerd Hoffmann static const char *mn[] = { 688cf36489SGerd Hoffmann [0] = "undefined", 698cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 708cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 718cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 728cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 738cf36489SGerd Hoffmann }; 7404d2529dSDaniel P. Berrange fprintf(stderr, "%s/%p: %s -> %s\n", __func__, 7504d2529dSDaniel P. Berrange vs->ioc, mn[vs->share_mode], mn[mode]); 768cf36489SGerd Hoffmann #endif 778cf36489SGerd Hoffmann 78e5f34cddSGerd Hoffmann switch (vs->share_mode) { 79e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 80e5f34cddSGerd Hoffmann vs->vd->num_connecting--; 81e5f34cddSGerd Hoffmann break; 82e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 83e5f34cddSGerd Hoffmann vs->vd->num_shared--; 84e5f34cddSGerd Hoffmann break; 85e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 868cf36489SGerd Hoffmann vs->vd->num_exclusive--; 87e5f34cddSGerd Hoffmann break; 88e5f34cddSGerd Hoffmann default: 89e5f34cddSGerd Hoffmann break; 908cf36489SGerd Hoffmann } 91e5f34cddSGerd Hoffmann 928cf36489SGerd Hoffmann vs->share_mode = mode; 93e5f34cddSGerd Hoffmann 94e5f34cddSGerd Hoffmann switch (vs->share_mode) { 95e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 96e5f34cddSGerd Hoffmann vs->vd->num_connecting++; 97e5f34cddSGerd Hoffmann break; 98e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 99e5f34cddSGerd Hoffmann vs->vd->num_shared++; 100e5f34cddSGerd Hoffmann break; 101e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 1028cf36489SGerd Hoffmann vs->vd->num_exclusive++; 103e5f34cddSGerd Hoffmann break; 104e5f34cddSGerd Hoffmann default: 105e5f34cddSGerd Hoffmann break; 1068cf36489SGerd Hoffmann } 1078cf36489SGerd Hoffmann } 1088cf36489SGerd Hoffmann 1093e230dd2SCorentin Chary 110bd269ebcSMarkus Armbruster static void vnc_init_basic_info(SocketAddress *addr, 11198481bfcSEric Blake VncBasicInfo *info, 11298481bfcSEric Blake Error **errp) 1133e230dd2SCorentin Chary { 11404d2529dSDaniel P. Berrange switch (addr->type) { 115bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 116bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 117bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 118bd269ebcSMarkus Armbruster if (addr->u.inet.ipv6) { 11904d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 12004d2529dSDaniel P. Berrange } else { 12104d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 12204d2529dSDaniel P. Berrange } 12304d2529dSDaniel P. Berrange break; 1243e230dd2SCorentin Chary 125bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 12604d2529dSDaniel P. Berrange info->host = g_strdup(""); 127bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 12804d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 12904d2529dSDaniel P. Berrange break; 13004d2529dSDaniel P. Berrange 131bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 132bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 133a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 134977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 13504d2529dSDaniel P. Berrange break; 136a6c76285SMarkus Armbruster default: 137a6c76285SMarkus Armbruster abort(); 13804d2529dSDaniel P. Berrange } 13904d2529dSDaniel P. Berrange 14098481bfcSEric Blake return; 1413e230dd2SCorentin Chary } 1423e230dd2SCorentin Chary 14304d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, 14404d2529dSDaniel P. Berrange VncBasicInfo *info, 14598481bfcSEric Blake Error **errp) 1463e230dd2SCorentin Chary { 147bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1483e230dd2SCorentin Chary 149624cdd46SDaniel P. Berrange if (!ioc) { 150624cdd46SDaniel P. Berrange error_setg(errp, "No listener socket available"); 151624cdd46SDaniel P. Berrange return; 152624cdd46SDaniel P. Berrange } 153624cdd46SDaniel P. Berrange 15404d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, errp); 15504d2529dSDaniel P. Berrange if (!addr) { 15698481bfcSEric Blake return; 1573e230dd2SCorentin Chary } 1583e230dd2SCorentin Chary 15904d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 160bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1613e230dd2SCorentin Chary } 1623e230dd2SCorentin Chary 16304d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, 16404d2529dSDaniel P. Berrange VncBasicInfo *info, 16598481bfcSEric Blake Error **errp) 1663e230dd2SCorentin Chary { 167bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1683e230dd2SCorentin Chary 16904d2529dSDaniel P. Berrange addr = qio_channel_socket_get_remote_address(ioc, errp); 17004d2529dSDaniel P. Berrange if (!addr) { 17198481bfcSEric Blake return; 1723e230dd2SCorentin Chary } 1733e230dd2SCorentin Chary 17404d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 175bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1763e230dd2SCorentin Chary } 1773e230dd2SCorentin Chary 1783e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1793e230dd2SCorentin Chary switch (vd->auth) { 1803e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1813e230dd2SCorentin Chary return "invalid"; 1823e230dd2SCorentin Chary case VNC_AUTH_NONE: 1833e230dd2SCorentin Chary return "none"; 1843e230dd2SCorentin Chary case VNC_AUTH_VNC: 1853e230dd2SCorentin Chary return "vnc"; 1863e230dd2SCorentin Chary case VNC_AUTH_RA2: 1873e230dd2SCorentin Chary return "ra2"; 1883e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1893e230dd2SCorentin Chary return "ra2ne"; 1903e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1913e230dd2SCorentin Chary return "tight"; 1923e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1933e230dd2SCorentin Chary return "ultra"; 1943e230dd2SCorentin Chary case VNC_AUTH_TLS: 1953e230dd2SCorentin Chary return "tls"; 1963e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 1973e230dd2SCorentin Chary switch (vd->subauth) { 1983e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 1993e230dd2SCorentin Chary return "vencrypt+plain"; 2003e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 2013e230dd2SCorentin Chary return "vencrypt+tls+none"; 2023e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 2033e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 2043e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 2053e230dd2SCorentin Chary return "vencrypt+tls+plain"; 2063e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 2073e230dd2SCorentin Chary return "vencrypt+x509+none"; 2083e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2093e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2103e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2113e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2123e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2133e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2143e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2153e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2163e230dd2SCorentin Chary default: 2173e230dd2SCorentin Chary return "vencrypt"; 2183e230dd2SCorentin Chary } 2193e230dd2SCorentin Chary case VNC_AUTH_SASL: 2203e230dd2SCorentin Chary return "sasl"; 2213e230dd2SCorentin Chary } 2223e230dd2SCorentin Chary return "unknown"; 2233e230dd2SCorentin Chary } 2243e230dd2SCorentin Chary 225d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd) 2263e230dd2SCorentin Chary { 227fb6ba0d5SWenchao Xia VncServerInfo *info; 22898481bfcSEric Blake Error *err = NULL; 2293e230dd2SCorentin Chary 2304ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 2314ee74fa7SDaniel P. Berrange return NULL; 2324ee74fa7SDaniel P. Berrange } 2334ee74fa7SDaniel P. Berrange 2343e7f136dSDaniel P. Berrange info = g_malloc0(sizeof(*info)); 2354ee74fa7SDaniel P. Berrange vnc_init_basic_info_from_server_addr(vd->lsock[0], 236ddf21908SEric Blake qapi_VncServerInfo_base(info), &err); 237fb6ba0d5SWenchao Xia info->has_auth = true; 238d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 23998481bfcSEric Blake if (err) { 24098481bfcSEric Blake qapi_free_VncServerInfo(info); 24198481bfcSEric Blake info = NULL; 24298481bfcSEric Blake error_free(err); 24398481bfcSEric Blake } 244fb6ba0d5SWenchao Xia return info; 2453e230dd2SCorentin Chary } 2463e230dd2SCorentin Chary 2473e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2483e230dd2SCorentin Chary { 2493e230dd2SCorentin Chary if (!client->info) { 2503e230dd2SCorentin Chary return; 2513e230dd2SCorentin Chary } 2523e230dd2SCorentin Chary 2533e305e4aSDaniel P. Berrange if (client->tls) { 2543e305e4aSDaniel P. Berrange client->info->x509_dname = 2553e305e4aSDaniel P. Berrange qcrypto_tls_session_get_peer_name(client->tls); 2563e305e4aSDaniel P. Berrange client->info->has_x509_dname = 2573e305e4aSDaniel P. Berrange client->info->x509_dname != NULL; 2583e230dd2SCorentin Chary } 2593e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2603e230dd2SCorentin Chary if (client->sasl.conn && 2613e230dd2SCorentin Chary client->sasl.username) { 262fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 263fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2643e230dd2SCorentin Chary } 2653e230dd2SCorentin Chary #endif 2663e230dd2SCorentin Chary } 2673e230dd2SCorentin Chary 2683e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2693e230dd2SCorentin Chary { 27098481bfcSEric Blake Error *err = NULL; 2713e230dd2SCorentin Chary 272fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 27304d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 274ddf21908SEric Blake qapi_VncClientInfo_base(client->info), 27598481bfcSEric Blake &err); 27698481bfcSEric Blake if (err) { 27798481bfcSEric Blake qapi_free_VncClientInfo(client->info); 27898481bfcSEric Blake client->info = NULL; 27998481bfcSEric Blake error_free(err); 280fb6ba0d5SWenchao Xia } 2813e230dd2SCorentin Chary } 2823e230dd2SCorentin Chary 283fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2843e230dd2SCorentin Chary { 285fb6ba0d5SWenchao Xia VncServerInfo *si; 2863e230dd2SCorentin Chary 2873e230dd2SCorentin Chary if (!vs->info) { 2883e230dd2SCorentin Chary return; 2893e230dd2SCorentin Chary } 2903e230dd2SCorentin Chary 291d616ccc5SGerd Hoffmann si = vnc_server_info_get(vs->vd); 292fb6ba0d5SWenchao Xia if (!si) { 2933e230dd2SCorentin Chary return; 2943e230dd2SCorentin Chary } 2953e230dd2SCorentin Chary 296fb6ba0d5SWenchao Xia switch (event) { 297fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 298ddf21908SEric Blake qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info), 299ddf21908SEric Blake &error_abort); 300fb6ba0d5SWenchao Xia break; 301fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 302fb6ba0d5SWenchao Xia qapi_event_send_vnc_initialized(si, vs->info, &error_abort); 303fb6ba0d5SWenchao Xia break; 304fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 305fb6ba0d5SWenchao Xia qapi_event_send_vnc_disconnected(si, vs->info, &error_abort); 306fb6ba0d5SWenchao Xia break; 307fb6ba0d5SWenchao Xia default: 308fb6ba0d5SWenchao Xia break; 309fb6ba0d5SWenchao Xia } 3103e230dd2SCorentin Chary 311fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 3123e230dd2SCorentin Chary } 3133e230dd2SCorentin Chary 3142b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3153e230dd2SCorentin Chary { 3162b54aa87SLuiz Capitulino VncClientInfo *info; 31704d2529dSDaniel P. Berrange Error *err = NULL; 3182b54aa87SLuiz Capitulino 3192b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 32004d2529dSDaniel P. Berrange 32104d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 32204d2529dSDaniel P. Berrange qapi_VncClientInfo_base(info), 32304d2529dSDaniel P. Berrange &err); 32404d2529dSDaniel P. Berrange if (err) { 32504d2529dSDaniel P. Berrange error_free(err); 32604d2529dSDaniel P. Berrange qapi_free_VncClientInfo(info); 32704d2529dSDaniel P. Berrange return NULL; 32804d2529dSDaniel P. Berrange } 32904d2529dSDaniel P. Berrange 330ddf21908SEric Blake info->websocket = client->websocket; 3313e230dd2SCorentin Chary 3323e305e4aSDaniel P. Berrange if (client->tls) { 3333e305e4aSDaniel P. Berrange info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); 3343e305e4aSDaniel P. Berrange info->has_x509_dname = info->x509_dname != NULL; 3352b54aa87SLuiz Capitulino } 3363e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3372b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3382b54aa87SLuiz Capitulino info->has_sasl_username = true; 3392b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3402b54aa87SLuiz Capitulino } 3413e230dd2SCorentin Chary #endif 3422b54aa87SLuiz Capitulino 3432b54aa87SLuiz Capitulino return info; 3443e230dd2SCorentin Chary } 3453e230dd2SCorentin Chary 346d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id) 347d616ccc5SGerd Hoffmann { 348d616ccc5SGerd Hoffmann VncDisplay *vd; 349d616ccc5SGerd Hoffmann 350d616ccc5SGerd Hoffmann if (id == NULL) { 351d616ccc5SGerd Hoffmann return QTAILQ_FIRST(&vnc_displays); 352d616ccc5SGerd Hoffmann } 353d616ccc5SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 354d616ccc5SGerd Hoffmann if (strcmp(id, vd->id) == 0) { 355d616ccc5SGerd Hoffmann return vd; 356d616ccc5SGerd Hoffmann } 357d616ccc5SGerd Hoffmann } 358d616ccc5SGerd Hoffmann return NULL; 359d616ccc5SGerd Hoffmann } 360d616ccc5SGerd Hoffmann 3612d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) 3622d29a436SGerd Hoffmann { 3632d29a436SGerd Hoffmann VncClientInfoList *cinfo, *prev = NULL; 3642d29a436SGerd Hoffmann VncState *client; 3652d29a436SGerd Hoffmann 3662d29a436SGerd Hoffmann QTAILQ_FOREACH(client, &vd->clients, next) { 3672d29a436SGerd Hoffmann cinfo = g_new0(VncClientInfoList, 1); 3682d29a436SGerd Hoffmann cinfo->value = qmp_query_vnc_client(client); 3692d29a436SGerd Hoffmann cinfo->next = prev; 3702d29a436SGerd Hoffmann prev = cinfo; 3712d29a436SGerd Hoffmann } 3722d29a436SGerd Hoffmann return prev; 3732d29a436SGerd Hoffmann } 3742d29a436SGerd Hoffmann 3752b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3763e230dd2SCorentin Chary { 3772b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 378d616ccc5SGerd Hoffmann VncDisplay *vd = vnc_display_find(NULL); 379bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3803e230dd2SCorentin Chary 3814ee74fa7SDaniel P. Berrange if (vd == NULL || !vd->nlsock) { 3822b54aa87SLuiz Capitulino info->enabled = false; 3833e230dd2SCorentin Chary } else { 3842b54aa87SLuiz Capitulino info->enabled = true; 3852b54aa87SLuiz Capitulino 3862b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3872b54aa87SLuiz Capitulino info->has_clients = true; 3882d29a436SGerd Hoffmann info->clients = qmp_query_client_list(vd); 3893e230dd2SCorentin Chary 39004d2529dSDaniel P. Berrange if (vd->lsock == NULL) { 391417b0b88SPaolo Bonzini return info; 392417b0b88SPaolo Bonzini } 393417b0b88SPaolo Bonzini 3944ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], errp); 39504d2529dSDaniel P. Berrange if (!addr) { 3962b54aa87SLuiz Capitulino goto out_error; 3972b54aa87SLuiz Capitulino } 3983e230dd2SCorentin Chary 39904d2529dSDaniel P. Berrange switch (addr->type) { 400bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 401bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 402bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 403bd269ebcSMarkus Armbruster if (addr->u.inet.ipv6) { 40404d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 40504d2529dSDaniel P. Berrange } else { 40604d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 40704d2529dSDaniel P. Berrange } 40804d2529dSDaniel P. Berrange break; 40904d2529dSDaniel P. Berrange 410bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 41104d2529dSDaniel P. Berrange info->host = g_strdup(""); 412bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 41304d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 41404d2529dSDaniel P. Berrange break; 41504d2529dSDaniel P. Berrange 416bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 417bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 418a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 419977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 4202b54aa87SLuiz Capitulino goto out_error; 421a6c76285SMarkus Armbruster default: 422a6c76285SMarkus Armbruster abort(); 4233e230dd2SCorentin Chary } 4242b54aa87SLuiz Capitulino 4252b54aa87SLuiz Capitulino info->has_host = true; 4262b54aa87SLuiz Capitulino info->has_service = true; 4272b54aa87SLuiz Capitulino info->has_family = true; 4282b54aa87SLuiz Capitulino 4292b54aa87SLuiz Capitulino info->has_auth = true; 430d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 4313e230dd2SCorentin Chary } 4322b54aa87SLuiz Capitulino 433bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4342b54aa87SLuiz Capitulino return info; 4352b54aa87SLuiz Capitulino 4362b54aa87SLuiz Capitulino out_error: 437bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4382b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4392b54aa87SLuiz Capitulino return NULL; 4403e230dd2SCorentin Chary } 4413e230dd2SCorentin Chary 4422a7e6857SDaniel P. Berrange 4432a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4442a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4452a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4462a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt); 4472a7e6857SDaniel P. Berrange 4482a7e6857SDaniel P. Berrange static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc, 4494478aa76SGerd Hoffmann bool websocket, 4502a7e6857SDaniel P. Berrange int auth, 4512a7e6857SDaniel P. Berrange int subauth, 4522a7e6857SDaniel P. Berrange VncServerInfo2List *prev) 453df887684SGerd Hoffmann { 4542a7e6857SDaniel P. Berrange VncServerInfo2List *list; 4552a7e6857SDaniel P. Berrange VncServerInfo2 *info; 45604d2529dSDaniel P. Berrange Error *err = NULL; 457bd269ebcSMarkus Armbruster SocketAddress *addr; 458df887684SGerd Hoffmann 45904d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, &err); 46004d2529dSDaniel P. Berrange if (!addr) { 46104d2529dSDaniel P. Berrange error_free(err); 462df887684SGerd Hoffmann return prev; 463df887684SGerd Hoffmann } 464df887684SGerd Hoffmann 4652a7e6857SDaniel P. Berrange info = g_new0(VncServerInfo2, 1); 4662a7e6857SDaniel P. Berrange vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err); 467bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 46804d2529dSDaniel P. Berrange if (err) { 4692a7e6857SDaniel P. Berrange qapi_free_VncServerInfo2(info); 47004d2529dSDaniel P. Berrange error_free(err); 47104d2529dSDaniel P. Berrange return prev; 47204d2529dSDaniel P. Berrange } 4734478aa76SGerd Hoffmann info->websocket = websocket; 474df887684SGerd Hoffmann 4752a7e6857SDaniel P. Berrange qmp_query_auth(auth, subauth, &info->auth, 4762a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 4772a7e6857SDaniel P. Berrange 4782a7e6857SDaniel P. Berrange list = g_new0(VncServerInfo2List, 1); 479df887684SGerd Hoffmann list->value = info; 480df887684SGerd Hoffmann list->next = prev; 481df887684SGerd Hoffmann return list; 482df887684SGerd Hoffmann } 483df887684SGerd Hoffmann 4842a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4852a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4862a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4872a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt) 488df887684SGerd Hoffmann { 4892a7e6857SDaniel P. Berrange switch (auth) { 490df887684SGerd Hoffmann case VNC_AUTH_VNC: 4912a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VNC; 492df887684SGerd Hoffmann break; 493df887684SGerd Hoffmann case VNC_AUTH_RA2: 4942a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2; 495df887684SGerd Hoffmann break; 496df887684SGerd Hoffmann case VNC_AUTH_RA2NE: 4972a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2NE; 498df887684SGerd Hoffmann break; 499df887684SGerd Hoffmann case VNC_AUTH_TIGHT: 5002a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TIGHT; 501df887684SGerd Hoffmann break; 502df887684SGerd Hoffmann case VNC_AUTH_ULTRA: 5032a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_ULTRA; 504df887684SGerd Hoffmann break; 505df887684SGerd Hoffmann case VNC_AUTH_TLS: 5062a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TLS; 507df887684SGerd Hoffmann break; 508df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT: 5092a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT; 5102a7e6857SDaniel P. Berrange *qmp_has_vencrypt = true; 5112a7e6857SDaniel P. Berrange switch (subauth) { 512df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_PLAIN: 5132a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; 514df887684SGerd Hoffmann break; 515df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSNONE: 5162a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; 517df887684SGerd Hoffmann break; 518df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSVNC: 5192a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; 520df887684SGerd Hoffmann break; 521df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSPLAIN: 5222a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; 523df887684SGerd Hoffmann break; 524df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509NONE: 5252a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; 526df887684SGerd Hoffmann break; 527df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509VNC: 5282a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; 529df887684SGerd Hoffmann break; 530df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509PLAIN: 5312a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; 532df887684SGerd Hoffmann break; 533df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSSASL: 5342a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; 535df887684SGerd Hoffmann break; 536df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509SASL: 5372a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; 538df887684SGerd Hoffmann break; 539df887684SGerd Hoffmann default: 5402a7e6857SDaniel P. Berrange *qmp_has_vencrypt = false; 541df887684SGerd Hoffmann break; 542df887684SGerd Hoffmann } 543df887684SGerd Hoffmann break; 544df887684SGerd Hoffmann case VNC_AUTH_SASL: 5452a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_SASL; 546df887684SGerd Hoffmann break; 547df887684SGerd Hoffmann case VNC_AUTH_NONE: 548df887684SGerd Hoffmann default: 5492a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_NONE; 550df887684SGerd Hoffmann break; 551df887684SGerd Hoffmann } 552df887684SGerd Hoffmann } 553df887684SGerd Hoffmann 554df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp) 555df887684SGerd Hoffmann { 556df887684SGerd Hoffmann VncInfo2List *item, *prev = NULL; 557df887684SGerd Hoffmann VncInfo2 *info; 558df887684SGerd Hoffmann VncDisplay *vd; 559df887684SGerd Hoffmann DeviceState *dev; 5604ee74fa7SDaniel P. Berrange size_t i; 561df887684SGerd Hoffmann 562df887684SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 563df887684SGerd Hoffmann info = g_new0(VncInfo2, 1); 564df887684SGerd Hoffmann info->id = g_strdup(vd->id); 565df887684SGerd Hoffmann info->clients = qmp_query_client_list(vd); 5662a7e6857SDaniel P. Berrange qmp_query_auth(vd->auth, vd->subauth, &info->auth, 5672a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 568df887684SGerd Hoffmann if (vd->dcl.con) { 569df887684SGerd Hoffmann dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con), 570df887684SGerd Hoffmann "device", NULL)); 571df887684SGerd Hoffmann info->has_display = true; 572df887684SGerd Hoffmann info->display = g_strdup(dev->id); 573df887684SGerd Hoffmann } 5744ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 57504d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 5764ee74fa7SDaniel P. Berrange vd->lsock[i], false, vd->auth, vd->subauth, info->server); 577df887684SGerd Hoffmann } 5784ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 57904d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 5804ee74fa7SDaniel P. Berrange vd->lwebsock[i], true, vd->ws_auth, 5814ee74fa7SDaniel P. Berrange vd->ws_subauth, info->server); 582df887684SGerd Hoffmann } 583df887684SGerd Hoffmann 584df887684SGerd Hoffmann item = g_new0(VncInfo2List, 1); 585df887684SGerd Hoffmann item->value = info; 586df887684SGerd Hoffmann item->next = prev; 587df887684SGerd Hoffmann prev = item; 588df887684SGerd Hoffmann } 589df887684SGerd Hoffmann return prev; 590df887684SGerd Hoffmann } 591df887684SGerd Hoffmann 5923e230dd2SCorentin Chary /* TODO 5933e230dd2SCorentin Chary 1) Get the queue working for IO. 5943e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 5953e230dd2SCorentin Chary and not totally invalidated 5963e230dd2SCorentin Chary 3) resolutions > 1024 5973e230dd2SCorentin Chary */ 5983e230dd2SCorentin Chary 5996af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty); 6003e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 6013e230dd2SCorentin Chary 6023e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 6033e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 6043e230dd2SCorentin Chary int x_position, int y_position, 6053e230dd2SCorentin Chary int w, int h); 6060f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 6073e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 6083e230dd2SCorentin Chary 609d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd) 610d05959c2SGerd Hoffmann { 611d05959c2SGerd Hoffmann return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 612d05959c2SGerd Hoffmann VNC_DIRTY_PIXELS_PER_BIT)); 613d05959c2SGerd Hoffmann } 614d05959c2SGerd Hoffmann 615d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd) 616d05959c2SGerd Hoffmann { 617d05959c2SGerd Hoffmann return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 618d05959c2SGerd Hoffmann } 619d05959c2SGerd Hoffmann 620bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 621bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 622f7b3d68cSGerd Hoffmann VncDisplay *vd, 623f7b3d68cSGerd Hoffmann int x, int y, int w, int h) 624f7b3d68cSGerd Hoffmann { 625f7b3d68cSGerd Hoffmann int width = vnc_width(vd); 626f7b3d68cSGerd Hoffmann int height = vnc_height(vd); 627f7b3d68cSGerd Hoffmann 62891937225SPeter Lieven /* this is needed this to ensure we updated all affected 62991937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 630b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 631b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 6323e230dd2SCorentin Chary 6339f64916dSGerd Hoffmann x = MIN(x, width); 6349f64916dSGerd Hoffmann y = MIN(y, height); 6359f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 63691937225SPeter Lieven h = MIN(y + h, height); 6373e230dd2SCorentin Chary 638b4c85ddcSPeter Lieven for (; y < h; y++) { 639bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 64091937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 641b4c85ddcSPeter Lieven } 6423e230dd2SCorentin Chary } 6433e230dd2SCorentin Chary 644bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 645bea60dd7SPeter Lieven int x, int y, int w, int h) 646bea60dd7SPeter Lieven { 647bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 648bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 649bea60dd7SPeter Lieven 650f7b3d68cSGerd Hoffmann vnc_set_area_dirty(s->dirty, vd, x, y, w, h); 651bea60dd7SPeter Lieven } 652bea60dd7SPeter Lieven 6533e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 6543e230dd2SCorentin Chary int32_t encoding) 6553e230dd2SCorentin Chary { 6563e230dd2SCorentin Chary vnc_write_u16(vs, x); 6573e230dd2SCorentin Chary vnc_write_u16(vs, y); 6583e230dd2SCorentin Chary vnc_write_u16(vs, w); 6593e230dd2SCorentin Chary vnc_write_u16(vs, h); 6603e230dd2SCorentin Chary 6613e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 6623e230dd2SCorentin Chary } 6633e230dd2SCorentin Chary 66432ed2680STim Hardeck 6653e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 6663e230dd2SCorentin Chary { 66704d2529dSDaniel P. Berrange if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 6683e230dd2SCorentin Chary return; 6693e230dd2SCorentin Chary } 670bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 671bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 6723e230dd2SCorentin Chary return; 6733e230dd2SCorentin Chary } 674bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 675bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 676bd023f95SCorentin Chary vnc_lock_output(vs); 6773e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 6783e230dd2SCorentin Chary vnc_write_u8(vs, 0); 6793e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 6803e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 6813e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 682bd023f95SCorentin Chary vnc_unlock_output(vs); 6833e230dd2SCorentin Chary vnc_flush(vs); 6843e230dd2SCorentin Chary } 6853e230dd2SCorentin Chary 686bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 687bd023f95SCorentin Chary { 688bd023f95SCorentin Chary VncState *vs; 689bd023f95SCorentin Chary 690bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 691bd023f95SCorentin Chary vnc_lock_output(vs); 692bd023f95SCorentin Chary vs->abort = true; 693bd023f95SCorentin Chary vnc_unlock_output(vs); 694bd023f95SCorentin Chary } 695bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 696bd023f95SCorentin Chary vnc_jobs_join(vs); 697bd023f95SCorentin Chary } 698bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 699bd023f95SCorentin Chary vnc_lock_output(vs); 700bd023f95SCorentin Chary vs->abort = false; 701bd023f95SCorentin Chary vnc_unlock_output(vs); 702bd023f95SCorentin Chary } 703bd023f95SCorentin Chary } 704bd023f95SCorentin Chary 7059f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 7069f64916dSGerd Hoffmann { 7079f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 7089f64916dSGerd Hoffmann } 7099f64916dSGerd Hoffmann 7109f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 7119f64916dSGerd Hoffmann { 7129f64916dSGerd Hoffmann uint8_t *ptr; 7139f64916dSGerd Hoffmann 7149f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 7159f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 7169f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 7179f64916dSGerd Hoffmann return ptr; 7189f64916dSGerd Hoffmann } 7199f64916dSGerd Hoffmann 720453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd) 721453f842bSGerd Hoffmann { 722b69a553bSDaniel P. Berrange int width, height; 723b69a553bSDaniel P. Berrange 724453f842bSGerd Hoffmann qemu_pixman_image_unref(vd->server); 725453f842bSGerd Hoffmann vd->server = NULL; 726453f842bSGerd Hoffmann 727c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 728c7628bffSGerd Hoffmann return; 729c7628bffSGerd Hoffmann } 730c7628bffSGerd Hoffmann 731b69a553bSDaniel P. Berrange width = vnc_width(vd); 732b69a553bSDaniel P. Berrange height = vnc_height(vd); 733453f842bSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 734b69a553bSDaniel P. Berrange width, height, 735453f842bSGerd Hoffmann NULL, 0); 736b69a553bSDaniel P. Berrange 737b69a553bSDaniel P. Berrange memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 738b69a553bSDaniel P. Berrange vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 739b69a553bSDaniel P. Berrange width, height); 740453f842bSGerd Hoffmann } 741453f842bSGerd Hoffmann 742c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 743c12aeb86SGerd Hoffmann DisplaySurface *surface) 7443e230dd2SCorentin Chary { 74521ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 7463e230dd2SCorentin Chary VncState *vs; 7473e230dd2SCorentin Chary 748bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 749453f842bSGerd Hoffmann vd->ds = surface; 750bd023f95SCorentin Chary 7513e230dd2SCorentin Chary /* server surface */ 752453f842bSGerd Hoffmann vnc_update_server_surface(vd); 7533e230dd2SCorentin Chary 7543e230dd2SCorentin Chary /* guest surface */ 7559f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 756d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 757d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 7583e230dd2SCorentin Chary 7593e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 7603e230dd2SCorentin Chary vnc_colordepth(vs); 7613e230dd2SCorentin Chary vnc_desktop_resize(vs); 7623e230dd2SCorentin Chary if (vs->vd->cursor) { 7633e230dd2SCorentin Chary vnc_cursor_define(vs); 7643e230dd2SCorentin Chary } 765bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 766f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vd, 0, 0, 767b69a553bSDaniel P. Berrange vnc_width(vd), 768b69a553bSDaniel P. Berrange vnc_height(vd)); 7693e230dd2SCorentin Chary } 7703e230dd2SCorentin Chary } 7713e230dd2SCorentin Chary 7723e230dd2SCorentin Chary /* fastest code */ 7739f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 7743e230dd2SCorentin Chary void *pixels, int size) 7753e230dd2SCorentin Chary { 7763e230dd2SCorentin Chary vnc_write(vs, pixels, size); 7773e230dd2SCorentin Chary } 7783e230dd2SCorentin Chary 7793e230dd2SCorentin Chary /* slowest but generic code. */ 7803e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 7813e230dd2SCorentin Chary { 7823e230dd2SCorentin Chary uint8_t r, g, b; 7833e230dd2SCorentin Chary 7849f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 7859f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 7869f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 7879f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 7889f64916dSGerd Hoffmann #else 7899f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 7909f64916dSGerd Hoffmann #endif 7919f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 7929f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 7939f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 7949f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 7953e230dd2SCorentin Chary case 1: 7963e230dd2SCorentin Chary buf[0] = v; 7973e230dd2SCorentin Chary break; 7983e230dd2SCorentin Chary case 2: 7999f64916dSGerd Hoffmann if (vs->client_be) { 8003e230dd2SCorentin Chary buf[0] = v >> 8; 8013e230dd2SCorentin Chary buf[1] = v; 8023e230dd2SCorentin Chary } else { 8033e230dd2SCorentin Chary buf[1] = v >> 8; 8043e230dd2SCorentin Chary buf[0] = v; 8053e230dd2SCorentin Chary } 8063e230dd2SCorentin Chary break; 8073e230dd2SCorentin Chary default: 8083e230dd2SCorentin Chary case 4: 8099f64916dSGerd Hoffmann if (vs->client_be) { 8103e230dd2SCorentin Chary buf[0] = v >> 24; 8113e230dd2SCorentin Chary buf[1] = v >> 16; 8123e230dd2SCorentin Chary buf[2] = v >> 8; 8133e230dd2SCorentin Chary buf[3] = v; 8143e230dd2SCorentin Chary } else { 8153e230dd2SCorentin Chary buf[3] = v >> 24; 8163e230dd2SCorentin Chary buf[2] = v >> 16; 8173e230dd2SCorentin Chary buf[1] = v >> 8; 8183e230dd2SCorentin Chary buf[0] = v; 8193e230dd2SCorentin Chary } 8203e230dd2SCorentin Chary break; 8213e230dd2SCorentin Chary } 8223e230dd2SCorentin Chary } 8233e230dd2SCorentin Chary 8249f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 8253e230dd2SCorentin Chary void *pixels1, int size) 8263e230dd2SCorentin Chary { 8273e230dd2SCorentin Chary uint8_t buf[4]; 8283e230dd2SCorentin Chary 8299f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 8303e230dd2SCorentin Chary uint32_t *pixels = pixels1; 8313e230dd2SCorentin Chary int n, i; 8323e230dd2SCorentin Chary n = size >> 2; 8333e230dd2SCorentin Chary for (i = 0; i < n; i++) { 8343e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 8359f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 8363e230dd2SCorentin Chary } 8373e230dd2SCorentin Chary } 8383e230dd2SCorentin Chary } 8393e230dd2SCorentin Chary 8403e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8413e230dd2SCorentin Chary { 8423e230dd2SCorentin Chary int i; 8433e230dd2SCorentin Chary uint8_t *row; 8443e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8453e230dd2SCorentin Chary 8469f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 8473e230dd2SCorentin Chary for (i = 0; i < h; i++) { 8489f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 8499f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 8503e230dd2SCorentin Chary } 8513e230dd2SCorentin Chary return 1; 8523e230dd2SCorentin Chary } 8533e230dd2SCorentin Chary 854bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8553e230dd2SCorentin Chary { 8563e230dd2SCorentin Chary int n = 0; 857de3f7de7SPeter Lieven bool encode_raw = false; 858de3f7de7SPeter Lieven size_t saved_offs = vs->output.offset; 8593e230dd2SCorentin Chary 8603e230dd2SCorentin Chary switch(vs->vnc_encoding) { 8613e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 8623e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 8633e230dd2SCorentin Chary break; 8643e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 8653e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 8663e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 8673e230dd2SCorentin Chary break; 8683e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 8693e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 8703e230dd2SCorentin Chary break; 871efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 872efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 873efe556adSCorentin Chary break; 874148954faSCorentin Chary case VNC_ENCODING_ZRLE: 875148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 876148954faSCorentin Chary break; 877148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 878148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 879148954faSCorentin Chary break; 8803e230dd2SCorentin Chary default: 881de3f7de7SPeter Lieven encode_raw = true; 8823e230dd2SCorentin Chary break; 8833e230dd2SCorentin Chary } 884de3f7de7SPeter Lieven 885de3f7de7SPeter Lieven /* If the client has the same pixel format as our internal buffer and 886de3f7de7SPeter Lieven * a RAW encoding would need less space fall back to RAW encoding to 887de3f7de7SPeter Lieven * save bandwidth and processing power in the client. */ 888de3f7de7SPeter Lieven if (!encode_raw && vs->write_pixels == vnc_write_pixels_copy && 889de3f7de7SPeter Lieven 12 + h * w * VNC_SERVER_FB_BYTES <= (vs->output.offset - saved_offs)) { 890de3f7de7SPeter Lieven vs->output.offset = saved_offs; 891de3f7de7SPeter Lieven encode_raw = true; 892de3f7de7SPeter Lieven } 893de3f7de7SPeter Lieven 894de3f7de7SPeter Lieven if (encode_raw) { 895de3f7de7SPeter Lieven vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 896de3f7de7SPeter Lieven n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 897de3f7de7SPeter Lieven } 898de3f7de7SPeter Lieven 8993e230dd2SCorentin Chary return n; 9003e230dd2SCorentin Chary } 9013e230dd2SCorentin Chary 9027c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 9037c20b4a3SGerd Hoffmann int x, int y, int visible) 9043e230dd2SCorentin Chary { 9053e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 9063e230dd2SCorentin Chary } 9073e230dd2SCorentin Chary 9083e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 9093e230dd2SCorentin Chary { 9103e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 9113e230dd2SCorentin Chary int isize; 9123e230dd2SCorentin Chary 9133e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 914d01f9595SCorentin Chary vnc_lock_output(vs); 9153e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 9163e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 9173e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 9183e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 9193e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 9209f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 9219f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 9223e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 923d01f9595SCorentin Chary vnc_unlock_output(vs); 9243e230dd2SCorentin Chary return 0; 9253e230dd2SCorentin Chary } 9263e230dd2SCorentin Chary return -1; 9273e230dd2SCorentin Chary } 9283e230dd2SCorentin Chary 9297c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 9307c20b4a3SGerd Hoffmann QEMUCursor *c) 9313e230dd2SCorentin Chary { 932d616ccc5SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 9333e230dd2SCorentin Chary VncState *vs; 9343e230dd2SCorentin Chary 9353e230dd2SCorentin Chary cursor_put(vd->cursor); 9367267c094SAnthony Liguori g_free(vd->cursor_mask); 9373e230dd2SCorentin Chary 9383e230dd2SCorentin Chary vd->cursor = c; 9393e230dd2SCorentin Chary cursor_get(vd->cursor); 9403e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 9417267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 9423e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 9433e230dd2SCorentin Chary 9443e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9453e230dd2SCorentin Chary vnc_cursor_define(vs); 9463e230dd2SCorentin Chary } 9473e230dd2SCorentin Chary } 9483e230dd2SCorentin Chary 9494769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs, 9506c71a539SCorentin Chary int y, int last_x, int x, int height) 9513e230dd2SCorentin Chary { 9523e230dd2SCorentin Chary int h; 9533e230dd2SCorentin Chary 9546c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 955bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 9563e230dd2SCorentin Chary break; 957bc2429b9SCorentin Chary } 958863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 9593e230dd2SCorentin Chary } 9603e230dd2SCorentin Chary 9613e230dd2SCorentin Chary return h; 9623e230dd2SCorentin Chary } 9633e230dd2SCorentin Chary 9646af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty) 9653e230dd2SCorentin Chary { 9663e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 967bd023f95SCorentin Chary VncJob *job; 9683e230dd2SCorentin Chary int y; 9692f487a3dSPeter Lieven int height, width; 970bd023f95SCorentin Chary int n = 0; 971bd023f95SCorentin Chary 972b939eb89SDaniel P. Berrange if (vs->disconnecting) { 973b939eb89SDaniel P. Berrange vnc_disconnect_finish(vs); 974b939eb89SDaniel P. Berrange return 0; 975b939eb89SDaniel P. Berrange } 976b939eb89SDaniel P. Berrange 977b939eb89SDaniel P. Berrange vs->has_dirty += has_dirty; 978fef1bbadSDaniel P. Berrange if (vs->update == VNC_STATE_UPDATE_NONE) { 979b939eb89SDaniel P. Berrange return 0; 980b939eb89SDaniel P. Berrange } 981b939eb89SDaniel P. Berrange 982fef1bbadSDaniel P. Berrange if (vs->output.offset && !vs->audio_cap && 983fef1bbadSDaniel P. Berrange vs->update != VNC_STATE_UPDATE_FORCE) { 9843e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 9853e230dd2SCorentin Chary return 0; 986b939eb89SDaniel P. Berrange } 9873e230dd2SCorentin Chary 988fef1bbadSDaniel P. Berrange if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) { 9893e230dd2SCorentin Chary return 0; 990b939eb89SDaniel P. Berrange } 9913e230dd2SCorentin Chary 9923e230dd2SCorentin Chary /* 9933e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9943e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9953e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9963e230dd2SCorentin Chary * send them to the client. 9973e230dd2SCorentin Chary */ 998bd023f95SCorentin Chary job = vnc_job_new(vs); 9993e230dd2SCorentin Chary 1000bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 1001bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 10023e230dd2SCorentin Chary 100312b316d4SPeter Lieven y = 0; 100412b316d4SPeter Lieven for (;;) { 100512b316d4SPeter Lieven int x, h; 100612b316d4SPeter Lieven unsigned long x2; 100712b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 100812b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 100912b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 101012b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 101112b316d4SPeter Lieven /* no more dirty bits */ 101212b316d4SPeter Lieven break; 10133e230dd2SCorentin Chary } 101412b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 101512b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 101612b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 101712b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 101812b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 101912b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10202f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10212f487a3dSPeter Lieven if (x2 > x) { 102212b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 102312b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10243e230dd2SCorentin Chary } 10250e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10260e7d6f60SPeter Lieven y += h; 10270e7d6f60SPeter Lieven if (y == height) { 10280e7d6f60SPeter Lieven break; 10290e7d6f60SPeter Lieven } 10300e7d6f60SPeter Lieven } 10312f487a3dSPeter Lieven } 1032bd023f95SCorentin Chary 1033bd023f95SCorentin Chary vnc_job_push(job); 1034*728a7ac9SDaniel P. Berrange vs->update = VNC_STATE_UPDATE_NONE; 103563658280SGerd Hoffmann vs->has_dirty = 0; 1036bd023f95SCorentin Chary return n; 10373e230dd2SCorentin Chary } 10383e230dd2SCorentin Chary 10393e230dd2SCorentin Chary /* audio */ 10403e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 10413e230dd2SCorentin Chary { 10423e230dd2SCorentin Chary VncState *vs = opaque; 10433e230dd2SCorentin Chary 10443e230dd2SCorentin Chary switch (cmd) { 10453e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1046bd023f95SCorentin Chary vnc_lock_output(vs); 10473e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10483e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10493e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1050bd023f95SCorentin Chary vnc_unlock_output(vs); 10513e230dd2SCorentin Chary vnc_flush(vs); 10523e230dd2SCorentin Chary break; 10533e230dd2SCorentin Chary 10543e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1055bd023f95SCorentin Chary vnc_lock_output(vs); 10563e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10573e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10583e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1059bd023f95SCorentin Chary vnc_unlock_output(vs); 10603e230dd2SCorentin Chary vnc_flush(vs); 10613e230dd2SCorentin Chary break; 10623e230dd2SCorentin Chary } 10633e230dd2SCorentin Chary } 10643e230dd2SCorentin Chary 10653e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 10663e230dd2SCorentin Chary { 10673e230dd2SCorentin Chary } 10683e230dd2SCorentin Chary 10693e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 10703e230dd2SCorentin Chary { 10713e230dd2SCorentin Chary VncState *vs = opaque; 10723e230dd2SCorentin Chary 1073bd023f95SCorentin Chary vnc_lock_output(vs); 10743e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10753e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10763e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 10773e230dd2SCorentin Chary vnc_write_u32(vs, size); 10783e230dd2SCorentin Chary vnc_write(vs, buf, size); 1079bd023f95SCorentin Chary vnc_unlock_output(vs); 10803e230dd2SCorentin Chary vnc_flush(vs); 10813e230dd2SCorentin Chary } 10823e230dd2SCorentin Chary 10833e230dd2SCorentin Chary static void audio_add(VncState *vs) 10843e230dd2SCorentin Chary { 10853e230dd2SCorentin Chary struct audio_capture_ops ops; 10863e230dd2SCorentin Chary 10873e230dd2SCorentin Chary if (vs->audio_cap) { 1088027a79c3SCole Robinson error_report("audio already running"); 10893e230dd2SCorentin Chary return; 10903e230dd2SCorentin Chary } 10913e230dd2SCorentin Chary 10923e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10933e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 10943e230dd2SCorentin Chary ops.capture = audio_capture; 10953e230dd2SCorentin Chary 10963e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 10973e230dd2SCorentin Chary if (!vs->audio_cap) { 1098027a79c3SCole Robinson error_report("Failed to add audio capture"); 10993e230dd2SCorentin Chary } 11003e230dd2SCorentin Chary } 11013e230dd2SCorentin Chary 11023e230dd2SCorentin Chary static void audio_del(VncState *vs) 11033e230dd2SCorentin Chary { 11043e230dd2SCorentin Chary if (vs->audio_cap) { 11053e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11063e230dd2SCorentin Chary vs->audio_cap = NULL; 11073e230dd2SCorentin Chary } 11083e230dd2SCorentin Chary } 11093e230dd2SCorentin Chary 11103e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11113e230dd2SCorentin Chary { 111204d2529dSDaniel P. Berrange if (vs->disconnecting) { 11133e230dd2SCorentin Chary return; 111404d2529dSDaniel P. Berrange } 1115ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_start(vs, vs->ioc); 11168cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 111704d2529dSDaniel P. Berrange if (vs->ioc_tag) { 111804d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 1119a75d6f07SBrandon Carpenter vs->ioc_tag = 0; 112004d2529dSDaniel P. Berrange } 112104d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 112204d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11233e230dd2SCorentin Chary } 11243e230dd2SCorentin Chary 11257536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11263e230dd2SCorentin Chary { 11277d964c9dSCorentin Chary int i; 11287d964c9dSCorentin Chary 1129ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_finish(vs, vs->ioc); 1130ad6374c4SDaniel P. Berrange 1131bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1132bd023f95SCorentin Chary 1133bd023f95SCorentin Chary vnc_lock_output(vs); 1134fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11353e230dd2SCorentin Chary 11363e230dd2SCorentin Chary buffer_free(&vs->input); 11373e230dd2SCorentin Chary buffer_free(&vs->output); 11383e230dd2SCorentin Chary 1139fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11403e230dd2SCorentin Chary 11413e230dd2SCorentin Chary vnc_zlib_clear(vs); 11423e230dd2SCorentin Chary vnc_tight_clear(vs); 1143148954faSCorentin Chary vnc_zrle_clear(vs); 11443e230dd2SCorentin Chary 11453e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11463e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 11473e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11483e230dd2SCorentin Chary audio_del(vs); 11497bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 11503e230dd2SCorentin Chary 115190cd03a3SDaniel P. Berrange if (vs->mouse_mode_notifier.notify != NULL) { 11526fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 115390cd03a3SDaniel P. Berrange } 115490cd03a3SDaniel P. Berrange QTAILQ_REMOVE(&vs->vd->clients, vs, next); 1155c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1156c7628bffSGerd Hoffmann /* last client gone */ 1157c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1158c7628bffSGerd Hoffmann } 11593e230dd2SCorentin Chary 1160bd023f95SCorentin Chary vnc_unlock_output(vs); 1161bd023f95SCorentin Chary 1162bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 11636fd8e79aSTim Hardeck if (vs->bh != NULL) { 1164175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 11656fd8e79aSTim Hardeck } 1166175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1167175b2a6eSCorentin Chary 11687d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 11697267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 11707d964c9dSCorentin Chary } 11717267c094SAnthony Liguori g_free(vs->lossy_rect); 117204d2529dSDaniel P. Berrange 117304d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 117404d2529dSDaniel P. Berrange vs->ioc = NULL; 117504d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 117604d2529dSDaniel P. Berrange vs->sioc = NULL; 11777267c094SAnthony Liguori g_free(vs); 11783e230dd2SCorentin Chary } 11793e230dd2SCorentin Chary 118004d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 11813e230dd2SCorentin Chary { 118204d2529dSDaniel P. Berrange if (ret <= 0) { 118304d2529dSDaniel P. Berrange if (ret == 0) { 1184ad6374c4SDaniel P. Berrange trace_vnc_client_eof(vs, vs->ioc); 1185537848eeSMichael Tokarev vnc_disconnect_start(vs); 118604d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 1187ad6374c4SDaniel P. Berrange trace_vnc_client_io_error(vs, vs->ioc, 1188ad6374c4SDaniel P. Berrange errp ? error_get_pretty(*errp) : 1189ad6374c4SDaniel P. Berrange "Unknown"); 1190537848eeSMichael Tokarev vnc_disconnect_start(vs); 11913e230dd2SCorentin Chary } 11923e230dd2SCorentin Chary 119304d2529dSDaniel P. Berrange if (errp) { 119404d2529dSDaniel P. Berrange error_free(*errp); 119504d2529dSDaniel P. Berrange *errp = NULL; 119604d2529dSDaniel P. Berrange } 11973e230dd2SCorentin Chary return 0; 11983e230dd2SCorentin Chary } 11993e230dd2SCorentin Chary return ret; 12003e230dd2SCorentin Chary } 12013e230dd2SCorentin Chary 12023e230dd2SCorentin Chary 12033e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12043e230dd2SCorentin Chary { 12053e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12063e230dd2SCorentin Chary vnc_disconnect_start(vs); 12073e230dd2SCorentin Chary } 12083e230dd2SCorentin Chary 12093e305e4aSDaniel P. Berrange 12103e230dd2SCorentin Chary /* 12113e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12123e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12133e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12143e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12153e230dd2SCorentin Chary * 12163e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12173e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12183e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12193e230dd2SCorentin Chary * is active 12203e230dd2SCorentin Chary * 12213e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12223e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12233e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12243e230dd2SCorentin Chary */ 1225fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12263e230dd2SCorentin Chary { 122704d2529dSDaniel P. Berrange Error *err = NULL; 1228fdd1ab6aSDaniel P. Berrange ssize_t ret; 122904d2529dSDaniel P. Berrange ret = qio_channel_write( 123004d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12313e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 123204d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12333e230dd2SCorentin Chary } 12343e230dd2SCorentin Chary 12353e230dd2SCorentin Chary 12363e230dd2SCorentin Chary /* 12373e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12383e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12393e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12403e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12413e230dd2SCorentin Chary * 12423e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12433e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 12443e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12453e230dd2SCorentin Chary */ 1246fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 12473e230dd2SCorentin Chary { 1248fdd1ab6aSDaniel P. Berrange ssize_t ret; 12493e230dd2SCorentin Chary 12503e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12513e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 12523e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 12533e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 12543e230dd2SCorentin Chary 12553e230dd2SCorentin Chary if (vs->sasl.conn && 12563e230dd2SCorentin Chary vs->sasl.runSSF && 12573e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 12583e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 12593e230dd2SCorentin Chary if (ret) 12603e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 12613e230dd2SCorentin Chary } else 12623e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12633e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 12643e230dd2SCorentin Chary if (!ret) 12653e230dd2SCorentin Chary return 0; 12663e230dd2SCorentin Chary 126732ed2680STim Hardeck buffer_advance(&vs->output, ret); 12683e230dd2SCorentin Chary 12693e230dd2SCorentin Chary if (vs->output.offset == 0) { 127004d2529dSDaniel P. Berrange if (vs->ioc_tag) { 127104d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 127204d2529dSDaniel P. Berrange } 127304d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 127404d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 12753e230dd2SCorentin Chary } 12763e230dd2SCorentin Chary 12773e230dd2SCorentin Chary return ret; 12783e230dd2SCorentin Chary } 12793e230dd2SCorentin Chary 12803e230dd2SCorentin Chary 12813e230dd2SCorentin Chary /* 12823e230dd2SCorentin Chary * First function called whenever there is data to be written to 12833e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12843e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12853e230dd2SCorentin Chary */ 128604d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 12873e230dd2SCorentin Chary { 12883e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12893e230dd2SCorentin Chary if (vs->sasl.conn && 12903e230dd2SCorentin Chary vs->sasl.runSSF && 12913e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12923e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12933e230dd2SCorentin Chary } else 12943e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12957536ee4bSTim Hardeck { 12963e230dd2SCorentin Chary vnc_client_write_plain(vs); 12973e230dd2SCorentin Chary } 12987536ee4bSTim Hardeck } 12993e230dd2SCorentin Chary 130004d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1301bd023f95SCorentin Chary { 1302bd023f95SCorentin Chary 1303bd023f95SCorentin Chary vnc_lock_output(vs); 1304d5f04223SDaniel P. Berrange if (vs->output.offset) { 130504d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 130604d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 130704d2529dSDaniel P. Berrange if (vs->ioc_tag) { 130804d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 130904d2529dSDaniel P. Berrange } 131004d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 131104d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1312bd023f95SCorentin Chary } 1313bd023f95SCorentin Chary vnc_unlock_output(vs); 1314bd023f95SCorentin Chary } 1315bd023f95SCorentin Chary 13163e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13173e230dd2SCorentin Chary { 13183e230dd2SCorentin Chary vs->read_handler = func; 13193e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13203e230dd2SCorentin Chary } 13213e230dd2SCorentin Chary 13223e230dd2SCorentin Chary 13233e230dd2SCorentin Chary /* 13243e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13253e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13263e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13273e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13283e230dd2SCorentin Chary * 13293e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13303e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13313e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13323e230dd2SCorentin Chary * is active 13333e230dd2SCorentin Chary * 13343e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13353e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13363e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13373e230dd2SCorentin Chary */ 1338fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13393e230dd2SCorentin Chary { 1340fdd1ab6aSDaniel P. Berrange ssize_t ret; 134104d2529dSDaniel P. Berrange Error *err = NULL; 134204d2529dSDaniel P. Berrange ret = qio_channel_read( 134304d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 13443e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 134504d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 13463e230dd2SCorentin Chary } 13473e230dd2SCorentin Chary 13483e230dd2SCorentin Chary 13493e230dd2SCorentin Chary /* 13503e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 13513e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 13523e230dd2SCorentin Chary * data as possible without blocking. 13533e230dd2SCorentin Chary * 13543e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 13553e230dd2SCorentin Chary * disconnects the client socket. 13563e230dd2SCorentin Chary */ 1357fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 13583e230dd2SCorentin Chary { 1359fdd1ab6aSDaniel P. Berrange ssize_t ret; 13603e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13613e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13623e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13633e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13643e230dd2SCorentin Chary if (!ret) 13653e230dd2SCorentin Chary return 0; 13663e230dd2SCorentin Chary vs->input.offset += ret; 13673e230dd2SCorentin Chary return ret; 13683e230dd2SCorentin Chary } 13693e230dd2SCorentin Chary 1370175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1371175b2a6eSCorentin Chary { 1372175b2a6eSCorentin Chary VncState *vs = opaque; 1373175b2a6eSCorentin Chary 1374175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1375175b2a6eSCorentin Chary } 13763e230dd2SCorentin Chary 13773e230dd2SCorentin Chary /* 13783e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13793e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13803e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 1381ea697449SDaniel P. Berrange * Returns 0 on success, -1 if client disconnected 13823e230dd2SCorentin Chary */ 1383ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs) 13843e230dd2SCorentin Chary { 1385fdd1ab6aSDaniel P. Berrange ssize_t ret; 13863e230dd2SCorentin Chary 13873e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13883e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13893e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13903e230dd2SCorentin Chary else 13913e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13923e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13933e230dd2SCorentin Chary if (!ret) { 139404d2529dSDaniel P. Berrange if (vs->disconnecting) { 13953e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1396ea697449SDaniel P. Berrange return -1; 139704d2529dSDaniel P. Berrange } 1398ea697449SDaniel P. Berrange return 0; 13993e230dd2SCorentin Chary } 14003e230dd2SCorentin Chary 14013e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14023e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14033e230dd2SCorentin Chary int ret; 14043e230dd2SCorentin Chary 14053e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 140604d2529dSDaniel P. Berrange if (vs->disconnecting) { 14073e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1408ea697449SDaniel P. Berrange return -1; 14093e230dd2SCorentin Chary } 14103e230dd2SCorentin Chary 14113e230dd2SCorentin Chary if (!ret) { 141232ed2680STim Hardeck buffer_advance(&vs->input, len); 14133e230dd2SCorentin Chary } else { 14143e230dd2SCorentin Chary vs->read_handler_expect = ret; 14153e230dd2SCorentin Chary } 14163e230dd2SCorentin Chary } 1417ea697449SDaniel P. Berrange return 0; 14183e230dd2SCorentin Chary } 14193e230dd2SCorentin Chary 142004d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 142104d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 142204d2529dSDaniel P. Berrange { 142304d2529dSDaniel P. Berrange VncState *vs = opaque; 142404d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 1425ea697449SDaniel P. Berrange if (vnc_client_read(vs) < 0) { 1426ea697449SDaniel P. Berrange return TRUE; 1427ea697449SDaniel P. Berrange } 142804d2529dSDaniel P. Berrange } 142904d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 143004d2529dSDaniel P. Berrange vnc_client_write(vs); 143104d2529dSDaniel P. Berrange } 143204d2529dSDaniel P. Berrange return TRUE; 143304d2529dSDaniel P. Berrange } 143404d2529dSDaniel P. Berrange 143504d2529dSDaniel P. Berrange 14363e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14373e230dd2SCorentin Chary { 14383e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14393e230dd2SCorentin Chary 144004d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 144104d2529dSDaniel P. Berrange if (vs->ioc_tag) { 144204d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 144304d2529dSDaniel P. Berrange } 144404d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 144504d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14463e230dd2SCorentin Chary } 14473e230dd2SCorentin Chary 14483e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 14493e230dd2SCorentin Chary } 14503e230dd2SCorentin Chary 14513e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 14523e230dd2SCorentin Chary { 14533e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 14543e230dd2SCorentin Chary } 14553e230dd2SCorentin Chary 14563e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 14573e230dd2SCorentin Chary { 14583e230dd2SCorentin Chary uint8_t buf[4]; 14593e230dd2SCorentin Chary 14603e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 14613e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 14623e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 14633e230dd2SCorentin Chary buf[3] = value & 0xFF; 14643e230dd2SCorentin Chary 14653e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14663e230dd2SCorentin Chary } 14673e230dd2SCorentin Chary 14683e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14693e230dd2SCorentin Chary { 14703e230dd2SCorentin Chary uint8_t buf[2]; 14713e230dd2SCorentin Chary 14723e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14733e230dd2SCorentin Chary buf[1] = value & 0xFF; 14743e230dd2SCorentin Chary 14753e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14763e230dd2SCorentin Chary } 14773e230dd2SCorentin Chary 14783e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14793e230dd2SCorentin Chary { 14803e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14813e230dd2SCorentin Chary } 14823e230dd2SCorentin Chary 14833e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14843e230dd2SCorentin Chary { 1485bd023f95SCorentin Chary vnc_lock_output(vs); 1486d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1487bd023f95SCorentin Chary vnc_client_write_locked(vs); 1488bd023f95SCorentin Chary } 1489bd023f95SCorentin Chary vnc_unlock_output(vs); 14903e230dd2SCorentin Chary } 14913e230dd2SCorentin Chary 149271a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14933e230dd2SCorentin Chary { 14943e230dd2SCorentin Chary return data[offset]; 14953e230dd2SCorentin Chary } 14963e230dd2SCorentin Chary 149771a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 14983e230dd2SCorentin Chary { 14993e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15003e230dd2SCorentin Chary } 15013e230dd2SCorentin Chary 150271a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15033e230dd2SCorentin Chary { 15043e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15053e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15063e230dd2SCorentin Chary } 15073e230dd2SCorentin Chary 15083e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15093e230dd2SCorentin Chary { 15103e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15113e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15123e230dd2SCorentin Chary } 15133e230dd2SCorentin Chary 15143e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15153e230dd2SCorentin Chary { 15163e230dd2SCorentin Chary } 15173e230dd2SCorentin Chary 15189e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15193e230dd2SCorentin Chary { 15203e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 152114768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15223e230dd2SCorentin Chary 15233e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1524bd023f95SCorentin Chary vnc_lock_output(vs); 15253e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15263e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15273e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15283e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1529bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1530bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15313e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1532bd023f95SCorentin Chary vnc_unlock_output(vs); 15333e230dd2SCorentin Chary vnc_flush(vs); 15343e230dd2SCorentin Chary } 15353e230dd2SCorentin Chary vs->absolute = absolute; 15363e230dd2SCorentin Chary } 15373e230dd2SCorentin Chary 15383e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15393e230dd2SCorentin Chary { 15407fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 154114768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 154214768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 154314768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1544f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1545f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 154614768ebaSGerd Hoffmann }; 154714768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1548bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1549bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 15503e230dd2SCorentin Chary 155114768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 155214768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 155314768ebaSGerd Hoffmann vs->last_bmask = button_mask; 155414768ebaSGerd Hoffmann } 15553e230dd2SCorentin Chary 15563e230dd2SCorentin Chary if (vs->absolute) { 15579cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); 15589cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); 15593e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 156014768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 156114768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 15623e230dd2SCorentin Chary } else { 156314768ebaSGerd Hoffmann if (vs->last_x != -1) { 156414768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 156514768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 156614768ebaSGerd Hoffmann } 15673e230dd2SCorentin Chary vs->last_x = x; 15683e230dd2SCorentin Chary vs->last_y = y; 15693e230dd2SCorentin Chary } 157014768ebaSGerd Hoffmann qemu_input_event_sync(); 15713e230dd2SCorentin Chary } 15723e230dd2SCorentin Chary 15733e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15743e230dd2SCorentin Chary { 15753e230dd2SCorentin Chary int i; 15763e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15773e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15788d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 1579c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15803e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15813e230dd2SCorentin Chary } 15823e230dd2SCorentin Chary } 15833e230dd2SCorentin Chary } 15843e230dd2SCorentin Chary 15853e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15863e230dd2SCorentin Chary { 15873e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15888d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 1589c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15908d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1591c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15923e230dd2SCorentin Chary } 15933e230dd2SCorentin Chary 1594ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1595ab99e5c1SLei Li { 1596ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1597ab99e5c1SLei Li return; 1598ab99e5c1SLei Li } 1599ab99e5c1SLei Li 1600ab99e5c1SLei Li vnc_lock_output(vs); 1601ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1602ab99e5c1SLei Li vnc_write_u8(vs, 0); 1603ab99e5c1SLei Li vnc_write_u16(vs, 1); 1604ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1605a54f0d2bSPierre Ossman vnc_write_u8(vs, vs->vd->ledstate); 1606ab99e5c1SLei Li vnc_unlock_output(vs); 1607ab99e5c1SLei Li vnc_flush(vs); 1608ab99e5c1SLei Li } 1609ab99e5c1SLei Li 16103e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16113e230dd2SCorentin Chary { 1612a54f0d2bSPierre Ossman VncDisplay *vd = opaque; 1613a54f0d2bSPierre Ossman VncState *client; 16143e230dd2SCorentin Chary 161540066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 161640066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 161740066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 161840066175SGerd Hoffmann 1619a54f0d2bSPierre Ossman if (ledstate == vd->ledstate) { 1620a54f0d2bSPierre Ossman return; 162196f3d174SLei Li } 1622ab99e5c1SLei Li 1623a54f0d2bSPierre Ossman vd->ledstate = ledstate; 1624a54f0d2bSPierre Ossman 1625a54f0d2bSPierre Ossman QTAILQ_FOREACH(client, &vd->clients, next) { 1626a54f0d2bSPierre Ossman vnc_led_state_change(client); 1627ab99e5c1SLei Li } 16283e230dd2SCorentin Chary } 16293e230dd2SCorentin Chary 16303e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 16313e230dd2SCorentin Chary { 16323e230dd2SCorentin Chary /* QEMU console switch */ 16333e230dd2SCorentin Chary switch(keycode) { 16343e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16353e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16363e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16373e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16383e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16393e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16403e230dd2SCorentin Chary if (down) 16413e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 16423e230dd2SCorentin Chary else 16433e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 16443e230dd2SCorentin Chary break; 16453e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 16461d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 16471d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 16483e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 16493e230dd2SCorentin Chary reset_keys(vs); 16503e230dd2SCorentin Chary console_select(keycode - 0x02); 16513e230dd2SCorentin Chary return; 16523e230dd2SCorentin Chary } 16533e230dd2SCorentin Chary break; 16543e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 16553e230dd2SCorentin Chary case 0x45: /* NumLock */ 16563e230dd2SCorentin Chary if (down) 16573e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 16583e230dd2SCorentin Chary break; 16593e230dd2SCorentin Chary } 16603e230dd2SCorentin Chary 1661e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1662e7b2aaccSLei Li state extension. 1663e7b2aaccSLei Li */ 16649892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1665e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16663e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 16673e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 16683e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16693e230dd2SCorentin Chary toggles numlock away from the VNC window. 16703e230dd2SCorentin Chary */ 16713e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 16723e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 167340066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 16743e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 16753e230dd2SCorentin Chary press_key(vs, 0xff7f); 16763e230dd2SCorentin Chary } 16773e230dd2SCorentin Chary } else { 16783e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 167940066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 16803e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 16813e230dd2SCorentin Chary press_key(vs, 0xff7f); 16823e230dd2SCorentin Chary } 16833e230dd2SCorentin Chary } 16843e230dd2SCorentin Chary } 16853e230dd2SCorentin Chary 16869892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1687e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16883e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 16893e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 16903e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16913e230dd2SCorentin Chary toggles capslock away from the VNC window. 16923e230dd2SCorentin Chary */ 16933e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 16943e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 16953e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 16963e230dd2SCorentin Chary if (capslock) { 16973e230dd2SCorentin Chary if (uppercase == shift) { 169840066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 16993e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17003e230dd2SCorentin Chary press_key(vs, 0xffe5); 17013e230dd2SCorentin Chary } 17023e230dd2SCorentin Chary } else { 17033e230dd2SCorentin Chary if (uppercase != shift) { 170440066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17053e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17063e230dd2SCorentin Chary press_key(vs, 0xffe5); 17073e230dd2SCorentin Chary } 17083e230dd2SCorentin Chary } 17093e230dd2SCorentin Chary } 17103e230dd2SCorentin Chary 171181c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17128d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 1713c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 17143e230dd2SCorentin Chary } else { 1715e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1716e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1717e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17183e230dd2SCorentin Chary /* QEMU console emulation */ 17193e230dd2SCorentin Chary if (down) { 17203e230dd2SCorentin Chary switch (keycode) { 17213e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17223e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17233e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17243e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17253e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17263e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17273e230dd2SCorentin Chary break; 17283e230dd2SCorentin Chary case 0xc8: 17293e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 17303e230dd2SCorentin Chary break; 17313e230dd2SCorentin Chary case 0xd0: 17323e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 17333e230dd2SCorentin Chary break; 17343e230dd2SCorentin Chary case 0xcb: 17353e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 17363e230dd2SCorentin Chary break; 17373e230dd2SCorentin Chary case 0xcd: 17383e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 17393e230dd2SCorentin Chary break; 17403e230dd2SCorentin Chary case 0xd3: 17413e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 17423e230dd2SCorentin Chary break; 17433e230dd2SCorentin Chary case 0xc7: 17443e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 17453e230dd2SCorentin Chary break; 17463e230dd2SCorentin Chary case 0xcf: 17473e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 17483e230dd2SCorentin Chary break; 17493e230dd2SCorentin Chary case 0xc9: 17503e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 17513e230dd2SCorentin Chary break; 17523e230dd2SCorentin Chary case 0xd1: 17533e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 17543e230dd2SCorentin Chary break; 17553e230dd2SCorentin Chary 17563e230dd2SCorentin Chary case 0x47: 17573e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 17583e230dd2SCorentin Chary break; 17593e230dd2SCorentin Chary case 0x48: 17603e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 17613e230dd2SCorentin Chary break; 17623e230dd2SCorentin Chary case 0x49: 17633e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 17643e230dd2SCorentin Chary break; 17653e230dd2SCorentin Chary case 0x4b: 17663e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 17673e230dd2SCorentin Chary break; 17683e230dd2SCorentin Chary case 0x4c: 17693e230dd2SCorentin Chary kbd_put_keysym('5'); 17703e230dd2SCorentin Chary break; 17713e230dd2SCorentin Chary case 0x4d: 17723e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 17733e230dd2SCorentin Chary break; 17743e230dd2SCorentin Chary case 0x4f: 17753e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 17763e230dd2SCorentin Chary break; 17773e230dd2SCorentin Chary case 0x50: 17783e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 17793e230dd2SCorentin Chary break; 17803e230dd2SCorentin Chary case 0x51: 17813e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 17823e230dd2SCorentin Chary break; 17833e230dd2SCorentin Chary case 0x52: 17843e230dd2SCorentin Chary kbd_put_keysym('0'); 17853e230dd2SCorentin Chary break; 17863e230dd2SCorentin Chary case 0x53: 17873e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 17883e230dd2SCorentin Chary break; 17893e230dd2SCorentin Chary 17903e230dd2SCorentin Chary case 0xb5: 17913e230dd2SCorentin Chary kbd_put_keysym('/'); 17923e230dd2SCorentin Chary break; 17933e230dd2SCorentin Chary case 0x37: 17943e230dd2SCorentin Chary kbd_put_keysym('*'); 17953e230dd2SCorentin Chary break; 17963e230dd2SCorentin Chary case 0x4a: 17973e230dd2SCorentin Chary kbd_put_keysym('-'); 17983e230dd2SCorentin Chary break; 17993e230dd2SCorentin Chary case 0x4e: 18003e230dd2SCorentin Chary kbd_put_keysym('+'); 18013e230dd2SCorentin Chary break; 18023e230dd2SCorentin Chary case 0x9c: 18033e230dd2SCorentin Chary kbd_put_keysym('\n'); 18043e230dd2SCorentin Chary break; 18053e230dd2SCorentin Chary 18063e230dd2SCorentin Chary default: 1807e26437c2SGerd Hoffmann if (control) { 1808e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1809e26437c2SGerd Hoffmann } else { 18103e230dd2SCorentin Chary kbd_put_keysym(sym); 1811e26437c2SGerd Hoffmann } 18123e230dd2SCorentin Chary break; 18133e230dd2SCorentin Chary } 18143e230dd2SCorentin Chary } 18153e230dd2SCorentin Chary } 18163e230dd2SCorentin Chary } 18173e230dd2SCorentin Chary 18187bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18197bc9318bSGerd Hoffmann { 18207bc9318bSGerd Hoffmann static const int keycodes[] = { 18217bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 18227bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 18237bc9318bSGerd Hoffmann }; 18247bc9318bSGerd Hoffmann int i, keycode; 18257bc9318bSGerd Hoffmann 182681c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 18277bc9318bSGerd Hoffmann return; 18287bc9318bSGerd Hoffmann } 18297bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 18307bc9318bSGerd Hoffmann keycode = keycodes[i]; 18317bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 18327bc9318bSGerd Hoffmann continue; 18337bc9318bSGerd Hoffmann } 18348d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1835c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 18367bc9318bSGerd Hoffmann } 18377bc9318bSGerd Hoffmann } 18387bc9318bSGerd Hoffmann 183940066175SGerd Hoffmann static const char *code2name(int keycode) 184040066175SGerd Hoffmann { 1841977c736fSMarkus Armbruster return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); 184240066175SGerd Hoffmann } 184340066175SGerd Hoffmann 18443e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 18453e230dd2SCorentin Chary { 18463e230dd2SCorentin Chary int keycode; 18473e230dd2SCorentin Chary int lsym = sym; 18483e230dd2SCorentin Chary 184981c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 18503e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 18513e230dd2SCorentin Chary } 18523e230dd2SCorentin Chary 18533e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 185440066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 18553e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18563e230dd2SCorentin Chary } 18573e230dd2SCorentin Chary 18583e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 18593e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 18603e230dd2SCorentin Chary { 18613e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 186240066175SGerd Hoffmann if (keyboard_layout) { 18633e230dd2SCorentin Chary key_event(vs, down, sym); 186440066175SGerd Hoffmann } else { 186540066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 18663e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18673e230dd2SCorentin Chary } 186840066175SGerd Hoffmann } 18693e230dd2SCorentin Chary 18703e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1871bea60dd7SPeter Lieven int x, int y, int w, int h) 18723e230dd2SCorentin Chary { 1873bea60dd7SPeter Lieven if (incremental) { 1874fef1bbadSDaniel P. Berrange if (vs->update != VNC_STATE_UPDATE_FORCE) { 1875fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_INCREMENTAL; 18763e230dd2SCorentin Chary } 1877fef1bbadSDaniel P. Berrange } else { 1878fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_FORCE; 1879f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 18803e230dd2SCorentin Chary } 1881fef1bbadSDaniel P. Berrange } 18823e230dd2SCorentin Chary 18833e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18843e230dd2SCorentin Chary { 1885bd023f95SCorentin Chary vnc_lock_output(vs); 18863e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18873e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18883e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1889d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1890bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1891bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 18923e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1893bd023f95SCorentin Chary vnc_unlock_output(vs); 18943e230dd2SCorentin Chary vnc_flush(vs); 18953e230dd2SCorentin Chary } 18963e230dd2SCorentin Chary 18973e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 18983e230dd2SCorentin Chary { 1899bd023f95SCorentin Chary vnc_lock_output(vs); 19003e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19013e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19023e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1903d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1904bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1905bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19063e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1907bd023f95SCorentin Chary vnc_unlock_output(vs); 19083e230dd2SCorentin Chary vnc_flush(vs); 19093e230dd2SCorentin Chary } 19103e230dd2SCorentin Chary 19113e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19123e230dd2SCorentin Chary { 19133e230dd2SCorentin Chary int i; 19143e230dd2SCorentin Chary unsigned int enc = 0; 19153e230dd2SCorentin Chary 19163e230dd2SCorentin Chary vs->features = 0; 19173e230dd2SCorentin Chary vs->vnc_encoding = 0; 1918d1af0e05SCorentin Chary vs->tight.compression = 9; 1919d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19203e230dd2SCorentin Chary vs->absolute = -1; 19213e230dd2SCorentin Chary 19223e230dd2SCorentin Chary /* 19233e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1924e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 19253e230dd2SCorentin Chary * will be set at the end of the loop. 19263e230dd2SCorentin Chary */ 19273e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 19283e230dd2SCorentin Chary enc = encodings[i]; 19293e230dd2SCorentin Chary switch (enc) { 19303e230dd2SCorentin Chary case VNC_ENCODING_RAW: 19313e230dd2SCorentin Chary vs->vnc_encoding = enc; 19323e230dd2SCorentin Chary break; 19333e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 19343e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 19353e230dd2SCorentin Chary break; 19363e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 19373e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 19383e230dd2SCorentin Chary vs->vnc_encoding = enc; 19393e230dd2SCorentin Chary break; 19403e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 19413e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 19423e230dd2SCorentin Chary vs->vnc_encoding = enc; 19433e230dd2SCorentin Chary break; 1944fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1945efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1946efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1947efe556adSCorentin Chary vs->vnc_encoding = enc; 1948efe556adSCorentin Chary break; 1949fe3e7f2dSJoel Martin #endif 19503e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 19513e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 19523e230dd2SCorentin Chary vs->vnc_encoding = enc; 19533e230dd2SCorentin Chary break; 1954148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1955148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1956148954faSCorentin Chary vs->vnc_encoding = enc; 1957148954faSCorentin Chary break; 1958148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1959148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1960148954faSCorentin Chary vs->vnc_encoding = enc; 1961148954faSCorentin Chary break; 19623e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 19633e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 19643e230dd2SCorentin Chary break; 19653e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 19663e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 19673e230dd2SCorentin Chary break; 19683e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 19693e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 197091ec41dcSFrediano Ziglio if (vs->vd->cursor) { 197191ec41dcSFrediano Ziglio vnc_cursor_define(vs); 197291ec41dcSFrediano Ziglio } 19733e230dd2SCorentin Chary break; 19743e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 19753e230dd2SCorentin Chary send_ext_key_event_ack(vs); 19763e230dd2SCorentin Chary break; 19773e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 19783e230dd2SCorentin Chary send_ext_audio_ack(vs); 19793e230dd2SCorentin Chary break; 19803e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19813e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19823e230dd2SCorentin Chary break; 1983ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 1984ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 1985ab99e5c1SLei Li break; 19863e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1987d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 19883e230dd2SCorentin Chary break; 19893e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1990b31f519eSCorentin Chary if (vs->vd->lossy) { 1991d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1992b31f519eSCorentin Chary } 19933e230dd2SCorentin Chary break; 19943e230dd2SCorentin Chary default: 19953e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 19963e230dd2SCorentin Chary break; 19973e230dd2SCorentin Chary } 19983e230dd2SCorentin Chary } 19993e230dd2SCorentin Chary vnc_desktop_resize(vs); 20009e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2001ab99e5c1SLei Li vnc_led_state_change(vs); 20023e230dd2SCorentin Chary } 20033e230dd2SCorentin Chary 20043e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20053e230dd2SCorentin Chary { 20069f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20079f64916dSGerd Hoffmann 20089f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20093e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20103e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20113e230dd2SCorentin Chary } else { 20123e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20133e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20143e230dd2SCorentin Chary } 20153e230dd2SCorentin Chary } 20163e230dd2SCorentin Chary 20170c426e45SAlexander Graf static void send_color_map(VncState *vs) 20180c426e45SAlexander Graf { 20190c426e45SAlexander Graf int i; 20200c426e45SAlexander Graf 20210c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 20220c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 20230c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 20240c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 20250c426e45SAlexander Graf 20260c426e45SAlexander Graf for (i = 0; i < 256; i++) { 20270c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 20280c426e45SAlexander Graf 20290c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 20300c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 20310c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 20320c426e45SAlexander Graf } 20330c426e45SAlexander Graf } 20340c426e45SAlexander Graf 2035ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 20363e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20373e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20383e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20393e230dd2SCorentin Chary { 20403e230dd2SCorentin Chary if (!true_color_flag) { 20410c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 20420c426e45SAlexander Graf bits_per_pixel = 8; 20430c426e45SAlexander Graf red_max = 7; 20440c426e45SAlexander Graf green_max = 7; 20450c426e45SAlexander Graf blue_max = 3; 20460c426e45SAlexander Graf red_shift = 0; 20470c426e45SAlexander Graf green_shift = 3; 20480c426e45SAlexander Graf blue_shift = 6; 20493e230dd2SCorentin Chary } 20503e230dd2SCorentin Chary 2051e6908bfeSPetr Matousek switch (bits_per_pixel) { 2052e6908bfeSPetr Matousek case 8: 2053e6908bfeSPetr Matousek case 16: 2054e6908bfeSPetr Matousek case 32: 2055e6908bfeSPetr Matousek break; 2056e6908bfeSPetr Matousek default: 2057e6908bfeSPetr Matousek vnc_client_error(vs); 2058e6908bfeSPetr Matousek return; 2059e6908bfeSPetr Matousek } 2060e6908bfeSPetr Matousek 20614c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 20627c9209e7SCédric Le Goater vs->client_pf.rbits = ctpopl(red_max); 20639f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 20649f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 20654c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 20667c9209e7SCédric Le Goater vs->client_pf.gbits = ctpopl(green_max); 20679f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 20689f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 20694c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 20707c9209e7SCédric Le Goater vs->client_pf.bbits = ctpopl(blue_max); 20719f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 20729f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 20739f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 20749f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 20759f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 20769f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 20773e230dd2SCorentin Chary 20780c426e45SAlexander Graf if (!true_color_flag) { 20790c426e45SAlexander Graf send_color_map(vs); 20800c426e45SAlexander Graf } 20810c426e45SAlexander Graf 20823e230dd2SCorentin Chary set_pixel_conversion(vs); 20833e230dd2SCorentin Chary 20841d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 20851d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 20863e230dd2SCorentin Chary } 20873e230dd2SCorentin Chary 20883e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 20893e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 20903e230dd2SCorentin Chary 20919f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 20929f64916dSGerd Hoffmann 20939f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 20949f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 20953e230dd2SCorentin Chary 20963e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 20973e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 20983e230dd2SCorentin Chary #else 20993e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21003e230dd2SCorentin Chary #endif 21013e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21029f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21039f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21049f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21059f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21069f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21079f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21089f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21093e230dd2SCorentin Chary 21103e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21113e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21123e230dd2SCorentin Chary } 21133e230dd2SCorentin Chary 21143e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21153e230dd2SCorentin Chary { 21163e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 21173e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2118bd023f95SCorentin Chary vnc_lock_output(vs); 21193e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 21203e230dd2SCorentin Chary vnc_write_u8(vs, 0); 21213e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2122d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2123bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2124bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2125d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 21263e230dd2SCorentin Chary pixel_format_message(vs); 2127bd023f95SCorentin Chary vnc_unlock_output(vs); 21283e230dd2SCorentin Chary vnc_flush(vs); 21293e230dd2SCorentin Chary } else { 21303e230dd2SCorentin Chary set_pixel_conversion(vs); 21313e230dd2SCorentin Chary } 21323e230dd2SCorentin Chary } 21333e230dd2SCorentin Chary 21343e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 21353e230dd2SCorentin Chary { 21363e230dd2SCorentin Chary int i; 21373e230dd2SCorentin Chary uint16_t limit; 21383e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21393e230dd2SCorentin Chary 21403e230dd2SCorentin Chary if (data[0] > 3) { 21410f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21423e230dd2SCorentin Chary } 21433e230dd2SCorentin Chary 21443e230dd2SCorentin Chary switch (data[0]) { 21453e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21463e230dd2SCorentin Chary if (len == 1) 21473e230dd2SCorentin Chary return 20; 21483e230dd2SCorentin Chary 2149ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 21503e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 21513e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 21523e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 21533e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 21543e230dd2SCorentin Chary break; 21553e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 21563e230dd2SCorentin Chary if (len == 1) 21573e230dd2SCorentin Chary return 4; 21583e230dd2SCorentin Chary 21593e230dd2SCorentin Chary if (len == 4) { 21603e230dd2SCorentin Chary limit = read_u16(data, 2); 21613e230dd2SCorentin Chary if (limit > 0) 21623e230dd2SCorentin Chary return 4 + (limit * 4); 21633e230dd2SCorentin Chary } else 21643e230dd2SCorentin Chary limit = read_u16(data, 2); 21653e230dd2SCorentin Chary 21663e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 21673e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 21683e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 21693e230dd2SCorentin Chary } 21703e230dd2SCorentin Chary 21713e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 21723e230dd2SCorentin Chary break; 21733e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 21743e230dd2SCorentin Chary if (len == 1) 21753e230dd2SCorentin Chary return 10; 21763e230dd2SCorentin Chary 21773e230dd2SCorentin Chary framebuffer_update_request(vs, 21783e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 21793e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 21803e230dd2SCorentin Chary break; 21813e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 21823e230dd2SCorentin Chary if (len == 1) 21833e230dd2SCorentin Chary return 8; 21843e230dd2SCorentin Chary 21853e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 21863e230dd2SCorentin Chary break; 21873e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 21883e230dd2SCorentin Chary if (len == 1) 21893e230dd2SCorentin Chary return 6; 21903e230dd2SCorentin Chary 21913e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 21923e230dd2SCorentin Chary break; 21933e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2194f9a70e79SPeter Lieven if (len == 1) { 21953e230dd2SCorentin Chary return 8; 2196f9a70e79SPeter Lieven } 21973e230dd2SCorentin Chary if (len == 8) { 21983e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2199f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2200f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2201f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2202f9a70e79SPeter Lieven vnc_client_error(vs); 2203f9a70e79SPeter Lieven break; 2204f9a70e79SPeter Lieven } 2205f9a70e79SPeter Lieven if (dlen > 0) { 22063e230dd2SCorentin Chary return 8 + dlen; 22073e230dd2SCorentin Chary } 2208f9a70e79SPeter Lieven } 22093e230dd2SCorentin Chary 22103e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22113e230dd2SCorentin Chary break; 22123e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22133e230dd2SCorentin Chary if (len == 1) 22143e230dd2SCorentin Chary return 2; 22153e230dd2SCorentin Chary 22163e230dd2SCorentin Chary switch (read_u8(data, 1)) { 22173e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 22183e230dd2SCorentin Chary if (len == 2) 22193e230dd2SCorentin Chary return 12; 22203e230dd2SCorentin Chary 22213e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 22223e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 22233e230dd2SCorentin Chary break; 22243e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 22253e230dd2SCorentin Chary if (len == 2) 22263e230dd2SCorentin Chary return 4; 22273e230dd2SCorentin Chary 22283e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 22293e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 22303e230dd2SCorentin Chary audio_add(vs); 22313e230dd2SCorentin Chary break; 22323e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 22333e230dd2SCorentin Chary audio_del(vs); 22343e230dd2SCorentin Chary break; 22353e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22363e230dd2SCorentin Chary if (len == 4) 22373e230dd2SCorentin Chary return 10; 22383e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22393e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22403e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22413e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22423e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22433e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22443e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22453e230dd2SCorentin Chary default: 2246153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 22473e230dd2SCorentin Chary vnc_client_error(vs); 22483e230dd2SCorentin Chary break; 22493e230dd2SCorentin Chary } 22503e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 22513e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2252090fdc83SMarc-André Lureau VNC_DEBUG("Invalid audio channel count %d\n", 22533e230dd2SCorentin Chary read_u8(data, 5)); 22543e230dd2SCorentin Chary vnc_client_error(vs); 22553e230dd2SCorentin Chary break; 22563e230dd2SCorentin Chary } 22573e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 22583e230dd2SCorentin Chary break; 22593e230dd2SCorentin Chary default: 2260153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 22613e230dd2SCorentin Chary vnc_client_error(vs); 22623e230dd2SCorentin Chary break; 22633e230dd2SCorentin Chary } 22643e230dd2SCorentin Chary break; 22653e230dd2SCorentin Chary 22663e230dd2SCorentin Chary default: 2267153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 22683e230dd2SCorentin Chary vnc_client_error(vs); 22693e230dd2SCorentin Chary break; 22703e230dd2SCorentin Chary } 22713e230dd2SCorentin Chary break; 22723e230dd2SCorentin Chary default: 2273153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 22743e230dd2SCorentin Chary vnc_client_error(vs); 22753e230dd2SCorentin Chary break; 22763e230dd2SCorentin Chary } 22773e230dd2SCorentin Chary 22783e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22793e230dd2SCorentin Chary return 0; 22803e230dd2SCorentin Chary } 22813e230dd2SCorentin Chary 22823e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 22833e230dd2SCorentin Chary { 22843e230dd2SCorentin Chary char buf[1024]; 22858cf36489SGerd Hoffmann VncShareMode mode; 22863e230dd2SCorentin Chary int size; 22873e230dd2SCorentin Chary 22888cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 22898cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 22908cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 22918cf36489SGerd Hoffmann /* 22928cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 22938cf36489SGerd Hoffmann * 22948cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 22958cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 22968cf36489SGerd Hoffmann * reasons. 22978cf36489SGerd Hoffmann */ 22988cf36489SGerd Hoffmann break; 22998cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23008cf36489SGerd Hoffmann /* 23018cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23028cf36489SGerd Hoffmann * 23038cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23048cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23058cf36489SGerd Hoffmann * as no exclusive connection exists. 23068cf36489SGerd Hoffmann * 23078cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23088cf36489SGerd Hoffmann */ 23098cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23108cf36489SGerd Hoffmann VncState *client; 23118cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23128cf36489SGerd Hoffmann if (vs == client) { 23138cf36489SGerd Hoffmann continue; 23148cf36489SGerd Hoffmann } 23158cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23168cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 23178cf36489SGerd Hoffmann continue; 23188cf36489SGerd Hoffmann } 23198cf36489SGerd Hoffmann vnc_disconnect_start(client); 23208cf36489SGerd Hoffmann } 23218cf36489SGerd Hoffmann } 23228cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 23238cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 23248cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23258cf36489SGerd Hoffmann return 0; 23268cf36489SGerd Hoffmann } 23278cf36489SGerd Hoffmann } 23288cf36489SGerd Hoffmann break; 23298cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 23308cf36489SGerd Hoffmann /* 23318cf36489SGerd Hoffmann * Policy: Shared connects only. 23328cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 23338cf36489SGerd Hoffmann * 23348cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 23358cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23368cf36489SGerd Hoffmann * client disconnect everybody else. 23378cf36489SGerd Hoffmann */ 23388cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23398cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23408cf36489SGerd Hoffmann return 0; 23418cf36489SGerd Hoffmann } 23428cf36489SGerd Hoffmann break; 23438cf36489SGerd Hoffmann } 23448cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23458cf36489SGerd Hoffmann 2346e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2347e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2348e5f34cddSGerd Hoffmann return 0; 2349e5f34cddSGerd Hoffmann } 2350e5f34cddSGerd Hoffmann 2351bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2352bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 23533e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 23543e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 23553e230dd2SCorentin Chary 23563e230dd2SCorentin Chary pixel_format_message(vs); 23573e230dd2SCorentin Chary 235897efe4f9SThomas Huth if (qemu_name) { 23593e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 236097efe4f9SThomas Huth if (size > sizeof(buf)) { 236197efe4f9SThomas Huth size = sizeof(buf); 236297efe4f9SThomas Huth } 236397efe4f9SThomas Huth } else { 23643e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 236597efe4f9SThomas Huth } 23663e230dd2SCorentin Chary 23673e230dd2SCorentin Chary vnc_write_u32(vs, size); 23683e230dd2SCorentin Chary vnc_write(vs, buf, size); 23693e230dd2SCorentin Chary vnc_flush(vs); 23703e230dd2SCorentin Chary 23713e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2372fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 23733e230dd2SCorentin Chary 23743e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23753e230dd2SCorentin Chary 23763e230dd2SCorentin Chary return 0; 23773e230dd2SCorentin Chary } 23783e230dd2SCorentin Chary 23793e230dd2SCorentin Chary void start_client_init(VncState *vs) 23803e230dd2SCorentin Chary { 23813e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 23823e230dd2SCorentin Chary } 23833e230dd2SCorentin Chary 23843e230dd2SCorentin Chary static void make_challenge(VncState *vs) 23853e230dd2SCorentin Chary { 23863e230dd2SCorentin Chary int i; 23873e230dd2SCorentin Chary 23883e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 23893e230dd2SCorentin Chary 23903e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 23913e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 23923e230dd2SCorentin Chary } 23933e230dd2SCorentin Chary 23943e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 23953e230dd2SCorentin Chary { 23963e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2397800567a6SDaniel P. Berrange size_t i, pwlen; 23983e230dd2SCorentin Chary unsigned char key[8]; 23993c9405a0SGerd Hoffmann time_t now = time(NULL); 240060928458SGonglei QCryptoCipher *cipher = NULL; 2401800567a6SDaniel P. Berrange Error *err = NULL; 24023e230dd2SCorentin Chary 24031cd20f8bSAnthony Liguori if (!vs->vd->password) { 24047364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); 24056bffdf0fSGerd Hoffmann goto reject; 24063e230dd2SCorentin Chary } 24073c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 24087364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); 24093c9405a0SGerd Hoffmann goto reject; 24103c9405a0SGerd Hoffmann } 24113e230dd2SCorentin Chary 24123e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24133e230dd2SCorentin Chary 24143e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24153e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24163e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24173e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2418800567a6SDaniel P. Berrange 2419800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2420800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2421800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2422800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2423800567a6SDaniel P. Berrange &err); 2424800567a6SDaniel P. Berrange if (!cipher) { 24257364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", 2426800567a6SDaniel P. Berrange error_get_pretty(err)); 2427800567a6SDaniel P. Berrange error_free(err); 2428800567a6SDaniel P. Berrange goto reject; 2429800567a6SDaniel P. Berrange } 2430800567a6SDaniel P. Berrange 2431a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2432800567a6SDaniel P. Berrange vs->challenge, 2433800567a6SDaniel P. Berrange response, 2434800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2435800567a6SDaniel P. Berrange &err) < 0) { 24367364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", 2437800567a6SDaniel P. Berrange error_get_pretty(err)); 2438800567a6SDaniel P. Berrange error_free(err); 2439800567a6SDaniel P. Berrange goto reject; 2440800567a6SDaniel P. Berrange } 24413e230dd2SCorentin Chary 24423e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 24433e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 24447364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); 24456bffdf0fSGerd Hoffmann goto reject; 24466bffdf0fSGerd Hoffmann } else { 24477364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 24486bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 24496bffdf0fSGerd Hoffmann vnc_flush(vs); 24506bffdf0fSGerd Hoffmann 24516bffdf0fSGerd Hoffmann start_client_init(vs); 24526bffdf0fSGerd Hoffmann } 245360928458SGonglei 245460928458SGonglei qcrypto_cipher_free(cipher); 24556bffdf0fSGerd Hoffmann return 0; 24566bffdf0fSGerd Hoffmann 24576bffdf0fSGerd Hoffmann reject: 24583e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 24593e230dd2SCorentin Chary if (vs->minor >= 8) { 24603e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24613e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24623e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24633e230dd2SCorentin Chary } 24643e230dd2SCorentin Chary vnc_flush(vs); 24653e230dd2SCorentin Chary vnc_client_error(vs); 246660928458SGonglei qcrypto_cipher_free(cipher); 24673e230dd2SCorentin Chary return 0; 24683e230dd2SCorentin Chary } 24693e230dd2SCorentin Chary 24703e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 24713e230dd2SCorentin Chary { 24723e230dd2SCorentin Chary make_challenge(vs); 24733e230dd2SCorentin Chary /* Send client a 'random' challenge */ 24743e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 24753e230dd2SCorentin Chary vnc_flush(vs); 24763e230dd2SCorentin Chary 24773e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 24783e230dd2SCorentin Chary } 24793e230dd2SCorentin Chary 24803e230dd2SCorentin Chary 24813e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 24823e230dd2SCorentin Chary { 24833e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 24843e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 24857e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 24867364dbdaSDaniel P. Berrange trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); 24873e230dd2SCorentin Chary vnc_write_u32(vs, 1); 24883e230dd2SCorentin Chary if (vs->minor >= 8) { 24893e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24903e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24913e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24923e230dd2SCorentin Chary } 24933e230dd2SCorentin Chary vnc_client_error(vs); 24943e230dd2SCorentin Chary } else { /* Accept requested auth */ 24957364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 24967e7e2ebcSDaniel P. Berrange switch (vs->auth) { 24973e230dd2SCorentin Chary case VNC_AUTH_NONE: 24983e230dd2SCorentin Chary if (vs->minor >= 8) { 24993e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25003e230dd2SCorentin Chary vnc_flush(vs); 25013e230dd2SCorentin Chary } 25027364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25033e230dd2SCorentin Chary start_client_init(vs); 25043e230dd2SCorentin Chary break; 25053e230dd2SCorentin Chary 25063e230dd2SCorentin Chary case VNC_AUTH_VNC: 25073e230dd2SCorentin Chary start_auth_vnc(vs); 25083e230dd2SCorentin Chary break; 25093e230dd2SCorentin Chary 25103e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25113e230dd2SCorentin Chary start_auth_vencrypt(vs); 25123e230dd2SCorentin Chary break; 25133e230dd2SCorentin Chary 25143e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25153e230dd2SCorentin Chary case VNC_AUTH_SASL: 25163e230dd2SCorentin Chary start_auth_sasl(vs); 25173e230dd2SCorentin Chary break; 25183e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 25193e230dd2SCorentin Chary 25203e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 25217364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); 25223e230dd2SCorentin Chary vnc_write_u8(vs, 1); 25233e230dd2SCorentin Chary if (vs->minor >= 8) { 25243e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25253e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25263e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25273e230dd2SCorentin Chary } 25283e230dd2SCorentin Chary vnc_client_error(vs); 25293e230dd2SCorentin Chary } 25303e230dd2SCorentin Chary } 25313e230dd2SCorentin Chary return 0; 25323e230dd2SCorentin Chary } 25333e230dd2SCorentin Chary 25343e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 25353e230dd2SCorentin Chary { 25363e230dd2SCorentin Chary char local[13]; 25373e230dd2SCorentin Chary 25383e230dd2SCorentin Chary memcpy(local, version, 12); 25393e230dd2SCorentin Chary local[12] = 0; 25403e230dd2SCorentin Chary 25413e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 25423e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 25433e230dd2SCorentin Chary vnc_client_error(vs); 25443e230dd2SCorentin Chary return 0; 25453e230dd2SCorentin Chary } 25463e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 25473e230dd2SCorentin Chary if (vs->major != 3 || 25483e230dd2SCorentin Chary (vs->minor != 3 && 25493e230dd2SCorentin Chary vs->minor != 4 && 25503e230dd2SCorentin Chary vs->minor != 5 && 25513e230dd2SCorentin Chary vs->minor != 7 && 25523e230dd2SCorentin Chary vs->minor != 8)) { 25533e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 25543e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25553e230dd2SCorentin Chary vnc_flush(vs); 25563e230dd2SCorentin Chary vnc_client_error(vs); 25573e230dd2SCorentin Chary return 0; 25583e230dd2SCorentin Chary } 25593e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 25603e230dd2SCorentin Chary * as equivalent to v3.3 by servers 25613e230dd2SCorentin Chary */ 25623e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 25633e230dd2SCorentin Chary vs->minor = 3; 25643e230dd2SCorentin Chary 25653e230dd2SCorentin Chary if (vs->minor == 3) { 25667364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25677e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 25687e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25693e230dd2SCorentin Chary vnc_flush(vs); 25707364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25713e230dd2SCorentin Chary start_client_init(vs); 25727e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 25733e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 25747e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25753e230dd2SCorentin Chary vnc_flush(vs); 25763e230dd2SCorentin Chary start_auth_vnc(vs); 25773e230dd2SCorentin Chary } else { 25787364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, 25797364dbdaSDaniel P. Berrange "Unsupported auth method for v3.3", ""); 25803e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25813e230dd2SCorentin Chary vnc_flush(vs); 25823e230dd2SCorentin Chary vnc_client_error(vs); 25833e230dd2SCorentin Chary } 25843e230dd2SCorentin Chary } else { 25853e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 25867e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 25873e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 25883e230dd2SCorentin Chary vnc_flush(vs); 25893e230dd2SCorentin Chary } 25903e230dd2SCorentin Chary 25913e230dd2SCorentin Chary return 0; 25923e230dd2SCorentin Chary } 25933e230dd2SCorentin Chary 2594999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2595999342a0SCorentin Chary { 2596999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2597999342a0SCorentin Chary 2598999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2599999342a0SCorentin Chary } 2600999342a0SCorentin Chary 26017d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26027d964c9dSCorentin Chary { 26037d964c9dSCorentin Chary int i, j; 26047d964c9dSCorentin Chary 26057d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26067d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26077d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26087d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26097d964c9dSCorentin Chary 2610207f328aSCorentin Chary for (j = y; j <= h; j++) { 2611207f328aSCorentin Chary for (i = x; i <= w; i++) { 26127d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26137d964c9dSCorentin Chary } 26147d964c9dSCorentin Chary } 26157d964c9dSCorentin Chary } 26167d964c9dSCorentin Chary 26177d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26187d964c9dSCorentin Chary { 26197d964c9dSCorentin Chary VncState *vs; 26207d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 26217d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 26227d964c9dSCorentin Chary int has_dirty = 0; 26237d964c9dSCorentin Chary 26245a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 26255a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 26267d964c9dSCorentin Chary 26277d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2628bc2429b9SCorentin Chary int j; 26297d964c9dSCorentin Chary 26307d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 26317d964c9dSCorentin Chary if (vs->output.offset) { 26327d964c9dSCorentin Chary continue; 26337d964c9dSCorentin Chary } 26347d964c9dSCorentin Chary 26357d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 26367d964c9dSCorentin Chary continue; 26377d964c9dSCorentin Chary } 2638207f328aSCorentin Chary 26397d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 26407d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2641b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2642b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2643b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 26447d964c9dSCorentin Chary } 26457d964c9dSCorentin Chary has_dirty++; 26467d964c9dSCorentin Chary } 2647207f328aSCorentin Chary 26487d964c9dSCorentin Chary return has_dirty; 26497d964c9dSCorentin Chary } 26507d964c9dSCorentin Chary 26517d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2652999342a0SCorentin Chary { 2653eebe0b79SGerd Hoffmann int width = MIN(pixman_image_get_width(vd->guest.fb), 2654eebe0b79SGerd Hoffmann pixman_image_get_width(vd->server)); 2655eebe0b79SGerd Hoffmann int height = MIN(pixman_image_get_height(vd->guest.fb), 2656eebe0b79SGerd Hoffmann pixman_image_get_height(vd->server)); 2657999342a0SCorentin Chary int x, y; 2658999342a0SCorentin Chary struct timeval res; 26597d964c9dSCorentin Chary int has_dirty = 0; 2660999342a0SCorentin Chary 26619f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26629f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2663999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2664999342a0SCorentin Chary 2665999342a0SCorentin Chary rect->updated = false; 2666999342a0SCorentin Chary } 2667999342a0SCorentin Chary } 2668999342a0SCorentin Chary 2669ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2670999342a0SCorentin Chary 2671999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 26727d964c9dSCorentin Chary return has_dirty; 2673999342a0SCorentin Chary } 2674999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2675999342a0SCorentin Chary 26769f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26779f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2678999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2679999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2680999342a0SCorentin Chary struct timeval min, max; 2681999342a0SCorentin Chary 2682999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2683999342a0SCorentin Chary continue ; 2684999342a0SCorentin Chary } 2685999342a0SCorentin Chary 2686999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2687ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2688999342a0SCorentin Chary 2689999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2690999342a0SCorentin Chary rect->freq = 0; 26917d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2692999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2693999342a0SCorentin Chary continue ; 2694999342a0SCorentin Chary } 2695999342a0SCorentin Chary 2696999342a0SCorentin Chary min = rect->times[rect->idx]; 2697999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2698ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2699999342a0SCorentin Chary 2700999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2701999342a0SCorentin Chary rect->freq /= count; 2702999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2703999342a0SCorentin Chary } 2704999342a0SCorentin Chary } 27057d964c9dSCorentin Chary return has_dirty; 2706999342a0SCorentin Chary } 2707999342a0SCorentin Chary 2708999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2709999342a0SCorentin Chary { 2710999342a0SCorentin Chary int i, j; 2711999342a0SCorentin Chary double total = 0; 2712999342a0SCorentin Chary int num = 0; 2713999342a0SCorentin Chary 27145a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 27155a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 2716999342a0SCorentin Chary 2717999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2718999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2719999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2720999342a0SCorentin Chary num++; 2721999342a0SCorentin Chary } 2722999342a0SCorentin Chary } 2723999342a0SCorentin Chary 2724999342a0SCorentin Chary if (num) { 2725999342a0SCorentin Chary return total / num; 2726999342a0SCorentin Chary } else { 2727999342a0SCorentin Chary return 0; 2728999342a0SCorentin Chary } 2729999342a0SCorentin Chary } 2730999342a0SCorentin Chary 2731999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2732999342a0SCorentin Chary { 2733999342a0SCorentin Chary VncRectStat *rect; 2734999342a0SCorentin Chary 2735999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2736999342a0SCorentin Chary if (rect->updated) { 2737999342a0SCorentin Chary return ; 2738999342a0SCorentin Chary } 2739999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2740999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2741999342a0SCorentin Chary rect->updated = true; 2742999342a0SCorentin Chary } 2743999342a0SCorentin Chary 27443e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 27453e230dd2SCorentin Chary { 2746bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2747bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2748bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2749bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2750eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 275112b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 27523e230dd2SCorentin Chary VncState *vs; 27533e230dd2SCorentin Chary int has_dirty = 0; 27549f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 27553e230dd2SCorentin Chary 275680e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2757999342a0SCorentin Chary 275880e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2759999342a0SCorentin Chary gettimeofday(&tv, NULL); 27607d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 276180e0c8c3SCorentin Chary } 2762999342a0SCorentin Chary 27633e230dd2SCorentin Chary /* 27643e230dd2SCorentin Chary * Walk through the guest dirty map. 27653e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 27663e230dd2SCorentin Chary * Update server dirty map. 27673e230dd2SCorentin Chary */ 2768bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2769eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2770eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2771bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2772bea60dd7SPeter Lieven server_stride); 27739f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 27749f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 27759f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 277612b316d4SPeter Lieven } else { 2777eb8934b0SGerd Hoffmann int guest_bpp = 2778eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 277912b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 278012b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2781659c90eeSMarc-André Lureau guest_ll = pixman_image_get_width(vd->guest.fb) * (DIV_ROUND_UP(guest_bpp, 8)); 27829f64916dSGerd Hoffmann } 2783eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 278412b316d4SPeter Lieven 278512b316d4SPeter Lieven for (;;) { 27863e230dd2SCorentin Chary int x; 278712b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 278812b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 278912b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 279012b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 279112b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 279212b316d4SPeter Lieven /* no more dirty bits */ 279312b316d4SPeter Lieven break; 279412b316d4SPeter Lieven } 279512b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 279612b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 279712b316d4SPeter Lieven 279812b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 27993e230dd2SCorentin Chary 28009f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2801bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28029f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28039f64916dSGerd Hoffmann } else { 280412b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28059f64916dSGerd Hoffmann } 280612b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28073e230dd2SCorentin Chary 280812b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 280912b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2810bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 281112b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28123e230dd2SCorentin Chary continue; 2813b4c85ddcSPeter Lieven } 2814eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2815eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2816bea60dd7SPeter Lieven } 2817eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2818bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28193e230dd2SCorentin Chary continue; 2820b4c85ddcSPeter Lieven } 2821bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 282212b316d4SPeter Lieven if (!vd->non_adaptive) { 282312b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 282412b316d4SPeter Lieven y, &tv); 282512b316d4SPeter Lieven } 28263e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 282712b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 28283e230dd2SCorentin Chary } 28293e230dd2SCorentin Chary has_dirty++; 28303e230dd2SCorentin Chary } 283112b316d4SPeter Lieven 283212b316d4SPeter Lieven y++; 28333e230dd2SCorentin Chary } 28349f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 28353e230dd2SCorentin Chary return has_dirty; 28363e230dd2SCorentin Chary } 28373e230dd2SCorentin Chary 28380f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 28393e230dd2SCorentin Chary { 28400f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 28413e230dd2SCorentin Chary VncState *vs, *vn; 28423e230dd2SCorentin Chary int has_dirty, rects = 0; 28433e230dd2SCorentin Chary 28449d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 28459d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 28469d6b2070SChenLiang return; 28479d6b2070SChenLiang } 28489d6b2070SChenLiang 28491d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 28503e230dd2SCorentin Chary 2851bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 28520f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2853bd023f95SCorentin Chary return; 2854bd023f95SCorentin Chary } 2855bd023f95SCorentin Chary 28563e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2857bd023f95SCorentin Chary vnc_unlock_display(vd); 28583e230dd2SCorentin Chary 28593e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 28606af998dbSDaniel P. Berrange rects += vnc_update_client(vs, has_dirty); 28613e230dd2SCorentin Chary /* vs might be free()ed here */ 28623e230dd2SCorentin Chary } 2863bd023f95SCorentin Chary 28643e230dd2SCorentin Chary if (has_dirty && rects) { 28650f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 28660f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 28670f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 28680f7b2864SGerd Hoffmann } 28693e230dd2SCorentin Chary } else { 28700f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 28710f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 28720f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 28733e230dd2SCorentin Chary } 28743e230dd2SCorentin Chary } 28753e230dd2SCorentin Chary } 28763e230dd2SCorentin Chary 287704d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 28782c8cf549SMichael Tokarev bool skipauth, bool websocket) 28793e230dd2SCorentin Chary { 2880fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 288190cd03a3SDaniel P. Berrange bool first_client = QTAILQ_EMPTY(&vd->clients); 28827d964c9dSCorentin Chary int i; 28837d964c9dSCorentin Chary 2884ad6374c4SDaniel P. Berrange trace_vnc_client_connect(vs, sioc); 288504d2529dSDaniel P. Berrange vs->sioc = sioc; 288604d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 288704d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 288804d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2889d616ccc5SGerd Hoffmann vs->vd = vd; 28907e7e2ebcSDaniel P. Berrange 289104d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 289204d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 289304d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2894543b9580SGerd Hoffmann 289504d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 289604d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 289704d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2898543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 289904d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2900543b9580SGerd Hoffmann #endif 2901543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 290204d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2903543b9580SGerd Hoffmann #endif 290404d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 290504d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 290604d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 290704d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2908543b9580SGerd Hoffmann 29097e7e2ebcSDaniel P. Berrange if (skipauth) { 29107e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29117e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29127e7e2ebcSDaniel P. Berrange } else { 2913f9148c8aSDaniel P. Berrange if (websocket) { 2914f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2915f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2916f9148c8aSDaniel P. Berrange } else { 29177e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29187e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29197e7e2ebcSDaniel P. Berrange } 2920f9148c8aSDaniel P. Berrange } 292104d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 292204d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 29237e7e2ebcSDaniel P. Berrange 29247267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 29257d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 2926fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 29277d964c9dSCorentin Chary } 29283e230dd2SCorentin Chary 292904d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 29300f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 293104d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 2932a75d6f07SBrandon Carpenter if (vs->ioc_tag) { 2933a75d6f07SBrandon Carpenter g_source_remove(vs->ioc_tag); 2934a75d6f07SBrandon Carpenter } 29357536ee4bSTim Hardeck if (websocket) { 29367536ee4bSTim Hardeck vs->websocket = 1; 293738e5756aSDaniel P. Berrange if (vd->tlscreds) { 293804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 293904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 29403e305e4aSDaniel P. Berrange } else { 294104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 29430057a0d5STim Hardeck } 294404d2529dSDaniel P. Berrange } else { 294504d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294604d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 29477536ee4bSTim Hardeck } 29483e230dd2SCorentin Chary 29493e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2950fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 29518cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 29523e230dd2SCorentin Chary 29533e230dd2SCorentin Chary vs->last_x = -1; 29543e230dd2SCorentin Chary vs->last_y = -1; 29553e230dd2SCorentin Chary 29563e230dd2SCorentin Chary vs->as.freq = 44100; 29573e230dd2SCorentin Chary vs->as.nchannels = 2; 29583e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 29593e230dd2SCorentin Chary vs->as.endianness = 0; 29603e230dd2SCorentin Chary 2961bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2962175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2963bd023f95SCorentin Chary 2964e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 2965c7628bffSGerd Hoffmann if (first_client) { 2966c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 2967c7628bffSGerd Hoffmann } 29683e230dd2SCorentin Chary 29691d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29703e230dd2SCorentin Chary 297190cd03a3SDaniel P. Berrange if (!vs->websocket) { 2972dbee9897SDaniel P. Berrange vnc_start_protocol(vs); 297390cd03a3SDaniel P. Berrange } 297490cd03a3SDaniel P. Berrange 297590cd03a3SDaniel P. Berrange if (vd->num_connecting > vd->connections_limit) { 297690cd03a3SDaniel P. Berrange QTAILQ_FOREACH(vs, &vd->clients, next) { 297790cd03a3SDaniel P. Berrange if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 297890cd03a3SDaniel P. Berrange vnc_disconnect_start(vs); 297990cd03a3SDaniel P. Berrange return; 298090cd03a3SDaniel P. Berrange } 298190cd03a3SDaniel P. Berrange } 298290cd03a3SDaniel P. Berrange } 298390cd03a3SDaniel P. Berrange } 298490cd03a3SDaniel P. Berrange 2985dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs) 298690cd03a3SDaniel P. Berrange { 29873e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 29883e230dd2SCorentin Chary vnc_flush(vs); 29893e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 29903e230dd2SCorentin Chary 29913e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 29923e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 29933e230dd2SCorentin Chary } 29943e230dd2SCorentin Chary 299504d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 299604d2529dSDaniel P. Berrange GIOCondition condition, 299704d2529dSDaniel P. Berrange void *opaque) 29983e230dd2SCorentin Chary { 2999bf01c179SDaniel P. Berrange VncDisplay *vd = opaque; 300004d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 300104d2529dSDaniel P. Berrange Error *err = NULL; 30024ee74fa7SDaniel P. Berrange bool isWebsock = false; 30034ee74fa7SDaniel P. Berrange size_t i; 30044ee74fa7SDaniel P. Berrange 30054ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30064ee74fa7SDaniel P. Berrange if (ioc == QIO_CHANNEL(vd->lwebsock[i])) { 30074ee74fa7SDaniel P. Berrange isWebsock = true; 30084ee74fa7SDaniel P. Berrange break; 30094ee74fa7SDaniel P. Berrange } 30104ee74fa7SDaniel P. Berrange } 30113e230dd2SCorentin Chary 301204d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 301304d2529dSDaniel P. Berrange if (sioc != NULL) { 301410bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), 30154ee74fa7SDaniel P. Berrange isWebsock ? "vnc-ws-server" : "vnc-server"); 301604d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 30174ee74fa7SDaniel P. Berrange vnc_connect(vd, sioc, false, isWebsock); 301804d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30198e9b0d24SDaniel P. Berrange } else { 302004d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 302104d2529dSDaniel P. Berrange error_free(err); 30227536ee4bSTim Hardeck } 30233e230dd2SCorentin Chary 302404d2529dSDaniel P. Berrange return TRUE; 30257536ee4bSTim Hardeck } 30267536ee4bSTim Hardeck 30277c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 30287c20b4a3SGerd Hoffmann .dpy_name = "vnc", 30290f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 30307c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3031c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 303234da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 30337c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 30347c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 30357c20b4a3SGerd Hoffmann }; 30367c20b4a3SGerd Hoffmann 303714f7143eSGerd Hoffmann void vnc_display_init(const char *id) 30383e230dd2SCorentin Chary { 3039bf01c179SDaniel P. Berrange VncDisplay *vd; 30404db14629SGerd Hoffmann 30414db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 30424db14629SGerd Hoffmann return; 30434db14629SGerd Hoffmann } 3044bf01c179SDaniel P. Berrange vd = g_malloc0(sizeof(*vd)); 30453e230dd2SCorentin Chary 3046bf01c179SDaniel P. Berrange vd->id = strdup(id); 3047bf01c179SDaniel P. Berrange QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); 30483e230dd2SCorentin Chary 3049bf01c179SDaniel P. Berrange QTAILQ_INIT(&vd->clients); 3050bf01c179SDaniel P. Berrange vd->expires = TIME_MAX; 30513e230dd2SCorentin Chary 305240066175SGerd Hoffmann if (keyboard_layout) { 305340066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 3054bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 305540066175SGerd Hoffmann } else { 3056bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 305740066175SGerd Hoffmann } 30583e230dd2SCorentin Chary 3059bf01c179SDaniel P. Berrange if (!vd->kbd_layout) { 30603e230dd2SCorentin Chary exit(1); 3061bf01c179SDaniel P. Berrange } 30623e230dd2SCorentin Chary 3063bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 3064bf01c179SDaniel P. Berrange vd->connections_limit = 32; 306512e29b16SDaniel P. Berrange 3066bf01c179SDaniel P. Berrange qemu_mutex_init(&vd->mutex); 3067bd023f95SCorentin Chary vnc_start_worker_thread(); 3068bd023f95SCorentin Chary 3069bf01c179SDaniel P. Berrange vd->dcl.ops = &dcl_ops; 3070bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 30713e230dd2SCorentin Chary } 30723e230dd2SCorentin Chary 30733e230dd2SCorentin Chary 3074bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd) 30753e230dd2SCorentin Chary { 30764ee74fa7SDaniel P. Berrange size_t i; 3077bf01c179SDaniel P. Berrange if (!vd) { 30783e230dd2SCorentin Chary return; 307904d2529dSDaniel P. Berrange } 3080bf01c179SDaniel P. Berrange vd->is_unix = false; 30814ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 30824ee74fa7SDaniel P. Berrange if (vd->lsock_tag[i]) { 30834ee74fa7SDaniel P. Berrange g_source_remove(vd->lsock_tag[i]); 30843e230dd2SCorentin Chary } 30854ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lsock[i])); 30864ee74fa7SDaniel P. Berrange } 30874ee74fa7SDaniel P. Berrange g_free(vd->lsock); 30884ee74fa7SDaniel P. Berrange g_free(vd->lsock_tag); 3089bf01c179SDaniel P. Berrange vd->lsock = NULL; 30902dc120beSGerd Hoffmann vd->lsock_tag = NULL; 30914ee74fa7SDaniel P. Berrange vd->nlsock = 0; 30924ee74fa7SDaniel P. Berrange 30934ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30944ee74fa7SDaniel P. Berrange if (vd->lwebsock_tag[i]) { 30954ee74fa7SDaniel P. Berrange g_source_remove(vd->lwebsock_tag[i]); 309604d2529dSDaniel P. Berrange } 30974ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lwebsock[i])); 30987536ee4bSTim Hardeck } 30994ee74fa7SDaniel P. Berrange g_free(vd->lwebsock); 31004ee74fa7SDaniel P. Berrange g_free(vd->lwebsock_tag); 3101bf01c179SDaniel P. Berrange vd->lwebsock = NULL; 31022dc120beSGerd Hoffmann vd->lwebsock_tag = NULL; 31034ee74fa7SDaniel P. Berrange vd->nlwebsock = 0; 31044ee74fa7SDaniel P. Berrange 3105bf01c179SDaniel P. Berrange vd->auth = VNC_AUTH_INVALID; 3106bf01c179SDaniel P. Berrange vd->subauth = VNC_AUTH_INVALID; 3107bf01c179SDaniel P. Berrange if (vd->tlscreds) { 3108bf01c179SDaniel P. Berrange object_unparent(OBJECT(vd->tlscreds)); 3109bf01c179SDaniel P. Berrange vd->tlscreds = NULL; 3110bf01c179SDaniel P. Berrange } 3111bf01c179SDaniel P. Berrange g_free(vd->tlsaclname); 3112bf01c179SDaniel P. Berrange vd->tlsaclname = NULL; 3113a54f0d2bSPierre Ossman if (vd->lock_key_sync) { 3114a54f0d2bSPierre Ossman qemu_remove_led_event_handler(vd->led); 31152dc120beSGerd Hoffmann vd->led = NULL; 3116a54f0d2bSPierre Ossman } 31173e230dd2SCorentin Chary } 31183e230dd2SCorentin Chary 311914f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31201cd20f8bSAnthony Liguori { 3121bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31221cd20f8bSAnthony Liguori 3123bf01c179SDaniel P. Berrange if (!vd) { 3124a6aa9d3eSLuiz Capitulino return -EINVAL; 31251cd20f8bSAnthony Liguori } 3126bf01c179SDaniel P. Berrange if (vd->auth == VNC_AUTH_NONE) { 3127cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 31287ea7d36eSMarkus Armbruster "password auth using '-vnc ${dpy},password'.\n"); 3129cf864569SGerd Hoffmann return -EINVAL; 31301cd20f8bSAnthony Liguori } 31311cd20f8bSAnthony Liguori 3132bf01c179SDaniel P. Berrange g_free(vd->password); 3133bf01c179SDaniel P. Berrange vd->password = g_strdup(password); 3134a6aa9d3eSLuiz Capitulino 3135a6aa9d3eSLuiz Capitulino return 0; 31363e230dd2SCorentin Chary } 31373e230dd2SCorentin Chary 313814f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31393c9405a0SGerd Hoffmann { 3140bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31413c9405a0SGerd Hoffmann 3142bf01c179SDaniel P. Berrange if (!vd) { 31431643f2b2SGerd Hoffmann return -EINVAL; 31441643f2b2SGerd Hoffmann } 31451643f2b2SGerd Hoffmann 3146bf01c179SDaniel P. Berrange vd->expires = expires; 31473c9405a0SGerd Hoffmann return 0; 31483c9405a0SGerd Hoffmann } 31493c9405a0SGerd Hoffmann 3150bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd) 31513e230dd2SCorentin Chary { 3152bd269ebcSMarkus Armbruster SocketAddress *addr; 315304d2529dSDaniel P. Berrange Error *err = NULL; 31543e230dd2SCorentin Chary 31554ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 31564ee74fa7SDaniel P. Berrange return; 31574ee74fa7SDaniel P. Berrange } 31584ee74fa7SDaniel P. Berrange 31594ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], &err); 316004d2529dSDaniel P. Berrange if (!addr) { 316133df7bf3SPaolo Bonzini return; 316204d2529dSDaniel P. Berrange } 316304d2529dSDaniel P. Berrange 3164bd269ebcSMarkus Armbruster if (addr->type != SOCKET_ADDRESS_TYPE_INET) { 3165bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 316633df7bf3SPaolo Bonzini return; 316704d2529dSDaniel P. Berrange } 316833df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3169bd269ebcSMarkus Armbruster addr->u.inet.host, 3170bd269ebcSMarkus Armbruster addr->u.inet.port); 3171bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 31723e230dd2SCorentin Chary } 31733e230dd2SCorentin Chary 31744db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 31754db14629SGerd Hoffmann .name = "vnc", 31764db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 31774db14629SGerd Hoffmann .implied_opt_name = "vnc", 31784db14629SGerd Hoffmann .desc = { 31794db14629SGerd Hoffmann { 31804db14629SGerd Hoffmann .name = "vnc", 31814db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31824db14629SGerd Hoffmann },{ 31834db14629SGerd Hoffmann .name = "websocket", 31844db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31854db14629SGerd Hoffmann },{ 31863e305e4aSDaniel P. Berrange .name = "tls-creds", 31873e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 31883e305e4aSDaniel P. Berrange },{ 31893e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 31904db14629SGerd Hoffmann .name = "x509", 31914db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31924db14629SGerd Hoffmann },{ 31934db14629SGerd Hoffmann .name = "share", 31944db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31954db14629SGerd Hoffmann },{ 31961d0d59feSGerd Hoffmann .name = "display", 31971d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 31981d0d59feSGerd Hoffmann },{ 31991d0d59feSGerd Hoffmann .name = "head", 32001d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32011d0d59feSGerd Hoffmann },{ 3202e5f34cddSGerd Hoffmann .name = "connections", 3203e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3204e5f34cddSGerd Hoffmann },{ 320588428b7aSGonglei .name = "to", 320688428b7aSGonglei .type = QEMU_OPT_NUMBER, 320788428b7aSGonglei },{ 320888428b7aSGonglei .name = "ipv4", 320988428b7aSGonglei .type = QEMU_OPT_BOOL, 321088428b7aSGonglei },{ 321188428b7aSGonglei .name = "ipv6", 321288428b7aSGonglei .type = QEMU_OPT_BOOL, 321388428b7aSGonglei },{ 32144db14629SGerd Hoffmann .name = "password", 32154db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32164db14629SGerd Hoffmann },{ 32174db14629SGerd Hoffmann .name = "reverse", 32184db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32194db14629SGerd Hoffmann },{ 32204db14629SGerd Hoffmann .name = "lock-key-sync", 32214db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32224db14629SGerd Hoffmann },{ 3223c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3224c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3225c5ce8333SGerd Hoffmann },{ 32264db14629SGerd Hoffmann .name = "sasl", 32274db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32284db14629SGerd Hoffmann },{ 32293e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32304db14629SGerd Hoffmann .name = "tls", 32314db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32324db14629SGerd Hoffmann },{ 32333e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32344db14629SGerd Hoffmann .name = "x509verify", 32358c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32364db14629SGerd Hoffmann },{ 32374db14629SGerd Hoffmann .name = "acl", 32384db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32394db14629SGerd Hoffmann },{ 32404db14629SGerd Hoffmann .name = "lossy", 32414db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32424db14629SGerd Hoffmann },{ 32434db14629SGerd Hoffmann .name = "non-adaptive", 32444db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32454db14629SGerd Hoffmann }, 32464db14629SGerd Hoffmann { /* end of list */ } 32474db14629SGerd Hoffmann }, 32484db14629SGerd Hoffmann }; 32494db14629SGerd Hoffmann 32500dd72e15SDaniel P. Berrange 32513e305e4aSDaniel P. Berrange static int 3252eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth, 3253eda24e18SDaniel P. Berrange int *subauth, 3254eda24e18SDaniel P. Berrange QCryptoTLSCreds *tlscreds, 32550dd72e15SDaniel P. Berrange bool password, 32560dd72e15SDaniel P. Berrange bool sasl, 32573e305e4aSDaniel P. Berrange bool websocket, 32583e305e4aSDaniel P. Berrange Error **errp) 32590dd72e15SDaniel P. Berrange { 32600dd72e15SDaniel P. Berrange /* 32610dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 32620dd72e15SDaniel P. Berrange * 32630dd72e15SDaniel P. Berrange * 1. none 32640dd72e15SDaniel P. Berrange * 2. vnc 32650dd72e15SDaniel P. Berrange * 3. sasl 32660dd72e15SDaniel P. Berrange * 32670dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 32680dd72e15SDaniel P. Berrange * 32690dd72e15SDaniel P. Berrange * 1. clear 32700dd72e15SDaniel P. Berrange * 2. tls 32710dd72e15SDaniel P. Berrange * 32720dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 32730dd72e15SDaniel P. Berrange * 32740dd72e15SDaniel P. Berrange * 1. anon 32750dd72e15SDaniel P. Berrange * 2. x509 32760dd72e15SDaniel P. Berrange * 32770dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 32780dd72e15SDaniel P. Berrange * 32790dd72e15SDaniel P. Berrange * 1. clear + none 32800dd72e15SDaniel P. Berrange * 2. clear + vnc 32810dd72e15SDaniel P. Berrange * 3. clear + sasl 32820dd72e15SDaniel P. Berrange * 4. tls + anon + none 32830dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 32840dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 32850dd72e15SDaniel P. Berrange * 7. tls + x509 + none 32860dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 32870dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 32880dd72e15SDaniel P. Berrange * 32890dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 32900dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 32910dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 32920dd72e15SDaniel P. Berrange * sub-auth types. 3293f9148c8aSDaniel P. Berrange * 3294f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3295f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3296f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3297f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3298f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3299f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3300f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3301f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3302f9148c8aSDaniel P. Berrange * 3303f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3304f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3305f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33060dd72e15SDaniel P. Berrange */ 3307eda24e18SDaniel P. Berrange if (websocket || !tlscreds) { 33080dd72e15SDaniel P. Berrange if (password) { 33090dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 3310eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VNC; 33110dd72e15SDaniel P. Berrange } else if (sasl) { 33120dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 3313eda24e18SDaniel P. Berrange *auth = VNC_AUTH_SASL; 33140dd72e15SDaniel P. Berrange } else { 33150dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 3316eda24e18SDaniel P. Berrange *auth = VNC_AUTH_NONE; 33170dd72e15SDaniel P. Berrange } 3318eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_INVALID; 3319f9148c8aSDaniel P. Berrange } else { 3320eda24e18SDaniel P. Berrange bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), 3321eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; 3322eda24e18SDaniel P. Berrange bool is_anon = object_dynamic_cast(OBJECT(tlscreds), 3323eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; 3324eda24e18SDaniel P. Berrange 3325eda24e18SDaniel P. Berrange if (!is_x509 && !is_anon) { 3326eda24e18SDaniel P. Berrange error_setg(errp, 3327eda24e18SDaniel P. Berrange "Unsupported TLS cred type %s", 3328eda24e18SDaniel P. Berrange object_get_typename(OBJECT(tlscreds))); 3329eda24e18SDaniel P. Berrange return -1; 3330eda24e18SDaniel P. Berrange } 3331eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VENCRYPT; 3332eda24e18SDaniel P. Berrange if (password) { 3333eda24e18SDaniel P. Berrange if (is_x509) { 3334eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 3335eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509VNC; 3336eda24e18SDaniel P. Berrange } else { 3337eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 3338eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSVNC; 3339eda24e18SDaniel P. Berrange } 3340eda24e18SDaniel P. Berrange 3341eda24e18SDaniel P. Berrange } else if (sasl) { 3342eda24e18SDaniel P. Berrange if (is_x509) { 3343eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 3344eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509SASL; 3345eda24e18SDaniel P. Berrange } else { 3346eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 3347eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSSASL; 3348eda24e18SDaniel P. Berrange } 3349eda24e18SDaniel P. Berrange } else { 3350eda24e18SDaniel P. Berrange if (is_x509) { 3351eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 3352eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509NONE; 3353eda24e18SDaniel P. Berrange } else { 3354eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 3355eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSNONE; 3356eda24e18SDaniel P. Berrange } 3357f9148c8aSDaniel P. Berrange } 33580dd72e15SDaniel P. Berrange } 33593e305e4aSDaniel P. Berrange return 0; 33600dd72e15SDaniel P. Berrange } 33610dd72e15SDaniel P. Berrange 33623e305e4aSDaniel P. Berrange 33633e305e4aSDaniel P. Berrange /* 33643e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 33653e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 33663e305e4aSDaniel P. Berrange */ 33673e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 33683e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 33693e305e4aSDaniel P. Berrange bool x509verify, 33703e305e4aSDaniel P. Berrange const char *dir, 33713e305e4aSDaniel P. Berrange const char *id, 33723e305e4aSDaniel P. Berrange Error **errp) 33733e305e4aSDaniel P. Berrange { 33743e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 33753e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 33763e305e4aSDaniel P. Berrange Object *creds; 33773e305e4aSDaniel P. Berrange Error *err = NULL; 33783e305e4aSDaniel P. Berrange 33793e305e4aSDaniel P. Berrange if (x509) { 33803e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 33813e305e4aSDaniel P. Berrange parent, 33823e305e4aSDaniel P. Berrange credsid, 33833e305e4aSDaniel P. Berrange &err, 33843e305e4aSDaniel P. Berrange "endpoint", "server", 33853e305e4aSDaniel P. Berrange "dir", dir, 33863e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 33873e305e4aSDaniel P. Berrange NULL); 33883e305e4aSDaniel P. Berrange } else { 33893e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 33903e305e4aSDaniel P. Berrange parent, 33913e305e4aSDaniel P. Berrange credsid, 33923e305e4aSDaniel P. Berrange &err, 33933e305e4aSDaniel P. Berrange "endpoint", "server", 33943e305e4aSDaniel P. Berrange NULL); 33953e305e4aSDaniel P. Berrange } 33963e305e4aSDaniel P. Berrange 33973e305e4aSDaniel P. Berrange g_free(credsid); 33983e305e4aSDaniel P. Berrange 33993e305e4aSDaniel P. Berrange if (err) { 34003e305e4aSDaniel P. Berrange error_propagate(errp, err); 34013e305e4aSDaniel P. Berrange return NULL; 34023e305e4aSDaniel P. Berrange } 34033e305e4aSDaniel P. Berrange 34043e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34053e305e4aSDaniel P. Berrange } 34063e305e4aSDaniel P. Berrange 34073e305e4aSDaniel P. Berrange 3408275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr, 3409275e0d61SDaniel P. Berrange bool websocket, 3410e5766eb4SGerd Hoffmann bool reverse, 3411275e0d61SDaniel P. Berrange int displaynum, 3412275e0d61SDaniel P. Berrange int to, 3413275e0d61SDaniel P. Berrange bool has_ipv4, 3414275e0d61SDaniel P. Berrange bool has_ipv6, 3415275e0d61SDaniel P. Berrange bool ipv4, 3416275e0d61SDaniel P. Berrange bool ipv6, 3417bd269ebcSMarkus Armbruster SocketAddress **retaddr, 3418275e0d61SDaniel P. Berrange Error **errp) 3419275e0d61SDaniel P. Berrange { 3420275e0d61SDaniel P. Berrange int ret = -1; 3421bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3422275e0d61SDaniel P. Berrange 3423bd269ebcSMarkus Armbruster addr = g_new0(SocketAddress, 1); 3424275e0d61SDaniel P. Berrange 3425275e0d61SDaniel P. Berrange if (strncmp(addrstr, "unix:", 5) == 0) { 3426bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_UNIX; 3427bd269ebcSMarkus Armbruster addr->u.q_unix.path = g_strdup(addrstr + 5); 3428275e0d61SDaniel P. Berrange 3429275e0d61SDaniel P. Berrange if (websocket) { 3430275e0d61SDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3431275e0d61SDaniel P. Berrange goto cleanup; 3432275e0d61SDaniel P. Berrange } 3433275e0d61SDaniel P. Berrange 3434275e0d61SDaniel P. Berrange if (to) { 3435275e0d61SDaniel P. Berrange error_setg(errp, "Port range not support with UNIX socket"); 3436275e0d61SDaniel P. Berrange goto cleanup; 3437275e0d61SDaniel P. Berrange } 3438275e0d61SDaniel P. Berrange ret = 0; 3439275e0d61SDaniel P. Berrange } else { 3440275e0d61SDaniel P. Berrange const char *port; 3441275e0d61SDaniel P. Berrange size_t hostlen; 3442275e0d61SDaniel P. Berrange unsigned long long baseport = 0; 3443275e0d61SDaniel P. Berrange InetSocketAddress *inet; 3444275e0d61SDaniel P. Berrange 3445275e0d61SDaniel P. Berrange port = strrchr(addrstr, ':'); 3446275e0d61SDaniel P. Berrange if (!port) { 3447275e0d61SDaniel P. Berrange if (websocket) { 3448275e0d61SDaniel P. Berrange hostlen = 0; 3449275e0d61SDaniel P. Berrange port = addrstr; 3450275e0d61SDaniel P. Berrange } else { 3451275e0d61SDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3452275e0d61SDaniel P. Berrange goto cleanup; 3453275e0d61SDaniel P. Berrange } 3454275e0d61SDaniel P. Berrange } else { 3455275e0d61SDaniel P. Berrange hostlen = port - addrstr; 3456275e0d61SDaniel P. Berrange port++; 3457275e0d61SDaniel P. Berrange if (*port == '\0') { 3458275e0d61SDaniel P. Berrange error_setg(errp, "vnc port cannot be empty"); 3459275e0d61SDaniel P. Berrange goto cleanup; 3460275e0d61SDaniel P. Berrange } 3461275e0d61SDaniel P. Berrange } 3462275e0d61SDaniel P. Berrange 3463bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_INET; 3464bd269ebcSMarkus Armbruster inet = &addr->u.inet; 3465275e0d61SDaniel P. Berrange if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { 3466275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr + 1, hostlen - 2); 3467275e0d61SDaniel P. Berrange } else { 3468275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr, hostlen); 3469275e0d61SDaniel P. Berrange } 3470275e0d61SDaniel P. Berrange /* plain VNC port is just an offset, for websocket 3471275e0d61SDaniel P. Berrange * port is absolute */ 3472275e0d61SDaniel P. Berrange if (websocket) { 3473275e0d61SDaniel P. Berrange if (g_str_equal(addrstr, "") || 3474275e0d61SDaniel P. Berrange g_str_equal(addrstr, "on")) { 3475396f935aSDaniel P. Berrange if (displaynum == -1) { 3476396f935aSDaniel P. Berrange error_setg(errp, "explicit websocket port is required"); 3477396f935aSDaniel P. Berrange goto cleanup; 3478396f935aSDaniel P. Berrange } 3479275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3480275e0d61SDaniel P. Berrange "%d", displaynum + 5700); 3481275e0d61SDaniel P. Berrange if (to) { 3482275e0d61SDaniel P. Berrange inet->has_to = true; 3483275e0d61SDaniel P. Berrange inet->to = to + 5700; 3484275e0d61SDaniel P. Berrange } 3485275e0d61SDaniel P. Berrange } else { 3486275e0d61SDaniel P. Berrange inet->port = g_strdup(port); 3487275e0d61SDaniel P. Berrange } 3488275e0d61SDaniel P. Berrange } else { 3489e5766eb4SGerd Hoffmann int offset = reverse ? 0 : 5900; 3490275e0d61SDaniel P. Berrange if (parse_uint_full(port, &baseport, 10) < 0) { 3491275e0d61SDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", port); 3492275e0d61SDaniel P. Berrange goto cleanup; 3493275e0d61SDaniel P. Berrange } 3494275e0d61SDaniel P. Berrange if (baseport > 65535 || 3495e5766eb4SGerd Hoffmann baseport + offset > 65535) { 3496275e0d61SDaniel P. Berrange error_setg(errp, "port %s out of range", port); 3497275e0d61SDaniel P. Berrange goto cleanup; 3498275e0d61SDaniel P. Berrange } 3499275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3500e5766eb4SGerd Hoffmann "%d", (int)baseport + offset); 3501275e0d61SDaniel P. Berrange 3502275e0d61SDaniel P. Berrange if (to) { 3503275e0d61SDaniel P. Berrange inet->has_to = true; 3504e5766eb4SGerd Hoffmann inet->to = to + offset; 3505275e0d61SDaniel P. Berrange } 3506275e0d61SDaniel P. Berrange } 3507275e0d61SDaniel P. Berrange 3508275e0d61SDaniel P. Berrange inet->ipv4 = ipv4; 3509275e0d61SDaniel P. Berrange inet->has_ipv4 = has_ipv4; 3510275e0d61SDaniel P. Berrange inet->ipv6 = ipv6; 3511275e0d61SDaniel P. Berrange inet->has_ipv6 = has_ipv6; 3512275e0d61SDaniel P. Berrange 3513275e0d61SDaniel P. Berrange ret = baseport; 3514275e0d61SDaniel P. Berrange } 3515275e0d61SDaniel P. Berrange 3516275e0d61SDaniel P. Berrange *retaddr = addr; 3517275e0d61SDaniel P. Berrange 3518275e0d61SDaniel P. Berrange cleanup: 3519275e0d61SDaniel P. Berrange if (ret < 0) { 3520bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 3521275e0d61SDaniel P. Berrange } 3522275e0d61SDaniel P. Berrange return ret; 3523275e0d61SDaniel P. Berrange } 3524275e0d61SDaniel P. Berrange 35259f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr, 35269f26f325SPhilippe Mathieu-Daudé size_t *retnsaddr) 35279f26f325SPhilippe Mathieu-Daudé { 35289f26f325SPhilippe Mathieu-Daudé size_t i; 35299f26f325SPhilippe Mathieu-Daudé 35309f26f325SPhilippe Mathieu-Daudé for (i = 0; i < *retnsaddr; i++) { 35319f26f325SPhilippe Mathieu-Daudé qapi_free_SocketAddress((*retsaddr)[i]); 35329f26f325SPhilippe Mathieu-Daudé } 35339f26f325SPhilippe Mathieu-Daudé g_free(*retsaddr); 35349f26f325SPhilippe Mathieu-Daudé 35359f26f325SPhilippe Mathieu-Daudé *retsaddr = NULL; 35369f26f325SPhilippe Mathieu-Daudé *retnsaddr = 0; 35379f26f325SPhilippe Mathieu-Daudé } 35389f26f325SPhilippe Mathieu-Daudé 3539275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts, 3540e5766eb4SGerd Hoffmann bool reverse, 3541bd269ebcSMarkus Armbruster SocketAddress ***retsaddr, 3542396f935aSDaniel P. Berrange size_t *retnsaddr, 3543bd269ebcSMarkus Armbruster SocketAddress ***retwsaddr, 3544396f935aSDaniel P. Berrange size_t *retnwsaddr, 3545275e0d61SDaniel P. Berrange Error **errp) 3546275e0d61SDaniel P. Berrange { 3547bd269ebcSMarkus Armbruster SocketAddress *saddr = NULL; 3548bd269ebcSMarkus Armbruster SocketAddress *wsaddr = NULL; 3549396f935aSDaniel P. Berrange QemuOptsIter addriter; 3550396f935aSDaniel P. Berrange const char *addr; 3551275e0d61SDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3552275e0d61SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 3553275e0d61SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 3554275e0d61SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3555275e0d61SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3556396f935aSDaniel P. Berrange int displaynum = -1; 3557396f935aSDaniel P. Berrange int ret = -1; 3558275e0d61SDaniel P. Berrange 3559275e0d61SDaniel P. Berrange *retsaddr = NULL; 3560396f935aSDaniel P. Berrange *retnsaddr = 0; 3561275e0d61SDaniel P. Berrange *retwsaddr = NULL; 3562396f935aSDaniel P. Berrange *retnwsaddr = 0; 3563275e0d61SDaniel P. Berrange 3564396f935aSDaniel P. Berrange addr = qemu_opt_get(opts, "vnc"); 3565396f935aSDaniel P. Berrange if (addr == NULL || g_str_equal(addr, "none")) { 3566396f935aSDaniel P. Berrange ret = 0; 3567396f935aSDaniel P. Berrange goto cleanup; 3568396f935aSDaniel P. Berrange } 3569396f935aSDaniel P. Berrange if (qemu_opt_get(opts, "websocket") && 3570275e0d61SDaniel P. Berrange !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3571275e0d61SDaniel P. Berrange error_setg(errp, 3572275e0d61SDaniel P. Berrange "SHA1 hash support is required for websockets"); 3573396f935aSDaniel P. Berrange goto cleanup; 3574275e0d61SDaniel P. Berrange } 3575275e0d61SDaniel P. Berrange 3576396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "vnc"); 3577396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3578396f935aSDaniel P. Berrange int rv; 3579e5766eb4SGerd Hoffmann rv = vnc_display_get_address(addr, false, reverse, 0, to, 3580275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3581275e0d61SDaniel P. Berrange ipv4, ipv6, 3582275e0d61SDaniel P. Berrange &saddr, errp); 3583396f935aSDaniel P. Berrange if (rv < 0) { 3584396f935aSDaniel P. Berrange goto cleanup; 3585275e0d61SDaniel P. Berrange } 3586396f935aSDaniel P. Berrange /* Historical compat - first listen address can be used 3587396f935aSDaniel P. Berrange * to set the default websocket port 3588396f935aSDaniel P. Berrange */ 3589396f935aSDaniel P. Berrange if (displaynum == -1) { 3590396f935aSDaniel P. Berrange displaynum = rv; 3591396f935aSDaniel P. Berrange } 3592bd269ebcSMarkus Armbruster *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1); 3593396f935aSDaniel P. Berrange (*retsaddr)[(*retnsaddr)++] = saddr; 3594396f935aSDaniel P. Berrange } 3595396f935aSDaniel P. Berrange 3596396f935aSDaniel P. Berrange /* If we had multiple primary displays, we don't do defaults 3597396f935aSDaniel P. Berrange * for websocket, and require explicit config instead. */ 3598396f935aSDaniel P. Berrange if (*retnsaddr > 1) { 3599396f935aSDaniel P. Berrange displaynum = -1; 3600396f935aSDaniel P. Berrange } 3601396f935aSDaniel P. Berrange 3602396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "websocket"); 3603396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3604e5766eb4SGerd Hoffmann if (vnc_display_get_address(addr, true, reverse, displaynum, to, 3605275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3606275e0d61SDaniel P. Berrange ipv4, ipv6, 3607275e0d61SDaniel P. Berrange &wsaddr, errp) < 0) { 3608396f935aSDaniel P. Berrange goto cleanup; 3609275e0d61SDaniel P. Berrange } 3610396f935aSDaniel P. Berrange 3611396f935aSDaniel P. Berrange /* Historical compat - if only a single listen address was 3612396f935aSDaniel P. Berrange * provided, then this is used to set the default listen 3613396f935aSDaniel P. Berrange * address for websocket too 3614396f935aSDaniel P. Berrange */ 3615396f935aSDaniel P. Berrange if (*retnsaddr == 1 && 3616bd269ebcSMarkus Armbruster (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET && 3617bd269ebcSMarkus Armbruster wsaddr->type == SOCKET_ADDRESS_TYPE_INET && 3618bd269ebcSMarkus Armbruster g_str_equal(wsaddr->u.inet.host, "") && 3619bd269ebcSMarkus Armbruster !g_str_equal((*retsaddr)[0]->u.inet.host, "")) { 3620bd269ebcSMarkus Armbruster g_free(wsaddr->u.inet.host); 3621bd269ebcSMarkus Armbruster wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host); 3622275e0d61SDaniel P. Berrange } 3623275e0d61SDaniel P. Berrange 3624bd269ebcSMarkus Armbruster *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1); 3625396f935aSDaniel P. Berrange (*retwsaddr)[(*retnwsaddr)++] = wsaddr; 3626396f935aSDaniel P. Berrange } 3627396f935aSDaniel P. Berrange 3628396f935aSDaniel P. Berrange ret = 0; 3629396f935aSDaniel P. Berrange cleanup: 3630396f935aSDaniel P. Berrange if (ret < 0) { 36319f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retsaddr, retnsaddr); 36329f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retwsaddr, retnwsaddr); 3633396f935aSDaniel P. Berrange } 3634396f935aSDaniel P. Berrange return ret; 3635275e0d61SDaniel P. Berrange } 3636275e0d61SDaniel P. Berrange 36378bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd, 3638bd269ebcSMarkus Armbruster SocketAddress **saddr, 3639396f935aSDaniel P. Berrange size_t nsaddr, 3640bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3641396f935aSDaniel P. Berrange size_t nwsaddr, 36428bd22f47SDaniel P. Berrange Error **errp) 36438bd22f47SDaniel P. Berrange { 36448bd22f47SDaniel P. Berrange /* connect to viewer */ 36458bd22f47SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3646396f935aSDaniel P. Berrange if (nwsaddr != 0) { 36478bd22f47SDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 36488bd22f47SDaniel P. Berrange return -1; 36498bd22f47SDaniel P. Berrange } 3650396f935aSDaniel P. Berrange if (nsaddr != 1) { 3651396f935aSDaniel P. Berrange error_setg(errp, "Expected a single address in reverse mode"); 3652396f935aSDaniel P. Berrange return -1; 3653396f935aSDaniel P. Berrange } 3654bd269ebcSMarkus Armbruster /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */ 3655bd269ebcSMarkus Armbruster vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX; 36568bd22f47SDaniel P. Berrange sioc = qio_channel_socket_new(); 36578bd22f47SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); 3658396f935aSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) { 36598bd22f47SDaniel P. Berrange return -1; 36608bd22f47SDaniel P. Berrange } 36618bd22f47SDaniel P. Berrange vnc_connect(vd, sioc, false, false); 36628bd22f47SDaniel P. Berrange object_unref(OBJECT(sioc)); 36638bd22f47SDaniel P. Berrange return 0; 36648bd22f47SDaniel P. Berrange } 36658bd22f47SDaniel P. Berrange 36668bd22f47SDaniel P. Berrange 36678bd22f47SDaniel P. Berrange static int vnc_display_listen_addr(VncDisplay *vd, 3668bd269ebcSMarkus Armbruster SocketAddress *addr, 36698bd22f47SDaniel P. Berrange const char *name, 36708bd22f47SDaniel P. Berrange QIOChannelSocket ***lsock, 36718bd22f47SDaniel P. Berrange guint **lsock_tag, 36728bd22f47SDaniel P. Berrange size_t *nlsock, 36738bd22f47SDaniel P. Berrange Error **errp) 36748bd22f47SDaniel P. Berrange { 367557a6d6d5SDaniel P. Berrange QIODNSResolver *resolver = qio_dns_resolver_get_instance(); 3676bd269ebcSMarkus Armbruster SocketAddress **rawaddrs = NULL; 367757a6d6d5SDaniel P. Berrange size_t nrawaddrs = 0; 367857a6d6d5SDaniel P. Berrange Error *listenerr = NULL; 3679396f935aSDaniel P. Berrange bool listening = false; 368057a6d6d5SDaniel P. Berrange size_t i; 36818bd22f47SDaniel P. Berrange 368257a6d6d5SDaniel P. Berrange if (qio_dns_resolver_lookup_sync(resolver, addr, &nrawaddrs, 368357a6d6d5SDaniel P. Berrange &rawaddrs, errp) < 0) { 36848bd22f47SDaniel P. Berrange return -1; 36858bd22f47SDaniel P. Berrange } 36868bd22f47SDaniel P. Berrange 368757a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 368857a6d6d5SDaniel P. Berrange QIOChannelSocket *sioc = qio_channel_socket_new(); 368957a6d6d5SDaniel P. Berrange 369057a6d6d5SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), name); 369157a6d6d5SDaniel P. Berrange if (qio_channel_socket_listen_sync( 369257a6d6d5SDaniel P. Berrange sioc, rawaddrs[i], listenerr == NULL ? &listenerr : NULL) < 0) { 36937bc4f084SMarc-André Lureau object_unref(OBJECT(sioc)); 369457a6d6d5SDaniel P. Berrange continue; 369557a6d6d5SDaniel P. Berrange } 3696396f935aSDaniel P. Berrange listening = true; 369757a6d6d5SDaniel P. Berrange (*nlsock)++; 369857a6d6d5SDaniel P. Berrange *lsock = g_renew(QIOChannelSocket *, *lsock, *nlsock); 369957a6d6d5SDaniel P. Berrange *lsock_tag = g_renew(guint, *lsock_tag, *nlsock); 370057a6d6d5SDaniel P. Berrange 370157a6d6d5SDaniel P. Berrange (*lsock)[*nlsock - 1] = sioc; 370257a6d6d5SDaniel P. Berrange (*lsock_tag)[*nlsock - 1] = 0; 370357a6d6d5SDaniel P. Berrange } 370457a6d6d5SDaniel P. Berrange 370557a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 3706bd269ebcSMarkus Armbruster qapi_free_SocketAddress(rawaddrs[i]); 370757a6d6d5SDaniel P. Berrange } 370857a6d6d5SDaniel P. Berrange g_free(rawaddrs); 370957a6d6d5SDaniel P. Berrange 371057a6d6d5SDaniel P. Berrange if (listenerr) { 3711396f935aSDaniel P. Berrange if (!listening) { 371257a6d6d5SDaniel P. Berrange error_propagate(errp, listenerr); 371357a6d6d5SDaniel P. Berrange return -1; 371457a6d6d5SDaniel P. Berrange } else { 371557a6d6d5SDaniel P. Berrange error_free(listenerr); 371657a6d6d5SDaniel P. Berrange } 371757a6d6d5SDaniel P. Berrange } 371857a6d6d5SDaniel P. Berrange 371957a6d6d5SDaniel P. Berrange for (i = 0; i < *nlsock; i++) { 372057a6d6d5SDaniel P. Berrange (*lsock_tag)[i] = qio_channel_add_watch( 372157a6d6d5SDaniel P. Berrange QIO_CHANNEL((*lsock)[i]), 37228bd22f47SDaniel P. Berrange G_IO_IN, vnc_listen_io, vd, NULL); 372357a6d6d5SDaniel P. Berrange } 37248bd22f47SDaniel P. Berrange 37258bd22f47SDaniel P. Berrange return 0; 37268bd22f47SDaniel P. Berrange } 37278bd22f47SDaniel P. Berrange 37288bd22f47SDaniel P. Berrange 37298bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd, 3730bd269ebcSMarkus Armbruster SocketAddress **saddr, 3731396f935aSDaniel P. Berrange size_t nsaddr, 3732bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3733396f935aSDaniel P. Berrange size_t nwsaddr, 37348bd22f47SDaniel P. Berrange Error **errp) 37358bd22f47SDaniel P. Berrange { 3736396f935aSDaniel P. Berrange size_t i; 37378bd22f47SDaniel P. Berrange 3738396f935aSDaniel P. Berrange for (i = 0; i < nsaddr; i++) { 3739396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, saddr[i], 37408bd22f47SDaniel P. Berrange "vnc-listen", 37418bd22f47SDaniel P. Berrange &vd->lsock, 37428bd22f47SDaniel P. Berrange &vd->lsock_tag, 37438bd22f47SDaniel P. Berrange &vd->nlsock, 37448bd22f47SDaniel P. Berrange errp) < 0) { 37458bd22f47SDaniel P. Berrange return -1; 37468bd22f47SDaniel P. Berrange } 3747396f935aSDaniel P. Berrange } 3748396f935aSDaniel P. Berrange for (i = 0; i < nwsaddr; i++) { 3749396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, wsaddr[i], 37508bd22f47SDaniel P. Berrange "vnc-ws-listen", 37518bd22f47SDaniel P. Berrange &vd->lwebsock, 37528bd22f47SDaniel P. Berrange &vd->lwebsock_tag, 37538bd22f47SDaniel P. Berrange &vd->nlwebsock, 37548bd22f47SDaniel P. Berrange errp) < 0) { 37558bd22f47SDaniel P. Berrange return -1; 37568bd22f47SDaniel P. Berrange } 3757396f935aSDaniel P. Berrange } 37588bd22f47SDaniel P. Berrange 37598bd22f47SDaniel P. Berrange return 0; 37608bd22f47SDaniel P. Berrange } 37618bd22f47SDaniel P. Berrange 37628bd22f47SDaniel P. Berrange 37634db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 37643e230dd2SCorentin Chary { 3765bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 37664db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3767bd269ebcSMarkus Armbruster SocketAddress **saddr = NULL, **wsaddr = NULL; 3768396f935aSDaniel P. Berrange size_t nsaddr, nwsaddr; 3769e2a11d9dSGonglei const char *share, *device_id; 37701d0d59feSGerd Hoffmann QemuConsole *con; 3771a2c72de0SGonglei bool password = false; 3772a2c72de0SGonglei bool reverse = false; 37733e305e4aSDaniel P. Berrange const char *credid; 3774a2c72de0SGonglei bool sasl = false; 3775d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 37763e230dd2SCorentin Chary int saslErr; 37773e230dd2SCorentin Chary #endif 37783e230dd2SCorentin Chary int acl = 0; 37793e230dd2SCorentin Chary int lock_key_sync = 1; 3780c5ce8333SGerd Hoffmann int key_delay_ms; 37813e230dd2SCorentin Chary 3782bf01c179SDaniel P. Berrange if (!vd) { 37832d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 37842d55f0e8SPaolo Bonzini return; 37852d55f0e8SPaolo Bonzini } 3786bf01c179SDaniel P. Berrange vnc_display_close(vd); 37874db14629SGerd Hoffmann 37884db14629SGerd Hoffmann if (!opts) { 37892d55f0e8SPaolo Bonzini return; 37904db14629SGerd Hoffmann } 3791275e0d61SDaniel P. Berrange 3792e5766eb4SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 3793e5766eb4SGerd Hoffmann if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr, 3794396f935aSDaniel P. Berrange &wsaddr, &nwsaddr, errp) < 0) { 3795275e0d61SDaniel P. Berrange goto fail; 3796275e0d61SDaniel P. Berrange } 3797275e0d61SDaniel P. Berrange 37984db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3799800567a6SDaniel P. Berrange if (password) { 3800800567a6SDaniel P. Berrange if (fips_get_state()) { 38012d55f0e8SPaolo Bonzini error_setg(errp, 38020f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 38030f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 38042d55f0e8SPaolo Bonzini "methods as an alternative"); 38051ce52c78SPaolo Bonzini goto fail; 38060f66998fSPaul Moore } 3807800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3808f844836dSGonglei QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) { 3809800567a6SDaniel P. Berrange error_setg(errp, 3810800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3811800567a6SDaniel P. Berrange goto fail; 3812800567a6SDaniel P. Berrange } 3813800567a6SDaniel P. Berrange } 38144db14629SGerd Hoffmann 38154db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3816d3b0db6dSAlexander Graf key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); 38174db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3818d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3819d169f04bSDaniel P. Berrange if (sasl) { 3820d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3821d169f04bSDaniel P. Berrange goto fail; 3822d169f04bSDaniel P. Berrange } 3823d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 38243e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 38253e305e4aSDaniel P. Berrange if (credid) { 38263e305e4aSDaniel P. Berrange Object *creds; 38273e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 38283e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 38293e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 38303e305e4aSDaniel P. Berrange error_setg(errp, 3831c62e90afSWolfgang Bumiller "'tls-creds' parameter is mutually exclusive with " 38323e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 38333e305e4aSDaniel P. Berrange goto fail; 38343e305e4aSDaniel P. Berrange } 38353e305e4aSDaniel P. Berrange 38363e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 38373e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 38383e305e4aSDaniel P. Berrange if (!creds) { 38393e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 38403e305e4aSDaniel P. Berrange credid); 38413e305e4aSDaniel P. Berrange goto fail; 38423e305e4aSDaniel P. Berrange } 3843bf01c179SDaniel P. Berrange vd->tlscreds = (QCryptoTLSCreds *) 38443e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 38453e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 3846bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38473e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 38483e305e4aSDaniel P. Berrange credid); 38493e305e4aSDaniel P. Berrange goto fail; 38503e305e4aSDaniel P. Berrange } 3851bf01c179SDaniel P. Berrange object_ref(OBJECT(vd->tlscreds)); 38523e305e4aSDaniel P. Berrange 3853bf01c179SDaniel P. Berrange if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 38543e305e4aSDaniel P. Berrange error_setg(errp, 38553e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 38563e305e4aSDaniel P. Berrange goto fail; 38573e305e4aSDaniel P. Berrange } 38583e305e4aSDaniel P. Berrange } else { 38593e305e4aSDaniel P. Berrange const char *path; 38603e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 38614db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 38623e305e4aSDaniel P. Berrange if (tls) { 38634db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 38643e305e4aSDaniel P. Berrange 38654db14629SGerd Hoffmann if (path) { 3866a2c72de0SGonglei x509 = true; 38673e305e4aSDaniel P. Berrange } else { 38683e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 38693e305e4aSDaniel P. Berrange if (path) { 38703e305e4aSDaniel P. Berrange x509 = true; 38713e305e4aSDaniel P. Berrange x509verify = true; 38723e305e4aSDaniel P. Berrange } 38733e305e4aSDaniel P. Berrange } 3874bf01c179SDaniel P. Berrange vd->tlscreds = vnc_display_create_creds(x509, 38753e305e4aSDaniel P. Berrange x509verify, 38763e305e4aSDaniel P. Berrange path, 3877bf01c179SDaniel P. Berrange vd->id, 38783e305e4aSDaniel P. Berrange errp); 3879bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38801ce52c78SPaolo Bonzini goto fail; 38813e230dd2SCorentin Chary } 38823e230dd2SCorentin Chary } 3883d169f04bSDaniel P. Berrange } 38844db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 38854db14629SGerd Hoffmann 38864db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 38874db14629SGerd Hoffmann if (share) { 38884db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 3889bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_IGNORE; 38904db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 3891bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 38924db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 3893bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 38948cf36489SGerd Hoffmann } else { 38952d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 38961ce52c78SPaolo Bonzini goto fail; 38978cf36489SGerd Hoffmann } 38984db14629SGerd Hoffmann } else { 3899bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39003e230dd2SCorentin Chary } 3901bf01c179SDaniel P. Berrange vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); 39023e230dd2SCorentin Chary 39034db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 3904bf01c179SDaniel P. Berrange vd->lossy = qemu_opt_get_bool(opts, "lossy", false); 39054db14629SGerd Hoffmann #endif 3906bf01c179SDaniel P. Berrange vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3907e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3908e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3909e22492d3SPeter Lieven * calculations otherwise */ 3910bf01c179SDaniel P. Berrange if (!vd->lossy) { 3911bf01c179SDaniel P. Berrange vd->non_adaptive = true; 3912e22492d3SPeter Lieven } 3913e22492d3SPeter Lieven 39143e305e4aSDaniel P. Berrange if (acl) { 3915bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3916bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup("vnc.x509dname"); 3917c8496408SGerd Hoffmann } else { 3918bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vd->id); 3919c8496408SGerd Hoffmann } 3920bf01c179SDaniel P. Berrange qemu_acl_init(vd->tlsaclname); 39213e230dd2SCorentin Chary } 39223e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39233e230dd2SCorentin Chary if (acl && sasl) { 3924c8496408SGerd Hoffmann char *aclname; 3925c8496408SGerd Hoffmann 3926bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3927c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3928c8496408SGerd Hoffmann } else { 3929bf01c179SDaniel P. Berrange aclname = g_strdup_printf("vnc.%s.username", vd->id); 3930c8496408SGerd Hoffmann } 3931bf01c179SDaniel P. Berrange vd->sasl.acl = qemu_acl_init(aclname); 3932c8496408SGerd Hoffmann g_free(aclname); 39333e230dd2SCorentin Chary } 39343e230dd2SCorentin Chary #endif 39353e230dd2SCorentin Chary 3936eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->auth, &vd->subauth, 3937eda24e18SDaniel P. Berrange vd->tlscreds, password, 3938eda24e18SDaniel P. Berrange sasl, false, errp) < 0) { 3939eda24e18SDaniel P. Berrange goto fail; 3940eda24e18SDaniel P. Berrange } 39417364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth); 3942eda24e18SDaniel P. Berrange 3943eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, 3944eda24e18SDaniel P. Berrange vd->tlscreds, password, 3945eda24e18SDaniel P. Berrange sasl, true, errp) < 0) { 39463e305e4aSDaniel P. Berrange goto fail; 39473e305e4aSDaniel P. Berrange } 39487364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth); 39493e230dd2SCorentin Chary 39503e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39513e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 39522d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 39533e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 39541ce52c78SPaolo Bonzini goto fail; 39553e230dd2SCorentin Chary } 39563e230dd2SCorentin Chary #endif 3957bf01c179SDaniel P. Berrange vd->lock_key_sync = lock_key_sync; 3958a54f0d2bSPierre Ossman if (lock_key_sync) { 3959a54f0d2bSPierre Ossman vd->led = qemu_add_led_event_handler(kbd_leds, vd); 3960a54f0d2bSPierre Ossman } 3961a54f0d2bSPierre Ossman vd->ledstate = 0; 3962bf01c179SDaniel P. Berrange vd->key_delay_ms = key_delay_ms; 39633e230dd2SCorentin Chary 39641d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 39651d0d59feSGerd Hoffmann if (device_id) { 39661d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3967f2c1d54cSGerd Hoffmann Error *err = NULL; 39681d0d59feSGerd Hoffmann 3969f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3970f2c1d54cSGerd Hoffmann if (err) { 3971f2c1d54cSGerd Hoffmann error_propagate(errp, err); 39721d0d59feSGerd Hoffmann goto fail; 39731d0d59feSGerd Hoffmann } 39741d0d59feSGerd Hoffmann } else { 39751d0d59feSGerd Hoffmann con = NULL; 39761d0d59feSGerd Hoffmann } 39771d0d59feSGerd Hoffmann 3978bf01c179SDaniel P. Berrange if (con != vd->dcl.con) { 3979bf01c179SDaniel P. Berrange unregister_displaychangelistener(&vd->dcl); 3980bf01c179SDaniel P. Berrange vd->dcl.con = con; 3981bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 39821d0d59feSGerd Hoffmann } 39831d0d59feSGerd Hoffmann 3984fa03cb7fSMarc-André Lureau if (saddr == NULL) { 3985fa03cb7fSMarc-André Lureau goto cleanup; 3986fa03cb7fSMarc-André Lureau } 3987fa03cb7fSMarc-André Lureau 39883e230dd2SCorentin Chary if (reverse) { 3989396f935aSDaniel P. Berrange if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 3990e0d03b8cSDaniel P. Berrange goto fail; 39913e230dd2SCorentin Chary } 39923e230dd2SCorentin Chary } else { 3993396f935aSDaniel P. Berrange if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 39943d00ac1aSCole Robinson goto fail; 39953d00ac1aSCole Robinson } 39963e230dd2SCorentin Chary } 3997e0d03b8cSDaniel P. Berrange 3998275e0d61SDaniel P. Berrange if (qemu_opt_get(opts, "to")) { 3999bf01c179SDaniel P. Berrange vnc_display_print_local_addr(vd); 400033df7bf3SPaolo Bonzini } 400133df7bf3SPaolo Bonzini 4002396f935aSDaniel P. Berrange cleanup: 40039f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&saddr, &nsaddr); 40049f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&wsaddr, &nwsaddr); 40052d55f0e8SPaolo Bonzini return; 40061ce52c78SPaolo Bonzini 40071ce52c78SPaolo Bonzini fail: 40084ee74fa7SDaniel P. Berrange vnc_display_close(vd); 4009396f935aSDaniel P. Berrange goto cleanup; 40103e230dd2SCorentin Chary } 401113661089SDaniel P. Berrange 401214f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 401313661089SDaniel P. Berrange { 4014bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 401504d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 401613661089SDaniel P. Berrange 4017bf01c179SDaniel P. Berrange if (!vd) { 4018d616ccc5SGerd Hoffmann return; 4019d616ccc5SGerd Hoffmann } 402004d2529dSDaniel P. Berrange 402104d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 402204d2529dSDaniel P. Berrange if (sioc) { 402310bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); 4024bf01c179SDaniel P. Berrange vnc_connect(vd, sioc, skipauth, false); 402504d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 402604d2529dSDaniel P. Berrange } 402713661089SDaniel P. Berrange } 40284db14629SGerd Hoffmann 40299634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 40302779672fSGonglei { 40312779672fSGonglei int i = 2; 40322779672fSGonglei char *id; 40332779672fSGonglei 40342779672fSGonglei id = g_strdup("default"); 40352779672fSGonglei while (qemu_opts_find(olist, id)) { 40362779672fSGonglei g_free(id); 40372779672fSGonglei id = g_strdup_printf("vnc%d", i++); 40382779672fSGonglei } 40392779672fSGonglei qemu_opts_set_id(opts, id); 40402779672fSGonglei } 40412779672fSGonglei 404270b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 40434db14629SGerd Hoffmann { 40444db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 404570b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 404681607cbfSGonglei const char *id; 40474db14629SGerd Hoffmann 404881607cbfSGonglei if (!opts) { 404981607cbfSGonglei return NULL; 405081607cbfSGonglei } 405181607cbfSGonglei 405281607cbfSGonglei id = qemu_opts_id(opts); 40534db14629SGerd Hoffmann if (!id) { 40544db14629SGerd Hoffmann /* auto-assign id if not present */ 40552779672fSGonglei vnc_auto_assign_id(olist, opts); 40569634f4e3SGerd Hoffmann } 40579634f4e3SGerd Hoffmann return opts; 40584db14629SGerd Hoffmann } 40594db14629SGerd Hoffmann 406028d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 40619634f4e3SGerd Hoffmann { 40629634f4e3SGerd Hoffmann Error *local_err = NULL; 40639634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 40649634f4e3SGerd Hoffmann 40659634f4e3SGerd Hoffmann assert(id); 40664db14629SGerd Hoffmann vnc_display_init(id); 40674db14629SGerd Hoffmann vnc_display_open(id, &local_err); 40684db14629SGerd Hoffmann if (local_err != NULL) { 4069c29b77f9SMarkus Armbruster error_reportf_err(local_err, "Failed to start VNC server: "); 40704db14629SGerd Hoffmann exit(1); 40714db14629SGerd Hoffmann } 40724db14629SGerd Hoffmann return 0; 40734db14629SGerd Hoffmann } 40744db14629SGerd Hoffmann 40754db14629SGerd Hoffmann static void vnc_register_config(void) 40764db14629SGerd Hoffmann { 40774db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 40784db14629SGerd Hoffmann } 407934294e2fSEduardo Habkost opts_init(vnc_register_config); 4080