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 964*0bad8342SDaniel P. Berrange static bool vnc_should_update(VncState *vs) 965*0bad8342SDaniel P. Berrange { 966*0bad8342SDaniel P. Berrange switch (vs->update) { 967*0bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_NONE: 968*0bad8342SDaniel P. Berrange break; 969*0bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_INCREMENTAL: 970*0bad8342SDaniel P. Berrange /* Only allow incremental updates if the output buffer 971*0bad8342SDaniel P. Berrange * is empty, or if audio capture is enabled. 972*0bad8342SDaniel P. Berrange */ 973*0bad8342SDaniel P. Berrange if (!vs->output.offset || vs->audio_cap) { 974*0bad8342SDaniel P. Berrange return true; 975*0bad8342SDaniel P. Berrange } 976*0bad8342SDaniel P. Berrange break; 977*0bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_FORCE: 978*0bad8342SDaniel P. Berrange return true; 979*0bad8342SDaniel P. Berrange } 980*0bad8342SDaniel P. Berrange return false; 981*0bad8342SDaniel P. Berrange } 982*0bad8342SDaniel P. Berrange 9836af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty) 9843e230dd2SCorentin Chary { 9853e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 986bd023f95SCorentin Chary VncJob *job; 9873e230dd2SCorentin Chary int y; 9882f487a3dSPeter Lieven int height, width; 989bd023f95SCorentin Chary int n = 0; 990bd023f95SCorentin Chary 991b939eb89SDaniel P. Berrange if (vs->disconnecting) { 992b939eb89SDaniel P. Berrange vnc_disconnect_finish(vs); 993b939eb89SDaniel P. Berrange return 0; 994b939eb89SDaniel P. Berrange } 995b939eb89SDaniel P. Berrange 996b939eb89SDaniel P. Berrange vs->has_dirty += has_dirty; 997*0bad8342SDaniel P. Berrange if (!vnc_should_update(vs)) { 9983e230dd2SCorentin Chary return 0; 999b939eb89SDaniel P. Berrange } 10003e230dd2SCorentin Chary 1001fef1bbadSDaniel P. Berrange if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) { 10023e230dd2SCorentin Chary return 0; 1003b939eb89SDaniel P. Berrange } 10043e230dd2SCorentin Chary 10053e230dd2SCorentin Chary /* 10063e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 10073e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 10083e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 10093e230dd2SCorentin Chary * send them to the client. 10103e230dd2SCorentin Chary */ 1011bd023f95SCorentin Chary job = vnc_job_new(vs); 10123e230dd2SCorentin Chary 1013bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 1014bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 10153e230dd2SCorentin Chary 101612b316d4SPeter Lieven y = 0; 101712b316d4SPeter Lieven for (;;) { 101812b316d4SPeter Lieven int x, h; 101912b316d4SPeter Lieven unsigned long x2; 102012b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 102112b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 102212b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 102312b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 102412b316d4SPeter Lieven /* no more dirty bits */ 102512b316d4SPeter Lieven break; 10263e230dd2SCorentin Chary } 102712b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 102812b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 102912b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 103012b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 103112b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 103212b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10332f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10342f487a3dSPeter Lieven if (x2 > x) { 103512b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 103612b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10373e230dd2SCorentin Chary } 10380e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10390e7d6f60SPeter Lieven y += h; 10400e7d6f60SPeter Lieven if (y == height) { 10410e7d6f60SPeter Lieven break; 10420e7d6f60SPeter Lieven } 10430e7d6f60SPeter Lieven } 10442f487a3dSPeter Lieven } 1045bd023f95SCorentin Chary 1046bd023f95SCorentin Chary vnc_job_push(job); 1047728a7ac9SDaniel P. Berrange vs->update = VNC_STATE_UPDATE_NONE; 104863658280SGerd Hoffmann vs->has_dirty = 0; 1049bd023f95SCorentin Chary return n; 10503e230dd2SCorentin Chary } 10513e230dd2SCorentin Chary 10523e230dd2SCorentin Chary /* audio */ 10533e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 10543e230dd2SCorentin Chary { 10553e230dd2SCorentin Chary VncState *vs = opaque; 10563e230dd2SCorentin Chary 10573e230dd2SCorentin Chary switch (cmd) { 10583e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1059bd023f95SCorentin Chary vnc_lock_output(vs); 10603e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10613e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10623e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1063bd023f95SCorentin Chary vnc_unlock_output(vs); 10643e230dd2SCorentin Chary vnc_flush(vs); 10653e230dd2SCorentin Chary break; 10663e230dd2SCorentin Chary 10673e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1068bd023f95SCorentin Chary vnc_lock_output(vs); 10693e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10703e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10713e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1072bd023f95SCorentin Chary vnc_unlock_output(vs); 10733e230dd2SCorentin Chary vnc_flush(vs); 10743e230dd2SCorentin Chary break; 10753e230dd2SCorentin Chary } 10763e230dd2SCorentin Chary } 10773e230dd2SCorentin Chary 10783e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 10793e230dd2SCorentin Chary { 10803e230dd2SCorentin Chary } 10813e230dd2SCorentin Chary 10823e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 10833e230dd2SCorentin Chary { 10843e230dd2SCorentin Chary VncState *vs = opaque; 10853e230dd2SCorentin Chary 1086bd023f95SCorentin Chary vnc_lock_output(vs); 10873e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10883e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10893e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 10903e230dd2SCorentin Chary vnc_write_u32(vs, size); 10913e230dd2SCorentin Chary vnc_write(vs, buf, size); 1092bd023f95SCorentin Chary vnc_unlock_output(vs); 10933e230dd2SCorentin Chary vnc_flush(vs); 10943e230dd2SCorentin Chary } 10953e230dd2SCorentin Chary 10963e230dd2SCorentin Chary static void audio_add(VncState *vs) 10973e230dd2SCorentin Chary { 10983e230dd2SCorentin Chary struct audio_capture_ops ops; 10993e230dd2SCorentin Chary 11003e230dd2SCorentin Chary if (vs->audio_cap) { 1101027a79c3SCole Robinson error_report("audio already running"); 11023e230dd2SCorentin Chary return; 11033e230dd2SCorentin Chary } 11043e230dd2SCorentin Chary 11053e230dd2SCorentin Chary ops.notify = audio_capture_notify; 11063e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 11073e230dd2SCorentin Chary ops.capture = audio_capture; 11083e230dd2SCorentin Chary 11093e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 11103e230dd2SCorentin Chary if (!vs->audio_cap) { 1111027a79c3SCole Robinson error_report("Failed to add audio capture"); 11123e230dd2SCorentin Chary } 11133e230dd2SCorentin Chary } 11143e230dd2SCorentin Chary 11153e230dd2SCorentin Chary static void audio_del(VncState *vs) 11163e230dd2SCorentin Chary { 11173e230dd2SCorentin Chary if (vs->audio_cap) { 11183e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11193e230dd2SCorentin Chary vs->audio_cap = NULL; 11203e230dd2SCorentin Chary } 11213e230dd2SCorentin Chary } 11223e230dd2SCorentin Chary 11233e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11243e230dd2SCorentin Chary { 112504d2529dSDaniel P. Berrange if (vs->disconnecting) { 11263e230dd2SCorentin Chary return; 112704d2529dSDaniel P. Berrange } 1128ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_start(vs, vs->ioc); 11298cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 113004d2529dSDaniel P. Berrange if (vs->ioc_tag) { 113104d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 1132a75d6f07SBrandon Carpenter vs->ioc_tag = 0; 113304d2529dSDaniel P. Berrange } 113404d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 113504d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11363e230dd2SCorentin Chary } 11373e230dd2SCorentin Chary 11387536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11393e230dd2SCorentin Chary { 11407d964c9dSCorentin Chary int i; 11417d964c9dSCorentin Chary 1142ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_finish(vs, vs->ioc); 1143ad6374c4SDaniel P. Berrange 1144bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1145bd023f95SCorentin Chary 1146bd023f95SCorentin Chary vnc_lock_output(vs); 1147fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11483e230dd2SCorentin Chary 11493e230dd2SCorentin Chary buffer_free(&vs->input); 11503e230dd2SCorentin Chary buffer_free(&vs->output); 11513e230dd2SCorentin Chary 1152fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11533e230dd2SCorentin Chary 11543e230dd2SCorentin Chary vnc_zlib_clear(vs); 11553e230dd2SCorentin Chary vnc_tight_clear(vs); 1156148954faSCorentin Chary vnc_zrle_clear(vs); 11573e230dd2SCorentin Chary 11583e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11593e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 11603e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11613e230dd2SCorentin Chary audio_del(vs); 11627bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 11633e230dd2SCorentin Chary 116490cd03a3SDaniel P. Berrange if (vs->mouse_mode_notifier.notify != NULL) { 11656fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 116690cd03a3SDaniel P. Berrange } 116790cd03a3SDaniel P. Berrange QTAILQ_REMOVE(&vs->vd->clients, vs, next); 1168c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1169c7628bffSGerd Hoffmann /* last client gone */ 1170c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1171c7628bffSGerd Hoffmann } 11723e230dd2SCorentin Chary 1173bd023f95SCorentin Chary vnc_unlock_output(vs); 1174bd023f95SCorentin Chary 1175bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 11766fd8e79aSTim Hardeck if (vs->bh != NULL) { 1177175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 11786fd8e79aSTim Hardeck } 1179175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1180175b2a6eSCorentin Chary 11817d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 11827267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 11837d964c9dSCorentin Chary } 11847267c094SAnthony Liguori g_free(vs->lossy_rect); 118504d2529dSDaniel P. Berrange 118604d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 118704d2529dSDaniel P. Berrange vs->ioc = NULL; 118804d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 118904d2529dSDaniel P. Berrange vs->sioc = NULL; 11907267c094SAnthony Liguori g_free(vs); 11913e230dd2SCorentin Chary } 11923e230dd2SCorentin Chary 119304d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 11943e230dd2SCorentin Chary { 119504d2529dSDaniel P. Berrange if (ret <= 0) { 119604d2529dSDaniel P. Berrange if (ret == 0) { 1197ad6374c4SDaniel P. Berrange trace_vnc_client_eof(vs, vs->ioc); 1198537848eeSMichael Tokarev vnc_disconnect_start(vs); 119904d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 1200ad6374c4SDaniel P. Berrange trace_vnc_client_io_error(vs, vs->ioc, 1201ad6374c4SDaniel P. Berrange errp ? error_get_pretty(*errp) : 1202ad6374c4SDaniel P. Berrange "Unknown"); 1203537848eeSMichael Tokarev vnc_disconnect_start(vs); 12043e230dd2SCorentin Chary } 12053e230dd2SCorentin Chary 120604d2529dSDaniel P. Berrange if (errp) { 120704d2529dSDaniel P. Berrange error_free(*errp); 120804d2529dSDaniel P. Berrange *errp = NULL; 120904d2529dSDaniel P. Berrange } 12103e230dd2SCorentin Chary return 0; 12113e230dd2SCorentin Chary } 12123e230dd2SCorentin Chary return ret; 12133e230dd2SCorentin Chary } 12143e230dd2SCorentin Chary 12153e230dd2SCorentin Chary 12163e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12173e230dd2SCorentin Chary { 12183e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12193e230dd2SCorentin Chary vnc_disconnect_start(vs); 12203e230dd2SCorentin Chary } 12213e230dd2SCorentin Chary 12223e305e4aSDaniel P. Berrange 12233e230dd2SCorentin Chary /* 12243e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12253e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12263e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12273e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12283e230dd2SCorentin Chary * 12293e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12303e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12313e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12323e230dd2SCorentin Chary * is active 12333e230dd2SCorentin Chary * 12343e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12353e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12363e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12373e230dd2SCorentin Chary */ 1238fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12393e230dd2SCorentin Chary { 124004d2529dSDaniel P. Berrange Error *err = NULL; 1241fdd1ab6aSDaniel P. Berrange ssize_t ret; 124204d2529dSDaniel P. Berrange ret = qio_channel_write( 124304d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12443e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 124504d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12463e230dd2SCorentin Chary } 12473e230dd2SCorentin Chary 12483e230dd2SCorentin Chary 12493e230dd2SCorentin Chary /* 12503e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12513e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12523e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12533e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12543e230dd2SCorentin Chary * 12553e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12563e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 12573e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12583e230dd2SCorentin Chary */ 1259fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 12603e230dd2SCorentin Chary { 1261fdd1ab6aSDaniel P. Berrange ssize_t ret; 12623e230dd2SCorentin Chary 12633e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12643e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 12653e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 12663e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 12673e230dd2SCorentin Chary 12683e230dd2SCorentin Chary if (vs->sasl.conn && 12693e230dd2SCorentin Chary vs->sasl.runSSF && 12703e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 12713e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 12723e230dd2SCorentin Chary if (ret) 12733e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 12743e230dd2SCorentin Chary } else 12753e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12763e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 12773e230dd2SCorentin Chary if (!ret) 12783e230dd2SCorentin Chary return 0; 12793e230dd2SCorentin Chary 128032ed2680STim Hardeck buffer_advance(&vs->output, ret); 12813e230dd2SCorentin Chary 12823e230dd2SCorentin Chary if (vs->output.offset == 0) { 128304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 128404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 128504d2529dSDaniel P. Berrange } 128604d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 128704d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 12883e230dd2SCorentin Chary } 12893e230dd2SCorentin Chary 12903e230dd2SCorentin Chary return ret; 12913e230dd2SCorentin Chary } 12923e230dd2SCorentin Chary 12933e230dd2SCorentin Chary 12943e230dd2SCorentin Chary /* 12953e230dd2SCorentin Chary * First function called whenever there is data to be written to 12963e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12973e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12983e230dd2SCorentin Chary */ 129904d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 13003e230dd2SCorentin Chary { 13013e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13023e230dd2SCorentin Chary if (vs->sasl.conn && 13033e230dd2SCorentin Chary vs->sasl.runSSF && 13043e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 13053e230dd2SCorentin Chary vnc_client_write_sasl(vs); 13063e230dd2SCorentin Chary } else 13073e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13087536ee4bSTim Hardeck { 13093e230dd2SCorentin Chary vnc_client_write_plain(vs); 13103e230dd2SCorentin Chary } 13117536ee4bSTim Hardeck } 13123e230dd2SCorentin Chary 131304d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1314bd023f95SCorentin Chary { 1315bd023f95SCorentin Chary 1316bd023f95SCorentin Chary vnc_lock_output(vs); 1317d5f04223SDaniel P. Berrange if (vs->output.offset) { 131804d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 131904d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 132004d2529dSDaniel P. Berrange if (vs->ioc_tag) { 132104d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 132204d2529dSDaniel P. Berrange } 132304d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 132404d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1325bd023f95SCorentin Chary } 1326bd023f95SCorentin Chary vnc_unlock_output(vs); 1327bd023f95SCorentin Chary } 1328bd023f95SCorentin Chary 13293e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13303e230dd2SCorentin Chary { 13313e230dd2SCorentin Chary vs->read_handler = func; 13323e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13333e230dd2SCorentin Chary } 13343e230dd2SCorentin Chary 13353e230dd2SCorentin Chary 13363e230dd2SCorentin Chary /* 13373e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13383e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13393e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13403e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13413e230dd2SCorentin Chary * 13423e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13433e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13443e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13453e230dd2SCorentin Chary * is active 13463e230dd2SCorentin Chary * 13473e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13483e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13493e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13503e230dd2SCorentin Chary */ 1351fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13523e230dd2SCorentin Chary { 1353fdd1ab6aSDaniel P. Berrange ssize_t ret; 135404d2529dSDaniel P. Berrange Error *err = NULL; 135504d2529dSDaniel P. Berrange ret = qio_channel_read( 135604d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 13573e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 135804d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 13593e230dd2SCorentin Chary } 13603e230dd2SCorentin Chary 13613e230dd2SCorentin Chary 13623e230dd2SCorentin Chary /* 13633e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 13643e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 13653e230dd2SCorentin Chary * data as possible without blocking. 13663e230dd2SCorentin Chary * 13673e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 13683e230dd2SCorentin Chary * disconnects the client socket. 13693e230dd2SCorentin Chary */ 1370fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 13713e230dd2SCorentin Chary { 1372fdd1ab6aSDaniel P. Berrange ssize_t ret; 13733e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13743e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13753e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13763e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13773e230dd2SCorentin Chary if (!ret) 13783e230dd2SCorentin Chary return 0; 13793e230dd2SCorentin Chary vs->input.offset += ret; 13803e230dd2SCorentin Chary return ret; 13813e230dd2SCorentin Chary } 13823e230dd2SCorentin Chary 1383175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1384175b2a6eSCorentin Chary { 1385175b2a6eSCorentin Chary VncState *vs = opaque; 1386175b2a6eSCorentin Chary 1387175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1388175b2a6eSCorentin Chary } 13893e230dd2SCorentin Chary 13903e230dd2SCorentin Chary /* 13913e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13923e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13933e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 1394ea697449SDaniel P. Berrange * Returns 0 on success, -1 if client disconnected 13953e230dd2SCorentin Chary */ 1396ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs) 13973e230dd2SCorentin Chary { 1398fdd1ab6aSDaniel P. Berrange ssize_t ret; 13993e230dd2SCorentin Chary 14003e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 14013e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 14023e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 14033e230dd2SCorentin Chary else 14043e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 14053e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 14063e230dd2SCorentin Chary if (!ret) { 140704d2529dSDaniel P. Berrange if (vs->disconnecting) { 14083e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1409ea697449SDaniel P. Berrange return -1; 141004d2529dSDaniel P. Berrange } 1411ea697449SDaniel P. Berrange return 0; 14123e230dd2SCorentin Chary } 14133e230dd2SCorentin Chary 14143e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14153e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14163e230dd2SCorentin Chary int ret; 14173e230dd2SCorentin Chary 14183e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 141904d2529dSDaniel P. Berrange if (vs->disconnecting) { 14203e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1421ea697449SDaniel P. Berrange return -1; 14223e230dd2SCorentin Chary } 14233e230dd2SCorentin Chary 14243e230dd2SCorentin Chary if (!ret) { 142532ed2680STim Hardeck buffer_advance(&vs->input, len); 14263e230dd2SCorentin Chary } else { 14273e230dd2SCorentin Chary vs->read_handler_expect = ret; 14283e230dd2SCorentin Chary } 14293e230dd2SCorentin Chary } 1430ea697449SDaniel P. Berrange return 0; 14313e230dd2SCorentin Chary } 14323e230dd2SCorentin Chary 143304d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 143404d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 143504d2529dSDaniel P. Berrange { 143604d2529dSDaniel P. Berrange VncState *vs = opaque; 143704d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 1438ea697449SDaniel P. Berrange if (vnc_client_read(vs) < 0) { 1439ea697449SDaniel P. Berrange return TRUE; 1440ea697449SDaniel P. Berrange } 144104d2529dSDaniel P. Berrange } 144204d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 144304d2529dSDaniel P. Berrange vnc_client_write(vs); 144404d2529dSDaniel P. Berrange } 144504d2529dSDaniel P. Berrange return TRUE; 144604d2529dSDaniel P. Berrange } 144704d2529dSDaniel P. Berrange 144804d2529dSDaniel P. Berrange 14493e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14503e230dd2SCorentin Chary { 14513e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14523e230dd2SCorentin Chary 145304d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 145404d2529dSDaniel P. Berrange if (vs->ioc_tag) { 145504d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 145604d2529dSDaniel P. Berrange } 145704d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 145804d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14593e230dd2SCorentin Chary } 14603e230dd2SCorentin Chary 14613e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 14623e230dd2SCorentin Chary } 14633e230dd2SCorentin Chary 14643e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 14653e230dd2SCorentin Chary { 14663e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 14673e230dd2SCorentin Chary } 14683e230dd2SCorentin Chary 14693e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 14703e230dd2SCorentin Chary { 14713e230dd2SCorentin Chary uint8_t buf[4]; 14723e230dd2SCorentin Chary 14733e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 14743e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 14753e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 14763e230dd2SCorentin Chary buf[3] = value & 0xFF; 14773e230dd2SCorentin Chary 14783e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14793e230dd2SCorentin Chary } 14803e230dd2SCorentin Chary 14813e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14823e230dd2SCorentin Chary { 14833e230dd2SCorentin Chary uint8_t buf[2]; 14843e230dd2SCorentin Chary 14853e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14863e230dd2SCorentin Chary buf[1] = value & 0xFF; 14873e230dd2SCorentin Chary 14883e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14893e230dd2SCorentin Chary } 14903e230dd2SCorentin Chary 14913e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14923e230dd2SCorentin Chary { 14933e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14943e230dd2SCorentin Chary } 14953e230dd2SCorentin Chary 14963e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14973e230dd2SCorentin Chary { 1498bd023f95SCorentin Chary vnc_lock_output(vs); 1499d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1500bd023f95SCorentin Chary vnc_client_write_locked(vs); 1501bd023f95SCorentin Chary } 1502bd023f95SCorentin Chary vnc_unlock_output(vs); 15033e230dd2SCorentin Chary } 15043e230dd2SCorentin Chary 150571a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 15063e230dd2SCorentin Chary { 15073e230dd2SCorentin Chary return data[offset]; 15083e230dd2SCorentin Chary } 15093e230dd2SCorentin Chary 151071a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 15113e230dd2SCorentin Chary { 15123e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15133e230dd2SCorentin Chary } 15143e230dd2SCorentin Chary 151571a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15163e230dd2SCorentin Chary { 15173e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15183e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15193e230dd2SCorentin Chary } 15203e230dd2SCorentin Chary 15213e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15223e230dd2SCorentin Chary { 15233e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15243e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15253e230dd2SCorentin Chary } 15263e230dd2SCorentin Chary 15273e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15283e230dd2SCorentin Chary { 15293e230dd2SCorentin Chary } 15303e230dd2SCorentin Chary 15319e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15323e230dd2SCorentin Chary { 15333e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 153414768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15353e230dd2SCorentin Chary 15363e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1537bd023f95SCorentin Chary vnc_lock_output(vs); 15383e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15393e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15403e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15413e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1542bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1543bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15443e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1545bd023f95SCorentin Chary vnc_unlock_output(vs); 15463e230dd2SCorentin Chary vnc_flush(vs); 15473e230dd2SCorentin Chary } 15483e230dd2SCorentin Chary vs->absolute = absolute; 15493e230dd2SCorentin Chary } 15503e230dd2SCorentin Chary 15513e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15523e230dd2SCorentin Chary { 15537fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 155414768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 155514768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 155614768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1557f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1558f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 155914768ebaSGerd Hoffmann }; 156014768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1561bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1562bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 15633e230dd2SCorentin Chary 156414768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 156514768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 156614768ebaSGerd Hoffmann vs->last_bmask = button_mask; 156714768ebaSGerd Hoffmann } 15683e230dd2SCorentin Chary 15693e230dd2SCorentin Chary if (vs->absolute) { 15709cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); 15719cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); 15723e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 157314768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 157414768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 15753e230dd2SCorentin Chary } else { 157614768ebaSGerd Hoffmann if (vs->last_x != -1) { 157714768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 157814768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 157914768ebaSGerd Hoffmann } 15803e230dd2SCorentin Chary vs->last_x = x; 15813e230dd2SCorentin Chary vs->last_y = y; 15823e230dd2SCorentin Chary } 158314768ebaSGerd Hoffmann qemu_input_event_sync(); 15843e230dd2SCorentin Chary } 15853e230dd2SCorentin Chary 15863e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15873e230dd2SCorentin Chary { 15883e230dd2SCorentin Chary int i; 15893e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15903e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15918d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 1592c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15933e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15943e230dd2SCorentin Chary } 15953e230dd2SCorentin Chary } 15963e230dd2SCorentin Chary } 15973e230dd2SCorentin Chary 15983e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15993e230dd2SCorentin Chary { 16003e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 16018d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 1602c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 16038d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1604c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 16053e230dd2SCorentin Chary } 16063e230dd2SCorentin Chary 1607ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1608ab99e5c1SLei Li { 1609ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1610ab99e5c1SLei Li return; 1611ab99e5c1SLei Li } 1612ab99e5c1SLei Li 1613ab99e5c1SLei Li vnc_lock_output(vs); 1614ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1615ab99e5c1SLei Li vnc_write_u8(vs, 0); 1616ab99e5c1SLei Li vnc_write_u16(vs, 1); 1617ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1618a54f0d2bSPierre Ossman vnc_write_u8(vs, vs->vd->ledstate); 1619ab99e5c1SLei Li vnc_unlock_output(vs); 1620ab99e5c1SLei Li vnc_flush(vs); 1621ab99e5c1SLei Li } 1622ab99e5c1SLei Li 16233e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16243e230dd2SCorentin Chary { 1625a54f0d2bSPierre Ossman VncDisplay *vd = opaque; 1626a54f0d2bSPierre Ossman VncState *client; 16273e230dd2SCorentin Chary 162840066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 162940066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 163040066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 163140066175SGerd Hoffmann 1632a54f0d2bSPierre Ossman if (ledstate == vd->ledstate) { 1633a54f0d2bSPierre Ossman return; 163496f3d174SLei Li } 1635ab99e5c1SLei Li 1636a54f0d2bSPierre Ossman vd->ledstate = ledstate; 1637a54f0d2bSPierre Ossman 1638a54f0d2bSPierre Ossman QTAILQ_FOREACH(client, &vd->clients, next) { 1639a54f0d2bSPierre Ossman vnc_led_state_change(client); 1640ab99e5c1SLei Li } 16413e230dd2SCorentin Chary } 16423e230dd2SCorentin Chary 16433e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 16443e230dd2SCorentin Chary { 16453e230dd2SCorentin Chary /* QEMU console switch */ 16463e230dd2SCorentin Chary switch(keycode) { 16473e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16483e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16493e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16503e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16513e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16523e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16533e230dd2SCorentin Chary if (down) 16543e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 16553e230dd2SCorentin Chary else 16563e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 16573e230dd2SCorentin Chary break; 16583e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 16591d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 16601d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 16613e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 16623e230dd2SCorentin Chary reset_keys(vs); 16633e230dd2SCorentin Chary console_select(keycode - 0x02); 16643e230dd2SCorentin Chary return; 16653e230dd2SCorentin Chary } 16663e230dd2SCorentin Chary break; 16673e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 16683e230dd2SCorentin Chary case 0x45: /* NumLock */ 16693e230dd2SCorentin Chary if (down) 16703e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 16713e230dd2SCorentin Chary break; 16723e230dd2SCorentin Chary } 16733e230dd2SCorentin Chary 1674e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1675e7b2aaccSLei Li state extension. 1676e7b2aaccSLei Li */ 16779892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1678e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16793e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 16803e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 16813e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16823e230dd2SCorentin Chary toggles numlock away from the VNC window. 16833e230dd2SCorentin Chary */ 16843e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 16853e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 168640066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 16873e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 16883e230dd2SCorentin Chary press_key(vs, 0xff7f); 16893e230dd2SCorentin Chary } 16903e230dd2SCorentin Chary } else { 16913e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 169240066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 16933e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 16943e230dd2SCorentin Chary press_key(vs, 0xff7f); 16953e230dd2SCorentin Chary } 16963e230dd2SCorentin Chary } 16973e230dd2SCorentin Chary } 16983e230dd2SCorentin Chary 16999892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1700e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 17013e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 17023e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 17033e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 17043e230dd2SCorentin Chary toggles capslock away from the VNC window. 17053e230dd2SCorentin Chary */ 17063e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 17073e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 17083e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 17093e230dd2SCorentin Chary if (capslock) { 17103e230dd2SCorentin Chary if (uppercase == shift) { 171140066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17123e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17133e230dd2SCorentin Chary press_key(vs, 0xffe5); 17143e230dd2SCorentin Chary } 17153e230dd2SCorentin Chary } else { 17163e230dd2SCorentin Chary if (uppercase != shift) { 171740066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17183e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17193e230dd2SCorentin Chary press_key(vs, 0xffe5); 17203e230dd2SCorentin Chary } 17213e230dd2SCorentin Chary } 17223e230dd2SCorentin Chary } 17233e230dd2SCorentin Chary 172481c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17258d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 1726c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 17273e230dd2SCorentin Chary } else { 1728e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1729e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1730e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17313e230dd2SCorentin Chary /* QEMU console emulation */ 17323e230dd2SCorentin Chary if (down) { 17333e230dd2SCorentin Chary switch (keycode) { 17343e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17353e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17363e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17373e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17383e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17393e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17403e230dd2SCorentin Chary break; 17413e230dd2SCorentin Chary case 0xc8: 17423e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 17433e230dd2SCorentin Chary break; 17443e230dd2SCorentin Chary case 0xd0: 17453e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 17463e230dd2SCorentin Chary break; 17473e230dd2SCorentin Chary case 0xcb: 17483e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 17493e230dd2SCorentin Chary break; 17503e230dd2SCorentin Chary case 0xcd: 17513e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 17523e230dd2SCorentin Chary break; 17533e230dd2SCorentin Chary case 0xd3: 17543e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 17553e230dd2SCorentin Chary break; 17563e230dd2SCorentin Chary case 0xc7: 17573e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 17583e230dd2SCorentin Chary break; 17593e230dd2SCorentin Chary case 0xcf: 17603e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 17613e230dd2SCorentin Chary break; 17623e230dd2SCorentin Chary case 0xc9: 17633e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 17643e230dd2SCorentin Chary break; 17653e230dd2SCorentin Chary case 0xd1: 17663e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 17673e230dd2SCorentin Chary break; 17683e230dd2SCorentin Chary 17693e230dd2SCorentin Chary case 0x47: 17703e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 17713e230dd2SCorentin Chary break; 17723e230dd2SCorentin Chary case 0x48: 17733e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 17743e230dd2SCorentin Chary break; 17753e230dd2SCorentin Chary case 0x49: 17763e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 17773e230dd2SCorentin Chary break; 17783e230dd2SCorentin Chary case 0x4b: 17793e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 17803e230dd2SCorentin Chary break; 17813e230dd2SCorentin Chary case 0x4c: 17823e230dd2SCorentin Chary kbd_put_keysym('5'); 17833e230dd2SCorentin Chary break; 17843e230dd2SCorentin Chary case 0x4d: 17853e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 17863e230dd2SCorentin Chary break; 17873e230dd2SCorentin Chary case 0x4f: 17883e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 17893e230dd2SCorentin Chary break; 17903e230dd2SCorentin Chary case 0x50: 17913e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 17923e230dd2SCorentin Chary break; 17933e230dd2SCorentin Chary case 0x51: 17943e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 17953e230dd2SCorentin Chary break; 17963e230dd2SCorentin Chary case 0x52: 17973e230dd2SCorentin Chary kbd_put_keysym('0'); 17983e230dd2SCorentin Chary break; 17993e230dd2SCorentin Chary case 0x53: 18003e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 18013e230dd2SCorentin Chary break; 18023e230dd2SCorentin Chary 18033e230dd2SCorentin Chary case 0xb5: 18043e230dd2SCorentin Chary kbd_put_keysym('/'); 18053e230dd2SCorentin Chary break; 18063e230dd2SCorentin Chary case 0x37: 18073e230dd2SCorentin Chary kbd_put_keysym('*'); 18083e230dd2SCorentin Chary break; 18093e230dd2SCorentin Chary case 0x4a: 18103e230dd2SCorentin Chary kbd_put_keysym('-'); 18113e230dd2SCorentin Chary break; 18123e230dd2SCorentin Chary case 0x4e: 18133e230dd2SCorentin Chary kbd_put_keysym('+'); 18143e230dd2SCorentin Chary break; 18153e230dd2SCorentin Chary case 0x9c: 18163e230dd2SCorentin Chary kbd_put_keysym('\n'); 18173e230dd2SCorentin Chary break; 18183e230dd2SCorentin Chary 18193e230dd2SCorentin Chary default: 1820e26437c2SGerd Hoffmann if (control) { 1821e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1822e26437c2SGerd Hoffmann } else { 18233e230dd2SCorentin Chary kbd_put_keysym(sym); 1824e26437c2SGerd Hoffmann } 18253e230dd2SCorentin Chary break; 18263e230dd2SCorentin Chary } 18273e230dd2SCorentin Chary } 18283e230dd2SCorentin Chary } 18293e230dd2SCorentin Chary } 18303e230dd2SCorentin Chary 18317bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18327bc9318bSGerd Hoffmann { 18337bc9318bSGerd Hoffmann static const int keycodes[] = { 18347bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 18357bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 18367bc9318bSGerd Hoffmann }; 18377bc9318bSGerd Hoffmann int i, keycode; 18387bc9318bSGerd Hoffmann 183981c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 18407bc9318bSGerd Hoffmann return; 18417bc9318bSGerd Hoffmann } 18427bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 18437bc9318bSGerd Hoffmann keycode = keycodes[i]; 18447bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 18457bc9318bSGerd Hoffmann continue; 18467bc9318bSGerd Hoffmann } 18478d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1848c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 18497bc9318bSGerd Hoffmann } 18507bc9318bSGerd Hoffmann } 18517bc9318bSGerd Hoffmann 185240066175SGerd Hoffmann static const char *code2name(int keycode) 185340066175SGerd Hoffmann { 1854977c736fSMarkus Armbruster return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); 185540066175SGerd Hoffmann } 185640066175SGerd Hoffmann 18573e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 18583e230dd2SCorentin Chary { 18593e230dd2SCorentin Chary int keycode; 18603e230dd2SCorentin Chary int lsym = sym; 18613e230dd2SCorentin Chary 186281c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 18633e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 18643e230dd2SCorentin Chary } 18653e230dd2SCorentin Chary 18663e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 186740066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 18683e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18693e230dd2SCorentin Chary } 18703e230dd2SCorentin Chary 18713e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 18723e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 18733e230dd2SCorentin Chary { 18743e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 187540066175SGerd Hoffmann if (keyboard_layout) { 18763e230dd2SCorentin Chary key_event(vs, down, sym); 187740066175SGerd Hoffmann } else { 187840066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 18793e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18803e230dd2SCorentin Chary } 188140066175SGerd Hoffmann } 18823e230dd2SCorentin Chary 18833e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1884bea60dd7SPeter Lieven int x, int y, int w, int h) 18853e230dd2SCorentin Chary { 1886bea60dd7SPeter Lieven if (incremental) { 1887fef1bbadSDaniel P. Berrange if (vs->update != VNC_STATE_UPDATE_FORCE) { 1888fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_INCREMENTAL; 18893e230dd2SCorentin Chary } 1890fef1bbadSDaniel P. Berrange } else { 1891fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_FORCE; 1892f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 18933e230dd2SCorentin Chary } 1894fef1bbadSDaniel P. Berrange } 18953e230dd2SCorentin Chary 18963e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18973e230dd2SCorentin Chary { 1898bd023f95SCorentin Chary vnc_lock_output(vs); 18993e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19003e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19013e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1902d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1903bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1904bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19053e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1906bd023f95SCorentin Chary vnc_unlock_output(vs); 19073e230dd2SCorentin Chary vnc_flush(vs); 19083e230dd2SCorentin Chary } 19093e230dd2SCorentin Chary 19103e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19113e230dd2SCorentin Chary { 1912bd023f95SCorentin Chary vnc_lock_output(vs); 19133e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19143e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19153e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1916d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1917bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1918bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19193e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1920bd023f95SCorentin Chary vnc_unlock_output(vs); 19213e230dd2SCorentin Chary vnc_flush(vs); 19223e230dd2SCorentin Chary } 19233e230dd2SCorentin Chary 19243e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19253e230dd2SCorentin Chary { 19263e230dd2SCorentin Chary int i; 19273e230dd2SCorentin Chary unsigned int enc = 0; 19283e230dd2SCorentin Chary 19293e230dd2SCorentin Chary vs->features = 0; 19303e230dd2SCorentin Chary vs->vnc_encoding = 0; 1931d1af0e05SCorentin Chary vs->tight.compression = 9; 1932d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19333e230dd2SCorentin Chary vs->absolute = -1; 19343e230dd2SCorentin Chary 19353e230dd2SCorentin Chary /* 19363e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1937e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 19383e230dd2SCorentin Chary * will be set at the end of the loop. 19393e230dd2SCorentin Chary */ 19403e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 19413e230dd2SCorentin Chary enc = encodings[i]; 19423e230dd2SCorentin Chary switch (enc) { 19433e230dd2SCorentin Chary case VNC_ENCODING_RAW: 19443e230dd2SCorentin Chary vs->vnc_encoding = enc; 19453e230dd2SCorentin Chary break; 19463e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 19473e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 19483e230dd2SCorentin Chary break; 19493e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 19503e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 19513e230dd2SCorentin Chary vs->vnc_encoding = enc; 19523e230dd2SCorentin Chary break; 19533e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 19543e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 19553e230dd2SCorentin Chary vs->vnc_encoding = enc; 19563e230dd2SCorentin Chary break; 1957fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1958efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1959efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1960efe556adSCorentin Chary vs->vnc_encoding = enc; 1961efe556adSCorentin Chary break; 1962fe3e7f2dSJoel Martin #endif 19633e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 19643e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 19653e230dd2SCorentin Chary vs->vnc_encoding = enc; 19663e230dd2SCorentin Chary break; 1967148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1968148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1969148954faSCorentin Chary vs->vnc_encoding = enc; 1970148954faSCorentin Chary break; 1971148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1972148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1973148954faSCorentin Chary vs->vnc_encoding = enc; 1974148954faSCorentin Chary break; 19753e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 19763e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 19773e230dd2SCorentin Chary break; 19783e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 19793e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 19803e230dd2SCorentin Chary break; 19813e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 19823e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 198391ec41dcSFrediano Ziglio if (vs->vd->cursor) { 198491ec41dcSFrediano Ziglio vnc_cursor_define(vs); 198591ec41dcSFrediano Ziglio } 19863e230dd2SCorentin Chary break; 19873e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 19883e230dd2SCorentin Chary send_ext_key_event_ack(vs); 19893e230dd2SCorentin Chary break; 19903e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 19913e230dd2SCorentin Chary send_ext_audio_ack(vs); 19923e230dd2SCorentin Chary break; 19933e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19943e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19953e230dd2SCorentin Chary break; 1996ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 1997ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 1998ab99e5c1SLei Li break; 19993e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 2000d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 20013e230dd2SCorentin Chary break; 20023e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 2003b31f519eSCorentin Chary if (vs->vd->lossy) { 2004d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 2005b31f519eSCorentin Chary } 20063e230dd2SCorentin Chary break; 20073e230dd2SCorentin Chary default: 20083e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 20093e230dd2SCorentin Chary break; 20103e230dd2SCorentin Chary } 20113e230dd2SCorentin Chary } 20123e230dd2SCorentin Chary vnc_desktop_resize(vs); 20139e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2014ab99e5c1SLei Li vnc_led_state_change(vs); 20153e230dd2SCorentin Chary } 20163e230dd2SCorentin Chary 20173e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20183e230dd2SCorentin Chary { 20199f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20209f64916dSGerd Hoffmann 20219f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20223e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20233e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20243e230dd2SCorentin Chary } else { 20253e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20263e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20273e230dd2SCorentin Chary } 20283e230dd2SCorentin Chary } 20293e230dd2SCorentin Chary 20300c426e45SAlexander Graf static void send_color_map(VncState *vs) 20310c426e45SAlexander Graf { 20320c426e45SAlexander Graf int i; 20330c426e45SAlexander Graf 20340c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 20350c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 20360c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 20370c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 20380c426e45SAlexander Graf 20390c426e45SAlexander Graf for (i = 0; i < 256; i++) { 20400c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 20410c426e45SAlexander Graf 20420c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 20430c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 20440c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 20450c426e45SAlexander Graf } 20460c426e45SAlexander Graf } 20470c426e45SAlexander Graf 2048ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 20493e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20503e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20513e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20523e230dd2SCorentin Chary { 20533e230dd2SCorentin Chary if (!true_color_flag) { 20540c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 20550c426e45SAlexander Graf bits_per_pixel = 8; 20560c426e45SAlexander Graf red_max = 7; 20570c426e45SAlexander Graf green_max = 7; 20580c426e45SAlexander Graf blue_max = 3; 20590c426e45SAlexander Graf red_shift = 0; 20600c426e45SAlexander Graf green_shift = 3; 20610c426e45SAlexander Graf blue_shift = 6; 20623e230dd2SCorentin Chary } 20633e230dd2SCorentin Chary 2064e6908bfeSPetr Matousek switch (bits_per_pixel) { 2065e6908bfeSPetr Matousek case 8: 2066e6908bfeSPetr Matousek case 16: 2067e6908bfeSPetr Matousek case 32: 2068e6908bfeSPetr Matousek break; 2069e6908bfeSPetr Matousek default: 2070e6908bfeSPetr Matousek vnc_client_error(vs); 2071e6908bfeSPetr Matousek return; 2072e6908bfeSPetr Matousek } 2073e6908bfeSPetr Matousek 20744c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 20757c9209e7SCédric Le Goater vs->client_pf.rbits = ctpopl(red_max); 20769f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 20779f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 20784c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 20797c9209e7SCédric Le Goater vs->client_pf.gbits = ctpopl(green_max); 20809f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 20819f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 20824c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 20837c9209e7SCédric Le Goater vs->client_pf.bbits = ctpopl(blue_max); 20849f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 20859f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 20869f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 20879f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 20889f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 20899f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 20903e230dd2SCorentin Chary 20910c426e45SAlexander Graf if (!true_color_flag) { 20920c426e45SAlexander Graf send_color_map(vs); 20930c426e45SAlexander Graf } 20940c426e45SAlexander Graf 20953e230dd2SCorentin Chary set_pixel_conversion(vs); 20963e230dd2SCorentin Chary 20971d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 20981d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 20993e230dd2SCorentin Chary } 21003e230dd2SCorentin Chary 21013e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 21023e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 21033e230dd2SCorentin Chary 21049f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 21059f64916dSGerd Hoffmann 21069f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 21079f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 21083e230dd2SCorentin Chary 21093e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 21103e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 21113e230dd2SCorentin Chary #else 21123e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21133e230dd2SCorentin Chary #endif 21143e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21159f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21169f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21179f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21189f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21199f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21209f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21219f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21223e230dd2SCorentin Chary 21233e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21243e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21253e230dd2SCorentin Chary } 21263e230dd2SCorentin Chary 21273e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21283e230dd2SCorentin Chary { 21293e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 21303e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2131bd023f95SCorentin Chary vnc_lock_output(vs); 21323e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 21333e230dd2SCorentin Chary vnc_write_u8(vs, 0); 21343e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2135d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2136bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2137bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2138d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 21393e230dd2SCorentin Chary pixel_format_message(vs); 2140bd023f95SCorentin Chary vnc_unlock_output(vs); 21413e230dd2SCorentin Chary vnc_flush(vs); 21423e230dd2SCorentin Chary } else { 21433e230dd2SCorentin Chary set_pixel_conversion(vs); 21443e230dd2SCorentin Chary } 21453e230dd2SCorentin Chary } 21463e230dd2SCorentin Chary 21473e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 21483e230dd2SCorentin Chary { 21493e230dd2SCorentin Chary int i; 21503e230dd2SCorentin Chary uint16_t limit; 21513e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21523e230dd2SCorentin Chary 21533e230dd2SCorentin Chary if (data[0] > 3) { 21540f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21553e230dd2SCorentin Chary } 21563e230dd2SCorentin Chary 21573e230dd2SCorentin Chary switch (data[0]) { 21583e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21593e230dd2SCorentin Chary if (len == 1) 21603e230dd2SCorentin Chary return 20; 21613e230dd2SCorentin Chary 2162ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 21633e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 21643e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 21653e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 21663e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 21673e230dd2SCorentin Chary break; 21683e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 21693e230dd2SCorentin Chary if (len == 1) 21703e230dd2SCorentin Chary return 4; 21713e230dd2SCorentin Chary 21723e230dd2SCorentin Chary if (len == 4) { 21733e230dd2SCorentin Chary limit = read_u16(data, 2); 21743e230dd2SCorentin Chary if (limit > 0) 21753e230dd2SCorentin Chary return 4 + (limit * 4); 21763e230dd2SCorentin Chary } else 21773e230dd2SCorentin Chary limit = read_u16(data, 2); 21783e230dd2SCorentin Chary 21793e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 21803e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 21813e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 21823e230dd2SCorentin Chary } 21833e230dd2SCorentin Chary 21843e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 21853e230dd2SCorentin Chary break; 21863e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 21873e230dd2SCorentin Chary if (len == 1) 21883e230dd2SCorentin Chary return 10; 21893e230dd2SCorentin Chary 21903e230dd2SCorentin Chary framebuffer_update_request(vs, 21913e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 21923e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 21933e230dd2SCorentin Chary break; 21943e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 21953e230dd2SCorentin Chary if (len == 1) 21963e230dd2SCorentin Chary return 8; 21973e230dd2SCorentin Chary 21983e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 21993e230dd2SCorentin Chary break; 22003e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 22013e230dd2SCorentin Chary if (len == 1) 22023e230dd2SCorentin Chary return 6; 22033e230dd2SCorentin Chary 22043e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 22053e230dd2SCorentin Chary break; 22063e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2207f9a70e79SPeter Lieven if (len == 1) { 22083e230dd2SCorentin Chary return 8; 2209f9a70e79SPeter Lieven } 22103e230dd2SCorentin Chary if (len == 8) { 22113e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2212f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2213f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2214f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2215f9a70e79SPeter Lieven vnc_client_error(vs); 2216f9a70e79SPeter Lieven break; 2217f9a70e79SPeter Lieven } 2218f9a70e79SPeter Lieven if (dlen > 0) { 22193e230dd2SCorentin Chary return 8 + dlen; 22203e230dd2SCorentin Chary } 2221f9a70e79SPeter Lieven } 22223e230dd2SCorentin Chary 22233e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22243e230dd2SCorentin Chary break; 22253e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22263e230dd2SCorentin Chary if (len == 1) 22273e230dd2SCorentin Chary return 2; 22283e230dd2SCorentin Chary 22293e230dd2SCorentin Chary switch (read_u8(data, 1)) { 22303e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 22313e230dd2SCorentin Chary if (len == 2) 22323e230dd2SCorentin Chary return 12; 22333e230dd2SCorentin Chary 22343e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 22353e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 22363e230dd2SCorentin Chary break; 22373e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 22383e230dd2SCorentin Chary if (len == 2) 22393e230dd2SCorentin Chary return 4; 22403e230dd2SCorentin Chary 22413e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 22423e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 22433e230dd2SCorentin Chary audio_add(vs); 22443e230dd2SCorentin Chary break; 22453e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 22463e230dd2SCorentin Chary audio_del(vs); 22473e230dd2SCorentin Chary break; 22483e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22493e230dd2SCorentin Chary if (len == 4) 22503e230dd2SCorentin Chary return 10; 22513e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22523e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22533e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22543e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22553e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22563e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22573e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22583e230dd2SCorentin Chary default: 2259153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 22603e230dd2SCorentin Chary vnc_client_error(vs); 22613e230dd2SCorentin Chary break; 22623e230dd2SCorentin Chary } 22633e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 22643e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2265090fdc83SMarc-André Lureau VNC_DEBUG("Invalid audio channel count %d\n", 22663e230dd2SCorentin Chary read_u8(data, 5)); 22673e230dd2SCorentin Chary vnc_client_error(vs); 22683e230dd2SCorentin Chary break; 22693e230dd2SCorentin Chary } 22703e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 22713e230dd2SCorentin Chary break; 22723e230dd2SCorentin Chary default: 2273153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 22743e230dd2SCorentin Chary vnc_client_error(vs); 22753e230dd2SCorentin Chary break; 22763e230dd2SCorentin Chary } 22773e230dd2SCorentin Chary break; 22783e230dd2SCorentin Chary 22793e230dd2SCorentin Chary default: 2280153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 22813e230dd2SCorentin Chary vnc_client_error(vs); 22823e230dd2SCorentin Chary break; 22833e230dd2SCorentin Chary } 22843e230dd2SCorentin Chary break; 22853e230dd2SCorentin Chary default: 2286153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 22873e230dd2SCorentin Chary vnc_client_error(vs); 22883e230dd2SCorentin Chary break; 22893e230dd2SCorentin Chary } 22903e230dd2SCorentin Chary 22913e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22923e230dd2SCorentin Chary return 0; 22933e230dd2SCorentin Chary } 22943e230dd2SCorentin Chary 22953e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 22963e230dd2SCorentin Chary { 22973e230dd2SCorentin Chary char buf[1024]; 22988cf36489SGerd Hoffmann VncShareMode mode; 22993e230dd2SCorentin Chary int size; 23003e230dd2SCorentin Chary 23018cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 23028cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 23038cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 23048cf36489SGerd Hoffmann /* 23058cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 23068cf36489SGerd Hoffmann * 23078cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 23088cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 23098cf36489SGerd Hoffmann * reasons. 23108cf36489SGerd Hoffmann */ 23118cf36489SGerd Hoffmann break; 23128cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23138cf36489SGerd Hoffmann /* 23148cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23158cf36489SGerd Hoffmann * 23168cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23178cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23188cf36489SGerd Hoffmann * as no exclusive connection exists. 23198cf36489SGerd Hoffmann * 23208cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23218cf36489SGerd Hoffmann */ 23228cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23238cf36489SGerd Hoffmann VncState *client; 23248cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23258cf36489SGerd Hoffmann if (vs == client) { 23268cf36489SGerd Hoffmann continue; 23278cf36489SGerd Hoffmann } 23288cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23298cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 23308cf36489SGerd Hoffmann continue; 23318cf36489SGerd Hoffmann } 23328cf36489SGerd Hoffmann vnc_disconnect_start(client); 23338cf36489SGerd Hoffmann } 23348cf36489SGerd Hoffmann } 23358cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 23368cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 23378cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23388cf36489SGerd Hoffmann return 0; 23398cf36489SGerd Hoffmann } 23408cf36489SGerd Hoffmann } 23418cf36489SGerd Hoffmann break; 23428cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 23438cf36489SGerd Hoffmann /* 23448cf36489SGerd Hoffmann * Policy: Shared connects only. 23458cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 23468cf36489SGerd Hoffmann * 23478cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 23488cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23498cf36489SGerd Hoffmann * client disconnect everybody else. 23508cf36489SGerd Hoffmann */ 23518cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23528cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23538cf36489SGerd Hoffmann return 0; 23548cf36489SGerd Hoffmann } 23558cf36489SGerd Hoffmann break; 23568cf36489SGerd Hoffmann } 23578cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23588cf36489SGerd Hoffmann 2359e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2360e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2361e5f34cddSGerd Hoffmann return 0; 2362e5f34cddSGerd Hoffmann } 2363e5f34cddSGerd Hoffmann 2364bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2365bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 23663e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 23673e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 23683e230dd2SCorentin Chary 23693e230dd2SCorentin Chary pixel_format_message(vs); 23703e230dd2SCorentin Chary 237197efe4f9SThomas Huth if (qemu_name) { 23723e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 237397efe4f9SThomas Huth if (size > sizeof(buf)) { 237497efe4f9SThomas Huth size = sizeof(buf); 237597efe4f9SThomas Huth } 237697efe4f9SThomas Huth } else { 23773e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 237897efe4f9SThomas Huth } 23793e230dd2SCorentin Chary 23803e230dd2SCorentin Chary vnc_write_u32(vs, size); 23813e230dd2SCorentin Chary vnc_write(vs, buf, size); 23823e230dd2SCorentin Chary vnc_flush(vs); 23833e230dd2SCorentin Chary 23843e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2385fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 23863e230dd2SCorentin Chary 23873e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23883e230dd2SCorentin Chary 23893e230dd2SCorentin Chary return 0; 23903e230dd2SCorentin Chary } 23913e230dd2SCorentin Chary 23923e230dd2SCorentin Chary void start_client_init(VncState *vs) 23933e230dd2SCorentin Chary { 23943e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 23953e230dd2SCorentin Chary } 23963e230dd2SCorentin Chary 23973e230dd2SCorentin Chary static void make_challenge(VncState *vs) 23983e230dd2SCorentin Chary { 23993e230dd2SCorentin Chary int i; 24003e230dd2SCorentin Chary 24013e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 24023e230dd2SCorentin Chary 24033e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 24043e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 24053e230dd2SCorentin Chary } 24063e230dd2SCorentin Chary 24073e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 24083e230dd2SCorentin Chary { 24093e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2410800567a6SDaniel P. Berrange size_t i, pwlen; 24113e230dd2SCorentin Chary unsigned char key[8]; 24123c9405a0SGerd Hoffmann time_t now = time(NULL); 241360928458SGonglei QCryptoCipher *cipher = NULL; 2414800567a6SDaniel P. Berrange Error *err = NULL; 24153e230dd2SCorentin Chary 24161cd20f8bSAnthony Liguori if (!vs->vd->password) { 24177364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); 24186bffdf0fSGerd Hoffmann goto reject; 24193e230dd2SCorentin Chary } 24203c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 24217364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); 24223c9405a0SGerd Hoffmann goto reject; 24233c9405a0SGerd Hoffmann } 24243e230dd2SCorentin Chary 24253e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24263e230dd2SCorentin Chary 24273e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24283e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24293e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24303e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2431800567a6SDaniel P. Berrange 2432800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2433800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2434800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2435800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2436800567a6SDaniel P. Berrange &err); 2437800567a6SDaniel P. Berrange if (!cipher) { 24387364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", 2439800567a6SDaniel P. Berrange error_get_pretty(err)); 2440800567a6SDaniel P. Berrange error_free(err); 2441800567a6SDaniel P. Berrange goto reject; 2442800567a6SDaniel P. Berrange } 2443800567a6SDaniel P. Berrange 2444a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2445800567a6SDaniel P. Berrange vs->challenge, 2446800567a6SDaniel P. Berrange response, 2447800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2448800567a6SDaniel P. Berrange &err) < 0) { 24497364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", 2450800567a6SDaniel P. Berrange error_get_pretty(err)); 2451800567a6SDaniel P. Berrange error_free(err); 2452800567a6SDaniel P. Berrange goto reject; 2453800567a6SDaniel P. Berrange } 24543e230dd2SCorentin Chary 24553e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 24563e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 24577364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); 24586bffdf0fSGerd Hoffmann goto reject; 24596bffdf0fSGerd Hoffmann } else { 24607364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 24616bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 24626bffdf0fSGerd Hoffmann vnc_flush(vs); 24636bffdf0fSGerd Hoffmann 24646bffdf0fSGerd Hoffmann start_client_init(vs); 24656bffdf0fSGerd Hoffmann } 246660928458SGonglei 246760928458SGonglei qcrypto_cipher_free(cipher); 24686bffdf0fSGerd Hoffmann return 0; 24696bffdf0fSGerd Hoffmann 24706bffdf0fSGerd Hoffmann reject: 24713e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 24723e230dd2SCorentin Chary if (vs->minor >= 8) { 24733e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24743e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24753e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24763e230dd2SCorentin Chary } 24773e230dd2SCorentin Chary vnc_flush(vs); 24783e230dd2SCorentin Chary vnc_client_error(vs); 247960928458SGonglei qcrypto_cipher_free(cipher); 24803e230dd2SCorentin Chary return 0; 24813e230dd2SCorentin Chary } 24823e230dd2SCorentin Chary 24833e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 24843e230dd2SCorentin Chary { 24853e230dd2SCorentin Chary make_challenge(vs); 24863e230dd2SCorentin Chary /* Send client a 'random' challenge */ 24873e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 24883e230dd2SCorentin Chary vnc_flush(vs); 24893e230dd2SCorentin Chary 24903e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 24913e230dd2SCorentin Chary } 24923e230dd2SCorentin Chary 24933e230dd2SCorentin Chary 24943e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 24953e230dd2SCorentin Chary { 24963e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 24973e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 24987e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 24997364dbdaSDaniel P. Berrange trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); 25003e230dd2SCorentin Chary vnc_write_u32(vs, 1); 25013e230dd2SCorentin Chary if (vs->minor >= 8) { 25023e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25033e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25043e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25053e230dd2SCorentin Chary } 25063e230dd2SCorentin Chary vnc_client_error(vs); 25073e230dd2SCorentin Chary } else { /* Accept requested auth */ 25087364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25097e7e2ebcSDaniel P. Berrange switch (vs->auth) { 25103e230dd2SCorentin Chary case VNC_AUTH_NONE: 25113e230dd2SCorentin Chary if (vs->minor >= 8) { 25123e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25133e230dd2SCorentin Chary vnc_flush(vs); 25143e230dd2SCorentin Chary } 25157364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25163e230dd2SCorentin Chary start_client_init(vs); 25173e230dd2SCorentin Chary break; 25183e230dd2SCorentin Chary 25193e230dd2SCorentin Chary case VNC_AUTH_VNC: 25203e230dd2SCorentin Chary start_auth_vnc(vs); 25213e230dd2SCorentin Chary break; 25223e230dd2SCorentin Chary 25233e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25243e230dd2SCorentin Chary start_auth_vencrypt(vs); 25253e230dd2SCorentin Chary break; 25263e230dd2SCorentin Chary 25273e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25283e230dd2SCorentin Chary case VNC_AUTH_SASL: 25293e230dd2SCorentin Chary start_auth_sasl(vs); 25303e230dd2SCorentin Chary break; 25313e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 25323e230dd2SCorentin Chary 25333e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 25347364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); 25353e230dd2SCorentin Chary vnc_write_u8(vs, 1); 25363e230dd2SCorentin Chary if (vs->minor >= 8) { 25373e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25383e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25393e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25403e230dd2SCorentin Chary } 25413e230dd2SCorentin Chary vnc_client_error(vs); 25423e230dd2SCorentin Chary } 25433e230dd2SCorentin Chary } 25443e230dd2SCorentin Chary return 0; 25453e230dd2SCorentin Chary } 25463e230dd2SCorentin Chary 25473e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 25483e230dd2SCorentin Chary { 25493e230dd2SCorentin Chary char local[13]; 25503e230dd2SCorentin Chary 25513e230dd2SCorentin Chary memcpy(local, version, 12); 25523e230dd2SCorentin Chary local[12] = 0; 25533e230dd2SCorentin Chary 25543e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 25553e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 25563e230dd2SCorentin Chary vnc_client_error(vs); 25573e230dd2SCorentin Chary return 0; 25583e230dd2SCorentin Chary } 25593e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 25603e230dd2SCorentin Chary if (vs->major != 3 || 25613e230dd2SCorentin Chary (vs->minor != 3 && 25623e230dd2SCorentin Chary vs->minor != 4 && 25633e230dd2SCorentin Chary vs->minor != 5 && 25643e230dd2SCorentin Chary vs->minor != 7 && 25653e230dd2SCorentin Chary vs->minor != 8)) { 25663e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 25673e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25683e230dd2SCorentin Chary vnc_flush(vs); 25693e230dd2SCorentin Chary vnc_client_error(vs); 25703e230dd2SCorentin Chary return 0; 25713e230dd2SCorentin Chary } 25723e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 25733e230dd2SCorentin Chary * as equivalent to v3.3 by servers 25743e230dd2SCorentin Chary */ 25753e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 25763e230dd2SCorentin Chary vs->minor = 3; 25773e230dd2SCorentin Chary 25783e230dd2SCorentin Chary if (vs->minor == 3) { 25797364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25807e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 25817e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25823e230dd2SCorentin Chary vnc_flush(vs); 25837364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25843e230dd2SCorentin Chary start_client_init(vs); 25857e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 25863e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 25877e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25883e230dd2SCorentin Chary vnc_flush(vs); 25893e230dd2SCorentin Chary start_auth_vnc(vs); 25903e230dd2SCorentin Chary } else { 25917364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, 25927364dbdaSDaniel P. Berrange "Unsupported auth method for v3.3", ""); 25933e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25943e230dd2SCorentin Chary vnc_flush(vs); 25953e230dd2SCorentin Chary vnc_client_error(vs); 25963e230dd2SCorentin Chary } 25973e230dd2SCorentin Chary } else { 25983e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 25997e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 26003e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 26013e230dd2SCorentin Chary vnc_flush(vs); 26023e230dd2SCorentin Chary } 26033e230dd2SCorentin Chary 26043e230dd2SCorentin Chary return 0; 26053e230dd2SCorentin Chary } 26063e230dd2SCorentin Chary 2607999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2608999342a0SCorentin Chary { 2609999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2610999342a0SCorentin Chary 2611999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2612999342a0SCorentin Chary } 2613999342a0SCorentin Chary 26147d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26157d964c9dSCorentin Chary { 26167d964c9dSCorentin Chary int i, j; 26177d964c9dSCorentin Chary 26187d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26197d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26207d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26217d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26227d964c9dSCorentin Chary 2623207f328aSCorentin Chary for (j = y; j <= h; j++) { 2624207f328aSCorentin Chary for (i = x; i <= w; i++) { 26257d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26267d964c9dSCorentin Chary } 26277d964c9dSCorentin Chary } 26287d964c9dSCorentin Chary } 26297d964c9dSCorentin Chary 26307d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26317d964c9dSCorentin Chary { 26327d964c9dSCorentin Chary VncState *vs; 26337d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 26347d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 26357d964c9dSCorentin Chary int has_dirty = 0; 26367d964c9dSCorentin Chary 26375a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 26385a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 26397d964c9dSCorentin Chary 26407d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2641bc2429b9SCorentin Chary int j; 26427d964c9dSCorentin Chary 26437d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 26447d964c9dSCorentin Chary if (vs->output.offset) { 26457d964c9dSCorentin Chary continue; 26467d964c9dSCorentin Chary } 26477d964c9dSCorentin Chary 26487d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 26497d964c9dSCorentin Chary continue; 26507d964c9dSCorentin Chary } 2651207f328aSCorentin Chary 26527d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 26537d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2654b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2655b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2656b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 26577d964c9dSCorentin Chary } 26587d964c9dSCorentin Chary has_dirty++; 26597d964c9dSCorentin Chary } 2660207f328aSCorentin Chary 26617d964c9dSCorentin Chary return has_dirty; 26627d964c9dSCorentin Chary } 26637d964c9dSCorentin Chary 26647d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2665999342a0SCorentin Chary { 2666eebe0b79SGerd Hoffmann int width = MIN(pixman_image_get_width(vd->guest.fb), 2667eebe0b79SGerd Hoffmann pixman_image_get_width(vd->server)); 2668eebe0b79SGerd Hoffmann int height = MIN(pixman_image_get_height(vd->guest.fb), 2669eebe0b79SGerd Hoffmann pixman_image_get_height(vd->server)); 2670999342a0SCorentin Chary int x, y; 2671999342a0SCorentin Chary struct timeval res; 26727d964c9dSCorentin Chary int has_dirty = 0; 2673999342a0SCorentin Chary 26749f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26759f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2676999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2677999342a0SCorentin Chary 2678999342a0SCorentin Chary rect->updated = false; 2679999342a0SCorentin Chary } 2680999342a0SCorentin Chary } 2681999342a0SCorentin Chary 2682ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2683999342a0SCorentin Chary 2684999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 26857d964c9dSCorentin Chary return has_dirty; 2686999342a0SCorentin Chary } 2687999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2688999342a0SCorentin Chary 26899f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26909f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2691999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2692999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2693999342a0SCorentin Chary struct timeval min, max; 2694999342a0SCorentin Chary 2695999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2696999342a0SCorentin Chary continue ; 2697999342a0SCorentin Chary } 2698999342a0SCorentin Chary 2699999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2700ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2701999342a0SCorentin Chary 2702999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2703999342a0SCorentin Chary rect->freq = 0; 27047d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2705999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2706999342a0SCorentin Chary continue ; 2707999342a0SCorentin Chary } 2708999342a0SCorentin Chary 2709999342a0SCorentin Chary min = rect->times[rect->idx]; 2710999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2711ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2712999342a0SCorentin Chary 2713999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2714999342a0SCorentin Chary rect->freq /= count; 2715999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2716999342a0SCorentin Chary } 2717999342a0SCorentin Chary } 27187d964c9dSCorentin Chary return has_dirty; 2719999342a0SCorentin Chary } 2720999342a0SCorentin Chary 2721999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2722999342a0SCorentin Chary { 2723999342a0SCorentin Chary int i, j; 2724999342a0SCorentin Chary double total = 0; 2725999342a0SCorentin Chary int num = 0; 2726999342a0SCorentin Chary 27275a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 27285a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 2729999342a0SCorentin Chary 2730999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2731999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2732999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2733999342a0SCorentin Chary num++; 2734999342a0SCorentin Chary } 2735999342a0SCorentin Chary } 2736999342a0SCorentin Chary 2737999342a0SCorentin Chary if (num) { 2738999342a0SCorentin Chary return total / num; 2739999342a0SCorentin Chary } else { 2740999342a0SCorentin Chary return 0; 2741999342a0SCorentin Chary } 2742999342a0SCorentin Chary } 2743999342a0SCorentin Chary 2744999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2745999342a0SCorentin Chary { 2746999342a0SCorentin Chary VncRectStat *rect; 2747999342a0SCorentin Chary 2748999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2749999342a0SCorentin Chary if (rect->updated) { 2750999342a0SCorentin Chary return ; 2751999342a0SCorentin Chary } 2752999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2753999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2754999342a0SCorentin Chary rect->updated = true; 2755999342a0SCorentin Chary } 2756999342a0SCorentin Chary 27573e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 27583e230dd2SCorentin Chary { 2759bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2760bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2761bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2762bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2763eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 276412b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 27653e230dd2SCorentin Chary VncState *vs; 27663e230dd2SCorentin Chary int has_dirty = 0; 27679f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 27683e230dd2SCorentin Chary 276980e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2770999342a0SCorentin Chary 277180e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2772999342a0SCorentin Chary gettimeofday(&tv, NULL); 27737d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 277480e0c8c3SCorentin Chary } 2775999342a0SCorentin Chary 27763e230dd2SCorentin Chary /* 27773e230dd2SCorentin Chary * Walk through the guest dirty map. 27783e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 27793e230dd2SCorentin Chary * Update server dirty map. 27803e230dd2SCorentin Chary */ 2781bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2782eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2783eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2784bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2785bea60dd7SPeter Lieven server_stride); 27869f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 27879f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 27889f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 278912b316d4SPeter Lieven } else { 2790eb8934b0SGerd Hoffmann int guest_bpp = 2791eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 279212b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 279312b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2794659c90eeSMarc-André Lureau guest_ll = pixman_image_get_width(vd->guest.fb) * (DIV_ROUND_UP(guest_bpp, 8)); 27959f64916dSGerd Hoffmann } 2796eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 279712b316d4SPeter Lieven 279812b316d4SPeter Lieven for (;;) { 27993e230dd2SCorentin Chary int x; 280012b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 280112b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 280212b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 280312b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 280412b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 280512b316d4SPeter Lieven /* no more dirty bits */ 280612b316d4SPeter Lieven break; 280712b316d4SPeter Lieven } 280812b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 280912b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 281012b316d4SPeter Lieven 281112b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 28123e230dd2SCorentin Chary 28139f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2814bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28159f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28169f64916dSGerd Hoffmann } else { 281712b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28189f64916dSGerd Hoffmann } 281912b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28203e230dd2SCorentin Chary 282112b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 282212b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2823bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 282412b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28253e230dd2SCorentin Chary continue; 2826b4c85ddcSPeter Lieven } 2827eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2828eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2829bea60dd7SPeter Lieven } 2830eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2831bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28323e230dd2SCorentin Chary continue; 2833b4c85ddcSPeter Lieven } 2834bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 283512b316d4SPeter Lieven if (!vd->non_adaptive) { 283612b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 283712b316d4SPeter Lieven y, &tv); 283812b316d4SPeter Lieven } 28393e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 284012b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 28413e230dd2SCorentin Chary } 28423e230dd2SCorentin Chary has_dirty++; 28433e230dd2SCorentin Chary } 284412b316d4SPeter Lieven 284512b316d4SPeter Lieven y++; 28463e230dd2SCorentin Chary } 28479f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 28483e230dd2SCorentin Chary return has_dirty; 28493e230dd2SCorentin Chary } 28503e230dd2SCorentin Chary 28510f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 28523e230dd2SCorentin Chary { 28530f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 28543e230dd2SCorentin Chary VncState *vs, *vn; 28553e230dd2SCorentin Chary int has_dirty, rects = 0; 28563e230dd2SCorentin Chary 28579d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 28589d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 28599d6b2070SChenLiang return; 28609d6b2070SChenLiang } 28619d6b2070SChenLiang 28621d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 28633e230dd2SCorentin Chary 2864bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 28650f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2866bd023f95SCorentin Chary return; 2867bd023f95SCorentin Chary } 2868bd023f95SCorentin Chary 28693e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2870bd023f95SCorentin Chary vnc_unlock_display(vd); 28713e230dd2SCorentin Chary 28723e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 28736af998dbSDaniel P. Berrange rects += vnc_update_client(vs, has_dirty); 28743e230dd2SCorentin Chary /* vs might be free()ed here */ 28753e230dd2SCorentin Chary } 2876bd023f95SCorentin Chary 28773e230dd2SCorentin Chary if (has_dirty && rects) { 28780f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 28790f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 28800f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 28810f7b2864SGerd Hoffmann } 28823e230dd2SCorentin Chary } else { 28830f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 28840f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 28850f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 28863e230dd2SCorentin Chary } 28873e230dd2SCorentin Chary } 28883e230dd2SCorentin Chary } 28893e230dd2SCorentin Chary 289004d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 28912c8cf549SMichael Tokarev bool skipauth, bool websocket) 28923e230dd2SCorentin Chary { 2893fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 289490cd03a3SDaniel P. Berrange bool first_client = QTAILQ_EMPTY(&vd->clients); 28957d964c9dSCorentin Chary int i; 28967d964c9dSCorentin Chary 2897ad6374c4SDaniel P. Berrange trace_vnc_client_connect(vs, sioc); 289804d2529dSDaniel P. Berrange vs->sioc = sioc; 289904d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 290004d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 290104d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2902d616ccc5SGerd Hoffmann vs->vd = vd; 29037e7e2ebcSDaniel P. Berrange 290404d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 290504d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 290604d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2907543b9580SGerd Hoffmann 290804d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 290904d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 291004d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2911543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 291204d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2913543b9580SGerd Hoffmann #endif 2914543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 291504d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2916543b9580SGerd Hoffmann #endif 291704d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 291804d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 291904d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 292004d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2921543b9580SGerd Hoffmann 29227e7e2ebcSDaniel P. Berrange if (skipauth) { 29237e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29247e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29257e7e2ebcSDaniel P. Berrange } else { 2926f9148c8aSDaniel P. Berrange if (websocket) { 2927f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2928f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2929f9148c8aSDaniel P. Berrange } else { 29307e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29317e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29327e7e2ebcSDaniel P. Berrange } 2933f9148c8aSDaniel P. Berrange } 293404d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 293504d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 29367e7e2ebcSDaniel P. Berrange 29377267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 29387d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 2939fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 29407d964c9dSCorentin Chary } 29413e230dd2SCorentin Chary 294204d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 29430f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 294404d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 2945a75d6f07SBrandon Carpenter if (vs->ioc_tag) { 2946a75d6f07SBrandon Carpenter g_source_remove(vs->ioc_tag); 2947a75d6f07SBrandon Carpenter } 29487536ee4bSTim Hardeck if (websocket) { 29497536ee4bSTim Hardeck vs->websocket = 1; 295038e5756aSDaniel P. Berrange if (vd->tlscreds) { 295104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 295204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 29533e305e4aSDaniel P. Berrange } else { 295404d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 295504d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 29560057a0d5STim Hardeck } 295704d2529dSDaniel P. Berrange } else { 295804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 295904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 29607536ee4bSTim Hardeck } 29613e230dd2SCorentin Chary 29623e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2963fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 29648cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 29653e230dd2SCorentin Chary 29663e230dd2SCorentin Chary vs->last_x = -1; 29673e230dd2SCorentin Chary vs->last_y = -1; 29683e230dd2SCorentin Chary 29693e230dd2SCorentin Chary vs->as.freq = 44100; 29703e230dd2SCorentin Chary vs->as.nchannels = 2; 29713e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 29723e230dd2SCorentin Chary vs->as.endianness = 0; 29733e230dd2SCorentin Chary 2974bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2975175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2976bd023f95SCorentin Chary 2977e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 2978c7628bffSGerd Hoffmann if (first_client) { 2979c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 2980c7628bffSGerd Hoffmann } 29813e230dd2SCorentin Chary 29821d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29833e230dd2SCorentin Chary 298490cd03a3SDaniel P. Berrange if (!vs->websocket) { 2985dbee9897SDaniel P. Berrange vnc_start_protocol(vs); 298690cd03a3SDaniel P. Berrange } 298790cd03a3SDaniel P. Berrange 298890cd03a3SDaniel P. Berrange if (vd->num_connecting > vd->connections_limit) { 298990cd03a3SDaniel P. Berrange QTAILQ_FOREACH(vs, &vd->clients, next) { 299090cd03a3SDaniel P. Berrange if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 299190cd03a3SDaniel P. Berrange vnc_disconnect_start(vs); 299290cd03a3SDaniel P. Berrange return; 299390cd03a3SDaniel P. Berrange } 299490cd03a3SDaniel P. Berrange } 299590cd03a3SDaniel P. Berrange } 299690cd03a3SDaniel P. Berrange } 299790cd03a3SDaniel P. Berrange 2998dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs) 299990cd03a3SDaniel P. Berrange { 30003e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 30013e230dd2SCorentin Chary vnc_flush(vs); 30023e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 30033e230dd2SCorentin Chary 30043e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 30053e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 30063e230dd2SCorentin Chary } 30073e230dd2SCorentin Chary 300804d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 300904d2529dSDaniel P. Berrange GIOCondition condition, 301004d2529dSDaniel P. Berrange void *opaque) 30113e230dd2SCorentin Chary { 3012bf01c179SDaniel P. Berrange VncDisplay *vd = opaque; 301304d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 301404d2529dSDaniel P. Berrange Error *err = NULL; 30154ee74fa7SDaniel P. Berrange bool isWebsock = false; 30164ee74fa7SDaniel P. Berrange size_t i; 30174ee74fa7SDaniel P. Berrange 30184ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30194ee74fa7SDaniel P. Berrange if (ioc == QIO_CHANNEL(vd->lwebsock[i])) { 30204ee74fa7SDaniel P. Berrange isWebsock = true; 30214ee74fa7SDaniel P. Berrange break; 30224ee74fa7SDaniel P. Berrange } 30234ee74fa7SDaniel P. Berrange } 30243e230dd2SCorentin Chary 302504d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 302604d2529dSDaniel P. Berrange if (sioc != NULL) { 302710bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), 30284ee74fa7SDaniel P. Berrange isWebsock ? "vnc-ws-server" : "vnc-server"); 302904d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 30304ee74fa7SDaniel P. Berrange vnc_connect(vd, sioc, false, isWebsock); 303104d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30328e9b0d24SDaniel P. Berrange } else { 303304d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 303404d2529dSDaniel P. Berrange error_free(err); 30357536ee4bSTim Hardeck } 30363e230dd2SCorentin Chary 303704d2529dSDaniel P. Berrange return TRUE; 30387536ee4bSTim Hardeck } 30397536ee4bSTim Hardeck 30407c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 30417c20b4a3SGerd Hoffmann .dpy_name = "vnc", 30420f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 30437c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3044c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 304534da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 30467c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 30477c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 30487c20b4a3SGerd Hoffmann }; 30497c20b4a3SGerd Hoffmann 305014f7143eSGerd Hoffmann void vnc_display_init(const char *id) 30513e230dd2SCorentin Chary { 3052bf01c179SDaniel P. Berrange VncDisplay *vd; 30534db14629SGerd Hoffmann 30544db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 30554db14629SGerd Hoffmann return; 30564db14629SGerd Hoffmann } 3057bf01c179SDaniel P. Berrange vd = g_malloc0(sizeof(*vd)); 30583e230dd2SCorentin Chary 3059bf01c179SDaniel P. Berrange vd->id = strdup(id); 3060bf01c179SDaniel P. Berrange QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); 30613e230dd2SCorentin Chary 3062bf01c179SDaniel P. Berrange QTAILQ_INIT(&vd->clients); 3063bf01c179SDaniel P. Berrange vd->expires = TIME_MAX; 30643e230dd2SCorentin Chary 306540066175SGerd Hoffmann if (keyboard_layout) { 306640066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 3067bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 306840066175SGerd Hoffmann } else { 3069bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 307040066175SGerd Hoffmann } 30713e230dd2SCorentin Chary 3072bf01c179SDaniel P. Berrange if (!vd->kbd_layout) { 30733e230dd2SCorentin Chary exit(1); 3074bf01c179SDaniel P. Berrange } 30753e230dd2SCorentin Chary 3076bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 3077bf01c179SDaniel P. Berrange vd->connections_limit = 32; 307812e29b16SDaniel P. Berrange 3079bf01c179SDaniel P. Berrange qemu_mutex_init(&vd->mutex); 3080bd023f95SCorentin Chary vnc_start_worker_thread(); 3081bd023f95SCorentin Chary 3082bf01c179SDaniel P. Berrange vd->dcl.ops = &dcl_ops; 3083bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 30843e230dd2SCorentin Chary } 30853e230dd2SCorentin Chary 30863e230dd2SCorentin Chary 3087bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd) 30883e230dd2SCorentin Chary { 30894ee74fa7SDaniel P. Berrange size_t i; 3090bf01c179SDaniel P. Berrange if (!vd) { 30913e230dd2SCorentin Chary return; 309204d2529dSDaniel P. Berrange } 3093bf01c179SDaniel P. Berrange vd->is_unix = false; 30944ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 30954ee74fa7SDaniel P. Berrange if (vd->lsock_tag[i]) { 30964ee74fa7SDaniel P. Berrange g_source_remove(vd->lsock_tag[i]); 30973e230dd2SCorentin Chary } 30984ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lsock[i])); 30994ee74fa7SDaniel P. Berrange } 31004ee74fa7SDaniel P. Berrange g_free(vd->lsock); 31014ee74fa7SDaniel P. Berrange g_free(vd->lsock_tag); 3102bf01c179SDaniel P. Berrange vd->lsock = NULL; 31032dc120beSGerd Hoffmann vd->lsock_tag = NULL; 31044ee74fa7SDaniel P. Berrange vd->nlsock = 0; 31054ee74fa7SDaniel P. Berrange 31064ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 31074ee74fa7SDaniel P. Berrange if (vd->lwebsock_tag[i]) { 31084ee74fa7SDaniel P. Berrange g_source_remove(vd->lwebsock_tag[i]); 310904d2529dSDaniel P. Berrange } 31104ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lwebsock[i])); 31117536ee4bSTim Hardeck } 31124ee74fa7SDaniel P. Berrange g_free(vd->lwebsock); 31134ee74fa7SDaniel P. Berrange g_free(vd->lwebsock_tag); 3114bf01c179SDaniel P. Berrange vd->lwebsock = NULL; 31152dc120beSGerd Hoffmann vd->lwebsock_tag = NULL; 31164ee74fa7SDaniel P. Berrange vd->nlwebsock = 0; 31174ee74fa7SDaniel P. Berrange 3118bf01c179SDaniel P. Berrange vd->auth = VNC_AUTH_INVALID; 3119bf01c179SDaniel P. Berrange vd->subauth = VNC_AUTH_INVALID; 3120bf01c179SDaniel P. Berrange if (vd->tlscreds) { 3121bf01c179SDaniel P. Berrange object_unparent(OBJECT(vd->tlscreds)); 3122bf01c179SDaniel P. Berrange vd->tlscreds = NULL; 3123bf01c179SDaniel P. Berrange } 3124bf01c179SDaniel P. Berrange g_free(vd->tlsaclname); 3125bf01c179SDaniel P. Berrange vd->tlsaclname = NULL; 3126a54f0d2bSPierre Ossman if (vd->lock_key_sync) { 3127a54f0d2bSPierre Ossman qemu_remove_led_event_handler(vd->led); 31282dc120beSGerd Hoffmann vd->led = NULL; 3129a54f0d2bSPierre Ossman } 31303e230dd2SCorentin Chary } 31313e230dd2SCorentin Chary 313214f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31331cd20f8bSAnthony Liguori { 3134bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31351cd20f8bSAnthony Liguori 3136bf01c179SDaniel P. Berrange if (!vd) { 3137a6aa9d3eSLuiz Capitulino return -EINVAL; 31381cd20f8bSAnthony Liguori } 3139bf01c179SDaniel P. Berrange if (vd->auth == VNC_AUTH_NONE) { 3140cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 31417ea7d36eSMarkus Armbruster "password auth using '-vnc ${dpy},password'.\n"); 3142cf864569SGerd Hoffmann return -EINVAL; 31431cd20f8bSAnthony Liguori } 31441cd20f8bSAnthony Liguori 3145bf01c179SDaniel P. Berrange g_free(vd->password); 3146bf01c179SDaniel P. Berrange vd->password = g_strdup(password); 3147a6aa9d3eSLuiz Capitulino 3148a6aa9d3eSLuiz Capitulino return 0; 31493e230dd2SCorentin Chary } 31503e230dd2SCorentin Chary 315114f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31523c9405a0SGerd Hoffmann { 3153bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31543c9405a0SGerd Hoffmann 3155bf01c179SDaniel P. Berrange if (!vd) { 31561643f2b2SGerd Hoffmann return -EINVAL; 31571643f2b2SGerd Hoffmann } 31581643f2b2SGerd Hoffmann 3159bf01c179SDaniel P. Berrange vd->expires = expires; 31603c9405a0SGerd Hoffmann return 0; 31613c9405a0SGerd Hoffmann } 31623c9405a0SGerd Hoffmann 3163bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd) 31643e230dd2SCorentin Chary { 3165bd269ebcSMarkus Armbruster SocketAddress *addr; 316604d2529dSDaniel P. Berrange Error *err = NULL; 31673e230dd2SCorentin Chary 31684ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 31694ee74fa7SDaniel P. Berrange return; 31704ee74fa7SDaniel P. Berrange } 31714ee74fa7SDaniel P. Berrange 31724ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], &err); 317304d2529dSDaniel P. Berrange if (!addr) { 317433df7bf3SPaolo Bonzini return; 317504d2529dSDaniel P. Berrange } 317604d2529dSDaniel P. Berrange 3177bd269ebcSMarkus Armbruster if (addr->type != SOCKET_ADDRESS_TYPE_INET) { 3178bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 317933df7bf3SPaolo Bonzini return; 318004d2529dSDaniel P. Berrange } 318133df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3182bd269ebcSMarkus Armbruster addr->u.inet.host, 3183bd269ebcSMarkus Armbruster addr->u.inet.port); 3184bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 31853e230dd2SCorentin Chary } 31863e230dd2SCorentin Chary 31874db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 31884db14629SGerd Hoffmann .name = "vnc", 31894db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 31904db14629SGerd Hoffmann .implied_opt_name = "vnc", 31914db14629SGerd Hoffmann .desc = { 31924db14629SGerd Hoffmann { 31934db14629SGerd Hoffmann .name = "vnc", 31944db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31954db14629SGerd Hoffmann },{ 31964db14629SGerd Hoffmann .name = "websocket", 31974db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31984db14629SGerd Hoffmann },{ 31993e305e4aSDaniel P. Berrange .name = "tls-creds", 32003e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 32013e305e4aSDaniel P. Berrange },{ 32023e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32034db14629SGerd Hoffmann .name = "x509", 32044db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32054db14629SGerd Hoffmann },{ 32064db14629SGerd Hoffmann .name = "share", 32074db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32084db14629SGerd Hoffmann },{ 32091d0d59feSGerd Hoffmann .name = "display", 32101d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 32111d0d59feSGerd Hoffmann },{ 32121d0d59feSGerd Hoffmann .name = "head", 32131d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32141d0d59feSGerd Hoffmann },{ 3215e5f34cddSGerd Hoffmann .name = "connections", 3216e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3217e5f34cddSGerd Hoffmann },{ 321888428b7aSGonglei .name = "to", 321988428b7aSGonglei .type = QEMU_OPT_NUMBER, 322088428b7aSGonglei },{ 322188428b7aSGonglei .name = "ipv4", 322288428b7aSGonglei .type = QEMU_OPT_BOOL, 322388428b7aSGonglei },{ 322488428b7aSGonglei .name = "ipv6", 322588428b7aSGonglei .type = QEMU_OPT_BOOL, 322688428b7aSGonglei },{ 32274db14629SGerd Hoffmann .name = "password", 32284db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32294db14629SGerd Hoffmann },{ 32304db14629SGerd Hoffmann .name = "reverse", 32314db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32324db14629SGerd Hoffmann },{ 32334db14629SGerd Hoffmann .name = "lock-key-sync", 32344db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32354db14629SGerd Hoffmann },{ 3236c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3237c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3238c5ce8333SGerd Hoffmann },{ 32394db14629SGerd Hoffmann .name = "sasl", 32404db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32414db14629SGerd Hoffmann },{ 32423e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32434db14629SGerd Hoffmann .name = "tls", 32444db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32454db14629SGerd Hoffmann },{ 32463e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32474db14629SGerd Hoffmann .name = "x509verify", 32488c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32494db14629SGerd Hoffmann },{ 32504db14629SGerd Hoffmann .name = "acl", 32514db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32524db14629SGerd Hoffmann },{ 32534db14629SGerd Hoffmann .name = "lossy", 32544db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32554db14629SGerd Hoffmann },{ 32564db14629SGerd Hoffmann .name = "non-adaptive", 32574db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32584db14629SGerd Hoffmann }, 32594db14629SGerd Hoffmann { /* end of list */ } 32604db14629SGerd Hoffmann }, 32614db14629SGerd Hoffmann }; 32624db14629SGerd Hoffmann 32630dd72e15SDaniel P. Berrange 32643e305e4aSDaniel P. Berrange static int 3265eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth, 3266eda24e18SDaniel P. Berrange int *subauth, 3267eda24e18SDaniel P. Berrange QCryptoTLSCreds *tlscreds, 32680dd72e15SDaniel P. Berrange bool password, 32690dd72e15SDaniel P. Berrange bool sasl, 32703e305e4aSDaniel P. Berrange bool websocket, 32713e305e4aSDaniel P. Berrange Error **errp) 32720dd72e15SDaniel P. Berrange { 32730dd72e15SDaniel P. Berrange /* 32740dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 32750dd72e15SDaniel P. Berrange * 32760dd72e15SDaniel P. Berrange * 1. none 32770dd72e15SDaniel P. Berrange * 2. vnc 32780dd72e15SDaniel P. Berrange * 3. sasl 32790dd72e15SDaniel P. Berrange * 32800dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 32810dd72e15SDaniel P. Berrange * 32820dd72e15SDaniel P. Berrange * 1. clear 32830dd72e15SDaniel P. Berrange * 2. tls 32840dd72e15SDaniel P. Berrange * 32850dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 32860dd72e15SDaniel P. Berrange * 32870dd72e15SDaniel P. Berrange * 1. anon 32880dd72e15SDaniel P. Berrange * 2. x509 32890dd72e15SDaniel P. Berrange * 32900dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 32910dd72e15SDaniel P. Berrange * 32920dd72e15SDaniel P. Berrange * 1. clear + none 32930dd72e15SDaniel P. Berrange * 2. clear + vnc 32940dd72e15SDaniel P. Berrange * 3. clear + sasl 32950dd72e15SDaniel P. Berrange * 4. tls + anon + none 32960dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 32970dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 32980dd72e15SDaniel P. Berrange * 7. tls + x509 + none 32990dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 33000dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 33010dd72e15SDaniel P. Berrange * 33020dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 33030dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 33040dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 33050dd72e15SDaniel P. Berrange * sub-auth types. 3306f9148c8aSDaniel P. Berrange * 3307f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3308f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3309f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3310f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3311f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3312f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3313f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3314f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3315f9148c8aSDaniel P. Berrange * 3316f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3317f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3318f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33190dd72e15SDaniel P. Berrange */ 3320eda24e18SDaniel P. Berrange if (websocket || !tlscreds) { 33210dd72e15SDaniel P. Berrange if (password) { 33220dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 3323eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VNC; 33240dd72e15SDaniel P. Berrange } else if (sasl) { 33250dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 3326eda24e18SDaniel P. Berrange *auth = VNC_AUTH_SASL; 33270dd72e15SDaniel P. Berrange } else { 33280dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 3329eda24e18SDaniel P. Berrange *auth = VNC_AUTH_NONE; 33300dd72e15SDaniel P. Berrange } 3331eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_INVALID; 3332f9148c8aSDaniel P. Berrange } else { 3333eda24e18SDaniel P. Berrange bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), 3334eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; 3335eda24e18SDaniel P. Berrange bool is_anon = object_dynamic_cast(OBJECT(tlscreds), 3336eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; 3337eda24e18SDaniel P. Berrange 3338eda24e18SDaniel P. Berrange if (!is_x509 && !is_anon) { 3339eda24e18SDaniel P. Berrange error_setg(errp, 3340eda24e18SDaniel P. Berrange "Unsupported TLS cred type %s", 3341eda24e18SDaniel P. Berrange object_get_typename(OBJECT(tlscreds))); 3342eda24e18SDaniel P. Berrange return -1; 3343eda24e18SDaniel P. Berrange } 3344eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VENCRYPT; 3345eda24e18SDaniel P. Berrange if (password) { 3346eda24e18SDaniel P. Berrange if (is_x509) { 3347eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 3348eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509VNC; 3349eda24e18SDaniel P. Berrange } else { 3350eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 3351eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSVNC; 3352eda24e18SDaniel P. Berrange } 3353eda24e18SDaniel P. Berrange 3354eda24e18SDaniel P. Berrange } else if (sasl) { 3355eda24e18SDaniel P. Berrange if (is_x509) { 3356eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 3357eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509SASL; 3358eda24e18SDaniel P. Berrange } else { 3359eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 3360eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSSASL; 3361eda24e18SDaniel P. Berrange } 3362eda24e18SDaniel P. Berrange } else { 3363eda24e18SDaniel P. Berrange if (is_x509) { 3364eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 3365eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509NONE; 3366eda24e18SDaniel P. Berrange } else { 3367eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 3368eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSNONE; 3369eda24e18SDaniel P. Berrange } 3370f9148c8aSDaniel P. Berrange } 33710dd72e15SDaniel P. Berrange } 33723e305e4aSDaniel P. Berrange return 0; 33730dd72e15SDaniel P. Berrange } 33740dd72e15SDaniel P. Berrange 33753e305e4aSDaniel P. Berrange 33763e305e4aSDaniel P. Berrange /* 33773e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 33783e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 33793e305e4aSDaniel P. Berrange */ 33803e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 33813e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 33823e305e4aSDaniel P. Berrange bool x509verify, 33833e305e4aSDaniel P. Berrange const char *dir, 33843e305e4aSDaniel P. Berrange const char *id, 33853e305e4aSDaniel P. Berrange Error **errp) 33863e305e4aSDaniel P. Berrange { 33873e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 33883e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 33893e305e4aSDaniel P. Berrange Object *creds; 33903e305e4aSDaniel P. Berrange Error *err = NULL; 33913e305e4aSDaniel P. Berrange 33923e305e4aSDaniel P. Berrange if (x509) { 33933e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 33943e305e4aSDaniel P. Berrange parent, 33953e305e4aSDaniel P. Berrange credsid, 33963e305e4aSDaniel P. Berrange &err, 33973e305e4aSDaniel P. Berrange "endpoint", "server", 33983e305e4aSDaniel P. Berrange "dir", dir, 33993e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 34003e305e4aSDaniel P. Berrange NULL); 34013e305e4aSDaniel P. Berrange } else { 34023e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 34033e305e4aSDaniel P. Berrange parent, 34043e305e4aSDaniel P. Berrange credsid, 34053e305e4aSDaniel P. Berrange &err, 34063e305e4aSDaniel P. Berrange "endpoint", "server", 34073e305e4aSDaniel P. Berrange NULL); 34083e305e4aSDaniel P. Berrange } 34093e305e4aSDaniel P. Berrange 34103e305e4aSDaniel P. Berrange g_free(credsid); 34113e305e4aSDaniel P. Berrange 34123e305e4aSDaniel P. Berrange if (err) { 34133e305e4aSDaniel P. Berrange error_propagate(errp, err); 34143e305e4aSDaniel P. Berrange return NULL; 34153e305e4aSDaniel P. Berrange } 34163e305e4aSDaniel P. Berrange 34173e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34183e305e4aSDaniel P. Berrange } 34193e305e4aSDaniel P. Berrange 34203e305e4aSDaniel P. Berrange 3421275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr, 3422275e0d61SDaniel P. Berrange bool websocket, 3423e5766eb4SGerd Hoffmann bool reverse, 3424275e0d61SDaniel P. Berrange int displaynum, 3425275e0d61SDaniel P. Berrange int to, 3426275e0d61SDaniel P. Berrange bool has_ipv4, 3427275e0d61SDaniel P. Berrange bool has_ipv6, 3428275e0d61SDaniel P. Berrange bool ipv4, 3429275e0d61SDaniel P. Berrange bool ipv6, 3430bd269ebcSMarkus Armbruster SocketAddress **retaddr, 3431275e0d61SDaniel P. Berrange Error **errp) 3432275e0d61SDaniel P. Berrange { 3433275e0d61SDaniel P. Berrange int ret = -1; 3434bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3435275e0d61SDaniel P. Berrange 3436bd269ebcSMarkus Armbruster addr = g_new0(SocketAddress, 1); 3437275e0d61SDaniel P. Berrange 3438275e0d61SDaniel P. Berrange if (strncmp(addrstr, "unix:", 5) == 0) { 3439bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_UNIX; 3440bd269ebcSMarkus Armbruster addr->u.q_unix.path = g_strdup(addrstr + 5); 3441275e0d61SDaniel P. Berrange 3442275e0d61SDaniel P. Berrange if (websocket) { 3443275e0d61SDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3444275e0d61SDaniel P. Berrange goto cleanup; 3445275e0d61SDaniel P. Berrange } 3446275e0d61SDaniel P. Berrange 3447275e0d61SDaniel P. Berrange if (to) { 3448275e0d61SDaniel P. Berrange error_setg(errp, "Port range not support with UNIX socket"); 3449275e0d61SDaniel P. Berrange goto cleanup; 3450275e0d61SDaniel P. Berrange } 3451275e0d61SDaniel P. Berrange ret = 0; 3452275e0d61SDaniel P. Berrange } else { 3453275e0d61SDaniel P. Berrange const char *port; 3454275e0d61SDaniel P. Berrange size_t hostlen; 3455275e0d61SDaniel P. Berrange unsigned long long baseport = 0; 3456275e0d61SDaniel P. Berrange InetSocketAddress *inet; 3457275e0d61SDaniel P. Berrange 3458275e0d61SDaniel P. Berrange port = strrchr(addrstr, ':'); 3459275e0d61SDaniel P. Berrange if (!port) { 3460275e0d61SDaniel P. Berrange if (websocket) { 3461275e0d61SDaniel P. Berrange hostlen = 0; 3462275e0d61SDaniel P. Berrange port = addrstr; 3463275e0d61SDaniel P. Berrange } else { 3464275e0d61SDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3465275e0d61SDaniel P. Berrange goto cleanup; 3466275e0d61SDaniel P. Berrange } 3467275e0d61SDaniel P. Berrange } else { 3468275e0d61SDaniel P. Berrange hostlen = port - addrstr; 3469275e0d61SDaniel P. Berrange port++; 3470275e0d61SDaniel P. Berrange if (*port == '\0') { 3471275e0d61SDaniel P. Berrange error_setg(errp, "vnc port cannot be empty"); 3472275e0d61SDaniel P. Berrange goto cleanup; 3473275e0d61SDaniel P. Berrange } 3474275e0d61SDaniel P. Berrange } 3475275e0d61SDaniel P. Berrange 3476bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_INET; 3477bd269ebcSMarkus Armbruster inet = &addr->u.inet; 3478275e0d61SDaniel P. Berrange if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { 3479275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr + 1, hostlen - 2); 3480275e0d61SDaniel P. Berrange } else { 3481275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr, hostlen); 3482275e0d61SDaniel P. Berrange } 3483275e0d61SDaniel P. Berrange /* plain VNC port is just an offset, for websocket 3484275e0d61SDaniel P. Berrange * port is absolute */ 3485275e0d61SDaniel P. Berrange if (websocket) { 3486275e0d61SDaniel P. Berrange if (g_str_equal(addrstr, "") || 3487275e0d61SDaniel P. Berrange g_str_equal(addrstr, "on")) { 3488396f935aSDaniel P. Berrange if (displaynum == -1) { 3489396f935aSDaniel P. Berrange error_setg(errp, "explicit websocket port is required"); 3490396f935aSDaniel P. Berrange goto cleanup; 3491396f935aSDaniel P. Berrange } 3492275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3493275e0d61SDaniel P. Berrange "%d", displaynum + 5700); 3494275e0d61SDaniel P. Berrange if (to) { 3495275e0d61SDaniel P. Berrange inet->has_to = true; 3496275e0d61SDaniel P. Berrange inet->to = to + 5700; 3497275e0d61SDaniel P. Berrange } 3498275e0d61SDaniel P. Berrange } else { 3499275e0d61SDaniel P. Berrange inet->port = g_strdup(port); 3500275e0d61SDaniel P. Berrange } 3501275e0d61SDaniel P. Berrange } else { 3502e5766eb4SGerd Hoffmann int offset = reverse ? 0 : 5900; 3503275e0d61SDaniel P. Berrange if (parse_uint_full(port, &baseport, 10) < 0) { 3504275e0d61SDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", port); 3505275e0d61SDaniel P. Berrange goto cleanup; 3506275e0d61SDaniel P. Berrange } 3507275e0d61SDaniel P. Berrange if (baseport > 65535 || 3508e5766eb4SGerd Hoffmann baseport + offset > 65535) { 3509275e0d61SDaniel P. Berrange error_setg(errp, "port %s out of range", port); 3510275e0d61SDaniel P. Berrange goto cleanup; 3511275e0d61SDaniel P. Berrange } 3512275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3513e5766eb4SGerd Hoffmann "%d", (int)baseport + offset); 3514275e0d61SDaniel P. Berrange 3515275e0d61SDaniel P. Berrange if (to) { 3516275e0d61SDaniel P. Berrange inet->has_to = true; 3517e5766eb4SGerd Hoffmann inet->to = to + offset; 3518275e0d61SDaniel P. Berrange } 3519275e0d61SDaniel P. Berrange } 3520275e0d61SDaniel P. Berrange 3521275e0d61SDaniel P. Berrange inet->ipv4 = ipv4; 3522275e0d61SDaniel P. Berrange inet->has_ipv4 = has_ipv4; 3523275e0d61SDaniel P. Berrange inet->ipv6 = ipv6; 3524275e0d61SDaniel P. Berrange inet->has_ipv6 = has_ipv6; 3525275e0d61SDaniel P. Berrange 3526275e0d61SDaniel P. Berrange ret = baseport; 3527275e0d61SDaniel P. Berrange } 3528275e0d61SDaniel P. Berrange 3529275e0d61SDaniel P. Berrange *retaddr = addr; 3530275e0d61SDaniel P. Berrange 3531275e0d61SDaniel P. Berrange cleanup: 3532275e0d61SDaniel P. Berrange if (ret < 0) { 3533bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 3534275e0d61SDaniel P. Berrange } 3535275e0d61SDaniel P. Berrange return ret; 3536275e0d61SDaniel P. Berrange } 3537275e0d61SDaniel P. Berrange 35389f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr, 35399f26f325SPhilippe Mathieu-Daudé size_t *retnsaddr) 35409f26f325SPhilippe Mathieu-Daudé { 35419f26f325SPhilippe Mathieu-Daudé size_t i; 35429f26f325SPhilippe Mathieu-Daudé 35439f26f325SPhilippe Mathieu-Daudé for (i = 0; i < *retnsaddr; i++) { 35449f26f325SPhilippe Mathieu-Daudé qapi_free_SocketAddress((*retsaddr)[i]); 35459f26f325SPhilippe Mathieu-Daudé } 35469f26f325SPhilippe Mathieu-Daudé g_free(*retsaddr); 35479f26f325SPhilippe Mathieu-Daudé 35489f26f325SPhilippe Mathieu-Daudé *retsaddr = NULL; 35499f26f325SPhilippe Mathieu-Daudé *retnsaddr = 0; 35509f26f325SPhilippe Mathieu-Daudé } 35519f26f325SPhilippe Mathieu-Daudé 3552275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts, 3553e5766eb4SGerd Hoffmann bool reverse, 3554bd269ebcSMarkus Armbruster SocketAddress ***retsaddr, 3555396f935aSDaniel P. Berrange size_t *retnsaddr, 3556bd269ebcSMarkus Armbruster SocketAddress ***retwsaddr, 3557396f935aSDaniel P. Berrange size_t *retnwsaddr, 3558275e0d61SDaniel P. Berrange Error **errp) 3559275e0d61SDaniel P. Berrange { 3560bd269ebcSMarkus Armbruster SocketAddress *saddr = NULL; 3561bd269ebcSMarkus Armbruster SocketAddress *wsaddr = NULL; 3562396f935aSDaniel P. Berrange QemuOptsIter addriter; 3563396f935aSDaniel P. Berrange const char *addr; 3564275e0d61SDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3565275e0d61SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 3566275e0d61SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 3567275e0d61SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3568275e0d61SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3569396f935aSDaniel P. Berrange int displaynum = -1; 3570396f935aSDaniel P. Berrange int ret = -1; 3571275e0d61SDaniel P. Berrange 3572275e0d61SDaniel P. Berrange *retsaddr = NULL; 3573396f935aSDaniel P. Berrange *retnsaddr = 0; 3574275e0d61SDaniel P. Berrange *retwsaddr = NULL; 3575396f935aSDaniel P. Berrange *retnwsaddr = 0; 3576275e0d61SDaniel P. Berrange 3577396f935aSDaniel P. Berrange addr = qemu_opt_get(opts, "vnc"); 3578396f935aSDaniel P. Berrange if (addr == NULL || g_str_equal(addr, "none")) { 3579396f935aSDaniel P. Berrange ret = 0; 3580396f935aSDaniel P. Berrange goto cleanup; 3581396f935aSDaniel P. Berrange } 3582396f935aSDaniel P. Berrange if (qemu_opt_get(opts, "websocket") && 3583275e0d61SDaniel P. Berrange !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3584275e0d61SDaniel P. Berrange error_setg(errp, 3585275e0d61SDaniel P. Berrange "SHA1 hash support is required for websockets"); 3586396f935aSDaniel P. Berrange goto cleanup; 3587275e0d61SDaniel P. Berrange } 3588275e0d61SDaniel P. Berrange 3589396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "vnc"); 3590396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3591396f935aSDaniel P. Berrange int rv; 3592e5766eb4SGerd Hoffmann rv = vnc_display_get_address(addr, false, reverse, 0, to, 3593275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3594275e0d61SDaniel P. Berrange ipv4, ipv6, 3595275e0d61SDaniel P. Berrange &saddr, errp); 3596396f935aSDaniel P. Berrange if (rv < 0) { 3597396f935aSDaniel P. Berrange goto cleanup; 3598275e0d61SDaniel P. Berrange } 3599396f935aSDaniel P. Berrange /* Historical compat - first listen address can be used 3600396f935aSDaniel P. Berrange * to set the default websocket port 3601396f935aSDaniel P. Berrange */ 3602396f935aSDaniel P. Berrange if (displaynum == -1) { 3603396f935aSDaniel P. Berrange displaynum = rv; 3604396f935aSDaniel P. Berrange } 3605bd269ebcSMarkus Armbruster *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1); 3606396f935aSDaniel P. Berrange (*retsaddr)[(*retnsaddr)++] = saddr; 3607396f935aSDaniel P. Berrange } 3608396f935aSDaniel P. Berrange 3609396f935aSDaniel P. Berrange /* If we had multiple primary displays, we don't do defaults 3610396f935aSDaniel P. Berrange * for websocket, and require explicit config instead. */ 3611396f935aSDaniel P. Berrange if (*retnsaddr > 1) { 3612396f935aSDaniel P. Berrange displaynum = -1; 3613396f935aSDaniel P. Berrange } 3614396f935aSDaniel P. Berrange 3615396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "websocket"); 3616396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3617e5766eb4SGerd Hoffmann if (vnc_display_get_address(addr, true, reverse, displaynum, to, 3618275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3619275e0d61SDaniel P. Berrange ipv4, ipv6, 3620275e0d61SDaniel P. Berrange &wsaddr, errp) < 0) { 3621396f935aSDaniel P. Berrange goto cleanup; 3622275e0d61SDaniel P. Berrange } 3623396f935aSDaniel P. Berrange 3624396f935aSDaniel P. Berrange /* Historical compat - if only a single listen address was 3625396f935aSDaniel P. Berrange * provided, then this is used to set the default listen 3626396f935aSDaniel P. Berrange * address for websocket too 3627396f935aSDaniel P. Berrange */ 3628396f935aSDaniel P. Berrange if (*retnsaddr == 1 && 3629bd269ebcSMarkus Armbruster (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET && 3630bd269ebcSMarkus Armbruster wsaddr->type == SOCKET_ADDRESS_TYPE_INET && 3631bd269ebcSMarkus Armbruster g_str_equal(wsaddr->u.inet.host, "") && 3632bd269ebcSMarkus Armbruster !g_str_equal((*retsaddr)[0]->u.inet.host, "")) { 3633bd269ebcSMarkus Armbruster g_free(wsaddr->u.inet.host); 3634bd269ebcSMarkus Armbruster wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host); 3635275e0d61SDaniel P. Berrange } 3636275e0d61SDaniel P. Berrange 3637bd269ebcSMarkus Armbruster *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1); 3638396f935aSDaniel P. Berrange (*retwsaddr)[(*retnwsaddr)++] = wsaddr; 3639396f935aSDaniel P. Berrange } 3640396f935aSDaniel P. Berrange 3641396f935aSDaniel P. Berrange ret = 0; 3642396f935aSDaniel P. Berrange cleanup: 3643396f935aSDaniel P. Berrange if (ret < 0) { 36449f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retsaddr, retnsaddr); 36459f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retwsaddr, retnwsaddr); 3646396f935aSDaniel P. Berrange } 3647396f935aSDaniel P. Berrange return ret; 3648275e0d61SDaniel P. Berrange } 3649275e0d61SDaniel P. Berrange 36508bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd, 3651bd269ebcSMarkus Armbruster SocketAddress **saddr, 3652396f935aSDaniel P. Berrange size_t nsaddr, 3653bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3654396f935aSDaniel P. Berrange size_t nwsaddr, 36558bd22f47SDaniel P. Berrange Error **errp) 36568bd22f47SDaniel P. Berrange { 36578bd22f47SDaniel P. Berrange /* connect to viewer */ 36588bd22f47SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3659396f935aSDaniel P. Berrange if (nwsaddr != 0) { 36608bd22f47SDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 36618bd22f47SDaniel P. Berrange return -1; 36628bd22f47SDaniel P. Berrange } 3663396f935aSDaniel P. Berrange if (nsaddr != 1) { 3664396f935aSDaniel P. Berrange error_setg(errp, "Expected a single address in reverse mode"); 3665396f935aSDaniel P. Berrange return -1; 3666396f935aSDaniel P. Berrange } 3667bd269ebcSMarkus Armbruster /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */ 3668bd269ebcSMarkus Armbruster vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX; 36698bd22f47SDaniel P. Berrange sioc = qio_channel_socket_new(); 36708bd22f47SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); 3671396f935aSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) { 36728bd22f47SDaniel P. Berrange return -1; 36738bd22f47SDaniel P. Berrange } 36748bd22f47SDaniel P. Berrange vnc_connect(vd, sioc, false, false); 36758bd22f47SDaniel P. Berrange object_unref(OBJECT(sioc)); 36768bd22f47SDaniel P. Berrange return 0; 36778bd22f47SDaniel P. Berrange } 36788bd22f47SDaniel P. Berrange 36798bd22f47SDaniel P. Berrange 36808bd22f47SDaniel P. Berrange static int vnc_display_listen_addr(VncDisplay *vd, 3681bd269ebcSMarkus Armbruster SocketAddress *addr, 36828bd22f47SDaniel P. Berrange const char *name, 36838bd22f47SDaniel P. Berrange QIOChannelSocket ***lsock, 36848bd22f47SDaniel P. Berrange guint **lsock_tag, 36858bd22f47SDaniel P. Berrange size_t *nlsock, 36868bd22f47SDaniel P. Berrange Error **errp) 36878bd22f47SDaniel P. Berrange { 368857a6d6d5SDaniel P. Berrange QIODNSResolver *resolver = qio_dns_resolver_get_instance(); 3689bd269ebcSMarkus Armbruster SocketAddress **rawaddrs = NULL; 369057a6d6d5SDaniel P. Berrange size_t nrawaddrs = 0; 369157a6d6d5SDaniel P. Berrange Error *listenerr = NULL; 3692396f935aSDaniel P. Berrange bool listening = false; 369357a6d6d5SDaniel P. Berrange size_t i; 36948bd22f47SDaniel P. Berrange 369557a6d6d5SDaniel P. Berrange if (qio_dns_resolver_lookup_sync(resolver, addr, &nrawaddrs, 369657a6d6d5SDaniel P. Berrange &rawaddrs, errp) < 0) { 36978bd22f47SDaniel P. Berrange return -1; 36988bd22f47SDaniel P. Berrange } 36998bd22f47SDaniel P. Berrange 370057a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 370157a6d6d5SDaniel P. Berrange QIOChannelSocket *sioc = qio_channel_socket_new(); 370257a6d6d5SDaniel P. Berrange 370357a6d6d5SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), name); 370457a6d6d5SDaniel P. Berrange if (qio_channel_socket_listen_sync( 370557a6d6d5SDaniel P. Berrange sioc, rawaddrs[i], listenerr == NULL ? &listenerr : NULL) < 0) { 37067bc4f084SMarc-André Lureau object_unref(OBJECT(sioc)); 370757a6d6d5SDaniel P. Berrange continue; 370857a6d6d5SDaniel P. Berrange } 3709396f935aSDaniel P. Berrange listening = true; 371057a6d6d5SDaniel P. Berrange (*nlsock)++; 371157a6d6d5SDaniel P. Berrange *lsock = g_renew(QIOChannelSocket *, *lsock, *nlsock); 371257a6d6d5SDaniel P. Berrange *lsock_tag = g_renew(guint, *lsock_tag, *nlsock); 371357a6d6d5SDaniel P. Berrange 371457a6d6d5SDaniel P. Berrange (*lsock)[*nlsock - 1] = sioc; 371557a6d6d5SDaniel P. Berrange (*lsock_tag)[*nlsock - 1] = 0; 371657a6d6d5SDaniel P. Berrange } 371757a6d6d5SDaniel P. Berrange 371857a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 3719bd269ebcSMarkus Armbruster qapi_free_SocketAddress(rawaddrs[i]); 372057a6d6d5SDaniel P. Berrange } 372157a6d6d5SDaniel P. Berrange g_free(rawaddrs); 372257a6d6d5SDaniel P. Berrange 372357a6d6d5SDaniel P. Berrange if (listenerr) { 3724396f935aSDaniel P. Berrange if (!listening) { 372557a6d6d5SDaniel P. Berrange error_propagate(errp, listenerr); 372657a6d6d5SDaniel P. Berrange return -1; 372757a6d6d5SDaniel P. Berrange } else { 372857a6d6d5SDaniel P. Berrange error_free(listenerr); 372957a6d6d5SDaniel P. Berrange } 373057a6d6d5SDaniel P. Berrange } 373157a6d6d5SDaniel P. Berrange 373257a6d6d5SDaniel P. Berrange for (i = 0; i < *nlsock; i++) { 373357a6d6d5SDaniel P. Berrange (*lsock_tag)[i] = qio_channel_add_watch( 373457a6d6d5SDaniel P. Berrange QIO_CHANNEL((*lsock)[i]), 37358bd22f47SDaniel P. Berrange G_IO_IN, vnc_listen_io, vd, NULL); 373657a6d6d5SDaniel P. Berrange } 37378bd22f47SDaniel P. Berrange 37388bd22f47SDaniel P. Berrange return 0; 37398bd22f47SDaniel P. Berrange } 37408bd22f47SDaniel P. Berrange 37418bd22f47SDaniel P. Berrange 37428bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd, 3743bd269ebcSMarkus Armbruster SocketAddress **saddr, 3744396f935aSDaniel P. Berrange size_t nsaddr, 3745bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3746396f935aSDaniel P. Berrange size_t nwsaddr, 37478bd22f47SDaniel P. Berrange Error **errp) 37488bd22f47SDaniel P. Berrange { 3749396f935aSDaniel P. Berrange size_t i; 37508bd22f47SDaniel P. Berrange 3751396f935aSDaniel P. Berrange for (i = 0; i < nsaddr; i++) { 3752396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, saddr[i], 37538bd22f47SDaniel P. Berrange "vnc-listen", 37548bd22f47SDaniel P. Berrange &vd->lsock, 37558bd22f47SDaniel P. Berrange &vd->lsock_tag, 37568bd22f47SDaniel P. Berrange &vd->nlsock, 37578bd22f47SDaniel P. Berrange errp) < 0) { 37588bd22f47SDaniel P. Berrange return -1; 37598bd22f47SDaniel P. Berrange } 3760396f935aSDaniel P. Berrange } 3761396f935aSDaniel P. Berrange for (i = 0; i < nwsaddr; i++) { 3762396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, wsaddr[i], 37638bd22f47SDaniel P. Berrange "vnc-ws-listen", 37648bd22f47SDaniel P. Berrange &vd->lwebsock, 37658bd22f47SDaniel P. Berrange &vd->lwebsock_tag, 37668bd22f47SDaniel P. Berrange &vd->nlwebsock, 37678bd22f47SDaniel P. Berrange errp) < 0) { 37688bd22f47SDaniel P. Berrange return -1; 37698bd22f47SDaniel P. Berrange } 3770396f935aSDaniel P. Berrange } 37718bd22f47SDaniel P. Berrange 37728bd22f47SDaniel P. Berrange return 0; 37738bd22f47SDaniel P. Berrange } 37748bd22f47SDaniel P. Berrange 37758bd22f47SDaniel P. Berrange 37764db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 37773e230dd2SCorentin Chary { 3778bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 37794db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3780bd269ebcSMarkus Armbruster SocketAddress **saddr = NULL, **wsaddr = NULL; 3781396f935aSDaniel P. Berrange size_t nsaddr, nwsaddr; 3782e2a11d9dSGonglei const char *share, *device_id; 37831d0d59feSGerd Hoffmann QemuConsole *con; 3784a2c72de0SGonglei bool password = false; 3785a2c72de0SGonglei bool reverse = false; 37863e305e4aSDaniel P. Berrange const char *credid; 3787a2c72de0SGonglei bool sasl = false; 3788d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 37893e230dd2SCorentin Chary int saslErr; 37903e230dd2SCorentin Chary #endif 37913e230dd2SCorentin Chary int acl = 0; 37923e230dd2SCorentin Chary int lock_key_sync = 1; 3793c5ce8333SGerd Hoffmann int key_delay_ms; 37943e230dd2SCorentin Chary 3795bf01c179SDaniel P. Berrange if (!vd) { 37962d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 37972d55f0e8SPaolo Bonzini return; 37982d55f0e8SPaolo Bonzini } 3799bf01c179SDaniel P. Berrange vnc_display_close(vd); 38004db14629SGerd Hoffmann 38014db14629SGerd Hoffmann if (!opts) { 38022d55f0e8SPaolo Bonzini return; 38034db14629SGerd Hoffmann } 3804275e0d61SDaniel P. Berrange 3805e5766eb4SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 3806e5766eb4SGerd Hoffmann if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr, 3807396f935aSDaniel P. Berrange &wsaddr, &nwsaddr, errp) < 0) { 3808275e0d61SDaniel P. Berrange goto fail; 3809275e0d61SDaniel P. Berrange } 3810275e0d61SDaniel P. Berrange 38114db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3812800567a6SDaniel P. Berrange if (password) { 3813800567a6SDaniel P. Berrange if (fips_get_state()) { 38142d55f0e8SPaolo Bonzini error_setg(errp, 38150f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 38160f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 38172d55f0e8SPaolo Bonzini "methods as an alternative"); 38181ce52c78SPaolo Bonzini goto fail; 38190f66998fSPaul Moore } 3820800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3821f844836dSGonglei QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) { 3822800567a6SDaniel P. Berrange error_setg(errp, 3823800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3824800567a6SDaniel P. Berrange goto fail; 3825800567a6SDaniel P. Berrange } 3826800567a6SDaniel P. Berrange } 38274db14629SGerd Hoffmann 38284db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3829d3b0db6dSAlexander Graf key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); 38304db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3831d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3832d169f04bSDaniel P. Berrange if (sasl) { 3833d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3834d169f04bSDaniel P. Berrange goto fail; 3835d169f04bSDaniel P. Berrange } 3836d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 38373e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 38383e305e4aSDaniel P. Berrange if (credid) { 38393e305e4aSDaniel P. Berrange Object *creds; 38403e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 38413e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 38423e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 38433e305e4aSDaniel P. Berrange error_setg(errp, 3844c62e90afSWolfgang Bumiller "'tls-creds' parameter is mutually exclusive with " 38453e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 38463e305e4aSDaniel P. Berrange goto fail; 38473e305e4aSDaniel P. Berrange } 38483e305e4aSDaniel P. Berrange 38493e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 38503e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 38513e305e4aSDaniel P. Berrange if (!creds) { 38523e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 38533e305e4aSDaniel P. Berrange credid); 38543e305e4aSDaniel P. Berrange goto fail; 38553e305e4aSDaniel P. Berrange } 3856bf01c179SDaniel P. Berrange vd->tlscreds = (QCryptoTLSCreds *) 38573e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 38583e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 3859bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38603e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 38613e305e4aSDaniel P. Berrange credid); 38623e305e4aSDaniel P. Berrange goto fail; 38633e305e4aSDaniel P. Berrange } 3864bf01c179SDaniel P. Berrange object_ref(OBJECT(vd->tlscreds)); 38653e305e4aSDaniel P. Berrange 3866bf01c179SDaniel P. Berrange if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 38673e305e4aSDaniel P. Berrange error_setg(errp, 38683e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 38693e305e4aSDaniel P. Berrange goto fail; 38703e305e4aSDaniel P. Berrange } 38713e305e4aSDaniel P. Berrange } else { 38723e305e4aSDaniel P. Berrange const char *path; 38733e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 38744db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 38753e305e4aSDaniel P. Berrange if (tls) { 38764db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 38773e305e4aSDaniel P. Berrange 38784db14629SGerd Hoffmann if (path) { 3879a2c72de0SGonglei x509 = true; 38803e305e4aSDaniel P. Berrange } else { 38813e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 38823e305e4aSDaniel P. Berrange if (path) { 38833e305e4aSDaniel P. Berrange x509 = true; 38843e305e4aSDaniel P. Berrange x509verify = true; 38853e305e4aSDaniel P. Berrange } 38863e305e4aSDaniel P. Berrange } 3887bf01c179SDaniel P. Berrange vd->tlscreds = vnc_display_create_creds(x509, 38883e305e4aSDaniel P. Berrange x509verify, 38893e305e4aSDaniel P. Berrange path, 3890bf01c179SDaniel P. Berrange vd->id, 38913e305e4aSDaniel P. Berrange errp); 3892bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38931ce52c78SPaolo Bonzini goto fail; 38943e230dd2SCorentin Chary } 38953e230dd2SCorentin Chary } 3896d169f04bSDaniel P. Berrange } 38974db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 38984db14629SGerd Hoffmann 38994db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 39004db14629SGerd Hoffmann if (share) { 39014db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 3902bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_IGNORE; 39034db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 3904bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39054db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 3906bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 39078cf36489SGerd Hoffmann } else { 39082d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 39091ce52c78SPaolo Bonzini goto fail; 39108cf36489SGerd Hoffmann } 39114db14629SGerd Hoffmann } else { 3912bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39133e230dd2SCorentin Chary } 3914bf01c179SDaniel P. Berrange vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); 39153e230dd2SCorentin Chary 39164db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 3917bf01c179SDaniel P. Berrange vd->lossy = qemu_opt_get_bool(opts, "lossy", false); 39184db14629SGerd Hoffmann #endif 3919bf01c179SDaniel P. Berrange vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3920e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3921e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3922e22492d3SPeter Lieven * calculations otherwise */ 3923bf01c179SDaniel P. Berrange if (!vd->lossy) { 3924bf01c179SDaniel P. Berrange vd->non_adaptive = true; 3925e22492d3SPeter Lieven } 3926e22492d3SPeter Lieven 39273e305e4aSDaniel P. Berrange if (acl) { 3928bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3929bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup("vnc.x509dname"); 3930c8496408SGerd Hoffmann } else { 3931bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vd->id); 3932c8496408SGerd Hoffmann } 3933bf01c179SDaniel P. Berrange qemu_acl_init(vd->tlsaclname); 39343e230dd2SCorentin Chary } 39353e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39363e230dd2SCorentin Chary if (acl && sasl) { 3937c8496408SGerd Hoffmann char *aclname; 3938c8496408SGerd Hoffmann 3939bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3940c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3941c8496408SGerd Hoffmann } else { 3942bf01c179SDaniel P. Berrange aclname = g_strdup_printf("vnc.%s.username", vd->id); 3943c8496408SGerd Hoffmann } 3944bf01c179SDaniel P. Berrange vd->sasl.acl = qemu_acl_init(aclname); 3945c8496408SGerd Hoffmann g_free(aclname); 39463e230dd2SCorentin Chary } 39473e230dd2SCorentin Chary #endif 39483e230dd2SCorentin Chary 3949eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->auth, &vd->subauth, 3950eda24e18SDaniel P. Berrange vd->tlscreds, password, 3951eda24e18SDaniel P. Berrange sasl, false, errp) < 0) { 3952eda24e18SDaniel P. Berrange goto fail; 3953eda24e18SDaniel P. Berrange } 39547364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth); 3955eda24e18SDaniel P. Berrange 3956eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, 3957eda24e18SDaniel P. Berrange vd->tlscreds, password, 3958eda24e18SDaniel P. Berrange sasl, true, errp) < 0) { 39593e305e4aSDaniel P. Berrange goto fail; 39603e305e4aSDaniel P. Berrange } 39617364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth); 39623e230dd2SCorentin Chary 39633e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39643e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 39652d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 39663e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 39671ce52c78SPaolo Bonzini goto fail; 39683e230dd2SCorentin Chary } 39693e230dd2SCorentin Chary #endif 3970bf01c179SDaniel P. Berrange vd->lock_key_sync = lock_key_sync; 3971a54f0d2bSPierre Ossman if (lock_key_sync) { 3972a54f0d2bSPierre Ossman vd->led = qemu_add_led_event_handler(kbd_leds, vd); 3973a54f0d2bSPierre Ossman } 3974a54f0d2bSPierre Ossman vd->ledstate = 0; 3975bf01c179SDaniel P. Berrange vd->key_delay_ms = key_delay_ms; 39763e230dd2SCorentin Chary 39771d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 39781d0d59feSGerd Hoffmann if (device_id) { 39791d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3980f2c1d54cSGerd Hoffmann Error *err = NULL; 39811d0d59feSGerd Hoffmann 3982f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3983f2c1d54cSGerd Hoffmann if (err) { 3984f2c1d54cSGerd Hoffmann error_propagate(errp, err); 39851d0d59feSGerd Hoffmann goto fail; 39861d0d59feSGerd Hoffmann } 39871d0d59feSGerd Hoffmann } else { 39881d0d59feSGerd Hoffmann con = NULL; 39891d0d59feSGerd Hoffmann } 39901d0d59feSGerd Hoffmann 3991bf01c179SDaniel P. Berrange if (con != vd->dcl.con) { 3992bf01c179SDaniel P. Berrange unregister_displaychangelistener(&vd->dcl); 3993bf01c179SDaniel P. Berrange vd->dcl.con = con; 3994bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 39951d0d59feSGerd Hoffmann } 39961d0d59feSGerd Hoffmann 3997fa03cb7fSMarc-André Lureau if (saddr == NULL) { 3998fa03cb7fSMarc-André Lureau goto cleanup; 3999fa03cb7fSMarc-André Lureau } 4000fa03cb7fSMarc-André Lureau 40013e230dd2SCorentin Chary if (reverse) { 4002396f935aSDaniel P. Berrange if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 4003e0d03b8cSDaniel P. Berrange goto fail; 40043e230dd2SCorentin Chary } 40053e230dd2SCorentin Chary } else { 4006396f935aSDaniel P. Berrange if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 40073d00ac1aSCole Robinson goto fail; 40083d00ac1aSCole Robinson } 40093e230dd2SCorentin Chary } 4010e0d03b8cSDaniel P. Berrange 4011275e0d61SDaniel P. Berrange if (qemu_opt_get(opts, "to")) { 4012bf01c179SDaniel P. Berrange vnc_display_print_local_addr(vd); 401333df7bf3SPaolo Bonzini } 401433df7bf3SPaolo Bonzini 4015396f935aSDaniel P. Berrange cleanup: 40169f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&saddr, &nsaddr); 40179f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&wsaddr, &nwsaddr); 40182d55f0e8SPaolo Bonzini return; 40191ce52c78SPaolo Bonzini 40201ce52c78SPaolo Bonzini fail: 40214ee74fa7SDaniel P. Berrange vnc_display_close(vd); 4022396f935aSDaniel P. Berrange goto cleanup; 40233e230dd2SCorentin Chary } 402413661089SDaniel P. Berrange 402514f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 402613661089SDaniel P. Berrange { 4027bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 402804d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 402913661089SDaniel P. Berrange 4030bf01c179SDaniel P. Berrange if (!vd) { 4031d616ccc5SGerd Hoffmann return; 4032d616ccc5SGerd Hoffmann } 403304d2529dSDaniel P. Berrange 403404d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 403504d2529dSDaniel P. Berrange if (sioc) { 403610bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); 4037bf01c179SDaniel P. Berrange vnc_connect(vd, sioc, skipauth, false); 403804d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 403904d2529dSDaniel P. Berrange } 404013661089SDaniel P. Berrange } 40414db14629SGerd Hoffmann 40429634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 40432779672fSGonglei { 40442779672fSGonglei int i = 2; 40452779672fSGonglei char *id; 40462779672fSGonglei 40472779672fSGonglei id = g_strdup("default"); 40482779672fSGonglei while (qemu_opts_find(olist, id)) { 40492779672fSGonglei g_free(id); 40502779672fSGonglei id = g_strdup_printf("vnc%d", i++); 40512779672fSGonglei } 40522779672fSGonglei qemu_opts_set_id(opts, id); 40532779672fSGonglei } 40542779672fSGonglei 405570b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 40564db14629SGerd Hoffmann { 40574db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 405870b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 405981607cbfSGonglei const char *id; 40604db14629SGerd Hoffmann 406181607cbfSGonglei if (!opts) { 406281607cbfSGonglei return NULL; 406381607cbfSGonglei } 406481607cbfSGonglei 406581607cbfSGonglei id = qemu_opts_id(opts); 40664db14629SGerd Hoffmann if (!id) { 40674db14629SGerd Hoffmann /* auto-assign id if not present */ 40682779672fSGonglei vnc_auto_assign_id(olist, opts); 40699634f4e3SGerd Hoffmann } 40709634f4e3SGerd Hoffmann return opts; 40714db14629SGerd Hoffmann } 40724db14629SGerd Hoffmann 407328d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 40749634f4e3SGerd Hoffmann { 40759634f4e3SGerd Hoffmann Error *local_err = NULL; 40769634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 40779634f4e3SGerd Hoffmann 40789634f4e3SGerd Hoffmann assert(id); 40794db14629SGerd Hoffmann vnc_display_init(id); 40804db14629SGerd Hoffmann vnc_display_open(id, &local_err); 40814db14629SGerd Hoffmann if (local_err != NULL) { 4082c29b77f9SMarkus Armbruster error_reportf_err(local_err, "Failed to start VNC server: "); 40834db14629SGerd Hoffmann exit(1); 40844db14629SGerd Hoffmann } 40854db14629SGerd Hoffmann return 0; 40864db14629SGerd Hoffmann } 40874db14629SGerd Hoffmann 40884db14629SGerd Hoffmann static void vnc_register_config(void) 40894db14629SGerd Hoffmann { 40904db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 40914db14629SGerd Hoffmann } 409234294e2fSEduardo Habkost opts_init(vnc_register_config); 4093