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 59938ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync); 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 96438ee14f4SGerd Hoffmann static int vnc_update_client(VncState *vs, int has_dirty, bool sync) 9653e230dd2SCorentin Chary { 9665a8be0f7SGerd Hoffmann if (vs->disconnecting) { 9675a8be0f7SGerd Hoffmann vnc_disconnect_finish(vs); 9685a8be0f7SGerd Hoffmann return 0; 9695a8be0f7SGerd Hoffmann } 9705a8be0f7SGerd Hoffmann 97163658280SGerd Hoffmann vs->has_dirty += has_dirty; 9725a693efdSGonglei if (vs->need_update && !vs->disconnecting) { 9733e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 974bd023f95SCorentin Chary VncJob *job; 9753e230dd2SCorentin Chary int y; 9762f487a3dSPeter Lieven int height, width; 977bd023f95SCorentin Chary int n = 0; 978bd023f95SCorentin Chary 9793e230dd2SCorentin Chary if (vs->output.offset && !vs->audio_cap && !vs->force_update) 9803e230dd2SCorentin Chary /* kernel send buffers are full -> drop frames to throttle */ 9813e230dd2SCorentin Chary return 0; 9823e230dd2SCorentin Chary 98363658280SGerd Hoffmann if (!vs->has_dirty && !vs->audio_cap && !vs->force_update) 9843e230dd2SCorentin Chary return 0; 9853e230dd2SCorentin Chary 9863e230dd2SCorentin Chary /* 9873e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 9883e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 9893e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 9903e230dd2SCorentin Chary * send them to the client. 9913e230dd2SCorentin Chary */ 992bd023f95SCorentin Chary job = vnc_job_new(vs); 9933e230dd2SCorentin Chary 994bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 995bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 9963e230dd2SCorentin Chary 99712b316d4SPeter Lieven y = 0; 99812b316d4SPeter Lieven for (;;) { 99912b316d4SPeter Lieven int x, h; 100012b316d4SPeter Lieven unsigned long x2; 100112b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 100212b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 100312b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 100412b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 100512b316d4SPeter Lieven /* no more dirty bits */ 100612b316d4SPeter Lieven break; 10073e230dd2SCorentin Chary } 100812b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 100912b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 101012b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 101112b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 101212b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 101312b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 10142f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 10152f487a3dSPeter Lieven if (x2 > x) { 101612b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 101712b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 10183e230dd2SCorentin Chary } 10190e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 10200e7d6f60SPeter Lieven y += h; 10210e7d6f60SPeter Lieven if (y == height) { 10220e7d6f60SPeter Lieven break; 10230e7d6f60SPeter Lieven } 10240e7d6f60SPeter Lieven } 10252f487a3dSPeter Lieven } 1026bd023f95SCorentin Chary 1027bd023f95SCorentin Chary vnc_job_push(job); 1028eb214ff8SGerd Hoffmann if (sync) { 1029eb214ff8SGerd Hoffmann vnc_jobs_join(vs); 1030eb214ff8SGerd Hoffmann } 10313e230dd2SCorentin Chary vs->force_update = 0; 103263658280SGerd Hoffmann vs->has_dirty = 0; 1033bd023f95SCorentin Chary return n; 10343e230dd2SCorentin Chary } 10353e230dd2SCorentin Chary 103604d2529dSDaniel P. Berrange if (vs->disconnecting) { 10373e230dd2SCorentin Chary vnc_disconnect_finish(vs); 103838ee14f4SGerd Hoffmann } else if (sync) { 103938ee14f4SGerd Hoffmann vnc_jobs_join(vs); 104038ee14f4SGerd Hoffmann } 10413e230dd2SCorentin Chary 10423e230dd2SCorentin Chary return 0; 10433e230dd2SCorentin Chary } 10443e230dd2SCorentin Chary 10453e230dd2SCorentin Chary /* audio */ 10463e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 10473e230dd2SCorentin Chary { 10483e230dd2SCorentin Chary VncState *vs = opaque; 10493e230dd2SCorentin Chary 10503e230dd2SCorentin Chary switch (cmd) { 10513e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1052bd023f95SCorentin Chary vnc_lock_output(vs); 10533e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10543e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10553e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1056bd023f95SCorentin Chary vnc_unlock_output(vs); 10573e230dd2SCorentin Chary vnc_flush(vs); 10583e230dd2SCorentin Chary break; 10593e230dd2SCorentin Chary 10603e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1061bd023f95SCorentin Chary vnc_lock_output(vs); 10623e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10633e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10643e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1065bd023f95SCorentin Chary vnc_unlock_output(vs); 10663e230dd2SCorentin Chary vnc_flush(vs); 10673e230dd2SCorentin Chary break; 10683e230dd2SCorentin Chary } 10693e230dd2SCorentin Chary } 10703e230dd2SCorentin Chary 10713e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 10723e230dd2SCorentin Chary { 10733e230dd2SCorentin Chary } 10743e230dd2SCorentin Chary 10753e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 10763e230dd2SCorentin Chary { 10773e230dd2SCorentin Chary VncState *vs = opaque; 10783e230dd2SCorentin Chary 1079bd023f95SCorentin Chary vnc_lock_output(vs); 10803e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 10813e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 10823e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 10833e230dd2SCorentin Chary vnc_write_u32(vs, size); 10843e230dd2SCorentin Chary vnc_write(vs, buf, size); 1085bd023f95SCorentin Chary vnc_unlock_output(vs); 10863e230dd2SCorentin Chary vnc_flush(vs); 10873e230dd2SCorentin Chary } 10883e230dd2SCorentin Chary 10893e230dd2SCorentin Chary static void audio_add(VncState *vs) 10903e230dd2SCorentin Chary { 10913e230dd2SCorentin Chary struct audio_capture_ops ops; 10923e230dd2SCorentin Chary 10933e230dd2SCorentin Chary if (vs->audio_cap) { 1094027a79c3SCole Robinson error_report("audio already running"); 10953e230dd2SCorentin Chary return; 10963e230dd2SCorentin Chary } 10973e230dd2SCorentin Chary 10983e230dd2SCorentin Chary ops.notify = audio_capture_notify; 10993e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 11003e230dd2SCorentin Chary ops.capture = audio_capture; 11013e230dd2SCorentin Chary 11023e230dd2SCorentin Chary vs->audio_cap = AUD_add_capture(&vs->as, &ops, vs); 11033e230dd2SCorentin Chary if (!vs->audio_cap) { 1104027a79c3SCole Robinson error_report("Failed to add audio capture"); 11053e230dd2SCorentin Chary } 11063e230dd2SCorentin Chary } 11073e230dd2SCorentin Chary 11083e230dd2SCorentin Chary static void audio_del(VncState *vs) 11093e230dd2SCorentin Chary { 11103e230dd2SCorentin Chary if (vs->audio_cap) { 11113e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 11123e230dd2SCorentin Chary vs->audio_cap = NULL; 11133e230dd2SCorentin Chary } 11143e230dd2SCorentin Chary } 11153e230dd2SCorentin Chary 11163e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 11173e230dd2SCorentin Chary { 111804d2529dSDaniel P. Berrange if (vs->disconnecting) { 11193e230dd2SCorentin Chary return; 112004d2529dSDaniel P. Berrange } 1121ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_start(vs, vs->ioc); 11228cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 112304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 112404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 1125a75d6f07SBrandon Carpenter vs->ioc_tag = 0; 112604d2529dSDaniel P. Berrange } 112704d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 112804d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 11293e230dd2SCorentin Chary } 11303e230dd2SCorentin Chary 11317536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 11323e230dd2SCorentin Chary { 11337d964c9dSCorentin Chary int i; 11347d964c9dSCorentin Chary 1135ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_finish(vs, vs->ioc); 1136ad6374c4SDaniel P. Berrange 1137bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1138bd023f95SCorentin Chary 1139bd023f95SCorentin Chary vnc_lock_output(vs); 1140fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 11413e230dd2SCorentin Chary 11423e230dd2SCorentin Chary buffer_free(&vs->input); 11433e230dd2SCorentin Chary buffer_free(&vs->output); 11443e230dd2SCorentin Chary 1145fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 11463e230dd2SCorentin Chary 11473e230dd2SCorentin Chary vnc_zlib_clear(vs); 11483e230dd2SCorentin Chary vnc_tight_clear(vs); 1149148954faSCorentin Chary vnc_zrle_clear(vs); 11503e230dd2SCorentin Chary 11513e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 11523e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 11533e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 11543e230dd2SCorentin Chary audio_del(vs); 11557bc9318bSGerd Hoffmann vnc_release_modifiers(vs); 11563e230dd2SCorentin Chary 115790cd03a3SDaniel P. Berrange if (vs->mouse_mode_notifier.notify != NULL) { 11586fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 115990cd03a3SDaniel P. Berrange } 116090cd03a3SDaniel P. Berrange QTAILQ_REMOVE(&vs->vd->clients, vs, next); 1161c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1162c7628bffSGerd Hoffmann /* last client gone */ 1163c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1164c7628bffSGerd Hoffmann } 11653e230dd2SCorentin Chary 1166bd023f95SCorentin Chary vnc_unlock_output(vs); 1167bd023f95SCorentin Chary 1168bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 11696fd8e79aSTim Hardeck if (vs->bh != NULL) { 1170175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 11716fd8e79aSTim Hardeck } 1172175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1173175b2a6eSCorentin Chary 11747d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 11757267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 11767d964c9dSCorentin Chary } 11777267c094SAnthony Liguori g_free(vs->lossy_rect); 117804d2529dSDaniel P. Berrange 117904d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 118004d2529dSDaniel P. Berrange vs->ioc = NULL; 118104d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 118204d2529dSDaniel P. Berrange vs->sioc = NULL; 11837267c094SAnthony Liguori g_free(vs); 11843e230dd2SCorentin Chary } 11853e230dd2SCorentin Chary 118604d2529dSDaniel P. Berrange ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) 11873e230dd2SCorentin Chary { 118804d2529dSDaniel P. Berrange if (ret <= 0) { 118904d2529dSDaniel P. Berrange if (ret == 0) { 1190ad6374c4SDaniel P. Berrange trace_vnc_client_eof(vs, vs->ioc); 1191537848eeSMichael Tokarev vnc_disconnect_start(vs); 119204d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 1193ad6374c4SDaniel P. Berrange trace_vnc_client_io_error(vs, vs->ioc, 1194ad6374c4SDaniel P. Berrange errp ? error_get_pretty(*errp) : 1195ad6374c4SDaniel P. Berrange "Unknown"); 1196537848eeSMichael Tokarev vnc_disconnect_start(vs); 11973e230dd2SCorentin Chary } 11983e230dd2SCorentin Chary 119904d2529dSDaniel P. Berrange if (errp) { 120004d2529dSDaniel P. Berrange error_free(*errp); 120104d2529dSDaniel P. Berrange *errp = NULL; 120204d2529dSDaniel P. Berrange } 12033e230dd2SCorentin Chary return 0; 12043e230dd2SCorentin Chary } 12053e230dd2SCorentin Chary return ret; 12063e230dd2SCorentin Chary } 12073e230dd2SCorentin Chary 12083e230dd2SCorentin Chary 12093e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 12103e230dd2SCorentin Chary { 12113e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 12123e230dd2SCorentin Chary vnc_disconnect_start(vs); 12133e230dd2SCorentin Chary } 12143e230dd2SCorentin Chary 12153e305e4aSDaniel P. Berrange 12163e230dd2SCorentin Chary /* 12173e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 12183e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 12193e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 12203e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 12213e230dd2SCorentin Chary * 12223e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 12233e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 12243e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 12253e230dd2SCorentin Chary * is active 12263e230dd2SCorentin Chary * 12273e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12283e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 12293e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12303e230dd2SCorentin Chary */ 1231fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 12323e230dd2SCorentin Chary { 123304d2529dSDaniel P. Berrange Error *err = NULL; 1234fdd1ab6aSDaniel P. Berrange ssize_t ret; 123504d2529dSDaniel P. Berrange ret = qio_channel_write( 123604d2529dSDaniel P. Berrange vs->ioc, (const char *)data, datalen, &err); 12373e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 123804d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 12393e230dd2SCorentin Chary } 12403e230dd2SCorentin Chary 12413e230dd2SCorentin Chary 12423e230dd2SCorentin Chary /* 12433e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 12443e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 12453e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 12463e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 12473e230dd2SCorentin Chary * 12483e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 12493e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 12503e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 12513e230dd2SCorentin Chary */ 1252fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_write_plain(VncState *vs) 12533e230dd2SCorentin Chary { 1254fdd1ab6aSDaniel P. Berrange ssize_t ret; 12553e230dd2SCorentin Chary 12563e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12573e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 12583e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 12593e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 12603e230dd2SCorentin Chary 12613e230dd2SCorentin Chary if (vs->sasl.conn && 12623e230dd2SCorentin Chary vs->sasl.runSSF && 12633e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 12643e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 12653e230dd2SCorentin Chary if (ret) 12663e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 12673e230dd2SCorentin Chary } else 12683e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12693e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 12703e230dd2SCorentin Chary if (!ret) 12713e230dd2SCorentin Chary return 0; 12723e230dd2SCorentin Chary 127332ed2680STim Hardeck buffer_advance(&vs->output, ret); 12743e230dd2SCorentin Chary 12753e230dd2SCorentin Chary if (vs->output.offset == 0) { 127604d2529dSDaniel P. Berrange if (vs->ioc_tag) { 127704d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 127804d2529dSDaniel P. Berrange } 127904d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 128004d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 12813e230dd2SCorentin Chary } 12823e230dd2SCorentin Chary 12833e230dd2SCorentin Chary return ret; 12843e230dd2SCorentin Chary } 12853e230dd2SCorentin Chary 12863e230dd2SCorentin Chary 12873e230dd2SCorentin Chary /* 12883e230dd2SCorentin Chary * First function called whenever there is data to be written to 12893e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 12903e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 12913e230dd2SCorentin Chary */ 129204d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 12933e230dd2SCorentin Chary { 12943e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12953e230dd2SCorentin Chary if (vs->sasl.conn && 12963e230dd2SCorentin Chary vs->sasl.runSSF && 12973e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 12983e230dd2SCorentin Chary vnc_client_write_sasl(vs); 12993e230dd2SCorentin Chary } else 13003e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13017536ee4bSTim Hardeck { 13023e230dd2SCorentin Chary vnc_client_write_plain(vs); 13033e230dd2SCorentin Chary } 13047536ee4bSTim Hardeck } 13053e230dd2SCorentin Chary 130604d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1307bd023f95SCorentin Chary { 1308bd023f95SCorentin Chary 1309bd023f95SCorentin Chary vnc_lock_output(vs); 1310d5f04223SDaniel P. Berrange if (vs->output.offset) { 131104d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 131204d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 131304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 131404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 131504d2529dSDaniel P. Berrange } 131604d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 131704d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1318bd023f95SCorentin Chary } 1319bd023f95SCorentin Chary vnc_unlock_output(vs); 1320bd023f95SCorentin Chary } 1321bd023f95SCorentin Chary 13223e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 13233e230dd2SCorentin Chary { 13243e230dd2SCorentin Chary vs->read_handler = func; 13253e230dd2SCorentin Chary vs->read_handler_expect = expecting; 13263e230dd2SCorentin Chary } 13273e230dd2SCorentin Chary 13283e230dd2SCorentin Chary 13293e230dd2SCorentin Chary /* 13303e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 13313e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 13323e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 13333e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13343e230dd2SCorentin Chary * 13353e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13363e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13373e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13383e230dd2SCorentin Chary * is active 13393e230dd2SCorentin Chary * 13403e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 13413e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 13423e230dd2SCorentin Chary * -1 on error, and disconnects the client socket. 13433e230dd2SCorentin Chary */ 1344fdd1ab6aSDaniel P. Berrange ssize_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 13453e230dd2SCorentin Chary { 1346fdd1ab6aSDaniel P. Berrange ssize_t ret; 134704d2529dSDaniel P. Berrange Error *err = NULL; 134804d2529dSDaniel P. Berrange ret = qio_channel_read( 134904d2529dSDaniel P. Berrange vs->ioc, (char *)data, datalen, &err); 13503e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 135104d2529dSDaniel P. Berrange return vnc_client_io_error(vs, ret, &err); 13523e230dd2SCorentin Chary } 13533e230dd2SCorentin Chary 13543e230dd2SCorentin Chary 13553e230dd2SCorentin Chary /* 13563e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 13573e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 13583e230dd2SCorentin Chary * data as possible without blocking. 13593e230dd2SCorentin Chary * 13603e230dd2SCorentin Chary * Returns the number of bytes read. Returns -1 on error, and 13613e230dd2SCorentin Chary * disconnects the client socket. 13623e230dd2SCorentin Chary */ 1363fdd1ab6aSDaniel P. Berrange static ssize_t vnc_client_read_plain(VncState *vs) 13643e230dd2SCorentin Chary { 1365fdd1ab6aSDaniel P. Berrange ssize_t ret; 13663e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 13673e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 13683e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 13693e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 13703e230dd2SCorentin Chary if (!ret) 13713e230dd2SCorentin Chary return 0; 13723e230dd2SCorentin Chary vs->input.offset += ret; 13733e230dd2SCorentin Chary return ret; 13743e230dd2SCorentin Chary } 13753e230dd2SCorentin Chary 1376175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1377175b2a6eSCorentin Chary { 1378175b2a6eSCorentin Chary VncState *vs = opaque; 1379175b2a6eSCorentin Chary 1380175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1381175b2a6eSCorentin Chary } 13823e230dd2SCorentin Chary 13833e230dd2SCorentin Chary /* 13843e230dd2SCorentin Chary * First function called whenever there is more data to be read from 13853e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 13863e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 1387ea697449SDaniel P. Berrange * Returns 0 on success, -1 if client disconnected 13883e230dd2SCorentin Chary */ 1389ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs) 13903e230dd2SCorentin Chary { 1391fdd1ab6aSDaniel P. Berrange ssize_t ret; 13923e230dd2SCorentin Chary 13933e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13943e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 13953e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 13963e230dd2SCorentin Chary else 13973e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13983e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 13993e230dd2SCorentin Chary if (!ret) { 140004d2529dSDaniel P. Berrange if (vs->disconnecting) { 14013e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1402ea697449SDaniel P. Berrange return -1; 140304d2529dSDaniel P. Berrange } 1404ea697449SDaniel P. Berrange return 0; 14053e230dd2SCorentin Chary } 14063e230dd2SCorentin Chary 14073e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 14083e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 14093e230dd2SCorentin Chary int ret; 14103e230dd2SCorentin Chary 14113e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 141204d2529dSDaniel P. Berrange if (vs->disconnecting) { 14133e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1414ea697449SDaniel P. Berrange return -1; 14153e230dd2SCorentin Chary } 14163e230dd2SCorentin Chary 14173e230dd2SCorentin Chary if (!ret) { 141832ed2680STim Hardeck buffer_advance(&vs->input, len); 14193e230dd2SCorentin Chary } else { 14203e230dd2SCorentin Chary vs->read_handler_expect = ret; 14213e230dd2SCorentin Chary } 14223e230dd2SCorentin Chary } 1423ea697449SDaniel P. Berrange return 0; 14243e230dd2SCorentin Chary } 14253e230dd2SCorentin Chary 142604d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 142704d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 142804d2529dSDaniel P. Berrange { 142904d2529dSDaniel P. Berrange VncState *vs = opaque; 143004d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 1431ea697449SDaniel P. Berrange if (vnc_client_read(vs) < 0) { 1432ea697449SDaniel P. Berrange return TRUE; 1433ea697449SDaniel P. Berrange } 143404d2529dSDaniel P. Berrange } 143504d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 143604d2529dSDaniel P. Berrange vnc_client_write(vs); 143704d2529dSDaniel P. Berrange } 143804d2529dSDaniel P. Berrange return TRUE; 143904d2529dSDaniel P. Berrange } 144004d2529dSDaniel P. Berrange 144104d2529dSDaniel P. Berrange 14423e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 14433e230dd2SCorentin Chary { 14443e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 14453e230dd2SCorentin Chary 144604d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 144704d2529dSDaniel P. Berrange if (vs->ioc_tag) { 144804d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 144904d2529dSDaniel P. Berrange } 145004d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 145104d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 14523e230dd2SCorentin Chary } 14533e230dd2SCorentin Chary 14543e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 14553e230dd2SCorentin Chary } 14563e230dd2SCorentin Chary 14573e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 14583e230dd2SCorentin Chary { 14593e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 14603e230dd2SCorentin Chary } 14613e230dd2SCorentin Chary 14623e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 14633e230dd2SCorentin Chary { 14643e230dd2SCorentin Chary uint8_t buf[4]; 14653e230dd2SCorentin Chary 14663e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 14673e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 14683e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 14693e230dd2SCorentin Chary buf[3] = value & 0xFF; 14703e230dd2SCorentin Chary 14713e230dd2SCorentin Chary vnc_write(vs, buf, 4); 14723e230dd2SCorentin Chary } 14733e230dd2SCorentin Chary 14743e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 14753e230dd2SCorentin Chary { 14763e230dd2SCorentin Chary uint8_t buf[2]; 14773e230dd2SCorentin Chary 14783e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 14793e230dd2SCorentin Chary buf[1] = value & 0xFF; 14803e230dd2SCorentin Chary 14813e230dd2SCorentin Chary vnc_write(vs, buf, 2); 14823e230dd2SCorentin Chary } 14833e230dd2SCorentin Chary 14843e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 14853e230dd2SCorentin Chary { 14863e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 14873e230dd2SCorentin Chary } 14883e230dd2SCorentin Chary 14893e230dd2SCorentin Chary void vnc_flush(VncState *vs) 14903e230dd2SCorentin Chary { 1491bd023f95SCorentin Chary vnc_lock_output(vs); 1492d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1493bd023f95SCorentin Chary vnc_client_write_locked(vs); 1494bd023f95SCorentin Chary } 1495bd023f95SCorentin Chary vnc_unlock_output(vs); 14963e230dd2SCorentin Chary } 14973e230dd2SCorentin Chary 149871a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 14993e230dd2SCorentin Chary { 15003e230dd2SCorentin Chary return data[offset]; 15013e230dd2SCorentin Chary } 15023e230dd2SCorentin Chary 150371a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 15043e230dd2SCorentin Chary { 15053e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 15063e230dd2SCorentin Chary } 15073e230dd2SCorentin Chary 150871a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 15093e230dd2SCorentin Chary { 15103e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 15113e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15123e230dd2SCorentin Chary } 15133e230dd2SCorentin Chary 15143e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 15153e230dd2SCorentin Chary { 15163e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 15173e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 15183e230dd2SCorentin Chary } 15193e230dd2SCorentin Chary 15203e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 15213e230dd2SCorentin Chary { 15223e230dd2SCorentin Chary } 15233e230dd2SCorentin Chary 15249e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 15253e230dd2SCorentin Chary { 15263e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 152714768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 15283e230dd2SCorentin Chary 15293e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1530bd023f95SCorentin Chary vnc_lock_output(vs); 15313e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 15323e230dd2SCorentin Chary vnc_write_u8(vs, 0); 15333e230dd2SCorentin Chary vnc_write_u16(vs, 1); 15343e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1535bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1536bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 15373e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1538bd023f95SCorentin Chary vnc_unlock_output(vs); 15393e230dd2SCorentin Chary vnc_flush(vs); 15403e230dd2SCorentin Chary } 15413e230dd2SCorentin Chary vs->absolute = absolute; 15423e230dd2SCorentin Chary } 15433e230dd2SCorentin Chary 15443e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 15453e230dd2SCorentin Chary { 15467fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 154714768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 154814768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 154914768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1550f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1551f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 155214768ebaSGerd Hoffmann }; 155314768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1554bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1555bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 15563e230dd2SCorentin Chary 155714768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 155814768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 155914768ebaSGerd Hoffmann vs->last_bmask = button_mask; 156014768ebaSGerd Hoffmann } 15613e230dd2SCorentin Chary 15623e230dd2SCorentin Chary if (vs->absolute) { 15639cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); 15649cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); 15653e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 156614768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 156714768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 15683e230dd2SCorentin Chary } else { 156914768ebaSGerd Hoffmann if (vs->last_x != -1) { 157014768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 157114768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 157214768ebaSGerd Hoffmann } 15733e230dd2SCorentin Chary vs->last_x = x; 15743e230dd2SCorentin Chary vs->last_y = y; 15753e230dd2SCorentin Chary } 157614768ebaSGerd Hoffmann qemu_input_event_sync(); 15773e230dd2SCorentin Chary } 15783e230dd2SCorentin Chary 15793e230dd2SCorentin Chary static void reset_keys(VncState *vs) 15803e230dd2SCorentin Chary { 15813e230dd2SCorentin Chary int i; 15823e230dd2SCorentin Chary for(i = 0; i < 256; i++) { 15833e230dd2SCorentin Chary if (vs->modifiers_state[i]) { 15848d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, i, false); 1585c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15863e230dd2SCorentin Chary vs->modifiers_state[i] = 0; 15873e230dd2SCorentin Chary } 15883e230dd2SCorentin Chary } 15893e230dd2SCorentin Chary } 15903e230dd2SCorentin Chary 15913e230dd2SCorentin Chary static void press_key(VncState *vs, int keysym) 15923e230dd2SCorentin Chary { 15933e230dd2SCorentin Chary int keycode = keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE_KEYMASK; 15948d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); 1595c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15968d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1597c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 15983e230dd2SCorentin Chary } 15993e230dd2SCorentin Chary 1600ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1601ab99e5c1SLei Li { 1602ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1603ab99e5c1SLei Li return; 1604ab99e5c1SLei Li } 1605ab99e5c1SLei Li 1606ab99e5c1SLei Li vnc_lock_output(vs); 1607ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1608ab99e5c1SLei Li vnc_write_u8(vs, 0); 1609ab99e5c1SLei Li vnc_write_u16(vs, 1); 1610ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1611a54f0d2bSPierre Ossman vnc_write_u8(vs, vs->vd->ledstate); 1612ab99e5c1SLei Li vnc_unlock_output(vs); 1613ab99e5c1SLei Li vnc_flush(vs); 1614ab99e5c1SLei Li } 1615ab99e5c1SLei Li 16163e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 16173e230dd2SCorentin Chary { 1618a54f0d2bSPierre Ossman VncDisplay *vd = opaque; 1619a54f0d2bSPierre Ossman VncState *client; 16203e230dd2SCorentin Chary 162140066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 162240066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 162340066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 162440066175SGerd Hoffmann 1625a54f0d2bSPierre Ossman if (ledstate == vd->ledstate) { 1626a54f0d2bSPierre Ossman return; 162796f3d174SLei Li } 1628ab99e5c1SLei Li 1629a54f0d2bSPierre Ossman vd->ledstate = ledstate; 1630a54f0d2bSPierre Ossman 1631a54f0d2bSPierre Ossman QTAILQ_FOREACH(client, &vd->clients, next) { 1632a54f0d2bSPierre Ossman vnc_led_state_change(client); 1633ab99e5c1SLei Li } 16343e230dd2SCorentin Chary } 16353e230dd2SCorentin Chary 16363e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 16373e230dd2SCorentin Chary { 16383e230dd2SCorentin Chary /* QEMU console switch */ 16393e230dd2SCorentin Chary switch(keycode) { 16403e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 16413e230dd2SCorentin Chary case 0x36: /* Right Shift */ 16423e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 16433e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 16443e230dd2SCorentin Chary case 0x38: /* Left ALT */ 16453e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 16463e230dd2SCorentin Chary if (down) 16473e230dd2SCorentin Chary vs->modifiers_state[keycode] = 1; 16483e230dd2SCorentin Chary else 16493e230dd2SCorentin Chary vs->modifiers_state[keycode] = 0; 16503e230dd2SCorentin Chary break; 16513e230dd2SCorentin Chary case 0x02 ... 0x0a: /* '1' to '9' keys */ 16521d0d59feSGerd Hoffmann if (vs->vd->dcl.con == NULL && 16531d0d59feSGerd Hoffmann down && vs->modifiers_state[0x1d] && vs->modifiers_state[0x38]) { 16543e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 16553e230dd2SCorentin Chary reset_keys(vs); 16563e230dd2SCorentin Chary console_select(keycode - 0x02); 16573e230dd2SCorentin Chary return; 16583e230dd2SCorentin Chary } 16593e230dd2SCorentin Chary break; 16603e230dd2SCorentin Chary case 0x3a: /* CapsLock */ 16613e230dd2SCorentin Chary case 0x45: /* NumLock */ 16623e230dd2SCorentin Chary if (down) 16633e230dd2SCorentin Chary vs->modifiers_state[keycode] ^= 1; 16643e230dd2SCorentin Chary break; 16653e230dd2SCorentin Chary } 16663e230dd2SCorentin Chary 1667e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1668e7b2aaccSLei Li state extension. 1669e7b2aaccSLei Li */ 16709892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1671e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16723e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 16733e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 16743e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16753e230dd2SCorentin Chary toggles numlock away from the VNC window. 16763e230dd2SCorentin Chary */ 16773e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 16783e230dd2SCorentin Chary if (!vs->modifiers_state[0x45]) { 167940066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 16803e230dd2SCorentin Chary vs->modifiers_state[0x45] = 1; 16813e230dd2SCorentin Chary press_key(vs, 0xff7f); 16823e230dd2SCorentin Chary } 16833e230dd2SCorentin Chary } else { 16843e230dd2SCorentin Chary if (vs->modifiers_state[0x45]) { 168540066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 16863e230dd2SCorentin Chary vs->modifiers_state[0x45] = 0; 16873e230dd2SCorentin Chary press_key(vs, 0xff7f); 16883e230dd2SCorentin Chary } 16893e230dd2SCorentin Chary } 16903e230dd2SCorentin Chary } 16913e230dd2SCorentin Chary 16929892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1693e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 16943e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 16953e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 16963e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 16973e230dd2SCorentin Chary toggles capslock away from the VNC window. 16983e230dd2SCorentin Chary */ 16993e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 17003e230dd2SCorentin Chary int shift = !!(vs->modifiers_state[0x2a] | vs->modifiers_state[0x36]); 17013e230dd2SCorentin Chary int capslock = !!(vs->modifiers_state[0x3a]); 17023e230dd2SCorentin Chary if (capslock) { 17033e230dd2SCorentin Chary if (uppercase == shift) { 170440066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 17053e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 0; 17063e230dd2SCorentin Chary press_key(vs, 0xffe5); 17073e230dd2SCorentin Chary } 17083e230dd2SCorentin Chary } else { 17093e230dd2SCorentin Chary if (uppercase != shift) { 171040066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 17113e230dd2SCorentin Chary vs->modifiers_state[0x3a] = 1; 17123e230dd2SCorentin Chary press_key(vs, 0xffe5); 17133e230dd2SCorentin Chary } 17143e230dd2SCorentin Chary } 17153e230dd2SCorentin Chary } 17163e230dd2SCorentin Chary 171781c0d5a6SGerd Hoffmann if (qemu_console_is_graphic(NULL)) { 17188d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, down); 1719c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 17203e230dd2SCorentin Chary } else { 1721e26437c2SGerd Hoffmann bool numlock = vs->modifiers_state[0x45]; 1722e26437c2SGerd Hoffmann bool control = (vs->modifiers_state[0x1d] || 1723e26437c2SGerd Hoffmann vs->modifiers_state[0x9d]); 17243e230dd2SCorentin Chary /* QEMU console emulation */ 17253e230dd2SCorentin Chary if (down) { 17263e230dd2SCorentin Chary switch (keycode) { 17273e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 17283e230dd2SCorentin Chary case 0x36: /* Right Shift */ 17293e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 17303e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 17313e230dd2SCorentin Chary case 0x38: /* Left ALT */ 17323e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 17333e230dd2SCorentin Chary break; 17343e230dd2SCorentin Chary case 0xc8: 17353e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 17363e230dd2SCorentin Chary break; 17373e230dd2SCorentin Chary case 0xd0: 17383e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 17393e230dd2SCorentin Chary break; 17403e230dd2SCorentin Chary case 0xcb: 17413e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 17423e230dd2SCorentin Chary break; 17433e230dd2SCorentin Chary case 0xcd: 17443e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 17453e230dd2SCorentin Chary break; 17463e230dd2SCorentin Chary case 0xd3: 17473e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 17483e230dd2SCorentin Chary break; 17493e230dd2SCorentin Chary case 0xc7: 17503e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 17513e230dd2SCorentin Chary break; 17523e230dd2SCorentin Chary case 0xcf: 17533e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 17543e230dd2SCorentin Chary break; 17553e230dd2SCorentin Chary case 0xc9: 17563e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 17573e230dd2SCorentin Chary break; 17583e230dd2SCorentin Chary case 0xd1: 17593e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 17603e230dd2SCorentin Chary break; 17613e230dd2SCorentin Chary 17623e230dd2SCorentin Chary case 0x47: 17633e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 17643e230dd2SCorentin Chary break; 17653e230dd2SCorentin Chary case 0x48: 17663e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 17673e230dd2SCorentin Chary break; 17683e230dd2SCorentin Chary case 0x49: 17693e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 17703e230dd2SCorentin Chary break; 17713e230dd2SCorentin Chary case 0x4b: 17723e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 17733e230dd2SCorentin Chary break; 17743e230dd2SCorentin Chary case 0x4c: 17753e230dd2SCorentin Chary kbd_put_keysym('5'); 17763e230dd2SCorentin Chary break; 17773e230dd2SCorentin Chary case 0x4d: 17783e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 17793e230dd2SCorentin Chary break; 17803e230dd2SCorentin Chary case 0x4f: 17813e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 17823e230dd2SCorentin Chary break; 17833e230dd2SCorentin Chary case 0x50: 17843e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 17853e230dd2SCorentin Chary break; 17863e230dd2SCorentin Chary case 0x51: 17873e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 17883e230dd2SCorentin Chary break; 17893e230dd2SCorentin Chary case 0x52: 17903e230dd2SCorentin Chary kbd_put_keysym('0'); 17913e230dd2SCorentin Chary break; 17923e230dd2SCorentin Chary case 0x53: 17933e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 17943e230dd2SCorentin Chary break; 17953e230dd2SCorentin Chary 17963e230dd2SCorentin Chary case 0xb5: 17973e230dd2SCorentin Chary kbd_put_keysym('/'); 17983e230dd2SCorentin Chary break; 17993e230dd2SCorentin Chary case 0x37: 18003e230dd2SCorentin Chary kbd_put_keysym('*'); 18013e230dd2SCorentin Chary break; 18023e230dd2SCorentin Chary case 0x4a: 18033e230dd2SCorentin Chary kbd_put_keysym('-'); 18043e230dd2SCorentin Chary break; 18053e230dd2SCorentin Chary case 0x4e: 18063e230dd2SCorentin Chary kbd_put_keysym('+'); 18073e230dd2SCorentin Chary break; 18083e230dd2SCorentin Chary case 0x9c: 18093e230dd2SCorentin Chary kbd_put_keysym('\n'); 18103e230dd2SCorentin Chary break; 18113e230dd2SCorentin Chary 18123e230dd2SCorentin Chary default: 1813e26437c2SGerd Hoffmann if (control) { 1814e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1815e26437c2SGerd Hoffmann } else { 18163e230dd2SCorentin Chary kbd_put_keysym(sym); 1817e26437c2SGerd Hoffmann } 18183e230dd2SCorentin Chary break; 18193e230dd2SCorentin Chary } 18203e230dd2SCorentin Chary } 18213e230dd2SCorentin Chary } 18223e230dd2SCorentin Chary } 18233e230dd2SCorentin Chary 18247bc9318bSGerd Hoffmann static void vnc_release_modifiers(VncState *vs) 18257bc9318bSGerd Hoffmann { 18267bc9318bSGerd Hoffmann static const int keycodes[] = { 18277bc9318bSGerd Hoffmann /* shift, control, alt keys, both left & right */ 18287bc9318bSGerd Hoffmann 0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 18297bc9318bSGerd Hoffmann }; 18307bc9318bSGerd Hoffmann int i, keycode; 18317bc9318bSGerd Hoffmann 183281c0d5a6SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 18337bc9318bSGerd Hoffmann return; 18347bc9318bSGerd Hoffmann } 18357bc9318bSGerd Hoffmann for (i = 0; i < ARRAY_SIZE(keycodes); i++) { 18367bc9318bSGerd Hoffmann keycode = keycodes[i]; 18377bc9318bSGerd Hoffmann if (!vs->modifiers_state[keycode]) { 18387bc9318bSGerd Hoffmann continue; 18397bc9318bSGerd Hoffmann } 18408d447d10SGerd Hoffmann qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); 1841c5ce8333SGerd Hoffmann qemu_input_event_send_key_delay(vs->vd->key_delay_ms); 18427bc9318bSGerd Hoffmann } 18437bc9318bSGerd Hoffmann } 18447bc9318bSGerd Hoffmann 184540066175SGerd Hoffmann static const char *code2name(int keycode) 184640066175SGerd Hoffmann { 1847977c736fSMarkus Armbruster return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); 184840066175SGerd Hoffmann } 184940066175SGerd Hoffmann 18503e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 18513e230dd2SCorentin Chary { 18523e230dd2SCorentin Chary int keycode; 18533e230dd2SCorentin Chary int lsym = sym; 18543e230dd2SCorentin Chary 185581c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 18563e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 18573e230dd2SCorentin Chary } 18583e230dd2SCorentin Chary 18593e230dd2SCorentin Chary keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANCODE_KEYMASK; 186040066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 18613e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18623e230dd2SCorentin Chary } 18633e230dd2SCorentin Chary 18643e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 18653e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 18663e230dd2SCorentin Chary { 18673e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 186840066175SGerd Hoffmann if (keyboard_layout) { 18693e230dd2SCorentin Chary key_event(vs, down, sym); 187040066175SGerd Hoffmann } else { 187140066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 18723e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 18733e230dd2SCorentin Chary } 187440066175SGerd Hoffmann } 18753e230dd2SCorentin Chary 18763e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1877bea60dd7SPeter Lieven int x, int y, int w, int h) 18783e230dd2SCorentin Chary { 18793e230dd2SCorentin Chary vs->need_update = 1; 1880bea60dd7SPeter Lieven 1881bea60dd7SPeter Lieven if (incremental) { 1882bea60dd7SPeter Lieven return; 18833e230dd2SCorentin Chary } 1884bea60dd7SPeter Lieven 188507535a89SStephan Kulow vs->force_update = 1; 1886f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 18873e230dd2SCorentin Chary } 18883e230dd2SCorentin Chary 18893e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 18903e230dd2SCorentin Chary { 1891bd023f95SCorentin Chary vnc_lock_output(vs); 18923e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 18933e230dd2SCorentin Chary vnc_write_u8(vs, 0); 18943e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1895d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1896bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1897bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 18983e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 1899bd023f95SCorentin Chary vnc_unlock_output(vs); 19003e230dd2SCorentin Chary vnc_flush(vs); 19013e230dd2SCorentin Chary } 19023e230dd2SCorentin Chary 19033e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 19043e230dd2SCorentin Chary { 1905bd023f95SCorentin Chary vnc_lock_output(vs); 19063e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 19073e230dd2SCorentin Chary vnc_write_u8(vs, 0); 19083e230dd2SCorentin Chary vnc_write_u16(vs, 1); 1909d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 1910bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1911bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 19123e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 1913bd023f95SCorentin Chary vnc_unlock_output(vs); 19143e230dd2SCorentin Chary vnc_flush(vs); 19153e230dd2SCorentin Chary } 19163e230dd2SCorentin Chary 19173e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 19183e230dd2SCorentin Chary { 19193e230dd2SCorentin Chary int i; 19203e230dd2SCorentin Chary unsigned int enc = 0; 19213e230dd2SCorentin Chary 19223e230dd2SCorentin Chary vs->features = 0; 19233e230dd2SCorentin Chary vs->vnc_encoding = 0; 1924d1af0e05SCorentin Chary vs->tight.compression = 9; 1925d1af0e05SCorentin Chary vs->tight.quality = -1; /* Lossless by default */ 19263e230dd2SCorentin Chary vs->absolute = -1; 19273e230dd2SCorentin Chary 19283e230dd2SCorentin Chary /* 19293e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 1930e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 19313e230dd2SCorentin Chary * will be set at the end of the loop. 19323e230dd2SCorentin Chary */ 19333e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 19343e230dd2SCorentin Chary enc = encodings[i]; 19353e230dd2SCorentin Chary switch (enc) { 19363e230dd2SCorentin Chary case VNC_ENCODING_RAW: 19373e230dd2SCorentin Chary vs->vnc_encoding = enc; 19383e230dd2SCorentin Chary break; 19393e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 19403e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 19413e230dd2SCorentin Chary break; 19423e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 19433e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 19443e230dd2SCorentin Chary vs->vnc_encoding = enc; 19453e230dd2SCorentin Chary break; 19463e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 19473e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 19483e230dd2SCorentin Chary vs->vnc_encoding = enc; 19493e230dd2SCorentin Chary break; 1950fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 1951efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 1952efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 1953efe556adSCorentin Chary vs->vnc_encoding = enc; 1954efe556adSCorentin Chary break; 1955fe3e7f2dSJoel Martin #endif 19563e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 19573e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 19583e230dd2SCorentin Chary vs->vnc_encoding = enc; 19593e230dd2SCorentin Chary break; 1960148954faSCorentin Chary case VNC_ENCODING_ZRLE: 1961148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 1962148954faSCorentin Chary vs->vnc_encoding = enc; 1963148954faSCorentin Chary break; 1964148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 1965148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 1966148954faSCorentin Chary vs->vnc_encoding = enc; 1967148954faSCorentin Chary break; 19683e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 19693e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 19703e230dd2SCorentin Chary break; 19713e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 19723e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 19733e230dd2SCorentin Chary break; 19743e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 19753e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 197691ec41dcSFrediano Ziglio if (vs->vd->cursor) { 197791ec41dcSFrediano Ziglio vnc_cursor_define(vs); 197891ec41dcSFrediano Ziglio } 19793e230dd2SCorentin Chary break; 19803e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 19813e230dd2SCorentin Chary send_ext_key_event_ack(vs); 19823e230dd2SCorentin Chary break; 19833e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 19843e230dd2SCorentin Chary send_ext_audio_ack(vs); 19853e230dd2SCorentin Chary break; 19863e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 19873e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 19883e230dd2SCorentin Chary break; 1989ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 1990ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 1991ab99e5c1SLei Li break; 19923e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 1993d1af0e05SCorentin Chary vs->tight.compression = (enc & 0x0F); 19943e230dd2SCorentin Chary break; 19953e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 1996b31f519eSCorentin Chary if (vs->vd->lossy) { 1997d1af0e05SCorentin Chary vs->tight.quality = (enc & 0x0F); 1998b31f519eSCorentin Chary } 19993e230dd2SCorentin Chary break; 20003e230dd2SCorentin Chary default: 20013e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 20023e230dd2SCorentin Chary break; 20033e230dd2SCorentin Chary } 20043e230dd2SCorentin Chary } 20053e230dd2SCorentin Chary vnc_desktop_resize(vs); 20069e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2007ab99e5c1SLei Li vnc_led_state_change(vs); 20083e230dd2SCorentin Chary } 20093e230dd2SCorentin Chary 20103e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 20113e230dd2SCorentin Chary { 20129f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 20139f64916dSGerd Hoffmann 20149f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 20153e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 20163e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 20173e230dd2SCorentin Chary } else { 20183e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 20193e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 20203e230dd2SCorentin Chary } 20213e230dd2SCorentin Chary } 20223e230dd2SCorentin Chary 20230c426e45SAlexander Graf static void send_color_map(VncState *vs) 20240c426e45SAlexander Graf { 20250c426e45SAlexander Graf int i; 20260c426e45SAlexander Graf 20270c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 20280c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 20290c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 20300c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 20310c426e45SAlexander Graf 20320c426e45SAlexander Graf for (i = 0; i < 256; i++) { 20330c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 20340c426e45SAlexander Graf 20350c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 20360c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 20370c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 20380c426e45SAlexander Graf } 20390c426e45SAlexander Graf } 20400c426e45SAlexander Graf 2041ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 20423e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 20433e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 20443e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 20453e230dd2SCorentin Chary { 20463e230dd2SCorentin Chary if (!true_color_flag) { 20470c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 20480c426e45SAlexander Graf bits_per_pixel = 8; 20490c426e45SAlexander Graf red_max = 7; 20500c426e45SAlexander Graf green_max = 7; 20510c426e45SAlexander Graf blue_max = 3; 20520c426e45SAlexander Graf red_shift = 0; 20530c426e45SAlexander Graf green_shift = 3; 20540c426e45SAlexander Graf blue_shift = 6; 20553e230dd2SCorentin Chary } 20563e230dd2SCorentin Chary 2057e6908bfeSPetr Matousek switch (bits_per_pixel) { 2058e6908bfeSPetr Matousek case 8: 2059e6908bfeSPetr Matousek case 16: 2060e6908bfeSPetr Matousek case 32: 2061e6908bfeSPetr Matousek break; 2062e6908bfeSPetr Matousek default: 2063e6908bfeSPetr Matousek vnc_client_error(vs); 2064e6908bfeSPetr Matousek return; 2065e6908bfeSPetr Matousek } 2066e6908bfeSPetr Matousek 20674c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 20687c9209e7SCédric Le Goater vs->client_pf.rbits = ctpopl(red_max); 20699f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 20709f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 20714c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 20727c9209e7SCédric Le Goater vs->client_pf.gbits = ctpopl(green_max); 20739f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 20749f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 20754c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 20767c9209e7SCédric Le Goater vs->client_pf.bbits = ctpopl(blue_max); 20779f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 20789f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 20799f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 20809f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 20819f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 20829f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 20833e230dd2SCorentin Chary 20840c426e45SAlexander Graf if (!true_color_flag) { 20850c426e45SAlexander Graf send_color_map(vs); 20860c426e45SAlexander Graf } 20870c426e45SAlexander Graf 20883e230dd2SCorentin Chary set_pixel_conversion(vs); 20893e230dd2SCorentin Chary 20901d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 20911d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 20923e230dd2SCorentin Chary } 20933e230dd2SCorentin Chary 20943e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 20953e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 20963e230dd2SCorentin Chary 20979f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 20989f64916dSGerd Hoffmann 20999f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 21009f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 21013e230dd2SCorentin Chary 21023e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 21033e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 21043e230dd2SCorentin Chary #else 21053e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 21063e230dd2SCorentin Chary #endif 21073e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 21089f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 21099f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 21109f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 21119f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 21129f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 21139f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 21149f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 21153e230dd2SCorentin Chary 21163e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21173e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21183e230dd2SCorentin Chary } 21193e230dd2SCorentin Chary 21203e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 21213e230dd2SCorentin Chary { 21223e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 21233e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2124bd023f95SCorentin Chary vnc_lock_output(vs); 21253e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 21263e230dd2SCorentin Chary vnc_write_u8(vs, 0); 21273e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2128d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2129bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2130bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2131d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 21323e230dd2SCorentin Chary pixel_format_message(vs); 2133bd023f95SCorentin Chary vnc_unlock_output(vs); 21343e230dd2SCorentin Chary vnc_flush(vs); 21353e230dd2SCorentin Chary } else { 21363e230dd2SCorentin Chary set_pixel_conversion(vs); 21373e230dd2SCorentin Chary } 21383e230dd2SCorentin Chary } 21393e230dd2SCorentin Chary 21403e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 21413e230dd2SCorentin Chary { 21423e230dd2SCorentin Chary int i; 21433e230dd2SCorentin Chary uint16_t limit; 21443e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 21453e230dd2SCorentin Chary 21463e230dd2SCorentin Chary if (data[0] > 3) { 21470f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 21483e230dd2SCorentin Chary } 21493e230dd2SCorentin Chary 21503e230dd2SCorentin Chary switch (data[0]) { 21513e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 21523e230dd2SCorentin Chary if (len == 1) 21533e230dd2SCorentin Chary return 20; 21543e230dd2SCorentin Chary 2155ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 21563e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 21573e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 21583e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 21593e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 21603e230dd2SCorentin Chary break; 21613e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 21623e230dd2SCorentin Chary if (len == 1) 21633e230dd2SCorentin Chary return 4; 21643e230dd2SCorentin Chary 21653e230dd2SCorentin Chary if (len == 4) { 21663e230dd2SCorentin Chary limit = read_u16(data, 2); 21673e230dd2SCorentin Chary if (limit > 0) 21683e230dd2SCorentin Chary return 4 + (limit * 4); 21693e230dd2SCorentin Chary } else 21703e230dd2SCorentin Chary limit = read_u16(data, 2); 21713e230dd2SCorentin Chary 21723e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 21733e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 21743e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 21753e230dd2SCorentin Chary } 21763e230dd2SCorentin Chary 21773e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 21783e230dd2SCorentin Chary break; 21793e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 21803e230dd2SCorentin Chary if (len == 1) 21813e230dd2SCorentin Chary return 10; 21823e230dd2SCorentin Chary 21833e230dd2SCorentin Chary framebuffer_update_request(vs, 21843e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 21853e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 21863e230dd2SCorentin Chary break; 21873e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 21883e230dd2SCorentin Chary if (len == 1) 21893e230dd2SCorentin Chary return 8; 21903e230dd2SCorentin Chary 21913e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 21923e230dd2SCorentin Chary break; 21933e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 21943e230dd2SCorentin Chary if (len == 1) 21953e230dd2SCorentin Chary return 6; 21963e230dd2SCorentin Chary 21973e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 21983e230dd2SCorentin Chary break; 21993e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2200f9a70e79SPeter Lieven if (len == 1) { 22013e230dd2SCorentin Chary return 8; 2202f9a70e79SPeter Lieven } 22033e230dd2SCorentin Chary if (len == 8) { 22043e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2205f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2206f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2207f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2208f9a70e79SPeter Lieven vnc_client_error(vs); 2209f9a70e79SPeter Lieven break; 2210f9a70e79SPeter Lieven } 2211f9a70e79SPeter Lieven if (dlen > 0) { 22123e230dd2SCorentin Chary return 8 + dlen; 22133e230dd2SCorentin Chary } 2214f9a70e79SPeter Lieven } 22153e230dd2SCorentin Chary 22163e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 22173e230dd2SCorentin Chary break; 22183e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 22193e230dd2SCorentin Chary if (len == 1) 22203e230dd2SCorentin Chary return 2; 22213e230dd2SCorentin Chary 22223e230dd2SCorentin Chary switch (read_u8(data, 1)) { 22233e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 22243e230dd2SCorentin Chary if (len == 2) 22253e230dd2SCorentin Chary return 12; 22263e230dd2SCorentin Chary 22273e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 22283e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 22293e230dd2SCorentin Chary break; 22303e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 22313e230dd2SCorentin Chary if (len == 2) 22323e230dd2SCorentin Chary return 4; 22333e230dd2SCorentin Chary 22343e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 22353e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 22363e230dd2SCorentin Chary audio_add(vs); 22373e230dd2SCorentin Chary break; 22383e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 22393e230dd2SCorentin Chary audio_del(vs); 22403e230dd2SCorentin Chary break; 22413e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 22423e230dd2SCorentin Chary if (len == 4) 22433e230dd2SCorentin Chary return 10; 22443e230dd2SCorentin Chary switch (read_u8(data, 4)) { 22453e230dd2SCorentin Chary case 0: vs->as.fmt = AUD_FMT_U8; break; 22463e230dd2SCorentin Chary case 1: vs->as.fmt = AUD_FMT_S8; break; 22473e230dd2SCorentin Chary case 2: vs->as.fmt = AUD_FMT_U16; break; 22483e230dd2SCorentin Chary case 3: vs->as.fmt = AUD_FMT_S16; break; 22493e230dd2SCorentin Chary case 4: vs->as.fmt = AUD_FMT_U32; break; 22503e230dd2SCorentin Chary case 5: vs->as.fmt = AUD_FMT_S32; break; 22513e230dd2SCorentin Chary default: 2252153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 22533e230dd2SCorentin Chary vnc_client_error(vs); 22543e230dd2SCorentin Chary break; 22553e230dd2SCorentin Chary } 22563e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 22573e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2258*090fdc83SMarc-André Lureau VNC_DEBUG("Invalid audio channel count %d\n", 22593e230dd2SCorentin Chary read_u8(data, 5)); 22603e230dd2SCorentin Chary vnc_client_error(vs); 22613e230dd2SCorentin Chary break; 22623e230dd2SCorentin Chary } 22633e230dd2SCorentin Chary vs->as.freq = read_u32(data, 6); 22643e230dd2SCorentin Chary break; 22653e230dd2SCorentin Chary default: 2266153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 22673e230dd2SCorentin Chary vnc_client_error(vs); 22683e230dd2SCorentin Chary break; 22693e230dd2SCorentin Chary } 22703e230dd2SCorentin Chary break; 22713e230dd2SCorentin Chary 22723e230dd2SCorentin Chary default: 2273153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 22743e230dd2SCorentin Chary vnc_client_error(vs); 22753e230dd2SCorentin Chary break; 22763e230dd2SCorentin Chary } 22773e230dd2SCorentin Chary break; 22783e230dd2SCorentin Chary default: 2279153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 22803e230dd2SCorentin Chary vnc_client_error(vs); 22813e230dd2SCorentin Chary break; 22823e230dd2SCorentin Chary } 22833e230dd2SCorentin Chary 22843e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 22853e230dd2SCorentin Chary return 0; 22863e230dd2SCorentin Chary } 22873e230dd2SCorentin Chary 22883e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 22893e230dd2SCorentin Chary { 22903e230dd2SCorentin Chary char buf[1024]; 22918cf36489SGerd Hoffmann VncShareMode mode; 22923e230dd2SCorentin Chary int size; 22933e230dd2SCorentin Chary 22948cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 22958cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 22968cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 22978cf36489SGerd Hoffmann /* 22988cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 22998cf36489SGerd Hoffmann * 23008cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 23018cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 23028cf36489SGerd Hoffmann * reasons. 23038cf36489SGerd Hoffmann */ 23048cf36489SGerd Hoffmann break; 23058cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 23068cf36489SGerd Hoffmann /* 23078cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 23088cf36489SGerd Hoffmann * 23098cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 23108cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 23118cf36489SGerd Hoffmann * as no exclusive connection exists. 23128cf36489SGerd Hoffmann * 23138cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 23148cf36489SGerd Hoffmann */ 23158cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23168cf36489SGerd Hoffmann VncState *client; 23178cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 23188cf36489SGerd Hoffmann if (vs == client) { 23198cf36489SGerd Hoffmann continue; 23208cf36489SGerd Hoffmann } 23218cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 23228cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 23238cf36489SGerd Hoffmann continue; 23248cf36489SGerd Hoffmann } 23258cf36489SGerd Hoffmann vnc_disconnect_start(client); 23268cf36489SGerd Hoffmann } 23278cf36489SGerd Hoffmann } 23288cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 23298cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 23308cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23318cf36489SGerd Hoffmann return 0; 23328cf36489SGerd Hoffmann } 23338cf36489SGerd Hoffmann } 23348cf36489SGerd Hoffmann break; 23358cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 23368cf36489SGerd Hoffmann /* 23378cf36489SGerd Hoffmann * Policy: Shared connects only. 23388cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 23398cf36489SGerd Hoffmann * 23408cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 23418cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 23428cf36489SGerd Hoffmann * client disconnect everybody else. 23438cf36489SGerd Hoffmann */ 23448cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 23458cf36489SGerd Hoffmann vnc_disconnect_start(vs); 23468cf36489SGerd Hoffmann return 0; 23478cf36489SGerd Hoffmann } 23488cf36489SGerd Hoffmann break; 23498cf36489SGerd Hoffmann } 23508cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 23518cf36489SGerd Hoffmann 2352e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2353e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2354e5f34cddSGerd Hoffmann return 0; 2355e5f34cddSGerd Hoffmann } 2356e5f34cddSGerd Hoffmann 2357bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2358bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 23593e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 23603e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 23613e230dd2SCorentin Chary 23623e230dd2SCorentin Chary pixel_format_message(vs); 23633e230dd2SCorentin Chary 236497efe4f9SThomas Huth if (qemu_name) { 23653e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 236697efe4f9SThomas Huth if (size > sizeof(buf)) { 236797efe4f9SThomas Huth size = sizeof(buf); 236897efe4f9SThomas Huth } 236997efe4f9SThomas Huth } else { 23703e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 237197efe4f9SThomas Huth } 23723e230dd2SCorentin Chary 23733e230dd2SCorentin Chary vnc_write_u32(vs, size); 23743e230dd2SCorentin Chary vnc_write(vs, buf, size); 23753e230dd2SCorentin Chary vnc_flush(vs); 23763e230dd2SCorentin Chary 23773e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2378fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 23793e230dd2SCorentin Chary 23803e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 23813e230dd2SCorentin Chary 23823e230dd2SCorentin Chary return 0; 23833e230dd2SCorentin Chary } 23843e230dd2SCorentin Chary 23853e230dd2SCorentin Chary void start_client_init(VncState *vs) 23863e230dd2SCorentin Chary { 23873e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 23883e230dd2SCorentin Chary } 23893e230dd2SCorentin Chary 23903e230dd2SCorentin Chary static void make_challenge(VncState *vs) 23913e230dd2SCorentin Chary { 23923e230dd2SCorentin Chary int i; 23933e230dd2SCorentin Chary 23943e230dd2SCorentin Chary srand(time(NULL)+getpid()+getpid()*987654+rand()); 23953e230dd2SCorentin Chary 23963e230dd2SCorentin Chary for (i = 0 ; i < sizeof(vs->challenge) ; i++) 23973e230dd2SCorentin Chary vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0)); 23983e230dd2SCorentin Chary } 23993e230dd2SCorentin Chary 24003e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 24013e230dd2SCorentin Chary { 24023e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2403800567a6SDaniel P. Berrange size_t i, pwlen; 24043e230dd2SCorentin Chary unsigned char key[8]; 24053c9405a0SGerd Hoffmann time_t now = time(NULL); 240660928458SGonglei QCryptoCipher *cipher = NULL; 2407800567a6SDaniel P. Berrange Error *err = NULL; 24083e230dd2SCorentin Chary 24091cd20f8bSAnthony Liguori if (!vs->vd->password) { 24107364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); 24116bffdf0fSGerd Hoffmann goto reject; 24123e230dd2SCorentin Chary } 24133c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 24147364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); 24153c9405a0SGerd Hoffmann goto reject; 24163c9405a0SGerd Hoffmann } 24173e230dd2SCorentin Chary 24183e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 24193e230dd2SCorentin Chary 24203e230dd2SCorentin Chary /* Calculate the expected challenge response */ 24213e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 24223e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 24233e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2424800567a6SDaniel P. Berrange 2425800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2426800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2427800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2428800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2429800567a6SDaniel P. Berrange &err); 2430800567a6SDaniel P. Berrange if (!cipher) { 24317364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", 2432800567a6SDaniel P. Berrange error_get_pretty(err)); 2433800567a6SDaniel P. Berrange error_free(err); 2434800567a6SDaniel P. Berrange goto reject; 2435800567a6SDaniel P. Berrange } 2436800567a6SDaniel P. Berrange 2437a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2438800567a6SDaniel P. Berrange vs->challenge, 2439800567a6SDaniel P. Berrange response, 2440800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2441800567a6SDaniel P. Berrange &err) < 0) { 24427364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", 2443800567a6SDaniel P. Berrange error_get_pretty(err)); 2444800567a6SDaniel P. Berrange error_free(err); 2445800567a6SDaniel P. Berrange goto reject; 2446800567a6SDaniel P. Berrange } 24473e230dd2SCorentin Chary 24483e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 24493e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 24507364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); 24516bffdf0fSGerd Hoffmann goto reject; 24526bffdf0fSGerd Hoffmann } else { 24537364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 24546bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 24556bffdf0fSGerd Hoffmann vnc_flush(vs); 24566bffdf0fSGerd Hoffmann 24576bffdf0fSGerd Hoffmann start_client_init(vs); 24586bffdf0fSGerd Hoffmann } 245960928458SGonglei 246060928458SGonglei qcrypto_cipher_free(cipher); 24616bffdf0fSGerd Hoffmann return 0; 24626bffdf0fSGerd Hoffmann 24636bffdf0fSGerd Hoffmann reject: 24643e230dd2SCorentin Chary vnc_write_u32(vs, 1); /* Reject auth */ 24653e230dd2SCorentin Chary if (vs->minor >= 8) { 24663e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24673e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24683e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24693e230dd2SCorentin Chary } 24703e230dd2SCorentin Chary vnc_flush(vs); 24713e230dd2SCorentin Chary vnc_client_error(vs); 247260928458SGonglei qcrypto_cipher_free(cipher); 24733e230dd2SCorentin Chary return 0; 24743e230dd2SCorentin Chary } 24753e230dd2SCorentin Chary 24763e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 24773e230dd2SCorentin Chary { 24783e230dd2SCorentin Chary make_challenge(vs); 24793e230dd2SCorentin Chary /* Send client a 'random' challenge */ 24803e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 24813e230dd2SCorentin Chary vnc_flush(vs); 24823e230dd2SCorentin Chary 24833e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 24843e230dd2SCorentin Chary } 24853e230dd2SCorentin Chary 24863e230dd2SCorentin Chary 24873e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 24883e230dd2SCorentin Chary { 24893e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 24903e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 24917e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 24927364dbdaSDaniel P. Berrange trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); 24933e230dd2SCorentin Chary vnc_write_u32(vs, 1); 24943e230dd2SCorentin Chary if (vs->minor >= 8) { 24953e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 24963e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 24973e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 24983e230dd2SCorentin Chary } 24993e230dd2SCorentin Chary vnc_client_error(vs); 25003e230dd2SCorentin Chary } else { /* Accept requested auth */ 25017364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25027e7e2ebcSDaniel P. Berrange switch (vs->auth) { 25033e230dd2SCorentin Chary case VNC_AUTH_NONE: 25043e230dd2SCorentin Chary if (vs->minor >= 8) { 25053e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 25063e230dd2SCorentin Chary vnc_flush(vs); 25073e230dd2SCorentin Chary } 25087364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25093e230dd2SCorentin Chary start_client_init(vs); 25103e230dd2SCorentin Chary break; 25113e230dd2SCorentin Chary 25123e230dd2SCorentin Chary case VNC_AUTH_VNC: 25133e230dd2SCorentin Chary start_auth_vnc(vs); 25143e230dd2SCorentin Chary break; 25153e230dd2SCorentin Chary 25163e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 25173e230dd2SCorentin Chary start_auth_vencrypt(vs); 25183e230dd2SCorentin Chary break; 25193e230dd2SCorentin Chary 25203e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 25213e230dd2SCorentin Chary case VNC_AUTH_SASL: 25223e230dd2SCorentin Chary start_auth_sasl(vs); 25233e230dd2SCorentin Chary break; 25243e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 25253e230dd2SCorentin Chary 25263e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 25277364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); 25283e230dd2SCorentin Chary vnc_write_u8(vs, 1); 25293e230dd2SCorentin Chary if (vs->minor >= 8) { 25303e230dd2SCorentin Chary static const char err[] = "Authentication failed"; 25313e230dd2SCorentin Chary vnc_write_u32(vs, sizeof(err)); 25323e230dd2SCorentin Chary vnc_write(vs, err, sizeof(err)); 25333e230dd2SCorentin Chary } 25343e230dd2SCorentin Chary vnc_client_error(vs); 25353e230dd2SCorentin Chary } 25363e230dd2SCorentin Chary } 25373e230dd2SCorentin Chary return 0; 25383e230dd2SCorentin Chary } 25393e230dd2SCorentin Chary 25403e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 25413e230dd2SCorentin Chary { 25423e230dd2SCorentin Chary char local[13]; 25433e230dd2SCorentin Chary 25443e230dd2SCorentin Chary memcpy(local, version, 12); 25453e230dd2SCorentin Chary local[12] = 0; 25463e230dd2SCorentin Chary 25473e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 25483e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 25493e230dd2SCorentin Chary vnc_client_error(vs); 25503e230dd2SCorentin Chary return 0; 25513e230dd2SCorentin Chary } 25523e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 25533e230dd2SCorentin Chary if (vs->major != 3 || 25543e230dd2SCorentin Chary (vs->minor != 3 && 25553e230dd2SCorentin Chary vs->minor != 4 && 25563e230dd2SCorentin Chary vs->minor != 5 && 25573e230dd2SCorentin Chary vs->minor != 7 && 25583e230dd2SCorentin Chary vs->minor != 8)) { 25593e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 25603e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25613e230dd2SCorentin Chary vnc_flush(vs); 25623e230dd2SCorentin Chary vnc_client_error(vs); 25633e230dd2SCorentin Chary return 0; 25643e230dd2SCorentin Chary } 25653e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 25663e230dd2SCorentin Chary * as equivalent to v3.3 by servers 25673e230dd2SCorentin Chary */ 25683e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 25693e230dd2SCorentin Chary vs->minor = 3; 25703e230dd2SCorentin Chary 25713e230dd2SCorentin Chary if (vs->minor == 3) { 25727364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 25737e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 25747e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25753e230dd2SCorentin Chary vnc_flush(vs); 25767364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25773e230dd2SCorentin Chary start_client_init(vs); 25787e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 25793e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 25807e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 25813e230dd2SCorentin Chary vnc_flush(vs); 25823e230dd2SCorentin Chary start_auth_vnc(vs); 25833e230dd2SCorentin Chary } else { 25847364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, 25857364dbdaSDaniel P. Berrange "Unsupported auth method for v3.3", ""); 25863e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 25873e230dd2SCorentin Chary vnc_flush(vs); 25883e230dd2SCorentin Chary vnc_client_error(vs); 25893e230dd2SCorentin Chary } 25903e230dd2SCorentin Chary } else { 25913e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 25927e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 25933e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 25943e230dd2SCorentin Chary vnc_flush(vs); 25953e230dd2SCorentin Chary } 25963e230dd2SCorentin Chary 25973e230dd2SCorentin Chary return 0; 25983e230dd2SCorentin Chary } 25993e230dd2SCorentin Chary 2600999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2601999342a0SCorentin Chary { 2602999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2603999342a0SCorentin Chary 2604999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2605999342a0SCorentin Chary } 2606999342a0SCorentin Chary 26077d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 26087d964c9dSCorentin Chary { 26097d964c9dSCorentin Chary int i, j; 26107d964c9dSCorentin Chary 26117d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 26127d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 26137d964c9dSCorentin Chary x /= VNC_STAT_RECT; 26147d964c9dSCorentin Chary y /= VNC_STAT_RECT; 26157d964c9dSCorentin Chary 2616207f328aSCorentin Chary for (j = y; j <= h; j++) { 2617207f328aSCorentin Chary for (i = x; i <= w; i++) { 26187d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 26197d964c9dSCorentin Chary } 26207d964c9dSCorentin Chary } 26217d964c9dSCorentin Chary } 26227d964c9dSCorentin Chary 26237d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 26247d964c9dSCorentin Chary { 26257d964c9dSCorentin Chary VncState *vs; 26267d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 26277d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 26287d964c9dSCorentin Chary int has_dirty = 0; 26297d964c9dSCorentin Chary 26305a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 26315a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 26327d964c9dSCorentin Chary 26337d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2634bc2429b9SCorentin Chary int j; 26357d964c9dSCorentin Chary 26367d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 26377d964c9dSCorentin Chary if (vs->output.offset) { 26387d964c9dSCorentin Chary continue; 26397d964c9dSCorentin Chary } 26407d964c9dSCorentin Chary 26417d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 26427d964c9dSCorentin Chary continue; 26437d964c9dSCorentin Chary } 2644207f328aSCorentin Chary 26457d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 26467d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2647b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2648b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2649b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 26507d964c9dSCorentin Chary } 26517d964c9dSCorentin Chary has_dirty++; 26527d964c9dSCorentin Chary } 2653207f328aSCorentin Chary 26547d964c9dSCorentin Chary return has_dirty; 26557d964c9dSCorentin Chary } 26567d964c9dSCorentin Chary 26577d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2658999342a0SCorentin Chary { 2659eebe0b79SGerd Hoffmann int width = MIN(pixman_image_get_width(vd->guest.fb), 2660eebe0b79SGerd Hoffmann pixman_image_get_width(vd->server)); 2661eebe0b79SGerd Hoffmann int height = MIN(pixman_image_get_height(vd->guest.fb), 2662eebe0b79SGerd Hoffmann pixman_image_get_height(vd->server)); 2663999342a0SCorentin Chary int x, y; 2664999342a0SCorentin Chary struct timeval res; 26657d964c9dSCorentin Chary int has_dirty = 0; 2666999342a0SCorentin Chary 26679f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26689f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2669999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2670999342a0SCorentin Chary 2671999342a0SCorentin Chary rect->updated = false; 2672999342a0SCorentin Chary } 2673999342a0SCorentin Chary } 2674999342a0SCorentin Chary 2675ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2676999342a0SCorentin Chary 2677999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 26787d964c9dSCorentin Chary return has_dirty; 2679999342a0SCorentin Chary } 2680999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2681999342a0SCorentin Chary 26829f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 26839f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2684999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2685999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2686999342a0SCorentin Chary struct timeval min, max; 2687999342a0SCorentin Chary 2688999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2689999342a0SCorentin Chary continue ; 2690999342a0SCorentin Chary } 2691999342a0SCorentin Chary 2692999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2693ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2694999342a0SCorentin Chary 2695999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2696999342a0SCorentin Chary rect->freq = 0; 26977d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2698999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2699999342a0SCorentin Chary continue ; 2700999342a0SCorentin Chary } 2701999342a0SCorentin Chary 2702999342a0SCorentin Chary min = rect->times[rect->idx]; 2703999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2704ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2705999342a0SCorentin Chary 2706999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2707999342a0SCorentin Chary rect->freq /= count; 2708999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2709999342a0SCorentin Chary } 2710999342a0SCorentin Chary } 27117d964c9dSCorentin Chary return has_dirty; 2712999342a0SCorentin Chary } 2713999342a0SCorentin Chary 2714999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2715999342a0SCorentin Chary { 2716999342a0SCorentin Chary int i, j; 2717999342a0SCorentin Chary double total = 0; 2718999342a0SCorentin Chary int num = 0; 2719999342a0SCorentin Chary 27205a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 27215a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 2722999342a0SCorentin Chary 2723999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2724999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2725999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2726999342a0SCorentin Chary num++; 2727999342a0SCorentin Chary } 2728999342a0SCorentin Chary } 2729999342a0SCorentin Chary 2730999342a0SCorentin Chary if (num) { 2731999342a0SCorentin Chary return total / num; 2732999342a0SCorentin Chary } else { 2733999342a0SCorentin Chary return 0; 2734999342a0SCorentin Chary } 2735999342a0SCorentin Chary } 2736999342a0SCorentin Chary 2737999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2738999342a0SCorentin Chary { 2739999342a0SCorentin Chary VncRectStat *rect; 2740999342a0SCorentin Chary 2741999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2742999342a0SCorentin Chary if (rect->updated) { 2743999342a0SCorentin Chary return ; 2744999342a0SCorentin Chary } 2745999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2746999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2747999342a0SCorentin Chary rect->updated = true; 2748999342a0SCorentin Chary } 2749999342a0SCorentin Chary 27503e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 27513e230dd2SCorentin Chary { 2752bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2753bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2754bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2755bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2756eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 275712b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 27583e230dd2SCorentin Chary VncState *vs; 27593e230dd2SCorentin Chary int has_dirty = 0; 27609f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 27613e230dd2SCorentin Chary 276280e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2763999342a0SCorentin Chary 276480e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2765999342a0SCorentin Chary gettimeofday(&tv, NULL); 27667d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 276780e0c8c3SCorentin Chary } 2768999342a0SCorentin Chary 27693e230dd2SCorentin Chary /* 27703e230dd2SCorentin Chary * Walk through the guest dirty map. 27713e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 27723e230dd2SCorentin Chary * Update server dirty map. 27733e230dd2SCorentin Chary */ 2774bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2775eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2776eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2777bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2778bea60dd7SPeter Lieven server_stride); 27799f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 27809f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 27819f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 278212b316d4SPeter Lieven } else { 2783eb8934b0SGerd Hoffmann int guest_bpp = 2784eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 278512b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 278612b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2787659c90eeSMarc-André Lureau guest_ll = pixman_image_get_width(vd->guest.fb) * (DIV_ROUND_UP(guest_bpp, 8)); 27889f64916dSGerd Hoffmann } 2789eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 279012b316d4SPeter Lieven 279112b316d4SPeter Lieven for (;;) { 27923e230dd2SCorentin Chary int x; 279312b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 279412b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 279512b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 279612b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 279712b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 279812b316d4SPeter Lieven /* no more dirty bits */ 279912b316d4SPeter Lieven break; 280012b316d4SPeter Lieven } 280112b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 280212b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 280312b316d4SPeter Lieven 280412b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 28053e230dd2SCorentin Chary 28069f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2807bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 28089f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 28099f64916dSGerd Hoffmann } else { 281012b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 28119f64916dSGerd Hoffmann } 281212b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 28133e230dd2SCorentin Chary 281412b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 281512b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2816bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 281712b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 28183e230dd2SCorentin Chary continue; 2819b4c85ddcSPeter Lieven } 2820eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2821eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2822bea60dd7SPeter Lieven } 2823eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2824bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 28253e230dd2SCorentin Chary continue; 2826b4c85ddcSPeter Lieven } 2827bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 282812b316d4SPeter Lieven if (!vd->non_adaptive) { 282912b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 283012b316d4SPeter Lieven y, &tv); 283112b316d4SPeter Lieven } 28323e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 283312b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 28343e230dd2SCorentin Chary } 28353e230dd2SCorentin Chary has_dirty++; 28363e230dd2SCorentin Chary } 283712b316d4SPeter Lieven 283812b316d4SPeter Lieven y++; 28393e230dd2SCorentin Chary } 28409f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 28413e230dd2SCorentin Chary return has_dirty; 28423e230dd2SCorentin Chary } 28433e230dd2SCorentin Chary 28440f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 28453e230dd2SCorentin Chary { 28460f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 28473e230dd2SCorentin Chary VncState *vs, *vn; 28483e230dd2SCorentin Chary int has_dirty, rects = 0; 28493e230dd2SCorentin Chary 28509d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 28519d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 28529d6b2070SChenLiang return; 28539d6b2070SChenLiang } 28549d6b2070SChenLiang 28551d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 28563e230dd2SCorentin Chary 2857bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 28580f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2859bd023f95SCorentin Chary return; 2860bd023f95SCorentin Chary } 2861bd023f95SCorentin Chary 28623e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2863bd023f95SCorentin Chary vnc_unlock_display(vd); 28643e230dd2SCorentin Chary 28653e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 286638ee14f4SGerd Hoffmann rects += vnc_update_client(vs, has_dirty, false); 28673e230dd2SCorentin Chary /* vs might be free()ed here */ 28683e230dd2SCorentin Chary } 2869bd023f95SCorentin Chary 28703e230dd2SCorentin Chary if (has_dirty && rects) { 28710f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 28720f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 28730f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 28740f7b2864SGerd Hoffmann } 28753e230dd2SCorentin Chary } else { 28760f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 28770f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 28780f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 28793e230dd2SCorentin Chary } 28803e230dd2SCorentin Chary } 28813e230dd2SCorentin Chary } 28823e230dd2SCorentin Chary 288304d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 28842c8cf549SMichael Tokarev bool skipauth, bool websocket) 28853e230dd2SCorentin Chary { 2886fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 288790cd03a3SDaniel P. Berrange bool first_client = QTAILQ_EMPTY(&vd->clients); 28887d964c9dSCorentin Chary int i; 28897d964c9dSCorentin Chary 2890ad6374c4SDaniel P. Berrange trace_vnc_client_connect(vs, sioc); 289104d2529dSDaniel P. Berrange vs->sioc = sioc; 289204d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 289304d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 289404d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 2895d616ccc5SGerd Hoffmann vs->vd = vd; 28967e7e2ebcSDaniel P. Berrange 289704d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 289804d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 289904d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 2900543b9580SGerd Hoffmann 290104d2529dSDaniel P. Berrange buffer_init(&vs->tight.tight, "vnc-tight/%p", sioc); 290204d2529dSDaniel P. Berrange buffer_init(&vs->tight.zlib, "vnc-tight-zlib/%p", sioc); 290304d2529dSDaniel P. Berrange buffer_init(&vs->tight.gradient, "vnc-tight-gradient/%p", sioc); 2904543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 290504d2529dSDaniel P. Berrange buffer_init(&vs->tight.jpeg, "vnc-tight-jpeg/%p", sioc); 2906543b9580SGerd Hoffmann #endif 2907543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 290804d2529dSDaniel P. Berrange buffer_init(&vs->tight.png, "vnc-tight-png/%p", sioc); 2909543b9580SGerd Hoffmann #endif 291004d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 291104d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zrle, "vnc-zrle/%p", sioc); 291204d2529dSDaniel P. Berrange buffer_init(&vs->zrle.fb, "vnc-zrle-fb/%p", sioc); 291304d2529dSDaniel P. Berrange buffer_init(&vs->zrle.zlib, "vnc-zrle-zlib/%p", sioc); 2914543b9580SGerd Hoffmann 29157e7e2ebcSDaniel P. Berrange if (skipauth) { 29167e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 29177e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 29187e7e2ebcSDaniel P. Berrange } else { 2919f9148c8aSDaniel P. Berrange if (websocket) { 2920f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 2921f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 2922f9148c8aSDaniel P. Berrange } else { 29237e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 29247e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 29257e7e2ebcSDaniel P. Berrange } 2926f9148c8aSDaniel P. Berrange } 292704d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 292804d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 29297e7e2ebcSDaniel P. Berrange 29307267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 29317d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 2932fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 29337d964c9dSCorentin Chary } 29343e230dd2SCorentin Chary 293504d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 29360f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 293704d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 2938a75d6f07SBrandon Carpenter if (vs->ioc_tag) { 2939a75d6f07SBrandon Carpenter g_source_remove(vs->ioc_tag); 2940a75d6f07SBrandon Carpenter } 29417536ee4bSTim Hardeck if (websocket) { 29427536ee4bSTim Hardeck vs->websocket = 1; 294338e5756aSDaniel P. Berrange if (vd->tlscreds) { 294404d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294504d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 29463e305e4aSDaniel P. Berrange } else { 294704d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 294804d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 29490057a0d5STim Hardeck } 295004d2529dSDaniel P. Berrange } else { 295104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 295204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 29537536ee4bSTim Hardeck } 29543e230dd2SCorentin Chary 29553e230dd2SCorentin Chary vnc_client_cache_addr(vs); 2956fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 29578cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 29583e230dd2SCorentin Chary 29593e230dd2SCorentin Chary vs->last_x = -1; 29603e230dd2SCorentin Chary vs->last_y = -1; 29613e230dd2SCorentin Chary 29623e230dd2SCorentin Chary vs->as.freq = 44100; 29633e230dd2SCorentin Chary vs->as.nchannels = 2; 29643e230dd2SCorentin Chary vs->as.fmt = AUD_FMT_S16; 29653e230dd2SCorentin Chary vs->as.endianness = 0; 29663e230dd2SCorentin Chary 2967bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 2968175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 2969bd023f95SCorentin Chary 2970e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 2971c7628bffSGerd Hoffmann if (first_client) { 2972c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 2973c7628bffSGerd Hoffmann } 29743e230dd2SCorentin Chary 29751d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29763e230dd2SCorentin Chary 297790cd03a3SDaniel P. Berrange if (!vs->websocket) { 2978dbee9897SDaniel P. Berrange vnc_start_protocol(vs); 297990cd03a3SDaniel P. Berrange } 298090cd03a3SDaniel P. Berrange 298190cd03a3SDaniel P. Berrange if (vd->num_connecting > vd->connections_limit) { 298290cd03a3SDaniel P. Berrange QTAILQ_FOREACH(vs, &vd->clients, next) { 298390cd03a3SDaniel P. Berrange if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 298490cd03a3SDaniel P. Berrange vnc_disconnect_start(vs); 298590cd03a3SDaniel P. Berrange return; 298690cd03a3SDaniel P. Berrange } 298790cd03a3SDaniel P. Berrange } 298890cd03a3SDaniel P. Berrange } 298990cd03a3SDaniel P. Berrange } 299090cd03a3SDaniel P. Berrange 2991dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs) 299290cd03a3SDaniel P. Berrange { 29933e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 29943e230dd2SCorentin Chary vnc_flush(vs); 29953e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 29963e230dd2SCorentin Chary 29973e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 29983e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 29993e230dd2SCorentin Chary } 30003e230dd2SCorentin Chary 300104d2529dSDaniel P. Berrange static gboolean vnc_listen_io(QIOChannel *ioc, 300204d2529dSDaniel P. Berrange GIOCondition condition, 300304d2529dSDaniel P. Berrange void *opaque) 30043e230dd2SCorentin Chary { 3005bf01c179SDaniel P. Berrange VncDisplay *vd = opaque; 300604d2529dSDaniel P. Berrange QIOChannelSocket *sioc = NULL; 300704d2529dSDaniel P. Berrange Error *err = NULL; 30084ee74fa7SDaniel P. Berrange bool isWebsock = false; 30094ee74fa7SDaniel P. Berrange size_t i; 30104ee74fa7SDaniel P. Berrange 30114ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 30124ee74fa7SDaniel P. Berrange if (ioc == QIO_CHANNEL(vd->lwebsock[i])) { 30134ee74fa7SDaniel P. Berrange isWebsock = true; 30144ee74fa7SDaniel P. Berrange break; 30154ee74fa7SDaniel P. Berrange } 30164ee74fa7SDaniel P. Berrange } 30173e230dd2SCorentin Chary 301804d2529dSDaniel P. Berrange sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), &err); 301904d2529dSDaniel P. Berrange if (sioc != NULL) { 302010bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), 30214ee74fa7SDaniel P. Berrange isWebsock ? "vnc-ws-server" : "vnc-server"); 302204d2529dSDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 30234ee74fa7SDaniel P. Berrange vnc_connect(vd, sioc, false, isWebsock); 302404d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 30258e9b0d24SDaniel P. Berrange } else { 302604d2529dSDaniel P. Berrange /* client probably closed connection before we got there */ 302704d2529dSDaniel P. Berrange error_free(err); 30287536ee4bSTim Hardeck } 30293e230dd2SCorentin Chary 303004d2529dSDaniel P. Berrange return TRUE; 30317536ee4bSTim Hardeck } 30327536ee4bSTim Hardeck 30337c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 30347c20b4a3SGerd Hoffmann .dpy_name = "vnc", 30350f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 30367c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3037c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 303834da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 30397c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 30407c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 30417c20b4a3SGerd Hoffmann }; 30427c20b4a3SGerd Hoffmann 304314f7143eSGerd Hoffmann void vnc_display_init(const char *id) 30443e230dd2SCorentin Chary { 3045bf01c179SDaniel P. Berrange VncDisplay *vd; 30464db14629SGerd Hoffmann 30474db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 30484db14629SGerd Hoffmann return; 30494db14629SGerd Hoffmann } 3050bf01c179SDaniel P. Berrange vd = g_malloc0(sizeof(*vd)); 30513e230dd2SCorentin Chary 3052bf01c179SDaniel P. Berrange vd->id = strdup(id); 3053bf01c179SDaniel P. Berrange QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); 30543e230dd2SCorentin Chary 3055bf01c179SDaniel P. Berrange QTAILQ_INIT(&vd->clients); 3056bf01c179SDaniel P. Berrange vd->expires = TIME_MAX; 30573e230dd2SCorentin Chary 305840066175SGerd Hoffmann if (keyboard_layout) { 305940066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 3060bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); 306140066175SGerd Hoffmann } else { 3062bf01c179SDaniel P. Berrange vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); 306340066175SGerd Hoffmann } 30643e230dd2SCorentin Chary 3065bf01c179SDaniel P. Berrange if (!vd->kbd_layout) { 30663e230dd2SCorentin Chary exit(1); 3067bf01c179SDaniel P. Berrange } 30683e230dd2SCorentin Chary 3069bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 3070bf01c179SDaniel P. Berrange vd->connections_limit = 32; 307112e29b16SDaniel P. Berrange 3072bf01c179SDaniel P. Berrange qemu_mutex_init(&vd->mutex); 3073bd023f95SCorentin Chary vnc_start_worker_thread(); 3074bd023f95SCorentin Chary 3075bf01c179SDaniel P. Berrange vd->dcl.ops = &dcl_ops; 3076bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 30773e230dd2SCorentin Chary } 30783e230dd2SCorentin Chary 30793e230dd2SCorentin Chary 3080bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd) 30813e230dd2SCorentin Chary { 30824ee74fa7SDaniel P. Berrange size_t i; 3083bf01c179SDaniel P. Berrange if (!vd) { 30843e230dd2SCorentin Chary return; 308504d2529dSDaniel P. Berrange } 3086bf01c179SDaniel P. Berrange vd->is_unix = false; 30874ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlsock; i++) { 30884ee74fa7SDaniel P. Berrange if (vd->lsock_tag[i]) { 30894ee74fa7SDaniel P. Berrange g_source_remove(vd->lsock_tag[i]); 30903e230dd2SCorentin Chary } 30914ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lsock[i])); 30924ee74fa7SDaniel P. Berrange } 30934ee74fa7SDaniel P. Berrange g_free(vd->lsock); 30944ee74fa7SDaniel P. Berrange g_free(vd->lsock_tag); 3095bf01c179SDaniel P. Berrange vd->lsock = NULL; 30962dc120beSGerd Hoffmann vd->lsock_tag = NULL; 30974ee74fa7SDaniel P. Berrange vd->nlsock = 0; 30984ee74fa7SDaniel P. Berrange 30994ee74fa7SDaniel P. Berrange for (i = 0; i < vd->nlwebsock; i++) { 31004ee74fa7SDaniel P. Berrange if (vd->lwebsock_tag[i]) { 31014ee74fa7SDaniel P. Berrange g_source_remove(vd->lwebsock_tag[i]); 310204d2529dSDaniel P. Berrange } 31034ee74fa7SDaniel P. Berrange object_unref(OBJECT(vd->lwebsock[i])); 31047536ee4bSTim Hardeck } 31054ee74fa7SDaniel P. Berrange g_free(vd->lwebsock); 31064ee74fa7SDaniel P. Berrange g_free(vd->lwebsock_tag); 3107bf01c179SDaniel P. Berrange vd->lwebsock = NULL; 31082dc120beSGerd Hoffmann vd->lwebsock_tag = NULL; 31094ee74fa7SDaniel P. Berrange vd->nlwebsock = 0; 31104ee74fa7SDaniel P. Berrange 3111bf01c179SDaniel P. Berrange vd->auth = VNC_AUTH_INVALID; 3112bf01c179SDaniel P. Berrange vd->subauth = VNC_AUTH_INVALID; 3113bf01c179SDaniel P. Berrange if (vd->tlscreds) { 3114bf01c179SDaniel P. Berrange object_unparent(OBJECT(vd->tlscreds)); 3115bf01c179SDaniel P. Berrange vd->tlscreds = NULL; 3116bf01c179SDaniel P. Berrange } 3117bf01c179SDaniel P. Berrange g_free(vd->tlsaclname); 3118bf01c179SDaniel P. Berrange vd->tlsaclname = NULL; 3119a54f0d2bSPierre Ossman if (vd->lock_key_sync) { 3120a54f0d2bSPierre Ossman qemu_remove_led_event_handler(vd->led); 31212dc120beSGerd Hoffmann vd->led = NULL; 3122a54f0d2bSPierre Ossman } 31233e230dd2SCorentin Chary } 31243e230dd2SCorentin Chary 312514f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 31261cd20f8bSAnthony Liguori { 3127bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31281cd20f8bSAnthony Liguori 3129bf01c179SDaniel P. Berrange if (!vd) { 3130a6aa9d3eSLuiz Capitulino return -EINVAL; 31311cd20f8bSAnthony Liguori } 3132bf01c179SDaniel P. Berrange if (vd->auth == VNC_AUTH_NONE) { 3133cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 31347ea7d36eSMarkus Armbruster "password auth using '-vnc ${dpy},password'.\n"); 3135cf864569SGerd Hoffmann return -EINVAL; 31361cd20f8bSAnthony Liguori } 31371cd20f8bSAnthony Liguori 3138bf01c179SDaniel P. Berrange g_free(vd->password); 3139bf01c179SDaniel P. Berrange vd->password = g_strdup(password); 3140a6aa9d3eSLuiz Capitulino 3141a6aa9d3eSLuiz Capitulino return 0; 31423e230dd2SCorentin Chary } 31433e230dd2SCorentin Chary 314414f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 31453c9405a0SGerd Hoffmann { 3146bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 31473c9405a0SGerd Hoffmann 3148bf01c179SDaniel P. Berrange if (!vd) { 31491643f2b2SGerd Hoffmann return -EINVAL; 31501643f2b2SGerd Hoffmann } 31511643f2b2SGerd Hoffmann 3152bf01c179SDaniel P. Berrange vd->expires = expires; 31533c9405a0SGerd Hoffmann return 0; 31543c9405a0SGerd Hoffmann } 31553c9405a0SGerd Hoffmann 3156bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd) 31573e230dd2SCorentin Chary { 3158bd269ebcSMarkus Armbruster SocketAddress *addr; 315904d2529dSDaniel P. Berrange Error *err = NULL; 31603e230dd2SCorentin Chary 31614ee74fa7SDaniel P. Berrange if (!vd->nlsock) { 31624ee74fa7SDaniel P. Berrange return; 31634ee74fa7SDaniel P. Berrange } 31644ee74fa7SDaniel P. Berrange 31654ee74fa7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->lsock[0], &err); 316604d2529dSDaniel P. Berrange if (!addr) { 316733df7bf3SPaolo Bonzini return; 316804d2529dSDaniel P. Berrange } 316904d2529dSDaniel P. Berrange 3170bd269ebcSMarkus Armbruster if (addr->type != SOCKET_ADDRESS_TYPE_INET) { 3171bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 317233df7bf3SPaolo Bonzini return; 317304d2529dSDaniel P. Berrange } 317433df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3175bd269ebcSMarkus Armbruster addr->u.inet.host, 3176bd269ebcSMarkus Armbruster addr->u.inet.port); 3177bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 31783e230dd2SCorentin Chary } 31793e230dd2SCorentin Chary 31804db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 31814db14629SGerd Hoffmann .name = "vnc", 31824db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 31834db14629SGerd Hoffmann .implied_opt_name = "vnc", 31844db14629SGerd Hoffmann .desc = { 31854db14629SGerd Hoffmann { 31864db14629SGerd Hoffmann .name = "vnc", 31874db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31884db14629SGerd Hoffmann },{ 31894db14629SGerd Hoffmann .name = "websocket", 31904db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31914db14629SGerd Hoffmann },{ 31923e305e4aSDaniel P. Berrange .name = "tls-creds", 31933e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 31943e305e4aSDaniel P. Berrange },{ 31953e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 31964db14629SGerd Hoffmann .name = "x509", 31974db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 31984db14629SGerd Hoffmann },{ 31994db14629SGerd Hoffmann .name = "share", 32004db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 32014db14629SGerd Hoffmann },{ 32021d0d59feSGerd Hoffmann .name = "display", 32031d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 32041d0d59feSGerd Hoffmann },{ 32051d0d59feSGerd Hoffmann .name = "head", 32061d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 32071d0d59feSGerd Hoffmann },{ 3208e5f34cddSGerd Hoffmann .name = "connections", 3209e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3210e5f34cddSGerd Hoffmann },{ 321188428b7aSGonglei .name = "to", 321288428b7aSGonglei .type = QEMU_OPT_NUMBER, 321388428b7aSGonglei },{ 321488428b7aSGonglei .name = "ipv4", 321588428b7aSGonglei .type = QEMU_OPT_BOOL, 321688428b7aSGonglei },{ 321788428b7aSGonglei .name = "ipv6", 321888428b7aSGonglei .type = QEMU_OPT_BOOL, 321988428b7aSGonglei },{ 32204db14629SGerd Hoffmann .name = "password", 32214db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32224db14629SGerd Hoffmann },{ 32234db14629SGerd Hoffmann .name = "reverse", 32244db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32254db14629SGerd Hoffmann },{ 32264db14629SGerd Hoffmann .name = "lock-key-sync", 32274db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32284db14629SGerd Hoffmann },{ 3229c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3230c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3231c5ce8333SGerd Hoffmann },{ 32324db14629SGerd Hoffmann .name = "sasl", 32334db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32344db14629SGerd Hoffmann },{ 32353e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32364db14629SGerd Hoffmann .name = "tls", 32374db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32384db14629SGerd Hoffmann },{ 32393e305e4aSDaniel P. Berrange /* Deprecated in favour of tls-creds */ 32404db14629SGerd Hoffmann .name = "x509verify", 32418c7d0645SDaniel P. Berrange .type = QEMU_OPT_STRING, 32424db14629SGerd Hoffmann },{ 32434db14629SGerd Hoffmann .name = "acl", 32444db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32454db14629SGerd Hoffmann },{ 32464db14629SGerd Hoffmann .name = "lossy", 32474db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32484db14629SGerd Hoffmann },{ 32494db14629SGerd Hoffmann .name = "non-adaptive", 32504db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 32514db14629SGerd Hoffmann }, 32524db14629SGerd Hoffmann { /* end of list */ } 32534db14629SGerd Hoffmann }, 32544db14629SGerd Hoffmann }; 32554db14629SGerd Hoffmann 32560dd72e15SDaniel P. Berrange 32573e305e4aSDaniel P. Berrange static int 3258eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth, 3259eda24e18SDaniel P. Berrange int *subauth, 3260eda24e18SDaniel P. Berrange QCryptoTLSCreds *tlscreds, 32610dd72e15SDaniel P. Berrange bool password, 32620dd72e15SDaniel P. Berrange bool sasl, 32633e305e4aSDaniel P. Berrange bool websocket, 32643e305e4aSDaniel P. Berrange Error **errp) 32650dd72e15SDaniel P. Berrange { 32660dd72e15SDaniel P. Berrange /* 32670dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 32680dd72e15SDaniel P. Berrange * 32690dd72e15SDaniel P. Berrange * 1. none 32700dd72e15SDaniel P. Berrange * 2. vnc 32710dd72e15SDaniel P. Berrange * 3. sasl 32720dd72e15SDaniel P. Berrange * 32730dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 32740dd72e15SDaniel P. Berrange * 32750dd72e15SDaniel P. Berrange * 1. clear 32760dd72e15SDaniel P. Berrange * 2. tls 32770dd72e15SDaniel P. Berrange * 32780dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 32790dd72e15SDaniel P. Berrange * 32800dd72e15SDaniel P. Berrange * 1. anon 32810dd72e15SDaniel P. Berrange * 2. x509 32820dd72e15SDaniel P. Berrange * 32830dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 32840dd72e15SDaniel P. Berrange * 32850dd72e15SDaniel P. Berrange * 1. clear + none 32860dd72e15SDaniel P. Berrange * 2. clear + vnc 32870dd72e15SDaniel P. Berrange * 3. clear + sasl 32880dd72e15SDaniel P. Berrange * 4. tls + anon + none 32890dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 32900dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 32910dd72e15SDaniel P. Berrange * 7. tls + x509 + none 32920dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 32930dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 32940dd72e15SDaniel P. Berrange * 32950dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 32960dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 32970dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 32980dd72e15SDaniel P. Berrange * sub-auth types. 3299f9148c8aSDaniel P. Berrange * 3300f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3301f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3302f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3303f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3304f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3305f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3306f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3307f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3308f9148c8aSDaniel P. Berrange * 3309f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3310f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3311f9148c8aSDaniel P. Berrange * result has the same security characteristics. 33120dd72e15SDaniel P. Berrange */ 3313eda24e18SDaniel P. Berrange if (websocket || !tlscreds) { 33140dd72e15SDaniel P. Berrange if (password) { 33150dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 3316eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VNC; 33170dd72e15SDaniel P. Berrange } else if (sasl) { 33180dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 3319eda24e18SDaniel P. Berrange *auth = VNC_AUTH_SASL; 33200dd72e15SDaniel P. Berrange } else { 33210dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 3322eda24e18SDaniel P. Berrange *auth = VNC_AUTH_NONE; 33230dd72e15SDaniel P. Berrange } 3324eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_INVALID; 3325f9148c8aSDaniel P. Berrange } else { 3326eda24e18SDaniel P. Berrange bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), 3327eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; 3328eda24e18SDaniel P. Berrange bool is_anon = object_dynamic_cast(OBJECT(tlscreds), 3329eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; 3330eda24e18SDaniel P. Berrange 3331eda24e18SDaniel P. Berrange if (!is_x509 && !is_anon) { 3332eda24e18SDaniel P. Berrange error_setg(errp, 3333eda24e18SDaniel P. Berrange "Unsupported TLS cred type %s", 3334eda24e18SDaniel P. Berrange object_get_typename(OBJECT(tlscreds))); 3335eda24e18SDaniel P. Berrange return -1; 3336eda24e18SDaniel P. Berrange } 3337eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VENCRYPT; 3338eda24e18SDaniel P. Berrange if (password) { 3339eda24e18SDaniel P. Berrange if (is_x509) { 3340eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 3341eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509VNC; 3342eda24e18SDaniel P. Berrange } else { 3343eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 3344eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSVNC; 3345eda24e18SDaniel P. Berrange } 3346eda24e18SDaniel P. Berrange 3347eda24e18SDaniel P. Berrange } else if (sasl) { 3348eda24e18SDaniel P. Berrange if (is_x509) { 3349eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 3350eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509SASL; 3351eda24e18SDaniel P. Berrange } else { 3352eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 3353eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSSASL; 3354eda24e18SDaniel P. Berrange } 3355eda24e18SDaniel P. Berrange } else { 3356eda24e18SDaniel P. Berrange if (is_x509) { 3357eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 3358eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509NONE; 3359eda24e18SDaniel P. Berrange } else { 3360eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 3361eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSNONE; 3362eda24e18SDaniel P. Berrange } 3363f9148c8aSDaniel P. Berrange } 33640dd72e15SDaniel P. Berrange } 33653e305e4aSDaniel P. Berrange return 0; 33660dd72e15SDaniel P. Berrange } 33670dd72e15SDaniel P. Berrange 33683e305e4aSDaniel P. Berrange 33693e305e4aSDaniel P. Berrange /* 33703e305e4aSDaniel P. Berrange * Handle back compat with old CLI syntax by creating some 33713e305e4aSDaniel P. Berrange * suitable QCryptoTLSCreds objects 33723e305e4aSDaniel P. Berrange */ 33733e305e4aSDaniel P. Berrange static QCryptoTLSCreds * 33743e305e4aSDaniel P. Berrange vnc_display_create_creds(bool x509, 33753e305e4aSDaniel P. Berrange bool x509verify, 33763e305e4aSDaniel P. Berrange const char *dir, 33773e305e4aSDaniel P. Berrange const char *id, 33783e305e4aSDaniel P. Berrange Error **errp) 33793e305e4aSDaniel P. Berrange { 33803e305e4aSDaniel P. Berrange gchar *credsid = g_strdup_printf("tlsvnc%s", id); 33813e305e4aSDaniel P. Berrange Object *parent = object_get_objects_root(); 33823e305e4aSDaniel P. Berrange Object *creds; 33833e305e4aSDaniel P. Berrange Error *err = NULL; 33843e305e4aSDaniel P. Berrange 33853e305e4aSDaniel P. Berrange if (x509) { 33863e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_X509, 33873e305e4aSDaniel P. Berrange parent, 33883e305e4aSDaniel P. Berrange credsid, 33893e305e4aSDaniel P. Berrange &err, 33903e305e4aSDaniel P. Berrange "endpoint", "server", 33913e305e4aSDaniel P. Berrange "dir", dir, 33923e305e4aSDaniel P. Berrange "verify-peer", x509verify ? "yes" : "no", 33933e305e4aSDaniel P. Berrange NULL); 33943e305e4aSDaniel P. Berrange } else { 33953e305e4aSDaniel P. Berrange creds = object_new_with_props(TYPE_QCRYPTO_TLS_CREDS_ANON, 33963e305e4aSDaniel P. Berrange parent, 33973e305e4aSDaniel P. Berrange credsid, 33983e305e4aSDaniel P. Berrange &err, 33993e305e4aSDaniel P. Berrange "endpoint", "server", 34003e305e4aSDaniel P. Berrange NULL); 34013e305e4aSDaniel P. Berrange } 34023e305e4aSDaniel P. Berrange 34033e305e4aSDaniel P. Berrange g_free(credsid); 34043e305e4aSDaniel P. Berrange 34053e305e4aSDaniel P. Berrange if (err) { 34063e305e4aSDaniel P. Berrange error_propagate(errp, err); 34073e305e4aSDaniel P. Berrange return NULL; 34083e305e4aSDaniel P. Berrange } 34093e305e4aSDaniel P. Berrange 34103e305e4aSDaniel P. Berrange return QCRYPTO_TLS_CREDS(creds); 34113e305e4aSDaniel P. Berrange } 34123e305e4aSDaniel P. Berrange 34133e305e4aSDaniel P. Berrange 3414275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr, 3415275e0d61SDaniel P. Berrange bool websocket, 3416e5766eb4SGerd Hoffmann bool reverse, 3417275e0d61SDaniel P. Berrange int displaynum, 3418275e0d61SDaniel P. Berrange int to, 3419275e0d61SDaniel P. Berrange bool has_ipv4, 3420275e0d61SDaniel P. Berrange bool has_ipv6, 3421275e0d61SDaniel P. Berrange bool ipv4, 3422275e0d61SDaniel P. Berrange bool ipv6, 3423bd269ebcSMarkus Armbruster SocketAddress **retaddr, 3424275e0d61SDaniel P. Berrange Error **errp) 3425275e0d61SDaniel P. Berrange { 3426275e0d61SDaniel P. Berrange int ret = -1; 3427bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3428275e0d61SDaniel P. Berrange 3429bd269ebcSMarkus Armbruster addr = g_new0(SocketAddress, 1); 3430275e0d61SDaniel P. Berrange 3431275e0d61SDaniel P. Berrange if (strncmp(addrstr, "unix:", 5) == 0) { 3432bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_UNIX; 3433bd269ebcSMarkus Armbruster addr->u.q_unix.path = g_strdup(addrstr + 5); 3434275e0d61SDaniel P. Berrange 3435275e0d61SDaniel P. Berrange if (websocket) { 3436275e0d61SDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3437275e0d61SDaniel P. Berrange goto cleanup; 3438275e0d61SDaniel P. Berrange } 3439275e0d61SDaniel P. Berrange 3440275e0d61SDaniel P. Berrange if (to) { 3441275e0d61SDaniel P. Berrange error_setg(errp, "Port range not support with UNIX socket"); 3442275e0d61SDaniel P. Berrange goto cleanup; 3443275e0d61SDaniel P. Berrange } 3444275e0d61SDaniel P. Berrange ret = 0; 3445275e0d61SDaniel P. Berrange } else { 3446275e0d61SDaniel P. Berrange const char *port; 3447275e0d61SDaniel P. Berrange size_t hostlen; 3448275e0d61SDaniel P. Berrange unsigned long long baseport = 0; 3449275e0d61SDaniel P. Berrange InetSocketAddress *inet; 3450275e0d61SDaniel P. Berrange 3451275e0d61SDaniel P. Berrange port = strrchr(addrstr, ':'); 3452275e0d61SDaniel P. Berrange if (!port) { 3453275e0d61SDaniel P. Berrange if (websocket) { 3454275e0d61SDaniel P. Berrange hostlen = 0; 3455275e0d61SDaniel P. Berrange port = addrstr; 3456275e0d61SDaniel P. Berrange } else { 3457275e0d61SDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3458275e0d61SDaniel P. Berrange goto cleanup; 3459275e0d61SDaniel P. Berrange } 3460275e0d61SDaniel P. Berrange } else { 3461275e0d61SDaniel P. Berrange hostlen = port - addrstr; 3462275e0d61SDaniel P. Berrange port++; 3463275e0d61SDaniel P. Berrange if (*port == '\0') { 3464275e0d61SDaniel P. Berrange error_setg(errp, "vnc port cannot be empty"); 3465275e0d61SDaniel P. Berrange goto cleanup; 3466275e0d61SDaniel P. Berrange } 3467275e0d61SDaniel P. Berrange } 3468275e0d61SDaniel P. Berrange 3469bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_INET; 3470bd269ebcSMarkus Armbruster inet = &addr->u.inet; 3471275e0d61SDaniel P. Berrange if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { 3472275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr + 1, hostlen - 2); 3473275e0d61SDaniel P. Berrange } else { 3474275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr, hostlen); 3475275e0d61SDaniel P. Berrange } 3476275e0d61SDaniel P. Berrange /* plain VNC port is just an offset, for websocket 3477275e0d61SDaniel P. Berrange * port is absolute */ 3478275e0d61SDaniel P. Berrange if (websocket) { 3479275e0d61SDaniel P. Berrange if (g_str_equal(addrstr, "") || 3480275e0d61SDaniel P. Berrange g_str_equal(addrstr, "on")) { 3481396f935aSDaniel P. Berrange if (displaynum == -1) { 3482396f935aSDaniel P. Berrange error_setg(errp, "explicit websocket port is required"); 3483396f935aSDaniel P. Berrange goto cleanup; 3484396f935aSDaniel P. Berrange } 3485275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3486275e0d61SDaniel P. Berrange "%d", displaynum + 5700); 3487275e0d61SDaniel P. Berrange if (to) { 3488275e0d61SDaniel P. Berrange inet->has_to = true; 3489275e0d61SDaniel P. Berrange inet->to = to + 5700; 3490275e0d61SDaniel P. Berrange } 3491275e0d61SDaniel P. Berrange } else { 3492275e0d61SDaniel P. Berrange inet->port = g_strdup(port); 3493275e0d61SDaniel P. Berrange } 3494275e0d61SDaniel P. Berrange } else { 3495e5766eb4SGerd Hoffmann int offset = reverse ? 0 : 5900; 3496275e0d61SDaniel P. Berrange if (parse_uint_full(port, &baseport, 10) < 0) { 3497275e0d61SDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", port); 3498275e0d61SDaniel P. Berrange goto cleanup; 3499275e0d61SDaniel P. Berrange } 3500275e0d61SDaniel P. Berrange if (baseport > 65535 || 3501e5766eb4SGerd Hoffmann baseport + offset > 65535) { 3502275e0d61SDaniel P. Berrange error_setg(errp, "port %s out of range", port); 3503275e0d61SDaniel P. Berrange goto cleanup; 3504275e0d61SDaniel P. Berrange } 3505275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3506e5766eb4SGerd Hoffmann "%d", (int)baseport + offset); 3507275e0d61SDaniel P. Berrange 3508275e0d61SDaniel P. Berrange if (to) { 3509275e0d61SDaniel P. Berrange inet->has_to = true; 3510e5766eb4SGerd Hoffmann inet->to = to + offset; 3511275e0d61SDaniel P. Berrange } 3512275e0d61SDaniel P. Berrange } 3513275e0d61SDaniel P. Berrange 3514275e0d61SDaniel P. Berrange inet->ipv4 = ipv4; 3515275e0d61SDaniel P. Berrange inet->has_ipv4 = has_ipv4; 3516275e0d61SDaniel P. Berrange inet->ipv6 = ipv6; 3517275e0d61SDaniel P. Berrange inet->has_ipv6 = has_ipv6; 3518275e0d61SDaniel P. Berrange 3519275e0d61SDaniel P. Berrange ret = baseport; 3520275e0d61SDaniel P. Berrange } 3521275e0d61SDaniel P. Berrange 3522275e0d61SDaniel P. Berrange *retaddr = addr; 3523275e0d61SDaniel P. Berrange 3524275e0d61SDaniel P. Berrange cleanup: 3525275e0d61SDaniel P. Berrange if (ret < 0) { 3526bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 3527275e0d61SDaniel P. Berrange } 3528275e0d61SDaniel P. Berrange return ret; 3529275e0d61SDaniel P. Berrange } 3530275e0d61SDaniel P. Berrange 35319f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr, 35329f26f325SPhilippe Mathieu-Daudé size_t *retnsaddr) 35339f26f325SPhilippe Mathieu-Daudé { 35349f26f325SPhilippe Mathieu-Daudé size_t i; 35359f26f325SPhilippe Mathieu-Daudé 35369f26f325SPhilippe Mathieu-Daudé for (i = 0; i < *retnsaddr; i++) { 35379f26f325SPhilippe Mathieu-Daudé qapi_free_SocketAddress((*retsaddr)[i]); 35389f26f325SPhilippe Mathieu-Daudé } 35399f26f325SPhilippe Mathieu-Daudé g_free(*retsaddr); 35409f26f325SPhilippe Mathieu-Daudé 35419f26f325SPhilippe Mathieu-Daudé *retsaddr = NULL; 35429f26f325SPhilippe Mathieu-Daudé *retnsaddr = 0; 35439f26f325SPhilippe Mathieu-Daudé } 35449f26f325SPhilippe Mathieu-Daudé 3545275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts, 3546e5766eb4SGerd Hoffmann bool reverse, 3547bd269ebcSMarkus Armbruster SocketAddress ***retsaddr, 3548396f935aSDaniel P. Berrange size_t *retnsaddr, 3549bd269ebcSMarkus Armbruster SocketAddress ***retwsaddr, 3550396f935aSDaniel P. Berrange size_t *retnwsaddr, 3551275e0d61SDaniel P. Berrange Error **errp) 3552275e0d61SDaniel P. Berrange { 3553bd269ebcSMarkus Armbruster SocketAddress *saddr = NULL; 3554bd269ebcSMarkus Armbruster SocketAddress *wsaddr = NULL; 3555396f935aSDaniel P. Berrange QemuOptsIter addriter; 3556396f935aSDaniel P. Berrange const char *addr; 3557275e0d61SDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3558275e0d61SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 3559275e0d61SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 3560275e0d61SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3561275e0d61SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3562396f935aSDaniel P. Berrange int displaynum = -1; 3563396f935aSDaniel P. Berrange int ret = -1; 3564275e0d61SDaniel P. Berrange 3565275e0d61SDaniel P. Berrange *retsaddr = NULL; 3566396f935aSDaniel P. Berrange *retnsaddr = 0; 3567275e0d61SDaniel P. Berrange *retwsaddr = NULL; 3568396f935aSDaniel P. Berrange *retnwsaddr = 0; 3569275e0d61SDaniel P. Berrange 3570396f935aSDaniel P. Berrange addr = qemu_opt_get(opts, "vnc"); 3571396f935aSDaniel P. Berrange if (addr == NULL || g_str_equal(addr, "none")) { 3572396f935aSDaniel P. Berrange ret = 0; 3573396f935aSDaniel P. Berrange goto cleanup; 3574396f935aSDaniel P. Berrange } 3575396f935aSDaniel P. Berrange if (qemu_opt_get(opts, "websocket") && 3576275e0d61SDaniel P. Berrange !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3577275e0d61SDaniel P. Berrange error_setg(errp, 3578275e0d61SDaniel P. Berrange "SHA1 hash support is required for websockets"); 3579396f935aSDaniel P. Berrange goto cleanup; 3580275e0d61SDaniel P. Berrange } 3581275e0d61SDaniel P. Berrange 3582396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "vnc"); 3583396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3584396f935aSDaniel P. Berrange int rv; 3585e5766eb4SGerd Hoffmann rv = vnc_display_get_address(addr, false, reverse, 0, to, 3586275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3587275e0d61SDaniel P. Berrange ipv4, ipv6, 3588275e0d61SDaniel P. Berrange &saddr, errp); 3589396f935aSDaniel P. Berrange if (rv < 0) { 3590396f935aSDaniel P. Berrange goto cleanup; 3591275e0d61SDaniel P. Berrange } 3592396f935aSDaniel P. Berrange /* Historical compat - first listen address can be used 3593396f935aSDaniel P. Berrange * to set the default websocket port 3594396f935aSDaniel P. Berrange */ 3595396f935aSDaniel P. Berrange if (displaynum == -1) { 3596396f935aSDaniel P. Berrange displaynum = rv; 3597396f935aSDaniel P. Berrange } 3598bd269ebcSMarkus Armbruster *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1); 3599396f935aSDaniel P. Berrange (*retsaddr)[(*retnsaddr)++] = saddr; 3600396f935aSDaniel P. Berrange } 3601396f935aSDaniel P. Berrange 3602396f935aSDaniel P. Berrange /* If we had multiple primary displays, we don't do defaults 3603396f935aSDaniel P. Berrange * for websocket, and require explicit config instead. */ 3604396f935aSDaniel P. Berrange if (*retnsaddr > 1) { 3605396f935aSDaniel P. Berrange displaynum = -1; 3606396f935aSDaniel P. Berrange } 3607396f935aSDaniel P. Berrange 3608396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "websocket"); 3609396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3610e5766eb4SGerd Hoffmann if (vnc_display_get_address(addr, true, reverse, displaynum, to, 3611275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3612275e0d61SDaniel P. Berrange ipv4, ipv6, 3613275e0d61SDaniel P. Berrange &wsaddr, errp) < 0) { 3614396f935aSDaniel P. Berrange goto cleanup; 3615275e0d61SDaniel P. Berrange } 3616396f935aSDaniel P. Berrange 3617396f935aSDaniel P. Berrange /* Historical compat - if only a single listen address was 3618396f935aSDaniel P. Berrange * provided, then this is used to set the default listen 3619396f935aSDaniel P. Berrange * address for websocket too 3620396f935aSDaniel P. Berrange */ 3621396f935aSDaniel P. Berrange if (*retnsaddr == 1 && 3622bd269ebcSMarkus Armbruster (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET && 3623bd269ebcSMarkus Armbruster wsaddr->type == SOCKET_ADDRESS_TYPE_INET && 3624bd269ebcSMarkus Armbruster g_str_equal(wsaddr->u.inet.host, "") && 3625bd269ebcSMarkus Armbruster !g_str_equal((*retsaddr)[0]->u.inet.host, "")) { 3626bd269ebcSMarkus Armbruster g_free(wsaddr->u.inet.host); 3627bd269ebcSMarkus Armbruster wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host); 3628275e0d61SDaniel P. Berrange } 3629275e0d61SDaniel P. Berrange 3630bd269ebcSMarkus Armbruster *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1); 3631396f935aSDaniel P. Berrange (*retwsaddr)[(*retnwsaddr)++] = wsaddr; 3632396f935aSDaniel P. Berrange } 3633396f935aSDaniel P. Berrange 3634396f935aSDaniel P. Berrange ret = 0; 3635396f935aSDaniel P. Berrange cleanup: 3636396f935aSDaniel P. Berrange if (ret < 0) { 36379f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retsaddr, retnsaddr); 36389f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retwsaddr, retnwsaddr); 3639396f935aSDaniel P. Berrange } 3640396f935aSDaniel P. Berrange return ret; 3641275e0d61SDaniel P. Berrange } 3642275e0d61SDaniel P. Berrange 36438bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd, 3644bd269ebcSMarkus Armbruster SocketAddress **saddr, 3645396f935aSDaniel P. Berrange size_t nsaddr, 3646bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3647396f935aSDaniel P. Berrange size_t nwsaddr, 36488bd22f47SDaniel P. Berrange Error **errp) 36498bd22f47SDaniel P. Berrange { 36508bd22f47SDaniel P. Berrange /* connect to viewer */ 36518bd22f47SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3652396f935aSDaniel P. Berrange if (nwsaddr != 0) { 36538bd22f47SDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 36548bd22f47SDaniel P. Berrange return -1; 36558bd22f47SDaniel P. Berrange } 3656396f935aSDaniel P. Berrange if (nsaddr != 1) { 3657396f935aSDaniel P. Berrange error_setg(errp, "Expected a single address in reverse mode"); 3658396f935aSDaniel P. Berrange return -1; 3659396f935aSDaniel P. Berrange } 3660bd269ebcSMarkus Armbruster /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */ 3661bd269ebcSMarkus Armbruster vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX; 36628bd22f47SDaniel P. Berrange sioc = qio_channel_socket_new(); 36638bd22f47SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); 3664396f935aSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) { 36658bd22f47SDaniel P. Berrange return -1; 36668bd22f47SDaniel P. Berrange } 36678bd22f47SDaniel P. Berrange vnc_connect(vd, sioc, false, false); 36688bd22f47SDaniel P. Berrange object_unref(OBJECT(sioc)); 36698bd22f47SDaniel P. Berrange return 0; 36708bd22f47SDaniel P. Berrange } 36718bd22f47SDaniel P. Berrange 36728bd22f47SDaniel P. Berrange 36738bd22f47SDaniel P. Berrange static int vnc_display_listen_addr(VncDisplay *vd, 3674bd269ebcSMarkus Armbruster SocketAddress *addr, 36758bd22f47SDaniel P. Berrange const char *name, 36768bd22f47SDaniel P. Berrange QIOChannelSocket ***lsock, 36778bd22f47SDaniel P. Berrange guint **lsock_tag, 36788bd22f47SDaniel P. Berrange size_t *nlsock, 36798bd22f47SDaniel P. Berrange Error **errp) 36808bd22f47SDaniel P. Berrange { 368157a6d6d5SDaniel P. Berrange QIODNSResolver *resolver = qio_dns_resolver_get_instance(); 3682bd269ebcSMarkus Armbruster SocketAddress **rawaddrs = NULL; 368357a6d6d5SDaniel P. Berrange size_t nrawaddrs = 0; 368457a6d6d5SDaniel P. Berrange Error *listenerr = NULL; 3685396f935aSDaniel P. Berrange bool listening = false; 368657a6d6d5SDaniel P. Berrange size_t i; 36878bd22f47SDaniel P. Berrange 368857a6d6d5SDaniel P. Berrange if (qio_dns_resolver_lookup_sync(resolver, addr, &nrawaddrs, 368957a6d6d5SDaniel P. Berrange &rawaddrs, errp) < 0) { 36908bd22f47SDaniel P. Berrange return -1; 36918bd22f47SDaniel P. Berrange } 36928bd22f47SDaniel P. Berrange 369357a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 369457a6d6d5SDaniel P. Berrange QIOChannelSocket *sioc = qio_channel_socket_new(); 369557a6d6d5SDaniel P. Berrange 369657a6d6d5SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), name); 369757a6d6d5SDaniel P. Berrange if (qio_channel_socket_listen_sync( 369857a6d6d5SDaniel P. Berrange sioc, rawaddrs[i], listenerr == NULL ? &listenerr : NULL) < 0) { 36997bc4f084SMarc-André Lureau object_unref(OBJECT(sioc)); 370057a6d6d5SDaniel P. Berrange continue; 370157a6d6d5SDaniel P. Berrange } 3702396f935aSDaniel P. Berrange listening = true; 370357a6d6d5SDaniel P. Berrange (*nlsock)++; 370457a6d6d5SDaniel P. Berrange *lsock = g_renew(QIOChannelSocket *, *lsock, *nlsock); 370557a6d6d5SDaniel P. Berrange *lsock_tag = g_renew(guint, *lsock_tag, *nlsock); 370657a6d6d5SDaniel P. Berrange 370757a6d6d5SDaniel P. Berrange (*lsock)[*nlsock - 1] = sioc; 370857a6d6d5SDaniel P. Berrange (*lsock_tag)[*nlsock - 1] = 0; 370957a6d6d5SDaniel P. Berrange } 371057a6d6d5SDaniel P. Berrange 371157a6d6d5SDaniel P. Berrange for (i = 0; i < nrawaddrs; i++) { 3712bd269ebcSMarkus Armbruster qapi_free_SocketAddress(rawaddrs[i]); 371357a6d6d5SDaniel P. Berrange } 371457a6d6d5SDaniel P. Berrange g_free(rawaddrs); 371557a6d6d5SDaniel P. Berrange 371657a6d6d5SDaniel P. Berrange if (listenerr) { 3717396f935aSDaniel P. Berrange if (!listening) { 371857a6d6d5SDaniel P. Berrange error_propagate(errp, listenerr); 371957a6d6d5SDaniel P. Berrange return -1; 372057a6d6d5SDaniel P. Berrange } else { 372157a6d6d5SDaniel P. Berrange error_free(listenerr); 372257a6d6d5SDaniel P. Berrange } 372357a6d6d5SDaniel P. Berrange } 372457a6d6d5SDaniel P. Berrange 372557a6d6d5SDaniel P. Berrange for (i = 0; i < *nlsock; i++) { 372657a6d6d5SDaniel P. Berrange (*lsock_tag)[i] = qio_channel_add_watch( 372757a6d6d5SDaniel P. Berrange QIO_CHANNEL((*lsock)[i]), 37288bd22f47SDaniel P. Berrange G_IO_IN, vnc_listen_io, vd, NULL); 372957a6d6d5SDaniel P. Berrange } 37308bd22f47SDaniel P. Berrange 37318bd22f47SDaniel P. Berrange return 0; 37328bd22f47SDaniel P. Berrange } 37338bd22f47SDaniel P. Berrange 37348bd22f47SDaniel P. Berrange 37358bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd, 3736bd269ebcSMarkus Armbruster SocketAddress **saddr, 3737396f935aSDaniel P. Berrange size_t nsaddr, 3738bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3739396f935aSDaniel P. Berrange size_t nwsaddr, 37408bd22f47SDaniel P. Berrange Error **errp) 37418bd22f47SDaniel P. Berrange { 3742396f935aSDaniel P. Berrange size_t i; 37438bd22f47SDaniel P. Berrange 3744396f935aSDaniel P. Berrange for (i = 0; i < nsaddr; i++) { 3745396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, saddr[i], 37468bd22f47SDaniel P. Berrange "vnc-listen", 37478bd22f47SDaniel P. Berrange &vd->lsock, 37488bd22f47SDaniel P. Berrange &vd->lsock_tag, 37498bd22f47SDaniel P. Berrange &vd->nlsock, 37508bd22f47SDaniel P. Berrange errp) < 0) { 37518bd22f47SDaniel P. Berrange return -1; 37528bd22f47SDaniel P. Berrange } 3753396f935aSDaniel P. Berrange } 3754396f935aSDaniel P. Berrange for (i = 0; i < nwsaddr; i++) { 3755396f935aSDaniel P. Berrange if (vnc_display_listen_addr(vd, wsaddr[i], 37568bd22f47SDaniel P. Berrange "vnc-ws-listen", 37578bd22f47SDaniel P. Berrange &vd->lwebsock, 37588bd22f47SDaniel P. Berrange &vd->lwebsock_tag, 37598bd22f47SDaniel P. Berrange &vd->nlwebsock, 37608bd22f47SDaniel P. Berrange errp) < 0) { 37618bd22f47SDaniel P. Berrange return -1; 37628bd22f47SDaniel P. Berrange } 3763396f935aSDaniel P. Berrange } 37648bd22f47SDaniel P. Berrange 37658bd22f47SDaniel P. Berrange return 0; 37668bd22f47SDaniel P. Berrange } 37678bd22f47SDaniel P. Berrange 37688bd22f47SDaniel P. Berrange 37694db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 37703e230dd2SCorentin Chary { 3771bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 37724db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3773bd269ebcSMarkus Armbruster SocketAddress **saddr = NULL, **wsaddr = NULL; 3774396f935aSDaniel P. Berrange size_t nsaddr, nwsaddr; 3775e2a11d9dSGonglei const char *share, *device_id; 37761d0d59feSGerd Hoffmann QemuConsole *con; 3777a2c72de0SGonglei bool password = false; 3778a2c72de0SGonglei bool reverse = false; 37793e305e4aSDaniel P. Berrange const char *credid; 3780a2c72de0SGonglei bool sasl = false; 3781d169f04bSDaniel P. Berrange #ifdef CONFIG_VNC_SASL 37823e230dd2SCorentin Chary int saslErr; 37833e230dd2SCorentin Chary #endif 37843e230dd2SCorentin Chary int acl = 0; 37853e230dd2SCorentin Chary int lock_key_sync = 1; 3786c5ce8333SGerd Hoffmann int key_delay_ms; 37873e230dd2SCorentin Chary 3788bf01c179SDaniel P. Berrange if (!vd) { 37892d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 37902d55f0e8SPaolo Bonzini return; 37912d55f0e8SPaolo Bonzini } 3792bf01c179SDaniel P. Berrange vnc_display_close(vd); 37934db14629SGerd Hoffmann 37944db14629SGerd Hoffmann if (!opts) { 37952d55f0e8SPaolo Bonzini return; 37964db14629SGerd Hoffmann } 3797275e0d61SDaniel P. Berrange 3798e5766eb4SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 3799e5766eb4SGerd Hoffmann if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr, 3800396f935aSDaniel P. Berrange &wsaddr, &nwsaddr, errp) < 0) { 3801275e0d61SDaniel P. Berrange goto fail; 3802275e0d61SDaniel P. Berrange } 3803275e0d61SDaniel P. Berrange 38044db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3805800567a6SDaniel P. Berrange if (password) { 3806800567a6SDaniel P. Berrange if (fips_get_state()) { 38072d55f0e8SPaolo Bonzini error_setg(errp, 38080f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 38090f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 38102d55f0e8SPaolo Bonzini "methods as an alternative"); 38111ce52c78SPaolo Bonzini goto fail; 38120f66998fSPaul Moore } 3813800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3814f844836dSGonglei QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) { 3815800567a6SDaniel P. Berrange error_setg(errp, 3816800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3817800567a6SDaniel P. Berrange goto fail; 3818800567a6SDaniel P. Berrange } 3819800567a6SDaniel P. Berrange } 38204db14629SGerd Hoffmann 38214db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3822d3b0db6dSAlexander Graf key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); 38234db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3824d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3825d169f04bSDaniel P. Berrange if (sasl) { 3826d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3827d169f04bSDaniel P. Berrange goto fail; 3828d169f04bSDaniel P. Berrange } 3829d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 38303e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 38313e305e4aSDaniel P. Berrange if (credid) { 38323e305e4aSDaniel P. Berrange Object *creds; 38333e305e4aSDaniel P. Berrange if (qemu_opt_get(opts, "tls") || 38343e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509") || 38353e305e4aSDaniel P. Berrange qemu_opt_get(opts, "x509verify")) { 38363e305e4aSDaniel P. Berrange error_setg(errp, 3837c62e90afSWolfgang Bumiller "'tls-creds' parameter is mutually exclusive with " 38383e305e4aSDaniel P. Berrange "'tls', 'x509' and 'x509verify' parameters"); 38393e305e4aSDaniel P. Berrange goto fail; 38403e305e4aSDaniel P. Berrange } 38413e305e4aSDaniel P. Berrange 38423e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 38433e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 38443e305e4aSDaniel P. Berrange if (!creds) { 38453e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 38463e305e4aSDaniel P. Berrange credid); 38473e305e4aSDaniel P. Berrange goto fail; 38483e305e4aSDaniel P. Berrange } 3849bf01c179SDaniel P. Berrange vd->tlscreds = (QCryptoTLSCreds *) 38503e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 38513e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 3852bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38533e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 38543e305e4aSDaniel P. Berrange credid); 38553e305e4aSDaniel P. Berrange goto fail; 38563e305e4aSDaniel P. Berrange } 3857bf01c179SDaniel P. Berrange object_ref(OBJECT(vd->tlscreds)); 38583e305e4aSDaniel P. Berrange 3859bf01c179SDaniel P. Berrange if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 38603e305e4aSDaniel P. Berrange error_setg(errp, 38613e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 38623e305e4aSDaniel P. Berrange goto fail; 38633e305e4aSDaniel P. Berrange } 38643e305e4aSDaniel P. Berrange } else { 38653e305e4aSDaniel P. Berrange const char *path; 38663e305e4aSDaniel P. Berrange bool tls = false, x509 = false, x509verify = false; 38674db14629SGerd Hoffmann tls = qemu_opt_get_bool(opts, "tls", false); 38683e305e4aSDaniel P. Berrange if (tls) { 38694db14629SGerd Hoffmann path = qemu_opt_get(opts, "x509"); 38703e305e4aSDaniel P. Berrange 38714db14629SGerd Hoffmann if (path) { 3872a2c72de0SGonglei x509 = true; 38733e305e4aSDaniel P. Berrange } else { 38743e305e4aSDaniel P. Berrange path = qemu_opt_get(opts, "x509verify"); 38753e305e4aSDaniel P. Berrange if (path) { 38763e305e4aSDaniel P. Berrange x509 = true; 38773e305e4aSDaniel P. Berrange x509verify = true; 38783e305e4aSDaniel P. Berrange } 38793e305e4aSDaniel P. Berrange } 3880bf01c179SDaniel P. Berrange vd->tlscreds = vnc_display_create_creds(x509, 38813e305e4aSDaniel P. Berrange x509verify, 38823e305e4aSDaniel P. Berrange path, 3883bf01c179SDaniel P. Berrange vd->id, 38843e305e4aSDaniel P. Berrange errp); 3885bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38861ce52c78SPaolo Bonzini goto fail; 38873e230dd2SCorentin Chary } 38883e230dd2SCorentin Chary } 3889d169f04bSDaniel P. Berrange } 38904db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 38914db14629SGerd Hoffmann 38924db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 38934db14629SGerd Hoffmann if (share) { 38944db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 3895bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_IGNORE; 38964db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 3897bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 38984db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 3899bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 39008cf36489SGerd Hoffmann } else { 39012d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 39021ce52c78SPaolo Bonzini goto fail; 39038cf36489SGerd Hoffmann } 39044db14629SGerd Hoffmann } else { 3905bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39063e230dd2SCorentin Chary } 3907bf01c179SDaniel P. Berrange vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); 39083e230dd2SCorentin Chary 39094db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 3910bf01c179SDaniel P. Berrange vd->lossy = qemu_opt_get_bool(opts, "lossy", false); 39114db14629SGerd Hoffmann #endif 3912bf01c179SDaniel P. Berrange vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3913e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3914e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3915e22492d3SPeter Lieven * calculations otherwise */ 3916bf01c179SDaniel P. Berrange if (!vd->lossy) { 3917bf01c179SDaniel P. Berrange vd->non_adaptive = true; 3918e22492d3SPeter Lieven } 3919e22492d3SPeter Lieven 39203e305e4aSDaniel P. Berrange if (acl) { 3921bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3922bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup("vnc.x509dname"); 3923c8496408SGerd Hoffmann } else { 3924bf01c179SDaniel P. Berrange vd->tlsaclname = g_strdup_printf("vnc.%s.x509dname", vd->id); 3925c8496408SGerd Hoffmann } 3926bf01c179SDaniel P. Berrange qemu_acl_init(vd->tlsaclname); 39273e230dd2SCorentin Chary } 39283e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39293e230dd2SCorentin Chary if (acl && sasl) { 3930c8496408SGerd Hoffmann char *aclname; 3931c8496408SGerd Hoffmann 3932bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3933c8496408SGerd Hoffmann aclname = g_strdup("vnc.username"); 3934c8496408SGerd Hoffmann } else { 3935bf01c179SDaniel P. Berrange aclname = g_strdup_printf("vnc.%s.username", vd->id); 3936c8496408SGerd Hoffmann } 3937bf01c179SDaniel P. Berrange vd->sasl.acl = qemu_acl_init(aclname); 3938c8496408SGerd Hoffmann g_free(aclname); 39393e230dd2SCorentin Chary } 39403e230dd2SCorentin Chary #endif 39413e230dd2SCorentin Chary 3942eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->auth, &vd->subauth, 3943eda24e18SDaniel P. Berrange vd->tlscreds, password, 3944eda24e18SDaniel P. Berrange sasl, false, errp) < 0) { 3945eda24e18SDaniel P. Berrange goto fail; 3946eda24e18SDaniel P. Berrange } 39477364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth); 3948eda24e18SDaniel P. Berrange 3949eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, 3950eda24e18SDaniel P. Berrange vd->tlscreds, password, 3951eda24e18SDaniel P. Berrange sasl, true, errp) < 0) { 39523e305e4aSDaniel P. Berrange goto fail; 39533e305e4aSDaniel P. Berrange } 39547364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth); 39553e230dd2SCorentin Chary 39563e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 39573e230dd2SCorentin Chary if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) { 39582d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 39593e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 39601ce52c78SPaolo Bonzini goto fail; 39613e230dd2SCorentin Chary } 39623e230dd2SCorentin Chary #endif 3963bf01c179SDaniel P. Berrange vd->lock_key_sync = lock_key_sync; 3964a54f0d2bSPierre Ossman if (lock_key_sync) { 3965a54f0d2bSPierre Ossman vd->led = qemu_add_led_event_handler(kbd_leds, vd); 3966a54f0d2bSPierre Ossman } 3967a54f0d2bSPierre Ossman vd->ledstate = 0; 3968bf01c179SDaniel P. Berrange vd->key_delay_ms = key_delay_ms; 39693e230dd2SCorentin Chary 39701d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 39711d0d59feSGerd Hoffmann if (device_id) { 39721d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3973f2c1d54cSGerd Hoffmann Error *err = NULL; 39741d0d59feSGerd Hoffmann 3975f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3976f2c1d54cSGerd Hoffmann if (err) { 3977f2c1d54cSGerd Hoffmann error_propagate(errp, err); 39781d0d59feSGerd Hoffmann goto fail; 39791d0d59feSGerd Hoffmann } 39801d0d59feSGerd Hoffmann } else { 39811d0d59feSGerd Hoffmann con = NULL; 39821d0d59feSGerd Hoffmann } 39831d0d59feSGerd Hoffmann 3984bf01c179SDaniel P. Berrange if (con != vd->dcl.con) { 3985bf01c179SDaniel P. Berrange unregister_displaychangelistener(&vd->dcl); 3986bf01c179SDaniel P. Berrange vd->dcl.con = con; 3987bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 39881d0d59feSGerd Hoffmann } 39891d0d59feSGerd Hoffmann 3990fa03cb7fSMarc-André Lureau if (saddr == NULL) { 3991fa03cb7fSMarc-André Lureau goto cleanup; 3992fa03cb7fSMarc-André Lureau } 3993fa03cb7fSMarc-André Lureau 39943e230dd2SCorentin Chary if (reverse) { 3995396f935aSDaniel P. Berrange if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 3996e0d03b8cSDaniel P. Berrange goto fail; 39973e230dd2SCorentin Chary } 39983e230dd2SCorentin Chary } else { 3999396f935aSDaniel P. Berrange if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 40003d00ac1aSCole Robinson goto fail; 40013d00ac1aSCole Robinson } 40023e230dd2SCorentin Chary } 4003e0d03b8cSDaniel P. Berrange 4004275e0d61SDaniel P. Berrange if (qemu_opt_get(opts, "to")) { 4005bf01c179SDaniel P. Berrange vnc_display_print_local_addr(vd); 400633df7bf3SPaolo Bonzini } 400733df7bf3SPaolo Bonzini 4008396f935aSDaniel P. Berrange cleanup: 40099f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&saddr, &nsaddr); 40109f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&wsaddr, &nwsaddr); 40112d55f0e8SPaolo Bonzini return; 40121ce52c78SPaolo Bonzini 40131ce52c78SPaolo Bonzini fail: 40144ee74fa7SDaniel P. Berrange vnc_display_close(vd); 4015396f935aSDaniel P. Berrange goto cleanup; 40163e230dd2SCorentin Chary } 401713661089SDaniel P. Berrange 401814f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 401913661089SDaniel P. Berrange { 4020bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 402104d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 402213661089SDaniel P. Berrange 4023bf01c179SDaniel P. Berrange if (!vd) { 4024d616ccc5SGerd Hoffmann return; 4025d616ccc5SGerd Hoffmann } 402604d2529dSDaniel P. Berrange 402704d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 402804d2529dSDaniel P. Berrange if (sioc) { 402910bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); 4030bf01c179SDaniel P. Berrange vnc_connect(vd, sioc, skipauth, false); 403104d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 403204d2529dSDaniel P. Berrange } 403313661089SDaniel P. Berrange } 40344db14629SGerd Hoffmann 40359634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 40362779672fSGonglei { 40372779672fSGonglei int i = 2; 40382779672fSGonglei char *id; 40392779672fSGonglei 40402779672fSGonglei id = g_strdup("default"); 40412779672fSGonglei while (qemu_opts_find(olist, id)) { 40422779672fSGonglei g_free(id); 40432779672fSGonglei id = g_strdup_printf("vnc%d", i++); 40442779672fSGonglei } 40452779672fSGonglei qemu_opts_set_id(opts, id); 40462779672fSGonglei } 40472779672fSGonglei 404870b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 40494db14629SGerd Hoffmann { 40504db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 405170b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 405281607cbfSGonglei const char *id; 40534db14629SGerd Hoffmann 405481607cbfSGonglei if (!opts) { 405581607cbfSGonglei return NULL; 405681607cbfSGonglei } 405781607cbfSGonglei 405881607cbfSGonglei id = qemu_opts_id(opts); 40594db14629SGerd Hoffmann if (!id) { 40604db14629SGerd Hoffmann /* auto-assign id if not present */ 40612779672fSGonglei vnc_auto_assign_id(olist, opts); 40629634f4e3SGerd Hoffmann } 40639634f4e3SGerd Hoffmann return opts; 40644db14629SGerd Hoffmann } 40654db14629SGerd Hoffmann 406628d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 40679634f4e3SGerd Hoffmann { 40689634f4e3SGerd Hoffmann Error *local_err = NULL; 40699634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 40709634f4e3SGerd Hoffmann 40719634f4e3SGerd Hoffmann assert(id); 40724db14629SGerd Hoffmann vnc_display_init(id); 40734db14629SGerd Hoffmann vnc_display_open(id, &local_err); 40744db14629SGerd Hoffmann if (local_err != NULL) { 4075c29b77f9SMarkus Armbruster error_reportf_err(local_err, "Failed to start VNC server: "); 40764db14629SGerd Hoffmann exit(1); 40774db14629SGerd Hoffmann } 40784db14629SGerd Hoffmann return 0; 40794db14629SGerd Hoffmann } 40804db14629SGerd Hoffmann 40814db14629SGerd Hoffmann static void vnc_register_config(void) 40824db14629SGerd Hoffmann { 40834db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 40844db14629SGerd Hoffmann } 408534294e2fSEduardo Habkost opts_init(vnc_register_config); 4086